I
I
Iangyl2021-10-05 15:17:53
Node.js
Iangyl, 2021-10-05 15:17:53

SendSignedTransaction Returned error: invalid sender, how to fix?

I try to create interaction with smart-contract through back-end.
I wrote this script from the tutorial: click me .
Tell me please, why I get an error from sending my signed transaction? And how can I fix it?

const Web3 = require('web3');
require('dotenv').config();
const MyContract = require('./build/contracts/HelloWorld.json');
const address = '0xD7cE788A5bA84b53932780DfCf98F7430ce5d3e1';
const private_key = process.env.PRIVATE_KEY;
const infuraUrl = process.env.INFURA_URL;

const init = async () => {
    var web3 = new Web3(new Web3.providers.HttpProvider(infuraUrl));

    const networkId = await web3.eth.net.getId();
    const myContract = new web3.eth.Contract(
        MyContract.abi,
        MyContract.networks[networkId].address
    );

    const tx = myContract.methods.update("LoremIpsum");
    const gas = await tx.estimateGas({from: address});
    const gasPrice = await web3.eth.getGasPrice();
    const data = tx.encodeABI();
    const nonce = await web3.eth.getTransactionCount(address);

    const signedTx = await web3.eth.accounts.signTransaction(
        {
            to: myContract.options.address,
            data,
            gas,
            gasPrice,
            nonce,
            chain: networkId,
            hardfork: 'newRule'
        },
        private_key
    ).catch(e => e.message);
    console.log(`Old data value ${await myContract.methods.message().call()}`);
    const receipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction).catch(e => e.message); //error
    console.log(`Transaction hash ${receipt}`);
    console.log(`New data value ${await myContract.methods.message().call()}`);
};

init();

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question