From 4dd6a0e17e8135ea6e03a80138c458601784e5fe Mon Sep 17 00:00:00 2001 From: marshacb Date: Fri, 15 Dec 2023 16:49:25 -0500 Subject: [PATCH] add check to ensure paysWithFeeOrigin multilocation is part of an existing liquidty pool adds tests --- src/AssetTransferApi.spec.ts | 9 +- src/AssetTransferApi.ts | 53 ++++++------ src/errors/checkXcmTxInputs.spec.ts | 2 +- src/errors/checkXcmTxInputs.ts | 9 +- src/testHelpers/adjustedMockSystemApi.ts | 105 +++++++++++++++++------ 5 files changed, 111 insertions(+), 67 deletions(-) diff --git a/src/AssetTransferApi.spec.ts b/src/AssetTransferApi.spec.ts index 65625b93..6b2c65c6 100644 --- a/src/AssetTransferApi.spec.ts +++ b/src/AssetTransferApi.spec.ts @@ -712,7 +712,7 @@ describe('AssetTransferAPI', () => { }); }); describe('paysWithFeeOrigin', () => { - it('Should correctly assign the assedId field to an unsigned transaction when a valid sufficient paysWithFeeOrigin option is provided', async () => { + it('Should correctly assign the assedId field to an unsigned transaction when a valid paysWithFeeOrigin MultiLocation option is provided', async () => { const expected = { parents: '0', interior: { X2: [{ PalletInstance: '50' }, { GeneralIndex: '1,984' }] } }; const payload = await systemAssetsApi.createTransferTransaction( '2023', @@ -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', @@ -729,15 +730,11 @@ describe('AssetTransferAPI', () => { }, ); - console.log('PAYLOAD', payload); - const result = mockSystemApi.registry.createType('ExtrinsicPayload', payload.tx, { version: 4, }); - console.log('RESULT TO HUMAN', result.toHuman()); const unsigned = result.toHuman() as unknown as UnsignedTransaction; - console.log('UNSIGNED IN TEST', unsigned); expect(unsigned.assetId).toStrictEqual(expected); }); diff --git a/src/AssetTransferApi.ts b/src/AssetTransferApi.ts index bc231ff1..93277339 100644 --- a/src/AssetTransferApi.ts +++ b/src/AssetTransferApi.ts @@ -750,7 +750,6 @@ export class AssetTransferApi { if (isValidInt) { assetId = new BN(paysWithFeeOrigin); - console.log('WHAT IS ASSET ID', assetId.toString()); const isSufficient = await this.checkAssetIsSufficient(assetId); if (!isSufficient) { @@ -773,8 +772,6 @@ export class AssetTransferApi { } } - console.log('ASSET ID', assetId?.toString()); - const lastHeader = await this._api.rpc.chain.getHeader(); const blockNumber = this._api.registry.createType('BlockNumber', lastHeader.number.toNumber()); const method = tx.method; @@ -783,8 +780,6 @@ export class AssetTransferApi { period: 64, }); - console.log('ASSET ID', assetId); - const nonce = await this._api.rpc.system.accountNextIndex(sendersAddr); const unsignedPayload: UnsignedTransaction = { specVersion: this._api.runtimeVersion.specVersion.toHex(), @@ -811,8 +806,6 @@ export class AssetTransferApi { version: tx.version, }; - console.log('UNSIGNED PAYLOAD', unsignedPayload); - const extrinsicPayload = this._api.registry.createType('ExtrinsicPayload', unsignedPayload, { version: unsignedPayload.version, }); @@ -903,33 +896,35 @@ export class AssetTransferApi { ); } - try { - const poolAssetData = await this._api.query.assetConversion.pools.entries(); - - for (let i = 0; i < poolAssetData.length; i++) { - const poolStorageKeyData = poolAssetData[i][0].toHuman(); - - // remove any commas from multilocation key values e.g. Parachain: 2,125 -> Parachain: 2125 - const poolAssetDataStr = JSON.stringify(poolStorageKeyData).replace(/(\d),/g, '$1'); + if (this._api.query.assetConversion !== undefined) { + try { + for (const poolPairsData of await this._api.query.assetConversion.pools.entries()) { + const poolStorageKeyData = poolPairsData[0]; - const palletAssetConversionNativeOrAssetIdData = sanitizeKeys( - JSON.parse(poolAssetDataStr), - ) as UnionXcmMultiLocation[][]; + // remove any commas from multilocation key values e.g. Parachain: 2,125 -> Parachain: 2125 + const poolAssetDataStr = JSON.stringify(poolStorageKeyData).replace(/(\d),/g, '$1'); + const palletAssetConversionNativeOrAssetIdData = sanitizeKeys( + JSON.parse(poolAssetDataStr), + ) as UnionXcmMultiLocation[]; - const firstLpToken = palletAssetConversionNativeOrAssetIdData[0][0]; - const secondLpToken = palletAssetConversionNativeOrAssetIdData[0][1]; + const firstLpToken = palletAssetConversionNativeOrAssetIdData[0]; + const secondLpToken = palletAssetConversionNativeOrAssetIdData[1]; - if (firstLpToken === feeAsset || secondLpToken === feeAsset) { - return [true, feeAsset]; + if ( + JSON.stringify(firstLpToken) == JSON.stringify(feeAsset) || + JSON.stringify(secondLpToken) == JSON.stringify(feeAsset) + ) { + return [true, feeAsset]; + } } + } catch (e) { + throw new BaseError( + `error querying ${this._specName} liquidity token pool assets: ${e as string}`, + BaseErrorsEnum.InternalError, + ); } - - return [false, feeAsset]; - } catch (e) { - throw new BaseError( - `error querying ${this._specName} liquidity token pool assets: ${e as string}`, - BaseErrorsEnum.InternalError, - ); } + + return [false, feeAsset]; }; } diff --git a/src/errors/checkXcmTxInputs.spec.ts b/src/errors/checkXcmTxInputs.spec.ts index 8c970425..a3c312b4 100644 --- a/src/errors/checkXcmTxInputs.spec.ts +++ b/src/errors/checkXcmTxInputs.spec.ts @@ -941,7 +941,7 @@ describe('checkParaAssets', () => { expect(registry.cacheLookupPoolAsset('0')).toEqual({ lpToken: '0', pairInfo: - '[[{"parents":"0","interior":{"Here":""}},{"parents":"0","interior":{"X2":[{"PalletInstance":"50"},{"GeneralIndex":"100"}]}}]]', + '[{"parents":"0","interior":{"Here":""}},{"parents":"0","interior":{"X2":[{"PalletInstance":"50"},{"GeneralIndex":"100"}]}}]', }); }); }); diff --git a/src/errors/checkXcmTxInputs.ts b/src/errors/checkXcmTxInputs.ts index 69c0aeb0..b70a7ebf 100644 --- a/src/errors/checkXcmTxInputs.ts +++ b/src/errors/checkXcmTxInputs.ts @@ -357,13 +357,12 @@ export const checkLiquidTokenValidity = async ( // query chain state const poolAsset = await api.query.poolAssets.asset(assetId); if (poolAsset.isSome) { - const poolAssets = await api.query.assetConversion.pools.entries(); + // const poolAssets = await api.query.assetConversion.pools.entries(); - for (let i = 0; i < poolAssets.length; i++) { - const poolAsset = poolAssets[i]; - const poolAssetData = JSON.stringify(poolAsset[0].toHuman()).replace(/(\d),/g, '$1'); + for (const poolPairsData of await api.query.assetConversion.pools.entries()) { + const poolAssetData = JSON.stringify(poolPairsData[0]).replace(/(\d),/g, '$1'); + const poolAssetInfo = poolPairsData[1].unwrap(); - const poolAssetInfo = poolAsset[1].unwrap(); if (poolAssetInfo.lpToken.toString() === assetId) { const asset: { lpToken: string; diff --git a/src/testHelpers/adjustedMockSystemApi.ts b/src/testHelpers/adjustedMockSystemApi.ts index 682b451c..ef15111f 100644 --- a/src/testHelpers/adjustedMockSystemApi.ts +++ b/src/testHelpers/adjustedMockSystemApi.ts @@ -251,9 +251,9 @@ const poolAsset = (asset: string): Promise> => const pools = ( _arg: ITuple<[PalletAssetConversionNativeOrAssetId, PalletAssetConversionNativeOrAssetId]>, -): Promise<[PalletAssetConversionNativeOrAssetId, PalletAssetConversionPoolInfo]> => +): Promise<[PalletAssetConversionNativeOrAssetId, PalletAssetConversionPoolInfo][]> => Promise.resolve().then(() => { - const palletAssetConversionNativeOrAssetId = mockSystemApi.registry.createType( + const palletAssetConversionNativeOrAssetId1 = mockSystemApi.registry.createType( 'PalletAssetConversionNativeOrAssetId', [ { parents: 0, interior: { Here: '' } }, @@ -264,11 +264,29 @@ const pools = ( ], ); - const poolInfo = mockSystemApi.registry.createType('PalletAssetConversionPoolInfo', { + const poolInfo1 = mockSystemApi.registry.createType('PalletAssetConversionPoolInfo', { lpToken: 0, }); - return [palletAssetConversionNativeOrAssetId, poolInfo]; + const palletAssetConversionNativeOrAssetId2 = mockSystemApi.registry.createType( + 'PalletAssetConversionNativeOrAssetId', + [ + { parents: 0, interior: { Here: '' } }, + { + parents: 0, + interior: { X2: [{ PalletInstance: 50 }, { GeneralIndex: 100 }] }, + }, + ], + ); + + const poolInfo2 = mockSystemApi.registry.createType('PalletAssetConversionPoolInfo', { + lpToken: 1, + }); + + return [ + [palletAssetConversionNativeOrAssetId1, poolInfo1], + [palletAssetConversionNativeOrAssetId2, poolInfo2], + ]; }); const mockApiAt = { @@ -312,15 +330,54 @@ export const adjustedMockSystemApi = { assetConversion: { pools: Object.assign(pools, { entries: () => { - const palletAssetConversionNativeOrAssetId = Object.assign( + const palletAssetConversionData = Object.assign( [ - { parents: 0, interior: { Here: '' } }, - { - parents: 0, - interior: { - X2: [{ PalletInstance: 50 }, { GeneralIndex: 100 }], - }, - }, + [ + [ + { parents: '0', interior: { Here: '' } }, + { + parents: '0', + interior: { + X2: [{ PalletInstance: '50' }, { GeneralIndex: '100' }], + }, + }, + ], + Object.assign( + { + lpToken: mockSystemApi.registry.createType('u32', 0), + }, + { + unwrap: () => { + return { + lpToken: mockSystemApi.registry.createType('u32', 0), + }; + }, + }, + ), + ], + [ + [ + { parents: '0', interior: { Here: '' } }, + { + parents: '0', + interior: { + X2: [{ PalletInstance: '50' }, { GeneralIndex: '1984' }], + }, + }, + ], + Object.assign( + { + lpToken: mockSystemApi.registry.createType('u32', 1), + }, + { + unwrap: () => { + return { + lpToken: mockSystemApi.registry.createType('u32', 1), + }; + }, + }, + ), + ], ], { toHuman: () => { @@ -334,25 +391,21 @@ export const adjustedMockSystemApi = { }, }, ], + [ + { parents: '0', interior: { Here: '' } }, + { + parents: '0', + interior: { + X2: [{ PalletInstance: '50' }, { GeneralIndex: '1984' }], + }, + }, + ], ]; }, }, ); - const poolInfo = Object.assign( - { - lpToken: mockSystemApi.registry.createType('u32', 0), - }, - { - unwrap: () => { - return { - lpToken: mockSystemApi.registry.createType('u32', 0), - }; - }, - }, - ); - - return [[palletAssetConversionNativeOrAssetId, poolInfo]]; + return palletAssetConversionData; }, }), },