Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP]: delta bs fix #1166

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 58 additions & 46 deletions projects/ui/src/state/bean/pools/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ 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';
import useL2OnlyEffect from '~/hooks/chain/useL2OnlyEffect';
import { TokenMap } from '~/constants';
Expand All @@ -16,8 +20,6 @@ import { updateDeltaB, updatePrice, updateSupply } from '../token/actions';

const pageContext = '[bean/pools/useGetPools]';

const extract = getExtractMulticallResult(pageContext);

export const useFetchPools = () => {
const dispatch = useDispatch();
const sdk = useSdk();
Expand All @@ -33,14 +35,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 +50,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,16 +68,16 @@ 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),
// USD value of 1 LP token == liquidity / supply
totalCrosses: new BigNumber(0),
lpUsd: transform(poolData.lpUsd, 'bnjs', BEAN),
lpBdv: transform(poolData.lpBdv, 'bnjs', BEAN),
twaDeltaB: lpResult.deltaB
? transform(lpResult.deltaB, 'bnjs', BEAN)
twaDeltaB: lpResult.twaDeltaB
? transform(lpResult.twaDeltaB, 'bnjs', BEAN)
: null,
},
} as UpdatePoolPayload;
Expand Down Expand Up @@ -149,43 +149,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('poolCurrentDeltaB', [
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;
twaDeltaB: 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(
'poolCurrentDeltaB',
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),
}
twaDeltaB: transform(deltaB, 'bnjs', sdk.tokens.BEAN),
};
return prev;
}, {})
}, {});

return datas;

}
Loading