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

Separate Routes between MRL and XCM and get moon chain fee balance in source #366

Merged
merged 4 commits into from
Oct 8, 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
Expand Up @@ -4,9 +4,9 @@ import { ExtrinsicBuilder } from '../../../../../extrinsic/ExtrinsicBuilder';
import { ExtrinsicConfig } from '../../../../../types/substrate/ExtrinsicConfig';
import type { MrlConfigBuilder } from '../../../../MrlBuilder.interfaces';

// TODO: Can we move them somewhere?
const BUY_EXECUTION_FEE = 100_000_000_000_000_000n;
const CROSS_CHAIN_FEE = 100_000_000_000_000_000n;
// TODO: these have to come from the configs
const BUY_EXECUTION_FEE = 100_000_000_000_000_000n; // moonChainFee
const CROSS_CHAIN_FEE = 100_000_000_000_000_000n; // fee for processing the xcm message in moon chain

export function polkadotXcm() {
return {
Expand Down
10 changes: 6 additions & 4 deletions packages/config/src/ConfigService/ConfigService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ import { chainsMap } from '../chains';
import { getKey } from '../config.utils';
import type { AssetRoute } from '../types/AssetRoute';
import type { ChainRoutes } from '../types/ChainRoutes';
import type { MrlAssetRoute } from '../types/MrlAssetRoute';
import type { MrlChainRoutes } from '../types/MrlChainRoutes';

export interface ConfigServiceOptions {
assets?: Map<string, Asset>;
chains?: Map<string, AnyChain>;
routes: Map<string, ChainRoutes>;
routes: Map<string, ChainRoutes | MrlChainRoutes>;
}

export class ConfigService {
protected assets: Map<string, Asset>;

protected chains: Map<string, AnyChain>;

protected routes: Map<string, ChainRoutes>;
protected routes: Map<string, ChainRoutes | MrlChainRoutes>;

constructor(options: ConfigServiceOptions) {
this.assets = options.assets ?? assetsMap;
Expand Down Expand Up @@ -67,7 +69,7 @@ export class ConfigService {
return chain;
}

getChainRoutes(keyOrChain: string | AnyChain): ChainRoutes {
getChainRoutes(keyOrChain: string | AnyChain): ChainRoutes | MrlChainRoutes {
const key = getKey(keyOrChain);
const route = this.routes.get(key);

Expand Down Expand Up @@ -126,7 +128,7 @@ export class ConfigService {
asset: string | AnyAsset;
source: string | AnyChain;
destination: string | AnyChain;
}): AssetRoute {
}): AssetRoute | MrlAssetRoute {
const routes = this.getChainRoutes(source);

return routes.getAssetRoute(asset, destination);
Expand Down
2 changes: 2 additions & 0 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ export * from './chains';
export * from './config.utils';
export * from './mrl-configs';
export * from './types/AssetRoute';
export * from './types/MrlAssetRoute';
export * from './types/ChainRoutes';
export * from './types/MrlChainRoutes';
export * from './xcm-configs';
4 changes: 2 additions & 2 deletions packages/config/src/mrl-configs/fantomTestnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
moonbaseBeta,
peaqAlphanet,
} from '../chains';
import { ChainRoutes } from '../types/ChainRoutes';
import { MrlChainRoutes } from '../types/MrlChainRoutes';

export const fantomTestnetRoutes = new ChainRoutes({
export const fantomTestnetRoutes = new MrlChainRoutes({
chain: fantomTestnet,
routes: [
{
Expand Down
8 changes: 5 additions & 3 deletions packages/config/src/mrl-configs/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import type { ChainRoutes } from '../types/ChainRoutes';
import type { MrlChainRoutes } from '../types/MrlChainRoutes';
import { fantomTestnetRoutes } from './fantomTestnet';
import { moonbaseAlphaRoutes } from './moonbaseAlpha';
import { moonbaseBetaRoutes } from './moonbaseBeta';
import { peaqAlphanetRoutes } from './peaqAlphanet';
import { peaqEvmAlphanetRoutes } from './peaqEvmAlphanet';

export const mrlRoutesList: ChainRoutes[] = [
export const mrlRoutesList: MrlChainRoutes[] = [
fantomTestnetRoutes,
moonbaseAlphaRoutes,
moonbaseBetaRoutes,
peaqAlphanetRoutes,
peaqEvmAlphanetRoutes,
];

export const mrlRoutesMap = new Map<string, ChainRoutes>(
export const mrlRoutesMap = new Map<string, MrlChainRoutes>(
mrlRoutesList.map((config) => [config.chain.key, config]),
);
4 changes: 2 additions & 2 deletions packages/config/src/mrl-configs/moonbaseAlpha.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { BalanceBuilder, MrlBuilder } from '@moonbeam-network/xcm-builder';
import { dev, ftm, ftmwh } from '../assets';
import { fantomTestnet, moonbaseAlpha } from '../chains';
import { ChainRoutes } from '../types/ChainRoutes';
import { MrlChainRoutes } from '../types/MrlChainRoutes';

export const moonbaseAlphaRoutes = new ChainRoutes({
export const moonbaseAlphaRoutes = new MrlChainRoutes({
chain: moonbaseAlpha,
routes: [
{
Expand Down
4 changes: 2 additions & 2 deletions packages/config/src/mrl-configs/moonbaseBeta.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { BalanceBuilder, MrlBuilder } from '@moonbeam-network/xcm-builder';
import { betaDEV, dev, ftm, ftmwh } from '../assets';
import { fantomTestnet, moonbaseBeta } from '../chains';
import { ChainRoutes } from '../types/ChainRoutes';
import { MrlChainRoutes } from '../types/MrlChainRoutes';

export const moonbaseBetaRoutes = new ChainRoutes({
export const moonbaseBetaRoutes = new MrlChainRoutes({
chain: moonbaseBeta,
routes: [
{
Expand Down
12 changes: 10 additions & 2 deletions packages/config/src/mrl-configs/peaqAlphanet.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { BalanceBuilder, MrlBuilder } from '@moonbeam-network/xcm-builder';
import { agng, dev, ftm, ftmwh } from '../assets';
import { fantomTestnet, peaqAlphanet } from '../chains';
import { ChainRoutes } from '../types/ChainRoutes';
import { MrlChainRoutes } from '../types/MrlChainRoutes';

export const peaqAlphanetRoutes = new ChainRoutes({
export const peaqAlphanetRoutes = new MrlChainRoutes({
chain: peaqAlphanet,
routes: [
{
Expand All @@ -14,6 +14,10 @@ export const peaqAlphanetRoutes = new ChainRoutes({
asset: ftmwh,
balance: BalanceBuilder().substrate().assets().account(),
},
moonChainFee: {
asset: dev,
balance: BalanceBuilder().substrate().assets().account(),
},
fee: {
asset: agng,
balance: BalanceBuilder().substrate().system().account(),
Expand Down Expand Up @@ -49,6 +53,10 @@ export const peaqAlphanetRoutes = new ChainRoutes({
asset: ftmwh,
balance: BalanceBuilder().substrate().assets().account(),
},
moonChainFee: {
asset: dev,
balance: BalanceBuilder().substrate().assets().account(),
},
},
destination: {
asset: agng,
Expand Down
53 changes: 53 additions & 0 deletions packages/config/src/mrl-configs/peaqEvmAlphanet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { BalanceBuilder, MrlBuilder } from '@moonbeam-network/xcm-builder';
import { agng, dev, ftm, ftmwh } from '../assets';
import { fantomTestnet, peaqEvmAlphanet } from '../chains';
import { MrlChainRoutes } from '../types/MrlChainRoutes';

export const peaqEvmAlphanetRoutes = new MrlChainRoutes({
chain: peaqEvmAlphanet,
routes: [
{
source: {
asset: ftmwh,
balance: BalanceBuilder().evm().erc20(),
destinationFee: {
asset: ftmwh,
balance: BalanceBuilder().evm().erc20(),
},
moonChainFee: {
asset: dev,
balance: BalanceBuilder().evm().erc20(),
},
fee: {
asset: agng,
balance: BalanceBuilder().substrate().system().accountEvmTo32(),
},
},
destination: {
asset: ftm,
chain: fantomTestnet,
balance: BalanceBuilder().evm().native(),
fee: {
asset: ftm,
amount: 0,
},
},
mrl: {
isAutomaticPossible: true,
transfer: MrlBuilder()
.wormhole()
.contract()
.TokenBridgeRelayer()
.transferTokensWithRelay(),
moonChain: {
asset: ftmwh,
fee: {
asset: dev,
amount: 0.1,
balance: BalanceBuilder().substrate().system().account(),
},
},
},
},
],
});
34 changes: 34 additions & 0 deletions packages/config/src/types/MrlAssetRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { BalanceConfigBuilder } from '@moonbeam-network/xcm-builder';
import type { Asset } from '@moonbeam-network/xcm-types';
import {
AssetRoute,
type AssetRouteConstructorParams,
type SourceConfig,
} from './AssetRoute';

export interface MrlAssetRouteConstructorParams
extends AssetRouteConstructorParams {
source: MrlSourceConfig;
}

export interface MrlSourceConfig extends SourceConfig {
moonChainFee?: {
asset: Asset;
balance: BalanceConfigBuilder;
};
}

export class MrlAssetRoute extends AssetRoute {
readonly source: MrlSourceConfig;

constructor({
source,
destination,
contract,
extrinsic,
mrl,
}: MrlAssetRouteConstructorParams & { source: MrlSourceConfig }) {
super({ source, destination, contract, extrinsic, mrl });
this.source = source;
}
}
40 changes: 40 additions & 0 deletions packages/config/src/types/MrlChainRoutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ChainRoutes, type ChainRoutesConstructorParams } from './ChainRoutes';
import {
MrlAssetRoute,
type MrlAssetRouteConstructorParams,
type MrlSourceConfig,
} from './MrlAssetRoute';

export interface MrlChainRoutesConstructorParams
extends ChainRoutesConstructorParams {
routes: MrlRoutesParam[];
}

interface MrlRoutesParam
extends Omit<MrlAssetRouteConstructorParams, 'source'> {
source: Omit<MrlSourceConfig, 'chain'>;
}

export class MrlChainRoutes extends ChainRoutes {
readonly #routes: Map<string, MrlAssetRoute>;

constructor({ chain, routes }: MrlChainRoutesConstructorParams) {
super({ chain, routes });
this.#routes = new Map(
routes.map(({ source, destination, contract, extrinsic, mrl }) => [
`${source.asset.key}-${destination.chain.key}`,
new MrlAssetRoute({
source: { ...source, chain },
destination,
contract,
extrinsic,
mrl,
}),
]),
);
}

getRoutes(): MrlAssetRoute[] {
return Array.from(this.#routes.values());
}
}
4 changes: 2 additions & 2 deletions packages/mrl/src/getTransferData/getMoonChainData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type AssetRoute, getMoonChain } from '@moonbeam-network/xcm-config';
import { type MrlAssetRoute, getMoonChain } from '@moonbeam-network/xcm-config';
import { getBalance, getDestinationFee } from '@moonbeam-network/xcm-sdk';
import { Parachain } from '@moonbeam-network/xcm-types';
import { getMultilocationDerivedAddresses } from '@moonbeam-network/xcm-utils';
Expand All @@ -9,7 +9,7 @@ import type {

export interface GetMoonChainDataParams {
destinationData: DestinationTransferData;
route: AssetRoute;
route: MrlAssetRoute;
sourceAddress: string;
}

Expand Down
52 changes: 50 additions & 2 deletions packages/mrl/src/getTransferData/getSourceData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
MrlBuilder,
WormholeConfig,
} from '@moonbeam-network/xcm-builder';
import type { AssetRoute, FeeConfig } from '@moonbeam-network/xcm-config';
import type { FeeConfig, MrlAssetRoute } from '@moonbeam-network/xcm-config';
import {
getAssetMin,
getBalance,
Expand All @@ -30,7 +30,7 @@ import {
} from './getTransferData.utils';

export interface GetSourceDataParams {
route: AssetRoute;
route: MrlAssetRoute;
destinationAddress: string;
destinationFee: AssetAmount;
sourceAddress: string;
Expand Down Expand Up @@ -68,13 +68,21 @@ export async function getSourceData({
chain: source,
})
: balance;

const destinationFeeBalance = await getDestinationFeeBalance({
balance,
feeBalance,
route,
sourceAddress,
});

const moonChainFeeBalance = await getMoonChainFeeBalance({
balance,
feeBalance,
route,
sourceAddress,
});

const existentialDeposit = await getExistentialDeposit(source);
const min = await getAssetMin({
asset,
Expand Down Expand Up @@ -121,6 +129,7 @@ export async function getSourceData({
balance,
chain: source,
destinationFeeBalance,
moonChainFeeBalance,
existentialDeposit,
fee,
feeBalance,
Expand Down Expand Up @@ -235,3 +244,42 @@ async function getWormholeFee({

return;
}

export interface GetMoonChainFeeBalanceParams {
balance: AssetAmount;
feeBalance: AssetAmount;
route: MrlAssetRoute;
sourceAddress: string;
}

export async function getMoonChainFeeBalance({
balance,
feeBalance,
route,
sourceAddress,
}: GetMoonChainFeeBalanceParams): Promise<AssetAmount | undefined> {
if (!route.source.moonChainFee) {
return undefined;
}

if (route.mrl?.moonChain.fee.asset.isEqual(balance)) {
return balance;
}

if (route.mrl?.moonChain.fee.asset.isEqual(feeBalance)) {
return feeBalance;
}

if (!route.source.moonChainFee.balance) {
throw new Error(
'BalanceBuilder must be defined for source.moonChainFee.balance for MrlAssetRoute',
);
}

return getBalance({
address: sourceAddress,
asset: route.source.chain.getChainAsset(route.source.moonChainFee.asset),
builder: route.source.moonChainFee.balance,
chain: route.source.chain,
});
}
Loading