@@ -53,54 +53,7 @@ This command downloads the latest Polkadot metadata and generates TypeScript des
5353Create a file named ` papi-fee-calculator.ts ` :
5454
5555``` typescript title="papi-fee-calculator.ts"
56- import { createClient } from " polkadot-api" ;
57- import { withPolkadotSdkCompat } from " polkadot-api/polkadot-sdk-compat" ;
58- import { polkadotTestNet } from " @polkadot-api/descriptors" ;
59- import { getWsProvider } from " polkadot-api/ws-provider" ;
60-
61- async function calculateFees() {
62- // Connect to chain
63- const client = createClient (
64- withPolkadotSdkCompat (getWsProvider (" INSERT_WS_ENDPOINT" ))
65- );
66-
67- // Get typed API
68- const api = client .getTypedApi (polkadotTestNet );
69-
70- // Define sender and recipient addresses
71- const aliceAddress = " INSERT_ALICE_ADDRESS" ;
72- const bobAddress = " INSERT_BOB_ADDRESS" ;
73-
74- // Amount to transfer (1 DOT = 10^10 plancks)
75- const amount = 1_000_000_000_000n ; // 1 DOT
76-
77- try {
78- // Create the transaction
79- const tx = api .tx .Balances .transfer_keep_alive ({
80- dest: {
81- type: " Id" ,
82- value: bobAddress ,
83- },
84- value: amount ,
85- });
86-
87- // Estimate fees
88- const estimatedFees = await tx .getEstimatedFees (aliceAddress );
89-
90- console .log (` Estimated fee: ${Number (estimatedFees ) / 1e10 } DOT ` );
91- console .log (` Transaction amount: ${Number (amount ) / 1e10 } DOT ` );
92- console .log (
93- ` Total deducted: ${Number (estimatedFees + amount ) / 1e10 } DOT `
94- );
95- } catch (error ) {
96- console .error (" Error calculating fees:" , error );
97- } finally {
98- // Clean up
99- client .destroy ();
100- }
101- }
102-
103- calculateFees ();
56+ -- 8 < -- ' code/chain-interactions/send-transactions/calculate-transaction-fees/papi-fee-calculator.ts'
10457```
10558
10659Ensure 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.
@@ -120,13 +73,7 @@ npx tsx papi-fee-calculator.ts
12073
12174You should see output similar to:
12275
123- <div class =" termynal " data-termynal >
124- <span data-ty="input"><span class="file-path"></span>npx tsx papi-fee-calculator.ts</span>
125- <span data-ty="progress"></span>
126- <span data-ty>Estimated fee: 0.0014668864 DOT</span>
127- <span data-ty>Transaction amount: 100 DOT</span>
128- <span data-ty>Total deducted: 100.0014668864 DOT</span>
129- </div >
76+ --8<-- 'code/chain-interactions/send-transactions/calculate-transaction-fees/papi-fee-calculator-output.html'
13077
13178## Polkadot.js API
13279
@@ -141,44 +88,7 @@ npm install @polkadot/api
14188Create a file named ` polkadotjs-fee-calculator.ts ` :
14289
14390``` typescript title="polkadotjs-fee-calculator.ts"
144- import { ApiPromise , WsProvider } from " @polkadot/api" ;
145-
146- async function calculateFees() {
147- // Connect to chain
148- const wsProvider = new WsProvider (" INSERT_WS_ENDPOINT" );
149- const api = await ApiPromise .create ({ provider: wsProvider });
150-
151- // Wait for API to be ready
152- await api .isReady ;
153-
154- // Define sender and recipient addresses
155- const aliceAddress = " INSERT_ALICE_ADDRESS" ;
156- const bobAddress = " INSERT_BOB_ADDRESS" ;
157-
158- // Amount to transfer (1 DOT = 10^10 plancks)
159- const amount = 1_000_000_000_000n ; // 1 DOT
160-
161- try {
162- // Create the transaction
163- const tx = api .tx .balances .transferKeepAlive (bobAddress , amount );
164-
165- // Get payment information
166- const paymentInfo = await tx .paymentInfo (aliceAddress );
167-
168- console .log (` Estimated fee: ${Number (paymentInfo .partialFee .toBigInt ()) / 1e10 } DOT ` );
169- console .log (` Transaction amount: ${Number (amount ) / 1e10 } DOT ` );
170- console .log (
171- ` Total deducted: ${Number (paymentInfo .partialFee .toBigInt () + amount ) / 1e10 } DOT `
172- );
173- } catch (error ) {
174- console .error (" Error calculating fees:" , error );
175- } finally {
176- // Clean up
177- await api .disconnect ();
178- }
179- }
180-
181- calculateFees ();
91+ -- 8 < -- ' code/chain-interactions/send-transactions/calculate-transaction-fees/polkadotjs-fee-calculator.ts'
18292```
18393
18494Ensure 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.
@@ -197,13 +107,7 @@ npx tsx polkadotjs-fee-calculator.ts
197107
198108You should see output similar to:
199109
200- <div class =" termynal " data-termynal >
201- <span data-ty="input"><span class="file-path"></span>npx tsx polkadotjs-fee-calculator.ts</span>
202- <span data-ty="progress"></span>
203- <span data-ty>Estimated fee: 0.0014668864 DOT</span>
204- <span data-ty>Transaction amount: 100 DOT</span>
205- <span data-ty>Total deducted: 100.0014668864 DOT</span>
206- </div >
110+ --8<-- 'code/chain-interactions/send-transactions/calculate-transaction-fees/polkadotjs-fee-calculator-output.html'
207111
208112## Polkadot-JS Apps Interface
209113
0 commit comments