16

I want to know how this works: web3.eth.getStorageAt(addressHexString, position [, defaultBlock] [, callback])

What data we can store at any address and how? And what will this eth API return?

1

3 Answers 3

15

Each contract consists of a EVM bytecode handling the execution and a storage to save the state of the contract. This is a low level function to get the state of the contract's storage. The storage is essentially a key/value store.
The function returns the value the contracts storage has at a certain position.

Refer to the GitHub wiki pages.

More on storage here.

1

As of version 1.0 of web3.js, the web3.utils.hexToAscii function should be used. Correct syntax:

web3.eth.getStorageAt('/* Contract Address */', 0).then(result => {
  console.log(web3.utils.hexToAscii(result));
});
0

for instance if you look at this contract source and you type this in geth:

web3.toAscii(web3.eth.getStorageAt("0x3589d05a1ec4Af9f65b0E5554e645707775Ee43C",1))

it returned the following

"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00WEEDMAPS"
1
  • Would you be able to also provide an example for getting the address of a dynamic array? i've tried with web3.sha3("0x01") to calculate the address but can't figure it out.
    – slothbag
    Commented Jun 27, 2016 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.