Skip to content

Commit

Permalink
ok pour api
Browse files Browse the repository at this point in the history
  • Loading branch information
jprudent committed Jan 10, 2025
1 parent 329e5b3 commit cb1be4f
Showing 1 changed file with 37 additions and 33 deletions.
70 changes: 37 additions & 33 deletions libs/coin-modules/coin-tezos/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,40 +74,44 @@ type PaginationState = {
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: [],
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);
}
state.accumulator;
} else {
// pagination mode
listOperations(address, { limit: limit });
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 });
}

0 comments on commit cb1be4f

Please sign in to comment.