Ethereum: How to calculate transaction size before sending (Legacy Non-Segwit – P2PKH/P2SH)

Calculating Transaction Size Before Sending on Ethereum

As a Bitcoin site owner who wants to prevent users from exceeding 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 bandwidth limits, it is essential to calculate the estimated transaction size before sending.

Legacy Non-Segwit Block Height

Ethereum: How to calculate transaction size before sending (Legacy Non-Segwit - P2PKH/P2SH)

Before calculating transaction size, you need to know the height of the block to which the transaction will be sent. You can get this information using the eth_blockNumber() function on the RPC API or by querying a Bitcoin index file like btindex.dat.

Here is 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:

  • Transaction Type: The txType field determines the type of transaction (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’s an example of how to estimate the transaction size for a send transaction using P2PKH:

const txType = 'send';

const inputDataSize = 0; // for simplicity, assume it is 0 bytes

// Estimate the transaction size (in kB)

const estimateSize = inputDataSize + 10; // add some extra data (e.g. headers, padding)

Sample Code

Here is a simple example of how to estimate the 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; // for simplicity, assume it is 0 bytes

// Estimate the transaction size (in kB)

const estimateSize = inputDataSize + 10; // add some extra data (e.g. headers, padding)

return estimateSize;

}

async function getBlockchain() {

const response = await fetch('

const data = JSON.parse(response.text);

return blockchain;

}

Best Practice

When calculating transaction sizes before sending, keep the following in mind:

  • Round up to the nearest kilobyte (kB) for most transactions.
  • Add some extra data (e.g. headers, padding) 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 bandwidth limits when sending transactions. Happy coding!

Ethereum Balance Address After

Stay in the Loop

Get the daily email from CryptoNews that makes reading the news actually enjoyable. Join our mailing list to stay in the loop to stay informed, for free.

Latest stories

- Advertisement - spot_img

You might also like...