Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaurello committed Sep 24, 2024
2 parents 6cddf64 + 2d80551 commit 7df4f90
Show file tree
Hide file tree
Showing 68 changed files with 790 additions and 206 deletions.
4 changes: 3 additions & 1 deletion mkdocs/docs/contribute.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,9 @@ export const polkadotAssetHubConfig = new ChainConfig({
balance: BalanceBuilder().substrate().assets().account(),
destination: moonbeam,
destinationFee: {
amount: FeeBuilder().assetManager().assetTypeUnitsPerSecond(),
amount: FeeBuilder()
.xcmPaymentApi()
.xcmPaymentFee({ isAssetReserveChain: false }),
asset: usdt,
balance: BalanceBuilder().substrate().assets().account(),
},
Expand Down
12 changes: 9 additions & 3 deletions mkdocs/docs/reference/interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,9 @@ Defines a chain's configurations, including information for each chain's support
balance: BalanceBuilder().substrate().assets().account(),
destination: moonbeam,
destinationFee: {
amount: FeeBuilder().assetManager().assetTypeUnitsPerSecond(),
amount: FeeBuilder()
.xcmPaymentApi()
.xcmPaymentFee({ isAssetReserveChain: false }),
asset: usdt,
balance: BalanceBuilder().substrate().assets().account(),
},
Expand Down Expand Up @@ -761,7 +763,9 @@ Defines an asset's configurations for a source chain and includes information ab
balance: BalanceBuilder().substrate().assets().account(),
destination: moonbeam,
destinationFee: {
amount: FeeBuilder().assetManager().assetTypeUnitsPerSecond(),
amount: FeeBuilder()
.xcmPaymentApi()
.xcmPaymentFee({ isAssetReserveChain: false }),
asset: usdt,
balance: BalanceBuilder().substrate().assets().account(),
},
Expand Down Expand Up @@ -831,7 +835,9 @@ Defines the fees for a particular asset on the destination chain.
{
asset: dot,
balance: BalanceBuilder().substrate().system().account(),
amount: FeeBuilder().assetManager().assetTypeUnitsPerSecond(),
amount: amount: FeeBuilder()
.xcmPaymentApi()
.xcmPaymentFee({ isAssetReserveChain: false }),
}
```

Expand Down
11 changes: 11 additions & 0 deletions packages/builder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# @moonbeam-network/xcm-builder

## 2.5.0

### Minor Changes

- [#351](https://github.com/moonbeam-foundation/xcm-sdk/pull/351) [`2a7ed04`](https://github.com/moonbeam-foundation/xcm-sdk/commit/2a7ed04887ee41e5a6c010f213265028a953a769) Thanks [@mmaurello](https://github.com/mmaurello)! - Implement XcmPaymentApi to calculate fees for routes going to Moonbeam / Moonriver / Moonbase-Alpha

### Patch Changes

- Updated dependencies [[`2a7ed04`](https://github.com/moonbeam-foundation/xcm-sdk/commit/2a7ed04887ee41e5a6c010f213265028a953a769)]:
- @moonbeam-network/xcm-types@2.4.0

## 2.4.7

### Patch Changes
Expand Down
12 changes: 9 additions & 3 deletions packages/builder/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@moonbeam-network/xcm-builder",
"version": "2.4.7",
"version": "2.5.0",
"description": "Moonbeam XCM builder",
"scripts": {
"build": "tsup",
Expand All @@ -16,14 +16,20 @@
"type": "git",
"url": "git+https://github.com/moonbeam-foundation/xcm-sdk.git"
},
"keywords": ["moonbeam", "moonriver", "xcm"],
"keywords": [
"moonbeam",
"moonriver",
"xcm"
],
"author": "moonbeam-foundation",
"license": "MIT",
"bugs": {
"url": "https://github.com/moonbeam-foundation/xcm-sdk/issues"
},
"homepage": "https://moonbeam-foundation.github.io/xcm-sdk/latest",
"files": ["build"],
"files": [
"build"
],
"type": "module",
"exports": "./build/index.mjs",
"types": "./build/index.d.ts",
Expand Down
20 changes: 18 additions & 2 deletions packages/builder/src/fee/FeeBuilder.interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { ChainAssetId } from '@moonbeam-network/xcm-types';
import type { AnyParachain, Asset } from '@moonbeam-network/xcm-types';
import type { ApiPromise } from '@polkadot/api';
import type { Enum } from '@polkadot/types';
import type { StagingXcmV3MultiLocation } from '@polkadot/types/lookup';
import type { ConfigBuilder } from '../builder.interfaces';
import type { SubstrateCallConfig } from '../types/substrate/SubstrateCallConfig';

Expand All @@ -9,6 +11,20 @@ export type FeeConfigBuilder = ConfigBuilder<
>;

export interface FeeConfigBuilderPrams {
asset: ChainAssetId;
address: string;
api: ApiPromise;
chain: AnyParachain;
feeAsset: Asset;
transferAsset: Asset;
}

export interface XcmPaymentFeeProps {
isAssetReserveChain: boolean;
shouldTransferAssetPrecedeAsset?: boolean;
}

export interface MoonbeamRuntimeXcmConfigAssetType extends Enum {
readonly isXcm: boolean;
readonly asXcm: StagingXcmV3MultiLocation;
readonly type: 'Xcm';
}
81 changes: 60 additions & 21 deletions packages/builder/src/fee/FeeBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,77 @@
import type { Option, u128 } from '@polkadot/types';
/* eslint-disable sort-keys */
/* eslint-disable @typescript-eslint/no-use-before-define */

import { SubstrateCallConfig } from '../types/substrate/SubstrateCallConfig';
import type { FeeConfigBuilder } from './FeeBuilder.interfaces';
import type {
FeeConfigBuilder,
FeeConfigBuilderPrams,
XcmPaymentFeeProps,
} from './FeeBuilder.interfaces';
import {
getBuyExecutionInstruction,
getClearOriginInstruction,
getDepositAssetInstruction,
getFeeForXcmInstructionsAndAsset,
getReserveAssetDepositedInstruction,
getSetTopicInstruction,
getVersionedAssetId,
getWithdrawAssetInstruction,
} from './FeeBuilder.utils';

export function FeeBuilder() {
return {
assetManager,
xcmPaymentApi,
};
}

function assetManager() {
function xcmPaymentApi() {
return {
assetTypeUnitsPerSecond: (weight = 1_000_000_000): FeeConfigBuilder => ({
build: ({ api, asset }) =>
xcmPaymentFee: ({
isAssetReserveChain,
shouldTransferAssetPrecedeAsset = false,
}: XcmPaymentFeeProps): FeeConfigBuilder => ({
build: ({
address,
api,
feeAsset,
chain,
transferAsset,
}: FeeConfigBuilderPrams) =>
new SubstrateCallConfig({
api,
call: async (): Promise<bigint> => {
const type = (await api.query.assetManager.assetIdType(
asset,
// biome-ignore lint/suspicious/noExplicitAny: not sure how to fix this
)) as unknown as Option<any>;

if (type.isNone) {
throw new Error(`No asset type found for asset ${asset}`);
}

const unwrappedType = type.unwrap();
const versionedAssetId = await getVersionedAssetId(
api,
feeAsset,
chain,
);
const versionedTransferAssetId = await getVersionedAssetId(
api,
transferAsset,
chain,
);
const versionedAssets = shouldTransferAssetPrecedeAsset
? [versionedTransferAssetId, versionedAssetId]
: [versionedAssetId, versionedTransferAssetId];

const res = (await api.query.assetManager.assetTypeUnitsPerSecond(
unwrappedType,
)) as unknown as Option<u128>;
const assets =
feeAsset === transferAsset ? [versionedAssetId] : versionedAssets;

const unitsPerSecond = res.unwrapOrDefault().toBigInt();
const instructions = [
isAssetReserveChain
? getWithdrawAssetInstruction(assets)
: getReserveAssetDepositedInstruction(assets),
getClearOriginInstruction(),
getBuyExecutionInstruction(versionedAssetId),
getDepositAssetInstruction(address, assets),
getSetTopicInstruction(),
];

return (BigInt(weight) * unitsPerSecond) / BigInt(10 ** 12);
return getFeeForXcmInstructionsAndAsset(
api,
instructions,
versionedAssetId,
);
},
}),
}),
Expand Down
Loading

0 comments on commit 7df4f90

Please sign in to comment.