Skip to content

Commit

Permalink
Using assert instead of if/throw
Browse files Browse the repository at this point in the history
  • Loading branch information
ekenigs committed Aug 8, 2024
1 parent 335837a commit 128946b
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 36 deletions.
15 changes: 15 additions & 0 deletions packages/builder/fixtures/builderParamsMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
};
1 change: 1 addition & 0 deletions packages/builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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',
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
27 changes: 10 additions & 17 deletions packages/sdk/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -52,23 +53,15 @@ export function Sdk({ configService, ecosystem }: SdkOptions = {}) {
): Promise<TransferData> {
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,
Expand Down
64 changes: 64 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 128946b

Please sign in to comment.