Skip to content

Commit

Permalink
Fix solana block by ts
Browse files Browse the repository at this point in the history
  • Loading branch information
vrtnd committed Jul 26, 2024
1 parent 9ddd4c3 commit 23c1f97
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/handlers/runAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const handler = async (event: any) => {
})
);
await promises;
console.log(`Adapters of ${bridgesToRun.map((bridge: any) => bridge.bridgeDbName).join(", ")} ran successfully`);
} catch (e) {
console.error(`Adapter ${bridgeNetworks[event.bridgeIndex].bridgeDbName} failed ${JSON.stringify(e)}`);
} finally {
Expand Down
12 changes: 6 additions & 6 deletions src/utils/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ export const runAdapterHistorical = async (
const { bridgeDbName } = bridgeNetwork;
const adapter = adapters[bridgeDbName];

console.log(`[INFO] Running adapter for ${bridgeDbName} on ${chain} from ${startBlock} to ${endBlock}.`);

const adapterChainEventsFn = adapter[chain];
if (chain?.toLowerCase() === bridgeNetwork.destinationChain?.toLowerCase() && !adapterChainEventsFn) {
console.log(`[INFO] Skipping ${bridgeDbName} on ${chain} because it is not the destination chain.`);
Expand Down Expand Up @@ -434,14 +436,12 @@ export const runAdapterHistorical = async (

const useChainBlocks = !(nonBlocksChains.includes(chainContractsAreOn) || ["ibc"].includes(bridgeDbName));
let block = startBlock;

while (block < endBlock) {
await wait(500);
const endBlockForQuery = block + maxBlocksToQuery > endBlock ? endBlock : block + maxBlocksToQuery;

let retryCount = 0;
const maxRetries = 3;

while (retryCount < maxRetries) {
try {
const eventLogs = await retry(
Expand All @@ -459,7 +459,6 @@ export const runAdapterHistorical = async (
console.log(`[INFO] No events found for ${bridgeDbName} on ${chain} from ${block} to ${endBlockForQuery}`);
block = block + maxBlocksToQuery;
if (block >= endBlock) break;
else continue;
}

let provider = undefined as any;
Expand Down Expand Up @@ -606,7 +605,7 @@ export const runAdapterHistorical = async (
{ retries: 3, factor: 2 }
).catch((e: any) => {
console.error(
`[ERROR] Failed to insert transaction row for ${bridgeDbName} on ${chain} after retries. Error: ${e.message}`
`[ERROR] Failed to insert transaction row for ${bridgeDbName} on ${chain} after retries. Error: ${e}`
);
throw e;
});
Expand All @@ -615,7 +614,9 @@ export const runAdapterHistorical = async (
},
{ retries: 3, factor: 2 }
);

console.log(
`[INFO] Inserted transactions for ${bridgeDbName} on ${chain} for blocks ${block}-${endBlockForQuery}`
);
break;
} catch (e: any) {
retryCount++;
Expand All @@ -641,7 +642,6 @@ export const runAdapterHistorical = async (
}
}
}

block = block + maxBlocksToQuery;
}
console.log(`finished inserting all transactions for ${bridgeID}`);
Expand Down
7 changes: 7 additions & 0 deletions src/utils/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,19 @@ export async function getBlockByTimestamp(
if (bridge && bridge.bridgeDbName === "ibc") {
return await ibcGetBlockFromTimestamp(bridge, timestamp, chain, position);
} else if (chain === "solana") {
const connection = getConnection();
const { timestamp: latestTimestamp, number } = await getLatestBlock(chain);
// There is not an easy way to get the slot number from a timestamp on Solana
// without hammering the RPC node with requests.
// So we estimate it by assuming that a slot is produced every 400ms.
if (timestamp <= latestTimestamp) {
const slot = number - Math.floor(((latestTimestamp - timestamp) * 1000) / 400);
const slotTs = (await connection.getBlockTime(slot)) as any;
if (Math.abs(slotTs - timestamp) > 400) {
const blocksOffset = Math.floor(((slotTs - timestamp) * 1000) / 400);

return { block: slot - blocksOffset, timestamp };
}
return { block: slot, timestamp };
}
} else {
Expand Down

0 comments on commit 23c1f97

Please sign in to comment.