How to call a setter function in a smart contract which is deployed on ropsten testnet using infura

Viewed 544

I want to set a value by calling a smart contract function. The Smart contract is deployed on Ropsten Testnet. I am using Infura instead running a node.

I already read that .send() is not supported by Infura. So which options do I have?

Here is my code:

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

I am getting the following error: Error: Returned error: The method eth_sendTransaction does not exist/is not available

Please help.

1 Answers

Calling a method using Infura as a provider requires you to either send a rawTransaction or sign it before sending.

If you're using truffle, you can use @truffle/hdwallet-provider to sign transactions

Here's a code snippet that should solve your issue

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