Skip to content

Commit

Permalink
adjust type logic to prevent function duplication and Type casting in…
Browse files Browse the repository at this point in the history
… Mrl Asset Routes
  • Loading branch information
mmaurello committed Jan 23, 2025
1 parent 562d7df commit c9252e4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
8 changes: 4 additions & 4 deletions packages/config/src/types/ChainRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ interface RoutesParam extends Omit<AssetRouteConstructorParams, 'source'> {
export class ChainRoutes {
readonly chain: AnyChain;

readonly #routes: Map<string, AssetRoute>;
protected routes: Map<string, AssetRoute>;

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

getRoutes(): AssetRoute[] {
return Array.from(this.#routes.values());
return Array.from(this.routes.values());
}

getAssetRoutes(keyOrAsset: string | AnyAsset): AssetRoute[] {
Expand Down Expand Up @@ -65,7 +65,7 @@ export class ChainRoutes {
): AssetRoute {
const assetKey = getKey(asset);
const destKey = getKey(destination);
const route = this.#routes.get(`${assetKey}-${destKey}`);
const route = this.routes.get(`${assetKey}-${destKey}`);

if (!route) {
throw new Error(
Expand Down
22 changes: 7 additions & 15 deletions packages/config/src/types/MrlChainRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { AnyAsset, AnyChain } from '@moonbeam-network/xcm-types';
import { getKey } from '../config.utils';
import { ChainRoutes, type ChainRoutesConstructorParams } from './ChainRoutes';
import {
MrlAssetRoute,
Expand All @@ -18,11 +17,11 @@ interface MrlRoutesParam
}

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

constructor({ chain, routes }: MrlChainRoutesConstructorParams) {
super({ chain, routes });
this.#routes = new Map(
this.routes = new Map(
routes.map(({ source, destination, contract, extrinsic, mrl }) => [
`${source.asset.key}-${destination.chain.key}`,
new MrlAssetRoute({
Expand All @@ -37,23 +36,16 @@ export class MrlChainRoutes extends ChainRoutes {
}

getRoutes(): MrlAssetRoute[] {
return Array.from(this.#routes.values());
return Array.from(this.routes.values());
}

getAssetRoute(
asset: string | AnyAsset,
destination: string | AnyChain,
): MrlAssetRoute {
const assetKey = getKey(asset);
const destKey = getKey(destination);
const route = this.#routes.get(`${assetKey}-${destKey}`);

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

return route;
const route = super.getAssetRoute(asset, destination);
// Since we know this class only stores MrlAssetRoute instances,
// we can safely cast the parent's return value
return route as MrlAssetRoute;
}
}
8 changes: 6 additions & 2 deletions packages/mrl/src/mrl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
ConfigService,
type MrlAssetRoute,
MrlAssetRoute,
mrlRoutesMap,
} from '@moonbeam-network/xcm-config';
import type {
Expand Down Expand Up @@ -44,6 +44,10 @@ export function Mrl(options?: MrlOptions) {
destination,
});

if (!(route instanceof MrlAssetRoute)) {
throw new Error('Route must be an MrlAssetRoute');
}

return {
setIsAutomatic(isAutomatic: boolean) {
return {
Expand All @@ -55,7 +59,7 @@ export function Mrl(options?: MrlOptions) {
destinationAddress: string;
}) {
return getTransferData({
route: route as MrlAssetRoute,
route,
sourceAddress,
destinationAddress,
isAutomatic,
Expand Down

0 comments on commit c9252e4

Please sign in to comment.