Skip to content

Commit

Permalink
Added ConfigService, support mutability of static xcm configs (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
nohaapav authored Aug 31, 2023
1 parent 3bf2a78 commit bda7709
Show file tree
Hide file tree
Showing 12 changed files with 408 additions and 197 deletions.
6 changes: 6 additions & 0 deletions .changeset/gold-tips-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@moonbeam-network/xcm-config': minor
'@moonbeam-network/xcm-sdk': minor
---

Introduce config service to support mutability of static xcm config
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,6 @@ dist

# mac
.DS_Store

# IntelliJ Idea
.idea
29 changes: 29 additions & 0 deletions packages/config/src/ConfigBuilder/ConfigBuilder.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable sort-keys */
import { Ecosystem } from '@moonbeam-network/xcm-types';
import { ConfigService } from '../ConfigService';
import { dev } from '../assets';
import { equilibriumAlphanet, moonbaseAlpha } from '../chains';
import { equilibriumAlphanetConfig } from '../configs/equilibriumAlphanet';
Expand Down Expand Up @@ -33,4 +34,32 @@ describe('configBuilder', () => {
},
});
});

it('should return correct dev config using mutable service', () => {
const configService = new ConfigService();
const config = ConfigBuilder(configService)
.assets(Ecosystem.AlphanetRelay)
.asset(dev)
.source(moonbaseAlpha)
.destination(equilibriumAlphanet)
.build();

expect(config).toStrictEqual({
asset: dev,
source: {
chain: moonbaseAlpha,
config: moonbaseAlphaConfig.getAssetDestinationConfig(
dev,
equilibriumAlphanet,
),
},
destination: {
chain: equilibriumAlphanet,
config: equilibriumAlphanetConfig.getAssetDestinationConfig(
dev,
moonbaseAlpha,
),
},
});
});
});
31 changes: 14 additions & 17 deletions packages/config/src/ConfigBuilder/ConfigBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,42 @@
/* eslint-disable sort-keys */
import { AnyChain, Asset, Ecosystem } from '@moonbeam-network/xcm-types';
import { ConfigService, IConfigService } from '../ConfigService';
import { TransferConfig } from './ConfigBuilder.interfaces';
import {
getAsset,
getAssetDestinationConfig,
getChain,
getDestinationChains,
getEcosystemAssets,
getSourceChains,
} from './ConfigBuilder.utils';

export function ConfigBuilder() {
export function ConfigBuilder(service?: IConfigService) {
const config = service ?? new ConfigService();
return {
assets: (ecosystem?: Ecosystem) => {
const assets = getEcosystemAssets(ecosystem);
const assets = config.getEcosystemAssets(ecosystem);

return {
assets,
asset: (keyOrAsset: string | Asset) => {
const asset = getAsset(keyOrAsset);
const sourceChains = getSourceChains(asset, ecosystem);
const asset = config.getAsset(keyOrAsset);
const sourceChains = config.getSourceChains(asset, ecosystem);

return {
sourceChains,
source: (keyOrChain: string | AnyChain) => {
const source = getChain(keyOrChain);
const destinationChains = getDestinationChains(asset, source);
const source = config.getChain(keyOrChain);
const destinationChains = config.getDestinationChains(
asset,
source,
);

return {
destinationChains,
destination: (
// eslint-disable-next-line @typescript-eslint/no-shadow
keyOrChain: string | AnyChain,
) => {
const destination = getChain(keyOrChain);
const sourceConfig = getAssetDestinationConfig(
const destination = config.getChain(keyOrChain);
const sourceConfig = config.getAssetDestinationConfig(
asset,
source,
destination,
);
const destinationConfig = getAssetDestinationConfig(
const destinationConfig = config.getAssetDestinationConfig(
asset,
destination,
source,
Expand Down
94 changes: 0 additions & 94 deletions packages/config/src/ConfigBuilder/ConfigBuilder.utils.test.ts

This file was deleted.

82 changes: 0 additions & 82 deletions packages/config/src/ConfigBuilder/ConfigBuilder.utils.ts

This file was deleted.

20 changes: 20 additions & 0 deletions packages/config/src/ConfigService/ConfigService.interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { AnyChain, Asset, Ecosystem } from '@moonbeam-network/xcm-types';
import { AssetConfig } from '../types/AssetConfig';

export interface IConfigService {
getEcosystemAssets(ecosystem?: Ecosystem): Asset[];

getAsset(keyOrAsset: string | Asset): Asset;

getChain(keyOrAsset: string | AnyChain): AnyChain;

getSourceChains(asset: Asset, ecosystem: Ecosystem | undefined): AnyChain[];

getDestinationChains(asset: Asset, source: AnyChain): AnyChain[];

getAssetDestinationConfig(
asset: Asset,
source: AnyChain,
destination: AnyChain,
): AssetConfig;
}
Loading

0 comments on commit bda7709

Please sign in to comment.