|
| 1 | +import type { Network } from "../types/network"; |
| 2 | +import { ExampleFactory } from "./factory"; |
| 3 | + |
| 4 | +export class XcmV4Example extends ExampleFactory { |
| 5 | + constructor() { |
| 6 | + super({ |
| 7 | + id: "xcm-v4-operations", |
| 8 | + name: "XCM V4 Operations", |
| 9 | + description: "Demonstrate latest XCM V4 features: teleport assets, reserve transfers, and cross-chain messaging", |
| 10 | + level: "advanced", |
| 11 | + categories: ["xcm", "cross-chain", "assets", "teleport"], |
| 12 | + }); |
| 13 | + } |
| 14 | + |
| 15 | + generateCode(network: Network): string { |
| 16 | + return `// XCM V4 Operations Example on ${network.name} |
| 17 | +${this.getImports(network, true)} |
| 18 | +
|
| 19 | +// Connect to ${network.name} |
| 20 | +const client = createClient( |
| 21 | + withPoladotSdkCompat( |
| 22 | + getWsProvider("${network.endpoint}") |
| 23 | + ) |
| 24 | +); |
| 25 | +
|
| 26 | +// Get the typed API using the descriptors |
| 27 | +const typedApi = client.getTypedApi(${network.descriptorKey}); |
| 28 | +
|
| 29 | +// XCM V4 operations demonstration |
| 30 | +const demonstrateXcmV4Operations = async () => { |
| 31 | + try { |
| 32 | + console.log("🌐 Starting XCM V4 operations demonstration..."); |
| 33 | +
|
| 34 | + // 1. Query XCM version |
| 35 | + console.log("\\n🔍 Querying XCM version and capabilities:"); |
| 36 | + try { |
| 37 | + const version = await typedApi.query.PolkadotXcm.VersionNotifiers.getValue(); |
| 38 | + console.log("XCM version notifiers:", version); |
| 39 | + } catch (error) { |
| 40 | + console.log("Version notifiers not available"); |
| 41 | + } |
| 42 | +
|
| 43 | + // 2. Query supported locations |
| 44 | + console.log("\\n📍 Querying supported XCM locations:"); |
| 45 | + try { |
| 46 | + const locations = await typedApi.query.PolkadotXcm.SupportedVersion.getEntries(); |
| 47 | + console.log("Supported XCM locations:", locations.length); |
| 48 | + } catch (error) { |
| 49 | + console.log("Supported locations not available"); |
| 50 | + } |
| 51 | +
|
| 52 | + // 3. Demonstrate teleport assets (XCM V4) |
| 53 | + console.log("\\n✈️ Teleport Assets Example (XCM V4):"); |
| 54 | + console.log("This would teleport assets to another chain:"); |
| 55 | + const teleportTx = typedApi.tx.PolkadotXcm.teleport_assets({ |
| 56 | + dest: { |
| 57 | + V4: { |
| 58 | + parents: 1, |
| 59 | + interior: { |
| 60 | + X1: { Parachain: 2000 } // Target parachain |
| 61 | + } |
| 62 | + } |
| 63 | + }, |
| 64 | + beneficiary: { |
| 65 | + V4: { |
| 66 | + parents: 0, |
| 67 | + interior: { |
| 68 | + X1: { AccountId32: { id: "${this.getTestAccount("alice")}" } } |
| 69 | + } |
| 70 | + } |
| 71 | + }, |
| 72 | + assets: { |
| 73 | + V4: [{ |
| 74 | + id: { Concrete: { parents: 1, interior: "Here" } }, |
| 75 | + fun: { Fungible: 1000000000000n } // 1000 DOT/KSM |
| 76 | + }] |
| 77 | + }, |
| 78 | + fee_asset_item: 0 |
| 79 | + }); |
| 80 | + console.log("Teleport assets transaction created (not submitted in simulator)"); |
| 81 | +
|
| 82 | + // 4. Demonstrate reserve transfer assets |
| 83 | + console.log("\\n🔄 Reserve Transfer Assets Example (XCM V4):"); |
| 84 | + console.log("This would perform a reserve transfer:"); |
| 85 | + const reserveTransferTx = typedApi.tx.PolkadotXcm.reserve_transfer_assets({ |
| 86 | + dest: { |
| 87 | + V4: { |
| 88 | + parents: 1, |
| 89 | + interior: { |
| 90 | + X1: { Parachain: 2000 } |
| 91 | + } |
| 92 | + } |
| 93 | + }, |
| 94 | + beneficiary: { |
| 95 | + V4: { |
| 96 | + parents: 0, |
| 97 | + interior: { |
| 98 | + X1: { AccountId32: { id: "${this.getTestAccount("bob")}" } } |
| 99 | + } |
| 100 | + } |
| 101 | + }, |
| 102 | + assets: { |
| 103 | + V4: [{ |
| 104 | + id: { Concrete: { parents: 1, interior: "Here" } }, |
| 105 | + fun: { Fungible: 500000000000n } // 500 DOT/KSM |
| 106 | + }] |
| 107 | + }, |
| 108 | + fee_asset_item: 0 |
| 109 | + }); |
| 110 | + console.log("Reserve transfer transaction created (not submitted in simulator)"); |
| 111 | +
|
| 112 | + // 5. Demonstrate limited teleport assets |
| 113 | + console.log("\\n🎯 Limited Teleport Assets Example:"); |
| 114 | + console.log("This would perform a limited teleport:"); |
| 115 | + const limitedTeleportTx = typedApi.tx.PolkadotXcm.limited_teleport_assets({ |
| 116 | + dest: { |
| 117 | + V4: { |
| 118 | + parents: 1, |
| 119 | + interior: { |
| 120 | + X1: { Parachain: 1000 } // Asset Hub |
| 121 | + } |
| 122 | + } |
| 123 | + }, |
| 124 | + beneficiary: { |
| 125 | + V4: { |
| 126 | + parents: 0, |
| 127 | + interior: { |
| 128 | + X1: { AccountId32: { id: "${this.getTestAccount("alice")}" } } |
| 129 | + } |
| 130 | + } |
| 131 | + }, |
| 132 | + assets: { |
| 133 | + V4: [{ |
| 134 | + id: { Concrete: { parents: 1, interior: "Here" } }, |
| 135 | + fun: { Fungible: 100000000000n } |
| 136 | + }] |
| 137 | + }, |
| 138 | + fee_asset_item: 0, |
| 139 | + weight_limit: { Limited: { ref_time: 2000000000n, proof_size: 50000n } } |
| 140 | + }); |
| 141 | + console.log("Limited teleport transaction created (not submitted in simulator)"); |
| 142 | +
|
| 143 | + // 6. Demonstrate transfer reserve asset |
| 144 | + console.log("\\n🏦 Transfer Reserve Asset Example:"); |
| 145 | + console.log("This would transfer reserve-backed assets:"); |
| 146 | + const transferReserveTx = typedApi.tx.PolkadotXcm.transfer_reserve_asset({ |
| 147 | + assets: { |
| 148 | + V4: [{ |
| 149 | + id: { Concrete: { parents: 1, interior: "Here" } }, |
| 150 | + fun: { Fungible: 200000000000n } |
| 151 | + }] |
| 152 | + }, |
| 153 | + dest: { |
| 154 | + V4: { |
| 155 | + parents: 1, |
| 156 | + interior: { |
| 157 | + X1: { Parachain: 2000 } |
| 158 | + } |
| 159 | + } |
| 160 | + }, |
| 161 | + xcm: { |
| 162 | + V4: [ |
| 163 | + { |
| 164 | + ReserveAssetDeposited: { |
| 165 | + assets: { Wild: { AllCounted: 1 } } |
| 166 | + } |
| 167 | + }, |
| 168 | + { |
| 169 | + ClearOrigin: null |
| 170 | + }, |
| 171 | + { |
| 172 | + DepositAsset: { |
| 173 | + assets: { Wild: { AllCounted: 1 } }, |
| 174 | + beneficiary: { |
| 175 | + AccountId32: { id: "${this.getTestAccount("bob")}" } |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + ] |
| 180 | + } |
| 181 | + }); |
| 182 | + console.log("Transfer reserve asset transaction created (not submitted in simulator)"); |
| 183 | +
|
| 184 | + // 7. Query XCM queues |
| 185 | + console.log("\\n📋 Querying XCM queues:"); |
| 186 | + try { |
| 187 | + const outboundQueue = await typedApi.query.PolkadotXcm.Queries.getValue(); |
| 188 | + console.log("Outbound XCM queries:", outboundQueue); |
| 189 | + } catch (error) { |
| 190 | + console.log("XCM queue queries not available"); |
| 191 | + } |
| 192 | +
|
| 193 | + // 8. Demonstrate execute XCM message |
| 194 | + console.log("\\n⚡ Execute XCM Message Example:"); |
| 195 | + console.log("This would execute a custom XCM message:"); |
| 196 | + const executeTx = typedApi.tx.PolkadotXcm.execute({ |
| 197 | + message: { |
| 198 | + V4: [ |
| 199 | + { |
| 200 | + WithdrawAsset: { |
| 201 | + assets: { |
| 202 | + Concrete: { |
| 203 | + parents: 0, |
| 204 | + interior: { |
| 205 | + X1: { PalletInstance: 10 } |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + }, |
| 211 | + { |
| 212 | + DepositAsset: { |
| 213 | + assets: { Wild: { AllCounted: 1 } }, |
| 214 | + beneficiary: { |
| 215 | + AccountId32: { id: "${this.getTestAccount("alice")}" } |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + ] |
| 220 | + }, |
| 221 | + max_weight: { ref_time: 1000000000n, proof_size: 10000n } |
| 222 | + }); |
| 223 | + console.log("Execute XCM message transaction created (not submitted in simulator)"); |
| 224 | +
|
| 225 | + // 9. Query XCM version negotiation |
| 226 | + console.log("\\n🤝 Querying XCM version negotiation:"); |
| 227 | + try { |
| 228 | + const versionNotify = await typedApi.query.PolkadotXcm.VersionNotifyTargets.getValue(); |
| 229 | + console.log("Version notify targets:", versionNotify); |
| 230 | + } catch (error) { |
| 231 | + console.log("Version negotiation queries not available"); |
| 232 | + } |
| 233 | +
|
| 234 | + // 10. Demonstrate force XCM version |
| 235 | + console.log("\\n🔧 Force XCM Version Example:"); |
| 236 | + console.log("This would force a specific XCM version (admin only):"); |
| 237 | + const forceVersionTx = typedApi.tx.PolkadotXcm.force_xcm_version({ |
| 238 | + location: { |
| 239 | + V4: { |
| 240 | + parents: 1, |
| 241 | + interior: { |
| 242 | + X1: { Parachain: 2000 } |
| 243 | + } |
| 244 | + } |
| 245 | + }, |
| 246 | + version: 4 |
| 247 | + }); |
| 248 | + console.log("Force XCM version transaction created (not submitted in simulator)"); |
| 249 | +
|
| 250 | + console.log("\\n✅ XCM V4 operations demonstration completed!"); |
| 251 | + console.log("Note: XCM operations require:"); |
| 252 | + console.log("- Understanding of XCM multilocation format"); |
| 253 | + console.log("- Knowledge of destination chain requirements"); |
| 254 | + console.log("- Proper weight limits and fee calculations"); |
| 255 | + console.log("- In a real application, you would sign and submit these transactions"); |
| 256 | + console.log("- XCM V4 introduces enhanced asset handling and better error reporting"); |
| 257 | +
|
| 258 | + } catch (error) { |
| 259 | + console.error("❌ Error in XCM V4 operations:", error); |
| 260 | + } |
| 261 | +}; |
| 262 | +
|
| 263 | +demonstrateXcmV4Operations().catch(console.error); |
| 264 | +`; |
| 265 | + } |
| 266 | +} |
0 commit comments