Skip to content

Commit

Permalink
Add moonChainFee and separate MrlChainRoute from ChainRoute
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaurello committed Oct 8, 2024
1 parent 737ed0a commit 69ff80e
Show file tree
Hide file tree
Showing 15 changed files with 167 additions and 28 deletions.
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
6 changes: 3 additions & 3 deletions packages/config/src/mrl-configs/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +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
12 changes: 8 additions & 4 deletions packages/config/src/mrl-configs/peaqEvmAlphanet.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, peaqEvmAlphanet } from '../chains';
import { ChainRoutes } from '../types/ChainRoutes';
import { MrlChainRoutes } from '../types/MrlChainRoutes';

export const peaqEvmAlphanetRoutes = new ChainRoutes({
export const peaqEvmAlphanetRoutes = new MrlChainRoutes({
chain: peaqEvmAlphanet,
routes: [
{
Expand All @@ -14,6 +14,10 @@ export const peaqEvmAlphanetRoutes = new ChainRoutes({
asset: ftmwh,
balance: BalanceBuilder().evm().erc20(),
},
moonChainFee: {
asset: dev,
balance: BalanceBuilder().evm().erc20(),
},
fee: {
asset: agng,
balance: BalanceBuilder().substrate().system().accountEvmTo32(),
Expand All @@ -33,8 +37,8 @@ export const peaqEvmAlphanetRoutes = new ChainRoutes({
transfer: MrlBuilder()
.wormhole()
.contract()
.TokenBridge()
.transferTokens(),
.TokenBridgeRelayer()
.transferTokensWithRelay(),
moonChain: {
asset: ftmwh,
fee: {
Expand Down
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 AssetRoute',
);
}

return getBalance({
address: sourceAddress,
asset: route.source.chain.getChainAsset(route.source.moonChainFee.asset),
builder: route.source.moonChainFee?.balance,
chain: route.source.chain,
});
}
4 changes: 2 additions & 2 deletions packages/mrl/src/getTransferData/getTransferData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
ExtrinsicConfig,
WormholeConfig,
} from '@moonbeam-network/xcm-builder';
import type { AssetRoute } from '@moonbeam-network/xcm-config';
import type { MrlAssetRoute } from '@moonbeam-network/xcm-config';
import {
EvmService,
PolkadotService,
Expand All @@ -29,7 +29,7 @@ import {
} from './getTransferData.utils';

export interface GetTransferDataParams {
route: AssetRoute;
route: MrlAssetRoute;
sourceAddress: string;
destinationAddress: string;
}
Expand Down
1 change: 1 addition & 0 deletions packages/mrl/src/mrl.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface TransferData {

export interface SourceTransferData extends SourceChainTransferData {
destinationFeeBalance: AssetAmount;
moonChainFeeBalance?: AssetAmount;
relayerFee?: AssetAmount;
feeBalance: AssetAmount;
max: AssetAmount;
Expand Down

0 comments on commit 69ff80e

Please sign in to comment.