我正在编写一个API来将我的自定义bep20合同令牌从所有者的帐户转移到另一个帐户。例如,我的钱包中有10,000个单位的令牌,我希望使用节点服务器内的web3js将任何指定的金额转移到另一个帐户。以下是我迄今所做的工作,而且工作不正常。我有Failed作为响应,也有Error: Transaction has been reverted by the EVM:
以下是我在节点中的令牌传输功能:
async transferToken(pkey, tokenContractAddress , toAddress , amount , chainId, callback) {
//import account by private key
let account = this.web3.eth.accounts.privateKeyToAccount(pkey);
let wallet = this.web3.eth.accounts.wallet.add(account);
// ABI to transfer ERC20 Token
let abi = ABI_CONFIG;
// calculate ERC20 token amount
let tokenAmount = this.web3.utils.toWei(amount.toString(), 'ether')
// Get ERC20 Token contract instance
let contract = new this.web3.eth.Contract(abi, tokenContractAddress, {from: wallet.address});
const data = await contract.methods.transfer(toAddress, tokenAmount).encodeABI();
// The gas price is determined by the last few blocks median gas price.
const gasPrice = await this.web3.eth.getGasPrice();
const gasLimit = 90000;
// Build a new transaction object.
const rawTransaction = {
'from': wallet.address,
'nonce': this.web3.utils.toHex(this.web3.eth.getTransactionCount(wallet.address)),
'gasPrice': this.web3.utils.toHex(gasPrice),
'gasLimit': this.web3.utils.toHex(gasLimit),
'to': tokenContractAddress,
'value': 0,
'data': data,
'chainId': chainId
};
const res = await contract.methods.transfer(toAddress, tokenAmount).send({
from: wallet.address,
gas: 30400
});
console.log(res);
// Get Name
let name = await contract.methods.name().call();
// Get Symbol
let symbol = await contract.methods.symbol().call();
/* send to hyperledger */
const map = {
"action_type" : "SEND_TOKEN",
"from_wallet_address" : wallet.address,
"to_wallet_address" : toAddress,
"amount" : this.web3.utils.toWei(amount.toString(), 'ether'),
"tx_hash" : res.transactionHash,
"gasLimit" : 21000,
"gasPrice" : gasPrice,
"fee" : gasPrice * 21000,
"token_smart_contract" : tokenContractAddress,
"token_name" : name,
"token_symbol" : symbol,
"status" : "SUCCESS"
}
callback({data: res, map});
}发布于 2022-03-12 06:03:41
没有足够的气体来完成操作,因为交易使用了30400中的大多数。例如,我认为你必须把汽油限额从30400提高到100000。
https://stackoverflow.com/questions/70847454
复制相似问题