Skip to main content
Share Your Experience: Take the 2024 Developer Survey
17 votes
Accepted

How to read a private variable from a contract?

If you know the location in storage, you can use web3 eth.getStorageAt(address, location) The variables are generally sequential, the first variable declared is in position 0, the second in ...
Tjaden Hess's user avatar
  • 37.1k
16 votes

How do I get the storage indices/keys?

You need to know the key, and then storage indices are based on keccak256 of the key. There are further details depending on mapping and dynamically-sized array types and see: https://solidity....
eth's user avatar
  • 85.9k
15 votes
Accepted

What does web3.eth.getStorageAt() return?

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 ...
Roland Kofler's user avatar
6 votes
Accepted

Need help understanding — web3.eth.getStorageAt

An index will only retrieve a single storage slot which is 32 bytes. y is a dynamically sized array, so it will not be at index 1. The information you are looking for is: https://solidity....
eth's user avatar
  • 85.9k
6 votes
Accepted

web3.eth.getStorageAt for mapping

balances is public, you get the balance from there. About storage: The address is not the key (although there is a story about this particular issue: https://www.pivotaltracker.com/story/show/...
Andreas Olofsson's user avatar
5 votes

web3.eth.getStorageAt for mapping

Retrieving via contract Not sure this is the answer you are looking for, but you can write a method which returns an array. Calling (and not transacting with) the contract is right now free and in ...
axic's user avatar
  • 2,569
5 votes

Accessing to a dynamically sized array value in a contract

The issue is that the RPC keccak is actually slightly different than the one used in Solidiy. To get hashes consistent with solidity, use the ethereumjs-abi library. For example: var abi = require('...
Tjaden Hess's user avatar
  • 37.1k
4 votes

Accessing storage of a map of a map

To do it that way, you must know what to look for, meaning that you should know beforehand some combinations of tokens addresses to compute the pair address for. Or, as @longhuynh suggested, you could ...
hroussille's user avatar
  • 7,721
3 votes

What should be the input for getStorageAt function in web3 js?

https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethgetstorageat web3.eth.getStorageAt(addressHexString, position ... For addressHexString you will want a contract address, since random ...
eth's user avatar
  • 85.9k
3 votes
Accepted

getStorageAt: What if two transactions in the same block?

web3.eth.getStorageAt will retrieve the value after all transactions in the block are executed in order: effectively the last transaction (but that transaction could set the value based on earlier ...
eth's user avatar
  • 85.9k
3 votes
Accepted

Read private large string

You might be interested in reading about the storage layout of dynamic arrays here. Long story short, your string "occupies" slot 0, but this slot only contains the string size. The data ...
hroussille's user avatar
  • 7,721
2 votes
Accepted

Modify storage in Ganache or Hardhat Network?

You can use hardhat_setStorageAt: await ethers.provider.send("hardhat_setStorageAt", [contractAddress, storageSlot, newValue]) See examples at https://github.com/mixbytes/hardhat-modify-...
Konstantin's user avatar
1 vote

Accessing storage of a map of a map

I don't think it is possible to iterate through the value of a mapping, unless you store an array of keys for that mapping. Smart contract's storage layout for dynamic mapping is quite complicated (...
longhuynh's user avatar
  • 121
1 vote

Accessing to a dynamically sized array value in a contract

Actually if your hashing algorithm gives you c89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6 when hashing 1 then it is the correct algorithm. It's just that you must encode the value ...
nyg's user avatar
  • 111
1 vote
Accepted

how to access multiple values using getStorageAt?

I see 2 things: to get the other values, you need to look at another index in memory. Like so web3.eth.getStorageAt(mycontractaddress, 1, startBlock-i); and web3.eth.getStorageAt(mycontractaddress, 2,...
Xavier Leprêtre B9lab's user avatar
1 vote

Modify storage in Ganache or Hardhat Network?

Even if it might sound like a good idea at first, it really isn't as it would tie this part of your testing suite to that specific software. Which in turn can lead to discrepancies and issues that ...
Micky Socaci's user avatar
  • 1,312
1 vote

How to download the blockchain of a single ethereum contract’s address?

I'm not sure if some client supports this but at least in theory you could start downloading the blockchain only from a certain block number. As the contract (most likely) won't have transactions into ...
Lauri Peltonen's user avatar
1 vote

Most efficient way to get all non-zero storage locations for a smart contract?

You can use an ethereum decompiler like https://github.com/palkeo/panoramix to find these slots for you. I came up against this issue recently. Normally storage slots are stored sequentially, but for ...
stepsal's user avatar
  • 11
1 vote
Accepted

Web3 behaves different on the client and server with different providers

After fixing up the syntax, both examples work for me. Solidity code I used, deployed to ganache: pragma solidity ^0.4.20; contract Test { uint256 a; uint256 b; uint256 c; uint256 d;...
user19510's user avatar
  • 28.1k
1 vote

How can I query data from contract storage?

There is no query api like SQL, you can invoke contract method to finish it. This is a simple contract, you can get balance by name pragma solidity ^0.4.0; contract Wallet { mapping (address =&...
BinGoBinBin's user avatar
  • 2,161
1 vote

How to read a private variable from a contract?

With web3.eth.getStorageAt you can read the complete storage of any contract externally (off-chain). But I want to make it clear - it is not as simple, as calculating the slots location could be ...
tenbits's user avatar
  • 2,438
1 vote

What does web3.eth.getStorageAt() return?

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....
UnknownUser's user avatar
1 vote

web3.eth.getStorageAt for mapping

This medium article explains very well how to access a mapping, see section index 5 — uints1: Mappings have a different indexation and should be read in other way. To read mapping value you should ...
jotud's user avatar
  • 330

Only top scored, non community-wiki answers of a minimum length are eligible