如何在智能合约中调用infura部署在ropsten testnet上的智能合约中的setter函数

时间:2020-08-24 13:28:56

标签: javascript node.js ethereum web3 web3js

我想通过调用智能合约函数来设置值。智能合约已部署在Ropsten Testnet上。我使用Infura而不是运行节点。

我已经读到Insura不支持.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方法不存在/不可用

请帮助。

1 个答案:

答案 0 :(得分:0)

调用将Infura用作提供程序的方法需要您发送rawTransaction或对其进行签名,然后再发送。

如果您正在使用松露,则可以使用@truffle/hdwallet-provider签署交易

这是一个应解决您的问题的代码段

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);
    })