Skip to content

Commit bda7709

Browse files
authored
Added ConfigService, support mutability of static xcm configs (#124)
1 parent 3bf2a78 commit bda7709

File tree

12 files changed

+408
-197
lines changed

12 files changed

+408
-197
lines changed

.changeset/gold-tips-travel.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@moonbeam-network/xcm-config': minor
3+
'@moonbeam-network/xcm-sdk': minor
4+
---
5+
6+
Introduce config service to support mutability of static xcm config

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,6 @@ dist
111111

112112
# mac
113113
.DS_Store
114+
115+
# IntelliJ Idea
116+
.idea

packages/config/src/ConfigBuilder/ConfigBuilder.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable sort-keys */
22
import { Ecosystem } from '@moonbeam-network/xcm-types';
3+
import { ConfigService } from '../ConfigService';
34
import { dev } from '../assets';
45
import { equilibriumAlphanet, moonbaseAlpha } from '../chains';
56
import { equilibriumAlphanetConfig } from '../configs/equilibriumAlphanet';
@@ -33,4 +34,32 @@ describe('configBuilder', () => {
3334
},
3435
});
3536
});
37+
38+
it('should return correct dev config using mutable service', () => {
39+
const configService = new ConfigService();
40+
const config = ConfigBuilder(configService)
41+
.assets(Ecosystem.AlphanetRelay)
42+
.asset(dev)
43+
.source(moonbaseAlpha)
44+
.destination(equilibriumAlphanet)
45+
.build();
46+
47+
expect(config).toStrictEqual({
48+
asset: dev,
49+
source: {
50+
chain: moonbaseAlpha,
51+
config: moonbaseAlphaConfig.getAssetDestinationConfig(
52+
dev,
53+
equilibriumAlphanet,
54+
),
55+
},
56+
destination: {
57+
chain: equilibriumAlphanet,
58+
config: equilibriumAlphanetConfig.getAssetDestinationConfig(
59+
dev,
60+
moonbaseAlpha,
61+
),
62+
},
63+
});
64+
});
3665
});

packages/config/src/ConfigBuilder/ConfigBuilder.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,42 @@
11
/* eslint-disable sort-keys */
22
import { AnyChain, Asset, Ecosystem } from '@moonbeam-network/xcm-types';
3+
import { ConfigService, IConfigService } from '../ConfigService';
34
import { TransferConfig } from './ConfigBuilder.interfaces';
4-
import {
5-
getAsset,
6-
getAssetDestinationConfig,
7-
getChain,
8-
getDestinationChains,
9-
getEcosystemAssets,
10-
getSourceChains,
11-
} from './ConfigBuilder.utils';
125

13-
export function ConfigBuilder() {
6+
export function ConfigBuilder(service?: IConfigService) {
7+
const config = service ?? new ConfigService();
148
return {
159
assets: (ecosystem?: Ecosystem) => {
16-
const assets = getEcosystemAssets(ecosystem);
10+
const assets = config.getEcosystemAssets(ecosystem);
1711

1812
return {
1913
assets,
2014
asset: (keyOrAsset: string | Asset) => {
21-
const asset = getAsset(keyOrAsset);
22-
const sourceChains = getSourceChains(asset, ecosystem);
15+
const asset = config.getAsset(keyOrAsset);
16+
const sourceChains = config.getSourceChains(asset, ecosystem);
2317

2418
return {
2519
sourceChains,
2620
source: (keyOrChain: string | AnyChain) => {
27-
const source = getChain(keyOrChain);
28-
const destinationChains = getDestinationChains(asset, source);
21+
const source = config.getChain(keyOrChain);
22+
const destinationChains = config.getDestinationChains(
23+
asset,
24+
source,
25+
);
2926

3027
return {
3128
destinationChains,
3229
destination: (
3330
// eslint-disable-next-line @typescript-eslint/no-shadow
3431
keyOrChain: string | AnyChain,
3532
) => {
36-
const destination = getChain(keyOrChain);
37-
const sourceConfig = getAssetDestinationConfig(
33+
const destination = config.getChain(keyOrChain);
34+
const sourceConfig = config.getAssetDestinationConfig(
3835
asset,
3936
source,
4037
destination,
4138
);
42-
const destinationConfig = getAssetDestinationConfig(
39+
const destinationConfig = config.getAssetDestinationConfig(
4340
asset,
4441
destination,
4542
source,

packages/config/src/ConfigBuilder/ConfigBuilder.utils.test.ts

Lines changed: 0 additions & 94 deletions
This file was deleted.

packages/config/src/ConfigBuilder/ConfigBuilder.utils.ts

Lines changed: 0 additions & 82 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { AnyChain, Asset, Ecosystem } from '@moonbeam-network/xcm-types';
2+
import { AssetConfig } from '../types/AssetConfig';
3+
4+
export interface IConfigService {
5+
getEcosystemAssets(ecosystem?: Ecosystem): Asset[];
6+
7+
getAsset(keyOrAsset: string | Asset): Asset;
8+
9+
getChain(keyOrAsset: string | AnyChain): AnyChain;
10+
11+
getSourceChains(asset: Asset, ecosystem: Ecosystem | undefined): AnyChain[];
12+
13+
getDestinationChains(asset: Asset, source: AnyChain): AnyChain[];
14+
15+
getAssetDestinationConfig(
16+
asset: Asset,
17+
source: AnyChain,
18+
destination: AnyChain,
19+
): AssetConfig;
20+
}

0 commit comments

Comments
 (0)