【问题标题】:Sending ERC-20 tokens fails. Transaction succeeds, but no tokens are sent发送 ERC-20 令牌失败。交易成功,但没有发送令牌
【发布时间】:2021-08-13 06:44:24
【问题描述】:

当我运行以下代码时,它准确地获取了两个地址的代币余额,并且交易甚至通过(我可以在测试网上看到),尽管没有发送代币。

我尝试了很多方法,包括用这个替换已签名的交易部分:

await contract.methods.transfer(toAddress, 100000).send({
  from: fromAddress
});

但失败并出现unknown account 错误。

我的代币发送代码:

const Web3 = require("web3");
const { hdkey } = require("ethereumjs-wallet");
const bip39 = require("bip39");
const token = require("./token.json");

const mnemonic = "12 word phrase";
const provider = "https://apis.ankr.com/.../binance/full/test";

(async() => {
    try {
        const seed = await bip39.mnemonicToSeed(mnemonic);
        const root = hdkey.fromMasterSeed(seed);
        const web3 = new Web3(provider);
        const addrNode = root.derivePath(`m/44'/60'/0'/0/0`);
        const wallet = addrNode.getWallet();
        // #0 in the hdwallet, the owner of the tokens
        const fromAddress = wallet.getAddressString();
        // #1 in the hdwallet, already has a token balance
        const toAddress = "0x...";

        const contract = new web3.eth.Contract(token.abi, token.contract_address);

        let fromAddressBalance = await contract.methods.balanceOf(fromAddress).call();
        let toAddressBalance = await contract.methods.balanceOf(toAddress).call();

        console.log(`Pre Transaction: Sender: ${fromAddressBalance} TOKENS / Wallet: ${toAddressBalance} TOKENS`);

        // token has 3 decimal places, this is 100.000
        const encodedABI = contract.methods.transfer(toAddress, 100000).encodeABI();
        const tx = {
            from: fromAddress,
            to: toAddress,
            gas: 2000000,
            value: 0x0,
            data: encodedABI
          }; 
        const signed = await web3.eth.accounts.signTransaction(tx, wallet.privateKey.toString("hex"));
        const trans = await web3.eth.sendSignedTransaction(signed.rawTransaction);

        fromAddressBalance = await contract.methods.balanceOf(fromAddress).call();
        toAddressBalance = await contract.methods.balanceOf(toAddress).call();
    
        console.log(`Post Transaction: Sender: ${fromAddressBalance} TOKENS / Wallet: ${toAddressBalance} TOKENS`);
    } catch (err) {
        console.error(err.stack);
    }
    process.exit();
})();

【问题讨论】:

  • 请在 EthereScan 上验证您的智能合约源代码并链接到相关交易。
  • 可能是代币合约执行不正确造成的。合约源码是什么?
  • @MikkoOhtamaa 我已经验证了合同,这是交易:testnet.bscscan.com/tx/…

标签: node.js ethereum web3 erc20


【解决方案1】:

有一些问题一旦解决就解决了我的问题。我没有回过头来测试哪个做了,或者是否所有都需要,但想把它留给未来的探索者。

  • 我正在使用 ethereum-js-wallet 创建一个钱包,然后将它与 web3 一起使用。你必须让 web3 知道钱包。
        const account = web3.eth.accounts.privateKeyToAccount(privateKey);
        web3.eth.accounts.wallet.create();
        web3.eth.accounts.wallet.add(account);
const privateKey = `0x${wallet.privateKey.toString("hex")}`;
  • 这最终无关紧要,也不是正确的转移方式,但仍然需要注意的是,我将签署的交易发送给第三方,而不是合同,发送交易的正确方式应该是曾经
        const tx = {
            from: fromAddress,
            to: token.contract_address,
            gas: 2000000,
            value: 0x0,
            data: encodedABI
          }; 

最终,我需要利用合约的转移方法与签署/发送交易

        const result = await contract.methods.transfer(toAddress, 100).send({
            from: fromAddress,
            gas: 2000000
        });

【讨论】:

  • 感谢您提出并提供答案,几个小时前我遇到了类似的问题,您的解决方案对我有用
猜你喜欢
  • 1970-01-01
  • 2020-01-25
  • 2014-07-12
  • 2016-06-19
  • 2022-01-02
  • 1970-01-01
  • 2021-10-16
  • 2015-03-19
  • 1970-01-01
相关资源
最近更新 更多