Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add XCM Dry Run and Location to Account Runtime Calls #1010

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
Dry run XCM result: {
"ok": {
"executionResult": {
"ok": {
"actualWeight": {
"refTime": 7301615000,
"proofSize": 20928
},
"paysFee": "Yes"
}
},
"emittedEvents": [
{
"index": "0x030b",
"data": [
"0x88bcE0b038eFFa09e58fE6d24fDe4b5Af21aa798",
"0x0000000000000000016345785d8a0000"
]
},
{
"index": "0x0300",
"data": [
"0x3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0",
"0x0000000000000000016345785d8a0000"
]
},
{
"index": "0x030a",
"data": [
"0x3Cd0A705a2DC65e5b1E1205896BaA2be8A07c6e0",
"0x0000000000000000016345785d8a0000"
]
},
{
"index": "0x1c00",
"data": [
{
"complete": {
"used": {
"refTime": 7250000000,
"proofSize": 19374
}
}
}
]
}
],
"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
Original file line number Diff line number Diff line change
@@ -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 =
'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 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);
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Dry run XCM result: {
"ok": {
"executionResult": {
"complete": {
"used": {
"refTime": 76473048000,
"proofSize": 222483
}
}
},
"emittedEvents": [
{
"index": "0x1d03",
"data": [
"0x1fcacbd218edc0eba20fc2308c778080",
"0x506172656E740000000000000000000000000000",
1000000000000
]
},
{
"index": "0x1d01",
"data": [
"0x1fcacbd218edc0eba20fc2308c778080",
"0x3B939FeaD1557C741Ff06492FD0127bd287A421e",
959944978002
]
},
{
"index": "0x1d01",
"data": [
"0x1fcacbd218edc0eba20fc2308c778080",
"0x6d6F646c70632f74727372790000000000000000",
40055021998
]
}
], // Additional events returned here
// Omitted for clarity
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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 XCM call...');

// Define the origin
const origin = { V4: { parents: 1, interior: 'Here' } };
const amountToSend = 1000000000000;

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 },
},
},
{
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);

console.log(
'Dry run XCM result:',
JSON.stringify(result.toJSON(), null, 2)
);

await api.disconnect();
console.log('Disconnected from the API.');
} catch (error) {
console.error('An error occurred:', error);
}
};

main().catch(console.error);
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion builders/interoperability/xcm/.pages
Original file line number Diff line number Diff line change
Expand Up @@ -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'
26 changes: 26 additions & 0 deletions builders/interoperability/xcm/core-concepts/multilocations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/?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
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' }
```

Loading