Skip to content

Commit

Permalink
Refactor APR calculation logic and improve number formatting in Pool …
Browse files Browse the repository at this point in the history
…components
  • Loading branch information
vuonghuuhung committed Jan 14, 2025
1 parent 4d97fa5 commit bb3daef
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 46 deletions.
32 changes: 23 additions & 9 deletions src/libs/contractSingleton.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CosmWasmClient, fromBinary, SigningCosmWasmClient, toBinary } from '@cosmjs/cosmwasm-stargate';
import { MulticallQueryClient } from '@oraichain/common-contracts-sdk';
import { AXIOS_THROTTLE_THRESHOLD, AXIOS_TIMEOUT, CoinGeckoId, toDisplay } from '@oraichain/oraidex-common';
import { BigDecimal, CoinGeckoId, toDisplay } from '@oraichain/oraidex-common';
import {
AssetInfo,
OraiswapTokenClient,
Expand Down Expand Up @@ -33,15 +33,14 @@ import {
positionToTick,
Tickmap
} from '@oraichain/oraiswap-v3';
import Axios from 'axios';
import { retryAdapterEnhancer, throttleAdapterEnhancer } from 'axios-extensions';
import { network, oraichainTokens } from 'initCommon';

import { CoinGeckoPrices } from 'hooks/useCoingecko';
import { TokenDataOnChain } from 'pages/Pool-V3/components/PriceRangePlot/utils';
import { numberExponentToLarge } from 'pages/Pool-V3/hooks/useCreatePositionForm';
import { getPools } from 'rest/graphClient';
import { PoolInfoResponse } from 'types/pool';
import { store } from 'store/configure';
import { PoolInfoResponse } from 'types/pool';

export const ALL_FEE_TIERS_DATA: FeeTier[] = [
{ fee: 100000000, tick_spacing: 1 },
Expand Down Expand Up @@ -845,22 +844,37 @@ export async function fetchPoolAprInfo(

const poolAprs: Record<string, PoolAprInfo> = {};
for (const item of pools) {
// Note: this logic is for pool v2

if ('liquidityAddr' in item) {
const { apr, aprBoost, liquidityAddr } = item;
const { liquidityAddr } = item;

// TODO: calculate incentive apr base on reward per sec later
const incentiveApr = 0;

// calculate apr base on volume 24h -> swap fee

const totalLiquidityUsd = new BigDecimal(numberExponentToLarge(item.totalLiquidity)); // usdt denom
const volume24hUsd = new BigDecimal(item.volume24Hour); // usdt denom

const swapFee = volume24hUsd.mul(0.002); // fee for LP is 0.2%
let apr = totalLiquidityUsd.toNumber() !== 0 ? swapFee.div(totalLiquidityUsd).toNumber() : 0;

if (apr < 0) apr = 0;

poolAprs[liquidityAddr] = {
apr: {
min: aprBoost || 0,
max: aprBoost || 0
min: apr || 0,
max: apr || 0
},
incentives: [],
swapFee: {
min: apr,
max: apr
},
incentivesApr: {
min: 0,
max: 0
min: incentiveApr,
max: incentiveApr
}
};
continue;
Expand Down
38 changes: 19 additions & 19 deletions src/pages/Pool-V3/components/PoolDetail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,26 +100,26 @@ const PoolV3Detail = () => {

const [aprInfo, setAprInfo] = useConfigReducer('aprPools');

useEffect(() => {
const getAPRInfo = async () => {
const res = await fetchPoolAprInfo(
[poolDetail],
poolPrice,
{
[poolKeyString]: liquidity.total
},
feeDailyData
);
setAprInfo({
...aprInfo,
[poolKeyString]: res[poolKeyString]
});
};
// useEffect(() => {
// const getAPRInfo = async () => {
// const res = await fetchPoolAprInfo(
// [poolDetail],
// poolPrice,
// {
// [poolKeyString]: liquidity.total
// },
// feeDailyData
// );
// setAprInfo({
// ...aprInfo,
// [poolKeyString]: res[poolKeyString]
// });
// };

if (poolDetail && poolPrice && liquidity && poolDetail.poolKey === poolKeyString) {
getAPRInfo();
}
}, [liquidity.total, feeDailyData, poolDetail, poolPrice, poolKeyString]);
// if (poolDetail && poolPrice && liquidity && poolDetail.poolKey === poolKeyString) {
// getAPRInfo();
// }
// }, [liquidity.total, feeDailyData, poolDetail, poolPrice, poolKeyString]);

const { spread, pool_key } = poolDetail || {};
const { allocation, total } = liquidity;
Expand Down
25 changes: 11 additions & 14 deletions src/pages/Pool-V3/components/PoolList/PoolItemTData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ const PoolItemTData = ({
setCurrentPool,
setPairDenomsDeposit
}) => {



const navigate = useNavigate();
const [openTooltip, setOpenTooltip] = useState(false);

Expand Down Expand Up @@ -99,10 +96,10 @@ const PoolItemTData = ({
<div className={styles.apr}>
<span className={styles.amount}>
{aprInfo.apr.min === aprInfo.apr.max
? `${numberWithCommas(aprInfo.apr.min * 100, undefined, { maximumFractionDigits: 1 })}`
? `${numberWithCommas(aprInfo.apr.min * 100, undefined, { maximumFractionDigits: 2 })}`
: `${numberWithCommas(aprInfo.apr.min * 100, undefined, {
maximumFractionDigits: 1
})} - ${numberWithCommas(aprInfo.apr.max * 100, undefined, { maximumFractionDigits: 1 })}`}
})} - ${numberWithCommas(aprInfo.apr.max * 100, undefined, { maximumFractionDigits: 2 })}`}
%
</span>
<TooltipIcon
Expand All @@ -117,11 +114,11 @@ const PoolItemTData = ({
<span>Swap fee</span>
<span className={styles.value}>
{aprInfo.swapFee.min === aprInfo.swapFee.max
? `${numberWithCommas(aprInfo.swapFee.min * 100, undefined, { maximumFractionDigits: 1 })}`
? `${numberWithCommas(aprInfo.swapFee.min * 100, undefined, { maximumFractionDigits: 2 })}`
: `${numberWithCommas(aprInfo.swapFee.min * 100, undefined, {
maximumFractionDigits: 1
maximumFractionDigits: 2
})} - ${numberWithCommas(aprInfo.swapFee.max * 100, undefined, {
maximumFractionDigits: 1
maximumFractionDigits: 2
})}`}
%
</span>
Expand All @@ -133,11 +130,11 @@ const PoolItemTData = ({
</span>
<span className={styles.value}>
{aprInfo.incentivesApr.min === aprInfo.incentivesApr.max
? `${numberWithCommas(aprInfo.incentivesApr.min * 100, undefined, { maximumFractionDigits: 1 })}`
? `${numberWithCommas(aprInfo.incentivesApr.min * 100, undefined, { maximumFractionDigits: 2 })}`
: `${numberWithCommas(aprInfo.incentivesApr.min * 100, undefined, {
maximumFractionDigits: 1
maximumFractionDigits: 2
})} - ${numberWithCommas(aprInfo.incentivesApr.max * 100, undefined, {
maximumFractionDigits: 1
maximumFractionDigits: 2
})}`}
%
</span>
Expand All @@ -146,10 +143,10 @@ const PoolItemTData = ({
<span>Total APR</span>
<span className={styles.totalApr}>
{aprInfo.apr.min === aprInfo.apr.max
? `${numberWithCommas(aprInfo.apr.min * 100, undefined, { maximumFractionDigits: 1 })}`
? `${numberWithCommas(aprInfo.apr.min * 100, undefined, { maximumFractionDigits: 2 })}`
: `${numberWithCommas(aprInfo.apr.min * 100, undefined, {
maximumFractionDigits: 1
})} - ${numberWithCommas(aprInfo.apr.max * 100, undefined, { maximumFractionDigits: 1 })}`}
maximumFractionDigits: 2
})} - ${numberWithCommas(aprInfo.apr.max * 100, undefined, { maximumFractionDigits: 2 })}`}
%
</span>
</div>
Expand Down
9 changes: 6 additions & 3 deletions src/pages/Pool-V3/components/PoolList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,13 @@ const PoolList = ({ search, filterType }: { search: string; filterType: POOL_TYP
showApr = {
...showApr,
apr: {
min: (showApr['apr']?.['min'] || 0) / 100,
max: (showApr['apr']?.['max'] || 0) / 100
min: (showApr['apr']?.['min'] || 0),
max: (showApr['apr']?.['max'] || 0)
},
swapFee: (showApr['swapFee'] || 0) / 100
swapFee: {
min: (showApr['swapFee']?.['min'] || 0),
max: (showApr['swapFee']?.['max'] || 0)
}
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/pages/Pool-V3/hooks/useCreatePositionForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ export function getLiqFrom(target: number, list: ActiveLiquidityPerTickRange[]):
return 0;
}

function numberExponentToLarge(numIn) {
export function numberExponentToLarge(numIn) {
numIn += ''; // To cater to numric entries
var sign = ''; // To remember the number sign
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
Expand Down

0 comments on commit bb3daef

Please sign in to comment.