Skip to content

Commit 2c4bb65

Browse files
committed
refactored
1 parent 50e177a commit 2c4bb65

File tree

5 files changed

+98
-25
lines changed

5 files changed

+98
-25
lines changed

.snippets/code/develop/interoperability/best-practices-for-teleporting-assets/xcm-payment-api.ts renamed to .snippets/code/develop/interoperability/best-practices-for-teleporting-assets/xcm-delivery-fees.ts

File renamed without changes.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { dot } from '@polkadot-api/descriptors';
2+
import { createClient } from 'polkadot-api';
3+
import { getWsProvider } from 'polkadot-api/ws-provider/web';
4+
5+
const client = createClient(getWsProvider('wss://rpc.polkadot.io'));
6+
const api = client.getTypedApi(dot);
7+
8+
const xcm = XcmVersionedXcm.V5([...]);
9+
10+
// These will be set if the runtime API calls are successful.
11+
let localExecutionFees = 0;
12+
// We query the weight of the xcm.
13+
const weightResult = await api.apis.XcmPaymentApi.query_xcm_weight(xcm);
14+
if (weightResult.success) {
15+
// We convert the weight to a fee amount.
16+
// The asset is { parents: 1, interior: Here }, aka, DOT.
17+
const executionFeesResult = await api.apis.XcmPaymentApi.query_weight_to_asset_fee(
18+
weightResult.value,
19+
XcmVersionedAssetId.V5({
20+
parents: 1,
21+
interior: XcmV3Junctions.Here(),
22+
}),
23+
);
24+
if (executionFeesResult.success) {
25+
localExecutionFees = executionFeesResult.value;
26+
}
27+
}

develop/interoperability/best-practices-for-teleporting-assets.md

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Account queries should check:
2929
- The correct address and address format is used.
3030
- Whether the account meets existential deposit requirements.
3131
- Balance of the sending account has:
32-
- Enough to pay for fees (transaction fees, delivery fees, and/or swap fees).
32+
- Enough to pay for fees (transaction fees, XCM execution and delivery fees, and/or swap fees).
3333
- Enough to cover the existential deposit before and after the transfer.
3434
- Balance of the recipient account has:
3535
- Enough to cover for the existential deposit.
@@ -57,11 +57,11 @@ You can refer to the following snippet as an example of checking for the Existen
5757
- **Kusama**: 0.0033 KSM (3.3 * 10^10 planck)
5858
- **Asset Hub**: 0.01 DOT (10^8 planck)
5959

60-
For non-sufficient assets, make sure the destination account has enough native tokens or assets to maintain the ED; otherwise, include asset conversion instructions in the XCM.
60+
For non-sufficient assets, make sure the destination account has a sufficient amount in the form of a native or sufficient asset to cover for the ED; otherwise, include asset conversion instructions in the XCM to swap for a sufficient asset or native token.
6161

6262
## Fee Estimation and Coverage
6363

64-
When it comes to transaction fees, XCM delivery fees, and asset conversion swapping fees, you can use associated runtime APIs to obtain an accurate estimate of the fee rather than hardcoded values.
64+
When it comes to transaction fees, XCM execution and delivery fees, and asset conversion swapping fees, you can use associated runtime APIs to obtain an accurate estimate of the fee rather than hardcoded values.
6565

6666
### Runtime API Integration
6767

@@ -72,13 +72,23 @@ When it comes to transaction fees, XCM delivery fees, and asset conversion swapp
7272
```
7373
This code establishes a connection to the Polkadot network and queries detailed fee information for a specific transaction call, including base fees, length fees, and tip calculations.
7474

75+
76+
- [XCM Payment API](https://paritytech.github.io/polkadot-sdk/master/xcm_runtime_apis/fees/trait.XcmPaymentApi.html){target=\_blank} for local XCM execution fees:
77+
78+
```typescript
79+
--8<-- 'code/develop/interoperability/best-practices-for-teleporting-assets/xcm-execution-fees.ts'
80+
```
81+
This code calculates the local XCM execution fees required to execute an XCM message on the parachain.
82+
83+
7584
- [XCM Payment API](https://paritytech.github.io/polkadot-sdk/master/xcm_runtime_apis/fees/trait.XcmPaymentApi.html){target=\_blank} for cross-chain delivery fees:
7685

7786
```typescript
78-
--8<-- 'code/develop/interoperability/best-practices-for-teleporting-assets/xcm-payment-api.ts'
87+
--8<-- 'code/develop/interoperability/best-practices-for-teleporting-assets/xcm-delivery-fees.ts'
7988
```
8089
This code calculates the delivery fees required to send an XCM message to a specific destination parachain, helping estimate cross-chain transaction costs.
8190

91+
8292
- [Asset Conversion API](https://paritytech.github.io/polkadot-sdk/master/pallet_asset_conversion/trait.AssetConversionApi.html){target=\_blank} (available on Asset Hub) for fee conversion:
8393

8494
```typescript
@@ -100,8 +110,8 @@ For dealing with non-sufficient assets and fees, there are different fee payment
100110

101111
Sufficient assets can:
102112

103-
- Pay for transaction and XCM delivery fees directly.
104113
- Suffice for account existence by meeting the existential deposit requirements.
114+
- Pay for transaction and, if the chain configuration allows, sufficient assets can also be used for XCM execution and delivery fees.
105115

106116
!!!note Sufficient Assets
107117
Always check the chain to see what assets are considered sufficient assets.
@@ -111,8 +121,8 @@ Sufficient assets can:
111121

112122
Non-sufficient assets can:
113123

114-
- Pay for transaction and XCM delivery fees by swapping for a sufficent asset.
115-
- Be used to create a new account by swapping for an exact amount of a sufficient asset to cover for the existential deposit of the new account.
124+
- Pay for transaction and XCM execution/delivery fees by swapping for a sufficent asset.
125+
- Be used to create new accounts by swapping for an exact amount of a sufficient asset to cover for the existential deposit of the new account.
116126

117127
!!!note Swapping Non-Sufficient Assets
118128
Swapping non-sufficient assets for a sufficient asset can be done with the help of [asset conversion](https://wiki.polkadot.network/learn/learn-asset-conversion-assethub){target=\_blank} which is live on Asset Hub. Always make sure there is an associated liquidity pool with healthy liquidity for the pair that you intend to swap.

llms.txt

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ Account queries should check:
376376
- The correct address and address format is used.
377377
- Whether the account meets existential deposit requirements.
378378
- Balance of the sending account has:
379-
- Enough to pay for fees (transaction fees, delivery fees, and/or swap fees).
379+
- Enough to pay for fees (transaction fees, XCM execution and delivery fees, and/or swap fees).
380380
- Enough to cover the existential deposit before and after the transfer.
381381
- Balance of the recipient account has:
382382
- Enough to cover for the existential deposit.
@@ -433,11 +433,11 @@ async function queryAccountBalance(address: string) {
433433
- **Kusama**: 0.0033 KSM (3.3 * 10^10 planck)
434434
- **Asset Hub**: 0.01 DOT (10^8 planck)
435435

436-
For non-sufficient assets, make sure the destination account has enough native tokens or assets to maintain the ED; otherwise, include asset conversion instructions in the XCM.
436+
For non-sufficient assets, make sure the destination account has a sufficient amount in the form of a native or sufficient asset to cover for the ED; otherwise, include asset conversion instructions in the XCM to swap for a sufficient asset or native token.
437437

438438
## Fee Estimation and Coverage
439439

440-
When it comes to transaction fees, XCM delivery fees, and asset conversion swapping fees, you can use associated runtime APIs to obtain an accurate estimate of the fee rather than hardcoded values.
440+
When it comes to transaction fees, XCM execution and delivery fees, and asset conversion swapping fees, you can use associated runtime APIs to obtain an accurate estimate of the fee rather than hardcoded values.
441441

442442
### Runtime API Integration
443443

@@ -458,6 +458,41 @@ const feeDetails = await api.apis.TransactionPaymentApi.query_fee_details(
458458
```
459459
This code establishes a connection to the Polkadot network and queries detailed fee information for a specific transaction call, including base fees, length fees, and tip calculations.
460460

461+
462+
- [XCM Payment API](https://paritytech.github.io/polkadot-sdk/master/xcm_runtime_apis/fees/trait.XcmPaymentApi.html){target=\_blank} for local XCM execution fees:
463+
464+
```typescript
465+
import { dot } from '@polkadot-api/descriptors';
466+
import { createClient } from 'polkadot-api';
467+
import { getWsProvider } from 'polkadot-api/ws-provider/web';
468+
469+
const client = createClient(getWsProvider('wss://rpc.polkadot.io'));
470+
const api = client.getTypedApi(dot);
471+
472+
const xcm = XcmVersionedXcm.V5([...]);
473+
474+
// These will be set if the runtime API calls are successful.
475+
let localExecutionFees = 0;
476+
// We query the weight of the xcm.
477+
const weightResult = await api.apis.XcmPaymentApi.query_xcm_weight(xcm);
478+
if (weightResult.success) {
479+
// We convert the weight to a fee amount.
480+
// The asset is { parents: 1, interior: Here }, aka, DOT.
481+
const executionFeesResult = await api.apis.XcmPaymentApi.query_weight_to_asset_fee(
482+
weightResult.value,
483+
XcmVersionedAssetId.V5({
484+
parents: 1,
485+
interior: XcmV3Junctions.Here(),
486+
}),
487+
);
488+
if (executionFeesResult.success) {
489+
localExecutionFees = executionFeesResult.value;
490+
}
491+
}
492+
```
493+
This code calculates the local XCM execution fees required to execute an XCM message on the parachain.
494+
495+
461496
- [XCM Payment API](https://paritytech.github.io/polkadot-sdk/master/xcm_runtime_apis/fees/trait.XcmPaymentApi.html){target=\_blank} for cross-chain delivery fees:
462497

463498
```typescript
@@ -475,6 +510,7 @@ const deliveryFees = await api.apis.XcmPaymentApi.query_delivery_fees(
475510
```
476511
This code calculates the delivery fees required to send an XCM message to a specific destination parachain, helping estimate cross-chain transaction costs.
477512

513+
478514
- [Asset Conversion API](https://paritytech.github.io/polkadot-sdk/master/pallet_asset_conversion/trait.AssetConversionApi.html){target=\_blank} (available on Asset Hub) for fee conversion:
479515

480516
```typescript
@@ -510,8 +546,8 @@ For dealing with non-sufficient assets and fees, there are different fee payment
510546

511547
Sufficient assets can:
512548

513-
- Pay for transaction and XCM delivery fees directly.
514549
- Suffice for account existence by meeting the existential deposit requirements.
550+
- Pay for transaction and, if the chain configuration allows, sufficient assets can also be used for XCM execution and delivery fees.
515551

516552
!!!note Sufficient Assets
517553
Always check the chain to see what assets are considered sufficient assets.
@@ -521,8 +557,8 @@ Sufficient assets can:
521557

522558
Non-sufficient assets can:
523559

524-
- Pay for transaction and XCM delivery fees by swapping for a sufficent asset.
525-
- Be used to create a new account by swapping for an exact amount of a sufficient asset to cover for the existential deposit of the new account.
560+
- Pay for transaction and XCM execution/delivery fees by swapping for a sufficent asset.
561+
- Be used to create new accounts by swapping for an exact amount of a sufficient asset to cover for the existential deposit of the new account.
526562

527563
!!!note Swapping Non-Sufficient Assets
528564
Swapping non-sufficient assets for a sufficient asset can be done with the help of [asset conversion](https://wiki.polkadot.network/learn/learn-asset-conversion-assethub){target=\_blank} which is live on Asset Hub. Always make sure there is an associated liquidity pool with healthy liquidity for the pair that you intend to swap.
@@ -22738,9 +22774,9 @@ Non-sufficient assets, on the other hand, **cannot** suffice on their own for ac
2273822774

2273922775
On Asset Hub, sufficient assets:
2274022776

22741-
- Can be natively used for the existential deposit (ED).
22742-
- Can be natively used for [paying transaction fees](#transaction-fees).
22743-
- Can be natively used for [paying XCM delivery fees](#xcm-fees).
22777+
- Can be used for the existential deposit (ED).
22778+
- Can be used for [paying transaction fees](#transaction-fees).
22779+
- Can be used for [paying XCM execution and delivery fees](#xcm-fees).
2274422780
- Cannot be used for storage deposits (only if swapped for the native token).
2274522781

2274622782
### Non-Sufficient Assets
@@ -22758,14 +22794,14 @@ This allows developers to create a seamless experience for end users by allowing
2275822794

2275922795
### Transaction Fees
2276022796

22761-
On Asset Hub, both sufficient and non-sufficient assets can be used for paying transaction fees. For sufficient assets, Asset Hub natively converts the native asset (e.g. DOT) amount required to an asset balance and the signer actually pays that asset to a collator. And for non-sufficient assets on Asset Hub developers can leverage [asset conversion](https://wiki.polkadot.network/learn/learn-asset-conversion-assethub){target=\_blank} via a [swap](/tutorials/polkadot-sdk/system-chains/asset-hub/asset-conversion/) or an XCM `ExchangeAsset` instruction to swap the non-sufficient asset for the native asset. For this to work, non-sufficient assets need to have a [liquidity pool set up](https://wiki.polkadot.network/learn/learn-guides-asset-conversion/#create-a-liquidity-pool){target=\_blank} against the native asset (e.g. DOT).
22797+
On Asset Hub, both sufficient and non-sufficient assets can be used for paying transaction fees. For sufficient assets, Asset Hub natively converts the native asset (e.g. DOT) amount required to an asset balance and the signer actually pays that asset to a collator. And for non-sufficient assets on Asset Hub developers can leverage [asset conversion](https://wiki.polkadot.network/learn/learn-asset-conversion-assethub){target=\_blank} via a [swap](/tutorials/polkadot-sdk/system-chains/asset-hub/asset-conversion/) or an XCM `ExchangeAsset` instruction to swap the non-sufficient asset for any asset that has a [liquidity pool set up](https://wiki.polkadot.network/learn/learn-guides-asset-conversion/#create-a-liquidity-pool){target=\_blank} against the native asset or sufficient asset and use those tokens to cover for transaction fees.
2276222798

2276322799
!!!note
2276422800
Existing UI's, wallets, and tools may have limitations because of design decisions and/or contraints it places e.g. constructing the XCM call in a specific manner and therefore limiting the end user when in reality the Asset Hub system parachain encompasses more functionality than exposed in the third-party UI, such as the ability to pay transaction fees and XCM delivery fees in any asset.
2276522801

22766-
### XCM Delivery Fees
22802+
### XCM Execution and Delivery Fees
2276722803

22768-
Both sufficient assets and non-sufficient assets can be used to pay for XCM delivery fees on Asset Hub. However, it is important to note that the XCM program needs to explicitly reference the asset to pay the XCM fee in.
22804+
Both sufficient assets and non-sufficient assets can be used to pay for XCM local execution fees and delivery fees on Asset Hub. However, it is important to note that the XCM program needs to explicitly reference the asset to pay the XCM fee in.
2276922805

2277022806
For non-sufficient assets, this can be done by calling [asset conversion's swap](https://paritytech.github.io/polkadot-sdk/master/pallet_asset_conversion/pallet/dispatchables/fn.swap_tokens_for_exact_tokens.html){target=\_blank} or including an [`ExchangeAsset`](https://paritytech.github.io/polkadot-sdk/master/cumulus_primitives_core/enum.Instruction.html#variant.ExchangeAsset){target=\_blank} instruction in the XCM program to swap the non-sufficient asset for the native asset to pay for the XCM delivery fee.
2277122807

polkadot-protocol/architecture/system-chains/asset-hub.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ Non-sufficient assets, on the other hand, **cannot** suffice on their own for ac
129129

130130
On Asset Hub, sufficient assets:
131131

132-
- Can be natively used for the existential deposit (ED).
133-
- Can be natively used for [paying transaction fees](#transaction-fees).
134-
- Can be natively used for [paying XCM delivery fees](#xcm-fees).
132+
- Can be used for the existential deposit (ED).
133+
- Can be used for [paying transaction fees](#transaction-fees).
134+
- Can be used for [paying XCM execution and delivery fees](#xcm-fees).
135135
- Cannot be used for storage deposits (only if swapped for the native token).
136136

137137
### Non-Sufficient Assets
@@ -149,14 +149,14 @@ This allows developers to create a seamless experience for end users by allowing
149149

150150
### Transaction Fees
151151

152-
On Asset Hub, both sufficient and non-sufficient assets can be used for paying transaction fees. For sufficient assets, Asset Hub natively converts the native asset (e.g. DOT) amount required to an asset balance and the signer actually pays that asset to a collator. And for non-sufficient assets on Asset Hub developers can leverage [asset conversion](https://wiki.polkadot.network/learn/learn-asset-conversion-assethub){target=\_blank} via a [swap](/tutorials/polkadot-sdk/system-chains/asset-hub/asset-conversion/) or an XCM `ExchangeAsset` instruction to swap the non-sufficient asset for the native asset. For this to work, non-sufficient assets need to have a [liquidity pool set up](https://wiki.polkadot.network/learn/learn-guides-asset-conversion/#create-a-liquidity-pool){target=\_blank} against the native asset (e.g. DOT).
152+
On Asset Hub, both sufficient and non-sufficient assets can be used for paying transaction fees. For sufficient assets, Asset Hub natively converts the native asset (e.g. DOT) amount required to an asset balance and the signer actually pays that asset to a collator. And for non-sufficient assets on Asset Hub developers can leverage [asset conversion](https://wiki.polkadot.network/learn/learn-asset-conversion-assethub){target=\_blank} via a [swap](/tutorials/polkadot-sdk/system-chains/asset-hub/asset-conversion/) or an XCM `ExchangeAsset` instruction to swap the non-sufficient asset for any asset that has a [liquidity pool set up](https://wiki.polkadot.network/learn/learn-guides-asset-conversion/#create-a-liquidity-pool){target=\_blank} against the native asset or sufficient asset and use those tokens to cover for transaction fees.
153153

154154
!!!note
155155
Existing UI's, wallets, and tools may have limitations because of design decisions and/or contraints it places e.g. constructing the XCM call in a specific manner and therefore limiting the end user when in reality the Asset Hub system parachain encompasses more functionality than exposed in the third-party UI, such as the ability to pay transaction fees and XCM delivery fees in any asset.
156156

157-
### XCM Delivery Fees
157+
### XCM Execution and Delivery Fees
158158

159-
Both sufficient assets and non-sufficient assets can be used to pay for XCM delivery fees on Asset Hub. However, it is important to note that the XCM program needs to explicitly reference the asset to pay the XCM fee in.
159+
Both sufficient assets and non-sufficient assets can be used to pay for XCM local execution fees and delivery fees on Asset Hub. However, it is important to note that the XCM program needs to explicitly reference the asset to pay the XCM fee in.
160160

161161
For non-sufficient assets, this can be done by calling [asset conversion's swap](https://paritytech.github.io/polkadot-sdk/master/pallet_asset_conversion/pallet/dispatchables/fn.swap_tokens_for_exact_tokens.html){target=\_blank} or including an [`ExchangeAsset`](https://paritytech.github.io/polkadot-sdk/master/cumulus_primitives_core/enum.Instruction.html#variant.ExchangeAsset){target=\_blank} instruction in the XCM program to swap the non-sufficient asset for the native asset to pay for the XCM delivery fee.
162162

0 commit comments

Comments
 (0)