我尝试使用web3js在ERC-20中批准+ transferFrom函数 我在contract.methods.approve
上发现了以下错误焦点 // File ctest.js
var Tx = require('ethereumjs-tx');
var fs = require('fs');
var path = require('path');
var CONWeb3 = require('web3');
var config = require('../etc/config.json');
var web3 = new CONWeb3("http://" + config.TKRPCSVR + ":" + config.RPCPORT ) ;
var coinbase = {} ;
coinbase.tokenApproval = async function (reqSendAdress,reqApproveSend , reqTokenContract ,sendCrypt, approveAmonut , callback) {
var myAddress = reqSendAdress ;
var _spender = reqApproveSend ;
var _value = approveAmonut ;
var count = await web3.eth.getTransactionCount(myAddress);
var abiArray = JSON.parse(fs.readFileSync(path.resolve(__dirname, './erc20_abi.json'), 'utf-8'));
var contractAddress = reqTokenContract ;
var contract = new web3.eth.Contract(abiArray, contractAddress, {
from: myAddress
});
var gasPriceGwei = 6;
var gasLimit = 900000;
var chainId = 9559;
var rawTransaction = {
"from": myAddress,
"nonce": "0x" + count.toString(16),
"gasPrice": web3.utils.toHex(gasPriceGwei * 1e9),
"gasLimit": web3.utils.toHex(gasLimit),
"to": contractAddress,
"value": "0x0",
/ * ****在行下方产生错误**** 它不起作用似乎是我错过了一些。或任何人有更好的方法 修复此* /
"data": contract.methods.approve(_spender, _value).encodeABI(),
"chainId": chainId
};
var privKey = new Buffer( sendCrypt , 'hex');
var tx = new Tx(rawTransaction);
tx.sign(privKey);
var serializedTx = tx.serialize();
try {
var receipt = await web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'));
}
catch (err) {
callback( "send error " + err , null );
return false ;
}
callback ( null , receipt ) ;
balance = await contract.methods.balanceOf(myAddress).call();
} ///
exports.coinbase=coinbase
END //方法
run > node caller.js
var coinFunc = require("./ctest") ;
coinFunc.coinbase.tokenApproval (
"0xd1dc78f2865a7288a4a4b84272aaa5316cxxxxb" ,
"0x82378cf137F5dF4FA730529eB79a4583EA6xxxx" ,
"0xa70a8d416c22e5817323f2cebf233d929exxxx1" ,
"87b9fd1caa5e822063c4cd7e812edc0cd44xxxxb2ee0cdda8277a2d8ee" ,
"1000" ,
function ( err , res ){
if (err) console.log("\n\n ================ERROR================\n\n" + err) ;
}) ;
=====错误出现如下====
------------------------
Attempting to send signed tx: f8ac04850165a0bc00830dbba094a70a8d416c22e5817323f2cebf233d929e5722a180b844095ea7b300000000000000000000000082378cf137f5df4fa730529eb79a4583ea605fa900000000000000000000000000000000000000000000000000000000000003e8824ad2a0218213f4bdb07bde4e8fe3b7822d8cdc8228fbaae1eca6f541f4015f0403d296a0230dd25d52baf93085c5ec94cce16e60cf836f3c35ec7748b9e36c266c38163a
------------------------
++++++++++++ Send Error +++++++++++++++
Error: Returned error: known transaction: de721a1a73f1ea63d5597a75e47fbbfcd2fe4142fd81bdcca37892ddb4276c4a
at Object.ErrorResponse (/opt/node2/Eth/node_modules/web3-core-helpers/src/errors.js:29:16)
at /opt/node2/Eth/node_modules/web3-core-