diff --git a/packages/builder/fixtures/builderParamsMock.ts b/packages/builder/fixtures/builderParamsMock.ts index 604eeba3..4ded0abb 100644 --- a/packages/builder/fixtures/builderParamsMock.ts +++ b/packages/builder/fixtures/builderParamsMock.ts @@ -133,3 +133,18 @@ export const mrlBuildParamsMock: MrlExtrinsicConfigBuilderPrams = { }, }, }; + +export const mrlBuildParamsMock2: MrlExtrinsicConfigBuilderPrams = { + ...buildParachainParamsMock, + moonApi: {} as any, + moonAsset: testAssetAmount, + moonChain: moonbaseAlphaMock, + moonGasLimit: 999_999n, + transact: { + call: '0x4d79207465787420737472696e67', + txWeight: { + refTime: 24_902_375_000n, + proofSize: 62_193n, + }, + }, +}; diff --git a/packages/builder/package.json b/packages/builder/package.json index 4876212d..5a8bb7a2 100644 --- a/packages/builder/package.json +++ b/packages/builder/package.json @@ -39,6 +39,7 @@ "dependencies": { "@moonbeam-network/xcm-types": "2.3.1", "@moonbeam-network/xcm-utils": "2.1.4", + "assert": "^2.1.0", "big.js": "^6.2.1" }, "peerDependencies": { diff --git a/packages/builder/src/extrinsic/mrl/wormhole/ethereumXcm/ethereumXcm.test.ts b/packages/builder/src/extrinsic/mrl/wormhole/ethereumXcm/ethereumXcm.test.ts index a5331013..b71000e5 100644 --- a/packages/builder/src/extrinsic/mrl/wormhole/ethereumXcm/ethereumXcm.test.ts +++ b/packages/builder/src/extrinsic/mrl/wormhole/ethereumXcm/ethereumXcm.test.ts @@ -3,7 +3,10 @@ import { describe, expect, it } from 'vitest'; import { ethereumXcm } from './ethereumXcm'; -import { mrlBuildParamsMock } from '../../../../../fixtures'; +import { + mrlBuildParamsMock, + mrlBuildParamsMock2, +} from '../../../../../fixtures'; describe('ethereumXcm', () => { describe('transact', () => { @@ -18,5 +21,16 @@ describe('ethereumXcm', () => { it('should get correct arguments', () => { expect(extrinsic.getArgs({} as any)).toMatchSnapshot(); }); + + it('should throw and error because destination has no wh name', () => { + expect(() => + ethereumXcm() + .transact({ isAutomatic: true }) + .build(mrlBuildParamsMock2) + .getArgs({} as any), + ).toThrow( + 'Destination chain Interlay Testnet does not have a wormhole name', + ); + }); }); }); diff --git a/packages/builder/src/extrinsic/mrl/wormhole/ethereumXcm/ethereumXcm.ts b/packages/builder/src/extrinsic/mrl/wormhole/ethereumXcm/ethereumXcm.ts index b27923bd..dfd4ac71 100644 --- a/packages/builder/src/extrinsic/mrl/wormhole/ethereumXcm/ethereumXcm.ts +++ b/packages/builder/src/extrinsic/mrl/wormhole/ethereumXcm/ethereumXcm.ts @@ -2,6 +2,7 @@ import { convertAddressTo32Bytes } from '@moonbeam-network/xcm-utils'; import { Address, encodeFunctionData } from 'viem'; import { Wormhole } from '@wormhole-foundation/sdk-connect'; import { EvmPlatform } from '@wormhole-foundation/sdk-evm'; +import assert from 'assert'; import { MrlExtrinsicConfigBuilder } from '../../../ExtrinsicBuilder.interfaces'; import { ExtrinsicConfig } from '../../../ExtrinsicConfig'; import { BATCH_CONTRACT_ABI } from './BatchContractAbi'; @@ -26,11 +27,10 @@ export function ethereumXcm() { moonChain, moonGasLimit, }) => { - if (!destination.wh?.name) { - throw new Error( - `Destination chain ${destination.name} does not have a wormhole name`, - ); - } + assert( + destination.wh?.name, + `Destination chain ${destination.name} does not have a wormhole name`, + ); const wh = new Wormhole(moonChain.isTestChain ? 'Testnet' : 'Mainnet', [ EvmPlatform, @@ -46,11 +46,10 @@ export function ethereumXcm() { const tokenAddressOnMoonChain = moonChain.getChainAsset(asset) .address as Address | undefined; - if (!tokenAddressOnMoonChain) { - throw new Error( - `Asset ${asset.symbol} does not have a token address on chain ${moonChain.name}`, - ); - } + assert( + tokenAddressOnMoonChain, + `Asset ${asset.symbol} does not have a token address on chain ${moonChain.name}`, + ); const destinationAddress32bytes = convertAddressTo32Bytes( destinationAddress, diff --git a/packages/builder/src/extrinsic/mrl/wormhole/polkadotXcm/polkadotXcm.ts b/packages/builder/src/extrinsic/mrl/wormhole/polkadotXcm/polkadotXcm.ts index bc042844..771d479b 100644 --- a/packages/builder/src/extrinsic/mrl/wormhole/polkadotXcm/polkadotXcm.ts +++ b/packages/builder/src/extrinsic/mrl/wormhole/polkadotXcm/polkadotXcm.ts @@ -1,5 +1,6 @@ import { getMultilocationDerivedAddresses } from '@moonbeam-network/xcm-utils'; import { AssetAmount } from '@moonbeam-network/xcm-types'; +import assert from 'assert'; import { ExtrinsicBuilder } from '../../../ExtrinsicBuilder'; import { MrlExtrinsicConfigBuilder } from '../../../ExtrinsicBuilder.interfaces'; import { ExtrinsicConfig } from '../../../ExtrinsicConfig'; @@ -22,14 +23,11 @@ export function polkadotXcm() { sourceApi, transact, } = params; - - if (!destination.wh?.name) { - throw new Error('Destination chain does not have a wormhole name'); - } - - if (!transact) { - throw new Error('Transact params are required'); - } + assert(transact, 'Transact param is required'); + assert( + destination.wh?.name, + `Destination chain ${destination.name} does not have a wormhole name`, + ); const { transfer } = sourceApi.tx.xTokens; const builder = ExtrinsicBuilder().xTokens().transfer(); diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 9cfdbb1f..0c15825b 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -43,6 +43,7 @@ "@moonbeam-network/xcm-config": "2.5.3", "@moonbeam-network/xcm-types": "2.3.1", "@moonbeam-network/xcm-utils": "2.1.4", + "assert": "^2.1.0", "big.js": "^6.2.1" }, "peerDependencies": { diff --git a/packages/sdk/src/sdk.ts b/packages/sdk/src/sdk.ts index 73ae14ea..19607039 100644 --- a/packages/sdk/src/sdk.ts +++ b/packages/sdk/src/sdk.ts @@ -7,6 +7,7 @@ import { EvmParachain, Parachain, } from '@moonbeam-network/xcm-types'; +import assert from 'assert'; import { getAssetsBalances } from './getTransferData/getSourceData'; import { getTransferData } from './getTransferData/getTransferData'; import { PolkadotService } from './polkadot'; @@ -52,23 +53,15 @@ export function Sdk({ configService, ecosystem }: SdkOptions = {}) { ): Promise { const sourceChain = service.getChain(source); - if ( - !Parachain.is(sourceChain) && - !EvmParachain.is(sourceChain) - ) { - throw new Error( - `Source chain should be a Parachain or EvmParachain`, - ); - } - - if ( - !Parachain.is(route.destination) && - !EvmParachain.is(route.destination) - ) { - throw new Error( - `Destination chain should be a Parachain or EvmParachain`, - ); - } + assert( + Parachain.is(sourceChain) || EvmParachain.is(sourceChain), + 'Source chain should be a Parachain or EvmParachain', + ); + assert( + Parachain.is(route.destination) || + EvmParachain.is(route.destination), + 'Destination chain should be a Parachain or EvmParachain', + ); return getTransferData({ route, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 40bf32b9..d28c8bf6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -151,6 +151,9 @@ importers: '@wormhole-foundation/sdk-evm': specifier: ^0.8.2 version: 0.8.2(bufferutil@4.0.8)(utf-8-validate@5.0.10) + assert: + specifier: ^2.1.0 + version: 2.1.0 big.js: specifier: ^6.2.1 version: 6.2.1 @@ -214,6 +217,9 @@ importers: '@polkadot/util': specifier: ^13.0.2 version: 13.0.2 + assert: + specifier: ^2.1.0 + version: 2.1.0 big.js: specifier: ^6.2.1 version: 6.2.1 @@ -2282,6 +2288,9 @@ packages: resolution: {integrity: sha512-bMxMKAjg13EBSVscxTaYA4mRc5t1UAXa2kXiGTNfZ079HIWXEkKmkgFrh/nJqamaLSrXO5H4WFFkPEaLJWbs3A==} engines: {node: '>= 0.4'} + assert@2.1.0: + resolution: {integrity: sha512-eLHpSK/Y4nhMJ07gDaAzoX/XAKS8PSaojml3M0DM4JpV1LAi5JOJ/p6H/XWrl8L+DzVEvVCW1z3vWAaB9oTsQw==} + assertion-error@2.0.1: resolution: {integrity: sha512-Izi8RQcffqCeNVgFigKli1ssklIbpHnCYc6AknXGYoB6grJqyeby7jv12JUQgmTAnIDnbck1uxksT4dzN3PWBA==} engines: {node: '>=12'} @@ -2347,6 +2356,7 @@ packages: bun@1.1.21: resolution: {integrity: sha512-mvqYEvafGskIVTjlftbKvsXtyR6z/SQnhJsVw0xCU46pc56oX1sAGvaemWKOy/sy/gGMHcgLE0KUidDQQzqXWQ==} + cpu: [arm64, x64] os: [darwin, linux, win32] hasBin: true @@ -3161,6 +3171,10 @@ packages: resolution: {integrity: sha512-B9ZWJxHHOHUhUjCPrMpLD4xEq35bUTClHM1S6CBU5ixQnkZmwipwgc96vAd7AAGM9TGHvJR+Uss+/Ak6UphK+Q==} engines: {node: '>=8'} + is-arguments@1.1.1: + resolution: {integrity: sha512-8Q7EARjzEnKpt/PCD7e1cgUS0a6X8u5tdSiMqXhojOdoV9TsMsiO+9VLC5vAmO8N7/GmXn7yjR8qnA6bVAEzfA==} + engines: {node: '>= 0.4'} + is-array-buffer@3.0.4: resolution: {integrity: sha512-wcjaerHw0ydZwfhiKbXJWLDY8A7yV7KhjQOpb83hGgGfId/aQa4TOvwyzn2PuswW2gPCYEL/nEAiSVpdOj1lXw==} engines: {node: '>= 0.4'} @@ -3200,10 +3214,18 @@ packages: resolution: {integrity: sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==} engines: {node: '>=8'} + is-generator-function@1.0.10: + resolution: {integrity: sha512-jsEjy9l3yiXEQ+PsXdmBwEPcOxaXWLspKdplFUVI9vq1iZgIekeC0L167qeu86czQaxed3q/Uzuw0swL0irL8A==} + engines: {node: '>= 0.4'} + is-glob@4.0.3: resolution: {integrity: sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==} engines: {node: '>=0.10.0'} + is-nan@1.3.2: + resolution: {integrity: sha512-E+zBKpQ2t6MEo1VsonYmluk9NxGrbzpeeLC2xIViuO2EjU2xsXsBPwTr3Ykv9l08UYEVEdWeRZNouaZqF6RN0w==} + engines: {node: '>= 0.4'} + is-negative-zero@2.0.3: resolution: {integrity: sha512-5KoIu2Ngpyek75jXodFvnafB6DJgr3u8uuK0LEZJjrU19DrMD3EVERaR8sjz8CCGgpZvxPl9SuE1GMVPFHx1mw==} engines: {node: '>= 0.4'} @@ -3561,6 +3583,10 @@ packages: resolution: {integrity: sha512-IRZSRuzJiynemAXPYtPe5BoI/RESNYR7TYm50MC5Mqbd3Jmw5y790sErYw3V6SryFJD64b74qQQs9wn5Bg/k3g==} engines: {node: '>= 0.4'} + object-is@1.1.6: + resolution: {integrity: sha512-F8cZ+KfGlSGi09lJT7/Nd6KJZ9ygtvYC0/UYYLI9nmQKLMnydpB9yvbv9K1uSkEu7FU9vYPmVwLg328tX+ot3Q==} + engines: {node: '>= 0.4'} + object-keys@1.1.1: resolution: {integrity: sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==} engines: {node: '>= 0.4'} @@ -4247,6 +4273,9 @@ packages: util-deprecate@1.0.2: resolution: {integrity: sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==} + util@0.12.5: + resolution: {integrity: sha512-kZf/K6hEIrWHI6XqOFUiiMa+79wE/D8Q+NCNAWclkyg3b4d2k7s0QGepNjiABc+aR3N1PAyHL7p6UcLY6LmrnA==} + uuid@9.0.1: resolution: {integrity: sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==} hasBin: true @@ -7417,6 +7446,14 @@ snapshots: is-array-buffer: 3.0.4 is-shared-array-buffer: 1.0.3 + assert@2.1.0: + dependencies: + call-bind: 1.0.7 + is-nan: 1.3.2 + object-is: 1.1.6 + object.assign: 4.1.5 + util: 0.12.5 + assertion-error@2.0.1: {} asynckit@0.4.0: {} @@ -8529,6 +8566,11 @@ snapshots: ip-regex@4.3.0: {} + is-arguments@1.1.1: + dependencies: + call-bind: 1.0.7 + has-tostringtag: 1.0.2 + is-array-buffer@3.0.4: dependencies: call-bind: 1.0.7 @@ -8565,10 +8607,19 @@ snapshots: is-fullwidth-code-point@3.0.0: {} + is-generator-function@1.0.10: + dependencies: + has-tostringtag: 1.0.2 + is-glob@4.0.3: dependencies: is-extglob: 2.1.1 + is-nan@1.3.2: + dependencies: + call-bind: 1.0.7 + define-properties: 1.2.1 + is-negative-zero@2.0.3: {} is-number-object@1.0.7: @@ -8877,6 +8928,11 @@ snapshots: object-inspect@1.13.2: {} + object-is@1.1.6: + dependencies: + call-bind: 1.0.7 + define-properties: 1.2.1 + object-keys@1.1.1: {} object.assign@4.1.5: @@ -9565,6 +9621,14 @@ snapshots: util-deprecate@1.0.2: {} + util@0.12.5: + dependencies: + inherits: 2.0.4 + is-arguments: 1.1.1 + is-generator-function: 1.0.10 + is-typed-array: 1.1.13 + which-typed-array: 1.1.15 + uuid@9.0.1: {} viem@2.18.7(bufferutil@4.0.8)(typescript@5.5.4)(utf-8-validate@5.0.10):