|
| 1 | +// `ahp` is the name given to `npx papi add` |
| 2 | +import { |
| 3 | + ahp, |
| 4 | + people, |
| 5 | + XcmV2OriginKind, |
| 6 | + XcmV3Junction, |
| 7 | + XcmV3Junctions, |
| 8 | + XcmV3MultiassetFungibility, |
| 9 | + XcmV5AssetFilter, |
| 10 | + XcmV5Instruction, |
| 11 | + XcmV5Junction, |
| 12 | + XcmV5Junctions, |
| 13 | + XcmV5WildAsset, |
| 14 | + XcmVersionedXcm, |
| 15 | +} from '@polkadot-api/descriptors'; |
| 16 | +import { Binary, createClient, Enum, FixedSizeBinary } from 'polkadot-api'; |
| 17 | +// import from "polkadot-api/ws-provider/node" |
| 18 | +// if running in a NodeJS environment |
| 19 | +import { getWsProvider } from 'polkadot-api/ws-provider/web'; |
| 20 | +import { withPolkadotSdkCompat } from 'polkadot-api/polkadot-sdk-compat'; |
| 21 | +import { sr25519CreateDerive } from '@polkadot-labs/hdkd'; |
| 22 | +import { |
| 23 | + DEV_PHRASE, |
| 24 | + entropyToMiniSecret, |
| 25 | + mnemonicToEntropy, |
| 26 | + ss58Address, |
| 27 | +} from '@polkadot-labs/hdkd-helpers'; |
| 28 | +import { getPolkadotSigner } from 'polkadot-api/signer'; |
| 29 | + |
| 30 | +const entropy = mnemonicToEntropy(DEV_PHRASE); |
| 31 | +const miniSecret = entropyToMiniSecret(entropy); |
| 32 | +const derive = sr25519CreateDerive(miniSecret); |
| 33 | +const keyPair = derive('//Alice'); |
| 34 | + |
| 35 | +const polkadotSigner = getPolkadotSigner( |
| 36 | + keyPair.publicKey, |
| 37 | + 'Sr25519', |
| 38 | + keyPair.sign |
| 39 | +); |
| 40 | + |
| 41 | +// Connect to Polkadot Asset Hub. |
| 42 | +// Pointing to localhost since this example uses chopsticks. |
| 43 | +const client = createClient( |
| 44 | + withPolkadotSdkCompat(getWsProvider('ws://localhost:8000')) |
| 45 | +); |
| 46 | + |
| 47 | +// Get the typed API, a typesafe API for interacting with the chain. |
| 48 | +const ahpApi = client.getTypedApi(ahp); |
| 49 | + |
| 50 | +const PEOPLE_PARA_ID = 1004; |
| 51 | +// The identifier for DOT is the location of the Polkadot Relay Chain, |
| 52 | +// which is 1 up relative to any parachain. |
| 53 | +const DOT = { |
| 54 | + parents: 1, |
| 55 | + interior: XcmV3Junctions.Here(), |
| 56 | +}; |
| 57 | +// DOT has 10 decimals. |
| 58 | +const DOT_UNITS = 10_000_000_000n; |
| 59 | + |
| 60 | +// The DOT to withdraw for both fees and transfer. |
| 61 | +const dotToWithdraw = { |
| 62 | + id: DOT, |
| 63 | + fun: XcmV3MultiassetFungibility.Fungible(10n * DOT_UNITS), |
| 64 | +}; |
| 65 | +// The DOT to use for local fee payment. |
| 66 | +const dotToPayFees = { |
| 67 | + id: DOT, |
| 68 | + fun: XcmV3MultiassetFungibility.Fungible(1n * DOT_UNITS), |
| 69 | +}; |
| 70 | +// The location of the People Chain from Asset Hub. |
| 71 | +const destination = { |
| 72 | + parents: 1, |
| 73 | + interior: XcmV3Junctions.X1(XcmV3Junction.Parachain(PEOPLE_PARA_ID)), |
| 74 | +}; |
| 75 | +// Pay for fees on the People Chain with teleported DOT. |
| 76 | +// This is specified independently of the transferred assets since they're used |
| 77 | +// exclusively for fees. Also because fees can be paid in a different |
| 78 | +// asset from the transferred assets. |
| 79 | +const remoteFees = Enum( |
| 80 | + 'Teleport', |
| 81 | + XcmV5AssetFilter.Definite([ |
| 82 | + { |
| 83 | + id: DOT, |
| 84 | + fun: XcmV3MultiassetFungibility.Fungible(1n * DOT_UNITS), |
| 85 | + }, |
| 86 | + ]) |
| 87 | +); |
| 88 | +// No need to preserve origin for this example. |
| 89 | +const preserveOrigin = false; |
| 90 | +// The assets to transfer are whatever remains in the |
| 91 | +// holding register at the time of executing the `InitiateTransfer` |
| 92 | +// instruction. DOT in this case, teleported. |
| 93 | +const assets = [ |
| 94 | + Enum('Teleport', XcmV5AssetFilter.Wild(XcmV5WildAsset.AllCounted(1))), |
| 95 | +]; |
| 96 | +// The beneficiary is the same account but on the People Chain. |
| 97 | +// This is a very common pattern for one public/private key pair |
| 98 | +// to hold assets on multiple chains. |
| 99 | +const beneficiary = FixedSizeBinary.fromBytes(keyPair.publicKey); |
| 100 | +// The call to be executed on the destination chain. |
| 101 | +// It's a simple remark with an event. |
| 102 | +// Create the call on Asset Hub since the system pallet is present in |
| 103 | +// every runtime, but if using any other pallet, connect to |
| 104 | +// the destination chain and create the call there. |
| 105 | +const remark = Binary.fromText('Hello, cross-chain!'); |
| 106 | +const call = await ahpApi.tx.System.remark_with_event({ |
| 107 | + remark, |
| 108 | +}).getEncodedData(); |
| 109 | +// The XCM to be executed on the destination chain. |
| 110 | +// It's basically depositing everything to the beneficiary. |
| 111 | +const remoteXcm = [ |
| 112 | + XcmV5Instruction.Transact({ |
| 113 | + origin_kind: XcmV2OriginKind.SovereignAccount(), |
| 114 | + fallback_max_weight: undefined, |
| 115 | + call, |
| 116 | + }), |
| 117 | + XcmV5Instruction.RefundSurplus(), |
| 118 | + XcmV5Instruction.DepositAsset({ |
| 119 | + assets: XcmV5AssetFilter.Wild(XcmV5WildAsset.AllCounted(1)), |
| 120 | + beneficiary: { |
| 121 | + parents: 0, |
| 122 | + interior: XcmV5Junctions.X1( |
| 123 | + XcmV5Junction.AccountId32({ |
| 124 | + id: beneficiary, |
| 125 | + network: undefined, |
| 126 | + }) |
| 127 | + ), |
| 128 | + }, |
| 129 | + }), |
| 130 | +]; |
| 131 | + |
| 132 | +// The message assembles all the previously defined parameters. |
| 133 | +const xcm = XcmVersionedXcm.V5([ |
| 134 | + XcmV5Instruction.WithdrawAsset([dotToWithdraw]), |
| 135 | + XcmV5Instruction.PayFees({ asset: dotToPayFees }), |
| 136 | + XcmV5Instruction.InitiateTransfer({ |
| 137 | + destination, |
| 138 | + remote_fees: remoteFees, |
| 139 | + preserve_origin: preserveOrigin, |
| 140 | + assets, |
| 141 | + remote_xcm: remoteXcm, |
| 142 | + }), |
| 143 | + // Return any leftover fees from the fees register back to holding. |
| 144 | + XcmV5Instruction.RefundSurplus(), |
| 145 | + // Deposit remaining assets (refunded fees) to the originating account. |
| 146 | + // Using AllCounted(1) since only one asset type (DOT) remains - a minor optimization. |
| 147 | + XcmV5Instruction.DepositAsset({ |
| 148 | + assets: XcmV5AssetFilter.Wild(XcmV5WildAsset.AllCounted(1)), |
| 149 | + beneficiary: { |
| 150 | + parents: 0, |
| 151 | + interior: XcmV5Junctions.X1( |
| 152 | + XcmV5Junction.AccountId32({ |
| 153 | + id: beneficiary, // The originating account. |
| 154 | + network: undefined, |
| 155 | + }) |
| 156 | + ), |
| 157 | + }, |
| 158 | + }), |
| 159 | +]); |
| 160 | + |
| 161 | +// The XCM weight is needed to set the `max_weight` parameter |
| 162 | +// on the actual `PolkadotXcm.execute()` call. |
| 163 | +const weightResult = await ahpApi.apis.XcmPaymentApi.query_xcm_weight(xcm); |
| 164 | + |
| 165 | +if (weightResult.success) { |
| 166 | + const weight = weightResult.success |
| 167 | + ? weightResult.value |
| 168 | + : { ref_time: 0n, proof_size: 0n }; |
| 169 | + |
| 170 | + console.dir(weight); |
| 171 | + |
| 172 | + // The actual transaction to submit. |
| 173 | + // This tells Asset Hub to execute the XCM. |
| 174 | + const tx = ahpApi.tx.PolkadotXcm.execute({ |
| 175 | + message: xcm, |
| 176 | + max_weight: weight, |
| 177 | + }); |
| 178 | + |
| 179 | + // Sign and propagate to the network. |
| 180 | + const result = await tx.signAndSubmit(polkadotSigner); |
| 181 | + console.log(stringify(result)); |
| 182 | +} |
| 183 | + |
| 184 | +client.destroy(); |
| 185 | + |
| 186 | +// A helper function to print numbers inside of the result. |
| 187 | +function stringify(obj: any) { |
| 188 | + return JSON.stringify( |
| 189 | + obj, |
| 190 | + (_, v) => (typeof v === 'bigint' ? v.toString() : v), |
| 191 | + 2 |
| 192 | + ); |
| 193 | +} |
0 commit comments