Skip to content

Commit

Permalink
fix: adjust fetchPalletInstanceId to handle ForeignAssets pallet (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
TarikGul authored Sep 1, 2023
1 parent f1c2970 commit 401ab7d
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 87 deletions.
2 changes: 0 additions & 2 deletions examples/systemToParaLpToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ const main = async () => {
assetsInfo: {},
foreignAssetsInfo: {},
specName: 'testing',
assetsPalletInstance: '100',
foreignAssetsPalletInstance: '1000',
poolPairsInfo: {},
},
},
Expand Down
2 changes: 0 additions & 2 deletions src/AssetsTransferApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,6 @@ describe('AssetTransferAPI', () => {
assetsInfo: {},
foreignAssetsInfo: {},
specName: 'testing',
assetsPalletInstance: '100',
foreignAssetsPalletInstance: '1000',
poolPairsInfo: {},
},
},
Expand Down
10 changes: 3 additions & 7 deletions src/createXcmTypes/ParaToSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type {
} from '@polkadot/types/interfaces';
import type { XcmV3MultiassetMultiAssets } from '@polkadot/types/lookup';

import { ASSET_HUB_CHAIN_ID } from '../consts';
import { BaseError, BaseErrorsEnum } from '../errors';
import { Registry } from '../registry';
import { XCMChainInfoDataKeys, XCMChainInfoKeys } from '../registry/types';
Expand All @@ -35,6 +34,7 @@ import type {
} from './types';
import { constructForeignAssetMultiLocationFromAssetId } from './util/constructForeignAssetMultiLocationFromAssetId';
import { dedupeMultiAssets } from './util/dedupeMultiAssets';
import { fetchPalletInstanceId } from './util/fetchPalletInstanceId';
import { getAssetId } from './util/getAssetId';
import { isParachainPrimaryNativeAsset } from './util/isParachainPrimaryNativeAsset';
import { isRelayNativeAsset } from './util/isRelayNativeAsset';
Expand Down Expand Up @@ -506,11 +506,7 @@ const createParaToSystemMultiAssets = async (
): Promise<MultiAsset[]> => {
const { xcAssets } = registry;
const currentRelayChainSpecName = registry.relayChain;
const { foreignAssetsPalletInstance } =
registry.currentRelayRegistry[ASSET_HUB_CHAIN_ID];
// This will always result in a value and will never be null because the AssetHub will always
// have the foreign assets pallet present, so we type cast here to work around the type compiler.
const foreignAssetsPalletId = foreignAssetsPalletInstance as string;
const palletId = fetchPalletInstanceId(api, false, isForeignAssetsTransfer);
let multiAssets: MultiAsset[] = [];
let concreteMultiLocation: MultiLocation;
const isPrimaryParachainNativeAsset = isParachainPrimaryNativeAsset(
Expand Down Expand Up @@ -585,7 +581,7 @@ const createParaToSystemMultiAssets = async (
concreteMultiLocation = constructForeignAssetMultiLocationFromAssetId(
api,
assetId,
foreignAssetsPalletId
palletId
);
} else {
concreteMultiLocation = api.registry.createType(
Expand Down
13 changes: 6 additions & 7 deletions src/createXcmTypes/SystemToPara.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,12 @@ export const createSystemToParaMultiAssets = async (
isForeignAssetsTransfer: boolean,
isLiquidTokenTransfer: boolean
): Promise<MultiAsset[]> => {
const assetHubChainId = '1000';
const { foreignAssetsPalletInstance } =
registry.currentRelayRegistry[assetHubChainId];
const foreignAssetsPalletId = foreignAssetsPalletInstance as string;
const palletId = fetchPalletInstanceId(api, isLiquidTokenTransfer);
let multiAssets: MultiAsset[] = [];

const palletId = fetchPalletInstanceId(
api,
isLiquidTokenTransfer,
isForeignAssetsTransfer
);
const systemChainId = getChainIdBySpecName(registry, specName);

if (!isSystemChain(systemChainId)) {
Expand Down Expand Up @@ -317,7 +316,7 @@ export const createSystemToParaMultiAssets = async (
concreteMultiLocation = constructForeignAssetMultiLocationFromAssetId(
api,
assetId,
foreignAssetsPalletId
palletId
);
} else {
const parents = isRelayNative ? 1 : 0;
Expand Down
22 changes: 7 additions & 15 deletions src/createXcmTypes/SystemToSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type {
} from '@polkadot/types/interfaces';
import type { XcmV3MultiassetMultiAssets } from '@polkadot/types/lookup';

import { ASSET_HUB_CHAIN_ID } from '../consts';
import { getChainIdBySpecName } from '../createXcmTypes/util/getChainIdBySpecName';
import { BaseError, BaseErrorsEnum } from '../errors';
import type { Registry } from '../registry';
Expand Down Expand Up @@ -271,11 +270,12 @@ export const createSystemToSystemMultiAssets = async (
isLiquidTokenTransfer: boolean
): Promise<MultiAsset[]> => {
let multiAssets: MultiAsset[] = [];

const { foreignAssetsPalletInstance } =
registry.currentRelayRegistry[ASSET_HUB_CHAIN_ID];
const foreignAssetsPalletId = foreignAssetsPalletInstance as string;
const systemChainId = getChainIdBySpecName(registry, specName);
const palletId = fetchPalletInstanceId(
api,
isLiquidTokenTransfer,
isForeignAssetsTransfer
);

if (!isSystemChain(systemChainId)) {
throw new BaseError(
Expand Down Expand Up @@ -325,7 +325,7 @@ export const createSystemToSystemMultiAssets = async (
const junctionCount = junctions.split('},').length + 1;

const numberOfJunctions = `"X${junctionCount}"`;
const palletInstanceJunctionStr = `{"PalletInstance":"${foreignAssetsPalletId}"},`;
const palletInstanceJunctionStr = `{"PalletInstance":"${palletId}"},`;
const interiorMultiLocationStr = `{${numberOfJunctions}:[${palletInstanceJunctionStr}${junctions}]}`;

concreteMultiLocation = api.registry.createType('MultiLocation', {
Expand All @@ -340,15 +340,7 @@ export const createSystemToSystemMultiAssets = async (
const interior: InteriorMultiLocation = isRelayNative
? api.registry.createType('InteriorMultiLocation', { Here: '' })
: api.registry.createType('InteriorMultiLocation', {
X2: [
{
PalletInstance: fetchPalletInstanceId(
api,
isLiquidTokenTransfer
),
},
{ GeneralIndex: assetId },
],
X2: [{ PalletInstance: palletId }, { GeneralIndex: assetId }],
});
concreteMultiLocation = api.registry.createType('MultiLocation', {
parents,
Expand Down
18 changes: 15 additions & 3 deletions src/createXcmTypes/util/fetchPalletInstanceId.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { fetchPalletInstanceId } from './fetchPalletInstanceId';

describe('fetchPalletInstanceId', () => {
it('Should return the correct string when the api has the assets pallet', () => {
const res = fetchPalletInstanceId(mockSystemApi, false);
const res = fetchPalletInstanceId(mockSystemApi, false, false);

expect(res).toEqual('50');
});
Expand All @@ -19,15 +19,27 @@ describe('fetchPalletInstanceId', () => {
},
},
} as unknown as ApiPromise;
const res = () => fetchPalletInstanceId(mockApi, false);
const res = () => fetchPalletInstanceId(mockApi, false, false);

expect(res).toThrowError(
"No Assets pallet available, can't find a valid PalletInstance."
);
});
it('Should correctly grab the poolAssets pallet instance', () => {
const res = fetchPalletInstanceId(mockSystemApi, true);
const res = fetchPalletInstanceId(mockSystemApi, true, false);

expect(res).toEqual('55');
});
it('Should correctly grab the foreignAssets pallet instance', () => {
const res = fetchPalletInstanceId(mockSystemApi, false, true);

expect(res).toEqual('53');
});
it('Should correctly error when both foreign assets and pool assets are true', () => {
const err = () => fetchPalletInstanceId(mockSystemApi, true, true);

expect(err).toThrowError(
"Can't find the appropriate pallet when both liquid tokens and foreign assets"
);
});
});
15 changes: 13 additions & 2 deletions src/createXcmTypes/util/fetchPalletInstanceId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@ import { BaseError, BaseErrorsEnum } from '../../errors';
*/
export const fetchPalletInstanceId = (
api: ApiPromise,
isLiquidToken: boolean
isLiquidToken: boolean,
isForeignAsset: boolean
): string => {
const palletName = isLiquidToken ? 'PoolAssets' : 'Assets';
if (isLiquidToken && isForeignAsset) {
throw new BaseError(
"Can't find the appropriate pallet when both liquid tokens and foreign assets",
BaseErrorsEnum.InternalError
);
}
const palletName = isLiquidToken
? 'PoolAssets'
: isForeignAsset
? 'ForeignAssets'
: 'Assets';
const pallet = api.registry.metadata.pallets.filter(
(pallet) => pallet.name.toString() === palletName
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ describe('foreignMultiAssetMultiLocationExists', () => {
'1000': {
assetsInfo: {},
poolPairsInfo: {},
foreignAssetsPalletInstance: null,
assetsPalletInstance: null,
specName: '',
tokens: [],
foreignAssetsInfo: {},
Expand Down
12 changes: 0 additions & 12 deletions src/errors/checkXcmTxInputs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -946,8 +946,6 @@ describe('checkParaAssets', () => {
'1000': {
assetsInfo: {},
poolPairsInfo: {},
foreignAssetsPalletInstance: null,
assetsPalletInstance: null,
specName: '',
tokens: [],
foreignAssetsInfo: {},
Expand All @@ -973,8 +971,6 @@ describe('checkParaAssets', () => {
'2023': {
assetsInfo: {},
poolPairsInfo: {},
foreignAssetsPalletInstance: null,
assetsPalletInstance: null,
specName: '',
tokens: [],
foreignAssetsInfo: {},
Expand Down Expand Up @@ -1004,8 +1000,6 @@ describe('checkParaAssets', () => {
'1000': {
assetsInfo: {},
poolPairsInfo: {},
foreignAssetsPalletInstance: null,
assetsPalletInstance: null,
specName: '',
tokens: [],
foreignAssetsInfo: {},
Expand All @@ -1017,8 +1011,6 @@ describe('checkParaAssets', () => {
'1000': {
assetsInfo: {},
poolPairsInfo: {},
foreignAssetsPalletInstance: null,
assetsPalletInstance: null,
specName: '',
tokens: [],
foreignAssetsInfo: {},
Expand Down Expand Up @@ -1053,8 +1045,6 @@ describe('checkParaAssets', () => {
'1000': {
assetsInfo: {},
poolPairsInfo: {},
foreignAssetsPalletInstance: null,
assetsPalletInstance: null,
specName: '',
tokens: [],
foreignAssetsInfo: {},
Expand All @@ -1066,8 +1056,6 @@ describe('checkParaAssets', () => {
'1000': {
assetsInfo: {},
poolPairsInfo: {},
foreignAssetsPalletInstance: null,
assetsPalletInstance: null,
specName: '',
tokens: [],
foreignAssetsInfo: {},
Expand Down
36 changes: 7 additions & 29 deletions src/registry/Registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ export class Registry {
this.cache[this.relayChain][currentChainId] = {
assetsInfo: {},
poolPairsInfo: {},
foreignAssetsPalletInstance: null,
assetsPalletInstance: null,
specName: '',
tokens: [],
foreignAssetsInfo: {},
Expand All @@ -63,8 +61,6 @@ export class Registry {
this.cache[this.relayChain][ASSET_HUB_CHAIN_ID] = {
assetsInfo: {},
poolPairsInfo: {},
foreignAssetsPalletInstance: null,
assetsPalletInstance: null,
specName: '',
tokens: [],
foreignAssetsInfo: {},
Expand All @@ -81,16 +77,10 @@ export class Registry {
assetKey: string
): ForeignAssetsData | undefined {
const currentChainId = getChainIdBySpecName(this, this.specName);
const lookup =
this.cache[this.relayChain][currentChainId]['foreignAssetsInfo'];

if (
this.cache[this.relayChain][currentChainId]['foreignAssetsInfo'][assetKey]
) {
return this.cache[this.relayChain][currentChainId]['foreignAssetsInfo'][
assetKey
];
}

return undefined;
return lookup[assetKey] ? lookup[assetKey] : undefined;
}

/**
Expand Down Expand Up @@ -118,16 +108,9 @@ export class Registry {
assetKey: string
): { lpToken: string; pairInfo: string } | undefined {
const currentChainId = getChainIdBySpecName(this, this.specName);
const lookup = this.cache[this.relayChain][currentChainId]['poolPairsInfo'];

if (
this.cache[this.relayChain][currentChainId]['poolPairsInfo'][assetKey]
) {
return this.cache[this.relayChain][currentChainId]['poolPairsInfo'][
assetKey
] as { lpToken: string; pairInfo: string };
}

return undefined;
return lookup[assetKey] ? lookup[assetKey] : undefined;
}

/**
Expand All @@ -153,14 +136,9 @@ export class Registry {
*/
public cacheLookupAsset(assetKey: string): string | undefined {
const currentChainId = getChainIdBySpecName(this, this.specName);
const lookup = this.cache[this.relayChain][currentChainId]['assetsInfo'];

if (this.cache[this.relayChain][currentChainId]['assetsInfo'][assetKey]) {
return this.cache[this.relayChain][currentChainId]['assetsInfo'][
assetKey
];
}

return undefined;
return lookup[assetKey] ? lookup[assetKey] : undefined;
}

/**
Expand Down
4 changes: 0 additions & 4 deletions src/registry/parseRegistry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ describe('parseRegistry', () => {
assetsInfo,
foreignAssetsInfo,
specName: 'testing',
assetsPalletInstance: '100',
foreignAssetsPalletInstance: '1000',
poolPairsInfo: {},
},
},
Expand All @@ -36,8 +34,6 @@ describe('parseRegistry', () => {
assetsInfo: {},
foreignAssetsInfo: {},
specName: 'testing',
assetsPalletInstance: '100',
foreignAssetsPalletInstance: '1000',
poolPairsInfo: {},
});
// Ensure nothing was overwritten
Expand Down
2 changes: 0 additions & 2 deletions src/registry/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ export interface ChainInfoKeys {
tokens: string[];
assetsInfo: AssetsInfo;
foreignAssetsInfo: ForeignAssetsInfo;
assetsPalletInstance: string | null;
foreignAssetsPalletInstance: string | null;
poolPairsInfo: PoolPairsData;
}

Expand Down

0 comments on commit 401ab7d

Please sign in to comment.