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

Changes related to polkadot-sdk v.1.7.2 and XCM v4 #912

Merged
merged 14 commits into from May 10, 2024
@@ -0,0 +1,6 @@
<div id="termynal" data-termynal>
eshaben marked this conversation as resolved.
Show resolved Hide resolved
<span data-ty="input">node build-xcm-message.js</span>
<br>
<span data-ty>Encoded Calldata: 0xb50a04630004000100a10f041000040000000f0000c16ff28623130000000f0000c16ff28623000601070053cd200a02350c007d09260001f0490200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008080000000000000000000000000000000000000000000000000000000000000000110896e292b8000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000000020000000000000000000000001fc56b105c4f0a1a8038c2b429932b122f6b631f000000000000000000000000ed13b028697febd70f34cf9a9e280a8f1e98fd29000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000042004ffd90000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000042004ffd9000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d010000010300f0615483cbe76f5b2aa80a8ce2b2e9a8206deb65</span>
<span data-ty="input"></span>
</div>
@@ -0,0 +1,17 @@
<div id="termynal" data-termynal>
<span data-ty="input"><span class="file-path"></span>yarn calculate-multilocation-derivative-account \</span>
<span data-ty>--ws-provider wss://wss.api.moonbase.moonbeam.network \</span>
<span data-ty>--address 5Fe4nNwxJ9ai9hVkUubiy4e6BVs1tzJGDLXAdhUKuePq9CLp \</span>
<br>
<span data-ty>yarn run v1.22.10</span>
<span data-ty>warning ../../../package.json: No license field</span>
<span data-ty>$ ts-node 'scripts/calculate-multilocation-derivative-account.ts' --ws-provider wss://wss.api.moonbase.moonbeam.network --address 5Fe4nNwxJ9ai9hVkUubiy4e6BVs1tzJGDLXAdhUKuePq9CLp --parents 1</span>
<br>
<span data-ty>Remote Origin calculated as ParentChain</span>
<span data-ty>Parents 1</span>
<span data-ty>AccountId32: 5Fe4nNwxJ9ai9hVkUubiy4e6BVs1tzJGDLXAdhUKuePq9CLp</span>
<span data-ty>32 byte address is 0xf0615483cbe76f5b2aa80a8ce2b2e9a8206deb65b8a1323270e25802f600f95c</span>
<span data-ty>20 byte address is 0xf0615483cbe76f5b2aa80a8ce2b2e9a8206deb65</span>
<span data-ty>✨ Done in 1.02s.</span>
<span data-ty="input"><span class="file-path"></span></span>
</div>
@@ -0,0 +1,6 @@
<div id="termynal" data-termynal>
eshaben marked this conversation as resolved.
Show resolved Hide resolved
<span data-ty="input">node generate-encoded-calldata.js</span>
<br>
<span data-ty>Encoded Calldata: 0x260001f0490200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008080000000000000000000000000000000000000000000000000000000000000000110896e292b8000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000000020000000000000000000000001fc56b105c4f0a1a8038c2b429932b122f6b631f000000000000000000000000ed13b028697febd70f34cf9a9e280a8f1e98fd29000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000042004ffd90000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000042004ffd900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</span>
<span data-ty="input"></span>
</div>
@@ -0,0 +1,7 @@
<div id="termynal" data-termynal>
eshaben marked this conversation as resolved.
Show resolved Hide resolved
<span data-ty="input">node send-xcm-message.js</span>
<br>
<span data-ty>Transaction sent</span>
<span data-ty>Transaction sent</span>
<span data-ty="input"></span>
</div>
@@ -1,7 +1,7 @@
import { ApiPromise, WsProvider } from '@polkadot/api';
const provider = new WsProvider('wss://wss.api.moonbase.moonbeam.network');

const candidate = '0x3A7D3048F3CB0391bb44B518e5729f07bCc7A45D';
const candidate = '0x12E7BCCA9b1B15f33585b5fc898B967149BDb9a5';
const amount = '1000000000000000000';
const autoCompound = 100;

Expand Down
@@ -1,4 +1,4 @@
const candidate = '0x3A7D3048F3CB0391bb44B518e5729f07bCc7A45D';
const candidate = '0x12E7BCCA9b1B15f33585b5fc898B967149BDb9a5';
const amount = '1000000000000000000';
const autoCompound = 100;

Expand Down
Expand Up @@ -34,7 +34,7 @@ const instr3 = {
requireWeightAtMost: { refTime: 40000000000n, proofSize: 900000n },
call: {
encoded:
'0x0c123a7d3048f3cb0391bb44b518e5729f07bcc7a45d000064a7b3b6e00d000000000000000064430000000600000000000000',
'0x0c1212e7bcca9b1b15f33585b5fc898b967149bdb9a5000064a7b3b6e00d000000000000000064070000000700000000000000',
},
},
};
Expand Down
@@ -0,0 +1,17 @@
<div id="termynal" data-termynal>
<span data-ty="input"><span class="file-path"></span>yarn calculate-multilocation-derivative-account \</span>
<span data-ty>--ws-provider wss://wss.api.moonbase.moonbeam.network \</span>
<span data-ty>--address 5GQtq2cSDLeN77T2Bs4jxV7yUQh841tWy6wtJoSUrFZAm5qS \</span>
<br>
<span data-ty>yarn run v1.22.10</span>
<span data-ty>warning ../../../package.json: No license field</span>
<span data-ty>$ ts-node 'scripts/calculate-multilocation-derivative-account.ts' --ws-provider wss://wss.api.moonbase.moonbeam.network --address 5GQtq2cSDLeN77T2Bs4jxV7yUQh841tWy6wtJoSUrFZAm5qS --parents 1</span>
<br>
<span data-ty>Remote Origin calculated as ParentChain</span>
<span data-ty>Parents 1</span>
<span data-ty>AccountId32: 5CPE8WuahbikCSobyxeEMYzdjz7QhXxEGeeQ2LpS2RB9ZEXJ</span>
<span data-ty>32 byte address is 0x3630dd0d236be6105c9f54d9d89183f9066e4a03ddc5eea1db922ef94cae2414</span>
<span data-ty>20 byte address is 0x3630dd0d236be6105c9f54d9d89183f9066e4a03</span>
<span data-ty>✨ Done in 1.02s.</span>
<span data-ty="input"><span class="file-path"></span></span>
</div>
@@ -1,5 +1,6 @@
import { ApiPromise, WsProvider } from '@polkadot/api'; // Version 9.13.6
eshaben marked this conversation as resolved.
Show resolved Hide resolved
import { ethers } from 'ethers'; // Version 6.0.2
import BN from 'bn.js'; // Importing directly from bn.js

// 1. Input Data
const providerWsURL = 'wss://wss.api.moonbase.moonbeam.network';
Expand All @@ -10,23 +11,26 @@ const contractCall =
const generateCallData = async () => {
// 2. Create Substrate API Provider
const substrateProvider = new WsProvider(providerWsURL);
const ethProvider = new ethers.WebSocketProvider(providerWsURL);
const ethProvider = new ethers.providers.WebSocketProvider(providerWsURL);
eshaben marked this conversation as resolved.
Show resolved Hide resolved
const api = await ApiPromise.create({ provider: substrateProvider });

// 3. Estimate Gas for EVM Call
const gasLimit = await ethProvider.estimateGas({
to: uniswapV2Router,
data: contractCall,
value: ethers.parseEther('0.01'),
value: ethers.utils.parseEther('0.01'),
});
console.log(`Gas required for call is ${gasLimit.toString()}`);

// Convert ethers' BigNumber to Polkadot's BN and add some extra
const totalGasLimit = new BN(gasLimit.toString()).add(new BN(10000));

// 4. Call Parameters
const callParams = {
V2: {
gasLimit: gasLimit + 10000n, // Estimated plus some extra gas
gasLimit: totalGasLimit, // Estimated plus some extra gas
action: { Call: uniswapV2Router }, // Uniswap V2 router address
value: ethers.parseEther('0.01'), // 0.01 DEV
value: new BN(ethers.utils.parseEther('0.01').toString()), // 0.01 DEV
input: contractCall, // Swap encoded calldata
},
};
Expand Down
@@ -0,0 +1,6 @@
<div id="termynal" data-termynal>
eshaben marked this conversation as resolved.
Show resolved Hide resolved
<span data-ty="input">node build-xcm-message-swap.js</span>
<br>
<span data-ty>Encoded Calldata: 0x450604630004000100a10f0410000400010403000f0000c16ff286231300010403000f0000c16ff286230006010700902f500982b92a00fd042600019b40090000000000000000000000000000000000000000000000000000000000008a1932d6e26433f3037bd6c3a40c816222a6ccd40000c16ff286230000000000000000000000000000000000000000000000000091037ff36ab50000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000061cd3e07fe7d7f6d4680e3e322986b7877f108dd00000000000000000000000000000000000000000000000000000000a036b1b90000000000000000000000000000000000000000000000000000000000000002000000000000000000000000d909178cc99d318e4d46e7e66a972955859670e10000000000000000000000001fc56b105c4f0a1a8038c2b429932b122f6b631f000d01000001030061cd3e07fe7d7f6d4680e3e322986b7877f108dd</span>
<span data-ty="input"></span>
</div>
@@ -0,0 +1,17 @@
<div id="termynal" data-termynal>
<span data-ty="input"><span class="file-path"></span>yarn calculate-multilocation-derivative-account \</span>
<span data-ty>--ws-provider wss://wss.api.moonbase.moonbeam.network \</span>
<span data-ty>--address 5GKh9gMK5dn9SJp6qfMNcJiMMnY7LReYmgug2Fr5fKE64imn \</span>
<br>
<span data-ty>yarn run v1.22.10</span>
<span data-ty>warning ../../../package.json: No license field</span>
<span data-ty>$ ts-node 'scripts/calculate-multilocation-derivative-account.ts' --ws-provider wss://wss.api.moonbase.moonbeam.network --address 5GKh9gMK5dn9SJp6qfMNcJiMMnY7LReYmgug2Fr5fKE64imn --parents 1</span>
<br>
<span data-ty>Remote Origin calculated as ParentChain</span>
<span data-ty>Parents 1</span>
<span data-ty>AccountId32: 5GKh9gMK5dn9SJp6qfMNcJiMMnY7LReYmgug2Fr5fKE64imn</span>
<span data-ty>32 byte address is 0x61cd3e07fe7d7f6d4680e3e322986b7877f108ddb18ec02c2f17e82fe15f9016</span>
<span data-ty>20 byte address is 0x61cd3e07fe7d7f6d4680e3e322986b7877f108dd</span>
<span data-ty>✨ Done in 1.02s.</span>
<span data-ty="input"><span class="file-path"></span></span>
</div>
@@ -0,0 +1,7 @@
<div id="termynal" data-termynal>
eshaben marked this conversation as resolved.
Show resolved Hide resolved
<span data-ty="input">node generate-call-data-swap.js</span>
<br>
<span data-ty>Gas required for call is 596363</span>
<span data-ty>Encoded Calldata: 0x2600019b40090000000000000000000000000000000000000000000000000000000000008a1932d6e26433f3037bd6c3a40c816222a6ccd40000c16ff286230000000000000000000000000000000000000000000000000091037ff36ab50000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000061cd3e07fe7d7f6d4680e3e322986b7877f108dd00000000000000000000000000000000000000000000000000000000a036b1b90000000000000000000000000000000000000000000000000000000000000002000000000000000000000000d909178cc99d318e4d46e7e66a972955859670e10000000000000000000000001fc56b105c4f0a1a8038c2b429932b122f6b631f00</span>
<span data-ty="input"></span>
</div>
@@ -0,0 +1,3 @@
<div id="termynal" data-termynal>
eshaben marked this conversation as resolved.
Show resolved Hide resolved
<span data-ty="input">node send-xcm-message-swap.js</span>
</div>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
12 changes: 6 additions & 6 deletions tutorials/interoperability/remote-batched-evm-calls.md
Expand Up @@ -44,7 +44,7 @@ Considering all the steps summarized in the [introduction](#introduction), the f

For our case, we will send the remote EVM call via XCM from Alice's account, which is `5Fe4nNwxJ9ai9hVkUubiy4e6BVs1tzJGDLXAdhUKuePq9CLp`. A parachain ID is omitted from the command since we are sending the XCM instruction from the relay chain. A parents value of `1` indicates that the relay chain is a parent of the destination parachain. The command and response should resemble the following image:

![Calculating the Computed Origin account](/images/tutorials/interoperability/remote-batched-evm-calls/remote-batched-evm-calls-2.webp)
--8<-- 'code/tutorials/interoperability/remote-batched-evm-calls/terminal/calculate.md'

The values are all summarized in the following table:

Expand Down Expand Up @@ -72,7 +72,7 @@ The easiest way to get the calldata is through the [Moonbase Minter](https://moo
3. MetaMask should pop up, but **do not sign the transaction**. In MetaMask, click on the **hex** tab, and the encoded calldata should show up
4. Click on the **Copy raw transaction data** button. This will copy the encoded calldata to the clipboard, which should match: `0x2004ffd9`

![Calldata for Minting action](/images/tutorials/interoperability/remote-batched-evm-calls/remote-batched-evm-calls-3.webp)
![Calldata for Minting action](/images/tutorials/interoperability/remote-batched-evm-calls/remote-batched-evm-calls-2.webp)

!!! note
Other wallets also offer the same capabilities of checking the encoded calldata before signing the transaction.
Expand Down Expand Up @@ -115,7 +115,7 @@ After inputting the address and pressing **At Address**, take the following step
7. MetaMask should pop up, but **do not sign the transaction**. In MetaMask, click on the **hex** tab, and the encoded calldata should show up
8. Click on the **Copy raw transaction data** button. This will copy the encoded calldata of the batch call to the clipboard

![Generate batch calls using Batch Precompile](/images/tutorials/interoperability/remote-batched-evm-calls/remote-batched-evm-calls-4.webp)
![Generate batch calls using Batch Precompile](/images/tutorials/interoperability/remote-batched-evm-calls/remote-batched-evm-calls-3.webp)

We've now finished preparing our EVM calldata for the batch call. Next, we'll need to prepare the XCM instructions that will execute our remote batch call.

Expand Down Expand Up @@ -146,7 +146,7 @@ Let's go through each of the main components of the snippet shown above:

Once you have the code set up, you can execute it with `node`, and you'll get the Moonbase Alpha remote EVM calldata:

![Getting the Moonbeam calldata for the remote evm call](/images/tutorials/interoperability/remote-batched-evm-calls/remote-batched-evm-calls-5.webp)
--8<-- 'code/tutorials/interoperability/remote-batched-evm-calls/terminal/encoded.md'

The encoded calldata for this example is:

Expand Down Expand Up @@ -197,7 +197,7 @@ Let's go through each of the main components of the snippet shown above:

Once you have the code set up, you can execute it with `node`, and you'll get the relay chain XCM calldata:

![Getting the Relay Chain XCM calldata for the Remote Batch call](/images/tutorials/interoperability/remote-batched-evm-calls/remote-batched-evm-calls-6.webp)
--8<-- 'code/tutorials/interoperability/remote-batched-evm-calls/terminal/build-xcm-message.md'

The encoded calldata for this example is:

Expand Down Expand Up @@ -228,7 +228,7 @@ To send the XCM message that we built in the previous section, you can use the f

Once you have the code set up, you can execute it with `node`, and the XCM message will be sent to initiate your call to the Batch Precompile for the mints of MARS and NEPT ERC-20 tokens in Moonbase Alpha. Don't worry if you see an `Abnormal Closure` error. You can verify that your remote batch call was successful by looking up your Computed Origin account on [Moonbase Moonscan](https://moonbase.moonscan.io/){target=\_blank}.

![Sending the XCM message from the Relay Chain to Moonbase Alpha for the batch EVM call](/images/tutorials/interoperability/remote-batched-evm-calls/remote-batched-evm-calls-7.webp)
--8<-- 'code/tutorials/interoperability/remote-batched-evm-calls/terminal/send.md'

And that is it! You've sent an XCM message, which performed a remote EVM call to the Batch Precompile via XCM and resulted in the minting of MARS and NEPT ERC-20 tokens. But let's go into more detail about what happened.

Expand Down
8 changes: 4 additions & 4 deletions tutorials/interoperability/remote-staking-xcm.md
Expand Up @@ -30,15 +30,15 @@ For development purposes this tutorial is written for Moonbase Alpha and Moonbas

Here, we have specified a parents value of `1` because the relay chain is the origin of the request (and the relay chain is considered a parent to the Moonbase alpha parachain). The relay chain does not have a parachain id so that field is omitted.

![Calculate Multi-Location Derivative Account](/images/tutorials/interoperability/remote-staking-via-xcm/xcm-stake-1.webp)
--8<-- 'code/tutorials/interoperability/remote-staking/terminal/calculate.md'

The script will return 32-byte and 20-byte addresses. We’re interested in the Ethereum-style account - the 20-byte one. Feel free to look up your Computed Origin account on [Moonscan](https://moonbase.moonscan.io/){target=\_blank}. You’ll note that this account is empty. You’ll now need to fund this account with at least 1.1 DEV which you can get from [the faucet](https://faucet.moonbeam.network/){target=\_blank}. And if you need more, you can always reach out to us on [Discord](https://discord.com/invite/amTRXQ9ZpW){target=\_blank} for additional DEV tokens.

## Preparing to Stake on Moonbase Alpha {: #preparing-to-stake-on-moonbase-alpha }

First and foremost, you’ll need the address of the collator you want to delegate to. To locate it, head to the [Moonbase Alpha Staking dApp](https://apps.moonbeam.network/moonbase-alpha/staking){target=\_blank} in a second window. Ensure you’re on the correct network, then press **Select a Collator**. Press the icon next to your desired collator to copy its address. You’ll also need to make a note of the number of delegations your collator has. The [PS-31 collator](https://moonbase.subscan.io/account/0x3A7D3048F3CB0391bb44B518e5729f07bCc7A45D){target=\_blank} shown below has `64` delegations at the time of writing.
First and foremost, you’ll need the address of the collator you want to delegate to. To locate it, head to the [Moonbase Alpha Staking dApp](https://apps.moonbeam.network/moonbase-alpha/staking){target=\_blank} in a second window. Ensure you’re on the correct network, then press **Select a Collator**. Press the icon next to your desired collator to copy its address. You’ll also need to make a note of the number of delegations your collator has. The [Moonbeam Foundation 01 collator](https://moonbase.subscan.io/account/{{networks.moonbase.precompiles.staking}}){target=\_blank} shown below has `7` delegations at the time of writing.

![Moonbeam Network Apps Dashboard](/images/tutorials/interoperability/remote-staking-via-xcm/xcm-stake-2.webp)
![Moonbeam Network Apps Dashboard](/images/tutorials/interoperability/remote-staking-via-xcm/xcm-stake-1.webp)

## Remote Staking via XCM with the Polkadot.js API {: #remote-staking-via-xcm-with-the-polkadot-api }

Expand All @@ -53,7 +53,7 @@ In order to generate the encoded call data, we'll need to assemble the arguments
1. Create a [Polkadot.js API](/builders/build/substrate-api/polkadot-js-api){target=\_blank} provider
2. Assemble the arguments for each of the parameters of the `delegateWithAutoCompound` function:

- `candidate`- for this example we'll use the [PS-31 collator](https://moonbase.subscan.io/account/0x3A7D3048F3CB0391bb44B518e5729f07bCc7A45D){target=\_blank}: `0x3A7D3048F3CB0391bb44B518e5729f07bCc7A45D`. To retrieve the entire list of candidates, you can refer back to the [Preparing to Stake](#preparing-to-stake-on-moonbase-alpha) section
- `candidate`- for this example we'll use the [Moonbeam Foundation 01 collator](https://moonbase.subscan.io/account/{{networks.moonbase.staking.candidates.address1}}){target=\_blank}: `{{networks.moonbase.staking.candidates.address1}}`. To retrieve the entire list of candidates, you can refer back to the [Preparing to Stake](#preparing-to-stake-on-moonbase-alpha) section
- `amount` - we'll stake the minimum amount, which is 1 DEV or `1000000000000000000` Wei. You can find a [unit converter on Moonscan](https://moonscan.io/unitconverter){target=\_blank}
- `autoCompound` - we'll set this to `100` to auto-compound all rewards
- `candidateDelegationCount` - we'll retrieve using the `candidateInfo` function of the Parachain Staking Pallet to get the exact count. Alternatively, you can enter the upper bound of `300` because this estimation is only used to determine the weight of the call
Expand Down