0

I'm trying to read a mapping storage variable from blockchain. Below, are my attempts, from client and server. Testing on Rinkeby network.

From client: Metamask is installed and I'm trying from the browser console. Web3 version is 0.2.x.

const index = "0000000000000000000000000000000000000000000000000000000000000005";
const key = web3.toHex('my_key');

web3.eth.getStorageAt(
  'contract_address_here',
   web3.sha3(key + index, { encoding: 'hex' }),
   (err, result) => {
        console.log(result); // Returns 0x00.....1; // Key exists
   }
);

It works. The key does exist on the Rinkeby network.

Attempt from server: Please note, that web3 version is 1.0.0-beta.26 and I'm using Infura as the provider.

// Init web3
const Web3 = require('web3');
const provider = new Web3.providers.HttpProvider(
  "my rinkeby infura link here"
);
const web3 = new Web3(provider); 

const index = "0000000000000000000000000000000000000000000000000000000000000005";
const key = web3.utils.toHex('my_key');

web3.eth.getStorageAt(
  'contract_address_here',
  web3.utils.sha3(key + index, { encoding: 'hex' }),
  (err, result) => {
    console.log(result); On the server, the result is always 0x...0
  }
);

Does anyone know what is going on here? I'm making double sure, the Contract address is the one that is on Rinkeby and also my Infura link is one from Rinkeby.

Edit: 1. Looks like the web3 version is the problem. If I try to use web3 0.2.x on the server and hook it up with Infura provider, it works well.

  • Your syntax looks broken for both. Are you sure that's the exact code you are/were running? – user19510 Mar 2 '18 at 20:11
  • Which part of the syntax is broken? I don't see the interpreter throwing any error for either. – InfinitePrime Mar 2 '18 at 20:12
  • For the web3.js 0.2x.x version, there's a missing curly brace. I also can't get the following syntax to work, but maybe you use a transpiler that understands it? () => (err, result) { It seems to me that would be a callback that takes no arguments and returns a function that takes two arguments? (But it's missing a fat arrow if that's the case.) – user19510 Mar 2 '18 at 20:16
  • There's also a non-comment comment in the 1.0 example, but I assume that was added after you pasted into Stack Exchange. – user19510 Mar 2 '18 at 20:17
  • I posted the (working) code I ran as an answer. – user19510 Mar 2 '18 at 20:20
1

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;
    uint256 e;
    mapping (string => bool) exists;

    function Test() public {
        exists["my_key"] = true;
    }
}

web3.js 0.2x.x code I used, in the browser with MetaMask pointing at ganache:

var index = "0000000000000000000000000000000000000000000000000000000000000005";
var key = web3.toHex('my_key');

web3.eth.getStorageAt(
  "0x6d533f202b5bece917de226d7d588480e0ba7c0e",
  web3.sha3(key+index, { encoding: 'hex' }),
  (err, result) => {
    console.log(result);
});

And here's the web3.js 1.0.0-beta code I used, also pointing at ganache:

const Web3 = require('web3');
const web3 = new Web3('http://localhost:8545');

var index = "0000000000000000000000000000000000000000000000000000000000000005";
var key = web3.utils.toHex('my_key');
web3.eth.getStorageAt(
  "0x6d533f202b5bece917de226d7d588480e0ba7c0e",
  web3.utils.sha3(key+index),
  (err, result) => {
    console.log(result);
  });

(The second parameter you have for web3.utils.sha3 in the 1.0 beta code is harmless but ignored. The code works with or without it. Unlike its 0.2x.x counterpart, web3.utils.sha3 interprets its first argument as hex if it starts with 0x.)

EDIT

Your key looks like hex already, so web3.utils.toHex isn't transforming it the same way web3.toHex in 0.2x.x is. You should probably be using fromAscii in both cases, assuming you do indeed want to treat the key as a string. This code works with 1.0.0-beta26:

const Web3 = require('web3');
const web3 = new Web3('https://rinkeby.infura.io/<key redacted>');

var index = "0000000000000000000000000000000000000000000000000000000000000005";
var key = web3.utils.fromAscii('708d343ff29ab3350d9a9499d7db1cfefc919bce');
web3.eth.getStorageAt(
  "0x670c3ee20d857ECD9A1C6024a2701443C3F7f2e2",
  web3.utils.sha3(key+index),
  (err, result) => {
    console.log(result);
  });
  • No actually. It is not working. I might have made an error while editing syntax, here on the forum. If it helps, I'm using "web3": "1.0.0-beta.26" – InfinitePrime Mar 2 '18 at 20:48
  • I fixed the editing. Just to make sure, I double checked the syntax. It still does not work. Could you give it a try from 1.0.0-beta.26 web3? – InfinitePrime Mar 2 '18 at 20:54
  • 1
    I'm on 1.0.0-beta.29. I can try downgrading. – user19510 Mar 2 '18 at 20:55
  • Same result for me on 1.0.0-beta.26. Perhaps you can share the contract address and key on Rinkeby so I can test against your actual contract. – user19510 Mar 2 '18 at 20:57
  • Contract address: 0x670c3ee20d857ECD9A1C6024a2701443C3F7f2e2 Key: 708d343ff29ab3350d9a9499d7db1cfefc919bce – InfinitePrime Mar 2 '18 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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