From 1368bf68bac647d8afeab1951f42002c5e5b5414 Mon Sep 17 00:00:00 2001 From: Kevin Peters Date: Thu, 18 Jan 2024 15:46:52 -0600 Subject: [PATCH 1/2] Solana - getBlockByTimestamp support We estimate the block for a timestamp for Solana. Prevents us from getting throttled by the RPC node. --- src/utils/blocks.ts | 19 ++++++++++++++++++- src/utils/runAdapterHistorical.ts | 6 +++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/utils/blocks.ts b/src/utils/blocks.ts index 202f8a32..6e109d8f 100644 --- a/src/utils/blocks.ts +++ b/src/utils/blocks.ts @@ -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 { if (chain === "sui") { @@ -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}`); +} diff --git a/src/utils/runAdapterHistorical.ts b/src/utils/runAdapterHistorical.ts index 10958938..adb447ab 100644 --- a/src/utils/runAdapterHistorical.ts +++ b/src/utils/runAdapterHistorical.ts @@ -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]); @@ -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, From 81501c8ffd909045b347e0d6dac52043c8a596b7 Mon Sep 17 00:00:00 2001 From: vrtnd Date: Fri, 19 Jan 2024 16:18:08 +0700 Subject: [PATCH 2/2] Get block ts by block number on solana --- src/utils/adapter.ts | 5 ++++- src/utils/blocks.ts | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/utils/adapter.ts b/src/utils/adapter.ts index f8ca6152..344af8be 100644 --- a/src/utils/adapter.ts +++ b/src/utils/adapter.ts @@ -1,4 +1,4 @@ -import { getLatestBlockNumber } from "./blocks"; +import { getLatestBlockNumber, getTimestampBySolanaSlot } from "./blocks"; import { Chain } from "@defillama/sdk/build/general"; import { sql } from "./db"; import { getBridgeID } from "./wrappa/postgres/query"; @@ -438,6 +438,9 @@ export const runAdapterHistorical = async ( blockTimestamps[i] = block.timestamp; break; } + } else if (chain === "solana") { + blockTimestamps[i] = await getTimestampBySolanaSlot(blockNumber); + break; } else { blockTimestamps[i] = currentTimestamp; break; diff --git a/src/utils/blocks.ts b/src/utils/blocks.ts index 6e109d8f..26a77b95 100644 --- a/src/utils/blocks.ts +++ b/src/utils/blocks.ts @@ -52,3 +52,11 @@ export async function getBlockByTimestamp(timestamp: number, chain: Chain) { } throw new Error(`Could not find block for timestamp ${timestamp} on chain ${chain}`); } + +export async function getTimestampBySolanaSlot(slot: number) { + const { timestamp: latestTimestamp, number } = await getLatestBlock("solana"); + + const timestamp = latestTimestamp - ((number - slot) * 400) / 1000; + + return timestamp; +}