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 tezos pagination #8875

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions libs/coin-modules/coin-tezos/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
"lint": "eslint ./src --no-error-on-unmatched-pattern --ext .ts,.tsx --cache",
"lint:fix": "pnpm lint --fix",
"test": "jest",
"test-watch": "jest --watch",
"test-integ": "jest --config=jest.integ.config.js",
"unimported": "unimported"
},
Expand Down
38 changes: 38 additions & 0 deletions libs/coin-modules/coin-tezos/src/api/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { createApi } from "./index";

const mockGetTransactions = jest.fn();
jest.mock("../logic/listOperations", () => ({
listOperations: () => mockGetTransactions(),
}));

describe("get operations", () => {
it("operations", async () => {
const api = createApi({
baker: {
url: "https://baker.example.com",
},
explorer: {
url: "foo",
maxTxQuery: 1,
},
node: {
url: "bar",
},
fees: {
minGasLimit: 1,
minRevealGasLimit: 1,
minStorageLimit: 1,
minFees: 1,
minEstimatedFees: 2,
},
});

mockGetTransactions.mockResolvedValue([]);

// When
const operations = await api.listOperations("addr", { limit: 100 });

// Then
expect(operations).toEqual([]);
});
});
52 changes: 50 additions & 2 deletions libs/coin-modules/coin-tezos/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
IncorrectTypeError,
Operation,
Pagination,
type Api,
type Transaction as ApiTransaction,
Expand Down Expand Up @@ -65,5 +66,52 @@ async function estimate(addr: string, amount: bigint): Promise<bigint> {
return estimatedFees.estimatedFees;
}

const operations = (address: string, { limit, start }: Pagination) =>
listOperations(address, { limit, lastId: start });
type PaginationState = {
pageSize: number;
heightLimit: number;
continueIterations: boolean;
apiNextCursor?: number;
accumulator: Operation[];
};

async function operationsFromHeight(
address: string,
start: number,
): Promise<[Operation[], number]> {
async function fetchNextPage(state: PaginationState): Promise<PaginationState> {
const [operations, apiNextCursor] = await listOperations(address, {
limit: state.pageSize,
lastId: state.apiNextCursor,
});
const filteredOperations = operations.filter(op => op.block.height >= state.heightLimit);
const isTruncated = operations.length !== filteredOperations.length;
const continueIteration = !(apiNextCursor === -1 || isTruncated);
const accumulated = state.accumulator.concat(filteredOperations);
return {
...state,
continueIterations: continueIteration,
apiNextCursor: apiNextCursor,
accumulator: accumulated,
};
}

const firstState: PaginationState = {
pageSize: 1,
heightLimit: start,
continueIterations: true,
accumulator: [],
};

let state = await fetchNextPage(firstState);
while (state.continueIterations) {
state = await fetchNextPage(state);
}
return [state.accumulator, state.apiNextCursor ?? 0];
}

async function operations(
address: string,
{ limit, start }: Pagination,
): Promise<[Operation[], number]> {
return start ? operationsFromHeight(address, start) : listOperations(address, { limit: limit });
}
10 changes: 8 additions & 2 deletions libs/coin-modules/coin-tezos/src/logic/listOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@ export async function listOperations(
address: string,
{ lastId, limit }: { lastId?: number; limit?: number },
): Promise<[Operation[], number]> {
const operations = await tzkt.getAccountOperations(address, { lastId, limit });
const operations = await tzkt.getAccountOperations(address, {
lastId: lastId,
sort: 1,
limit: limit,
});
const lastOperation = operations.slice(-1)[0];
const nextLastId = lastOperation?.id ?? -1;
return [
operations
.filter(op => isAPITransactionType(op) || isAPIDelegationType(op))
.reduce((acc, op) => acc.concat(convertOperation(address, op)), [] as Operation[]),
operations.slice(-1)[0].id,
nextLastId,
];
}

Expand Down
Loading