[英]Using ethereum.request to Retrieve Data from Smart Contract in JavaScript
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 )使用 javascript,我目前能够使用此代码(取自MetaMask 文档)将数据写入我在 Ethereum Rovan test.network 上构建的智能合约
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:
读取数据的方法的十六进制代码是 0x1f1bd692,所以我认为使用这些参数会起作用:
to: '0xacb241f59e1a8c7a61f0781aed7ad067269feb26',
from: account,
data: '0x1f1bd692',
Unfortunately, this just returns the transaction hash, not the data I want from the smart contract.不幸的是,这只会返回交易 hash,而不是我想要的智能合约数据。
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. params[] 中的第一个参数是合约的地址。 The second argument is the index of the item in storage you want to retrieve.
第二个参数是要检索的存储项目的索引。 It must be a hex number starting with '0x'.
它必须是以“0x”开头的十六进制数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.