|
| 1 | +import type { Network } from "../types/network"; |
| 2 | +import { ExampleFactory } from "./factory"; |
| 3 | + |
| 4 | +export class StakingOperationsExample extends ExampleFactory { |
| 5 | + constructor() { |
| 6 | + super({ |
| 7 | + id: "staking-operations", |
| 8 | + name: "Staking Operations", |
| 9 | + description: "Demonstrate staking operations: bonding, nominating, and managing stake", |
| 10 | + level: "intermediate", |
| 11 | + categories: ["staking", "transactions", "validators"], |
| 12 | + }); |
| 13 | + } |
| 14 | + |
| 15 | + generateCode(network: Network): string { |
| 16 | + return `// Staking Operations Example on ${network.name} |
| 17 | +${this.getImports(network, true)} |
| 18 | +
|
| 19 | +// Connect to ${network.name} using WebSocket |
| 20 | +const client = createClient( |
| 21 | + withPolkadotSdkCompat( |
| 22 | + getWsProvider("${network.endpoint}") |
| 23 | + ) |
| 24 | +); |
| 25 | +
|
| 26 | +// Get the typed API using the descriptors |
| 27 | +const typedApi = client.getTypedApi(${network.descriptorKey}); |
| 28 | +
|
| 29 | +// Example staking operations |
| 30 | +const demonstrateStakingOperations = async () => { |
| 31 | + try { |
| 32 | + console.log("🚀 Starting staking operations demonstration..."); |
| 33 | + |
| 34 | + // 1. Query current staking information for Alice |
| 35 | + const aliceAddress = "${this.getTestAccount("alice")}"; |
| 36 | + console.log("\\n📊 Querying staking info for Alice:", aliceAddress); |
| 37 | + |
| 38 | + // Get staking ledger |
| 39 | + const ledger = await typedApi.query.Staking.Ledger.getValue(aliceAddress); |
| 40 | + console.log("Staking Ledger:", { |
| 41 | + stash: ledger?.stash, |
| 42 | + total: ledger?.total?.toString(), |
| 43 | + active: ledger?.active?.toString(), |
| 44 | + unlocking: ledger?.unlocking?.length || 0 |
| 45 | + }); |
| 46 | + |
| 47 | + // Get bonded amount |
| 48 | + const bonded = await typedApi.query.Staking.Bonded.getValue(aliceAddress); |
| 49 | + console.log("Bonded to:", bonded); |
| 50 | + |
| 51 | + // Get nominators |
| 52 | + const nominators = await typedApi.query.Staking.Nominators.getValue(aliceAddress); |
| 53 | + console.log("Current nominators:", nominators?.targets || []); |
| 54 | + |
| 55 | + // 2. Demonstrate bonding transaction (would normally require signer) |
| 56 | + console.log("\\n🔗 Bonding Transaction Example:"); |
| 57 | + console.log("This would bond tokens to a stash account:"); |
| 58 | + const bondAmount = 1000000000000n; // 1000 tokens (adjust for decimals) |
| 59 | + const bondTx = typedApi.tx.Staking.bond({ |
| 60 | + value: bondAmount, |
| 61 | + payee: { Staked: null } |
| 62 | + }); |
| 63 | + console.log("Bond transaction created (not submitted in simulator)"); |
| 64 | + |
| 65 | + // 3. Demonstrate nomination transaction |
| 66 | + console.log("\\n🎯 Nomination Transaction Example:"); |
| 67 | + console.log("This would nominate validators:"); |
| 68 | + const validator1 = "${this.getTestAccount("bob")}"; // Example validator |
| 69 | + const validator2 = "${this.getTestAccount("charlie")}"; // Example validator |
| 70 | + const nominateTx = typedApi.tx.Staking.nominate({ |
| 71 | + targets: [ |
| 72 | + MultiAddress.Id(validator1), |
| 73 | + MultiAddress.Id(validator2) |
| 74 | + ] |
| 75 | + }); |
| 76 | + console.log("Nominate transaction created for validators:", [validator1, validator2]); |
| 77 | + |
| 78 | + // 4. Query validator information |
| 79 | + console.log("\\n🏆 Querying Validator Information:"); |
| 80 | + const validatorInfo = await typedApi.query.Staking.Validators.getValue(validator1); |
| 81 | + console.log("Validator", validator1, "info:", { |
| 82 | + commission: validatorInfo?.commission?.toString(), |
| 83 | + blocked: validatorInfo?.blocked |
| 84 | + }); |
| 85 | + |
| 86 | + // 5. Demonstrate unbonding |
| 87 | + console.log("\\n🔓 Unbonding Transaction Example:"); |
| 88 | + const unbondAmount = 500000000000n; // 500 tokens |
| 89 | + const unbondTx = typedApi.tx.Staking.unbond({ |
| 90 | + value: unbondAmount |
| 91 | + }); |
| 92 | + console.log("Unbond transaction created for", unbondAmount.toString(), "tokens"); |
| 93 | + |
| 94 | + // 6. Query era information |
| 95 | + console.log("\\n📅 Current Era Information:"); |
| 96 | + const currentEra = await typedApi.query.Staking.CurrentEra.getValue(); |
| 97 | + const activeEra = await typedApi.query.Staking.ActiveEra.getValue(); |
| 98 | + console.log("Current era:", currentEra?.toString()); |
| 99 | + console.log("Active era:", activeEra?.index?.toString()); |
| 100 | + |
| 101 | + // 7. Query reward points for current era |
| 102 | + console.log("\\n💰 Reward Information:"); |
| 103 | + if (activeEra?.index) { |
| 104 | + const eraRewardPoints = await typedApi.query.Staking.ErasRewardPoints.getValue(activeEra.index); |
| 105 | + console.log("Era", activeEra.index.toString(), "total reward points:", eraRewardPoints?.total?.toString()); |
| 106 | + } |
| 107 | + |
| 108 | + console.log("\\n✅ Staking operations demonstration completed!"); |
| 109 | + console.log("Note: In a real application, you would sign and submit these transactions using a wallet."); |
| 110 | + |
| 111 | + } catch (error) { |
| 112 | + console.error("❌ Error in staking operations:", error); |
| 113 | + } |
| 114 | +}; |
| 115 | +
|
| 116 | +demonstrateStakingOperations().catch(console.error); |
| 117 | +`; |
| 118 | + } |
| 119 | +} |
0 commit comments