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

fix: 🐛 duplicate entries returned on historicalInstructions #1425

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/api/client/Settlements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import {
PolymeshError,
Venue,
} from '~/internal';
import { instructionPartiesQuery } from '~/middleware/queries/settlements';
import { historicalInstructionsQuery } from '~/middleware/queries/settlements';
import { Query } from '~/middleware/types';
import {
AddInstructionWithVenueIdParams,
CreateVenueParams,
ErrorCode,
HistoricalInstructionFilters,
HistoricInstruction,
InstructionAffirmationOperation,
InstructionIdParams,
InstructionPartiesFilters,
ProcedureMethod,
} from '~/types';
import { Ensured } from '~/types/utils';
Expand Down Expand Up @@ -134,19 +134,19 @@ export class Settlements {
*
*/
public async getHistoricalInstructions(
filter: InstructionPartiesFilters
filter: HistoricalInstructionFilters
): Promise<HistoricInstruction[]> {
const { context } = this;

const query = await instructionPartiesQuery(filter, context);
const query = await historicalInstructionsQuery(filter, context);

const {
data: {
instructionParties: { nodes: instructionsResult },
instructions: { nodes: instructionsResult },
},
} = await context.queryMiddleware<Ensured<Query, 'instructionParties'>>(query);
} = await context.queryMiddleware<Ensured<Query, 'instructions'>>(query);

return instructionsResult.map(({ instruction }) =>
return instructionsResult.map(instruction =>
middlewareInstructionToHistoricInstruction(instruction!, context)
);
}
Expand Down
10 changes: 5 additions & 5 deletions src/api/client/__tests__/Settlements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { when } from 'jest-when';

import { Settlements } from '~/api/client/Settlements';
import { addInstructionTransformer, Context, PolymeshTransaction, Venue } from '~/internal';
import { instructionPartiesQuery } from '~/middleware/queries/settlements';
import { historicalInstructionsQuery } from '~/middleware/queries/settlements';
import { dsMockUtils, entityMockUtils, procedureMockUtils } from '~/testUtils/mocks';
import { Mocked } from '~/testUtils/types';
import {
Expand Down Expand Up @@ -195,13 +195,13 @@ describe('Settlements Class', () => {
'middlewareInstructionToHistoricInstruction'
);

const legsResponse = {
const instructionsResponse = {
totalCount: 5,
nodes: [{ instruction: 'instruction' }],
nodes: [{ id: '1' }],
};

dsMockUtils.createApolloQueryMock(await instructionPartiesQuery({}, context), {
instructionParties: legsResponse,
dsMockUtils.createApolloQueryMock(await historicalInstructionsQuery({}, context), {
instructions: instructionsResponse,
});

const mockHistoricInstruction = 'mockData' as unknown as HistoricInstruction;
Expand Down
2 changes: 1 addition & 1 deletion src/api/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export type CustomClaimTypeWithDid = CustomClaimType & { did?: string };
* Filters for instructions
*
*/
export interface InstructionPartiesFilters {
export interface HistoricalInstructionFilters {
/**
* The DID of the identity to filter by
*/
Expand Down
10 changes: 5 additions & 5 deletions src/api/entities/Identity/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from '~/internal';
import { assetHoldersQuery, nftHoldersQuery } from '~/middleware/queries/assets';
import { trustingAssetsQuery } from '~/middleware/queries/claims';
import { instructionPartiesQuery } from '~/middleware/queries/settlements';
import { historicalInstructionsQuery } from '~/middleware/queries/settlements';
import { AssetHoldersOrderBy, NftHoldersOrderBy } from '~/middleware/types';
import { dsMockUtils, entityMockUtils, procedureMockUtils } from '~/testUtils/mocks';
import { MockContext } from '~/testUtils/mocks/dataSources';
Expand Down Expand Up @@ -1288,15 +1288,15 @@ describe('Identity class', () => {
'middlewareInstructionToHistoricInstruction'
);

const legsResponse = {
const instructionsResponse = {
totalCount: 5,
nodes: [{ instruction: 'instruction' }],
nodes: [{ id: '1' }],
};

const query = await instructionPartiesQuery({ identity: identity.did }, context);
const query = await historicalInstructionsQuery({ identity: identity.did }, context);

dsMockUtils.createApolloQueryMock(query, {
instructionParties: legsResponse,
instructions: instructionsResponse,
});

const mockHistoricInstruction = 'mockData' as unknown as HistoricInstruction;
Expand Down
17 changes: 8 additions & 9 deletions src/api/entities/Identity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
} from '~/internal';
import { assetHoldersQuery, nftHoldersQuery } from '~/middleware/queries/assets';
import { trustingAssetsQuery } from '~/middleware/queries/claims';
import { instructionPartiesQuery } from '~/middleware/queries/settlements';
import { historicalInstructionsQuery } from '~/middleware/queries/settlements';
import { AssetHoldersOrderBy, NftHoldersOrderBy, Query } from '~/middleware/types';
import { CddStatus } from '~/polkadot/polymesh';
import {
Expand All @@ -39,8 +39,8 @@ import {
GroupedInstructions,
GroupedInvolvedInstructions,
HeldNfts,
HistoricalInstructionFilters,
HistoricInstruction,
InstructionPartiesFilters,
InstructionsByStatus,
MultiSigSigners,
NumberedPortfolio,
Expand Down Expand Up @@ -906,21 +906,20 @@ export class Identity extends Entity<UniqueIdentifiers, string> {
*
*/
public async getHistoricalInstructions(
filter?: Omit<InstructionPartiesFilters, 'identity'>
filter?: Omit<HistoricalInstructionFilters, 'identity'>
): Promise<HistoricInstruction[]> {
const { context, did } = this;

const query = await instructionPartiesQuery({ ...filter, identity: did }, context);
const query = await historicalInstructionsQuery({ ...filter, identity: did }, context);

const {
data: {
instructionParties: { nodes: instructionsResult },
instructions: { nodes: instructionsResult },
},
} = await context.queryMiddleware<Ensured<Query, 'instructionParties'>>(query);
} = await context.queryMiddleware<Ensured<Query, 'instructions'>>(query);

return instructionsResult.map(({ instruction }) =>
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
middlewareInstructionToHistoricInstruction(instruction!, context)
return instructionsResult.map(instruction =>
middlewareInstructionToHistoricInstruction(instruction, context)
);
}

Expand Down
73 changes: 39 additions & 34 deletions src/middleware/__tests__/queries/settlements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import BigNumber from 'bignumber.js';

import { Context } from '~/internal';
import {
buildInstructionPartiesFilter,
buildHistoricalInstructionsQueryFilter,
historicalInstructionsQuery,
instructionEventsQuery,
instructionPartiesQuery,
instructionsQuery,
settlementsForAllPortfoliosQuery,
settlementsQuery,
Expand Down Expand Up @@ -46,7 +46,7 @@ describe('instructionsQuery', () => {
});
});

describe('buildInstructionPartiesFilter', () => {
describe('buildHistoricalInstructionsQueryFilter', () => {
let context: Context;

beforeEach(() => {
Expand All @@ -61,87 +61,92 @@ describe('buildInstructionPartiesFilter', () => {
});

it('should create the correct graphql filter with identity', async () => {
const result = await buildInstructionPartiesFilter({ identity: 'someDid' }, context);
const result = await buildHistoricalInstructionsQueryFilter({ identity: 'someDid' }, context);

expect(result.args).toBe('($start: Int,$size: Int,$identity: String!)');
expect(result.filter).toBe('filter: { identity: { equalTo: $identity } }');
expect(result.filter).toBe(
'filter: { parties: { some: { identity: { equalTo: $identity } } } }'
);
});

it('should create the correct graphql filter with identity as Identity object', async () => {
const did = 'someDid';
const mockIdentity = entityMockUtils.getIdentityInstance({ did });
const result = await buildInstructionPartiesFilter({ identity: mockIdentity }, context);
const result = await buildHistoricalInstructionsQueryFilter(
{ identity: mockIdentity },
context
);

expect(result.args).toBe('($start: Int,$size: Int,$identity: String!)');
expect(result.filter).toBe('filter: { identity: { equalTo: $identity } }');
expect(result.filter).toBe(
'filter: { parties: { some: { identity: { equalTo: $identity } } } }'
);
expect(result.variables.identity).toBe(did);
});

it('should create the correct graphql filter with status', async () => {
const result = await buildInstructionPartiesFilter(
const result = await buildHistoricalInstructionsQueryFilter(
{ status: InstructionStatusEnum.Executed },
context
);

expect(result.args).toBe('($start: Int,$size: Int,$status: InstructionStatusEnum!)');
expect(result.filter).toBe('filter: { instruction: { status: { equalTo: $status } } }');
expect(result.filter).toBe('filter: { status: { equalTo: $status } }');
});

it('should create the correct graphql filter with mediator', async () => {
const result = await buildInstructionPartiesFilter({ mediator: 'someMediator' }, context);
const result = await buildHistoricalInstructionsQueryFilter(
{ mediator: 'someMediator' },
context
);

expect(result.args).toBe('($start: Int,$size: Int,$mediator: String!)');
expect(result.filter).toBe(
'filter: { instruction: { mediators: { containsKey: $mediator } } }'
);
expect(result.filter).toBe('filter: { mediators: { containsKey: $mediator } }');
});

it('should create the correct graphql filter with party', async () => {
const result = await buildInstructionPartiesFilter({ party: 'someParty' }, context);
const result = await buildHistoricalInstructionsQueryFilter({ party: 'someParty' }, context);

expect(result.args).toBe('($start: Int,$size: Int,$party: String!)');
expect(result.filter).toBe(
'filter: { instruction: { parties: { some: { identity: { equalTo: $party } } } } }'
);
expect(result.filter).toBe('filter: { parties: { some: { identity: { equalTo: $party } } } }');
});

it('should create the correct graphql filter with assetId', async () => {
const result = await buildInstructionPartiesFilter({ asset: 'TICKER' }, context);
const result = await buildHistoricalInstructionsQueryFilter({ asset: 'TICKER' }, context);

expect(result.args).toBe('($start: Int,$size: Int,$asset: String!)');
expect(result.filter).toBe(
'filter: { instruction: { legs: { assetId: { equalTo: $asset } } } }'
);
expect(result.filter).toBe('filter: { legs: { some: { assetId: { equalTo: $asset } } } }');
});

it('should create the correct graphql filter with asset as Asset object', async () => {
const assetId = 'TICKE';
const mockAsset = entityMockUtils.getFungibleAssetInstance({ assetId });
const result = await buildInstructionPartiesFilter({ asset: mockAsset }, context);
const result = await buildHistoricalInstructionsQueryFilter({ asset: mockAsset }, context);

expect(result.args).toBe('($start: Int,$size: Int,$asset: String!)');
expect(result.filter).toBe(
'filter: { instruction: { legs: { assetId: { equalTo: $asset } } } }'
);
expect(result.filter).toBe('filter: { legs: { some: { assetId: { equalTo: $asset } } } }');
expect(result.variables.asset).toBe(assetId);
});

it('should create the correct graphql filter with sender', async () => {
const result = await buildInstructionPartiesFilter({ sender: 'someSender' }, context);
const result = await buildHistoricalInstructionsQueryFilter({ sender: 'someSender' }, context);

expect(result.args).toBe('($start: Int,$size: Int,$sender: String!)');
expect(result.filter).toBe('filter: { instruction: { legs: { from: { equalTo: $sender } } } }');
expect(result.filter).toBe('filter: { legs: { some: { from: { equalTo: $sender } } } }');
});

it('should create the correct graphql filter with receiver', async () => {
const result = await buildInstructionPartiesFilter({ receiver: 'someReceiver' }, context);
const result = await buildHistoricalInstructionsQueryFilter(
{ receiver: 'someReceiver' },
context
);

expect(result.args).toBe('($start: Int,$size: Int,$receiver: String!)');
expect(result.filter).toBe('filter: { instruction: { legs: { to: { equalTo: $receiver } } } }');
expect(result.filter).toBe('filter: { legs: { some: { to: { equalTo: $receiver } } } }');
});

it('should create the correct graphql filter with multiple parameters', async () => {
const result = await buildInstructionPartiesFilter(
const result = await buildHistoricalInstructionsQueryFilter(
{
identity: 'someDid',
status: InstructionStatusEnum.Executed,
Expand All @@ -155,12 +160,12 @@ describe('buildInstructionPartiesFilter', () => {
'($start: Int,$size: Int,$identity: String!,$status: InstructionStatusEnum!,$asset: String!,$sender: String!)'
);
expect(result.filter).toBe(
'filter: { identity: { equalTo: $identity }, instruction: { status: { equalTo: $status }, legs: { assetId: { equalTo: $asset }, from: { equalTo: $sender } } } }'
'filter: { status: { equalTo: $status }, legs: { some: { assetId: { equalTo: $asset }, from: { equalTo: $sender } } }, parties: { some: { identity: { equalTo: $identity } } } }'
);
});

it('should return empty filter with default pagination when no parameters are provided', async () => {
const result = await buildInstructionPartiesFilter({}, context);
const result = await buildHistoricalInstructionsQueryFilter({}, context);

expect(result.args).toBe('($start: Int,$size: Int)');
expect(result.filter).toBe('');
Expand All @@ -169,15 +174,15 @@ describe('buildInstructionPartiesFilter', () => {
});
});

describe('instructionPartiesQuery', () => {
describe('historicalInstructionsQuery', () => {
let context: Context;

beforeEach(() => {
context = dsMockUtils.getContextInstance();
});

it('should pass the variables to the grapqhl query', async () => {
const result = await instructionPartiesQuery(
const result = await historicalInstructionsQuery(
{
identity: 'someDid',
size: new BigNumber(10),
Expand Down
Loading
Loading