Skip to content

Commit

Permalink
retry on not found
Browse files Browse the repository at this point in the history
  • Loading branch information
mn13 committed Apr 1, 2024
1 parent 7b81d1f commit 47201a2
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions src/sync.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,49 @@
import { CONTRACT_CREATION_LEDGER, HORIZON_URL, POOL_ID } from "./configuration";
import { Horizon, SorobanRpc, humanizeEvents, xdr } from "@stellar/stellar-sdk";
import { Horizon, NotFoundError, SorobanRpc, humanizeEvents, xdr } from "@stellar/stellar-sdk";
import { insertBorrowers, readLastSyncedLedger, updateLastSyncedLedger } from "./infrastructure/db/domain";
import { delay } from "./infrastructure/soroban/contracts";

export const populateDbWithBorrowers = async (rpc: SorobanRpc.Server) => {
let lastLedger = (await rpc.getLatestLedger()).sequence;
const lastSyncedLedger = await readLastSyncedLedger();

if (lastLedger > lastSyncedLedger) {
const horizon = new Horizon.Server(HORIZON_URL);
let retries = parseInt(process.env.GET_TRANSACTIONS_RETRY, 10);
let currentLedger = lastSyncedLedger === 0 ? parseInt(CONTRACT_CREATION_LEDGER.toString()) : lastSyncedLedger + 1;

console.log(`Sync from: ${currentLedger} to: ${lastLedger}`);

while (lastLedger > currentLedger) {
const transactions = await horizon.transactions().forLedger(currentLedger).call();

for (const tx of transactions.records) {
const xdrMeta = xdr.TransactionMeta.fromXDR(tx.result_meta_xdr, "base64").v3().sorobanMeta();

if (!xdrMeta || !xdrMeta.events())
try {
const transactions = await horizon.transactions().forLedger(currentLedger).call();
for (const tx of transactions.records) {
const xdrMeta = xdr.TransactionMeta.fromXDR(tx.result_meta_xdr, "base64").v3().sorobanMeta();

if (!xdrMeta || !xdrMeta.events())
continue;

const events = humanizeEvents(xdrMeta.events())
.filter(e => e.contractId === POOL_ID && e.topics[0] === 'borrow');
const borrower = events.map(e => e.topics[1]);

await insertBorrowers(borrower);
}

await updateLastSyncedLedger(currentLedger);
} catch (e) {
if (e instanceof NotFoundError) {
retries -= 1;
if (retries === 0) {
throw e;
}
await delay(process.env.GET_TRANSACTIONS_DELAY_MS);
continue;

const events = humanizeEvents(xdrMeta.events())
.filter(e => e.contractId === POOL_ID && e.topics[0] === 'borrow');
const borrower = events.map(e => e.topics[1]);

await insertBorrowers(borrower);
} else {
throw e;
}
}

await updateLastSyncedLedger(currentLedger);

currentLedger += 1;
lastLedger = (await rpc.getLatestLedger()).sequence;
}
Expand Down

0 comments on commit 47201a2

Please sign in to comment.