Setting the Transaction Fee in JavaScript
This tutorial is intended to help you understand the difference between using a flat fee versus a suggested fee with any transaction. The sample also shows how to set these fees with JavaScript.
Requirements
Background
All transactions on the Algorand blockchain require a fee, which must be greater than or equal to the minimum transaction fee (currently 1000 microAlgos). The SDKs provide two primary methods to determine the fee for your transaction. One method uses the suggested fee-per-byte and the other allows you to specify a flat fee. See Fees to learn more about how these are calculated and why you may want to use one over the other. This tutorial also creates and submits the transaction. See Your First Transaction for details on transactions.
Steps
1. Connect to Algod and Get Suggested Params
The Rest API provides a method to get the suggested fee per byte of a transaction (suggestedFee
). It is important to realize that this number is a fee per byte and not a total fee. For transactions of normal size (1kb) this is currently 1000 microAlgos. For transactions that are larger (note field usage and smart contracts) this fee may be higher than the minimum transaction. You can also set the fee per byte to 0 and the system will automatically set the fee to the current minimum transaction fee. The code below sets up a connection to the algod process, gets the suggested transaction parameters and the suggested transaction fee per byte.
const algosdk = require('algosdk');
//Retrieve the token, server and port values for your installation in the algod.net
//and algod.token files within the data directory
const token = "<your-api-token>";
const server = "http://<your-algod-host>";
const port = //<your-alogd-port>;
// Recover accounts used in example
// Example only. Never store mnemonic in code
var account1_mnemonic = "portion never forward pill lunch organ biology" +
" weird catch curve isolate plug innocent skin grunt" +
" bounce clown mercy hole eagle soul chunk type absorb trim";
(async() => {
// instantiate the algod wrapper
let algodclient = new algosdk.Algod(token, server, port);
let params = await algodclient.getTransactionParams();
let endRound = params.lastRound + parseInt(1000);
// This is the suggested fee per byte
let feePerByte = await algodclient.suggestedFee();
})().catch(e => {
console.log(e);
});
2. Recover Sender Account
Recover the account that is going to submit the transaction. Make sure to use the dispenser to fund the recovered account.
const algosdk = require('algosdk');
//Retrieve the token, server and port values for your installation in the algod.net
//and algod.token files within the data directory
const token = "<your-api-token>";
const server = "http://<your-algod-host>";
const port = //<your-alogd-port>;
// Recover accounts used in example
// Example only. Never store mnemonic in code
var account1_mnemonic = "portion never forward pill lunch organ biology" +
" weird catch curve isolate plug innocent skin grunt" +
" bounce clown mercy hole eagle soul chunk type absorb trim";
(async() => {
// instantiate the algod wrapper
let algodclient = new algosdk.Algod(token, server, port);
let params = await algodclient.getTransactionParams();
let endRound = params.lastRound + parseInt(1000);
// This is the suggested fee per byte
let feePerByte = await algodclient.suggestedFee();
// Recover sender acccount
var recoveredAccount1 = algosdk.mnemonicToSecretKey(account1_mnemonic);
console.log(recoveredAccount1.addr);
const receiver = "GD64YIY3TWGDMCNPP553DZPPR6LDUSFQOIJVFDPPXWEG3FVOJCCDBBHU5A";
console.log( algosdk.isValidAddress(recoveredAccount1.addr));
})().catch(e => {
console.log(e);
});
Learn More
- Add Funds using Dispenser
3. Construct Transaction with Fee
The transaction can now be created. The fee can be set to the suggested fee per byte or optionally you can set a flat fee.
Use a suggested fee per byte
const algosdk = require('algosdk');
//Retrieve the token, server and port values for your installation in the algod.net
//and algod.token files within the data directory
const token = "<your-api-token>";
const server = "http://<your-algod-host>";
const port = //<your-alogd-port>;
// Recover accounts used in example
// Example only. Never store mnemonic in code
var account1_mnemonic = "portion never forward pill lunch organ biology" +
" weird catch curve isolate plug innocent skin grunt" +
" bounce clown mercy hole eagle soul chunk type absorb trim";
(async() => {
// instantiate the algod wrapper
let algodclient = new algosdk.Algod(token, server, port);
let params = await algodclient.getTransactionParams();
let endRound = params.lastRound + parseInt(1000);
// This is the suggested fee per byte
let feePerByte = await algodclient.suggestedFee();
// Recover sender acccount
var recoveredAccount1 = algosdk.mnemonicToSecretKey(account1_mnemonic);
console.log(recoveredAccount1.addr);
const receiver = "GD64YIY3TWGDMCNPP553DZPPR6LDUSFQOIJVFDPPXWEG3FVOJCCDBBHU5A";
console.log( algosdk.isValidAddress(recoveredAccount1.addr));
// Make the payment transaction and set the fee to feePerByte
let tx1 = algosdk.makePaymentTxn(recoveredAccount1.addr,
receiver, feePerByte.fee, 200000, undefined, params.lastRound,
endRound, new Uint8Array(0), params.genesishashb64, params.genesisID);
})().catch(e => {
console.log(e);
});
Using a Flat Fee
To set a flat fee, just override the transaction fee with the two lines shown below after creating the transaction.
// Make the payment transaction and set the fee to feePerByte
let tx1 = algosdk.makePaymentTxn(recoveredAccount1.addr,
receiver, feePerByte.fee, 200000, undefined, params.lastRound,
endRound, new Uint8Array(0), params.genesishashb64, params.genesisID);
tx1.flatfee = true;
tx1.fee = 1000;
4. Sign and Submit Transaction
Sign and submit the create transaction.
const algosdk = require('algosdk');
//Retrieve the token, server and port values for your installation in the algod.net
//and algod.token files within the data directory
const token = "<your-api-token>";
const server = "http://<your-algod-host>";
const port = //<your-alogd-port>;
// Recover accounts used in example
// Example only. Never store mnemonic in code
var account1_mnemonic = "portion never forward pill lunch organ biology" +
" weird catch curve isolate plug innocent skin grunt" +
" bounce clown mercy hole eagle soul chunk type absorb trim";
(async() => {
// instantiate the algod wrapper
let algodclient = new algosdk.Algod(token, server, port);
let params = await algodclient.getTransactionParams();
let endRound = params.lastRound + parseInt(1000);
// This is the suggested fee per byte
let feePerByte = await algodclient.suggestedFee();
// Recover sender acccount
var recoveredAccount1 = algosdk.mnemonicToSecretKey(account1_mnemonic);
console.log(recoveredAccount1.addr);
const receiver = "GD64YIY3TWGDMCNPP553DZPPR6LDUSFQOIJVFDPPXWEG3FVOJCCDBBHU5A";
console.log( algosdk.isValidAddress(recoveredAccount1.addr));
// Make the payment transaction and set the fee to feePerByte
let tx1 = algosdk.makePaymentTxn(recoveredAccount1.addr,
receiver, feePerByte.fee, 200000, undefined, params.lastRound,
endRound, new Uint8Array(0), params.genesishashb64, params.genesisID);
// Sign and submit the transaction
stx1 = tx1.signTxn(recoveredAccount1.sk);
let tx = (await algodclient.sendRawTransaction(stx1));
console.log("Transaction : " + tx.txId);
})().catch(e => {
console.log(e);
});