【问题标题】:How to get completed, failed, pending status after erc20token transfererc20token转移后如何获得完成、失败、挂起状态
【发布时间】: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 条件中的“已确认”状态以及“待定”状态和任何“知道和错误”状态。请告诉我如何确定交易状态。

【问题讨论】:

    标签: php node.js laravel ethereum web3


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-25
      • 2023-01-08
      • 1970-01-01
      • 2018-01-01
      • 1970-01-01
      • 2020-07-17
      相关资源
      最近更新 更多