Skip to content

Commit d4ceb56

Browse files
committed
\fix: snippets
1 parent 3ef3ae2 commit d4ceb56

File tree

5 files changed

+104
-100
lines changed

5 files changed

+104
-100
lines changed

.chain-interactions/send-transactions/calculate-transaction-fees.md

Lines changed: 4 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -53,54 +53,7 @@ This command downloads the latest Polkadot metadata and generates TypeScript des
5353
Create 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

10659
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.
@@ -120,13 +73,7 @@ npx tsx papi-fee-calculator.ts
12073

12174
You 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
14188
Create 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

18494
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.
@@ -197,13 +107,7 @@ npx tsx polkadotjs-fee-calculator.ts
197107

198108
You 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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div class="termynal" data-termynal>
2+
<span data-ty="input"><span class="file-path"></span>npx tsx papi-fee-calculator.ts</span>
3+
<span data-ty="progress"></span>
4+
<span data-ty>Estimated fee: 0.0014668864 DOT</span>
5+
<span data-ty>Transaction amount: 100 DOT</span>
6+
<span data-ty>Total deducted: 100.0014668864 DOT</span>
7+
</div>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { createClient } from "polkadot-api";
2+
import { withPolkadotSdkCompat } from "polkadot-api/polkadot-sdk-compat";
3+
import { polkadotTestNet } from "@polkadot-api/descriptors";
4+
import { getWsProvider } from "polkadot-api/ws-provider";
5+
6+
async function calculateFees() {
7+
// Connect to chain
8+
const client = createClient(
9+
withPolkadotSdkCompat(getWsProvider("INSERT_WS_ENDPOINT"))
10+
);
11+
12+
// Get typed API
13+
const api = client.getTypedApi(polkadotTestNet);
14+
15+
// Define sender and recipient addresses
16+
const aliceAddress = "INSERT_ALICE_ADDRESS";
17+
const bobAddress = "INSERT_BOB_ADDRESS";
18+
19+
// Amount to transfer (1 DOT = 10^10 plancks)
20+
const amount = 1_000_000_000_000n; // 1 DOT
21+
22+
try {
23+
// Create the transaction
24+
const tx = api.tx.Balances.transfer_keep_alive({
25+
dest: {
26+
type: "Id",
27+
value: bobAddress,
28+
},
29+
value: amount,
30+
});
31+
32+
// Estimate fees
33+
const estimatedFees = await tx.getEstimatedFees(aliceAddress);
34+
35+
console.log(`Estimated fee: ${Number(estimatedFees) / 1e10} DOT`);
36+
console.log(`Transaction amount: ${Number(amount) / 1e10} DOT`);
37+
console.log(
38+
`Total deducted: ${Number(estimatedFees + amount) / 1e10} DOT`
39+
);
40+
} catch (error) {
41+
console.error("Error calculating fees:", error);
42+
} finally {
43+
// Clean up
44+
client.destroy();
45+
}
46+
}
47+
48+
calculateFees();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div class="termynal" data-termynal>
2+
<span data-ty="input"><span class="file-path"></span>npx tsx polkadotjs-fee-calculator.ts</span>
3+
<span data-ty="progress"></span>
4+
<span data-ty>Estimated fee: 0.0014668864 DOT</span>
5+
<span data-ty>Transaction amount: 100 DOT</span>
6+
<span data-ty>Total deducted: 100.0014668864 DOT</span>
7+
</div>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { ApiPromise, WsProvider } from "@polkadot/api";
2+
3+
async function calculateFees() {
4+
// Connect to chain
5+
const wsProvider = new WsProvider("INSERT_WS_ENDPOINT");
6+
const api = await ApiPromise.create({ provider: wsProvider });
7+
8+
// Wait for API to be ready
9+
await api.isReady;
10+
11+
// Define sender and recipient addresses
12+
const aliceAddress = "INSERT_ALICE_ADDRESS";
13+
const bobAddress = "INSERT_BOB_ADDRESS";
14+
15+
// Amount to transfer (1 DOT = 10^10 plancks)
16+
const amount = 1_000_000_000_000n; // 1 DOT
17+
18+
try {
19+
// Create the transaction
20+
const tx = api.tx.balances.transferKeepAlive(bobAddress, amount);
21+
22+
// Get payment information
23+
const paymentInfo = await tx.paymentInfo(aliceAddress);
24+
25+
console.log(`Estimated fee: ${Number(paymentInfo.partialFee.toBigInt()) / 1e10} DOT`);
26+
console.log(`Transaction amount: ${Number(amount) / 1e10} DOT`);
27+
console.log(
28+
`Total deducted: ${Number(paymentInfo.partialFee.toBigInt() + amount) / 1e10} DOT`
29+
);
30+
} catch (error) {
31+
console.error("Error calculating fees:", error);
32+
} finally {
33+
// Clean up
34+
await api.disconnect();
35+
}
36+
}
37+
38+
calculateFees();

0 commit comments

Comments
 (0)