@ton3/liteclient
Version:
TON Blockchain LiteClient
60 lines (48 loc) • 1.63 kB
text/typescript
import { AccountTransactionId, Transaction } from '@tonkite/core';
import { LiteApi } from '../liteapi';
import { Address, BOC, Slice } from 'ton3-core';
import { loadTransaction } from '@tonkite/types';
import { TransactionList } from '../../dataTypes/liteServer'
export class TransactionCursor implements AsyncIterable<Transaction> {
static readonly CHUNK_SIZE = 30;
constructor(
private readonly api: LiteApi,
private readonly account: Address,
private readonly offset: AccountTransactionId,
private readonly take: number,
) {}
async *[Symbol.asyncIterator](): AsyncIterator<Transaction> {
let taken = 0;
let last: AccountTransactionId | null = null;
let complete = false;
let result: TransactionList;
do {
result = await this.api.getTransactions({
account: {
workchain: this.account.workchain,
id: this.account.hash,
},
lt: last?.lt ?? this.offset.lt,
hash: last?.hash ?? this.offset.hash,
count: TransactionCursor.CHUNK_SIZE
});
const cells = BOC.from(result.transactions);
for (let cellIndex in cells) {
const block = result.ids[cellIndex];
const transaction = loadTransaction(Slice.parse(cells[cellIndex]));
yield {
block,
transaction,
};
last = transaction.id;
taken += 1;
if (transaction.previousTransaction.lt === 0n) {
// NOTE: The first transaction is reached.
complete = true;
break;
}
}
complete ||= taken >= this.take;
} while (!complete);
}
}