Skip to content

Commit

Permalink
add fallback to getting gnosis blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
ribeirojose committed Sep 20, 2023
1 parent b5824fb commit c45b95f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { NetworkChainId } from "@bleu-balancer-tools/utils";

import { blocks } from "#/lib/gql/server";

const MINUTE_IN_SECONDS = 60;

const GNOSIS_API_EXPLORER = (timestamp: number): string =>
`https://api.gnosisscan.io/api?module=block&action=getblocknobytime&timestamp=${timestamp}&closest=before`;

async function bestGuess(chain: number, timestamp: number): Promise<number> {
if (chain !== 100) {
return 0;
}
const url = GNOSIS_API_EXPLORER(timestamp);
const response = await fetch(url);
const data = await response.json();
return parseInt(data.result);
}

function getTimestamps(time: Date): Record<string, string> {
const timestamp_gte = (time.getTime() / 1000).toString();
const timestamp_lt = (
time.getTime() / 1000 +
MINUTE_IN_SECONDS * 10
).toString();
return { timestamp_gte, timestamp_lt };
}

export default async function getBlockNumberByTimestamp(
chain: number,
endTime: Date,
): Promise<number> {
if (endTime > new Date()) {
return -1;
}

const timestamps = getTimestamps(endTime);

if (chain === NetworkChainId.GNOSIS) {
return bestGuess(chain, parseInt(timestamps.timestamp_gte));
}

const data = await blocks.gql(String(chain)).Blocks({
timestamp_gte: timestamps.timestamp_gte,
timestamp_lt: timestamps.timestamp_lt,
});

if (data.blocks.length > 0) {
return parseInt(data.blocks[0].number);
}

return 0;
}
28 changes: 13 additions & 15 deletions apps/balancer-tools/src/app/apr/(utils)/tokenYield.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { Address, networkFor, networkIdFor } from "@bleu-balancer-tools/utils";
import { zeroAddress } from "viem";

import { withCache } from "#/lib/cache";
import { blocks, pools } from "#/lib/gql/server";
import { pools } from "#/lib/gql/server";

import { ChainName, publicClients } from "./chainsPublicClients";
import getBlockNumberByTimestamp from "./getBlockNumberByTimestamp";
import { manualPoolsRateProvider } from "./poolsRateProvider";
import { vunerabilityAffecteRateProviders } from "./vunerabilityAffectedPool";

Expand Down Expand Up @@ -44,7 +45,7 @@ export const getPoolTokensAprForDate = withCache(
address: tokenAddress,
symbol,
yield: await getAPRFromRateProviderInterval(
rateProviderAddress,
rateProviderAddress as Address,
date - SECONDS_IN_DAY,
date,
chainName,
Expand Down Expand Up @@ -117,7 +118,7 @@ const getPoolTokensRateProviders = withCache(
chain: string,
poolId: Address,
): Promise<
{ address: Address; token: { address: Address; symbol: string } }[]
{ address: string; token: { address: string; symbol: string } }[]
> {
const data = await pools.gql(String(chain)).PoolRateProviders({ poolId });

Expand Down Expand Up @@ -157,20 +158,17 @@ const getIntervalRates = withCache(async function getIntervalRatesFn(
timeEnd: number,
chainName: ChainName,
) {
const [dataStart, dataEnd] = await Promise.all([
blocks.gql(String(networkIdFor(chainName))).Blocks({
timestamp_gte: timeStart,
timestamp_lt: timeEnd,
}),
blocks.gql(String(networkIdFor(chainName))).Blocks({
timestamp_gte: timeEnd,
timestamp_lt: timeEnd + SECONDS_IN_DAY,
}),
const [blockStart, blockEnd] = await Promise.all([
getBlockNumberByTimestamp(
parseInt(networkIdFor(chainName)),
new Date(timeStart),
),
getBlockNumberByTimestamp(
parseInt(networkIdFor(chainName)),
new Date(timeEnd + SECONDS_IN_DAY),
),
]);

const blockStart = dataStart.blocks[0]?.number;
const blockEnd = dataEnd.blocks[0]?.number;

if (blockStart === undefined || blockEnd === undefined) {
// eslint-disable-next-line no-console
console.error(
Expand Down

0 comments on commit c45b95f

Please sign in to comment.