I deployed the contract on my private geth network for testing.

pragma solidity ^0.4.26;

contract MyContract {
    mapping (address => uint) private balances;

    function depositFunds() public payable {
        balances[msg.sender] += msg.value;

I called the depositFunds function and ETH was transferred.

> myinstance.depositFunds({from:'0x2476b5bf267aba5c9b4bff499863e8ae5e39564c', value: 13333})
> eth.getBalance(myinstance.address)

However, the state of the contract has not changed.

> eth.getStorageAt(myinstance.address, 0)

Where is the mistake?

  • I am not sure but could you try this --> eth.getStorageAt(Mycontract.address, 0) I think storage changed for your's contract address, not your account that received ETH
    – Sky
    Feb 23, 2022 at 11:24
  • Isn't the contract address permanent? Feb 23, 2022 at 11:32
  • 2
    You are just reading the wrong storage slot. Please read this to learn about mappings layout in contract storage : ethereum.stackexchange.com/questions/115382/… . You have to read slot keccak256(h(k) . p) where h(k) is the address you want to read in balances padded to 32 bytes and p is the value 0 (for the slot of your mapping declaration : storage slot 0)
    – hroussille
    Feb 23, 2022 at 11:51
  • @hroussille, please correct me if I am wrong. If the contract had only one variable of type value, then the method above would return the value of the variable, correct? I did it once (getStorage(address, 0)) and it returned me the value of the variable, as expected. Feb 23, 2022 at 14:36
  • You are correct, but mappings are not value types, they are reference types. Please see my answer explaining storage layout of mappings and how to access them through getStorageAt (ethereum.stackexchange.com/questions/114186/…) , Long story short, you need to read storage slot 0xd1552d9b9a211afb39ba81aeb6779bf0f57a902c6da8b4bc43342834917cbe70 tell me if I did a mistake of if something is not clear enough from the answer I linked
    – hroussille
    Feb 23, 2022 at 17:07


Your Answer

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