From feabfa60e876328fbd244979d10cb81d8c33a5ac Mon Sep 17 00:00:00 2001 From: Kevin Neilson Date: Fri, 18 Oct 2024 17:16:06 -0700 Subject: [PATCH 01/11] add --- .../dry-run/dry-run-call-return-data.json | 46 +++++++++++++ .../send-execute-xcm/dry-run/dry-run-call.js | 41 +++++++++++ .../dry-run/dry-run-xcm-return-data.json | 60 ++++++++++++++++ .../send-execute-xcm/dry-run/dry-run-xcm.js | 62 +++++++++++++++++ .../interoperability/xcm/send-execute-xcm.md | 69 +++++++++++++++++++ 5 files changed, 278 insertions(+) create mode 100644 .snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-call-return-data.json create mode 100644 .snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-call.js create mode 100644 .snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-xcm-return-data.json create mode 100644 .snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-xcm.js diff --git a/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-call-return-data.json b/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-call-return-data.json new file mode 100644 index 000000000..ca82eae4b --- /dev/null +++ b/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-call-return-data.json @@ -0,0 +1,46 @@ +Ok: { + executionResult: { + Ok: { + actualWeight: { + refTime: 7,301,734,000 + proofSize: 20,928 + } + paysFee: Yes + } + } + emittedEvents: [ + { + method: Burned + section: balances + index: 0x030b + data: { + who: 0x88bcE0b038eFFa09e58fE6d24fDe4b5Af21aa798 + amount: 100,000,000,000,000,000 + } + } + { + method: Minted + section: balances + index: 0x030a + data: { + who: 0x3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0 + amount: 100,000,000,000,000,000 + } + } + { + method: Attempted + section: polkadotXcm + index: 0x1c00 + data: { + outcome: { + Complete: { + used: { + refTime: 7,250,000,000 + proofSize: 19,374 + } + } + } + } + } + ] // Additional events returned here + // Omitted for clarity \ No newline at end of file diff --git a/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-call.js b/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-call.js new file mode 100644 index 000000000..74d4494f1 --- /dev/null +++ b/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-call.js @@ -0,0 +1,41 @@ +import { ApiPromise, WsProvider } from '@polkadot/api'; +import { hexToU8a } from '@polkadot/util'; + +const main = async () => { + try { + // Construct API provider + const wsProvider = new WsProvider('INSERT_WSS_ENDPOINT'); + const api = await ApiPromise.create({ provider: wsProvider }); + + console.log('Connected to the API. Preparing dry run call...'); + + // Create a test account (you should replace this with an actual account) + const testAccount = api.createType( + 'AccountId20', + '0x88bcE0b038eFFa09e58fE6d24fDe4b5Af21aa798' + ); + + // The call data (replace with your actual call data) + const callData = + '0x1c030408000400010403001300008a5d784563010d010204000103003cd0a705a2dc65e5b1e1205896baa2be8a07c6e007803822b001ba2e0100'; // Your hex-encoded call data + + // Convert hex to Uint8Array + const callDataU8a = hexToU8a(callData); + + // Perform the dry run call + const result = await api.call.dryRunApi.dryRunCall( + { system: { Signed: testAccount } }, // origin + callDataU8a // call + ); + + console.log('Dry run result:', result.toHuman()); + + // Disconnect the API + await api.disconnect(); + console.log('Disconnected from the API.'); + } catch (error) { + console.error('An error occurred:', error); + } +}; + +main().catch(console.error); diff --git a/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-xcm-return-data.json b/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-xcm-return-data.json new file mode 100644 index 000000000..a25d909f7 --- /dev/null +++ b/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-xcm-return-data.json @@ -0,0 +1,60 @@ +Ok: { + executionResult: { + Complete: { + used: { + refTime: 76,473,048,000 + proofSize: 222,483 + } + } + } + emittedEvents: [ + { + method: Burned + section: assets + index: 0x1d03 + data: { + assetId: 42,259,045,809,535,163,221,576,417,993,425,387,648 + owner: 0x506172656E740000000000000000000000000000 + balance: 1,000,000,000,000 + } + } + { + method: NewAccount + section: system + index: 0x0003 + data: { + account: 0x1Ccc29B6C837be6A0C03d544b5Aa8580f9fb9489 + } + } + { + method: Issued + section: assets + index: 0x1d01 + data: { + assetId: 42,259,045,809,535,163,221,576,417,993,425,387,648 + owner: 0x1Ccc29B6C837be6A0C03d544b5Aa8580f9fb9489 + amount: 959,944,978,002 + } + } + { + method: Issued + section: assets + index: 0x1d01 + data: { + assetId: 42,259,045,809,535,163,221,576,417,993,425,387,648 + owner: 0x6d6F646c70632f74727372790000000000000000 + amount: 40,055,021,998 + } + } + ] + forwardedXcms: [ + [ + { + V4: { + parents: 1 + interior: Here + } + } + [] + ] // Additional events returned here + // Omitted for clarity \ No newline at end of file diff --git a/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-xcm.js b/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-xcm.js new file mode 100644 index 000000000..3476f5a82 --- /dev/null +++ b/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-xcm.js @@ -0,0 +1,62 @@ +import { ApiPromise, WsProvider } from '@polkadot/api'; + +const main = async () => { + try { + // Construct API provider + const wsProvider = new WsProvider('INSERT_WSS_ENDPOINT'); + const api = await ApiPromise.create({ provider: wsProvider }); + console.log('Connected to the API. Preparing dry run XCM call...'); + + // Define the origin + const origin = { V4: { parents: 1, interior: 'Here' } }; + + const assetMultiLocation = { + parents: 0, + interior: { X1: { PalletInstance: 3 } }, + }; // The asset's location (adjust PalletInstance as needed) + + const amountToSend = 1000000000000; // Adjust this value as needed + + const message = { + V4: [ + { + WithdrawAsset: [ + { + id: { parents: 1, interior: 'Here' }, + fun: { Fungible: amountToSend }, + }, + ], + }, + { + BuyExecution: { + fees: { + id: { parents: 1, interior: 'Here' }, + fun: { Fungible: amountToSend }, + }, + weightLimit: { Unlimited: null }, + }, + }, + { + ClearOrigin: null, + }, + ], + }; + + // Perform the dry run XCM call + const result = await api.call.dryRunApi.dryRunXcm(origin, message); + + // Use JSON.stringify for better output formatting + console.log( + 'Dry run XCM result:', + JSON.stringify(result.toJSON(), null, 2) + ); + + // Disconnect the API + await api.disconnect(); + console.log('Disconnected from the API.'); + } catch (error) { + console.error('An error occurred:', error); + } +}; + +main().catch(console.error); diff --git a/builders/interoperability/xcm/send-execute-xcm.md b/builders/interoperability/xcm/send-execute-xcm.md index 8484b0e95..da2c6e4d6 100644 --- a/builders/interoperability/xcm/send-execute-xcm.md +++ b/builders/interoperability/xcm/send-execute-xcm.md @@ -180,6 +180,75 @@ Now that you have the values for each of the parameters, you can write the scrip Once the transaction is processed, the 0.1 DEV tokens should be withdrawn from Alice's account along with the associated XCM fees, and the destination account should have received 0.1 DEV tokens in their account. A `polkadotXcm.Attempted` event will be emitted with the outcome. + +### Test an XCM Message with the XCM Dry Run API {: #test-an-xcm-message-with-the-dry-run-api } + +The XCM Dry Run API is an easy and convenient way to test the integrity of your XCM message without incurring any transaction fees. The XCM Dry Run API can be accessed from the [Runtime Calls](https://polkadot.js.org/apps/#/runtime){target=\_blank} tab of the **Developer** section of Polkadot.js Apps. + +#### Dry Run Call API Method {: #dry-run-call-api-method } + +This method takes as a parameter the origin and the call data and returns an execution result, actual weight, and event data. + +```javascript +const testAccount = api.createType( + 'AccountId20', + '0x88bcE0b038eFFa09e58fE6d24fDe4b5Af21aa798' +); +const callData = + '0x1c030408000400010403001300008a5d784563010d010204000103003cd0a705a2dc65e5b1e1205896baa2be8a07c6e007803822b001ba2e0100'; +const callDataU8a = hexToU8a(callData); + +const result = await api.call.dryRunApi.dryRunCall( + { system: { Signed: testAccount } }, + callDataU8a +); +``` + +??? code "View the complete script" + + ```js + --8<-- 'code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-call.js' + ``` + +Upon calling the XCM Dry Run API, the method will tell you whether the call would be successful and returns the event data that would be emitted if the XCM were to be actually submitted on chain. You can view the initial output of the `dryRunCall` below. + +??? code "View the complete output" + + ```json + --8<-- 'code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-call-return-data.json' + ``` + +#### Dry Run XCM API Method {: #dry-run-xcm-api-method } + +The `dryRunXCM` method of the XCM Dry Run API takes a full XCM message as a parameter instead of an encoded call, as well as the origin of the message. + +`dryRunXCM` takes as a parameter the origin and the XCM message and returns an execution result, actual weight, and event data. + +```javascript +// Define the origin +const origin = { V4: { parents: 1, interior: 'Here' } }; + +const message = // Insert XCM Message Here; + +// Perform the dry run XCM call +const result = await api.call.dryRunApi.dryRunXcm(origin, message); +``` + +??? code "View the complete script" + + ```js + --8<-- 'code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-xcm.js' + ``` + +Upon calling the XCM Dry Run API, the method will tell you whether the call would be successful and returns the event data that would be emitted if the XCM were to be actually submitted on chain. You can view the initial output of the `dryRunXCM` below. + +??? code "View the complete output" + + ```json + --8<-- 'code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-xcm-return-data.json' + ``` + + ### Execute an XCM Message with the XCM Utilities Precompile {: #execute-xcm-utils-precompile } In this section, you'll use the `xcmExecute` function of the [XCM Utilities Precompile](/builders/interoperability/xcm/xcm-utils/){target=\_blank}, which is only supported on Moonbase Alpha, to execute an XCM message locally. The XCM Utilities Precompile is located at the following address: From 856f440c04711a37a4817967e6541d39da8cccc1 Mon Sep 17 00:00:00 2001 From: Kevin Neilson Date: Fri, 18 Oct 2024 17:30:14 -0700 Subject: [PATCH 02/11] add --- .../xcm/core-concepts/location-to-account.js | 25 ++++++++++++++++++ .../xcm/core-concepts/multilocations.md | 26 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 .snippets/code/builders/interoperability/xcm/core-concepts/location-to-account.js diff --git a/.snippets/code/builders/interoperability/xcm/core-concepts/location-to-account.js b/.snippets/code/builders/interoperability/xcm/core-concepts/location-to-account.js new file mode 100644 index 000000000..cb50b36b2 --- /dev/null +++ b/.snippets/code/builders/interoperability/xcm/core-concepts/location-to-account.js @@ -0,0 +1,25 @@ +import { ApiPromise, WsProvider } from '@polkadot/api'; + +const main = async () => { + // Construct API provider + const wsProvider = new WsProvider('INSERT_WSS_ENDPOINT'); + const api = await ApiPromise.create({ provider: wsProvider }); + + // Define the multilocation parameter + const multilocation = { + V4: { + parents: 1, + interior: 'Here', + }, + }; + + // Query the locationToAccountApi using convertLocation method + const result = + await api.call.locationToAccountApi.convertLocation(multilocation); + console.log('Conversion result:', result.toHuman()); + + // Disconnect the API + await api.disconnect(); +}; + +main().catch(console.error); diff --git a/builders/interoperability/xcm/core-concepts/multilocations.md b/builders/interoperability/xcm/core-concepts/multilocations.md index 1e53bdafd..6725083dc 100644 --- a/builders/interoperability/xcm/core-concepts/multilocations.md +++ b/builders/interoperability/xcm/core-concepts/multilocations.md @@ -357,3 +357,29 @@ To target another parachain (for example, a parachain that has an ID of 1234) fr }, }; ``` + +### Location to Account API {: #location-to-account-api } + +The Location to Account API is an easy way to convert a multilocation into an `AccountID20` address. The Location to Account API can be accessed from the [Runtime Calls](https://polkadot.js.org/apps/#/runtime){target=\_blank} tab of the **Developer** section of Polkadot.js Apps. The `convertLocation` method of the Location to Account API takes a multilocation as a parameter and returns an `AccountID20` address. + +```javascript +// Query the locationToAccountApi using convertLocation method +const result = + await api.call.locationToAccountApi.convertLocation(multilocation); +console.log('Conversion result:', result.toHuman()); +``` + +You can view the complete script below. + +??? code "View the complete script" + + ```js + --8<-- 'code/builders/interoperability/xcm/core-concepts/location-to-account.js' + ``` + +The method will return the `AccountID20` address corresponding to the provided multilocation as follows: + +```bash +Conversion result: { Ok: '0x506172656E740000000000000000000000000000' } +``` + From bf9c5dab4633f74a045e77743ed7e48ed588a9b2 Mon Sep 17 00:00:00 2001 From: Kevin Neilson Date: Wed, 30 Oct 2024 11:24:49 +0100 Subject: [PATCH 03/11] Update builders/interoperability/xcm/send-execute-xcm.md Co-authored-by: albertov19 <64150856+albertov19@users.noreply.github.com> --- builders/interoperability/xcm/send-execute-xcm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builders/interoperability/xcm/send-execute-xcm.md b/builders/interoperability/xcm/send-execute-xcm.md index da2c6e4d6..d5e3a840c 100644 --- a/builders/interoperability/xcm/send-execute-xcm.md +++ b/builders/interoperability/xcm/send-execute-xcm.md @@ -228,7 +228,7 @@ The `dryRunXCM` method of the XCM Dry Run API takes a full XCM message as a para // Define the origin const origin = { V4: { parents: 1, interior: 'Here' } }; -const message = // Insert XCM Message Here; +const message = []; // Insert XCM Message Here // Perform the dry run XCM call const result = await api.call.dryRunApi.dryRunXcm(origin, message); From 5496e7752b3ce5f9fdcee16bbbd18f4e79d1d276 Mon Sep 17 00:00:00 2001 From: Kevin Neilson Date: Tue, 10 Dec 2024 18:27:17 -0800 Subject: [PATCH 04/11] extensive revisions --- .../dry-run/dry-run-call-return-data.json | 121 ++++++++++++------ .../send-execute-xcm/dry-run/dry-run-call.js | 5 +- .../dry-run/dry-run-xcm-return-data.json | 89 +++++-------- .../send-execute-xcm/dry-run/dry-run-xcm.js | 35 +++-- 4 files changed, 143 insertions(+), 107 deletions(-) diff --git a/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-call-return-data.json b/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-call-return-data.json index ca82eae4b..4f4988ef2 100644 --- a/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-call-return-data.json +++ b/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-call-return-data.json @@ -1,46 +1,93 @@ -Ok: { - executionResult: { - Ok: { - actualWeight: { - refTime: 7,301,734,000 - proofSize: 20,928 - } - paysFee: Yes +Dry run XCM result: { + "ok": { + "executionResult": { + "ok": { + "actualWeight": { + "refTime": 7301615000, + "proofSize": 20928 + }, + "paysFee": "Yes" } - } - emittedEvents: [ + }, + "emittedEvents": [ { - method: Burned - section: balances - index: 0x030b - data: { - who: 0x88bcE0b038eFFa09e58fE6d24fDe4b5Af21aa798 - amount: 100,000,000,000,000,000 - } - } + "index": "0x030b", + "data": [ + "0x88bcE0b038eFFa09e58fE6d24fDe4b5Af21aa798", + "0x0000000000000000016345785d8a0000" + ] + }, { - method: Minted - section: balances - index: 0x030a - data: { - who: 0x3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0 - amount: 100,000,000,000,000,000 - } - } + "index": "0x0300", + "data": [ + "0x3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0", + "0x0000000000000000016345785d8a0000" + ] + }, + { + "index": "0x030a", + "data": [ + "0x3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0", + "0x0000000000000000016345785d8a0000" + ] + }, { - method: Attempted - section: polkadotXcm - index: 0x1c00 - data: { - outcome: { - Complete: { - used: { - refTime: 7,250,000,000 - proofSize: 19,374 + "index": "0x1c00", + "data": [ + { + "complete": { + "used": { + "refTime": 7250000000, + "proofSize": 19374 } } } - } + ] } - ] // Additional events returned here + ], + "localXcm": { + "v4": [ + { + "withdrawAsset": [ + { + "id": { + "parents": 0, + "interior": { + "x1": [ + { + "palletInstance": 3 + } + ] + } + }, + "fun": { + "fungible": "0x0000000000000000016345785d8a0000" + } + } + ] + }, + { + "depositAsset": { + "assets": { + "wild": { + "allCounted": 1 + } + }, + "beneficiary": { + "parents": 0, + "interior": { + "x1": [ + { + "accountKey20": { + "network": null, + "key": "0x3cd0a705a2dc65e5b1e1205896baa2be8a07c6e0" + } + } + ] + } + } + } + } + ] + } // Additional events returned here // Omitted for clarity \ No newline at end of file diff --git a/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-call.js b/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-call.js index 74d4494f1..c3779b5a6 100644 --- a/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-call.js +++ b/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-call.js @@ -28,7 +28,10 @@ const main = async () => { callDataU8a // call ); - console.log('Dry run result:', result.toHuman()); + console.log( + 'Dry run XCM result:', + JSON.stringify(result.toJSON(), null, 2) + ); // Disconnect the API await api.disconnect(); diff --git a/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-xcm-return-data.json b/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-xcm-return-data.json index a25d909f7..7ad32815a 100644 --- a/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-xcm-return-data.json +++ b/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-xcm-return-data.json @@ -1,60 +1,37 @@ -Ok: { - executionResult: { - Complete: { - used: { - refTime: 76,473,048,000 - proofSize: 222,483 - } - } - } - emittedEvents: [ - { - method: Burned - section: assets - index: 0x1d03 - data: { - assetId: 42,259,045,809,535,163,221,576,417,993,425,387,648 - owner: 0x506172656E740000000000000000000000000000 - balance: 1,000,000,000,000 - } - } - { - method: NewAccount - section: system - index: 0x0003 - data: { - account: 0x1Ccc29B6C837be6A0C03d544b5Aa8580f9fb9489 - } - } - { - method: Issued - section: assets - index: 0x1d01 - data: { - assetId: 42,259,045,809,535,163,221,576,417,993,425,387,648 - owner: 0x1Ccc29B6C837be6A0C03d544b5Aa8580f9fb9489 - amount: 959,944,978,002 - } - } - { - method: Issued - section: assets - index: 0x1d01 - data: { - assetId: 42,259,045,809,535,163,221,576,417,993,425,387,648 - owner: 0x6d6F646c70632f74727372790000000000000000 - amount: 40,055,021,998 +Dry run XCM result: { + "ok": { + "executionResult": { + "complete": { + "used": { + "refTime": 76473048000, + "proofSize": 222483 + } } - } - ] - forwardedXcms: [ - [ + }, + "emittedEvents": [ { - V4: { - parents: 1 - interior: Here - } + "index": "0x1d03", + "data": [ + "0x1fcacbd218edc0eba20fc2308c778080", + "0x506172656E740000000000000000000000000000", + 1000000000000 + ] + }, + { + "index": "0x1d01", + "data": [ + "0x1fcacbd218edc0eba20fc2308c778080", + "0x3B939FeaD1557C741Ff06492FD0127bd287A421e", + 959944978002 + ] + }, + { + "index": "0x1d01", + "data": [ + "0x1fcacbd218edc0eba20fc2308c778080", + "0x6d6F646c70632f74727372790000000000000000", + 40055021998 + ] } - [] - ] // Additional events returned here + ], // Additional events returned here // Omitted for clarity \ No newline at end of file diff --git a/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-xcm.js b/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-xcm.js index 3476f5a82..956108bd5 100644 --- a/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-xcm.js +++ b/.snippets/code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-xcm.js @@ -1,4 +1,5 @@ import { ApiPromise, WsProvider } from '@polkadot/api'; +import { hexToU8a } from '@polkadot/util'; const main = async () => { try { @@ -9,13 +10,7 @@ const main = async () => { // Define the origin const origin = { V4: { parents: 1, interior: 'Here' } }; - - const assetMultiLocation = { - parents: 0, - interior: { X1: { PalletInstance: 3 } }, - }; // The asset's location (adjust PalletInstance as needed) - - const amountToSend = 1000000000000; // Adjust this value as needed + const amountToSend = 1000000000000; const message = { V4: [ @@ -37,21 +32,35 @@ const main = async () => { }, }, { - ClearOrigin: null, - }, + DepositAsset: { + assets: { Wild: { AllOf: { id: { parents: 1, interior: 'Here' } } } }, + maxAssets: 1, + beneficiary: { + parents: 0, + interior: { + X1: [ + { + AccountKey20: { + network: null, + key: hexToU8a('0x3B939FeaD1557C741Ff06492FD0127bd287A421e') + } + } + ] + } + } + } + } ], }; // Perform the dry run XCM call const result = await api.call.dryRunApi.dryRunXcm(origin, message); - - // Use JSON.stringify for better output formatting + console.log( 'Dry run XCM result:', JSON.stringify(result.toJSON(), null, 2) ); - // Disconnect the API await api.disconnect(); console.log('Disconnected from the API.'); } catch (error) { @@ -59,4 +68,4 @@ const main = async () => { } }; -main().catch(console.error); +main().catch(console.error); \ No newline at end of file From 0030ec5bc55ff62faf2274e0fce580462de7c3ba Mon Sep 17 00:00:00 2001 From: Kevin Neilson Date: Tue, 10 Dec 2024 18:45:07 -0800 Subject: [PATCH 05/11] rev --- builders/interoperability/xcm/.pages | 2 +- .../interoperability/xcm/send-execute-xcm.md | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/builders/interoperability/xcm/.pages b/builders/interoperability/xcm/.pages index 88401330b..b4efb3ced 100644 --- a/builders/interoperability/xcm/.pages +++ b/builders/interoperability/xcm/.pages @@ -7,5 +7,5 @@ nav: - xc20 - remote-execution - 'XCM SDK': 'https://moonbeam-foundation.github.io/xcm-sdk/latest/' - - 'Send & Execute XCM Messages': 'send-execute-xcm.md' + - 'Send, Execute, & Test XCMs': 'send-execute-xcm.md' - 'XCM Utilities': 'xcm-utils.md' diff --git a/builders/interoperability/xcm/send-execute-xcm.md b/builders/interoperability/xcm/send-execute-xcm.md index d5e3a840c..aedd8cfc9 100644 --- a/builders/interoperability/xcm/send-execute-xcm.md +++ b/builders/interoperability/xcm/send-execute-xcm.md @@ -1,9 +1,9 @@ --- -title: Send & Execute XCM Messages -description: Learn how to build a custom XCM message, by combining and experimenting with different XCM instructions, and execute it locally on Moonbeam to see the results. +title: Send, Execute and Test XCM Messages +description: Build a custom XCM message, verify its construction and integrity using the XCM Dry Run API, then execute it locally on Moonbeam to observe the results. --- -# Send and Execute XCM Messages +# Send, Execute, and Test XCM Messages ## Introduction {: #introduction } @@ -181,11 +181,11 @@ Now that you have the values for each of the parameters, you can write the scrip Once the transaction is processed, the 0.1 DEV tokens should be withdrawn from Alice's account along with the associated XCM fees, and the destination account should have received 0.1 DEV tokens in their account. A `polkadotXcm.Attempted` event will be emitted with the outcome. -### Test an XCM Message with the XCM Dry Run API {: #test-an-xcm-message-with-the-dry-run-api } +## Test an XCM Message with the Dry Run API {: #test-an-xcm-message-with-the-dry-run-api } The XCM Dry Run API is an easy and convenient way to test the integrity of your XCM message without incurring any transaction fees. The XCM Dry Run API can be accessed from the [Runtime Calls](https://polkadot.js.org/apps/#/runtime){target=\_blank} tab of the **Developer** section of Polkadot.js Apps. -#### Dry Run Call API Method {: #dry-run-call-api-method } +### Dry Run Call API Method {: #dry-run-call-api-method } This method takes as a parameter the origin and the call data and returns an execution result, actual weight, and event data. @@ -218,7 +218,7 @@ Upon calling the XCM Dry Run API, the method will tell you whether the call woul --8<-- 'code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-call-return-data.json' ``` -#### Dry Run XCM API Method {: #dry-run-xcm-api-method } +### Dry Run XCM API Method {: #dry-run-xcm-api-method } The `dryRunXCM` method of the XCM Dry Run API takes a full XCM message as a parameter instead of an encoded call, as well as the origin of the message. @@ -249,7 +249,7 @@ Upon calling the XCM Dry Run API, the method will tell you whether the call woul ``` -### Execute an XCM Message with the XCM Utilities Precompile {: #execute-xcm-utils-precompile } +## Execute an XCM Message with the XCM Utilities Precompile {: #execute-xcm-utils-precompile } In this section, you'll use the `xcmExecute` function of the [XCM Utilities Precompile](/builders/interoperability/xcm/xcm-utils/){target=\_blank}, which is only supported on Moonbase Alpha, to execute an XCM message locally. The XCM Utilities Precompile is located at the following address: @@ -263,7 +263,7 @@ The `xcmExecute` function accepts two parameters: the SCALE encoded versioned XC First, you'll learn how to generate the encoded calldata, and then you'll learn how to use the encoded calldata to interact with the XCM Utilities Precompile. -#### Generate the Encoded Calldata of an XCM Message {: #generate-encoded-calldata } +### Generate the Encoded Calldata of an XCM Message {: #generate-encoded-calldata } To get the encoded calldata of the XCM message, you can create a script similar to the one you created in the [Execute an XCM Message with the Polkadot.js API](#execute-an-xcm-message-with-polkadotjs-api) section. Instead of building the message and sending the transaction, you'll build the message to get the encoded calldata. You'll take the following steps: @@ -280,7 +280,7 @@ The entire script is as follows: --8<-- 'code/builders/interoperability/xcm/send-execute-xcm/execute/generate-encoded-calldata.js' ``` -#### Execute the XCM Message {: #execute-xcm-message } +### Execute the XCM Message {: #execute-xcm-message } Now that you have the SCALE encoded XCM message, you can use the following code snippets to programmatically call the `xcmExecute` function of the XCM Utilities Precompile using your [Ethereum library](/builders/ethereum/libraries/){target=\_blank} of choice. Generally speaking, you'll take the following steps: From 1149ba937fe3a527ab285e6fe2ba79c4ae4466a5 Mon Sep 17 00:00:00 2001 From: Kevin Neilson Date: Tue, 10 Dec 2024 18:56:35 -0800 Subject: [PATCH 06/11] revision --- .../polkadot-js-api/dry-run-result.json | 13 ++++++ .../libraries/polkadot-js-api/dry-run.js | 44 +++++++++++++++++++ .../substrate/libraries/polkadot-js-api.md | 35 +++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 .snippets/code/builders/substrate/libraries/polkadot-js-api/dry-run-result.json create mode 100644 .snippets/code/builders/substrate/libraries/polkadot-js-api/dry-run.js diff --git a/.snippets/code/builders/substrate/libraries/polkadot-js-api/dry-run-result.json b/.snippets/code/builders/substrate/libraries/polkadot-js-api/dry-run-result.json new file mode 100644 index 000000000..ed7d472e5 --- /dev/null +++ b/.snippets/code/builders/substrate/libraries/polkadot-js-api/dry-run-result.json @@ -0,0 +1,13 @@ +Dry run XCM result: { + "ok": { + "executionResult": { + "ok": { + "actualWeight": null, + "paysFee": "Yes" + } + }, + "emittedEvents": [], + "localXcm": null, + + // Additional data returned here + // Omitted for clarity \ No newline at end of file diff --git a/.snippets/code/builders/substrate/libraries/polkadot-js-api/dry-run.js b/.snippets/code/builders/substrate/libraries/polkadot-js-api/dry-run.js new file mode 100644 index 000000000..62bc4b709 --- /dev/null +++ b/.snippets/code/builders/substrate/libraries/polkadot-js-api/dry-run.js @@ -0,0 +1,44 @@ +import { ApiPromise, WsProvider } from '@polkadot/api'; +import { hexToU8a } from '@polkadot/util'; + +const main = async () => { + try { + // Construct API provider + const wsProvider = new WsProvider('INSERT_WSS_ENDPOINT'); + const api = await ApiPromise.create({ provider: wsProvider }); + + console.log('Connected to the API. Preparing dry run call...'); + + // Create a test account (you should replace this with an actual account) + const testAccount = api.createType( + 'AccountId20', + '0x88bcE0b038eFFa09e58fE6d24fDe4b5Af21aa798' + ); + + // The call data (replace with your actual call data) + const callData = + '0x030088bce0b038effa09e58fe6d24fde4b5af21aa79813000064a7b3b6e00d'; // Your hex-encoded call data + + // Convert hex to Uint8Array + const callDataU8a = hexToU8a(callData); + + // Perform the dry run call + const result = await api.call.dryRunApi.dryRunCall( + { system: { Signed: testAccount } }, // origin + callDataU8a // call + ); + + console.log( + 'Dry run XCM result:', + JSON.stringify(result.toJSON(), null, 2) + ); + + // Disconnect the API + await api.disconnect(); + console.log('Disconnected from the API.'); + } catch (error) { + console.error('An error occurred:', error); + } +}; + +main().catch(console.error); diff --git a/builders/substrate/libraries/polkadot-js-api.md b/builders/substrate/libraries/polkadot-js-api.md index be3303ffe..6fc722ebd 100644 --- a/builders/substrate/libraries/polkadot-js-api.md +++ b/builders/substrate/libraries/polkadot-js-api.md @@ -277,6 +277,41 @@ There are a number of ways to add an account to the keyring instance, including --8<-- 'code/builders/substrate/libraries/polkadot-js-api/adding-accounts-private-key.js' ``` +## Dry Run API {: #dry-run-api } + +The Dry Run API is an easy and convenient way to test the integrity of a call without incurring any transaction fees. The XCM Dry Run API can be accessed from the [Runtime Calls](https://polkadot.js.org/apps/#/runtime){target=\_blank} tab of the **Developer** section of Polkadot.js Apps. While primarily intended for the [testing of XCM messages](/builders/interoperability/xcm/send-execute-xcm/#test-an-xcm-message-with-the-dry-run-api), the Dry Run API can be used to test any arbitrary call. + +This method takes as a parameter the origin and the call data and returns an execution result, actual weight, and event data. + +```javascript +const testAccount = api.createType( + 'AccountId20', + '0x88bcE0b038eFFa09e58fE6d24fDe4b5Af21aa798' +); +const callData = + '0x030088bce0b038effa09e58fe6d24fde4b5af21aa79813000064a7b3b6e00d'; +const callDataU8a = hexToU8a(callData); + +const result = await api.call.dryRunApi.dryRunCall( + { system: { Signed: testAccount } }, + callDataU8a +); +``` + +??? code "View the complete script" + + ```js + --8<-- 'code/builders/substrate/libraries/polkadot-js-api/dry-run.js' + ``` + +Upon calling the XCM Dry Run API, the method will tell you whether the call would be successful and returns the event data that would be emitted if the XCM were to be actually submitted on chain. You can view the initial output of the `dryRunCall` below. + +??? code "View the complete output" + + ```json + --8<-- 'code/builders/substrate/libraries/polkadot-js-api/dry-run-result.json' + ``` + ## Send Transactions on Moonbeam {: #transactions } Transaction endpoints are exposed on endpoints generally of the form `api.tx..`, where the module and method decorations are generated through metadata. These allow you to submit transactions for inclusion in blocks, be it transfers, interacting with pallets, or anything else Moonbeam supports. You can see a list of all available endpoints by examining the `api.tx` object, for example via: From c4af81b2b648e78289771d3f82fecbebd8e54fec Mon Sep 17 00:00:00 2001 From: Kevin Neilson Date: Tue, 10 Dec 2024 18:58:55 -0800 Subject: [PATCH 07/11] revision --- builders/substrate/libraries/polkadot-js-api.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/builders/substrate/libraries/polkadot-js-api.md b/builders/substrate/libraries/polkadot-js-api.md index 6fc722ebd..f473299a3 100644 --- a/builders/substrate/libraries/polkadot-js-api.md +++ b/builders/substrate/libraries/polkadot-js-api.md @@ -279,9 +279,9 @@ There are a number of ways to add an account to the keyring instance, including ## Dry Run API {: #dry-run-api } -The Dry Run API is an easy and convenient way to test the integrity of a call without incurring any transaction fees. The XCM Dry Run API can be accessed from the [Runtime Calls](https://polkadot.js.org/apps/#/runtime){target=\_blank} tab of the **Developer** section of Polkadot.js Apps. While primarily intended for the [testing of XCM messages](/builders/interoperability/xcm/send-execute-xcm/#test-an-xcm-message-with-the-dry-run-api), the Dry Run API can be used to test any arbitrary call. +The Dry Run API is an easy and convenient way to test the integrity of a call without incurring any transaction fees. The Dry Run API can be accessed from the [Runtime Calls](https://polkadot.js.org/apps/#/runtime){target=\_blank} tab of the **Developer** section of Polkadot.js Apps. While primarily intended for the [testing of XCM messages](/builders/interoperability/xcm/send-execute-xcm/#test-an-xcm-message-with-the-dry-run-api){target=\_blank} , the Dry Run API can be used to test any arbitrary call. -This method takes as a parameter the origin and the call data and returns an execution result, actual weight, and event data. +This method takes as a parameter the origin and the call data and returns an execution result and additional event data. ```javascript const testAccount = api.createType( @@ -304,7 +304,7 @@ const result = await api.call.dryRunApi.dryRunCall( --8<-- 'code/builders/substrate/libraries/polkadot-js-api/dry-run.js' ``` -Upon calling the XCM Dry Run API, the method will tell you whether the call would be successful and returns the event data that would be emitted if the XCM were to be actually submitted on chain. You can view the initial output of the `dryRunCall` below. +Upon calling the Dry Run API, the method will tell you whether the call would be successful and returns the event data that would be emitted if the call were to be actually submitted on chain. You can view the initial output of the `dryRunCall` below. ??? code "View the complete output" From 0949b936d622776a75907797f4afc4d56976850e Mon Sep 17 00:00:00 2001 From: Kevin Neilson Date: Tue, 10 Dec 2024 19:02:43 -0800 Subject: [PATCH 08/11] run grammarly --- builders/interoperability/xcm/send-execute-xcm.md | 4 ++-- builders/substrate/libraries/polkadot-js-api.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/builders/interoperability/xcm/send-execute-xcm.md b/builders/interoperability/xcm/send-execute-xcm.md index aedd8cfc9..dbee2ac5c 100644 --- a/builders/interoperability/xcm/send-execute-xcm.md +++ b/builders/interoperability/xcm/send-execute-xcm.md @@ -1,6 +1,6 @@ --- title: Send, Execute and Test XCM Messages -description: Build a custom XCM message, verify its construction and integrity using the XCM Dry Run API, then execute it locally on Moonbeam to observe the results. +description: Build a custom XCM message, verify its construction and integrity using the XCM Dry Run API, and then execute it locally on Moonbeam to observe the results. --- # Send, Execute, and Test XCM Messages @@ -210,7 +210,7 @@ const result = await api.call.dryRunApi.dryRunCall( --8<-- 'code/builders/interoperability/xcm/send-execute-xcm/dry-run/dry-run-call.js' ``` -Upon calling the XCM Dry Run API, the method will tell you whether the call would be successful and returns the event data that would be emitted if the XCM were to be actually submitted on chain. You can view the initial output of the `dryRunCall` below. +Upon calling the XCM Dry Run API, the method will tell you whether the call would be successful and returns the event data that would be emitted if the call were actually submitted on chain. You can view the initial output of the `dryRunCall` below. ??? code "View the complete output" diff --git a/builders/substrate/libraries/polkadot-js-api.md b/builders/substrate/libraries/polkadot-js-api.md index f473299a3..c918107b1 100644 --- a/builders/substrate/libraries/polkadot-js-api.md +++ b/builders/substrate/libraries/polkadot-js-api.md @@ -281,7 +281,7 @@ There are a number of ways to add an account to the keyring instance, including The Dry Run API is an easy and convenient way to test the integrity of a call without incurring any transaction fees. The Dry Run API can be accessed from the [Runtime Calls](https://polkadot.js.org/apps/#/runtime){target=\_blank} tab of the **Developer** section of Polkadot.js Apps. While primarily intended for the [testing of XCM messages](/builders/interoperability/xcm/send-execute-xcm/#test-an-xcm-message-with-the-dry-run-api){target=\_blank} , the Dry Run API can be used to test any arbitrary call. -This method takes as a parameter the origin and the call data and returns an execution result and additional event data. +This method takes the origin and call data as parameters and returns an execution result and additional event data. ```javascript const testAccount = api.createType( @@ -304,7 +304,7 @@ const result = await api.call.dryRunApi.dryRunCall( --8<-- 'code/builders/substrate/libraries/polkadot-js-api/dry-run.js' ``` -Upon calling the Dry Run API, the method will tell you whether the call would be successful and returns the event data that would be emitted if the call were to be actually submitted on chain. You can view the initial output of the `dryRunCall` below. +Upon calling the Dry Run API, the method will tell you whether the call would be successful and returns the event data that would be emitted if the call were actually submitted on chain. You can view the initial output of the `dryRunCall` below. ??? code "View the complete output" From caee07b95612c5b7c7b4df3ac78bbed773be3035 Mon Sep 17 00:00:00 2001 From: Kevin Neilson Date: Mon, 23 Dec 2024 17:22:51 -0500 Subject: [PATCH 09/11] Update builders/interoperability/xcm/send-execute-xcm.md Co-authored-by: albertov19 <64150856+albertov19@users.noreply.github.com> --- builders/interoperability/xcm/send-execute-xcm.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builders/interoperability/xcm/send-execute-xcm.md b/builders/interoperability/xcm/send-execute-xcm.md index dbee2ac5c..e5c913f32 100644 --- a/builders/interoperability/xcm/send-execute-xcm.md +++ b/builders/interoperability/xcm/send-execute-xcm.md @@ -183,7 +183,7 @@ Once the transaction is processed, the 0.1 DEV tokens should be withdrawn from A ## Test an XCM Message with the Dry Run API {: #test-an-xcm-message-with-the-dry-run-api } -The XCM Dry Run API is an easy and convenient way to test the integrity of your XCM message without incurring any transaction fees. The XCM Dry Run API can be accessed from the [Runtime Calls](https://polkadot.js.org/apps/#/runtime){target=\_blank} tab of the **Developer** section of Polkadot.js Apps. +The XCM Dry Run API is an easy and convenient way to test the integrity of your XCM message without incurring any transaction fees. The XCM Dry Run API can be accessed from the [Runtime Calls](https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fwss.api.moonbeam.network#/runtime){target=\_blank} tab of the **Developer** section of Polkadot.js Apps. ### Dry Run Call API Method {: #dry-run-call-api-method } From 08c961a4b87b2525a9ed7b15e32b37b6f30af5b1 Mon Sep 17 00:00:00 2001 From: Kevin Neilson Date: Mon, 23 Dec 2024 17:23:02 -0500 Subject: [PATCH 10/11] Update builders/interoperability/xcm/core-concepts/multilocations.md Co-authored-by: albertov19 <64150856+albertov19@users.noreply.github.com> --- builders/interoperability/xcm/core-concepts/multilocations.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builders/interoperability/xcm/core-concepts/multilocations.md b/builders/interoperability/xcm/core-concepts/multilocations.md index 6725083dc..bd154a9d9 100644 --- a/builders/interoperability/xcm/core-concepts/multilocations.md +++ b/builders/interoperability/xcm/core-concepts/multilocations.md @@ -360,7 +360,7 @@ To target another parachain (for example, a parachain that has an ID of 1234) fr ### Location to Account API {: #location-to-account-api } -The Location to Account API is an easy way to convert a multilocation into an `AccountID20` address. The Location to Account API can be accessed from the [Runtime Calls](https://polkadot.js.org/apps/#/runtime){target=\_blank} tab of the **Developer** section of Polkadot.js Apps. The `convertLocation` method of the Location to Account API takes a multilocation as a parameter and returns an `AccountID20` address. +The Location to Account API is an easy way to convert a multilocation into an `AccountID20` address. The Location to Account API can be accessed from the [Runtime Calls](https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fwss.api.moonbeam.network#/runtime){target=\_blank} tab of the **Developer** section of Polkadot.js Apps. The `convertLocation` method of the Location to Account API takes a multilocation as a parameter and returns an `AccountID20` address. ```javascript // Query the locationToAccountApi using convertLocation method From 95e4d2d407a04eb38d57b9f8dfd7820fd30e82c6 Mon Sep 17 00:00:00 2001 From: Kevin Neilson Date: Mon, 23 Dec 2024 17:23:08 -0500 Subject: [PATCH 11/11] Update builders/substrate/libraries/polkadot-js-api.md Co-authored-by: albertov19 <64150856+albertov19@users.noreply.github.com> --- builders/substrate/libraries/polkadot-js-api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/builders/substrate/libraries/polkadot-js-api.md b/builders/substrate/libraries/polkadot-js-api.md index c918107b1..5dd3fd888 100644 --- a/builders/substrate/libraries/polkadot-js-api.md +++ b/builders/substrate/libraries/polkadot-js-api.md @@ -279,7 +279,7 @@ There are a number of ways to add an account to the keyring instance, including ## Dry Run API {: #dry-run-api } -The Dry Run API is an easy and convenient way to test the integrity of a call without incurring any transaction fees. The Dry Run API can be accessed from the [Runtime Calls](https://polkadot.js.org/apps/#/runtime){target=\_blank} tab of the **Developer** section of Polkadot.js Apps. While primarily intended for the [testing of XCM messages](/builders/interoperability/xcm/send-execute-xcm/#test-an-xcm-message-with-the-dry-run-api){target=\_blank} , the Dry Run API can be used to test any arbitrary call. +The Dry Run API is an easy and convenient way to test the integrity of a call without incurring any transaction fees. The Dry Run API can be accessed from the [Runtime Calls](https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fwss.api.moonbeam.network#/runtime){target=\_blank} tab of the **Developer** section of Polkadot.js Apps. While primarily intended for the [testing of XCM messages](/builders/interoperability/xcm/send-execute-xcm/#test-an-xcm-message-with-the-dry-run-api){target=\_blank} , the Dry Run API can be used to test any arbitrary call. This method takes the origin and call data as parameters and returns an execution result and additional event data.