Skip to content

Commit

Permalink
feat: fix pool deltab
Browse files Browse the repository at this point in the history
  • Loading branch information
Space-Bean committed Dec 13, 2024
1 parent db89ccf commit 16a6650
Showing 1 changed file with 55 additions and 40 deletions.
95 changes: 55 additions & 40 deletions projects/ui/src/state/bean/pools/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import throttle from 'lodash/throttle';

import { displayBeanPrice, getTokenIndex, tokenResult } from '~/util';
import useSdk from '~/hooks/sdk';
import { AdvancedPipeStruct, BeanstalkSDK, Clipboard, Pool } from '@beanstalk/sdk';
import {
AdvancedPipeStruct,
BeanstalkSDK,
Clipboard,
Pool,
} from '@beanstalk/sdk';
import { chunkArray } from '~/util/UI';
import { getExtractMulticallResult } from '~/util/Multicall';
import { transform } from '~/util/BigNumber';
Expand Down Expand Up @@ -33,14 +38,13 @@ export const useFetchPools = () => {
const poolsArr = [...whitelistedPools.values()];
const BEAN = sdk.tokens.BEAN;



const [priceResult, beanTotalSupply, totalDeltaB, lpResults] = await Promise.all([
beanstalkPrice.price(),
BEAN.getContract().totalSupply().then(tokenResult(BEAN)),
beanstalk.totalDeltaB().then(tokenResult(BEAN)),
fetchPoolsData(sdk, poolsArr)
]);
const [priceResult, beanTotalSupply, totalDeltaB, lpResults] =
await Promise.all([
beanstalkPrice.price(),
BEAN.getContract().totalSupply().then(tokenResult(BEAN)),
beanstalk.totalDeltaB().then(tokenResult(BEAN)),
fetchPoolsData(sdk, poolsArr),
]);

console.debug(`${pageContext} FETCH: `, {
priceResult,
Expand All @@ -49,15 +53,14 @@ export const useFetchPools = () => {
totalDeltaB,
});


if (priceResult) {
const price = tokenResult(BEAN)(priceResult.price.toString());

const poolsPayload = priceResult.ps.reduce<UpdatePoolPayload[]>(
(acc, poolData) => {
const address = poolData.pool.toLowerCase();
const pool = sdk.pools.getPoolByLPToken(address);

if (pool) {
const lpResult = lpResults[getTokenIndex(pool.lpToken)];
const payload: UpdatePoolPayload = {
Expand All @@ -68,7 +71,7 @@ export const useFetchPools = () => {
transform(poolData.balances[0], 'bnjs', pool.tokens[0]),
transform(poolData.balances[1], 'bnjs', pool.tokens[1]),
],
deltaB: lpResult.deltaB,
deltaB: transform(poolData.deltaB, 'bnjs', BEAN),
supply: lpResult.totalSupply,
// Liquidity: always denominated in USD for the price contract
liquidity: transform(poolData.liquidity, 'bnjs', BEAN),
Expand Down Expand Up @@ -149,43 +152,55 @@ export default PoolsUpdater;
async function fetchPoolsData(sdk: BeanstalkSDK, pools: Pool[]) {
const { beanstalk } = sdk.contracts;

const calls: AdvancedPipeStruct[] = pools.map((pool) => {
const deltaBCall = {
target: beanstalk.address,
callData: beanstalk.interface.encodeFunctionData('poolDeltaB', [pool.address]),
clipboard: Clipboard.encode([])
};
const supplyCall = {
target: pool.lpToken.address,
callData: pool.lpToken.getContract().interface.encodeFunctionData('totalSupply'),
clipboard: Clipboard.encode([])
};

return [deltaBCall, supplyCall];
}).flat();

const result = await sdk.contracts.beanstalk.callStatic.advancedPipe(calls, '0');
const calls: AdvancedPipeStruct[] = pools
.map((pool) => {
const deltaBCall = {
target: beanstalk.address,
callData: beanstalk.interface.encodeFunctionData('poolDeltaB', [
pool.address,
]),
clipboard: Clipboard.encode([]),
};
const supplyCall = {
target: pool.lpToken.address,
callData: pool.lpToken
.getContract()
.interface.encodeFunctionData('totalSupply'),
clipboard: Clipboard.encode([]),
};

return [deltaBCall, supplyCall];
})
.flat();

const result = await sdk.contracts.beanstalk.callStatic.advancedPipe(
calls,
'0'
);
const chunkedByPool = chunkArray(result, 2);

const datas = pools.reduce<TokenMap<{
totalSupply: BigNumber;
deltaB: BigNumber;
}>>((prev, curr, i) => {

const datas = pools.reduce<
TokenMap<{
totalSupply: BigNumber;
deltaB: BigNumber;
}>
>((prev, curr, i) => {
const [deltaBResult, totalSupplyResult] = chunkedByPool[i];

const deltaB = beanstalk.interface.decodeFunctionResult('poolDeltaB', deltaBResult)[0];
const totalSupply = curr.lpToken.getContract().interface.decodeFunctionResult(
'totalSupply',
totalSupplyResult
const deltaB = beanstalk.interface.decodeFunctionResult(
'poolDeltaB',
deltaBResult
)[0];
const totalSupply = curr.lpToken
.getContract()
.interface.decodeFunctionResult('totalSupply', totalSupplyResult)[0];

prev[getTokenIndex(curr)] = {
totalSupply: transform(totalSupply, 'bnjs', curr.lpToken),
deltaB: transform(deltaB, 'bnjs', sdk.tokens.BEAN),
}
};
return prev;
}, {})
}, {});

return datas;

}

0 comments on commit 16a6650

Please sign in to comment.