10 web3js sendSignedTransaction 报错:Error: Returned error: invalid sender

先贴代码:


import Web3 from 'web3';
import fs from 'fs'
import Tx from 'ethereumjs-tx'
var myAddress = "*************************";

var destAddress = "*****************";
var contractAddress = "*********************";
var agentAPIKEY = "************************"
var transferAmount = 10000 * 10 ** 18;
const web3 = new Web3("https://rpc-mumbai.maticvigil.com")
web3.eth.defaultAccount = myAddress
web3.eth.getChainId((err, chainid) => {
    if (err != null) {
        console.log('区块链ID:' + chainid + ' 错误信息:' + err)
        return
    }
    var abiArray = JSON.parse(fs.readFileSync('../json/erc20.json', 'utf-8'));
    const Contract = new web3.eth.Contract(
        abiArray,
        contractAddress,
        {
            from: myAddress,
        })

    const encodedABI = Contract.methods.transfer(destAddress, BigInt(transferAmount)).encodeABI()

    web3.eth.getTransactionCount(myAddress, 'pending', (error, txCount) => {
        if (!error) {
            const txObject = {
                chainId: web3.utils.toHex(chainid),
                nonce: web3.utils.toHex(txCount),
                from: myAddress,
                to: contractAddress,
                value: "0x00",
                gasLimit: web3.utils.toHex(210000),
                gasPrice: web3.utils.toHex(8),
                data: encodedABI
            }

            console.log('txObject = ', txObject)
            const tx = new Tx.Transaction(txObject)
            const privateKey = Buffer.from(agentAPIKEY, "hex")
            tx.sign(privateKey)
            const serializedTx = tx.serialize()

            web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'), (err, txHash) => {
                if (err) console.error('错误 = ', err)
                else console.log('txHash = ', txHash)
            })
        }
    })
})

运行报错:

Error: Returned error: invalid sender

有懂的朋友还望不吝赐教,谢谢。

请先 登录 后评论

最佳答案 2022-11-14 23:36

已经解决,是gasPrice的问题 理论上 gasPrice 单位应该是gwei, 上面代码里只传了8wei 所以不会被打包 改成 25 * 10 ** 9 就好了

请先 登录 后评论

其它 2 个回答

Tiny熊 - 布道者
  擅长:智能合约,以太坊
请先 登录 后评论
UTools - 链上工具 - 区块链工程师
请先 登录 后评论