Skip to content

Commit

Permalink
Merge pull request #107 from DefiLlama/solana-get-block-by-timestamp
Browse files Browse the repository at this point in the history
Solana get block by timestamp
  • Loading branch information
vrtnd authored Jan 19, 2024
2 parents 74c8615 + 81501c8 commit a5ef0c8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/utils/adapter.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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;
Expand Down
27 changes: 26 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,27 @@ 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}`);
}

export async function getTimestampBySolanaSlot(slot: number) {
const { timestamp: latestTimestamp, number } = await getLatestBlock("solana");

const timestamp = latestTimestamp - ((number - slot) * 400) / 1000;

return timestamp;
}
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 a5ef0c8

Please sign in to comment.