A short while ago, I found a fantastic Medium article explaining Smart Contract storage.
The examples in this article have helped me to understand how to read a Smart Contract's storage values from the command line / console.
However, with the version 1.0.0 of web3js going into Beta - and the NPM version of web3js being installed by default being the 1.0.0 Beta version, I thought that I would upgrade my code using the information from the aforementioned article to the new library.
The problem is that I can't replicate the same behavior of web3.sha3 using the new version of the library; web3Utils.sha3.
To clarify, here are some examples - starting with the output that I get from the stable, but pre-V1.0.0 web3.sha3 function and then from the new, Beta version of the web3js library:
To give you some context, the test is based on accessing the mappings data in this contrived Smart Contract:
pragma solidity ^0.4.0;
contract StorageTest {
mapping(address => uint256) public storedData;
function addDataToMapping(address account, uint256 amount) public {
storedData[account] = amount;
}
}
The way that I determine how to access the mapping's data for the address 0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1
is by using the following pre-V1.0.0 web3js call:
web3.sha3("00000000000000000000000x90F8bf6A479f320ead074411a4B0e7944Ea8c9C10000000000000000000000000000000000000000000000000000000000000000", {"encoding":"hex"})
This results in the hash value
0xbbf27a261d686d37ad5c6299e09a73a2a829c9c10eaf9c5c19e6ff2488e52f20
Now, if we simply port this over to the V1.0.0 library then the code looks like this:
web3Utils.sha3("00000000000000000000000x90F8bf6A479f320ead074411a4B0e7944Ea8c9C10000000000000000000000000000000000000000000000000000000000000000", {"encoding":"hex"})
However, this results in the following (different) hash being output:
0x02d38ce98b8dae9d7cc32b64bb52580b4989cb56d07114b95037da4af5f500cf
I tried removing the {"encoding":"hex"}
parameter (because this isn't mentioned in the documentation:
web3Utils.sha3("00000000000000000000000x90F8bf6A479f320ead074411a4B0e7944Ea8c9C10000000000000000000000000000000000000000000000000000000000000000")
But unfortunately, I still get the same output:
0x02d38ce98b8dae9d7cc32b64bb52580b4989cb56d07114b95037da4af5f500cf
The documentation mentions a web3Utils.soliditySha3
function that sounded like it might do the trick, so I switched this function call in instead:
web3Utils.soliditySha3("00000000000000000000000x90F8bf6A479f320ead074411a4B0e7944Ea8c9C10000000000000000000000000000000000000000000000000000000000000000", {"encoding":"hex"})
This time I got a different value, but it is not the one that I am looking for:
0x0780d226e6d7ac5576d20e78b8c3a2122ace468d419adefef1e79c49d7f4116f
I also tried removing the {"encoding":"hex"}
parameter (because this also isn't mentioned in the documentation:
web3Utils.soliditySha3("00000000000000000000000x90F8bf6A479f320ead074411a4B0e7944Ea8c9C10000000000000000000000000000000000000000000000000000000000000000");
But this just results in the same, incorrect, value as the web3Utils.sha3
function calls:
0x02d38ce98b8dae9d7cc32b64bb52580b4989cb56d07114b95037da4af5f500cf
I have also tried various permutations of the "Any type, or an object with {type: 'uint', value: '123456'}" parameter format:
web3Utils.soliditySha3({type: 'string', value: "00000000000000000000000x90F8bf6A479f320ead074411a4B0e7944Ea8c9C10000000000000000000000000000000000000000000000000000000000000000"})
And yet I still can't get the correct value.
Does anyone know how to correctly address a mapping or struct storage address using the latest web3js library functions?