erc20token转移后如何获得完成、失败、挂起状态

Posted

技术标签:

【中文标题】erc20token转移后如何获得完成、失败、挂起状态【英文标题】:How to get completed, failed, pending status after erc20token transfer 【发布时间】:2021-10-03 12:46:56 【问题描述】:

我在 node.js 中有一个函数可以像这样使用 api 传输 erc20tokens:

app.post('/sendtokens', async(req, res) =>
    console.log(req.body)
    let privateKey = req.body.privateKey;
    let receiverPublicKey = req.body.receiverPublicKey;
    let amountToSend = req.body.amountToSend;

    // change here yout contract address
    let contractAddress = 'contractaddresss';
    // put your infura API here
    let provider = new HDWalletProvider(privateKey, "nodeurl");
    let web3 = new Web3(provider);
    let gasPrice ;
    console.log(gasPrice)
    let data = await axios.get('https://api.etherscan.io/api?module=gastracker&action=gasoracle');
    gasPrice = parseInt(data.result.FastGasPrice) * 1000000000;
    console.log('gasPrice = '+gasPrice);
    const mytoken = new web3.eth.Contract(abiarray, mytoken.methods.balanceOf(receiverPublicKey).call(,function(error, result)
        console.log("balance ");
        console.log(result);
    )
    let account = await web3.eth.getAccounts();
    console.log(account[0])
    let receiptToSend = null;
    BigNumber.config( EXPONENTIAL_AT: 40 )
    let tokens = new BigNumber(amountToSend);
    tokens = tokens.multipliedBy(new BigNumber(Math.pow(10,18)));
    console.log(tokens.toString())
    tokens = tokens.toString()
    await mytoken.methods.transfer(receiverPublicKey, tokens).send(
        from: account[0],
        gasPrice: gasPrice,
        gas: '60000'
    ).then(function(receipt)
        console.log('hi')
        receiptToSend = receipt;
        console.log(receipt);
    )
    res.send(receiptToSend);
)

这是 Laravel 中的 api 代码:

$privateKey = "privatekey";
$receiverAddress= 'etherium wallet';
$amountTosend = 'token amount';

$res = $client->post('https://website.com/sendtokens', [
    'json' => [
        'privateKey' => $privateKey,
        'receiverPublicKey' => $receiverAddress,
        'amountToSend' => $amountTosend,
    ]
]);

$data = $res->getBody();

$decode = json_decode($data);

$transHash = $decode->transactionHash;
$transStatus = $decode->status;

if($transStatus == confirmed)
    //transaction completed and confirmed
    

else if ($transStatus == pending)
    //transaction completed and is pending

else if ($transStatus == anyknown error)
    //transaction error and error name

else
    //unknown error

在这里,我想获取状态名称,例如应该用什么字符串或代码替换 if 条件中的“已确认”状态以及“待定”状态和任何“知道和错误”状态。请告诉我如何确定交易状态。

【问题讨论】:

【参考方案1】:

我对以太坊不太了解,但如果您查看 the documentation,您会发现他们正在为您尝试执行的操作指定可能的返回值。

0 表示失败,1 表示通过。

在我看来,没有待处理交易的状态。或者它可能是Null

【讨论】:

以上是关于erc20token转移后如何获得完成、失败、挂起状态的主要内容,如果未能解决你的问题,请参考以下文章

ERC20 代币转移到智能合约

无法使用 web3.js 发送 ERC20 令牌

如何使用 Web3 购买 ERC20 代币

ethereum发erc20token

如何从智能合约中转移 ERC20 代币而不转移到基本账户

我可以使用 PHP 从 ERC20 合约中转移代币吗?