Ethereum: Deduct fee from transaction amount
Ethereum: Decoding of the amount of the transaction with the costs
When you use the problem of the problem is that you do not specify the full amount of the transaction, which includes the costs associated with the sending of parts of your wallet to a recipient.
In this article, we will explore how to calculate and deduce from the amount of the transaction uses
Understand the types of transactions
Before diving in fresh, it is essential to understand the different councils of transactions on there are two main types:
- Basic payment transaction (BPT) : This is a type of transaction which sends a specific address of address.
- Transaction for sending addresses : This type of transaction allows the parts of sending your easer of your.
Calculation of the amount of the transaction
To calculate the amount of the transaction, including the costs, you can for the formula:
transaction_amunt = basic_payment_payment + (basic_payment_amunt * fee_per_bpt)
When basic_payment_amount is the amount of the parts sent, and
fee_per_bptis the costs loaded by basic payment transaction.
Example: Calculation of the amount of the transaction
Let's greet Saly assuming that the basic amount is $ 100 (BTC) and that the costs per BPT are 1%, we can calculate
Basic_payment_amunt = $ 100
fee_per_bpt = 0.01 '
transaction_amunt = basic_payment_payment + (basic_payment_amunt * fee_per_bpt)
= 100 $ + ($ 100 x 0.01)
"= $ 110"
In this example, the total amount of the transaction would be $ 110.
Fraics with API Sendtoadress Call
Now that you have the amount of the calculation, including the costs, you can "send feelings."
Here is an update version of the SNIPPT code:
Javascript
Const web3 = require ('web3');
Const Ethers = require ('ETERS');
// Define Eddress of your portfolio and the recipient
Const cheeddress = '0xyourwalletaddress';
Const toaddress = '0xrecicipientethereumaddress';
// Calculate the amount of the transaction, including the costs
Const Basicpaymentamount = 100; // BTC
Const feeperbpt = 1;
Const Transactionamount = basicpaymentamount + (basicpaymentamount * Feeperbpt);
// Create a new Ethereum portfolio object
Const web3instance = new web3.providers.httpprovider ('
// Get the current WALT balance
Const Frombalaance = Await Web3instance.et.Getbalancia (Co -fence);
Const Tubalating = Wait Web3instance.et.Getbalance (Toaddress);
// Calculate the amount of the transaction, including the costs
Const Gascost = 1000; // gas units (approximate)
Const transactionfee = 10; // Ether per unit of gas
// create a new transaction object
Const tx = {
De: cheese,
at: Toaddress,
Value: of FALAND * basicpaymentamount,
Gas: Gascost,
Gasprice: Web3instance.et.Gasprice,
Nose: Web3instance.et.gettransActionCount (cheese),
Chainid: web3instance.et.net.currentblock.chainid,
};
// update the transaction with the costs
Const updatedtx = wait web3instance.et.sendransaction (tx, {
Include: True,
});
'
In this example, we calculate the MUNT, including the costs, and the EddressSendtoadress”. Note that you will need to replace `у-infu-project-Id
with your Infura activity program.