0

Currently, we can read the contract A data with the help of getters functions defined in contract A.
But I want to do something like contract A puts some data in ethereum blockchain but do not define the getters or other functions to access it. And then using another smart contract B, I want to access smart contract B data directly from blocks fo ethereum blockchain.

1 Answer 1

2

A smart contract can only access its own storage, you cannot read the storage of another contract directly, with Solidity. If you cannot make the variables public, or make a getter for the variables, you can use something like Web3.js or Ethers.js to read the contract storage, and send that data to your contract, e.g.:

import { JsonRpcProvider } from 'ethers';

const provider = new JsonRpcProvider('https://<URL of your node>'); // or use Infura, Etherscan, ...
const data = await provider.getStorageAt("<contract address>", <storage slot>);

You can read more about how the storage works here: https://solidity.readthedocs.io/en/v0.7.0/internals/layout_in_storage.html

1
  • Thanks a lot. Your answer and link cleared most of my doubts.
    – krritik
    Commented Sep 3, 2020 at 14:34

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.