-
Notifications
You must be signed in to change notification settings - Fork 78
Add estimate fee tx #1279
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+264
β1
Merged
Add estimate fee tx #1279
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3ef3ae2
Add tutorial for calculating transaction fees in Polkadot
nhussein11 d4ceb56
\fix: snippets
nhussein11 ad00220
Apply suggestions from code review
nhussein11 77e7d7f
fix: adding endpoint
nhussein11 f3d0d27
Apply suggestions from code review
eshaben 10c3d6c
styling
0xlukem 0f27238
Apply suggestions from code review
eshaben c4d2dae
improve description
0xlukem 91f4082
fix
0xlukem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
163 changes: 162 additions & 1 deletion
163
.chain-interactions/send-transactions/calculate-transaction-fees.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,162 @@ | ||
| TODO | ||
| --- | ||
| title: Calculate Transaction Fees | ||
| description: Learn how to calculate transaction fees on Polkadot using Polkadot-API, Polkadot.js API, and the Polkadot.js Apps UI to estimate transfer costs. | ||
| categories: Basics, Transactions, Developer Tools | ||
| --- | ||
|
|
||
| # Calculate Transaction Fees | ||
|
|
||
| ## Introduction | ||
|
|
||
| Transaction fees are essential costs for executing operations on Polkadot and its parachains. Understanding how to estimate these fees helps you manage account balances and build better user experiences in your applications. | ||
|
|
||
| This tutorial will guide you through different methods for calculating transaction fees. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| Before starting, make sure you have: | ||
|
|
||
| - [Node.js](https://nodejs.org/){target=\_blank} version 18 or higher installed | ||
| - Basic understanding of JavaScript/TypeScript | ||
eshaben marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| - Test accounts with sufficient balance to pay transaction fees | ||
eshaben marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| !!! note | ||
| Transaction fees on Polkadot are calculated based on three components: a base fee, a length fee (proportional to transaction size), and a weight fee (proportional to computational complexity). An optional tip can be added to prioritize transaction inclusion. | ||
|
|
||
| ## Polkadot-API (PAPI) | ||
|
|
||
| [Polkadot-API](/reference/tools/papi){target=\_blank} is the modern, recommended library for building TypeScript applications with type safety and light client support. | ||
|
|
||
| Create a new project directory and initialize it: | ||
|
|
||
| ```bash | ||
| mkdir fee-calculator | ||
| cd fee-calculator | ||
| npm init -y && npm pkg set type=module | ||
| ``` | ||
|
|
||
| Install the required packages: | ||
|
|
||
| ```bash | ||
| npm install polkadot-api | ||
| npm install --save-dev typescript tsx | ||
| ``` | ||
|
|
||
| Add the Polkadot relay chain to generate type-safe descriptors: | ||
|
|
||
| ```bash | ||
| npx papi add polkadotTestNet -w INSERT_WS_ENDPOINT | ||
| ``` | ||
|
|
||
| This command downloads the latest Polkadot metadata and generates TypeScript descriptors in the `@polkadot-api/descriptors` package. Ensure to replace `INSERT_WS_ENDPOINT` with the proper websocket endpoint. For this example, we will use the Polkadot Testnet (`wss://pas-rpc.stakeworld.io/assethub`). | ||
|
|
||
| Create a file named `papi-fee-calculator.ts`: | ||
|
|
||
| ```typescript title="papi-fee-calculator.ts" | ||
| --8<-- 'code/chain-interactions/send-transactions/calculate-transaction-fees/papi-fee-calculator.ts' | ||
| ``` | ||
|
|
||
| Ensure to replace `INSERT_WS_ENDPOINT` with your WebSocket endpoint, `INSERT_ALICE_ADDRESS` with the sender's address, and `INSERT_BOB_ADDRESS` with the recipient's address. | ||
|
|
||
| Key aspects of the code: | ||
|
|
||
| - **Transaction creation**: The `api.tx.Balances.transfer_keep_alive()` method constructs a balance transfer transaction. | ||
| - **`dest` parameter**: Specifies the recipient using a `MultiAddress` type with `Id` variant. | ||
| - **`getEstimatedFees()`**: Returns the estimated fee in plancks (the smallest unit, where 1 DOT = 10^10 plancks). | ||
| - The method applies a dummy signature internally to simulate the transaction. | ||
|
|
||
| Execute the script using `tsx`: | ||
|
|
||
| ```bash | ||
| npx tsx papi-fee-calculator.ts | ||
| ``` | ||
|
|
||
| You should see output similar to: | ||
|
|
||
| --8<-- 'code/chain-interactions/send-transactions/calculate-transaction-fees/papi-fee-calculator-output.html' | ||
|
|
||
| ## Polkadot.js API | ||
|
|
||
| [Polkadot.js API](https://polkadot.js.org/docs/api/){target=\_blank} is a mature JavaScript/TypeScript library for interacting with Polkadot SDK-based chains, providing comprehensive RPC client functionality and transaction building capabilities. | ||
|
|
||
| In the same project directory (or a new one), install the Polkadot.js packages: | ||
|
|
||
| ```bash | ||
| npm install @polkadot/api | ||
| ``` | ||
|
|
||
| Create a file named `polkadotjs-fee-calculator.ts`: | ||
|
|
||
| ```typescript title="polkadotjs-fee-calculator.ts" | ||
| --8<-- 'code/chain-interactions/send-transactions/calculate-transaction-fees/polkadotjs-fee-calculator.ts' | ||
| ``` | ||
|
|
||
| Ensure to replace `INSERT_WS_ENDPOINT` with your WebSocket endpoint, `INSERT_ALICE_ADDRESS` with the sender's address, and `INSERT_BOB_ADDRESS` with the recipient's address. | ||
|
|
||
| Key aspects of the code: | ||
|
|
||
| - **Transaction creation**: The `api.tx.balances.transferKeepAlive()` method constructs a balance transfer transaction. | ||
| - **`paymentInfo()`**: Applies a dummy signature and queries the RPC endpoint for fee estimation. | ||
| - **Return values**: The `partialFee` property contains the estimated fee in the smallest unit (plancks). | ||
|
|
||
| Execute the script using `tsx`: | ||
|
|
||
| ```bash | ||
| npx tsx polkadotjs-fee-calculator.ts | ||
| ``` | ||
|
|
||
| You should see output similar to: | ||
|
|
||
| --8<-- 'code/chain-interactions/send-transactions/calculate-transaction-fees/polkadotjs-fee-calculator-output.html' | ||
|
|
||
| ## Polkadot.js Apps Interface | ||
|
|
||
| For non-programmatic fee inspection, the PolkadotJS Apps interface provides a visual way to estimate transaction fees. | ||
|
|
||
| Navigate to the [Polkadot.js Apps interface](https://polkadot.js.org/apps){target=\_blank} and ensure you're connected to the Polkadot relay chain (or your desired network). | ||
|
|
||
| ### Estimate Fees via Transfer Interface | ||
|
|
||
| To see fees before submitting a transfer: | ||
|
|
||
| 1. Navigate to **Accounts** > **Accounts** in the top menu. | ||
| 2. Choose an account and click **send**. | ||
| 3. Fill in the transfer details: | ||
| - **Send to address**: Enter Bob's address. | ||
| - **Amount**: Enter the amount you wish to transfer (e.g., 1 DOT). | ||
| 4. Click **Sign and Submit**. | ||
| 5. The transaction fee will be displayed in the confirmation dialog before you sign. | ||
|
|
||
|  | ||
|
|
||
| ## Where to Go Next | ||
|
|
||
| Now that you can calculate transaction fees, explore related guides to send transactions and manage fees in your applications. | ||
|
|
||
| <div class="grid cards" markdown> | ||
|
|
||
| - <span class="badge guide">Guide</span> __Pay Fees with Different Tokens__ | ||
|
|
||
| --- | ||
|
|
||
| Learn how to send transactions while paying fees using alternative tokens instead of the native chain token. | ||
|
|
||
| [:octicons-arrow-right-24: Get Started](/chain-interactions/send-transactions/pay-fees-with-different-tokens/) | ||
|
|
||
| - <span class="badge guide">Guide</span> __Send Transactions with SDKs__ | ||
|
|
||
| --- | ||
|
|
||
| Learn how to send signed transactions using Polkadot-API and Polkadot.js API libraries. | ||
|
|
||
| [:octicons-arrow-right-24: Get Started](/chain-interactions/send-transactions/with-sdks/) | ||
|
|
||
| - <span class="badge guide">Guide</span> __Query Chain Data__ | ||
|
|
||
| --- | ||
|
|
||
| Explore different methods for querying blockchain data using REST APIs, SDKs, and runtime API calls. | ||
|
|
||
| [:octicons-arrow-right-24: Get Started](/chain-interactions/query-data/query-sdks/) | ||
|
|
||
| </div> | ||
7 changes: 7 additions & 0 deletions
7
...interactions/send-transactions/calculate-transaction-fees/papi-fee-calculator-output.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <div class="termynal" data-termynal> | ||
| <span data-ty="input"><span class="file-path"></span>npx tsx papi-fee-calculator.ts</span> | ||
| <span data-ty="progress"></span> | ||
| <span data-ty>Estimated fee: 0.0014668864 DOT</span> | ||
| <span data-ty>Transaction amount: 1 DOT</span> | ||
| <span data-ty>Total deducted: 1.0014668864 DOT</span> | ||
| </div> |
46 changes: 46 additions & 0 deletions
46
...de/chain-interactions/send-transactions/calculate-transaction-fees/papi-fee-calculator.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { createClient } from 'polkadot-api'; | ||
| import { withPolkadotSdkCompat } from 'polkadot-api/polkadot-sdk-compat'; | ||
| import { polkadotTestNet } from '@polkadot-api/descriptors'; | ||
| import { getWsProvider } from 'polkadot-api/ws-provider'; | ||
|
|
||
| async function calculateFees() { | ||
| // Connect to chain | ||
| const client = createClient( | ||
| withPolkadotSdkCompat(getWsProvider('INSERT_WS_ENDPOINT')) | ||
| ); | ||
|
|
||
| // Get typed API | ||
| const api = client.getTypedApi(polkadotTestNet); | ||
|
|
||
| // Define sender and recipient addresses | ||
| const aliceAddress = 'INSERT_ALICE_ADDRESS'; | ||
| const bobAddress = 'INSERT_BOB_ADDRESS'; | ||
|
|
||
| // Amount to transfer (1 DOT = 10^10 plancks) | ||
| const amount = 10_000_000_000n; // 1 DOT | ||
|
|
||
| try { | ||
| // Create the transaction | ||
| const tx = api.tx.Balances.transfer_keep_alive({ | ||
| dest: { | ||
| type: 'Id', | ||
| value: bobAddress, | ||
| }, | ||
| value: amount, | ||
| }); | ||
|
|
||
| // Estimate fees | ||
| const estimatedFees = await tx.getEstimatedFees(aliceAddress); | ||
|
|
||
| console.log(`Estimated fee: ${Number(estimatedFees) / 1e10} DOT`); | ||
| console.log(`Transaction amount: ${Number(amount) / 1e10} DOT`); | ||
| console.log(`Total deducted: ${Number(estimatedFees + amount) / 1e10} DOT`); | ||
| } catch (error) { | ||
| console.error('Error calculating fees:', error); | ||
| } finally { | ||
| // Clean up | ||
| client.destroy(); | ||
| } | ||
| } | ||
|
|
||
| calculateFees(); |
7 changes: 7 additions & 0 deletions
7
...ctions/send-transactions/calculate-transaction-fees/polkadotjs-fee-calculator-output.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <div class="termynal" data-termynal> | ||
| <span data-ty="input"><span class="file-path"></span>npx tsx polkadotjs-fee-calculator.ts</span> | ||
| <span data-ty="progress"></span> | ||
| <span data-ty>Estimated fee: 0.0014668864 DOT</span> | ||
| <span data-ty>Transaction amount: 1 DOT</span> | ||
| <span data-ty>Total deducted: 1.0014668864 DOT</span> | ||
| </div> |
42 changes: 42 additions & 0 deletions
42
...in-interactions/send-transactions/calculate-transaction-fees/polkadotjs-fee-calculator.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { ApiPromise, WsProvider } from '@polkadot/api'; | ||
|
|
||
| async function calculateFees() { | ||
| // Connect to chain | ||
| const wsProvider = new WsProvider('INSERT_WS_ENDPOINT'); | ||
| const api = await ApiPromise.create({ provider: wsProvider }); | ||
|
|
||
| // Wait for API to be ready | ||
| await api.isReady; | ||
|
|
||
| // Define sender and recipient addresses | ||
| const aliceAddress = 'INSERT_ALICE_ADDRESS'; | ||
| const bobAddress = 'INSERT_BOB_ADDRESS'; | ||
|
|
||
| // Amount to transfer (1 DOT = 10^10 plancks) | ||
| const amount = 10_000_000_000n; // 1 DOT | ||
|
|
||
| try { | ||
| // Create the transaction | ||
| const tx = api.tx.balances.transferKeepAlive(bobAddress, amount); | ||
|
|
||
| // Get payment information | ||
| const paymentInfo = await tx.paymentInfo(aliceAddress); | ||
|
|
||
| console.log( | ||
| `Estimated fee: ${Number(paymentInfo.partialFee.toBigInt()) / 1e10} DOT` | ||
| ); | ||
| console.log(`Transaction amount: ${Number(amount) / 1e10} DOT`); | ||
| console.log( | ||
| `Total deducted: ${ | ||
| Number(paymentInfo.partialFee.toBigInt() + amount) / 1e10 | ||
| } DOT` | ||
| ); | ||
| } catch (error) { | ||
| console.error('Error calculating fees:', error); | ||
| } finally { | ||
| // Clean up | ||
| await api.disconnect(); | ||
| } | ||
| } | ||
|
|
||
| calculateFees(); |
Binary file added
BIN
+1.02 MB
.../send-transactions/calculate-transaction-fees/calculate-transaction-fees-01.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.