Skip to content

Commit

Permalink
interactions loader - previous sk instead of offset
Browse files Browse the repository at this point in the history
  • Loading branch information
Tadeuchi committed Dec 20, 2023
1 parent fdb7f29 commit 330e583
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/__tests__/unit/gateway-interactions.loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('WarpGatewayInteractionsLoader -> load', () => {
it('should be called with correct params', async () => {
const loader = getLoader();
await loader.load(contractId, fromBlockHeight, toBlockHeight);
expect(fetchMock).toBeCalledWith(`${baseUrl}&page=1&fromSdk=true`);
expect(fetchMock).toBeCalledWith(`${baseUrl}&fromSdk=true`);
});
it('should be called accordingly to the amount of pages', async () => {
const fetchMock = jest.spyOn(global, 'fetch').mockImplementation(
Expand All @@ -110,7 +110,7 @@ describe('WarpGatewayInteractionsLoader -> load', () => {
);
const loader = getLoader();
await loader.load(contractId, fromBlockHeight, toBlockHeight);
expect(fetchMock).toBeCalledWith(`${baseUrl}&page=1&fromSdk=true`);
expect(fetchMock).toBeCalledWith(`${baseUrl}&fromSdk=true`);
/*expect(fetchMock).toBeCalledWith(`${baseUrl}&page=2&fromSdk=true`);
expect(fetchMock).toBeCalledWith(`${baseUrl}&page=3&fromSdk=true`);
expect(fetchMock).toBeCalledWith(`${baseUrl}&page=4&fromSdk=true`);
Expand All @@ -120,12 +120,12 @@ describe('WarpGatewayInteractionsLoader -> load', () => {
it('should be called with confirmationStatus set to "confirmed"', async () => {
const loader = getLoader({ confirmed: true });
await loader.load(contractId, fromBlockHeight, toBlockHeight);
expect(fetchMock).toBeCalledWith(`${baseUrl}&page=1&fromSdk=true&confirmationStatus=confirmed`);
expect(fetchMock).toBeCalledWith(`${baseUrl}&fromSdk=true&confirmationStatus=confirmed`);
});
it('should be called with confirmationStatus set to "not_corrupted"', async () => {
const loader = getLoader({ notCorrupted: true });
await loader.load(contractId, fromBlockHeight, toBlockHeight);
expect(fetchMock).toBeCalledWith(`${baseUrl}&page=1&fromSdk=true&confirmationStatus=not_corrupted`);
expect(fetchMock).toBeCalledWith(`${baseUrl}&fromSdk=true&confirmationStatus=not_corrupted`);
});
it('should throw an error in case of timeout', async () => {
jest.spyOn(global, 'fetch').mockImplementation(() => Promise.reject({ status: 504, ok: false }));
Expand Down
5 changes: 3 additions & 2 deletions src/core/modules/impl/WarpGatewayInteractionsLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class WarpGatewayInteractionsLoader implements InteractionsLoader {
this.logger.debug('Loading interactions: for ', { contractId, fromSortKey, toSortKey });

const interactions: GQLNodeInterface[] = [];
let page = 0;
let page = 1;
let limit = 0;
let items = 0;
const pagesPerBatch = evaluationOptions?.transactionsPagesPerBatch || Number.MAX_SAFE_INTEGER;
Expand All @@ -97,7 +97,6 @@ export class WarpGatewayInteractionsLoader implements InteractionsLoader {
...(this._warp.whoAmI ? { client: this._warp.whoAmI } : ''),
...(fromSortKey ? { from: fromSortKey } : ''),
...(toSortKey ? { to: toSortKey } : ''),
page: (++page).toString(),
fromSdk: 'true',
...(this.confirmationStatus && this.confirmationStatus.confirmed
? { confirmationStatus: 'confirmed' }
Expand All @@ -114,6 +113,8 @@ export class WarpGatewayInteractionsLoader implements InteractionsLoader {
interactions.push(...response.interactions);
limit = response.paging.limit;
items = response.paging.items;
fromSortKey = interactions[interactions.length - 1]?.sortKey;
page++;

this.logger.debug(`Loaded interactions length: ${interactions.length}, from: ${fromSortKey}, to: ${toSortKey}`);
} while (items == limit && page < pagesPerBatch); // note: items < limit means that we're on the last page
Expand Down

0 comments on commit 330e583

Please sign in to comment.