Skip to content

Commit

Permalink
Getting moon chain data
Browse files Browse the repository at this point in the history
  • Loading branch information
ekenigs committed Sep 12, 2024
1 parent 1c32e11 commit e5b530d
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 39 deletions.
28 changes: 0 additions & 28 deletions .eslintrc.json

This file was deleted.

5 changes: 0 additions & 5 deletions .prettierrc.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export function polkadotXcm() {
);

const { address20 } = getMultilocationDerivedAddresses({
address: destinationAddress,
address: sourceAddress,
paraId: moonChain.parachainId,
isParents: true,
});
Expand Down
10 changes: 5 additions & 5 deletions packages/config/src/types/AssetRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ export interface FeeConfig {
export interface MrlConfig {
isAutomatic: boolean;
transfer: MrlConfigBuilder;
moonChainFee: {
asset: Asset;
amount: number | FeeConfigBuilder;
balance: BalanceConfigBuilder;
};
moonChainFee: MoonChainFeeConfig;
}

export interface DestinationFeeConfig
extends SetOptional<FeeConfig, 'balance'> {
amount: number | FeeConfigBuilder;
}

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

export class AssetRoute {
readonly asset: Asset;

Expand Down
69 changes: 69 additions & 0 deletions packages/mrl/src/getTransferData/getMoonChainData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { type AssetRoute, 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';
import type {
DestinationTransferData,
MoonChainTransferData,
} from '../mrl.interfaces';

export interface GetMoonChainDataParams {
destinationData: DestinationTransferData;
route: AssetRoute;
sourceAddress: string;
}

export async function getMoonChainData({
destinationData,
route,
sourceAddress,
}: GetMoonChainDataParams): Promise<MoonChainTransferData> {
if (!route.mrl) {
throw new Error(
`MRL config is not defined for source chain ${route.source.chain.name} and asset ${route.asset.originSymbol}`,
);
}

const moonChain = getMoonChain(route.source.chain);
const asset = moonChain.getChainAsset(route.asset);
const isDestinationMoonChain = route.destination.chain.isEqual(moonChain);

if (isDestinationMoonChain) {
return {
balance: destinationData.balance,
chain: destinationData.chain,
fee: destinationData.fee,
};
}

const fee = await getDestinationFee({
asset,
chain: moonChain,
fee: route.mrl.moonChainFee.amount,
});

let address = sourceAddress;

if (Parachain.is(route.source.chain)) {
const { address20 } = getMultilocationDerivedAddresses({
address: sourceAddress,
paraId: moonChain.parachainId,
isParents: true,
});

address = address20;
}

const balance = await getBalance({
address,
asset,
builder: route.mrl.moonChainFee.balance,
chain: moonChain,
});

return {
balance,
chain: moonChain,
fee,
};
}
8 changes: 8 additions & 0 deletions packages/mrl/src/getTransferData/getTransferData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { toBigInt } from '@moonbeam-network/xcm-utils';
import Big from 'big.js';
import type { TransferData } from '../mrl.interfaces';
import { WormholeService } from '../services/wormhole';
import { getMoonChainData } from './getMoonChainData';
import { getSourceData } from './getSourceData';
import { buildTransfer } from './getTransferData.utils';

Expand Down Expand Up @@ -59,6 +60,12 @@ export async function getTransferData({
sourceAddress,
});

const moonChainData = await getMoonChainData({
destinationData,
route,
sourceAddress,
});

return {
destination: destinationData,
getEstimate(amount: number | string) {
Expand All @@ -77,6 +84,7 @@ export async function getTransferData({
},
max: sourceData.max,
min: getMin(destinationData),
moonChain: moonChainData,
source: sourceData,
async transfer(
amount,
Expand Down
3 changes: 3 additions & 0 deletions packages/mrl/src/mrl.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface TransferData {
getEstimate(amount: number | string): AssetAmount;
max: AssetAmount;
min: AssetAmount;
moonChain: MoonChainTransferData;
source: SourceTransferData;
transfer(
amount: bigint | number | string,
Expand All @@ -29,6 +30,8 @@ export interface SourceTransferData extends ChainTransferData {

export interface DestinationTransferData extends ChainTransferData {}

export type MoonChainTransferData = Omit<ChainTransferData, 'min'>;

export interface ChainTransferData {
chain: AnyChain;
balance: AssetAmount;
Expand Down

0 comments on commit e5b530d

Please sign in to comment.