Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out gas fees paid for the latest transactions of a token on RSK?

Tags:

sql

rsk

I want to query the Covalent database to find out the amount of gas paid out in the latest 100 rUSDT token transfer transactions on the RSK blockchain.

In the following SQL query I am trying to join these two tables to find out the gas fees paid for each of the latest 100 transactions.

SELECT
  t.fees_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 @> array[E'\\xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'::bytea]
  AND e.topics[1] = E'\\xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
  AND e.sender = E'\\xEf213441a85DF4d7acBdAe0Cf78004E1e486BB96'
ORDER BY e.block_id DESC, e.tx_offset DESC
LIMIT 100;

Unfortunately this query appears to take too long to process.

How can I modify this query?

More context:

  • 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef is the ERC20 Transfer event log's topic ID.
  • 0xEf213441a85DF4d7acBdAe0Cf78004E1e486BB96 is the smart contract of the ERC20 token.
  • the \\x in Postgres' bytea format is used to type hexadecimal values as string literals, may be considered to be equivalent to the 0x prefix.
  • In the Covalent database, chain_rsk_mainnet.block_log_events is a table with all events emitted by smart contracts on RSK Mainnet
  • In the Covalent database, chain_rsk_mainnet.block_transactions is a table with all RSK Mainnet transaction details
  • The reason that e.topics is matched twice is a performance optimisation. Strictly speaking, only the latter one is necessary.
like image 970
serlokiyo Avatar asked Nov 29 '21 11:11

serlokiyo


People also ask

What is the gas fee for NFT?

2. The gas calculation formula is: 21,000 (gas limit) x (50 (base fee) + 15 (Tip)), or 21,000 x (50 + 15). This returns a total gas fee of 1,365,000 gwei or 0.001365 ETH.

What is gas fee crypto?

A gas fee is the term given to transaction fees on the Ethereum (CRYPTO:ETH) blockchain network. According to Ethereum's developer pages, gas is “the fuel that allows the [Ethereum network] to operate, in the same way that a car needs gasoline to run.”

Is RSK EVM compatible?

RSK currently supports all the opcodes and precompiles contracts of Ethereum, and therefore it can support any language that compiles to the EVM. This includes Solidity, Julia, and new or experimental programming languages such as Vyper.

Is RSK decentralized?

RSK also allows decentralized finance application developers to create their DeFi protocols, NFTs, or other digital assets using the Bitcoin blockchain. This will enable Bitcoin to become a cryptocurrency ecosystem similar to Ethereum. Layers allow more applications to use or assist a blockchain.

How to calculate gas price in RSK?

You have several ways to calculate the gas price. From a developer point of view, you need to know that RSK has a minimumGasPrice limit, this means that if you set a gasPrice below that minimum, your transaction will be rejected. Using JSON-RPC method eth_gasPrice. This gives you a current average gas price in the network.

How are gas fees calculated for ETH transactions?

So if a transaction's gas limit was 21,000 units with a gas price of 200 gwei, its gas fee would have been calculated as (21,000 * 200) = 4,200,000 gwei (0.0042 ETH). Under this fee structure, there were no minimum or maximum transaction costs—the price of gas was completely determined by supply and demand in the network at any given time.

What is transaction fee and gas fee?

What is Transaction Fee? Transaction is the cost of activities on the Ethereum network. It is deducted whenever a smart contract activity is done on the network. If you transfer Ethereum or any other smart contract activity the transcation fee will be deducted in ether. This fee is paid to the miner who computed the transcation. What is A Gas Fee?

How many gas units are consumed per transaction?

Every transaction uses a gas fee. The units of gas used for the transaction is called Gas Unit. How many Gas Units consumed for every transactions depends on its complexity. It also depends on the number of operations performed by the smart contract. Price per Gas unit is the amount we are willing to pay for 1 Gas unit.


1 Answers

You need to put a date range on the query or else it will run for a very long time. There are a huge number of rUSDT Transfer event logs on RSK. Scanning the full table to find all of them, and joining these all in one go is the root cause that this query takes too long.

To solve this, for each of the tables being joined, add a condition to the time-related fields (block_log_events.block_signed_at and block_transactions.signed_at), to limit it to a certain interval, say a month:

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()

Here's the full query:

SELECT
  t.fees_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 @> array[E'\\xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'::bytea]
  AND 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 100;
like image 189
bguiz Avatar answered Oct 22 '22 14:10

bguiz