Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaurello committed Dec 17, 2024
1 parent d3fc046 commit ec3fa08
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 13 deletions.
5 changes: 4 additions & 1 deletion packages/mrl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"build": "tsup",
"dev": "tsup --watch",
"link": "pnpm ln --global",
"typecheck": "tsc --noEmit"
"typecheck": "tsc --noEmit",
"test": "vitest --run",
"test:watch": "vitest",
"test:update": "vitest -u"
},
"repository": {
"directory": "packages/mrl",
Expand Down
60 changes: 60 additions & 0 deletions packages/mrl/src/getTransferData/getMoonChainData.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { describe, expect, it } from 'vitest';

import {
fantomTestnet,
moonbaseAlpha,
peaqAlphanet,
} from '@moonbeam-network/xcm-config';
import { getMoonChainAddress } from './getMoonChainData';

describe('mrl - getMoonChainData', () => {
describe('getMoonChainAddress', () => {
it('should return the correct moonchain address for a parachain to evm transaction', () => {
const params = {
source: peaqAlphanet,
destination: fantomTestnet,
sourceAddress: '5GWpSdqkkKGZmdKQ9nkSF7TmHp6JWt28BMGQNuG4MXtSvq3e',
destinationAddress: '0x08480769599E23F626efff39B89F3137e9917a40',
};

const result = getMoonChainAddress(params);
expect(result).toBe('0xa18b59fcd9d8a76c3cb16dc6dc42296ebb66a57a'); // computed origin account
});

it('should return the correct moonchain address for a evm to parachain transaction', () => {
const params = {
source: fantomTestnet,
destination: peaqAlphanet,
sourceAddress: '0x08480769599E23F626efff39B89F3137e9917a40',
destinationAddress: '5GWpSdqkkKGZmdKQ9nkSF7TmHp6JWt28BMGQNuG4MXtSvq3e',
};

const result = getMoonChainAddress(params);
expect(result).toBe('0x08480769599E23F626efff39B89F3137e9917a40');
});

it('should return the source chain address when source is a moonchain', () => {
const params = {
source: moonbaseAlpha,
destination: fantomTestnet,
sourceAddress: '0x08480769599E23F626efff39B89F3137e9917a40',
destinationAddress: '0x08480769599E23F626efff39B89F3137e9917a40',
};

const result = getMoonChainAddress(params);
expect(result).toBe('0x08480769599E23F626efff39B89F3137e9917a40');
});

it('should return the destination chain address when destination is a moonchain', () => {
const params = {
source: fantomTestnet,
destination: moonbaseAlpha,
sourceAddress: '0x08480769599E23F626efff39B89F3137e9917a40',
destinationAddress: '0x08480769599E23F626efff39B89F3137e9917a40',
};

const result = getMoonChainAddress(params);
expect(result).toBe('0x08480769599E23F626efff39B89F3137e9917a40');
});
});
});
37 changes: 25 additions & 12 deletions packages/mrl/src/getTransferData/getMoonChainData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { type MrlAssetRoute, getMoonChain } from '@moonbeam-network/xcm-config';
import { getBalance, getDestinationFee } from '@moonbeam-network/xcm-sdk';
import { EvmParachain, Parachain } from '@moonbeam-network/xcm-types';
import {
type AnyChain,
EvmParachain,
Parachain,
} from '@moonbeam-network/xcm-types';
import { getMultilocationDerivedAddresses } from '@moonbeam-network/xcm-utils';
import { evmToAddress } from '@polkadot/util-crypto';
import type { MoonChainTransferData } from '../mrl.interfaces';
Expand All @@ -24,11 +28,13 @@ export async function getMoonChainData({

const moonChain = getMoonChain(route.source.chain);
const moonChainAddress = getMoonChainAddress({
route,
source: route.source.chain,
destination: route.destination.chain,
sourceAddress,
destinationAddress,
});

console.log('sourceAddress', sourceAddress);
const fee = await getDestinationFee({
address: moonChainAddress,
asset: route.source.asset,
Expand Down Expand Up @@ -60,30 +66,37 @@ export async function getMoonChainData({
};
}

function getMoonChainAddress({
route: { source, destination },
interface GetMoonChainAddressParams {
source: AnyChain;
destination: AnyChain;
sourceAddress: string;
destinationAddress: string;
}

export function getMoonChainAddress({
source,
destination,
sourceAddress,
destinationAddress,
}: GetMoonChainDataParams): string {
const moonChain = getMoonChain(source.chain);
const isDestinationMoonChain = moonChain.isEqual(destination.chain);
const isSourceMoonChain = moonChain.isEqual(source.chain);
}: GetMoonChainAddressParams): string {
const moonChain = getMoonChain(source);
const isDestinationMoonChain = moonChain.isEqual(destination);
const isSourceMoonChain = moonChain.isEqual(source);

let moonChainAddress = isDestinationMoonChain
? destinationAddress
: sourceAddress;

// for Parachain to EVM transactions, we use the computed origin account in the moonchain
if (Parachain.is(source.chain) && !isSourceMoonChain) {
const isSourceEvmSigner =
EvmParachain.is(source.chain) && source.chain.isEvmSigner;
if (Parachain.is(source) && !isSourceMoonChain) {
const isSourceEvmSigner = EvmParachain.is(source) && source.isEvmSigner;

const { address20: computedOriginAccount } =
getMultilocationDerivedAddresses({
address: isSourceEvmSigner
? evmToAddress(sourceAddress)
: sourceAddress,
paraId: source.chain.parachainId,
paraId: source.parachainId,
isParents: true,
});

Expand Down

0 comments on commit ec3fa08

Please sign in to comment.