Fetching transactions from particular address

Tried to fetch transaction of a certain address with Trongrid API but got following error

Error executing query error: value "13200000000000000000" is out of range for type bigint

I’m really confused with how and where from this value comes. Any help is appreciated!

My code for fetching:

async function searchTxTRC20(network_id, walletAddressTemp) {
    network_id = 5000;
    try {
        const config = {
            headers: {
                'TRON-PRO-API-KEY': ''
            }
        };
        const limit = 200;
        let transactions = [];
        let moreTransactions = true;
        let url = `https://api.trongrid.io/v1/accounts/` + walletAddressTemp + `/transactions/trc20?&only_confirmed=true&only_to=true&limit=200`;

        while (moreTransactions) {

            const response = await axios.get(url, config);
            await new Promise(resolve => setTimeout(resolve, 100));
            if (response.data && response.data.data && response.data.data.length > 0 && response.data.meta.links) {
                console.log(response.data.data.length, " ", response.data.meta.at, " ", transactions.length)
                console.log(url)
                console.log(transactions.slice(-1))
                transactions = transactions.concat(response.data.data);
                url = response.data.meta.links.next
            } else {
                moreTransactions = false;
            }
        }

        for (const t of transactions) {
            await addTxTRC20(t, network_id);
        }
        console.log(`Finished download.`);
    } catch (err) {
        console.error('Error executing query', err.stack);
    }
}

exports.searchTxTRC20 = searchTxTRC20;

addTxTRC20 = async function(trx, network_id, block_num_input = -1){
    const contract_addr = fromHEX(trx.token_info.address)
    const to = fromHEX(trx.to)
    const from = fromHEX(trx.from)
    const t = await pool.query(
        "SELECT * FROM f_add_tx($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12)",
        [
            trx.transaction_id,
            +trx.block_timestamp,
            +network_id,
            from,
            to,
            contract_addr,
            parseInt(trx.value),
            allToHEX(trx.value),
            (trx.blockNumber !== undefined) ? trx.blockNumber : -1,
            0,
            '',
            trx.type
        ]
    )
    console.log('tx_id is :',t.rows[0].id)
}
exports.addTxTRC20 = addTxTRC20

add tx to table function:

create function f_add_tx(v_hash character varying, v_ts bigint, v_network_id smallint, v_address_from character varying, v_address_to character varying, v_token character varying, v_value bigint, v_value_txt character varying, v_block integer, v_tx_fee numeric, v_tx_fee_txt character varying, v_info character varying) returns tx
    language plpgsql
as
$$
DECLARE
    v_result    tx%ROWTYPE;
    v_t         tokens%ROWTYPE;
BEGIN

    IF v_hash ISNULL THEN
        RAISE 'Wrong parameters. hash: %', v_hash USING ERRCODE = '23502';
    END IF;


    SELECT * FROM tx WHERE token_id = 0 AND network_id = v_network_id AND hash = v_hash INTO v_result; -- token_id = 0 

    IF v_result.id IS NULL THEN
        SELECT * from tokens WHERE network_id = v_network_id AND id = 0 AND contract IS NULL INTO v_t;
        IF v_t.id IS NULL THEN
            INSERT INTO tokens(id, network_id, contract)
            values (0, v_network_id, v_token)
            returning * INTO v_t;
        end if;
        v_result.token_id = v_t.id;

        IF v_address_to IS NULL THEN
            v_address_to = 0;
        END IF;
        SELECT id from addresses WHERE v_address_to = address INTO v_result.to_id;
        IF v_result.to_id IS NULL THEN
            INSERT INTO addresses(address, network_id)
            values(v_address_to,v_network_id)
            returning id INTO v_result.to_id;
        end if;


        SELECT id from addresses WHERE v_address_from = address INTO v_result.from_id;
        IF v_result.from_id IS NULL THEN
            INSERT INTO addresses(address, network_id)
            values(v_address_from,v_network_id)
            returning id INTO v_result.from_id;
        end if;

        INSERT INTO tx(from_id, to_id, network_id, value, value_txt, block, hash, token_id, tx_fee, tx_fee_txt, ts, info)
        VALUES(v_result.from_id, v_result.to_id, v_network_id, v_value/(10^v_t.decimals), v_value_txt, v_block, v_hash, v_result.token_id, v_tx_fee/ (10^v_t.decimals), v_tx_fee_txt, v_ts, v_info)
        RETURNING id INTO v_result.id;
    END IF;

    RETURN v_result;
END
$$;
3 Likes

@SimbadMarino will help or @adeel will give them a heads up.

4 Likes

After adding additional logs, the original error did not occur. However, my program do not fetch all transactions from the address.
image

Maybe there is some problem with my request? :sweat_smile:

async function searchTxTRC20(network_id, walletAddressTemp) {
    network_id = 5000;
    try {
        const config = {
            headers: {
                'TRON-PRO-API-KEY': ''
            }
        };
        const limit = 200;
        let transactions = [];
        let moreTransactions = true;
        let url = `https://api.trongrid.io/v1/accounts/` + walletAddressTemp + `/transactions/trc20?&only_confirmed=true&only_to=true&limit=200`;

        while (moreTransactions) {
            try {
                const response = await axios.get(url, config);
                await new Promise(resolve => setTimeout(resolve, 100)); // Delay to handle rate limits

                if (response.data && response.data.data && response.data.data.length > 0) {
                    console.log(`Fetched ${response.data.data.length} transactions from ${url}`);
                    transactions = transactions.concat(response.data.data);

                    if (response.data.meta && response.data.meta.links && response.data.meta.links.next) {
                        url = response.data.meta.links.next;
                    } else {
                        moreTransactions = false;
                        console.log('No more transactions to fetch'); // No more transactions to fetch
                    }

                    // current state log
                    console.log(`----- Total transactions fetched: ${transactions.length}`);
                } else {
                    moreTransactions = false;
                    console.log('No more transactions to fetch');
                }
            } catch (err) {
                console.error('Error fetching transactions:', err);
                moreTransactions = false;
            }
        }

        for (const t of transactions) {
            try {
                await addTxTRC20(t, network_id);
            } catch (err) {
                console.error('Error processing transaction:', t, err);
            }
        }
        console.log(`Finished download. Total transactions processed: ${transactions.length}`);
    } catch (err) {
        console.error('Error executing query', err.stack);
    }
}

hello @karmack , BigInt variables are limited to 15 digit long, as the error states you are passing a bigger value as argument in your function and that is causing the out of range error, please check deeper in your code what might be causing the value to add additional 0’s for some transactions… is this variable used for transaction value? As per your last message maybe it is working for v_ts values in the range of 15 digits but failing for bigger values.

I believe the problem is in the v_ts argument input . Let us now if you find the root cause of the issue :slight_smile:

2 Likes

You can fetch transactions from a specific address using blockchain explorers or APIs like Etherscan, TRONScan, or similar tools for your blockchain. Ensure to use the correct endpoint and query parameters to filter transactions for the desired address efficiently.