【问题标题】:How to call a setter function in a smart contract which is deployed on ropsten testnet using infura如何在使用 infura 部署在 ropsten 测试网上的智能合约中调用 setter 函数
【发布时间】:2020-12-13 03:39:57
【问题描述】:

我想通过调用智能合约函数来设置一个值。智能合约部署在 Ropsten 测试网上。我正在使用 Infura 而不是运行节点。

我已经读到 Infura 不支持 .send()。那么我有哪些选择?

这是我的代码:

web3 = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/v3/xxxxxxxxxxxxxxxxxxxxx'));
const abi = PrinterMarketplace;
const contractAddress = '0xa498b78b32755xxxxxxxxxxxxxxf3101a1b92'        
contract = await new web3.eth.Contract(
            abi,
            contractAddress);
contract.methods.setOffer(offerprice, fileHash, client, account).send({ from: account, gas: 3000000 })

我收到以下错误:错误:返回错误:方法 eth_sendTransaction 不存在/不可用

请帮忙。

【问题讨论】:

    标签: javascript node.js ethereum web3 web3js


    【解决方案1】:

    使用 Infura 作为提供者调用方法需要您发送 rawTransaction 或在发送前对其进行签名。

    如果您使用的是 truffle,可以使用@truffle/hdwallet-provider 签署交易

    这是一个可以解决您的问题的代码 sn-p

    const Web3 = require('web3')
    const HDWallet = require('@truffle/hdwallet-provider')
    
    const abi = PrinterMarketplace;
    const contractAddress = '0xa498b78b32755xxxxxxxxxxxxxxf3101a1b92'
    
    const web3 = new Web3(new HDWallet('YOUR_PRIVATE_KEY', 'INFURA_ROPSTEN_URL'))
    
    const yourContract = new web3.eth.Contract(abi, contractAddress)
    
    yourContract.methods
        .setOffer(offerprice, fileHash, client, account)
        .send({ from: account, gas: 3000000 })
        .on('confirmation', (confirmations, receipt) => {
          console.log('CONFIRMATION');
          console.log(confirmations);
          console.log(receipt);
        })
        .on('error', (error, receipt, confirmations) => {
          console.log('ERROR');
          console.log(error);
          console.log(receipt);
          console.log(confirmations);
        })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-10
      • 2020-11-14
      • 2019-06-15
      • 2020-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多