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

[Content migration] - Asset Transfer API #57

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<div id="termynal" data-termynal>
<span data-ty="input"><span class="file-path"></span>ts-node localParachainTx.ts</span>
<br>
<span data-ty>Call data:</span>
<span data-ty>{</span>
<span data-ty> "origin": "moonbeam",</span>
<span data-ty> "dest": "moonbeam",</span>
<span data-ty> "direction": "local",</span>
<span data-ty> "xcmVersion": null,</span>
<span data-ty> "method": "balances::transferKeepAlive",</span>
<span data-ty> "format": "call",</span>
<span data-ty> "tx": "0x0a03f977814e90da44bfa03b6295a0616a897441acec821a0600"</span>
<span data-ty>}</span>
<span data-ty></span>
<span data-ty>Decoded tx:</span>
<span data-ty>{</span>
<span data-ty> "args": {</span>
<span data-ty> "dest": "0xF977814e90dA44bFA03b6295A0616a897441aceC",</span>
<span data-ty> "value": "1,000,000,000,000,000,000"</span>
<span data-ty> },</span>
<span data-ty> "method": "transferKeepAlive",</span>
<span data-ty> "section": "balances"</span>
<span data-ty>}</span>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
AssetTransferApi,
constructApiPromise,
} from "@substrate/asset-transfer-api";

async function main() {
const { api, specName, safeXcmVersion } = await constructApiPromise(
"wss://wss.api.moonbeam.network"
);
const assetApi = new AssetTransferApi(api, specName, safeXcmVersion);

let callInfo;
try {
callInfo = await assetApi.createTransferTransaction(
"2004",
"0xF977814e90dA44bFA03b6295A0616a897441aceC",
[],
["1000000000000000000"],
{
format: "call",
keepAlive: true,
}
);

console.log(`Call data:\n${JSON.stringify(callInfo, null, 4)}`);
} catch (e) {
console.error(e);
throw Error(e as string);
}

const decoded = assetApi.decodeExtrinsic(callInfo.tx, "call");
console.log(`\nDecoded tx:\n${JSON.stringify(JSON.parse(decoded), null, 4)}`);
}

main()
.catch((err) => console.error(err))
.finally(() => process.exit());
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<div id='termynal' data-termynal>
<span data-ty='input'><span class='file-path'></span>ts-node paraToPara.ts</span>

<br>
<span data-ty>Call data:</span>
<span data-ty>{</span>
<span data-ty> "origin": "moonriver",</span>
<span data-ty> "dest": "bifrost",</span>
<span data-ty> "direction": "ParaToPara",</span>
<span data-ty> "xcmVersion": 2,</span>
<span data-ty> "method": "transferMultiassets",</span>
<span data-ty> "format": "call",</span>
<span data-ty> "tx": "0x6a05010800010200451f06080101000700e40b540200010200451f0608010a0002093d000000000001010200451f0100c4db7bcb733e117c0b34ac96354b10d47e84a006b9e7e66a229d174e8ff2a06300"</span>
<span data-ty>}</span>
<span data-ty></span>
<span data-ty>Decoded tx:</span>
<span data-ty>{</span>
<span data-ty> "args": {</span>
<span data-ty> "assets": {</span>
<span data-ty> "V2": [</span>
<span data-ty> {</span>
<span data-ty> "id": {</span>
<span data-ty> "Concrete": {</span>
<span data-ty> "parents": "1",</span>
<span data-ty> "interior": {</span>
<span data-ty> "X2": [</span>
<span data-ty> {</span>
<span data-ty> "Parachain": "2,001"</span>
<span data-ty> },</span>
<span data-ty> {</span>
<span data-ty> "GeneralKey": "0x0101"</span>
<span data-ty> }</span>
<span data-ty> ]</span>
<span data-ty> }</span>
<span data-ty> }</span>
<span data-ty> },</span>
<span data-ty> "fun": {</span>
<span data-ty> "Fungible": "10,000,000,000"</span>
<span data-ty> }</span>
<span data-ty> },</span>
<span data-ty> {</span>
<span data-ty> "id": {</span>
<span data-ty> "Concrete": {</span>
<span data-ty> "parents": "1",</span>
<span data-ty> "interior": {</span>
<span data-ty> "X2": [</span>
<span data-ty> {</span>
<span data-ty> "Parachain": "2,001"</span>
<span data-ty> },</span>
<span data-ty> {</span>
<span data-ty> "GeneralKey": "0x010a"</span>
<span data-ty> }</span>
<span data-ty> ]</span>
<span data-ty> }</span>
<span data-ty> }</span>
<span data-ty> },</span>
<span data-ty> "fun": {</span>
<span data-ty> "Fungible": "1,000,000"</span>
<span data-ty> }</span>
<span data-ty> }</span>
<span data-ty> ]</span>
<span data-ty> },</span>
<span data-ty> "fee_item": "0",</span>
<span data-ty> "dest": {</span>
<span data-ty> "V2": {</span>
<span data-ty> "parents": "1",</span>
<span data-ty> "interior": {</span>
<span data-ty> "X2": [</span>
<span data-ty> {</span>
<span data-ty> "Parachain": "2,001"</span>
<span data-ty> },</span>
<span data-ty> {</span>
<span data-ty> "AccountId32": {</span>
<span data-ty> "network": "Any",</span>
<span data-ty> "id": "0xc4db7bcb733e117c0b34ac96354b10d47e84a006b9e7e66a229d174e8ff2a063"</span>
<span data-ty> }</span>
<span data-ty> }</span>
<span data-ty> ]</span>
<span data-ty> }</span>
<span data-ty> }</span>
<span data-ty> },</span>
<span data-ty> "dest_weight_limit": "Unlimited"</span>
<span data-ty> },</span>
<span data-ty> "method": "transferMultiassets",</span>
<span data-ty> "section": "xTokens"</span>
<span data-ty>}</span>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
AssetTransferApi,
constructApiPromise,
} from "@substrate/asset-transfer-api";

async function main() {
const { api, specName, safeXcmVersion } = await constructApiPromise(
"wss://moonriver.public.blastapi.io"
);
const assetApi = new AssetTransferApi(api, specName, safeXcmVersion);
let callInfo;
try {
callInfo = await assetApi.createTransferTransaction(
"2001",
"0xc4db7bcb733e117c0b34ac96354b10d47e84a006b9e7e66a229d174e8ff2a063",
["vMOVR", "72145018963825376852137222787619937732"],
["1000000", "10000000000"],
{
format: "call",
xcmVersion: safeXcmVersion,
}
);

console.log(`Call data:\n${JSON.stringify(callInfo, null, 4)}`);
} catch (e) {
console.error(e);
throw Error(e as string);
}

const decoded = assetApi.decodeExtrinsic(callInfo.tx, "call");
console.log(`\nDecoded tx:\n${JSON.stringify(JSON.parse(decoded), null, 4)}`);
}

main()
.catch((err) => console.error(err))
.finally(() => process.exit());
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<div id="termynal" data-termynal>
<span data-ty="input"><span class="file-path"></span>ts-node relayToSystem.ts</span>
<br>
<span data-ty>Call data:</span>
<span data-ty>{</span>
<span data-ty> "origin": "westend",</span>
<span data-ty> "dest": "westmint",</span>
<span data-ty> "direction": "RelayToSystem",</span>
<span data-ty> "xcmVersion": 3,</span>
<span data-ty> "method": "transferAssets",</span>
<span data-ty> "format": "call",</span>
<span data-ty> "tx": "0x630b03000100a10f03000101006c0c32faf970eacb2d4d8e538ac0dab3642492561a1be6f241c645876c056c1d030400000000070010a5d4e80000000000"</span>
<span data-ty>}</span>
<span data-ty></span>
<span data-ty>Decoded tx:</span>
<span data-ty>{</span>
<span data-ty> "args": {</span>
<span data-ty> "dest": {</span>
<span data-ty> "V3": {</span>
<span data-ty> "parents": "0",</span>
<span data-ty> "interior": {</span>
<span data-ty> "X1": {</span>
<span data-ty> "Parachain": "1,000"</span>
<span data-ty> }</span>
<span data-ty> }</span>
<span data-ty> }</span>
<span data-ty> },</span>
<span data-ty> "beneficiary": {</span>
<span data-ty> "V3": {</span>
<span data-ty> "parents": "0",</span>
<span data-ty> "interior": {</span>
<span data-ty> "X1": {</span>
<span data-ty> "AccountId32": {</span>
<span data-ty> "network": null,</span>
<span data-ty> "id": "0x6c0c32faf970eacb2d4d8e538ac0dab3642492561a1be6f241c645876c056c1d"</span>
<span data-ty> }</span>
<span data-ty> }</span>
<span data-ty> }</span>
<span data-ty> }</span>
<span data-ty> },</span>
<span data-ty> "assets": {</span>
<span data-ty> "V3": [</span>
<span data-ty> {</span>
<span data-ty> "id": {</span>
<span data-ty> "Concrete": {</span>
<span data-ty> "parents": "0",</span>
<span data-ty> "interior": "Here"</span>
<span data-ty> }</span>
<span data-ty> },</span>
<span data-ty> "fun": {</span>
<span data-ty> "Fungible": "1,000,000,000,000"</span>
<span data-ty> }</span>
<span data-ty> }</span>
<span data-ty> ]</span>
<span data-ty> },</span>
<span data-ty> "fee_asset_item": "0",</span>
<span data-ty> "weight_limit": "Unlimited"</span>
<span data-ty> },</span>
<span data-ty> "method": "transferAssets",</span>
<span data-ty> "section": "xcmPallet"</span>
<span data-ty>}</span>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
AssetTransferApi,
constructApiPromise,
} from "@substrate/asset-transfer-api";

async function main() {
const { api, specName, safeXcmVersion } = await constructApiPromise(
"wss://westend-rpc.polkadot.io"
);
const assetApi = new AssetTransferApi(api, specName, safeXcmVersion);
let callInfo;
try {
callInfo = await assetApi.createTransferTransaction(
"1000",
"5EWNeodpcQ6iYibJ3jmWVe85nsok1EDG8Kk3aFg8ZzpfY1qX",
["WND"],
["1000000000000"],
{
format: "call",
xcmVersion: safeXcmVersion,
}
);

console.log(`Call data:\n${JSON.stringify(callInfo, null, 4)}`);
} catch (e) {
console.error(e);
throw Error(e as string);
}

const decoded = assetApi.decodeExtrinsic(callInfo.tx, "call");
console.log(`\nDecoded tx:\n${JSON.stringify(JSON.parse(decoded), null, 4)}`);
}

main()
.catch((err) => console.error(err))
.finally(() => process.exit());
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {
AssetTransferApi,
constructApiPromise,
} from "@substrate/asset-transfer-api";

async function main() {
const { api, specName, safeXcmVersion } = await constructApiPromise(
"INSERT_WEBSOCKET_URL"
);

const assetsApi = new AssetTransferApi(api, specName, safeXcmVersion);

// Your code using assetsApi goes here
Copy link
Collaborator

@nhussein11 nhussein11 Sep 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some snippets use assetsApi and other assetApi, stick to one way

Also, I find kind of weird this comment:
// Your code using assetsApi goes here
But is nitpicking at the end of the day, if you find a better way to express it cool, if not no worries :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It meant the variable called assetsApi that has already been initialized in line 11. Do you think it looks better if we put assetTransferApi ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I get that. My point here was that in some snippets, you initialized the variable in a plural form, and in others, you used assetApi in a singular

}

main();
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
```ts
export type AssetTransferType =
| LocalReserve
| DestinationReserve
| Teleport
| RemoteReserve;
```

!!! note
To use the `assetTransferType` parameter, which is a string, you should use the `AssetTransferType` type as if each of its variants are strings. For example: `assetTransferType = 'LocalReserve'`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {
AssetTransferApi,
constructApiPromise,
} from "@substrate/asset-transfer-api";

async function main() {
const { api, specName, safeXcmVersion } = await constructApiPromise(
"wss://westend-rpc.polkadot.io"
);
const assetsApi = new AssetTransferApi(api, specName, safeXcmVersion);

let callInfo;
try {
callInfo = await assetsApi.claimAssets(
[
`{"parents":"0","interior":{"X2":[{"PalletInstance":"50"},{"GeneralIndex":"1984"}]}}`,
],
["1000000000000"],
"0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b",
{
format: "call",
xcmVersion: 2,
}
);

console.log(`Call data:\n${JSON.stringify(callInfo, null, 4)}`);
} catch (e) {
console.error(e);
throw Error(e as string);
}
}

main()
.catch((err) => console.error(err))
.finally(() => process.exit());
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div id="termynal" data-termynal>
<span data-ty>Call data:</span>
<span data-ty>{</span>
<span data-ty> "origin": "0",</span>
<span data-ty> "dest": "westend",</span>
<span data-ty> "direction": "local",</span>
<span data-ty> "xcmVersion": 2,</span>
<span data-ty> "method": "claimAssets",</span>
<span data-ty> "format": "call",</span>
<span data-ty> "tx": "0x630c0104000002043205011f00070010a5d4e80100010100f5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b"</span>
<span data-ty>}</span>
<div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public async claimAssets<T extends Format>(
assetIds: string[],
amounts: string[],
beneficiary: string,
opts: TransferArgsOpts<T>
): Promise<TxResult<T>>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
```ts
export type ConstructedFormat<T> = T extends 'payload'
? GenericExtrinsicPayload
: T extends 'call'
? `0x${string}`
: T extends 'submittable'
? SubmittableExtrinsic<'promise', ISubmittableResult>
: never;
```

The `ConstructedFormat` type is a conditional type that returns a specific type based on the value of the TxResult `format` field.

- Payload format - if the format field is set to `'payload'`, the `ConstructedFormat` type will return a [`GenericExtrinsicPayload`](https://github.com/polkadot-js/api/blob/3b7b44f048ff515579dd233ea6964acec39c0589/packages/types/src/extrinsic/ExtrinsicPayload.ts#L48){target=_blank}
- Call format - if the format field is set to `'call'`, the `ConstructedFormat` type will return a hexadecimal string (`0x${string}`). This is the encoded representation of the extrinsic call
- Submittable format - if the format field is set to `'submittable'`, the `ConstructedFormat` type will return a [`SubmittableExtrinsic`](https://github.com/polkadot-js/api/blob/3b7b44f048ff515579dd233ea6964acec39c0589/packages/api-base/src/types/submittable.ts#L56){target=_blank}. This is a Polkadot.js type that represents a transaction that can be submitted to the blockchain
Loading