Skip to content

Commit b357afe

Browse files
nhussein11Telucero
andauthored
Add tutorial for paying transaction fees with different tokens (#966)
* Add tutorial for paying transaction fees with different tokens * fix: llms * Remove tutorial and code snippets for paying transaction fees with different tokens * fix: llms * Update tutorial for sending transactions with alternative fee payment token and adjust navigation link * Enhance tutorial for sending transactions with alternative fee payment token by adding TypeScript module configuration step * fix: llms * Refine tutorial for sending transactions with alternative fee payment token by clarifying implementation steps and enhancing readability * fix: llms * Refine tutorial title for sending transactions with alternative fee payment token to improve clarity * Apply suggestions from code review Co-authored-by: Taylor Lucero <[email protected]> * fix: llms * Apply suggestions from code review Co-authored-by: Taylor Lucero <[email protected]> * fix: title * fix: llms --------- Co-authored-by: Taylor Lucero <[email protected]>
1 parent c917776 commit b357afe

File tree

6 files changed

+567
-0
lines changed

6 files changed

+567
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<div id="termynal" data-termynal>
2+
<span data-ty="input"><span class="file-path"></span>npx ts-node fee-payment-transaction.ts</span>
3+
<pre>
4+
Tx finalized: 0x771956fdf40b3741bdc3c1e981a6daacbe5521877ad1915542e7413bb4a820bc (ok=true)
5+
Block: #9645060 0x57710514f168b5c444c8e47b1e1a31dd9e7bc7e9a51d8d25ccdbc6053e159f6b [tx index 2]
6+
Events:
7+
- Assets
8+
- Balances
9+
- Assets
10+
- AssetConversion
11+
- Balances
12+
- Balances
13+
- AssetTxPayment
14+
- System
15+
</pre>
16+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { sr25519CreateDerive } from "@polkadot-labs/hdkd";
2+
import {
3+
DEV_PHRASE,
4+
entropyToMiniSecret,
5+
mnemonicToEntropy,
6+
} from "@polkadot-labs/hdkd-helpers";
7+
import { getPolkadotSigner } from "polkadot-api/signer";
8+
import { createClient } from "polkadot-api";
9+
import { assetHub } from "@polkadot-api/descriptors";
10+
import { withPolkadotSdkCompat } from "polkadot-api/polkadot-sdk-compat";
11+
import { getWsProvider } from "polkadot-api/ws-provider/node";
12+
import { MultiAddress } from "@polkadot-api/descriptors";
13+
14+
const TARGET_ADDRESS = "14E5nqKAp3oAJcmzgZhUD2RcptBeUBScxKHgJKU4HPNcKVf3"; // Bob's address
15+
const TRANSFER_AMOUNT = 3_000_000_000n; // 3 DOT
16+
const USD_ASSET_ID = 1337;
17+
18+
const createSigner = async () => {
19+
const entropy = mnemonicToEntropy(DEV_PHRASE);
20+
const miniSecret = entropyToMiniSecret(entropy);
21+
const derive = sr25519CreateDerive(miniSecret);
22+
const hdkdKeyPair = derive("//Alice");
23+
const polkadotSigner = getPolkadotSigner(
24+
hdkdKeyPair.publicKey,
25+
"Sr25519",
26+
hdkdKeyPair.sign
27+
);
28+
return polkadotSigner;
29+
};
30+
31+
const client = createClient(
32+
withPolkadotSdkCompat(
33+
getWsProvider("ws://localhost:8000") // Chopsticks Asset Hub
34+
)
35+
);
36+
37+
const api = client.getTypedApi(assetHub);
38+
39+
const tx = api.tx.Balances.transfer_keep_alive({
40+
dest: MultiAddress.Id(TARGET_ADDRESS),
41+
value: BigInt(TRANSFER_AMOUNT),
42+
});
43+
44+
const signer = await createSigner();
45+
46+
const result = await tx.signAndSubmit(signer, {
47+
asset: {
48+
parents: 0,
49+
interior: {
50+
type: "X2",
51+
value: [
52+
{ type: "PalletInstance", value: 50 },
53+
{ type: "GeneralIndex", value: BigInt(USD_ASSET_ID) },
54+
],
55+
},
56+
},
57+
});
58+
59+
const { txHash, ok, block, events } = result;
60+
console.log(`Tx finalized: ${txHash} (ok=${ok})`);
61+
console.log(`Block: #${block.number} ${block.hash} [tx index ${block.index}]`);
62+
63+
console.log("Events:");
64+
for (const ev of events) {
65+
const type = (ev as any).type ?? "unknown";
66+
console.log(`- ${type}`);
67+
}
68+
69+
process.exit(0);

0 commit comments

Comments
 (0)