utils#getDaysCurrentYear TypeScript Examples
The following examples show how to use
utils#getDaysCurrentYear.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LiquidityPoolRow.tsx From interface-v2 with GNU General Public License v3.0 | 4 votes |
LiquidityPoolRow: React.FC<{
pair: any;
key: number;
}> = ({ pair, key }) => {
const classes = useStyles();
const { palette, breakpoints } = useTheme();
const daysCurrentYear = getDaysCurrentYear();
const isMobile = useMediaQuery(breakpoints.down('xs'));
const dayVolumeUSD =
Number(
pair.oneDayVolumeUSD ? pair.oneDayVolumeUSD : pair.oneDayVolumeUntracked,
) *
GlobalConst.utils.FEEPERCENT *
daysCurrentYear *
100;
const trackReserveUSD = Number(
pair.oneDayVolumeUSD ? pair.trackedReserveUSD : pair.reserveUSD,
);
const apy =
isNaN(dayVolumeUSD) || trackReserveUSD === 0
? 0
: dayVolumeUSD / trackReserveUSD;
const liquidity = pair.trackedReserveUSD
? pair.trackedReserveUSD
: pair.reserveUSD;
const volume = pair.oneDayVolumeUSD
? pair.oneDayVolumeUSD
: pair.oneDayVolumeUntracked;
const token0 = useCurrency(pair.token0.id);
const token1 = useCurrency(pair.token1.id);
return (
<Box
key={key}
display='flex'
flexWrap='wrap'
className={classes.liquidityContent}
padding={2}
>
<Box display='flex' alignItems='center' width={isMobile ? 1 : 0.5}>
<DoubleCurrencyLogo
currency0={token0 ?? undefined}
currency1={token1 ?? undefined}
size={28}
/>
<Typography variant='body2' style={{ marginLeft: 12 }}>
{pair.token0.symbol.toUpperCase()} /{' '}
{pair.token1.symbol.toUpperCase()}
</Typography>
</Box>
<Box
width={isMobile ? 1 : 0.2}
mt={isMobile ? 2.5 : 0}
display='flex'
justifyContent='space-between'
>
{isMobile && (
<Typography variant='body2' style={{ color: palette.text.secondary }}>
TVL
</Typography>
)}
<Typography variant='body2'>${formatCompact(liquidity)}</Typography>
</Box>
<Box
width={isMobile ? 1 : 0.15}
mt={isMobile ? 1 : 0}
display='flex'
justifyContent='space-between'
>
{isMobile && (
<Typography variant='body2' style={{ color: palette.text.secondary }}>
24H Volume
</Typography>
)}
<Typography variant='body2'>${formatCompact(volume)}</Typography>
</Box>
<Box
width={isMobile ? 1 : 0.15}
mt={isMobile ? 1 : 0}
display='flex'
justifyContent={isMobile ? 'space-between' : 'flex-end'}
>
{isMobile && (
<Typography variant='body2' style={{ color: palette.text.secondary }}>
APY
</Typography>
)}
<Typography
variant='body2'
align='right'
style={{
color: apy < 0 ? palette.error.main : palette.success.main,
}}
>
{apy.toFixed(2)}%
</Typography>
</Box>
</Box>
);
}
Example #2
Source File: hooks.ts From interface-v2 with GNU General Public License v3.0 | 4 votes |
// gets the dual rewards staking info from the network for the active chain id
export function useDualStakingInfo(
pairToFilterBy?: Pair | null,
startIndex?: number,
endIndex?: number,
filter?: { search: string; isStaked: boolean },
): DualStakingInfo[] {
const { chainId, account } = useActiveWeb3React();
const info = useMemo(
() =>
chainId
? returnDualStakingInfo()
[chainId]?.slice(startIndex, endIndex)
?.filter((stakingRewardInfo) =>
pairToFilterBy === undefined || pairToFilterBy === null
? getSearchFiltered(
stakingRewardInfo,
filter ? filter.search : '',
)
: pairToFilterBy.involvesToken(stakingRewardInfo.tokens[0]) &&
pairToFilterBy.involvesToken(stakingRewardInfo.tokens[1]),
) ?? []
: [],
[chainId, pairToFilterBy, startIndex, endIndex, filter],
);
const uni = chainId ? GlobalValue.tokens.UNI[chainId] : undefined;
const rewardsAddresses = useMemo(
() => info.map(({ stakingRewardAddress }) => stakingRewardAddress),
[info],
);
// const pairAddresses = useMemo(() => info.map(({ pair }) => pair), [info]);
// useEffect(() => {
// getDualBulkPairData(pairAddresses);
// }, [pairAddresses]);
const accountArg = useMemo(() => [account ?? undefined], [account]);
// get all the info from the staking rewards contracts
const balances = useMultipleContractSingleData(
rewardsAddresses,
STAKING_DUAL_REWARDS_INTERFACE,
'balanceOf',
accountArg,
);
const earnedAAmounts = useMultipleContractSingleData(
rewardsAddresses,
STAKING_DUAL_REWARDS_INTERFACE,
'earnedA',
accountArg,
);
const earnedBAmounts = useMultipleContractSingleData(
rewardsAddresses,
STAKING_DUAL_REWARDS_INTERFACE,
'earnedB',
accountArg,
);
const totalSupplies = useMultipleContractSingleData(
rewardsAddresses,
STAKING_DUAL_REWARDS_INTERFACE,
'totalSupply',
);
const rewardRatesA = useMultipleContractSingleData(
rewardsAddresses,
STAKING_DUAL_REWARDS_INTERFACE,
'rewardRateA',
undefined,
NEVER_RELOAD,
);
const rewardRatesB = useMultipleContractSingleData(
rewardsAddresses,
STAKING_DUAL_REWARDS_INTERFACE,
'rewardRateB',
undefined,
NEVER_RELOAD,
);
const baseTokens = info.map((item) => {
const unwrappedCurrency = unwrappedToken(item.baseToken);
const empty = unwrappedToken(returnTokenFromKey('EMPTY'));
return unwrappedCurrency === empty ? item.tokens[0] : item.baseToken;
});
const usdPrices = useUSDCPrices(baseTokens);
const totalSupplys = useTotalSupplys(
info.map((item) => getFarmLPToken(item)),
);
const stakingPairs = usePairs(info.map((item) => item.tokens));
const rewardTokenAPrices = useUSDCPricesToken(
info.map((item) => item.rewardTokenA),
);
const rewardTokenBPrices = useUSDCPricesToken(
info.map((item) => item.rewardTokenB),
);
return useMemo(() => {
if (!chainId || !uni) return [];
return rewardsAddresses.reduce<DualStakingInfo[]>(
(memo, rewardsAddress, index) => {
// these two are dependent on account
const balanceState = balances[index];
const earnedAAmountState = earnedAAmounts[index];
const earnedBAmountState = earnedBAmounts[index];
// these get fetched regardless of account
const totalSupplyState = totalSupplies[index];
const rewardRateAState = rewardRatesA[index];
const rewardRateBState = rewardRatesB[index];
const stakingInfo = info[index];
const rewardTokenAPrice = rewardTokenAPrices[index];
const rewardTokenBPrice = rewardTokenBPrices[index];
if (
// these may be undefined if not logged in
!balanceState?.loading &&
!earnedAAmountState?.loading &&
!earnedBAmountState?.loading &&
// always need these
totalSupplyState &&
!totalSupplyState.loading &&
rewardRateAState &&
!rewardRateAState.loading &&
rewardRateBState &&
!rewardRateBState.loading
) {
const rateA = web3.utils.toWei(stakingInfo.rateA.toString());
const rateB = web3.utils.toWei(stakingInfo.rateB.toString());
const stakedAmount = initTokenAmountFromCallResult(
getFarmLPToken(stakingInfo),
balanceState,
);
const totalStakedAmount = initTokenAmountFromCallResult(
getFarmLPToken(stakingInfo),
totalSupplyState,
);
const totalRewardRateA = new TokenAmount(uni, JSBI.BigInt(rateA));
const totalRewardRateB = new TokenAmount(uni, JSBI.BigInt(rateB));
//const pair = info[index].pair.toLowerCase();
//const fees = (pairData && pairData[pair] ? pairData[pair].oneDayVolumeUSD * 0.0025: 0);
const totalRewardRateA01 = initTokenAmountFromCallResult(
uni,
rewardRateAState,
);
const totalRewardRateB01 = initTokenAmountFromCallResult(
uni,
rewardRateBState,
);
const getHypotheticalRewardRate = (
stakedAmount?: TokenAmount,
totalStakedAmount?: TokenAmount,
totalRewardRate?: TokenAmount,
): TokenAmount | undefined => {
if (!stakedAmount || !totalStakedAmount || !totalRewardRate) return;
return new TokenAmount(
uni,
JSBI.greaterThan(totalStakedAmount.raw, JSBI.BigInt(0))
? JSBI.divide(
JSBI.multiply(totalRewardRate.raw, stakedAmount.raw),
totalStakedAmount.raw,
)
: JSBI.BigInt(0),
);
};
const individualRewardRateA = getHypotheticalRewardRate(
stakedAmount,
totalStakedAmount,
totalRewardRateA01,
);
const individualRewardRateB = getHypotheticalRewardRate(
stakedAmount,
totalStakedAmount,
totalRewardRateB01,
);
const { oneDayFee, accountFee } = getStakingFees(
stakingInfo,
balanceState,
totalSupplyState,
);
let valueOfTotalStakedAmountInBaseToken: TokenAmount | undefined;
const [, stakingTokenPair] = stakingPairs[index];
const totalSupply = totalSupplys[index];
const usdPrice = usdPrices[index];
if (
totalSupply &&
stakingTokenPair &&
baseTokens[index] &&
totalStakedAmount
) {
// take the total amount of LP tokens staked, multiply by ETH value of all LP tokens, divide by all LP tokens
valueOfTotalStakedAmountInBaseToken = new TokenAmount(
baseTokens[index],
JSBI.divide(
JSBI.multiply(
JSBI.multiply(
totalStakedAmount.raw,
stakingTokenPair.reserveOf(baseTokens[index]).raw,
),
JSBI.BigInt(2), // this is b/c the value of LP shares are ~double the value of the WETH they entitle owner to
),
totalSupply.raw,
),
);
}
const valueOfTotalStakedAmountInUSDC =
valueOfTotalStakedAmountInBaseToken &&
usdPrice?.quote(valueOfTotalStakedAmountInBaseToken);
const tvl = valueOfTotalStakedAmountInUSDC
? valueOfTotalStakedAmountInUSDC.toExact()
: valueOfTotalStakedAmountInBaseToken?.toExact();
const perMonthReturnInRewards =
((stakingInfo.rateA * rewardTokenAPrice +
stakingInfo.rateB * rewardTokenBPrice) *
(getDaysCurrentYear() / 12)) /
Number(valueOfTotalStakedAmountInUSDC?.toExact());
memo.push({
stakingRewardAddress: rewardsAddress,
tokens: stakingInfo.tokens,
ended: stakingInfo.ended,
name: stakingInfo.name,
lp: stakingInfo.lp,
earnedAmountA: initTokenAmountFromCallResult(
uni,
earnedAAmountState,
),
earnedAmountB: initTokenAmountFromCallResult(
uni,
earnedBAmountState,
),
rewardRateA: individualRewardRateA,
rewardRateB: individualRewardRateB,
totalRewardRateA: totalRewardRateA,
totalRewardRateB: totalRewardRateB,
stakedAmount: stakedAmount,
totalStakedAmount: totalStakedAmount,
getHypotheticalRewardRate,
baseToken: stakingInfo.baseToken,
pair: stakingInfo.pair,
rateA: stakingInfo.rateA,
rateB: stakingInfo.rateB,
rewardTokenA: stakingInfo.rewardTokenA,
rewardTokenB: stakingInfo.rewardTokenB,
rewardTokenBBase: stakingInfo.rewardTokenBBase,
rewardTokenAPrice,
rewardTokenBPrice,
tvl,
perMonthReturnInRewards,
totalSupply,
usdPrice,
stakingTokenPair,
oneDayFee,
accountFee,
});
}
return memo;
},
[],
);
}, [
balances,
chainId,
earnedAAmounts,
earnedBAmounts,
info,
rewardsAddresses,
totalSupplies,
uni,
rewardRatesA,
rewardRatesB,
baseTokens,
totalSupplys,
usdPrices,
stakingPairs,
rewardTokenAPrices,
rewardTokenBPrices,
]).filter((stakingInfo) =>
filter && filter.isStaked
? stakingInfo.stakedAmount && stakingInfo.stakedAmount.greaterThan('0')
: true,
);
}
Example #3
Source File: hooks.ts From interface-v2 with GNU General Public License v3.0 | 4 votes |
// gets the staking info from the network for the active chain id
export function useStakingInfo(
pairToFilterBy?: Pair | null,
startIndex?: number,
endIndex?: number,
filter?: { search: string; isStaked: boolean },
): StakingInfo[] {
const { chainId, account } = useActiveWeb3React();
const info = useMemo(
() =>
chainId
? returnStakingInfo()
[chainId]?.slice(startIndex, endIndex)
?.filter((stakingRewardInfo) =>
pairToFilterBy === undefined || pairToFilterBy === null
? getSearchFiltered(
stakingRewardInfo,
filter ? filter.search : '',
)
: pairToFilterBy.involvesToken(stakingRewardInfo.tokens[0]) &&
pairToFilterBy.involvesToken(stakingRewardInfo.tokens[1]),
) ?? []
: [],
[chainId, pairToFilterBy, startIndex, endIndex, filter],
);
const uni = chainId ? GlobalValue.tokens.UNI[chainId] : undefined;
const rewardsAddresses = useMemo(
() => info.map(({ stakingRewardAddress }) => stakingRewardAddress),
[info],
);
// const pairAddresses = useMemo(() => info.map(({ pair }) => pair), [info]);
// useEffect(() => {
// getBulkPairData(allPairAddress);
// }, [allPairAddress]);
const accountArg = useMemo(() => [account ?? undefined], [account]);
// get all the info from the staking rewards contracts
const balances = useMultipleContractSingleData(
rewardsAddresses,
STAKING_REWARDS_INTERFACE,
'balanceOf',
accountArg,
);
const earnedAmounts = useMultipleContractSingleData(
rewardsAddresses,
STAKING_REWARDS_INTERFACE,
'earned',
accountArg,
);
const totalSupplies = useMultipleContractSingleData(
rewardsAddresses,
STAKING_REWARDS_INTERFACE,
'totalSupply',
);
const rewardRates = useMultipleContractSingleData(
rewardsAddresses,
STAKING_REWARDS_INTERFACE,
'rewardRate',
undefined,
NEVER_RELOAD,
);
const baseTokens = info.map((item) => {
const unwrappedCurrency = unwrappedToken(item.baseToken);
const empty = unwrappedToken(returnTokenFromKey('EMPTY'));
return unwrappedCurrency === empty ? item.tokens[0] : item.baseToken;
});
const rewardTokens = info.map((item) => item.rewardToken);
const usdPrices = useUSDCPrices(baseTokens);
const usdPricesRewardTokens = useUSDCPricesToken(rewardTokens);
const totalSupplys = useTotalSupplys(
info.map((item) => {
const lp = item.lp;
const dummyPair = new Pair(
new TokenAmount(item.tokens[0], '0'),
new TokenAmount(item.tokens[1], '0'),
);
return lp && lp !== ''
? new Token(137, lp, 18, 'SLP', 'Staked LP')
: dummyPair.liquidityToken;
}),
);
const stakingPairs = usePairs(info.map((item) => item.tokens));
return useMemo(() => {
if (!chainId || !uni) return [];
return rewardsAddresses.reduce<StakingInfo[]>(
(memo, rewardsAddress, index) => {
// these two are dependent on account
const balanceState = balances[index];
const earnedAmountState = earnedAmounts[index];
// these get fetched regardless of account
const totalSupplyState = totalSupplies[index];
const rewardRateState = rewardRates[index];
const stakingInfo = info[index];
const rewardTokenPrice = usdPricesRewardTokens[index];
if (
// these may be undefined if not logged in
!balanceState?.loading &&
!earnedAmountState?.loading &&
// always need these
totalSupplyState &&
!totalSupplyState.loading &&
rewardRateState &&
!rewardRateState.loading
) {
// get the LP token
const tokens = stakingInfo.tokens;
const dummyPair = new Pair(
new TokenAmount(tokens[0], '0'),
new TokenAmount(tokens[1], '0'),
);
// check for account, if no account set to 0
const lp = stakingInfo.lp;
const rate = web3.utils.toWei(stakingInfo.rate.toString());
const stakedAmount = initTokenAmountFromCallResult(
getFarmLPToken(stakingInfo),
balanceState,
);
const totalStakedAmount = initTokenAmountFromCallResult(
getFarmLPToken(stakingInfo),
totalSupplyState,
);
const totalRewardRate = new TokenAmount(uni, JSBI.BigInt(rate));
//const pair = info[index].pair.toLowerCase();
//const fees = (pairData && pairData[pair] ? pairData[pair].oneDayVolumeUSD * 0.0025: 0);
const totalRewardRate01 = initTokenAmountFromCallResult(
uni,
rewardRateState,
);
const getHypotheticalRewardRate = (
stakedAmount?: TokenAmount,
totalStakedAmount?: TokenAmount,
totalRewardRate?: TokenAmount,
): TokenAmount | undefined => {
if (!stakedAmount || !totalStakedAmount || !totalRewardRate) return;
return new TokenAmount(
uni,
JSBI.greaterThan(totalStakedAmount.raw, JSBI.BigInt(0))
? JSBI.divide(
JSBI.multiply(totalRewardRate.raw, stakedAmount.raw),
totalStakedAmount.raw,
)
: JSBI.BigInt(0),
);
};
const individualRewardRate = getHypotheticalRewardRate(
stakedAmount,
totalStakedAmount,
totalRewardRate01,
);
const { oneYearFeeAPY, oneDayFee, accountFee } = getStakingFees(
stakingInfo,
balanceState,
totalSupplyState,
);
let valueOfTotalStakedAmountInBaseToken: TokenAmount | undefined;
const [, stakingTokenPair] = stakingPairs[index];
const totalSupply = totalSupplys[index];
const usdPrice = usdPrices[index];
if (
totalSupply &&
stakingTokenPair &&
baseTokens[index] &&
totalStakedAmount
) {
// take the total amount of LP tokens staked, multiply by ETH value of all LP tokens, divide by all LP tokens
valueOfTotalStakedAmountInBaseToken = new TokenAmount(
baseTokens[index],
JSBI.divide(
JSBI.multiply(
JSBI.multiply(
totalStakedAmount.raw,
stakingTokenPair.reserveOf(baseTokens[index]).raw,
),
JSBI.BigInt(2), // this is b/c the value of LP shares are ~double the value of the WETH they entitle owner to
),
totalSupply.raw,
),
);
}
const valueOfTotalStakedAmountInUSDC =
valueOfTotalStakedAmountInBaseToken &&
usdPrice?.quote(valueOfTotalStakedAmountInBaseToken);
const tvl = valueOfTotalStakedAmountInUSDC
? valueOfTotalStakedAmountInUSDC.toExact()
: valueOfTotalStakedAmountInBaseToken?.toExact();
const perMonthReturnInRewards =
(Number(stakingInfo.rate) *
rewardTokenPrice *
(getDaysCurrentYear() / 12)) /
Number(valueOfTotalStakedAmountInUSDC?.toExact());
memo.push({
stakingRewardAddress: rewardsAddress,
tokens: stakingInfo.tokens,
ended: stakingInfo.ended,
name: stakingInfo.name,
lp: stakingInfo.lp,
rewardToken: stakingInfo.rewardToken,
rewardTokenPrice,
earnedAmount: initTokenAmountFromCallResult(uni, earnedAmountState),
rewardRate: individualRewardRate,
totalRewardRate: totalRewardRate,
stakedAmount: stakedAmount,
totalStakedAmount: totalStakedAmount,
getHypotheticalRewardRate,
baseToken: stakingInfo.baseToken,
pair: stakingInfo.pair,
rate: stakingInfo.rate,
oneYearFeeAPY: oneYearFeeAPY,
oneDayFee,
accountFee,
tvl,
perMonthReturnInRewards,
valueOfTotalStakedAmountInBaseToken,
usdPrice,
stakingTokenPair,
totalSupply,
});
}
return memo;
},
[],
);
}, [
balances,
chainId,
earnedAmounts,
info,
rewardsAddresses,
totalSupplies,
uni,
rewardRates,
usdPricesRewardTokens,
baseTokens,
totalSupplys,
usdPrices,
stakingPairs,
]).filter((stakingInfo) =>
filter && filter.isStaked
? stakingInfo.stakedAmount && stakingInfo.stakedAmount.greaterThan('0')
: true,
);
}