Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(internal): refactor dest and beneficiary types generators #303

Merged
merged 11 commits into from
Oct 20, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export const limitedReserveTransferAssets = async (
const pallet = establishXcmPallet(api);
const ext = api.tx[pallet].limitedReserveTransferAssets;
const typeCreator = createXcmTypes[direction];
const beneficiary = typeCreator.createBeneficiary(api, destAddr, xcmVersion);
const dest = typeCreator.createDest(api, destChainId, xcmVersion);
const beneficiary = typeCreator.createBeneficiary(destAddr, xcmVersion);
const dest = typeCreator.createDest(destChainId, xcmVersion);
const assets = await typeCreator.createAssets(api, normalizeArrToStr(amounts), xcmVersion, specName, assetIds, {
registry,
isForeignAssetsTransfer,
Expand Down
4 changes: 2 additions & 2 deletions src/createXcmCalls/polkadotXcm/limitedTeleportAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export const limitedTeleportAssets = async (
const pallet = establishXcmPallet(api);
const ext = api.tx[pallet].limitedTeleportAssets;
const typeCreator = createXcmTypes[direction];
const beneficiary = typeCreator.createBeneficiary(api, destAddr, xcmVersion);
const dest = typeCreator.createDest(api, destChainId, xcmVersion);
const beneficiary = typeCreator.createBeneficiary(destAddr, xcmVersion);
const dest = typeCreator.createDest(destChainId, xcmVersion);
const assets = await typeCreator.createAssets(api, normalizeArrToStr(amounts), xcmVersion, specName, assetIds, {
registry,
isForeignAssetsTransfer,
Expand Down
4 changes: 2 additions & 2 deletions src/createXcmCalls/polkadotXcm/reserveTransferAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export const reserveTransferAssets = async (
const pallet = establishXcmPallet(api);
const ext = api.tx[pallet].reserveTransferAssets;
const typeCreator = createXcmTypes[direction];
const beneficiary = typeCreator.createBeneficiary(api, destAddr, xcmVersion);
const dest = typeCreator.createDest(api, destChainId, xcmVersion);
const beneficiary = typeCreator.createBeneficiary(destAddr, xcmVersion);
const dest = typeCreator.createDest(destChainId, xcmVersion);
const assets = await typeCreator.createAssets(api, normalizeArrToStr(amounts), xcmVersion, specName, assetIds, {
registry,
isForeignAssetsTransfer,
Expand Down
4 changes: 2 additions & 2 deletions src/createXcmCalls/polkadotXcm/teleportAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export const teleportAssets = async (
const pallet = establishXcmPallet(api);
const ext = api.tx[pallet].teleportAssets;
const typeCreator = createXcmTypes[direction];
const beneficiary = typeCreator.createBeneficiary(api, destAddr, xcmVersion);
const dest = typeCreator.createDest(api, destChainId, xcmVersion);
const beneficiary = typeCreator.createBeneficiary(destAddr, xcmVersion);
const dest = typeCreator.createDest(destChainId, xcmVersion);
const assets = await typeCreator.createAssets(api, normalizeArrToStr(amounts), xcmVersion, specName, assetIds, {
registry,
isForeignAssetsTransfer,
Expand Down
31 changes: 13 additions & 18 deletions src/createXcmTypes/ParaToPara.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,85 +9,80 @@ describe('ParaToPara', () => {
describe('Beneficiary', () => {
it('Should work for V2', () => {
const beneficiary = ParaToPara.createBeneficiary(
mockParachainApi,
'0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b',
2
);

const expectedRes = {
v2: {
V2: {
parents: 0,
interior: {
X1: {
AccountId32: {
id: '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b',
network: {
any: null,
},
network: 'Any',
},
},
},
},
};

expect(beneficiary.toJSON()?.toString()).toStrictEqual(expectedRes.toString());
expect(beneficiary).toStrictEqual(expectedRes);
});
it('Should work for V3', () => {
const beneficiary = ParaToPara.createBeneficiary(
mockParachainApi,
'0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b',
3
);

const expectedRes = {
v3: {
V3: {
parents: 0,
interior: {
X1: {
AccountId32: {
id: '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b',
network: null,
},
},
},
},
};

expect(beneficiary.toJSON()?.toString()).toStrictEqual(expectedRes.toString());
expect(beneficiary).toStrictEqual(expectedRes);
});
});
describe('Destination', () => {
it('Should work for V2', () => {
const destination = ParaToPara.createDest(mockParachainApi, '100', 2);
const destination = ParaToPara.createDest('100', 2);

const expectedRes = {
v2: {
V2: {
parents: 1,
interior: {
X1: {
Parachain: 100,
Parachain: '100',
},
},
},
};

expect(destination.toJSON()?.toString()).toStrictEqual(expectedRes.toString());
expect(destination).toStrictEqual(expectedRes);
});
it('Should work for V3', () => {
const destination = ParaToPara.createDest(mockParachainApi, '100', 3);
const destination = ParaToPara.createDest('100', 3);

const expectedRes = {
v3: {
V3: {
parents: 1,
interior: {
X1: {
Parachain: 100,
Parachain: '100',
},
},
},
};

expect(destination.toJSON()?.toString()).toStrictEqual(expectedRes.toString());
expect(destination).toStrictEqual(expectedRes);
});
});
describe('Assets', () => {
Expand Down
34 changes: 14 additions & 20 deletions src/createXcmTypes/ParaToPara.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

import type { ApiPromise } from '@polkadot/api';
import type { u32 } from '@polkadot/types';
import type {
MultiAssetsV2,
VersionedMultiAssets,
VersionedMultiLocation,
WeightLimitV2,
} from '@polkadot/types/interfaces';
import type { MultiAssetsV2, VersionedMultiAssets, WeightLimitV2 } from '@polkadot/types/interfaces';
import type { XcmV3MultiassetMultiAssets } from '@polkadot/types/lookup';
import type { AnyJson } from '@polkadot/types/types';

Expand All @@ -32,6 +27,7 @@ import type {
CreateWeightLimitOpts,
ICreateXcmType,
IWeightLimit,
XcmBase,
} from './types';
import { constructForeignAssetMultiLocationFromAssetId } from './util/constructForeignAssetMultiLocationFromAssetId';
import { dedupeMultiAssets } from './util/dedupeMultiAssets';
Expand All @@ -45,66 +41,64 @@ export const ParaToPara: ICreateXcmType = {
/**
* Create a XcmVersionedMultiLocation type for a beneficiary.
*
* @param api ApiPromise
* @param accountId The accountId of the beneficiary
* @param xcmVersion The accepted xcm version
*/
createBeneficiary: (api: ApiPromise, accountId: string, xcmVersion?: number): VersionedMultiLocation => {
createBeneficiary: (accountId: string, xcmVersion?: number): XcmBase => {
if (xcmVersion == 2) {
return api.registry.createType('XcmVersionedMultiLocation', {
return {
V2: {
parents: 0,
interior: {
X1: { AccountId32: { network: 'Any', id: accountId } },
},
},
});
};
}

return api.registry.createType('XcmVersionedMultiLocation', {
return {
V3: {
parents: 0,
interior: {
X1: { AccountId32: { id: accountId } },
},
},
});
};
},
/**
* Create a XcmVersionedMultiLocation type for a destination.
*
* @param api ApiPromise
* @param destId The parachain Id of the destination
* @param xcmVersion The accepted xcm version
*/
createDest: (api: ApiPromise, destId: string, xcmVersion?: number) => {
createDest: (destId: string, xcmVersion?: number): XcmBase => {
if (xcmVersion === 2) {
return api.registry.createType('XcmVersionedMultiLocation', {
return {
V2: {
parents: 1,
interior: {
X1: {
parachain: destId,
Parachain: destId,
},
},
},
});
};
}

/**
* Ensure that the `parents` field is a `1` when sending a destination MultiLocation
* from a system parachain to a sovereign parachain.
*/
return api.registry.createType('XcmVersionedMultiLocation', {
return {
V3: {
parents: 1,
interior: {
X1: {
parachain: destId,
Parachain: destId,
},
},
},
});
};
},
/**
* Create a VersionedMultiAsset type.
Expand Down
47 changes: 21 additions & 26 deletions src/createXcmTypes/ParaToSystem.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,85 +9,80 @@ describe('ParaToSystem', () => {
describe('Beneficiary', () => {
it('Should work for V2', () => {
const beneficiary = ParaToSystem.createBeneficiary(
mockParachainApi,
'0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b',
2
);

const expectedRes = {
v2: {
V2: {
parents: 0,
interior: mockParachainApi.registry.createType('InteriorMultiLocation', {
interior: {
X1: {
AccountId32: {
id: '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b',
network: {
any: null,
},
network: 'Any',
},
},
}),
},
},
};

expect(beneficiary.toJSON()?.toString()).toStrictEqual(expectedRes.toString());
expect(beneficiary).toStrictEqual(expectedRes);
});
it('Should work for V3', () => {
const beneficiary = ParaToSystem.createBeneficiary(
mockParachainApi,
'0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b',
3
);

const expectedRes = {
v3: {
V3: {
parents: 0,
interior: mockParachainApi.registry.createType('InteriorMultiLocation', {
interior: {
X1: {
AccountId32: {
id: '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b',
network: null,
},
},
}),
},
},
};

expect(beneficiary.toJSON()?.toString()).toStrictEqual(expectedRes.toString());
expect(beneficiary).toStrictEqual(expectedRes);
});
});
describe('Destination', () => {
it('Should work for V2', () => {
const destination = ParaToSystem.createDest(mockParachainApi, '100', 2);
const destination = ParaToSystem.createDest('100', 2);

const expectedRes = {
v2: {
V2: {
parents: 1,
interior: mockParachainApi.registry.createType('InteriorMultiLocation', {
interior: {
X1: {
Parachain: 100,
Parachain: '100',
},
}),
},
},
};

expect(destination.toJSON()?.toString()).toStrictEqual(expectedRes.toString());
expect(destination).toStrictEqual(expectedRes);
});
it('Should work for V3', () => {
const destination = ParaToSystem.createDest(mockParachainApi, '100', 3);
const destination = ParaToSystem.createDest('100', 3);

const expectedRes = {
v3: {
V3: {
parents: 1,
interior: mockParachainApi.registry.createType('InteriorMultiLocation', {
interior: {
X1: {
Parachain: 100,
Parachain: '100',
},
}),
},
},
};

expect(destination.toJSON()?.toString()).toStrictEqual(expectedRes.toString());
expect(destination).toStrictEqual(expectedRes);
});
});
describe('Assets', () => {
Expand Down
Loading