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.

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

| improve this answer | |
  • Thanks a lot. Your answer and link cleared most of my doubts. – krritik Sep 3 at 14:34
  • Please mark the answer as accepted if it helped you out. – Morten Sep 3 at 15:20

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.