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
Changes from 1 commit
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
Next Next commit
wip
jprudent committed Jan 10, 2025
commit 329e5b384b48d1dacbd6fe5e2f1590847a7a4c90
1 change: 1 addition & 0 deletions libs/coin-modules/coin-tezos/package.json
Original file line number Diff line number Diff line change
@@ -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"
},
48 changes: 46 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,
@@ -65,5 +66,48 @@ 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 operations(address: string, { limit, start }: Pagination) {
if (start) {
// dump from height mode
async function fetchNextPage(state: PaginationState): Promise<PaginationState> {
const [operations, apiNextCursor] = await listOperations(address, {
limit: limit,
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: 100,
heightLimit: start,
continueIterations: true,
accumulator: [],
};

let state = await fetchNextPage(firstState);
while (state.continueIterations) {
state = await fetchNextPage(state);
}
state.accumulator;
} else {
// pagination mode
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
@@ -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,
];
}