From ec3fa08958a2047aa2e9608f50b565794b53284c Mon Sep 17 00:00:00 2001 From: Mario J Maurello Date: Tue, 17 Dec 2024 17:31:52 +0100 Subject: [PATCH] add tests --- packages/mrl/package.json | 5 +- .../getTransferData/getMoonChainData.test.ts | 60 +++++++++++++++++++ .../src/getTransferData/getMoonChainData.ts | 37 ++++++++---- 3 files changed, 89 insertions(+), 13 deletions(-) create mode 100644 packages/mrl/src/getTransferData/getMoonChainData.test.ts diff --git a/packages/mrl/package.json b/packages/mrl/package.json index 67228005..a557220b 100644 --- a/packages/mrl/package.json +++ b/packages/mrl/package.json @@ -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", diff --git a/packages/mrl/src/getTransferData/getMoonChainData.test.ts b/packages/mrl/src/getTransferData/getMoonChainData.test.ts new file mode 100644 index 00000000..292f04a1 --- /dev/null +++ b/packages/mrl/src/getTransferData/getMoonChainData.test.ts @@ -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'); + }); + }); +}); diff --git a/packages/mrl/src/getTransferData/getMoonChainData.ts b/packages/mrl/src/getTransferData/getMoonChainData.ts index 6e11e50b..ed03836d 100644 --- a/packages/mrl/src/getTransferData/getMoonChainData.ts +++ b/packages/mrl/src/getTransferData/getMoonChainData.ts @@ -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'; @@ -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, @@ -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, });