Using javascript, I am currently able to write data to a smart contract I built on the Ethereum Rovan test network using this code (taken from the MetaMask docs)
const transactionParameters = {
to: '0xacb241f59e1a8c7a61f0781aed7ad067269feb26',
from: account,
data: '0xfcc74f71aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
};
const txHash = await ethereum.request({
method: 'eth_sendTransaction',
params: [ transactionParameters ],
});
However, I am not able to read data. How can I do this? The hex code of the method for reading data is 0x1f1bd692, so I thought using these parameters would work:
to: '0xacb241f59e1a8c7a61f0781aed7ad067269feb26',
from: account,
data: '0x1f1bd692',
Unfortunately, this just returns the transaction hash, not the data I want from the smart contract.
Note: If possible, please do not suggest any libraries.
Here is the solution:
const data = await ethereum.request({
method: 'eth_getStorageAt',
params: [ '0xacb241f59e1a8c7a61f0781aed7ad067269feb26', '0x0' ],
})
The first argument in params[] is the address of the contract. The second argument is the index of the item in storage you want to retrieve. It must be a hex number starting with '0x'.