Skip to content

Commit

Permalink
Add paysWithFeeOrigin logic
Browse files Browse the repository at this point in the history
  • Loading branch information
TarikGul committed Nov 29, 2023
1 parent cbb7443 commit fa3229d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
7 changes: 5 additions & 2 deletions src/AssetTransferApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,8 @@ describe('AssetTransferAPI', () => {
['1984', 'usdc'],
['5000000', '4000000000'],
{
paysWithFeeOrigin: '1984',
paysWithFeeOrigin:
'{"parents": "0", "interior": { "X2": [{"PalletInstance": "50", "GeneralIndex": "1984"}]}}',
format: 'payload',
keepAlive: true,
paysWithFeeDest: 'USDC',
Expand Down Expand Up @@ -753,7 +754,9 @@ describe('AssetTransferAPI', () => {
sendersAddr: 'FBeL7DanUDs5SZrxZY1CizMaPgG9vZgJgvr52C2dg81SsF1',
}
);
}).rejects.toThrowError('paysWithFeeOrigin value must be a valid number. Received: hello there');
}).rejects.toThrowError(
'paysWithFeeOrigin is an invalid asset. The asset must be a valid integer or multiLocation depending on the runtime: SyntaxError: Unexpected token \'h\', "hello there" is not valid JSON'
);
});

it('Should error during payload construction when a paysWithFeeOrigin is provided that matches a non sufficient asset', async () => {
Expand Down
38 changes: 22 additions & 16 deletions src/AssetTransferApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { ApiPromise } from '@polkadot/api';
import type { SubmittableExtrinsic } from '@polkadot/api/submittable/types';
import { EXTRINSIC_VERSION } from '@polkadot/types/extrinsic/v4/Extrinsic';
import type { RuntimeDispatchInfo, RuntimeDispatchInfoV1 } from '@polkadot/types/interfaces';
import type { ISubmittableResult } from '@polkadot/types/types';
import type { AnyJson, ISubmittableResult } from '@polkadot/types/types';
import BN from 'bn.js';

import { CDN_URL, RELAY_CHAIN_IDS, RELAY_CHAIN_NAMES, SYSTEM_PARACHAINS_NAMES } from './consts';
Expand Down Expand Up @@ -736,7 +736,7 @@ export class AssetTransferApi {
opts: { paysWithFeeOrigin?: string; sendersAddr: string }
): Promise<`0x${string}`> => {
const { paysWithFeeOrigin, sendersAddr } = opts;
let assetId = new BN(0);
let assetId: BN | AnyJson = new BN(0);

// if a paysWithFeeOrigin is provided and the chain is of system origin
// we assign the assetId to the value of paysWithFeeOrigin
Expand All @@ -745,21 +745,27 @@ export class AssetTransferApi {
if (paysWithFeeOrigin && isOriginSystemParachain) {
const isValidInt = validateNumber(paysWithFeeOrigin);

if (!isValidInt) {
throw new BaseError(
`paysWithFeeOrigin value must be a valid number. Received: ${paysWithFeeOrigin}`,
BaseErrorsEnum.InvalidInput
);
}

assetId = new BN(paysWithFeeOrigin);
const isSufficient = await this.checkAssetIsSufficient(assetId);
if (isValidInt) {
assetId = new BN(paysWithFeeOrigin);
const isSufficient = await this.checkAssetIsSufficient(assetId);

if (!isSufficient) {
throw new BaseError(
`asset with assetId ${assetId.toString()} is not a sufficient asset to pay for fees`,
BaseErrorsEnum.InvalidAsset
);
if (!isSufficient) {
throw new BaseError(
`asset with assetId ${assetId.toString()} is not a sufficient asset to pay for fees`,
BaseErrorsEnum.InvalidAsset
);
}
} else {
try {
assetId = JSON.parse(paysWithFeeOrigin) as AnyJson;
} catch (e) {
throw new BaseError(
`paysWithFeeOrigin is an invalid asset. The asset must be a valid integer or multiLocation depending on the runtime: ${
e as string
}`,
BaseErrorsEnum.InvalidAsset
);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import type { ApiPromise } from '@polkadot/api';
import type { SubmittableExtrinsic } from '@polkadot/api/submittable/types';
import type { InteriorMultiLocation } from '@polkadot/types/interfaces';
import type { ISubmittableResult } from '@polkadot/types/types';
import type { AnyJson, ISubmittableResult } from '@polkadot/types/types';
import BN from 'bn.js';

import { XcmPalletName } from './createXcmCalls/util/establishXcmPallet';
Expand Down Expand Up @@ -318,7 +318,7 @@ export interface UnsignedTransaction extends SignerPayloadJSON {
*
* @default 0
*/
assetId: BN;
assetId: BN | AnyJson;
}

export interface XcmBaseArgs {
Expand Down

0 comments on commit fa3229d

Please sign in to comment.