Today's blockchain is not the almost unnoticeable fee network is was once, albeit much better now than towards the end of 2017 through early 2018 when they were astronomical. Fees were once in the pennies if even a penny. Now days fees can run anywhere from just under $1 to well over $20 or more, so taking the time to calculate the transaction fees prior to sending your transaction is not only good practice, but it can save you money.

If you're sending from an online wallet hosted somewhere, chances are you won't be able to do anything and will just have to live with whatever fees are being sent or charged by the wallet service.
Some wallet services actually generate revenue this way. They charge you x$ (some as high as 0.0005), then group the transactions together from everyone sending fees and pay the actual "expedited" fee and keep the change.

Calculating Blockchain (Bitcoin) Fees 

Before you can calculate the fees for any given transaction you'll need to know a few things. First of all you need the going rate. "Rates" are calculated in Satoshi's per byte. A Satoshi is the smallest possible unit of bitcoin, which is 0.00000001 and a byte is comprised of 8 bits.

Transaction Bytes

It takes anywhere from 8 to 32 bits or more to represent am alphanumeric character. Since bitcoin processes a script combined of 8-bit/1-byte alphanumeric, it is 1-byte per character (including spaces).

Here's simple calculator to explain, paste a bitcoin transaction script or type some text below:






To calculate the estimate byte size you'll need both the number of inputs and the number of outputs, plus any additional data you may be sending along (such as a message).

Inputs: The number wallet address and amount combinations that will be included in a transaction. Let's say you are sending Mary 0.025, Jim 0.01, and Susan 1.1. Let's also say that you're sending these all from two wallets, one with a balance of 0.8 and one with a balance of 0.5, we'll call them wallet address 1 and wallet address 2 receptively. Your transaction would start looking like this:


Outputs: The number of destination and amount combinations that will be included in a transaction. However, we wouldn't have 4 outputs since wallet 1 and wallet 2 are both contributing to a single output (Mary's) and the network fee. Your transaction would look like this with outputs:



Using the above example we would have 4 inputs so if we're using i as a variable for inputs that would be: i = 2
[code type="JavaScript"]var i = 4;[/code]
Let's assume o as the variable for outputs so in the above example o would be equal to 3.
[code type="JavaScript"]var o = 3;[/code]
However will not be equal 5, we have to ad 1 for the output for the fee, so o = 3 + 1.
[code type="JavaScript"]var o = (3 + 1);[/code]

We can estimate approximately 148 bytes per input and 34 bytes  per output. Using the above example and code i would equal  × 148 and o would be × 34 and the sum of i and o would be your input and output bytes, for which we'll use a variable of b.
[code type="JavaScript"]var b = o + i;[/code]

Satoshi's Per Byte

Next we need to know what the going rate is. For this there are a number of API's and tools to use, including here at dinbits.com or you can simply use the bitcoin client.

The easiest rpc to use in that case would be estimatesmartfee which can be run by typing this into the console:

estimatesmartfee n

Where n is the number of blocks you wish the transaction to confirm in. If you want your transaction to confirm within 6 blocks you would enter:

estimatesmartfee 6

Which would return the fee. Here's this example in bitcoin client.














 e 

To breakdown how fees are calculated we can use a more primitive rpc (estimatefee) by typing:

estimatefee 6

Which will return the amount in Sastoshi's per byte, for example

725

That return would mean 725 Satoshi's per byte for a confirmation within 6 blocks (about an hour) estimated.

Let's use s as Satoshi's per byte, s = 725. (note this example was taken at a higher fee period)

[code type="JavaScript"]var s = 725;[/code]

Additional Data

Before we can put it all together we need to account for any additional data and 10 additional bytes (10 bytes are always sent).

Let's say we want to include a message "Hello World!" in our transaction. That's an additional 12 bytes. To calculate the additional bytes we would add 12 to 10 for a sum of 22.  Using data as the bytes of additional data (12) and d as the sum, we would have.

[code type="JavaScript"]var d = data + 10;[/code]

Calculating the Fee

Now that you have all of the above you can plug in the numbers.

The bytes would equal i plus o plus d or:

a + +

Then accommodate or plus or minus i and multiply that by the Satoshi's per byte, represented below as variable s:

b=(a ± i)s

Which can also be written as (b=(a ± i) × s) 

Or all together in one equation:

= ((i 48 + (o + 1)34 + d + 10) ± i )s

Finally we need to divide by 10 (10 to the power of 8) and multiple by the price of bitcoin.

Let p := bitcoin market price: f = ( a ÷ 10⁸ ) × or simplified as: 



All together now:

× ( i × 148 ) + o + ) × 34 ) + ( d + 10 ) ) +- ) ) ÷ 10⁸ × or simplified as:

 

If was recommended at 725 sat/byte then the fee would come to 341598 sat. Which you would multiply by 10^8. Which means the fee would be 0.00341598. With a going rate of $9,600 that would come to $32.79 (rounded down) for an optimally fast transaction.

Coded Example

Here's a working example in Javascript/JQuery where ins = inputs, outs = outputs, d = data bytes (additional data), and p = bitcoin price. The function uses 21.co's (now earn.com) API to get the fastestFee.

[code type="JQuery"]function GetFee(ins, outs, d, p){
                //inbound parameter >> p = bitcoin price
                //inbound parameter >> d = bytes of additional data
    $.ajax({
        url: 'https://bitcoinfees.earn.com/api/v1/fees/recommended',
        jsonpCallback: 'jsonCallback',
        contentType: "application/json",
        dataType: 'json', async: false,
        success: function(json)
            {
                var estm = (ins * 148) + (outs + 1) * 34) +d_bytes + 10 + -ins;
                var fee = ((estm * (json.fastestFee)) * (10 ^ 8));
                return (fee * p);
            },
        error: function(e)
            {
                console.log(e.message);
            }
        });
}[/code]

As shown above, with proper planning and calculation of fees, especially when grouping transactions, you can not only speed up your transactions and minimize the space needed on the blockchain, but you can save yourself some money as well.

Powered by Blogger.