Skip to content

Commit

Permalink
feat: add bnjs devtools utils + remove logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Space-Bean committed Sep 16, 2024
1 parent 8c466f7 commit 25444dc
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 22 deletions.
4 changes: 3 additions & 1 deletion projects/ui/src/components/App/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ import MorningFieldUpdater from '~/state/beanstalk/field/morning';
import BeanstalkCaseUpdater from '~/state/beanstalk/case/updater';
import useChainState from '~/hooks/chain/useChainState';
import EthMainnet from '~/pages/mainnet';
import { runOnDev } from '~/util/dev';
// import Snowflakes from './theme/winter/Snowflakes';

BigNumber.set({ EXPONENTIAL_AT: [-12, 20] });

runOnDev();

const CustomToaster: FC<{ navHeight: number }> = ({ navHeight }) => (
<Toaster
containerStyle={{
Expand Down Expand Up @@ -150,7 +153,6 @@ function Arbitrum() {
{/* -----------------------
* Bean Updaters
* ----------------------- */}
{/* price contract not working */}
<PoolsUpdater />
<UnripeUpdater />

Expand Down
36 changes: 25 additions & 11 deletions projects/ui/src/hooks/beanstalk/useAvgSeedsPerBDV.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { BigNumber } from 'bignumber.js';
import { useCallback, useEffect, useState } from 'react';
import { DocumentNode, gql } from '@apollo/client';
import { BeanstalkSDK, Token } from '@beanstalk/sdk';
import { BeanstalkSDK } from '@beanstalk/sdk';
import { Time, Range } from 'lightweight-charts';
import * as LegacyToken from '~/constants/tokens';

import { ChartQueryData } from '~/components/Analytics/AdvancedChart';
import useSdk from '~/hooks/sdk';
import { apolloClient } from '~/graph/client';
import { toBNWithDecimals } from '~/util';
import { toBNWithDecimals, tokenIshEqual } from '~/util';
import { ZERO_BN } from '~/constants';
import { TokenInstance } from './useTokens';

type SeasonMap<T> = { [season: number]: T };

Expand Down Expand Up @@ -53,9 +55,10 @@ const apolloFetch = async (
variables: { first, season_lte: season },
fetchPolicy: 'no-cache',
notifyOnNetworkStatusChange: true,
// BS3TODO: Fix me to include L2 silo whitelist
context: { subgraph: 'beanstalk_eth' },
});

// BS3TODO: Fix me to include L1 silo whitelist
// Main hook with improved error handling and performance
const useAvgSeedsPerBDV = (
range: Range<Time> | undefined,
Expand All @@ -82,8 +85,12 @@ const useAvgSeedsPerBDV = (
setError(false);
setLoading(true);
console.debug('[useAvgSeedsPerBDV/fetch]: fetching...');

const tokens = [sdk.tokens.BEAN, ...sdk.tokens.wellLP];
const tokens = [
LegacyToken.BEAN[1],
LegacyToken.BEAN_ETH_WELL_LP[1],
LegacyToken.BEAN_WSTETH_WELL_LP[1],
];
// const tokens = [sdk.tokens.BEAN, ...sdk.tokens.wellLP];
const document = createMultiTokenQuery(
sdk.contracts.beanstalk.address,
tokens
Expand All @@ -107,15 +114,19 @@ const useAvgSeedsPerBDV = (

await fetchData(earliestSeason);

console.log('output: ', output);

if (numQueries > 1) {
const seasons: number[] = [];
const _seasons: number[] = [];
for (let i = 0; i < numQueries - 1; i += 1) {
const offset = (i + 1) * MAX_DATA_PER_QUERY - MAX_DATA_PER_QUERY;
const season_lte = Math.max(0, earliestSeason - offset);
if (season_lte < SEED_GAUGE_DEPLOYMENT_SEASON) break;
seasons.push(season_lte);
_seasons.push(season_lte);
}

const seasons = _seasons.filter(Boolean);

await Promise.all(seasons.map((season) => fetchData(season)));
}

Expand Down Expand Up @@ -160,7 +171,10 @@ function getNumQueries(range: Range<Time> | undefined): number {
return Math.ceil(numSeasons / MAX_DATA_PER_QUERY);
}

function createMultiTokenQuery(beanstalkAddress: string, tokens: Token[]) {
function createMultiTokenQuery(
beanstalkAddress: string,
tokens: TokenInstance[]
) {
const queryParts = tokens.map(
(token) => `seasonsSA_${token.symbol}: siloAssetHourlySnapshots(
first: $first
Expand Down Expand Up @@ -211,7 +225,7 @@ function createMultiTokenQuery(beanstalkAddress: string, tokens: Token[]) {
}

function processTokenData(
token: Token,
token: TokenInstance,
sData: SiloAssetsReturn | null,
wData: WhitelistReturn | null,
output: SiloTokenDataBySeason,
Expand Down Expand Up @@ -249,7 +263,7 @@ function processTokenData(
function parseResult(
data: any,
sdk: BeanstalkSDK,
tokens: Token[],
tokens: TokenInstance[],
output: SiloTokenDataBySeason
) {
let earliestSeason = Infinity;
Expand All @@ -262,7 +276,7 @@ function parseResult(

// Results are sorted in desc order.
// Find earliest season from the BEAN dataset
if (token.equals(sdk.tokens.BEAN)) {
if (tokenIshEqual(token, sdk.tokens.BEAN)) {
earliestSeason = Math.max(
siloAssets[siloAssets.length - 1].season,
whitelisted[whitelisted.length - 1].season
Expand Down
10 changes: 0 additions & 10 deletions projects/ui/src/hooks/beanstalk/useDataFeedTokenPrices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,6 @@ export default function useDataFeedTokenPrices() {
}
});

console.log(
'Oracle Result: ',
Object.fromEntries(
Object.entries(priceDataCache).map(([key, value]) => [
key,
value.toNumber(),
])
)
);

console.debug(
`[beanstalk/tokenPrices/useCrvUnderlyingPrices] RESULT:`,
priceDataCache
Expand Down
28 changes: 28 additions & 0 deletions projects/ui/src/util/dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import BN from 'bignumber.js';

interface WindowExtended extends Window {
devtoolsFormatters?: Array<{
header: (obj: any) => (string | null) | (Node[] | null);
hasBody: (obj: any) => boolean;
}>;
}

export const runOnDev = () => {
if (!import.meta.env.VITE_SHOW_DEV_CHAINS) return;

// useful in logging BigNumberJS objects in the console.
if (typeof window !== 'undefined') {
if (!(window as WindowExtended)?.devtoolsFormatters) {
(window as WindowExtended).devtoolsFormatters = [];
}
(window as WindowExtended).devtoolsFormatters?.push({
header: (obj: any) => {
if (BN.isBigNumber(obj)) {
return ['div', {}, obj.toString()] as Node[];
}
return null;
},
hasBody: () => false,
});
}
};

0 comments on commit 25444dc

Please sign in to comment.