diff --git a/src/createXcmTypes/util/getXcAssetMultiLocationByAssetId.spec.ts b/src/createXcmTypes/util/getXcAssetMultiLocationByAssetId.spec.ts index 62b6da82..89404ea0 100644 --- a/src/createXcmTypes/util/getXcAssetMultiLocationByAssetId.spec.ts +++ b/src/createXcmTypes/util/getXcAssetMultiLocationByAssetId.spec.ts @@ -13,47 +13,54 @@ const moonriverRegistry = new Registry('moonriver', {}); const moonriverApi = new AssetTransferApi(adjustedMockMoonriverParachainApi, 'moonriver', 2); describe('getXcAssetMultiLocationByAssetId', () => { - it('Should correctly return the multilocation when given a valid symbol assetId', async () => { - bifrostRegistry.currentRelayRegistry['2001'].xcAssetsData = [ + describe('Bifrost', () => { + bifrostRegistry.currentRelayRegistry['2023'].xcAssetsData = [ { - paraID: 1000, - symbol: 'RMRK', - decimals: 10, - xcmV1MultiLocation: - '{"v1":{"parents":1,"interior":{"x3":[{"parachain":1000},{"palletInstance":50},{"generalIndex":8}]}}}', - asset: { - Token: 'RMRK', - }, + paraID: 2001, + symbol: 'vBNC', + decimals: 12, + xcmV1MultiLocation: '{"v1":{"parents":1,"interior":{"x2":[{"parachain":2001},{"generalKey":"0x0101"}]}}}', + asset: '72145018963825376852137222787619937732', }, { - paraID: 1000, - symbol: 'USDT', - decimals: 6, - xcmV1MultiLocation: - '{"v1":{"parents":1,"interior":{"x3":[{"parachain":1000},{"palletInstance":50},{"generalIndex":1984}]}}}', - asset: { - Token2: '0', - }, + paraID: 2001, + symbol: 'vMOVR', + decimals: 18, + xcmV1MultiLocation: '{"v1":{"parents":1,"interior":{"x2":[{"parachain":2001},{"generalKey":"0x010a"}]}}}', + asset: '203223821023327994093278529517083736593', }, ]; - const assetId = 'USDT'; - const xcmVersion = 3; - const specName = 'bifrost'; - - const expected = - '{"v1":{"parents":1,"interior":{"x3":[{"parachain":1000},{"palletInstance":50},{"generalIndex":1984}]}}}'; - const result = await getXcAssetMultiLocationByAssetId( - bifrostApi._api, - assetId, - specName, - xcmVersion, - bifrostRegistry - ); - - expect(result).toStrictEqual(expected); + + it('Should correctly return the multilocation when given a valid symbol assetId', async () => { + const assetId = 'USDT'; + const xcmVersion = 3; + const specName = 'bifrost'; + + const expected = + '{"v1":{"parents":1,"interior":{"x3":[{"parachain":1000},{"palletInstance":50},{"generalIndex":1984}]}}}'; + const result = await getXcAssetMultiLocationByAssetId( + bifrostApi._api, + assetId, + specName, + xcmVersion, + bifrostRegistry + ); + + expect(result).toStrictEqual(expected); + }); + + it('Should correctly error when given an invalid symbol assetId', async () => { + const assetId = 'vmover'; + const xcmVersion = 2; + const specName = 'bifrost'; + + await expect(async () => { + await getXcAssetMultiLocationByAssetId(bifrostApi._api, assetId, specName, xcmVersion, bifrostRegistry); + }).rejects.toThrowError(`parachain assetId vmover is not a valid symbol assetId in bifrost`); + }); }); - it('Should correctly return the multilocation when given a valid integer assetId', async () => { + describe('Moonriver', () => { moonriverRegistry.currentRelayRegistry['2023'].xcAssetsData = [ { paraID: 2001, @@ -70,19 +77,58 @@ describe('getXcAssetMultiLocationByAssetId', () => { asset: '203223821023327994093278529517083736593', }, ]; - const assetId = '203223821023327994093278529517083736593'; - const xcmVersion = 2; - const specName = 'moonriver'; - - const expected = '{"v1":{"parents":1,"interior":{"x2":[{"parachain":2001},{"generalKey":"0x010a"}]}}}'; - const result = await getXcAssetMultiLocationByAssetId( - moonriverApi._api, - assetId, - specName, - xcmVersion, - moonriverRegistry - ); - - expect(result).toStrictEqual(expected); + it('Should correctly return the multilocation when given a valid integer assetId', async () => { + const assetId = '203223821023327994093278529517083736593'; + const xcmVersion = 2; + const specName = 'moonriver'; + + const expected = '{"v1":{"parents":1,"interior":{"x2":[{"parachain":2001},{"generalKey":"0x010a"}]}}}'; + const result = await getXcAssetMultiLocationByAssetId( + moonriverApi._api, + assetId, + specName, + xcmVersion, + moonriverRegistry + ); + + expect(result).toStrictEqual(expected); + }); + + it('Should correctly return the multilocation when given a valid symbol assetId', async () => { + const assetId = 'vbnc'; + const xcmVersion = 2; + const specName = 'moonriver'; + + const expected = '{"v1":{"parents":1,"interior":{"x2":[{"parachain":2001},{"generalKey":"0x0101"}]}}}'; + const result = await getXcAssetMultiLocationByAssetId( + moonriverApi._api, + assetId, + specName, + xcmVersion, + moonriverRegistry + ); + + expect(result).toStrictEqual(expected); + }); + + it('Should correctly error when given an invalid asset symbol', async () => { + const assetId = 'mover'; + const xcmVersion = 2; + const specName = 'moonriver'; + + await expect(async () => { + await getXcAssetMultiLocationByAssetId(moonriverApi._api, assetId, specName, xcmVersion, moonriverRegistry); + }).rejects.toThrowError(`parachain assetId mover is not a valid symbol assetIid in moonriver`); + }); + + it('Should correctly error when given an invalid integer assetId ', async () => { + const assetId = '242424332422323423424'; + const xcmVersion = 2; + const specName = 'moonriver'; + + await expect(async () => { + await getXcAssetMultiLocationByAssetId(moonriverApi._api, assetId, specName, xcmVersion, moonriverRegistry); + }).rejects.toThrowError(`assetId 242424332422323423424 is not a valid symbol or integer asset id for moonriver`); + }); }); }); diff --git a/src/createXcmTypes/util/getXcAssetMultiLocationByAssetId.ts b/src/createXcmTypes/util/getXcAssetMultiLocationByAssetId.ts index 2c833ba3..7a954f57 100644 --- a/src/createXcmTypes/util/getXcAssetMultiLocationByAssetId.ts +++ b/src/createXcmTypes/util/getXcAssetMultiLocationByAssetId.ts @@ -2,6 +2,7 @@ import { ApiPromise } from '@polkadot/api'; +import { BaseError, BaseErrorsEnum } from '../../errors'; import { Registry } from '../../registry'; import type { SanitizedXcAssetsData } from '../../registry/types'; import { validateNumber } from '../../validate'; @@ -28,7 +29,12 @@ export const getXcAssetMultiLocationByAssetId = async ( return info.xcmV1MultiLocation; } } + } else if (assetId.toLowerCase().includes('parents')) { + return assetId; } - return assetId; + throw new BaseError( + `assetId ${assetId} is not a valid symbol or integer asset id for ${specName}`, + BaseErrorsEnum.InvalidAsset + ); };