Skip to content

Commit

Permalink
get solana latest block
Browse files Browse the repository at this point in the history
  • Loading branch information
vrtnd committed Jul 10, 2024
1 parent 814932d commit 725e96e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
13 changes: 10 additions & 3 deletions src/utils/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getLatestBlockNumber, getTimestampBySolanaSlot } from "./blocks";
import { getLatestBlock, 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 @@ -403,6 +403,7 @@ export const runAdapterHistorical = async (
const useChainBlocks = !(nonBlocksChains.includes(chainContractsAreOn) || ["ibc"].includes(bridgeDbName));
let block = startBlock;
console.log(`Searching for transactions for ${bridgeDbName} on ${chain} from ${startBlock} to ${endBlock}.`);

while (block < endBlock) {
await wait(500);
const endBlockForQuery = block + maxBlocksToQuery > endBlock ? endBlock : block + maxBlocksToQuery;
Expand All @@ -412,7 +413,7 @@ export const runAdapterHistorical = async (
{ retries: 4, factor: 2 }
);

if (eventLogs.length === 0) {
if (!eventLogs || eventLogs?.length === 0) {
console.log(`No transactions found for ${bridgeID} (${bridgeDbName}-${chain}) from ${block} to ${endBlock}.`);
block = block + maxBlocksToQuery;
continue;
Expand Down Expand Up @@ -447,6 +448,12 @@ export const runAdapterHistorical = async (
// in order to reduce number of getBlock calls
let blockTimestamps = {} as { [bucket: number]: number };
let block = {} as { timestamp: number; number: number };

let latestSolanaBlock = null;
if (chain === "solana") {
latestSolanaBlock = await getLatestBlock("solana");
}
console.log(2);
for (let i = 0; i < 10; i++) {
const blockNumber = Math.floor(minBlock + i * (blockRange / 10));
for (let j = 0; j < 4; j++) {
Expand All @@ -461,7 +468,7 @@ export const runAdapterHistorical = async (
break;
}
} else if (chain === "solana") {
blockTimestamps[i] = await getTimestampBySolanaSlot(blockNumber);
blockTimestamps[i] = await getTimestampBySolanaSlot(blockNumber, latestSolanaBlock);
break;
} else {
blockTimestamps[i] = currentTimestamp;
Expand Down
22 changes: 13 additions & 9 deletions src/utils/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { getConnection } from "../helpers/solana";
import { Chain } from "@defillama/sdk/build/general";
import fetch from "node-fetch";
import { BridgeNetwork } from "../data/types";
import { getLatestBlockHeightForZoneFromMoz, getLatestBlockForZoneFromMoz, ibcGetBlockFromTimestamp } from "../adapters/ibc";
import {
getLatestBlockHeightForZoneFromMoz,
getLatestBlockForZoneFromMoz,
ibcGetBlockFromTimestamp,
} from "../adapters/ibc";
const retry = require("async-retry");

export async function getLatestBlockNumber(chain: string, bridge?: string): Promise<number> {
Expand Down Expand Up @@ -69,7 +73,7 @@ export async function getLatestBlock(chain: string, bridge?: string): Promise<{
} else if (bridge && bridge === "ibc") {
return await getLatestBlockForZoneFromMoz(chain);
}

const timestamp = Math.floor(Date.now() / 1000) - 60;
return await lookupBlock(timestamp, { chain });
}
Expand All @@ -79,13 +83,10 @@ export async function getBlockByTimestamp(
chain: Chain,
bridge?: BridgeNetwork,
position?: "First" | "Last"
)
{
) {
if (bridge && bridge.bridgeDbName === "ibc") {
return await ibcGetBlockFromTimestamp(bridge, timestamp, chain, position);
}

else if (chain === "solana") {
} else 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.
Expand All @@ -100,8 +101,11 @@ export async function getBlockByTimestamp(
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");
export async function getTimestampBySolanaSlot(
slot: number,
latestBlock?: { number: number; timestamp: number } | null
) {
const { timestamp: latestTimestamp, number } = latestBlock ? latestBlock : await getLatestBlock("solana");

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

Expand Down
4 changes: 2 additions & 2 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ export const maxBlocksToQueryByChain = {
celo: 1200,
klaytn: 6000,
sui: 2400, // sui creates a checkpoint about every 3 seconds
solana: 18000, // solana produces slots every 400ms, so 2 hours is 18000 slots
solana: 18000, // solana produces slots every 400ms, so 2 hours is 18000 slotsб
"tko-mainnet": 100,
} as { [chain: string]: number };


// will be handled by the bridge adapter
export const nonBlocksChains: string[] = [];

Expand Down

0 comments on commit 725e96e

Please sign in to comment.