Skip to content

Commit

Permalink
add check to ensure paysWithFeeOrigin multilocation is part of an exi…
Browse files Browse the repository at this point in the history
…sting liquidty pool

adds tests
  • Loading branch information
marshacb committed Dec 15, 2023
1 parent bce5743 commit 4dd6a0e
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 67 deletions.
9 changes: 3 additions & 6 deletions src/AssetTransferApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -712,15 +712,16 @@ 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',
'0xf5d5714c084c112843aca74f8c498da06cc5a2d63153b825189baa51043b1f0b',
['1984', 'usdc'],
['5000000', '4000000000'],
{
paysWithFeeOrigin: '1984',
paysWithFeeOrigin:
'{"parents": "0", "interior": {"X2": [{"PalletInstance": "50"},{"GeneralIndex": "1984"}]}}',
format: 'payload',
keepAlive: true,
paysWithFeeDest: 'USDC',
Expand All @@ -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);
});

Expand Down
53 changes: 24 additions & 29 deletions src/AssetTransferApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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(),
Expand All @@ -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,
});
Expand Down Expand Up @@ -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];
};
}
2 changes: 1 addition & 1 deletion src/errors/checkXcmTxInputs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"}]}}]',
});
});
});
Expand Down
9 changes: 4 additions & 5 deletions src/errors/checkXcmTxInputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
105 changes: 79 additions & 26 deletions src/testHelpers/adjustedMockSystemApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,9 @@ const poolAsset = (asset: string): Promise<Option<PalletAssetsAssetDetails>> =>

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: '' } },
Expand All @@ -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 = {
Expand Down Expand Up @@ -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: () => {
Expand All @@ -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;
},
}),
},
Expand Down

0 comments on commit 4dd6a0e

Please sign in to comment.