本文介绍了传输BEP20令牌时发件人无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我想使用ankr API和Web3提供程序将bep20内标识从一个地址转移到另一个地址,但收到错误,如
这是我用来传输令牌的代码,我已经对BNB、BSC BINNCE智能链主机等二进制地址使用了chaind id
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) => {
参见readme中的示例(查找const signedTx
代码段)
这篇关于传输BEP20令牌时发件人无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!