Skip to content

[question] How to convert address/bignumber to hex string? #1079

Closed
@shanecarey17

Description

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?

index = '0000000000000000000000000000000000000000000000000000000000000005'
key =  '00000000000000000000000xbccc714d56bc0da0fd33d96d2a87b680dd6d0df6'
let newKey =  web3.sha3(key + index, {"encoding":"hex"})
console.log(web3.eth.getStorageAt(contractAddress, newKey))
console.log('DEC: ' + web3.toDecimal(web3.eth.getStorageAt(contractAddress, newKey)))

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):

let key = ethers.getAddress('0x...');
let offset = ethers.BigNumber.from(5);
let mappingKey = ethers.utils.keccak256([key, offset]);
let mappingData = await ethers.provider.getStorageAt(contractAddress, mappingKey, blockNumber);

Specifically, how does one convert the key and offset to appropriately sized hex strings for the hash function? Thanks in advance!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    discussionQuestions, feedback and general information.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions