发送方在传输BEP20令牌时无效



你好,我想把bep20令牌一个地址转移到另一个地址,使用ankr api和web3的提供者,但得到错误,如

无效发送者

这是我用于转移令牌的代码,我已经为binanace使用了chainid,如bnb, bsc binance智能链主网

let tokenAddress = '0x41XXXXXXXXXXXXXXXXXXXXXXXXXXXXXbce' //  contract address

let toAddress = req.params.toaddress // where to send it
let fromAddress = req.params.fromaddress // your wallet
let pKey = req.params.privatekey.split('0x');
let privateKey = Buffer.from(pKey[1],'hex');

let contractABI = [
// transfer
{
'constant': false,
'inputs': [
{
'name': '_to',
'type': 'address'
},
{
'name': '_value',
'type': 'uint256'
}
],
'name': 'transfer',
'outputs': [
{
'name': '',
'type': 'bool'
}
],
'type': 'function'
}
]
let contract = new web32.eth.Contract(contractABI, tokenAddress, {from: fromAddress})
// 1e18 === 1 HST
let amount1 = req.params.amount;
let amount = web32.utils.toBN(amount1*1000000000000000000)
// console.log(amount);
web32.eth.getTransactionCount(fromAddress)
.then((count) => {
let rawTransaction = {
'from': fromAddress,
'gasPrice': web32.utils.toHex(20 * 1e9),
'gasLimit': web32.utils.toHex(210000),
'to': tokenAddress,
'value': 0x0,
'data': contract.methods.transfer(toAddress, amount).encodeABI(),
'nonce': web32.utils.toHex(count),
"chainId": 56
}
let transaction = new EthereumTx(rawTransaction,{chain:'bsc'})
transaction.sign(privateKey)
web32.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'),(err,hash) => {
console.log(err)
var url = "https://etherscan.io/tx/"+hash
res.json({hash: hash, status_url: url,success:'true',message : 'Transaction has been pushed.'});
})

})
transaction.sign(privateKey)
web32.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'),(err,hash) => {

您正在传递未签名的事务,需要传递已签名的事务。

const signedTx = transaction.sign(privateKey)
web32.eth.sendSignedTransaction('0x' + signedTx.serialize().toString('hex'),(err,hash) => {

请参阅自述文件中的示例(查找const signedTx片段)