How to get completed, failed, pending status after erc20token transfer

118 views Asked by At

I have a function in node.js to transfer erc20tokens using api like this:

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);
})

And here is the api code which is in Laravel:

$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
}

Here I want to get the status name like what string or code should be replace the status of 'confirmed' in if condition and also for 'pending' status and any 'know and error' status. Can you please let me know how to determine the status of transaction.

1

There are 1 answers

0
Professor On

I don't really know much about Ethereum, but if you take a look at the documentation you'll see they're specifying the possible return values for the operation you're trying to do.

0 for Fail, 1 for Pass.

It doesn't seem to me like there's a status for pending transactions. Or maybe it's Null.