Skip to content

Commit

Permalink
AssetRoute refactoring (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekenigs authored Aug 27, 2024
1 parent 154b63b commit f65ae80
Show file tree
Hide file tree
Showing 65 changed files with 3,871 additions and 2,642 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-turtles-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@moonbeam-network/xcm-config': patch
---

Adjust kbtc and ibtc fees
4 changes: 4 additions & 0 deletions packages/builder/src/contract/ContractConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export class ContractConfig extends BaseConfig {

readonly address?: string;

static is(obj: unknown): obj is ContractConfig {
return obj instanceof ContractConfig;
}

constructor({ args, address, ...other }: ContractConfigConstructorParams) {
super({ ...other, type: CallType.Evm });

Expand Down
4 changes: 4 additions & 0 deletions packages/builder/src/extrinsic/ExtrinsicConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export class ExtrinsicConfig extends BaseConfig {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getArgs: (func?: SubmittableExtrinsicFunction<'promise'>) => any[];

static is(obj: unknown): obj is ExtrinsicConfig {
return obj instanceof ExtrinsicConfig;
}

constructor({ getArgs, ...other }: ExtrinsicConfigConstructorParams) {
super({ ...other, type: CallType.Substrate });

Expand Down
67 changes: 24 additions & 43 deletions packages/config/src/ConfigService/ConfigService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
} from '../chains';
import { ConfigService } from './ConfigService';

import { AssetRoute } from '../types/AssetRoute';
import { ChainRoutes } from '../types/ChainRoutes';
import { xcmRoutesMap } from '../xcm-configs';

Expand Down Expand Up @@ -201,53 +200,35 @@ describe('config service', () => {
});
});

describe('updateChainConfig', () => {
describe('updateChainRoute', () => {
it('should update existing chain config', () => {
const assetConfig = new AssetRoute({
asset: glmr,
balance: BalanceBuilder().substrate().tokens().accounts(),
destination: moonbeam,
destinationFee: {
amount: 0.02,
asset: glmr,
balance: BalanceBuilder().substrate().tokens().accounts(),
},
extrinsic: ExtrinsicBuilder().xTokens().transfer(),
});

const chainConfig = new ChainRoutes({
routes: [assetConfig],
const routes = new ChainRoutes({
chain: hydration,
routes: [
{
asset: glmr,
source: {
balance: BalanceBuilder().substrate().tokens().accounts(),
},
destination: {
chain: moonbeam,
balance: BalanceBuilder().substrate().tokens().accounts(),
fee: {
amount: 0.02,
asset: glmr,
balance: BalanceBuilder().substrate().tokens().accounts(),
},
},
extrinsic: ExtrinsicBuilder().xTokens().transfer(),
},
],
});

configService.updateChainConfig(chainConfig);
const updated = configService.getChainRoutes(hydration);
expect(updated.getRoutes()).toStrictEqual(chainConfig.getRoutes());
});

it('should create new chain config', () => {
configService.updateChain(TEST_CHAIN);

const assetConfig = new AssetRoute({
asset: glmr,
balance: BalanceBuilder().substrate().tokens().accounts(),
destination: moonbeam,
destinationFee: {
amount: 0.02,
asset: glmr,
balance: BalanceBuilder().substrate().tokens().accounts(),
},
extrinsic: ExtrinsicBuilder().xTokens().transfer(),
});

const chainConfig = new ChainRoutes({
routes: [assetConfig],
chain: TEST_CHAIN,
});
configService.updateChainRoute(routes);

configService.updateChainConfig(chainConfig);
const updated = configService.getChainRoutes('test');
expect(updated.getRoutes()).toStrictEqual(chainConfig.getRoutes());
expect(configService.getChainRoutes(hydration).getRoutes()).toStrictEqual(
routes.getRoutes(),
);
});
});
});
20 changes: 11 additions & 9 deletions packages/config/src/ConfigService/ConfigService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ export class ConfigService {

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

if (!config) {
throw new Error(`Chain config for ${key} not found`);
if (!route) {
throw new Error(`ChainRoute for ${key} not found`);
}

return config;
return route;
}

getSourceChains({
Expand Down Expand Up @@ -105,14 +105,16 @@ export class ConfigService {
asset?: string | AnyAsset;
source: string | AnyChain;
}): AnyChain[] {
const config = this.getChainRoutes(source);
const chainRoutes = this.getChainRoutes(source);

if (asset) {
return config.getAssetDestinations(asset);
return chainRoutes.getAssetDestinations(asset);
}

return Array.from(
new Set(config.getRoutes().map((routes) => routes.destination)),
new Set(
chainRoutes.getRoutes().map((routes) => routes.destination.chain),
),
);
}

Expand Down Expand Up @@ -150,7 +152,7 @@ export class ConfigService {
this.chains.set(chain.key, chain);
}

updateChainConfig(chainConfig: ChainRoutes): void {
this.routes.set(chainConfig.chain.key, chainConfig);
updateChainRoute(route: ChainRoutes): void {
this.routes.set(route.chain.key, route);
}
}
24 changes: 14 additions & 10 deletions packages/config/src/mrl-configs/hydrationAlphanet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@ import {
} from '@moonbeam-network/xcm-builder';
import { ftmwh, hdx } from '../assets';
import { fantomTestnet, hydrationAlphanet } from '../chains';
import { AssetRoute } from '../types/AssetRoute';
import { ChainRoutes } from '../types/ChainRoutes';

export const hydrationAlphanetRoutes = new ChainRoutes({
chain: hydrationAlphanet,
routes: [
new AssetRoute({
{
asset: ftmwh,
balance: BalanceBuilder().substrate().system().account(), // TODO:
destination: fantomTestnet,
destinationFee: {
// TODO:
amount: FeeBuilder().assetManager().assetTypeUnitsPerSecond(),
asset: hdx,
balance: BalanceBuilder().substrate().system().account(),
source: {
balance: BalanceBuilder().substrate().system().account(), // TODO:
},
destination: {
chain: fantomTestnet,
balance: BalanceBuilder().substrate().system().account(), // TODO:
fee: {
// TODO:
amount: FeeBuilder().assetManager().assetTypeUnitsPerSecond(),
asset: hdx,
balance: BalanceBuilder().substrate().system().account(),
},
},
extrinsic: ExtrinsicBuilder().xTokens().transfer(), // TODO:
}),
},
],
});
55 changes: 26 additions & 29 deletions packages/config/src/types/AssetRoute.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,65 @@
import {
import type {
AssetMinConfigBuilder,
BalanceConfigBuilder,
ContractConfigBuilder,
ExtrinsicConfigBuilder,
FeeConfigBuilder,
} from '@moonbeam-network/xcm-builder';
import { AnyChain, Asset } from '@moonbeam-network/xcm-types';
import type { AnyChain, Asset } from '@moonbeam-network/xcm-types';

export interface AssetRouteConstructorParams {
asset: Asset;
balance: BalanceConfigBuilder;
source: SourceConfig;
destination: DestinationConfig;
contract?: ContractConfigBuilder;
destination: AnyChain;
destinationFee: DestinationFeeConfig;
extrinsic?: ExtrinsicConfigBuilder;
fee?: FeeAssetConfig;
}

export interface SourceConfig {
chain: AnyChain;
balance: BalanceConfigBuilder;
fee?: FeeConfig;
min?: AssetMinConfigBuilder;
}

export interface DestinationFeeConfig extends FeeAssetConfig {
amount: number | FeeConfigBuilder;
export interface DestinationConfig extends Omit<SourceConfig, 'fee'> {
fee: DestinationFeeConfig;
}

export interface FeeAssetConfig {
export interface FeeConfig {
asset: Asset;
balance: BalanceConfigBuilder;
// Sometimes we need to add some extra amount ("XCM Delivery Fee") to a fee that is returned by "paymentInfo" for extrinsic to not fail.
// NOTE: Sometimes we need to add some extra amount ("XCM Delivery Fee") to a fee
// that is returned by "paymentInfo" for extrinsic to not fail.
extra?: number;
}

export interface DestinationFeeConfig extends FeeConfig {
amount: number | FeeConfigBuilder;
}

export class AssetRoute {
readonly asset: Asset;

readonly balance: BalanceConfigBuilder;
readonly source: SourceConfig;

readonly contract?: ContractConfigBuilder;
readonly destination: DestinationConfig;

readonly destination: AnyChain;

readonly destinationFee: DestinationFeeConfig;
readonly contract?: ContractConfigBuilder;

readonly extrinsic?: ExtrinsicConfigBuilder;

readonly fee?: FeeAssetConfig;

readonly min?: AssetMinConfigBuilder;

constructor({
asset,
balance,
contract,
source,
destination,
destinationFee,
contract,
extrinsic,
fee,
min,
}: AssetRouteConstructorParams) {
this.asset = asset;
this.balance = balance;
this.contract = contract;
this.source = source;
this.destination = destination;
this.destinationFee = destinationFee;
this.contract = contract;
this.extrinsic = extrinsic;
this.fee = fee;
this.min = min;
}
}
44 changes: 29 additions & 15 deletions packages/config/src/types/ChainRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
import { AnyAsset, AnyChain, Asset } from '@moonbeam-network/xcm-types';
import type { AnyAsset, AnyChain, Asset } from '@moonbeam-network/xcm-types';
import { getKey } from '../config.utils';
import { AssetRoute } from './AssetRoute';
import {
AssetRoute,
AssetRouteConstructorParams,
SourceConfig,
} from './AssetRoute';

export interface ChainRoutesConstructorParams {
routes: AssetRoute[];
chain: AnyChain;
routes: RoutesParam[];
}

export class ChainRoutes {
readonly #routes: Map<string, AssetRoute>;
interface RoutesParam extends Omit<AssetRouteConstructorParams, 'source'> {
source: Omit<SourceConfig, 'chain'>;
}

export class ChainRoutes {
readonly chain: AnyChain;

constructor({ routes: assets, chain }: ChainRoutesConstructorParams) {
readonly #routes: Map<string, AssetRoute>;

constructor({ chain, routes }: ChainRoutesConstructorParams) {
this.chain = chain;
this.#routes = new Map(
assets.map((asset) => [
`${asset.asset.key}-${asset.destination.key}`,
asset,
routes.map(({ asset, source, destination, contract, extrinsic }) => [
`${asset.key}-${destination.chain.key}`,
new AssetRoute({
asset,
source: { ...source, chain },
destination,
contract,
extrinsic,
}),
]),
);
}
Expand All @@ -34,15 +48,15 @@ export class ChainRoutes {

getAssetDestinations(keyOrAsset: string | AnyAsset): AnyChain[] {
return this.getAssetRoutes(keyOrAsset).map(
(assetConfig) => assetConfig.destination,
(assetConfig) => assetConfig.destination.chain,
);
}

getDestinationAssets(keyOrChain: string | AnyChain): Asset[] {
const key = getKey(keyOrChain);

return this.getRoutes()
.filter((cfg) => cfg.destination.key === key)
.filter((cfg) => cfg.destination.chain.key === key)
.map((cfg) => cfg.asset);
}

Expand All @@ -52,14 +66,14 @@ export class ChainRoutes {
): AssetRoute {
const assetKey = getKey(asset);
const destKey = getKey(destination);
const config = this.#routes.get(`${assetKey}-${destKey}`);
const route = this.#routes.get(`${assetKey}-${destKey}`);

if (!config) {
if (!route) {
throw new Error(
`AssetConfig for ${assetKey} and destination ${destKey} not found`,
`AssetRoute for asset ${assetKey} and destination ${destKey} not found`,
);
}

return config;
return route;
}
}
Loading

0 comments on commit f65ae80

Please sign in to comment.