Skip to content

Commit

Permalink
feat: fix twa reserves query
Browse files Browse the repository at this point in the history
  • Loading branch information
Space-Bean committed Jun 21, 2024
1 parent 0f4690d commit 298d994
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions projects/dex-ui/src/wells/useMultiFlowPumpTWAReserves.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import { config } from "src/utils/wagmi/config";

export const useMultiFlowPumpTWAReserves = () => {
const { data: wells } = useWells();
const { getIsMultiPumpWell } = useBeanstalkSiloWhitelist();
const { getIsMultiPumpWell, getIsWhitelisted } = useBeanstalkSiloWhitelist();
const sdk = useSdk();

const query = useQuery({
queryKey: ["wells", "multiFlowPumpTWAReserves"],

queryFn: async () => {
const whitelistedWells = (wells || []).filter((well) => getIsMultiPumpWell(well));
const whitelistedWells = (wells || []).filter((well) => getIsMultiPumpWell(well) && getIsWhitelisted(well) );

const [{ timestamp: seasonTimestamp }, ...wellOracleSnapshots] = await Promise.all([
sdk.contracts.beanstalk.time(),
Expand All @@ -42,24 +42,23 @@ export const useMultiFlowPumpTWAReserves = () => {
const twaReservesResult: any[] = await multicall(config, { contracts: calls });

const mapping: Record<string, TokenValue[]> = {};
let index = 0;

whitelistedWells.forEach((well) => {
const twa = [TokenValue.ZERO, TokenValue.ZERO];
const numPumps = well.pumps?.length || 1;

well.pumps?.forEach((_pump) => {
const twaResult = twaReservesResult[index];
well.pumps?.forEach((_pump, index) => {
const indexedResult = twaReservesResult[index];
if (indexedResult.error) return;

const reserves = indexedResult?.result?.[0]
const token1 = well.tokens?.[0];
const token2 = well.tokens?.[1];

const reserves = twaResult["twaReserves"];

if (token1 && token2 && reserves.length === 2) {
if (token1 && token2 && reserves.length === 2 && reserves.length === 2) {
twa[0] = twa[0].add(TokenValue.fromBlockchain(reserves[0], token1.decimals));
twa[1] = twa[1].add(TokenValue.fromBlockchain(reserves[1], token2.decimals));
}
index += 1;
});

/// In case there is more than one pump, divide the reserves by the number of pumps
Expand Down

0 comments on commit 298d994

Please sign in to comment.