Skip to content

Commit

Permalink
refactor Xtokens calls
Browse files Browse the repository at this point in the history
  • Loading branch information
TarikGul committed Nov 21, 2023
1 parent 8b7634b commit 4bf50eb
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 296 deletions.
42 changes: 3 additions & 39 deletions src/AssetTransferApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,49 +335,13 @@ export class AssetTransferApi {
// This ensures paraToRelay always uses `transferMultiAsset`.
if (xcmDirection === Direction.ParaToRelay || (!paysWithFeeDest && assetIds.length < 2)) {
txMethod = 'transferMultiasset';
transaction = await transferMultiasset(
_api,
xcmDirection,
addr,
assetIds,
amounts,
destChainId,
declaredXcmVersion,
_specName,
this.registry,
xcmPallet,
baseOpts
);
transaction = await transferMultiasset({ ...baseArgs, xcmPallet }, baseOpts);
} else if (paysWithFeeDest && paysWithFeeDest.includes('parents')) {
txMethod = 'transferMultiassetWithFee';
transaction = await transferMultiassetWithFee(
_api,
xcmDirection,
addr,
assetIds,
amounts,
destChainId,
declaredXcmVersion,
_specName,
this.registry,
xcmPallet,
baseOpts
);
transaction = await transferMultiassetWithFee({ ...baseArgs, xcmPallet }, baseOpts);
} else {
txMethod = 'transferMultiassets';
transaction = await transferMultiassets(
_api,
xcmDirection,
addr,
assetIds,
amounts,
destChainId,
declaredXcmVersion,
_specName,
this.registry,
xcmPallet,
baseOpts
);
transaction = await transferMultiassets({ ...baseArgs, xcmPallet }, baseOpts);
}
} else if (assetCallType === AssetCallType.Reserve) {
if (isLimited) {
Expand Down
4 changes: 2 additions & 2 deletions src/createXcmCalls/polkadotXcm/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { ApiPromise } from '@polkadot/api';
import type { Registry } from '../../registry';
import type { XcmDirection } from '../../types';

export type PolkadotXcmBaseArgs = {
export interface PolkadotXcmBaseArgs {
api: ApiPromise;
direction: XcmDirection;
destAddr: string;
Expand All @@ -15,4 +15,4 @@ export type PolkadotXcmBaseArgs = {
xcmVersion: number;
specName: string;
registry: Registry;
};
}
89 changes: 29 additions & 60 deletions src/createXcmCalls/xTokens/transferMultiAsset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,46 @@

import { Registry } from '../../registry';
import { adjustedMockMoonriverParachainApi } from '../../testHelpers/adjustedMockMoonriverParachainApi';
import { Direction } from '../../types';
import { Direction, XcmDirection } from '../../types';
import { XcmPalletName } from '../util/establishXcmPallet';
import { transferMultiasset } from './transferMultiasset';

describe('transferMultiasset', () => {
describe('ParaToSystem', () => {
const registry = new Registry('moonriver', {});

const baseArgs = {
api: adjustedMockMoonriverParachainApi,
direction: Direction.ParaToSystem as XcmDirection,
destAddr: '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b',
assetIds: ['311091173110107856861649819128533077277'],
amounts: ['1000000'],
destChainId: '1000',
xcmVersion: 2,
specName: 'moonriver',
registry,
xcmPallet: XcmPalletName.xTokens,
};
it('Should correctly construct an Unlimited transferMultiasset tx for V2', async () => {
const ext = await transferMultiasset(
adjustedMockMoonriverParachainApi,
Direction.ParaToSystem,
'0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b',
['311091173110107856861649819128533077277'],
['1000000'],
'1000',
2,
'moonriver',
registry,
XcmPalletName.xTokens,
{
isLimited: false,
isForeignAssetsTransfer: false,
isLiquidTokenTransfer: false,
}
);
const ext = await transferMultiasset(baseArgs, {
isLimited: false,
isForeignAssetsTransfer: false,
isLiquidTokenTransfer: false,
});

expect(ext.toHex()).toBe(
'0xf4046a010100010300a10f043205011f0002093d0001010200a10f0100f5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b00'
);
});
it('Should correctly construct a Limited transferMultiasset tx for V2', async () => {
const ext = await transferMultiasset(
adjustedMockMoonriverParachainApi,
Direction.ParaToSystem,
'0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b',
['311091173110107856861649819128533077277'],
['1000000'],
'1000',
2,
'moonriver',
registry,
XcmPalletName.xTokens,
{
isLimited: true,
weightLimit: {
refTime: '1000',
proofSize: '2000',
},
isForeignAssetsTransfer: false,
isLiquidTokenTransfer: false,
}
);
const ext = await transferMultiasset(baseArgs, {
isLimited: true,
weightLimit: {
refTime: '1000',
proofSize: '2000',
},
isForeignAssetsTransfer: false,
isLiquidTokenTransfer: false,
});

expect(ext.toHex()).toBe(
'0x0501046a010100010300a10f043205011f0002093d0001010200a10f0100f5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b01a10f411f'
Expand All @@ -63,16 +50,7 @@ describe('transferMultiasset', () => {

it('Should correctly construct an Unlimited transferMultiasset tx for V3', async () => {
const ext = await transferMultiasset(
adjustedMockMoonriverParachainApi,
Direction.ParaToSystem,
'0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b',
['311091173110107856861649819128533077277'],
['100'],
'1000',
3,
'moonriver',
registry,
XcmPalletName.xTokens,
{ ...baseArgs, xcmVersion: 3, amounts: ['100'] },
{
isLimited: false,
isForeignAssetsTransfer: false,
Expand All @@ -86,16 +64,7 @@ describe('transferMultiasset', () => {
});
it('Should correctly construct a Limited transferMultiasset tx for V3', async () => {
const ext = await transferMultiasset(
adjustedMockMoonriverParachainApi,
Direction.ParaToSystem,
'0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b',
['42259045809535163221576417993425387648'],
['1000000'],
'1000',
3,
'moonriver',
registry,
XcmPalletName.xTokens,
{ ...baseArgs, xcmVersion: 3, assetIds: ['42259045809535163221576417993425387648'] },
{
isLimited: true,
weightLimit: {
Expand Down
110 changes: 44 additions & 66 deletions src/createXcmCalls/xTokens/transferMultiAssetWithFee.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,42 @@

import { Registry } from '../../registry';
import { adjustedMockMoonriverParachainApi } from '../../testHelpers/adjustedMockMoonriverParachainApi';
import { Direction } from '../../types';
import { Direction, XcmDirection } from '../../types';
import { XcmPalletName } from '../util/establishXcmPallet';
import { transferMultiassetWithFee } from './transferMultiassetWithFee';

describe('transferMultiassetWithFee', () => {
describe('ParaToSystem', () => {
const registry = new Registry('moonriver', {});

const baseArgs = {
api: adjustedMockMoonriverParachainApi,
direction: Direction.ParaToSystem as XcmDirection,
destAddr: '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b',
assetIds: ['311091173110107856861649819128533077277'],
amounts: ['1000000'],
destChainId: '1000',
xcmVersion: 2,
specName: 'moonriver',
registry,
xcmPallet: XcmPalletName.xTokens,
};
it('Should correctly construct an Unlimited transferMultiassetWithFee tx for V2', async () => {
const isLimited = false;
const refTime = undefined;
const proofSize = undefined;
const paysWithFeeDest =
'{"parents": "1", "interior": {"X3": [{"Parachain": "1000"}, {"PalletInstance": "50"}, {"GeneralIndex": "1984"}]}}';

const ext = await transferMultiassetWithFee(
adjustedMockMoonriverParachainApi,
Direction.ParaToSystem,
'0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b',
['311091173110107856861649819128533077277'],
['1000000'],
'1000',
2,
'moonriver',
registry,
XcmPalletName.xTokens,
{
isLimited,
weightLimit: {
refTime,
proofSize,
},
paysWithFeeDest,
isForeignAssetsTransfer: false,
isLiquidTokenTransfer: false,
}
);
const ext = await transferMultiassetWithFee(baseArgs, {
isLimited,
weightLimit: {
refTime,
proofSize,
},
paysWithFeeDest,
isForeignAssetsTransfer: false,
isLiquidTokenTransfer: false,
});

expect(ext.toHex()).toBe(
'0x2d01046a030100010300a10f043205011f0002093d000100010300a10f043205011f000001010200a10f0100f5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b00'
Expand All @@ -51,28 +50,16 @@ describe('transferMultiassetWithFee', () => {
const paysWithFeeDest =
'{"parents": "1", "interior": {"X3": [{"Parachain": "1000"}, {"PalletInstance": "50"}, {"GeneralIndex": "1984"}]}}';

const ext = await transferMultiassetWithFee(
adjustedMockMoonriverParachainApi,
Direction.ParaToSystem,
'0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b',
['311091173110107856861649819128533077277'],
['1000000'],
'1000',
2,
'moonriver',
registry,
XcmPalletName.xTokens,
{
isLimited,
weightLimit: {
refTime,
proofSize,
},
paysWithFeeDest,
isForeignAssetsTransfer: false,
isLiquidTokenTransfer: false,
}
);
const ext = await transferMultiassetWithFee(baseArgs, {
isLimited,
weightLimit: {
refTime,
proofSize,
},
paysWithFeeDest,
isForeignAssetsTransfer: false,
isLiquidTokenTransfer: false,
});

expect(ext.toHex()).toBe(
'0x3d01046a030100010300a10f043205011f0002093d000100010300a10f043205011f000001010200a10f0100f5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b01a10f411f'
Expand All @@ -87,16 +74,12 @@ describe('transferMultiassetWithFee', () => {
'{"parents": "1", "interior": {"X3": [{"Parachain": "1000"}, {"PalletInstance": "50"}, {"GeneralIndex": "1984"}]}}';

const ext = await transferMultiassetWithFee(
adjustedMockMoonriverParachainApi,
Direction.ParaToSystem,
'0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b',
['42259045809535163221576417993425387648'],
['10000000000'],
'1000',
3,
'moonriver',
registry,
XcmPalletName.xTokens,
{
...baseArgs,
assetIds: ['42259045809535163221576417993425387648'],
amounts: ['10000000000'],
xcmVersion: 3,
},
{
isLimited,
weightLimit: {
Expand All @@ -121,16 +104,11 @@ describe('transferMultiassetWithFee', () => {
'{"parents": "1", "interior": {"X3": [{"Parachain": "1000"}, {"PalletInstance": "50"}, {"GeneralIndex": "1984"}]}}';

const ext = await transferMultiassetWithFee(
adjustedMockMoonriverParachainApi,
Direction.ParaToSystem,
'0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b',
['311091173110107856861649819128533077277'],
['100'],
'1000',
3,
'moonriver',
registry,
XcmPalletName.xTokens,
{
...baseArgs,
amounts: ['100'],
xcmVersion: 3,
},
{
isLimited,
weightLimit: {
Expand Down
Loading

0 comments on commit 4bf50eb

Please sign in to comment.