Skip to content

Commit

Permalink
fix: 🐛 handle createdAt order by for SQ v19+
Browse files Browse the repository at this point in the history
Fix offChainAffirmations, instructionAffirmations order by. Handle
CREATED_AT order by in getTransactionHistory, getHeldAssets and
getHeldNfts by replacing it with CREATED_BLOCK_ID order instead (which
is equivalent in SQv19+). Created at ordering is marked as deprecated
and will be removed when SQ v18 is no longer supported
  • Loading branch information
polymath-eric committed Dec 6, 2024
1 parent c8da9df commit bb0c704
Show file tree
Hide file tree
Showing 10 changed files with 15,390 additions and 16,215 deletions.
4 changes: 4 additions & 0 deletions src/api/entities/Account/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ describe('Account class', () => {

dsMockUtils.createApolloQueryMock(
extrinsicsByArgs(
false,
{
blockId: blockNumber1.toString(),
address,
Expand Down Expand Up @@ -370,6 +371,7 @@ describe('Account class', () => {

dsMockUtils.createApolloQueryMock(
extrinsicsByArgs(
false,
{
blockId: blockNumber1.toString(),
address,
Expand Down Expand Up @@ -409,6 +411,7 @@ describe('Account class', () => {

dsMockUtils.createApolloQueryMock(
extrinsicsByArgs(
false,
{
blockId: undefined,
address,
Expand Down Expand Up @@ -438,6 +441,7 @@ describe('Account class', () => {

dsMockUtils.createApolloQueryMock(
extrinsicsByArgs(
false,
{
blockId: undefined,
address,
Expand Down
1 change: 1 addition & 0 deletions src/api/entities/Account/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ export class Account extends Entity<UniqueIdentifiers, string> {
},
} = await context.queryMiddleware<Ensured<Query, 'extrinsics'>>(
extrinsicsByArgs(
context.isSqIdPadded,
{
blockId: blockNumber ? blockNumber.toString() : undefined,
address,
Expand Down
6 changes: 4 additions & 2 deletions src/api/entities/Identity/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ describe('Identity class', () => {
.calledWith({ id: assetId }, context)
.mockReturnValue(assetId)
);
dsMockUtils.createApolloQueryMock(assetHoldersQuery({ identityId: did }), {
dsMockUtils.createApolloQueryMock(assetHoldersQuery(false, { identityId: did }), {
assetHolders: {
nodes: assetIds.map(assetId => ({ asset: { id: assetId } })),
totalCount: 2,
Expand All @@ -642,6 +642,7 @@ describe('Identity class', () => {

dsMockUtils.createApolloQueryMock(
assetHoldersQuery(
false,
{ identityId: did },
new BigNumber(1),
new BigNumber(0),
Expand Down Expand Up @@ -679,7 +680,7 @@ describe('Identity class', () => {
.mockReturnValue(assetId)
);

dsMockUtils.createApolloQueryMock(nftHoldersQuery({ identityId: did }), {
dsMockUtils.createApolloQueryMock(nftHoldersQuery(false, { identityId: did }), {
nftHolders: {
nodes: assetIds.map(assetId => ({ asset: { id: assetId }, nftIds: [] })),
totalCount: 2,
Expand All @@ -693,6 +694,7 @@ describe('Identity class', () => {

dsMockUtils.createApolloQueryMock(
nftHoldersQuery(
false,
{ identityId: did },
new BigNumber(1),
new BigNumber(0),
Expand Down
7 changes: 6 additions & 1 deletion src/api/entities/Identity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ export class Identity extends Entity<UniqueIdentifiers, string> {
},
} = await context.queryMiddleware<Ensured<Query, 'assetHolders'>>(
assetHoldersQuery(
context.isSqIdPadded,
{
identityId: did,
},
Expand Down Expand Up @@ -451,6 +452,7 @@ export class Identity extends Entity<UniqueIdentifiers, string> {
},
} = await context.queryMiddleware<Ensured<Query, 'nftHolders'>>(
nftHoldersQuery(
context.isSqIdPadded,
{
identityId: did,
},
Expand Down Expand Up @@ -755,7 +757,10 @@ export class Identity extends Entity<UniqueIdentifiers, string> {
let start: BigNumber | undefined;

while (!allFetched) {
const { data, next } = await this.getHeldAssets({ size: MAX_PAGE_SIZE, start });
const { data, next } = await this.getHeldAssets({
size: MAX_PAGE_SIZE,
start,
});
start = next ? new BigNumber(next) : undefined;
allFetched = !next;
assets = [...assets, ...data];
Expand Down
8 changes: 4 additions & 4 deletions src/middleware/__tests__/queries/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ describe('assetHoldersQuery', () => {
start: 0,
};

let result = assetHoldersQuery(variables);
let result = assetHoldersQuery(false, variables);

expect(result.query).toBeDefined();
expect(result.variables).toEqual(variables);

result = assetHoldersQuery(variables, new BigNumber(1), new BigNumber(0));
result = assetHoldersQuery(false, variables, new BigNumber(1), new BigNumber(0));

expect(result.query).toBeDefined();
expect(result.variables).toEqual({
Expand All @@ -54,12 +54,12 @@ describe('nftHoldersQuery', () => {
start: 0,
};

let result = nftHoldersQuery(variables);
let result = nftHoldersQuery(false, variables);

expect(result.query).toBeDefined();
expect(result.variables).toEqual(variables);

result = nftHoldersQuery(variables, new BigNumber(1), new BigNumber(0));
result = nftHoldersQuery(false, variables, new BigNumber(1), new BigNumber(0));

expect(result.query).toBeDefined();
expect(result.variables).toEqual({
Expand Down
4 changes: 2 additions & 2 deletions src/middleware/__tests__/queries/extrinsics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('extrinsicByHash', () => {

describe('extrinsicsByArgs', () => {
it('should pass the variables to the grapqhl query', () => {
let result = extrinsicsByArgs({});
let result = extrinsicsByArgs(false, {});

expect(result.query).toBeDefined();
expect(result.variables).toEqual({ size: DEFAULT_GQL_PAGE_SIZE, start: 0 });
Expand All @@ -32,7 +32,7 @@ describe('extrinsicsByArgs', () => {
start: 0,
};

result = extrinsicsByArgs(variables);
result = extrinsicsByArgs(false, variables);

expect(result.query).toBeDefined();
expect(result.variables).toEqual(variables);
Expand Down
20 changes: 20 additions & 0 deletions src/middleware/queries/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,21 @@ export function assetQuery(
* Get asset held by a DID
*/
export function assetHoldersQuery(
isSqIdPadded: boolean,
filters: QueryArgs<AssetHolder, 'identityId'>,
size?: BigNumber,
start?: BigNumber,
orderBy = AssetHoldersOrderBy.AssetIdAsc
): QueryOptions<PaginatedQueryArgs<QueryArgs<DistributionPayment, 'distributionId'>>> {
if (isSqIdPadded) {
if (orderBy === AssetHoldersOrderBy.CreatedAtAsc) {
orderBy = AssetHoldersOrderBy.CreatedBlockIdAsc;
}
if (orderBy === AssetHoldersOrderBy.CreatedAtDesc) {
orderBy = AssetHoldersOrderBy.CreatedBlockIdDesc;
}
}

const query = gql`
query AssetHoldersQuery($identityId: String!, $size: Int, $start: Int) {
assetHolders(
Expand Down Expand Up @@ -89,11 +99,21 @@ export function assetHoldersQuery(
* Get NFTs held by a DID
*/
export function nftHoldersQuery(
isSqIdPadded: boolean,
filters: QueryArgs<NftHolder, 'identityId'>,
size?: BigNumber,
start?: BigNumber,
orderBy = NftHoldersOrderBy.AssetIdAsc
): QueryOptions<PaginatedQueryArgs<QueryArgs<NftHolder, 'identityId'>>> {
if (isSqIdPadded) {
if (orderBy === NftHoldersOrderBy.CreatedAtAsc) {
orderBy = NftHoldersOrderBy.CreatedBlockIdAsc;
}
if (orderBy === NftHoldersOrderBy.CreatedAtDesc) {
orderBy = NftHoldersOrderBy.CreatedBlockIdDesc;
}
}

const query = gql`
query NftHolderQuery($identityId: String!, $size: Int, $start: Int) {
nftHolders(
Expand Down
11 changes: 11 additions & 0 deletions src/middleware/queries/extrinsics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type ExtrinsicArgs = 'blockId' | 'address' | 'moduleId' | 'callId' | 'success';
* Get transactions
*/
export function extrinsicsByArgs(
isSqIdPadded: boolean,
filters: QueryArgs<Extrinsic, ExtrinsicArgs>,
size?: BigNumber,
start?: BigNumber,
Expand All @@ -65,6 +66,16 @@ export function extrinsicsByArgs(
callId: 'CallIdEnum',
success: 'Int',
});

if (isSqIdPadded) {
if (orderBy === ExtrinsicsOrderBy.CreatedAtAsc) {
orderBy = ExtrinsicsOrderBy.BlockIdAsc;
}
if (orderBy === ExtrinsicsOrderBy.CreatedAtDesc) {
orderBy = ExtrinsicsOrderBy.BlockIdDesc;
}
}

const query = gql`
query TransactionsQuery
${args}
Expand Down
15 changes: 13 additions & 2 deletions src/middleware/queries/settlements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ type InstructionAffirmationArgs = 'instructionId' | 'status' | 'identity' | 'isM
* Get a specific instruction within a venue for a specific event
*/
export function instructionAffirmationsQuery(
paddedIds: boolean,
filters: QueryArgs<InstructionAffirmation, InstructionAffirmationArgs>,
size?: BigNumber,
start?: BigNumber
Expand All @@ -218,6 +219,11 @@ export function instructionAffirmationsQuery(
status: 'AffirmStatusEnum',
isMediator: 'Boolean',
});

const orderBy = paddedIds
? `[${InstructionAffirmationsOrderBy.CreatedBlockIdDesc}]`
: `[${InstructionAffirmationsOrderBy.CreatedAtAsc}, ${InstructionAffirmationsOrderBy.CreatedBlockIdAsc}]`;

const query = gql`
query InstructionAffirmationsQuery
${args}
Expand All @@ -226,7 +232,7 @@ export function instructionAffirmationsQuery(
${filter}
first: $size
offset: $start
orderBy: [${InstructionAffirmationsOrderBy.CreatedAtAsc}, ${InstructionAffirmationsOrderBy.CreatedBlockIdAsc}]
orderBy: ${orderBy}
) {
totalCount
nodes {
Expand All @@ -248,16 +254,21 @@ export function instructionAffirmationsQuery(
* Get a specific instruction within a venue for a specific event
*/
export function offChainAffirmationsQuery(
paddedIds: boolean,
filters: QueryArgs<InstructionAffirmation, 'instructionId'>
): QueryOptions<QueryArgs<InstructionAffirmation, 'instructionId'>> {
const orderBy = paddedIds
? `${InstructionAffirmationsOrderBy.CreatedBlockIdAsc}`
: `${InstructionAffirmationsOrderBy.CreatedAtAsc}, ${InstructionAffirmationsOrderBy.CreatedBlockIdAsc}`;

const query = gql`
query InstructionAffirmationsQuery($instructionId: String!) {
instructionAffirmations(
filter: {
instructionId: { equalTo: $instructionId }
offChainReceiptExists: true
}
orderBy: [${InstructionAffirmationsOrderBy.CreatedAtAsc}, ${InstructionAffirmationsOrderBy.CreatedBlockIdAsc}]
orderBy: [${orderBy}]
) {
nodes {
${instructionAffirmationAttributes}
Expand Down
Loading

0 comments on commit bb0c704

Please sign in to comment.