Skip to content

Commit

Permalink
Solana - getBlockByTimestamp support
Browse files Browse the repository at this point in the history
We estimate the block for a timestamp for Solana.
Prevents us from getting throttled by the RPC node.
  • Loading branch information
kev1n-peters committed Jan 18, 2024
1 parent 16dea02 commit 1368bf6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
19 changes: 18 additions & 1 deletion src/utils/blocks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { getLatestBlock as getLatestBlockSdk } from "@defillama/sdk/build/util";
import { getLatestBlock as getLatestBlockSdk, lookupBlock } from "@defillama/sdk/build/util";
// import { getClient } from "../helpers/sui";
import { tronGetLatestBlock } from "../helpers/tron";
import { getConnection } from "../helpers/solana";
import { Chain } from "@defillama/sdk/build/general";

export async function getLatestBlockNumber(chain: string): Promise<number> {
if (chain === "sui") {
Expand Down Expand Up @@ -35,3 +36,19 @@ export async function getLatestBlock(chain: string): Promise<{ number: number; t
}
return await getLatestBlockSdk(chain);
}

export async function getBlockByTimestamp(timestamp: number, chain: Chain) {
if (chain === "solana") {
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);
return { block: slot, timestamp };
}
} else {
return await lookupBlock(timestamp, { chain });
}
throw new Error(`Could not find block for timestamp ${timestamp} on chain ${chain}`);
}
6 changes: 3 additions & 3 deletions src/utils/runAdapterHistorical.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Chain } from "@defillama/sdk/build/general";
import { lookupBlock } from "@defillama/sdk/build/util";
import bridgeNetworkData from "../data/bridgeNetworkData";
import { wait } from "../helpers/etherscan";
import { runAdapterHistorical } from "./adapter";
import { getBlockByTimestamp } from "./blocks";

const startTs = Number(process.argv[2]);
const endTs = Number(process.argv[3]);
Expand Down Expand Up @@ -35,8 +35,8 @@ async function fillAdapterHistorical(
if (restrictChainTo && nChain !== restrictChainTo) return;
console.log(`Running adapter for ${chain} for ${bridgeDbName}`);
await wait(500 * i);
const startBlock = await lookupBlock(startTimestamp, { chain: nChain as Chain });
const endBlock = await lookupBlock(endTimestamp, { chain: nChain as Chain });
const startBlock = await getBlockByTimestamp(startTimestamp, nChain as Chain);
const endBlock = await getBlockByTimestamp(endTimestamp, nChain as Chain);
await runAdapterHistorical(
startBlock.block,
endBlock.block,
Expand Down

0 comments on commit 1368bf6

Please sign in to comment.