首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未能传输BEP20智能合同令牌

未能传输BEP20智能合同令牌
EN

Stack Overflow用户
提问于 2022-01-25 10:56:46
回答 1查看 286关注 0票数 0

我正在编写一个API来将我的自定义bep20合同令牌从所有者的帐户转移到另一个帐户。例如,我的钱包中有10,000个单位的令牌,我希望使用节点服务器内的web3js将任何指定的金额转移到另一个帐户。以下是我迄今所做的工作,而且工作不正常。我有Failed作为响应,也有Error: Transaction has been reverted by the EVM:

以下是我在节点中的令牌传输功能:

代码语言:javascript
复制
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});
    }
EN

回答 1

Stack Overflow用户

发布于 2022-03-12 06:03:41

没有足够的气体来完成操作,因为交易使用了30400中的大多数。例如,我认为你必须把汽油限额从30400提高到100000。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70847454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档