错误:仅允许通过 RPC 进行重放保护 (EIP-155) 事务

时间:2021-05-31 12:56:46

标签: node.js token web3 cryptocurrency

当我尝试从一个代币地址向另一个地址发送资金时,我遇到了这个错误:只允许通过 RPC 进行重放保护 (EIP-155) 交易

我的代码:

const contract = new web3.eth.Contract(ABI,token_contract_address);

    const data = contract.methods.transfer(to, req.body.value).encodeABI();

    const rawTransaction = {
        'from': from,
        'nonce': web3.utils.toHex(web3.eth.getTransactionCount(from)),
        'gasPrice': web3.utils.toHex(web3.eth.gasPrice),
        'gasLimit': web3.utils.toHex(21000),
        'to': token_contract_address,
        'value': 0,
        'data': data,
        'chainId': web3.utils.toHex(chainid)
    };

    const privateKey = new Buffer.from(req.body.PrivateKey, 'hex');
    const tx = new Tx(rawTransaction);
    tx.sign(privateKey);

    const serializedTx = tx.serialize();
    web3.eth.sendSignedTransaction(('0x' + serializedTx.toString('hex')),req.body.PrivateKey) 
    .then(function (result) {
        res.statusCode = 200;
        res.setHeader('Content-Type', 'application/json');
        res.json(result*decimals);
        console.log(result);
    })
    .catch((err) => next(err));

注意我已经添加了 ChainId

0 个答案:

没有答案