简体   繁体   English

在 JavaScript 中使用 ethereum.request 从智能合约中检索数据

[英]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.

相关问题 如何使用 nodejs 上的 Web3js 解码在以太坊上调用智能合约的响应 - How to decode response from calling smart contract on Ethereum using Web3js on nodejs 如何从 Javascript 向 Solidity 智能合约 function 输入数据? - How to input data to a solidity smart contract function from Javascript? ethereum.request 中的“Personal_Sign”问题 - “无效参数:必须提供以太坊地址。” - Problem with "Personal_Sign" in ethereum.request - "Invalid parameters: must provide an Ethereum address." 来自以太坊的运营商合同_; - Operator from Ethereum contract _; 没有为以太坊智能合约生成事件日志 - Event log is not generated for Ethereum smart contract Web3.js 和 MetaMask ethereum.request:是否可以指定用于支付的特定加密/令牌? - Web3.js and MetaMask ethereum.request: is it possible to specify specific crypto/token used to pay? 在 JS 循环内调用以太坊智能合约函数不同步 - Calling Ethereum Smart contract function inside a JS loop is not synchronize 如何从JavaScript中的发布请求中检索数据 - How to retrieve data from post request in javascript 如何通过来自智能合约的数据 map - How to map through data coming from smart contract 我无法将数据从智能合约提取到 HTML - I can't draw data from smart contract to HTML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM