Skip to content

Commit

Permalink
Refactor ParaToSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
TarikGul committed Oct 20, 2023
1 parent 07af5c1 commit 4f4f63c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 41 deletions.
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
28 changes: 13 additions & 15 deletions src/createXcmTypes/ParaToSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type { u32 } from '@polkadot/types';
import type {
MultiAssetsV2,
VersionedMultiAssets,
VersionedMultiLocation,
WeightLimitV2,
} from '@polkadot/types/interfaces';
import type { XcmV3MultiassetMultiAssets } from '@polkadot/types/lookup';
Expand All @@ -32,6 +31,7 @@ import type {
CreateWeightLimitOpts,
ICreateXcmType,
IWeightLimit,
XcmBase,
} from './types';
import { constructForeignAssetMultiLocationFromAssetId } from './util/constructForeignAssetMultiLocationFromAssetId';
import { dedupeMultiAssets } from './util/dedupeMultiAssets';
Expand All @@ -45,66 +45,64 @@ export const ParaToSystem: 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

0 comments on commit 4f4f63c

Please sign in to comment.