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

feat: add transfer assets call #378

Merged
merged 15 commits into from
Mar 5, 2024
83 changes: 47 additions & 36 deletions src/AssetTransferApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ import {
limitedReserveTransferAssets,
reserveTransferAssets,
teleportAssets,
transferAssets,
transferMultiasset,
transferMultiassets,
transferMultiassetWithFee,
} from './createXcmCalls';
import { XcmPalletName } from './createXcmCalls/util/establishXcmPallet';
import { XTokensBaseArgs } from './createXcmCalls/xTokens/types';
import type { XTokensBaseArgs } from './createXcmCalls/xTokens/types';
import { Registry } from './registry';
import { adjustedMockBifrostParachainApi } from './testHelpers/adjustedMockBifrostParachainApi';
import { adjustedMockMoonriverParachainApi } from './testHelpers/adjustedMockMoonriverParachainApi';
import { adjustedMockMoonriverNoXTokensParachainApi } from './testHelpers/adjustedMockMoonriverParachainNoXTokens';
import { adjustedMockRelayApiNoLimitedReserveTransferAssets } from './testHelpers/adjustedMockRelayApiNoLimitedReserveTransferAssets';
import { adjustedMockRelayApi } from './testHelpers/adjustedMockRelayApiV9420';
import { adjustedMockSystemApi } from './testHelpers/adjustedMockSystemApiV1004000';
import { adjustedMockSystemApiV1007000 } from './testHelpers/adjustedMockSystemApiV1007000';
import { mockSystemApi } from './testHelpers/mockSystemApi';
import { mockWeightInfo } from './testHelpers/mockWeightInfo';
import { AssetCallType, Direction, ResolvedCallInfo, UnsignedTransaction, XcmBaseArgs, XcmDirection } from './types';
Expand Down Expand Up @@ -49,6 +51,9 @@ const bifrostAssetsApi = new AssetTransferApi(adjustedMockBifrostParachainApi, '
const moonriverAssetsNoXTokensApi = new AssetTransferApi(adjustedMockMoonriverNoXTokensParachainApi, 'moonriver', 2, {
registryType: 'NPM',
});
const westmintAssetsApi = new AssetTransferApi(adjustedMockSystemApiV1007000, 'westmint', 4, {
registryType: 'NPM',
});

describe('AssetTransferAPI', () => {
describe('establishDirection', () => {
Expand Down Expand Up @@ -292,21 +297,6 @@ describe('AssetTransferAPI', () => {

expect(assetCallType).toEqual('Teleport');
});
it('Should correctly throw an error when sending a foreign asset to a system chain', () => {
const err = () =>
systemAssetsApi['fetchCallType'](
'1000',
'1001',
[`{"parents": "1", "interior": { "X1": {"Parachain": "2023"}}}`],
Direction.SystemToSystem,
AssetType.Foreign,
true,
false,
systemAssetsApi.registry,
);

expect(err).toThrow('Unable to send foreign assets in direction SystemToSystem');
});
});
describe('SystemToPara', () => {
it('Should correctly return Teleport when sending to origin Parachain', () => {
Expand Down Expand Up @@ -875,9 +865,49 @@ describe('AssetTransferAPI', () => {
);
});
});

describe('resolveCall', () => {
describe('SystemToPara', () => {
it('Should correctly resolve to a `transferAssets` call for runtime with the `transferAssets` call', async () => {
const specName = 'westmint';
const registry = new Registry(specName, {});

const mockBaseArgs: XcmBaseArgs = {
api: westmintAssetsApi.api,
direction: Direction.SystemToPara as XcmDirection,
destAddr: '0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b',
assetIds: ['usdt'],
amounts: ['10000000000'],
destChainId: '2023',
xcmVersion: 4,
specName: 'westmint',
registry: registry,
};

const mockBaseOpts = {
isLimited: true,
weightLimit: {
refTime: '3000',
proofSize: '10000',
},
isLiquidTokenTransfer: false,
isForeignAssetsTransfer: false,
};

const expected: ResolvedCallInfo = ['transferAssets', await transferAssets(mockBaseArgs, mockBaseOpts)];

const result = await westmintAssetsApi['resolveCall'](
['usdt'],
'polkadotXcm' as XcmPalletName,
Direction.SystemToPara,
'Reserve' as AssetCallType,
mockBaseArgs,
mockBaseOpts,
true,
);

expect(JSON.stringify(result)).toEqual(JSON.stringify(expected));
});

it('Should correctly resolve to a `reserveTransferAssets` call', async () => {
const specName = 'statemine';
const registry = new Registry(specName, {});
Expand All @@ -893,7 +923,6 @@ describe('AssetTransferAPI', () => {
specName: 'statemine',
registry: registry,
};

const mockBaseOpts = {
isLimited: false,
isLiquidTokenTransfer: false,
Expand Down Expand Up @@ -933,7 +962,6 @@ describe('AssetTransferAPI', () => {
specName: 'statemine',
registry: registry,
};

const mockBaseOpts = {
isLimited: true,
weightLimit: {
Expand Down Expand Up @@ -979,7 +1007,6 @@ describe('AssetTransferAPI', () => {
specName: 'statemine',
registry: registry,
};

const mockBaseOpts = {
isLimited: false,
weightLimit: {
Expand Down Expand Up @@ -1022,7 +1049,6 @@ describe('AssetTransferAPI', () => {
specName: 'statemine',
registry: registry,
};

const mockBaseOpts = {
isLimited: false,
weightLimit: {
Expand Down Expand Up @@ -1066,7 +1092,6 @@ describe('AssetTransferAPI', () => {
registry: registry,
xcmPallet: 'xTokens' as XcmPalletName,
};

const mockBaseOpts = {
isLimited: true,
weightLimit: {
Expand Down Expand Up @@ -1108,7 +1133,6 @@ describe('AssetTransferAPI', () => {
registry: registry,
xcmPallet: 'xTokens' as XcmPalletName,
};

const mockBaseOpts = {
isLimited: true,
weightLimit: {
Expand Down Expand Up @@ -1155,7 +1179,6 @@ describe('AssetTransferAPI', () => {
registry: registry,
xcmPallet: 'xTokens' as XcmPalletName,
};

const mockBaseOpts = {
isLimited: true,
weightLimit: {
Expand Down Expand Up @@ -1201,7 +1224,6 @@ describe('AssetTransferAPI', () => {
specName: 'moonriver',
registry: registry,
};

const mockBaseOpts = {
isLimited: false,
paysWithFeeDest: '1984',
Expand Down Expand Up @@ -1242,7 +1264,6 @@ describe('AssetTransferAPI', () => {
specName: 'moonriver',
registry: registry,
};

const mockBaseOpts = {
isLimited: true,
weightLimit: {
Expand Down Expand Up @@ -1282,7 +1303,6 @@ describe('AssetTransferAPI', () => {
const direction = Direction.ParaToRelay;
const assetIds = ['ksm'];
const isLimited = true;

const mockBaseArgs: XTokensBaseArgs = {
api: bifrostAssetsApi.api,
direction,
Expand All @@ -1295,7 +1315,6 @@ describe('AssetTransferAPI', () => {
registry: registry,
xcmPallet,
};

const mockBaseOpts = {
isLimited,
weightLimit: {
Expand Down Expand Up @@ -1342,7 +1361,6 @@ describe('AssetTransferAPI', () => {
specName,
registry: registry,
};

const mockBaseOpts = {
isLimited,
paysWithFeeDest: '1984',
Expand Down Expand Up @@ -1389,7 +1407,6 @@ describe('AssetTransferAPI', () => {
specName,
registry: registry,
};

const mockBaseOpts = {
isLimited,
isLiquidTokenTransfer: false,
Expand Down Expand Up @@ -1438,7 +1455,6 @@ describe('AssetTransferAPI', () => {
registry: registry,
xcmPallet,
};

const mockBaseOpts = {
isLimited,
isLiquidTokenTransfer: false,
Expand Down Expand Up @@ -1482,7 +1498,6 @@ describe('AssetTransferAPI', () => {
registry: registry,
xcmPallet,
};

const mockBaseOpts = {
isLimited,
isLiquidTokenTransfer: false,
Expand Down Expand Up @@ -1531,7 +1546,6 @@ describe('AssetTransferAPI', () => {
registry: registry,
xcmPallet,
};

const mockBaseOpts = {
isLimited,
paysWithFeeDest,
Expand Down Expand Up @@ -1580,7 +1594,6 @@ describe('AssetTransferAPI', () => {
specName,
registry: registry,
};

const mockBaseOpts = {
isLimited,
isLiquidTokenTransfer: false,
Expand Down Expand Up @@ -1625,7 +1638,6 @@ describe('AssetTransferAPI', () => {
specName,
registry: registry,
};

const mockBaseOpts = {
isLimited,
isLiquidTokenTransfer: false,
Expand Down Expand Up @@ -1671,7 +1683,6 @@ describe('AssetTransferAPI', () => {
specName,
registry: registry,
};

const mockBaseOpts = {
isLimited,
weightLimit: {
Expand Down
12 changes: 4 additions & 8 deletions src/AssetTransferApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
limitedTeleportAssets,
reserveTransferAssets,
teleportAssets,
transferAssets,
transferMultiasset,
transferMultiassets,
transferMultiassetWithFee,
Expand Down Expand Up @@ -590,14 +591,6 @@ export class AssetTransferApi {
return AssetCallType.Teleport;
}

// system to system foreign asset -> not allowed
if (assetType === AssetType.Foreign && xcmDirection === Direction.SystemToSystem) {
throw new BaseError(
`Unable to send foreign assets in direction ${xcmDirection}`,
BaseErrorsEnum.InvalidDirection,
);
}

// system to para native asset -> reserve
if (assetType === AssetType.Native && xcmDirection === Direction.SystemToPara) {
return AssetCallType.Reserve;
Expand Down Expand Up @@ -1019,6 +1012,8 @@ export class AssetTransferApi {
} else {
txMethod = 'transferMultiassets';
}
} else if (api.tx[xcmPallet] && api.tx[xcmPallet].transferAssets) {
txMethod = 'transferAssets';
} else if (assetCallType === AssetCallType.Reserve) {
if (isLimited) {
txMethod = 'limitedReserveTransferAssets';
Expand Down Expand Up @@ -1051,6 +1046,7 @@ export class AssetTransferApi {
reserveTransferAssets: [reserveTransferAssets, [baseArgs, baseOpts]],
limitedTeleportAssets: [limitedTeleportAssets, [baseArgs, baseOpts]],
teleportAssets: [teleportAssets, [baseArgs, baseOpts]],
transferAssets: [transferAssets, [baseArgs, baseOpts]],
};

let call: XTokensCallSignature | XcmPalletCallSignature;
Expand Down
1 change: 1 addition & 0 deletions src/createXcmCalls/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { limitedReserveTransferAssets } from './polkadotXcm/limitedReserveTransf
export { limitedTeleportAssets } from './polkadotXcm/limitedTeleportAssets';
export { reserveTransferAssets } from './polkadotXcm/reserveTransferAssets';
export { teleportAssets } from './polkadotXcm/teleportAssets';
export { transferAssets } from './polkadotXcm/transferAssets';
export { transferMultiasset } from './xTokens/transferMultiasset';
export { transferMultiassets } from './xTokens/transferMultiassets';
export { transferMultiassetWithFee } from './xTokens/transferMultiassetWithFee';
Loading