Delayed Swap due to Numeric Underflow Bug by using Tron’s triggerSmartContract Method


Yesterday, @steemit-market sent 20 STEEM to @steem2usdt because he/she wants to swap to USDT on Tron Blockchain (TRC-20), the status was showing “Pending Sent” in the Steem to USDT Swap Tool.

This isn’t normal – I took a look and investigated, and found out the invoke of API triggerSmartContract fails and says “Numeric Underflow” (Exception) see below

1
2
3
4
5
6
7
8
9
10
11
const tx = await tronWeb.transactionBuilder.triggerSmartContract(
    CONTRACT, 'transfer(address,uint256)', options,
    [{
        type: 'address',
        value: toAddress
    }, {
        type: 'uint256',
        value: parseFloat(amount * 1000000)
    }],
    tronWeb.address.toHex(fromAddress)
);
const tx = await tronWeb.transactionBuilder.triggerSmartContract(
    CONTRACT, 'transfer(address,uint256)', options,
    [{
        type: 'address',
        value: toAddress
    }, {
        type: 'uint256',
        value: parseFloat(amount * 1000000)
    }],
    tronWeb.address.toHex(fromAddress)
);

The triggerSmartContract method is used to trigger a smart contract on Tron Blockchain e.g. TRC-20. The TRC-20 USDT is a smart contract aka token, and we need to trigger the contract’s method transfer in order to send USDT tokens from one address to another.

The USDT transaction was not showing on the tronscan (which is a Tron Blockchain Explorer) so it means the transaction fails locally and not yet pushed to the chain.

The problem is that the amount due to floating precision cannot be safely represented in the current type. The “parseFloat” function is causing the problem (Numeric Underflow Exception) here. After changing it to parseInt, the fund was safely sent out to the destination wallet address (Tron).

parseFloat method returns a float and parseInt returns a whole of integer. This is absolutely fine here because for example: 4.184999999 times 1000000 to convert to the SUN unit is 4184999.9999 which causes the underflow issue – but if we use parseInt, it will be 4184999 – or we can use the Math.round which rounds up to 41185000 and that is acceptable as well.

Robustness of the Swap Tool

This incident shows the correctness of the tool – only mark the status to “Sent” when the fund has been sent. The transactions on the steem blockchain are immutable database records which we can easily scan or lookup, so are the ones on the Tron Blockchain.

Tron Blockchain Posts

Here are some popular posts regarding the Tron Blockchain:

whale-300x200 Delayed Swap due to Numeric Underflow Bug by using Tron's triggerSmartContract Method blockchain Cryptocurrency javascript Smart Contract STEEM Tron Blockchain

Steem to the Moon🚀!

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
513 words
Last Post: Teaching Kids Programming - Minimum Amount of Time to Fill Cups (Greedy Simulation Algorithm and Math)
Next Post: Teaching Kids Programming - Sum the Multiples in a Range using Venn Diagram (Math and Bruteforce Algorithm)

The Permanent URL is: Delayed Swap due to Numeric Underflow Bug by using Tron’s triggerSmartContract Method

Leave a Reply