4

I am trying to calculate how much gas is spent for rUSDT token transfer transaction on RSK. If I go to RSK explorer rUSDT events page and pick up the first available transaction with a transfer event, I see that besides the transfer event I am interested in, it also includes 24 other events (sent, conversion, approval etc.). So the gas fee paid for this transaction (769,510 gas * 0.000000000065540656 RBTC c) consists of the amount of gas paid for all of these 24 events. Is there a way to "separate" transfers from the others and to calculate the gas fees paid only for them? My idea was to use a Covalent database to query RSK blockchain. I wrote this query to calculate gas fees paid for the last 20 transactions with transfer events.

SELECT encode(t.hash, 'hex'),  t.gas_spent * t.gas_price / 10^18 AS gas_paid
FROM chain_rsk_mainnet.block_log_events e
    INNER JOIN chain_rsk_mainnet.block_transactions t
    ON e.block_id = t.block_id
    AND e.tx_offset = t.tx_offset
WHERE e.topics[1] = E'\\xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
    AND e.sender = E'\\xEf213441a85DF4d7acBdAe0Cf78004E1e486BB96'
    AND e.block_signed_at > NOW() - INTERVAL '1 month' AND e.block_signed_at <= NOW()
    AND t.signed_at > NOW() - INTERVAL '1 month' AND t.signed_at <= NOW()
ORDER BY e.block_id DESC, e.tx_offset DESC
LIMIT 20

where 0xEf213441a85DF4d7acBdAe0Cf78004E1e486BB96 is a rUSDT token address and 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef is a constant (ERC20 Transfer event signature hash).

Is it possible to modify this query so as to calculate the gas paid separately for the token transfers?

Aleks Shenshin
  • 2,117
  • 5
  • 18

1 Answers1

5

You can restrict your query to only include transactions where the transaction'sto field matches the smart contract address of the rUSDT token, by adding another condition to your WHERE clause:

AND t.to = E'\\xEf213441a85DF4d7acBdAe0Cf78004E1e486BB96'

Explanation:

  • Your original query was picking up Transfer events within transactions between Externally Owned Account (EOA) addresses and the Smart Contract (SC) address of the ERC20 token.
  • Your original query was also picking up Transfer events within internal transactions between other SC addresses (e.g. a DeFi protocol), and the SC address of the ERC20 token
  • From your question, it appears that you only want to pick up the former, and exclude the latter.
  • To do this, one approach would be to
    • see where the EOA sends a transaction directly to the SC of the ERC20 token --> include this
    • see where the EOA sends a transaction to another SC address, which in turn indirectly executes an internal transaction on the SC of the ERC20 token --> exclude this

The above should give you a result where the gas fee paid for this transaction is indeed for a single Transfer occurrence.

However, regarding this:

Is there a way to "separate" transfers from the others and to calculate the gas fees paid only for them?

There is no direct way to do this AFAIK. Potentially, you might be able to do this using RPC calls to debug_traceTransaction... however since this isn't available in your database, you will require a completely new approach based on RPCs instead of SQL. I'll leave this alternate approach to other answerers!


Full query:

SELECT encode(t.hash, 'hex'),  t.gas_spent * t.gas_price / 10^18
    AS gas_paid
FROM chain_rsk_mainnet.block_log_events e
    INNER JOIN chain_rsk_mainnet.block_transactions t
    ON e.block_id = t.block_id
    AND e.tx_offset = t.tx_offset
WHERE e.topics[1] = E'\\xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
    AND e.sender = E'\\xEf213441a85DF4d7acBdAe0Cf78004E1e486BB96'
    AND e.block_signed_at > NOW() - INTERVAL '1 month' AND e.block_signed_at <= NOW()
    AND t.signed_at > NOW() - INTERVAL '1 month' AND t.signed_at <= NOW()
    AND t.to = E'\\xEf213441a85DF4d7acBdAe0Cf78004E1e486BB96'
ORDER BY e.block_id DESC, e.tx_offset DESC
LIMIT 20;

bguiz
  • 27,371
  • 47
  • 154
  • 243