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

retry on not found #5

Merged
merged 2 commits into from
Apr 1, 2024
Merged
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
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@ To configure the service for your specific environment, you'll need to set the f
`NETWORK_PASSPHRASE` - Soroban passphrase\
`LIQUIDATOR_ADDRESS` - liquidator's account address\
`LIQUIDATOR_SECRET` - liquidator's secret key\
`GET_TRANSACTIONS_RETRY` - number of retries to get transaction for particular ledgers\
`GET_TRANSACTIONS_DELAY_MS` - delay before retrying
## License
This project is licensed under the MIT License - see the LICENSE file for details.
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
Loading