Skip to content

Commit

Permalink
fix: default to xcmPallet (#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
marshacb authored Oct 4, 2024
1 parent ecf265d commit 81904a2
Show file tree
Hide file tree
Showing 11 changed files with 542 additions and 316 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ interface TransferArgsOpts<T extends Format> {
* Optional assetId that will be used to pay for fees. Used with the `dryRunCall` option to determine fees in the specified asset.
*/
xcmFeeAsset?: string;

/**
* Optionally sets the pallet to be used for the current tx.
*/
xcmPalletOverride?: XcmPallet;
}
```

Expand Down
3 changes: 3 additions & 0 deletions src/AssetTransferApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ describe('AssetTransferAPI', () => {
{
format: 'call',
keepAlive: false,
xcmPalletOverride: 'xTokens',
},
);

Expand All @@ -604,6 +605,7 @@ describe('AssetTransferAPI', () => {
format: 'payload',
keepAlive: false,
sendersAddr: 'FBeL7DanUDs5SZrxZY1CizMaPgG9vZgJgvr52C2dg81SsF1',
xcmPalletOverride: 'xTokens',
},
);

Expand All @@ -622,6 +624,7 @@ describe('AssetTransferAPI', () => {
['10000000000000'],
{
format: 'submittable',
xcmPalletOverride: 'xTokens',
},
);

Expand Down
3 changes: 2 additions & 1 deletion src/AssetTransferApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ export class AssetTransferApi {
customXcmOnDest,
dryRunCall,
xcmFeeAsset,
xcmPalletOverride,
} = opts;

if (!this.registryConfig.registryInitialized) {
Expand Down Expand Up @@ -242,7 +243,7 @@ export class AssetTransferApi {
const xcmDirection = this.establishDirection(isLocalTx, chainOriginDestInfo);
const isForeignAssetsTransfer = await this.checkContainsForeignAssets(api, assetIds);
const isPrimaryParachainNativeAsset = isParachainPrimaryNativeAsset(registry, specName, xcmDirection, assetIds[0]);
const xcmPallet = establishXcmPallet(api, xcmDirection);
const xcmPallet = establishXcmPallet(api, xcmDirection, xcmPalletOverride);
const declaredXcmVersion = xcmVersion === undefined ? safeXcmVersion : xcmVersion;
checkXcmVersion(declaredXcmVersion); // Throws an error when the xcmVersion is not supported.

Expand Down
5 changes: 5 additions & 0 deletions src/config/disabledOpts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,9 @@ export const disabledOpts: DisabledOptions = {
chains: [],
error: (opt: string, chain: string) => callError(opt, chain),
},
xcmPalletOverride: {
disabled: false,
chains: [],
error: (opt: string, chain: string) => callError(opt, chain),
},
};
5 changes: 5 additions & 0 deletions src/createXcmCalls/util/establishXcmPallet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ describe('establishXcmPallet', () => {
const res = establishXcmPallet(mockSystemApi);
expect(res).toEqual('polkadotXcm');
});
it('Should correctly throw an error when an overrided pallet is not found for the given runtime', () => {
const xcmPalletOverride = 'xTokens';
const err = () => establishXcmPallet(mockSystemApi, undefined, xcmPalletOverride);
expect(err).toThrow('Pallet xTokens not found in the current runtime.');
});
});
31 changes: 23 additions & 8 deletions src/createXcmCalls/util/establishXcmPallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { ApiPromise } from '@polkadot/api';

import { SUPPORTED_XCM_PALLETS } from '../../consts';
import { BaseError, BaseErrorsEnum } from '../../errors';
import { Direction } from '../../types';
import { Direction, XcmPallet } from '../../types';

export enum XcmPalletName {
xcmPallet = 'xcmPallet',
Expand All @@ -20,7 +20,28 @@ export enum XcmPalletName {
*
* @param api ApiPromise
*/
export const establishXcmPallet = (api: ApiPromise, direction?: Direction): XcmPalletName => {
export const establishXcmPallet = (
api: ApiPromise,
direction?: Direction,
xcmPalletOverride?: XcmPallet,
): XcmPalletName => {
if (xcmPalletOverride) {
if (api.tx[xcmPalletOverride]) {
return XcmPalletName[xcmPalletOverride];
} else {
throw new BaseError(
`Pallet ${xcmPalletOverride} not found in the current runtime.`,
BaseErrorsEnum.PalletNotFound,
);
}
}

if (api.tx.polkadotXcm) {
return XcmPalletName.polkadotXcm;
} else if (api.tx.xcmPallet) {
return XcmPalletName.xcmPallet;
}

let xPallet: XcmPalletName | undefined;

if (api.tx.xTokens) {
Expand All @@ -33,12 +54,6 @@ export const establishXcmPallet = (api: ApiPromise, direction?: Direction): XcmP
return xPallet as XcmPalletName;
}

if (api.tx.polkadotXcm) {
return XcmPalletName.polkadotXcm;
} else if (api.tx.xcmPallet) {
return XcmPalletName.xcmPallet;
}

const supportedPallets = SUPPORTED_XCM_PALLETS.map((pallet) => {
return pallet;
}).join(', ');
Expand Down
Loading

0 comments on commit 81904a2

Please sign in to comment.