php 如何在erc20token传输后获得完成、失败、挂起状态

yk9xbfzb  于 2022-10-30  发布在  PHP
关注(0)|答案(1)|浏览(107)

我在node.js中有一个函数,使用api传输erc20令牌,如下所示:

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条件中的“已确认”状态,以及“待定”状态和任何“已知和错误”状态。您能告诉我如何确定事务的状态吗?

zkure5ic

zkure5ic1#

我对以太坊了解不多,但如果你看一下文档,你会发现他们为你尝试执行的操作指定了可能的返回值。

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

在我看来,待定事务处理没有状态。或者它可能是Null

相关问题