Calculating Transaction Size Before Sending on Ethereum
As a website owner who uses Bitcoins and wants to prevent users from exceeding their bandwidth limits when sending transactions, you are in a good position to understand how Ethereum works. In this article, we will explore the concept of calculating transaction size before sending via the RPC API.
Why Calculate Transaction Size?
In legacy non-segwit (LNW) blockchains such as Ethereum Classic (ETC), Bitcoin Cash (BCH), and others, the size of a transaction is determined by its data payload. To prevent users from exceeding their bandwidth limits, it is important to calculate the estimated transaction size before sending.
Legacy Non-Segwit Block Height
Before calculating the transaction size, you need to know the block height at which the transaction will be sent. You can get this information using the RPC API’s eth_blockNumber() function or by querying a Bitcoin index file like btindex.dat.
Here’s an example of how to calculate the block height:
const blockchain = {
blockHeight: null,
data: {},
};
// Get the latest block
async function getLatestBlock() {
const response = await fetch('
const data = JSON.parse(response.text);
// ...
blockchain.blockHeight = data[0].height;
}
getLatestBlock();
Calculating transaction size
Once you have the block height, you can calculate the estimated transaction size by adding the following components together:
- Transaction type: The
txType
field determines the transaction type (e.g.send
,receive
, etc.).
- Input data
: If your site uses P2PKH or P2SH transactions, you need to calculate the estimated size of the input data.
- Output data: If your site uses a different output format, you may need to adjust this calculation accordingly.
Here is an example of how to estimate the transaction size for a send
transaction using P2PKH:
const txType = 'send';
const inputDataSize = 0; // assume 0 bytes for simplicity
// estimate transaction size (in kB)
const estimateSize = inputDataSize + 10; // add some additional data (e.g. header, padding)
Example code
Here is a simple example of how to estimate transaction size using Node.js and the ethers.js
library:
const ethers = require('ethers');
async function calculateTransactionSize(blockHeight) {
const blockchain = await getBlockchain();
const txType = 'send';
const inputDataSize = 0; // assume 0 bytes for simplicity
// estimate transaction size (in kB)
const estimateSize = inputDataSize + 10; // add some additional data (e.g. header, padding)
return estimateSize;
}
async function getBlockchain() {
const response = await fetch('
const data = JSON.parse(response.text);
return blockchain;
}
Best Practices
When calculating transaction size before sending, keep the following in mind:
- Round up to the nearest kilobyte (kB) for most transactions.
- Include some additional data (e.g. headers, padding characters) as a minimum estimate.
- Consider using a more complex estimation algorithm if you need accurate results.
By following these guidelines and understanding how Ethereum works, you can ensure that your site’s users do not exceed their bandwidth limits when sending transactions. Happy coding!