[question] How to convert address/bignumber to hex string?
See original GitHub issueHello, 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!
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Convert big number (hex value) to a string or number
Try this const theUsedAccount = await provider.send("eth_requestAccounts", [0]); const balance = await provider.
Read more >Truffle CMD Bignumber to number; hex to string
I tried instance.getAge().toNumber() but that throws an error saying that toNumber isn't defined. I checked How to convert BigNumber to Number ...
Read more >BigNumber - ethers
A HexString or a decimal string, either of which may be negative. ... The BigNumber class is immutable, so no operations can change...
Read more >Big number converter - Mobilefish.com
Big number converter. This service allows you to convert big positive integer numbers into binary, decimal, hexadecimal or base64 encoding schemes.
Read more >web3.utils — web3.js 1.0.0 documentation
For safe conversion of many types, incl BigNumber.js use utils.toBN. Parameters¶. mixed - String|Number : A number, number string or HEX string to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub 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.
I hope that helps! Let me know if you have any other questions. 😃
Ah! Your solution worked perfectly, I was dealing with parsing the return of
getStorageAt
but was confused because the stack trace did not show which function I had called. Obviously this is because its an async function, but I hadnt realized. Thanks!