-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[question] How to convert address/bignumber to hex string? #1079
Comments
Heya! You are on the right track. To accomplish the same thing in ethers, here is the demo re-written a bit, with more documentation added. const { BigNumber, ethers, utils } = require("ethers");
(async function() {
// Connect to Ropsten
const provider = ethers.getDefaultProvider("ropsten");
// The contract address
const addr = "0xf1f5896ace3a78c347eb7eab503450bc93bd0c3b";
// The key into the mapping which is located at the given Solidity slot
const key = "0xbccc714d56bc0da0fd33d96d2a87b680dd6d0df6";
const index = 5;
// The pre-image used to compute the Storage location
const newKeyPreimage = utils.concat([
// Mappings' keys in Solidity must all be word-aligned (32 bytes)
utils.hexZeroPad(key, 32),
// Similarly with the slot-index into the Solidity variable layout
utils.hexZeroPad(BigNumber.from(index).toHexString(), 32),
]);
console.log("New Key Preimage:", utils.hexlify(newKeyPreimage));
// "0x000000000000000000000000bccc714d56bc0da0fd33d96d2a87b680dd6d0df60000000000000000000000000000000000000000000000000000000000000005"
const newKey = utils.keccak256(newKeyPreimage);
console.log("New Key:", newKey);
// "0xafef6be2b419f4d69d56fe34788202bf06650015554457a2470181981bcce7ef"
const value = await provider.getStorageAt(addr, newKey);
console.log("Value:", value);
// "0x0000000000000000000000000000000000000000000000000000000000000058"
})(); I hope that helps! Let me know if you have any other questions. :) |
Thanks for the help! I am still getting the same error with the updated code:
The error is
I don't understand what the error is indicating. It looks like there is a fixed sized buffer somewhere. Is this because of my inputs? |
Also, |
Ah! Your solution worked perfectly, I was dealing with parsing the return of |
Glad to hear it! :) |
(I'll also be sure to export |
|
Hello, trying to implement the following tutorial in ethers https://medium.com/aigang-network/how-to-read-ethereum-contract-storage-44252c8af925 in order to load data from a "private" mapping.
With respect to the mapping example, how would the following code look in ethers?
I have tried the following which raises an error
EXCEPTION Error: data out-of-bounds (length=32, offset=64, code=BUFFER_OVERRUN, version=abi/5.0.5)
:Specifically, how does one convert the key and offset to appropriately sized hex strings for the hash function? Thanks in advance!
The text was updated successfully, but these errors were encountered: