utils#initTokenAmountFromCallResult TypeScript Examples
The following examples show how to use
utils#initTokenAmountFromCallResult.
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: hooks.ts From interface-v2 with GNU General Public License v3.0 | 4 votes |
export function useSyrupInfo(
tokenToFilterBy?: Token | null,
startIndex?: number,
endIndex?: number,
filter?: { search: string; isStaked: boolean },
): SyrupInfo[] {
const { chainId, account } = useActiveWeb3React();
const currentTimestamp = dayjs().unix();
const info = useMemo(
() =>
chainId
? returnSyrupInfo()
[chainId]?.slice(startIndex, endIndex)
.filter(
(syrupInfo) =>
syrupInfo.ending > currentTimestamp &&
(tokenToFilterBy === undefined || tokenToFilterBy === null
? getSearchFiltered(syrupInfo, filter ? filter.search : '')
: tokenToFilterBy.equals(syrupInfo.token)),
) ?? []
: [],
[chainId, tokenToFilterBy, startIndex, endIndex, filter, currentTimestamp],
);
const uni = chainId ? GlobalValue.tokens.UNI[chainId] : undefined;
const rewardsAddresses = useMemo(
() => info.map(({ stakingRewardAddress }) => stakingRewardAddress),
[info],
);
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 stakingTokenPairs = usePairs(
info.map((item) => [
unwrappedToken(item.token),
unwrappedToken(item.baseToken),
]),
);
const usdBaseTokenPrices = useUSDCPrices(
info.map((item) => unwrappedToken(item.baseToken)),
);
const stakingTokenPrices = useUSDCPricesToken(
info.map((item) => item.stakingToken),
);
return useMemo(() => {
if (!chainId || !uni) return [];
return rewardsAddresses.reduce<SyrupInfo[]>(
(memo, rewardsAddress, index) => {
// these two are dependent on account
const balanceState = balances[index];
const earnedAmountState = earnedAmounts[index];
const stakingTokenPrice = stakingTokenPrices[index];
// these get fetched regardless of account
const totalSupplyState = totalSupplies[index];
const rewardRateState = rewardRates[index];
const syrupInfo = info[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 token = syrupInfo.token;
const [, stakingTokenPair] = stakingTokenPairs[index];
const tokenPairPrice = stakingTokenPair?.priceOf(token);
const usdPriceBaseToken = usdBaseTokenPrices[index];
const priceOfRewardTokenInUSD =
tokenPairPrice && usdPriceBaseToken
? Number(tokenPairPrice.toSignificant()) *
Number(usdPriceBaseToken.toSignificant())
: undefined;
const rewards = syrupInfo.rate * (priceOfRewardTokenInUSD ?? 0);
// check for account, if no account set to 0
const rate = web3.utils.toWei(syrupInfo.rate.toString());
const syrupToken = getSyrupLPToken(syrupInfo);
const stakedAmount = initTokenAmountFromCallResult(
syrupToken,
balanceState,
);
const totalStakedAmount = initTokenAmountFromCallResult(
syrupToken,
totalSupplyState,
);
const totalRewardRate = new TokenAmount(token, JSBI.BigInt(rate));
//const pair = info[index].pair.toLowerCase();
//const fees = (pairData && pairData[pair] ? pairData[pair].oneDayVolumeUSD * 0.0025: 0);
const rewardRate = initTokenAmountFromCallResult(
token,
rewardRateState,
);
const getHypotheticalRewardRate = (
stakedAmount?: TokenAmount,
totalStakedAmount?: TokenAmount,
): TokenAmount | undefined => {
if (!stakedAmount || !totalStakedAmount || !rewardRate) return;
return new TokenAmount(
token,
JSBI.greaterThan(totalStakedAmount.raw, JSBI.BigInt(0))
? JSBI.divide(
JSBI.multiply(rewardRate.raw, stakedAmount.raw),
totalStakedAmount.raw,
)
: JSBI.BigInt(0),
);
};
const individualRewardRate = getHypotheticalRewardRate(
stakedAmount,
totalStakedAmount,
);
const periodFinishMs = syrupInfo.ending;
memo.push({
stakingRewardAddress: rewardsAddress,
token: syrupInfo.token,
ended: syrupInfo.ended,
name: syrupInfo.name,
lp: syrupInfo.lp,
periodFinish: periodFinishMs,
earnedAmount: initTokenAmountFromCallResult(
token,
earnedAmountState,
),
rewardRate: individualRewardRate,
totalRewardRate: totalRewardRate,
stakedAmount: stakedAmount,
totalStakedAmount: totalStakedAmount,
getHypotheticalRewardRate,
baseToken: syrupInfo.baseToken,
rate: syrupInfo.rate,
rewardTokenPriceinUSD: priceOfRewardTokenInUSD,
rewards,
stakingToken: syrupInfo.stakingToken,
valueOfTotalStakedAmountInUSDC: totalStakedAmount
? Number(totalStakedAmount.toExact()) * stakingTokenPrice
: undefined,
});
}
return memo;
},
[],
);
}, [
balances,
chainId,
earnedAmounts,
info,
rewardsAddresses,
totalSupplies,
uni,
rewardRates,
stakingTokenPairs,
usdBaseTokenPrices,
stakingTokenPrices,
]).filter((syrupInfo) =>
filter && filter.isStaked
? syrupInfo.stakedAmount && syrupInfo.stakedAmount.greaterThan('0')
: true,
);
}
Example #2
Source File: hooks.ts From interface-v2 with GNU General Public License v3.0 | 4 votes |
export function useOldSyrupInfo(
tokenToFilterBy?: Token | null,
startIndex?: number,
endIndex?: number,
filter?: { search: string; isStaked: boolean },
): SyrupInfo[] {
const { chainId, account } = useActiveWeb3React();
const currentTimestamp = dayjs().unix();
const info = useMemo(() => {
if (!chainId) return [];
const endedSyrupInfos =
returnSyrupInfo(false)[chainId]?.filter(
(syrupInfo) => syrupInfo.ending <= currentTimestamp,
) ?? [];
const oldSyrupInfos = returnSyrupInfo(true)[chainId] ?? [];
const allOldSyrupInfos = endedSyrupInfos.concat(oldSyrupInfos);
return allOldSyrupInfos
.slice(startIndex, endIndex)
.filter((syrupInfo) =>
tokenToFilterBy === undefined || tokenToFilterBy === null
? getSearchFiltered(syrupInfo, filter ? filter.search : '')
: tokenToFilterBy.equals(syrupInfo.token),
);
}, [
chainId,
tokenToFilterBy,
startIndex,
endIndex,
filter,
currentTimestamp,
]);
const uni = chainId ? GlobalValue.tokens.UNI[chainId] : undefined;
const rewardsAddresses = useMemo(
() => info.map(({ stakingRewardAddress }) => stakingRewardAddress),
[info],
);
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 stakingTokenPairs = usePairs(
info.map((item) => [
unwrappedToken(item.token),
unwrappedToken(item.baseToken),
]),
);
const usdBaseTokenPrices = useUSDCPrices(
info.map((item) => unwrappedToken(item.baseToken)),
);
const stakingTokenPrices = useUSDCPricesToken(
info.map((item) => item.stakingToken),
);
return useMemo(() => {
if (!chainId || !uni) return [];
return rewardsAddresses.reduce<SyrupInfo[]>(
(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 syrupInfo = info[index];
const stakingTokenPrice = stakingTokenPrices[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 token = syrupInfo.token;
// check for account, if no account set to 0
const rate = web3.utils.toWei(syrupInfo.rate.toString());
const stakedAmount = initTokenAmountFromCallResult(
getSyrupLPToken(syrupInfo),
balanceState,
);
const totalStakedAmount = initTokenAmountFromCallResult(
getSyrupLPToken(syrupInfo),
totalSupplyState,
);
const totalRewardRate = new TokenAmount(token, JSBI.BigInt(rate));
//const pair = info[index].pair.toLowerCase();
//const fees = (pairData && pairData[pair] ? pairData[pair].oneDayVolumeUSD * 0.0025: 0);
const rewardRate = initTokenAmountFromCallResult(
token,
rewardRateState,
);
const getHypotheticalRewardRate = (
stakedAmount?: TokenAmount,
totalStakedAmount?: TokenAmount,
): TokenAmount | undefined => {
if (!stakedAmount || !totalStakedAmount || !rewardRate) return;
return new TokenAmount(
token,
JSBI.greaterThan(totalStakedAmount.raw, JSBI.BigInt(0))
? JSBI.divide(
JSBI.multiply(rewardRate.raw, stakedAmount.raw),
totalStakedAmount.raw,
)
: JSBI.BigInt(0),
);
};
const individualRewardRate = getHypotheticalRewardRate(
stakedAmount,
totalStakedAmount,
);
const periodFinishMs = syrupInfo.ending;
const [, stakingTokenPair] = stakingTokenPairs[index];
const tokenPairPrice = stakingTokenPair?.priceOf(token);
const usdPriceBaseToken = usdBaseTokenPrices[index];
const priceOfRewardTokenInUSD =
Number(tokenPairPrice?.toSignificant()) *
Number(usdPriceBaseToken?.toSignificant());
memo.push({
stakingRewardAddress: rewardsAddress,
token: syrupInfo.token,
ended: true,
name: syrupInfo.name,
lp: syrupInfo.lp,
periodFinish: periodFinishMs,
earnedAmount: initTokenAmountFromCallResult(
token,
earnedAmountState,
),
rewardRate: individualRewardRate,
totalRewardRate: totalRewardRate,
stakedAmount: stakedAmount,
totalStakedAmount: totalStakedAmount,
getHypotheticalRewardRate,
baseToken: syrupInfo.baseToken,
rate: 0,
rewardTokenPriceinUSD: priceOfRewardTokenInUSD,
stakingToken: syrupInfo.stakingToken,
valueOfTotalStakedAmountInUSDC: totalStakedAmount
? Number(totalStakedAmount.toExact()) * stakingTokenPrice
: undefined,
});
}
return memo;
},
[],
);
}, [
balances,
chainId,
earnedAmounts,
info,
rewardsAddresses,
totalSupplies,
uni,
rewardRates,
stakingTokenPairs,
usdBaseTokenPrices,
stakingTokenPrices,
]).filter((syrupInfo) =>
filter && filter.isStaked
? syrupInfo.stakedAmount && syrupInfo.stakedAmount.greaterThan('0')
: true,
);
}
Example #3
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 #4
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,
);
}
Example #5
Source File: hooks.ts From interface-v2 with GNU General Public License v3.0 | 4 votes |
export function useOldStakingInfo(
pairToFilterBy?: Pair | null,
startIndex?: number,
endIndex?: number,
filter?: { search: string; isStaked: boolean },
): StakingInfo[] {
const { chainId, account } = useActiveWeb3React();
const info = useMemo(
() =>
chainId
? returnStakingInfo('old')
[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 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 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 stakingInfo = info[index];
if (
// these may be undefined if not logged in
!balanceState?.loading &&
!earnedAmountState?.loading &&
// always need these
totalSupplyState &&
!totalSupplyState.loading
) {
const stakedAmount = initTokenAmountFromCallResult(
getFarmLPToken(stakingInfo),
balanceState,
);
const totalStakedAmount = initTokenAmountFromCallResult(
getFarmLPToken(stakingInfo),
totalSupplyState,
);
const totalRewardRate = new TokenAmount(uni, JSBI.BigInt(0));
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,
totalRewardRate,
);
const [, stakingTokenPair] = stakingPairs[index];
memo.push({
stakingRewardAddress: rewardsAddress,
tokens: stakingInfo.tokens,
ended: stakingInfo.ended,
name: stakingInfo.name,
lp: stakingInfo.lp,
rewardToken: stakingInfo.rewardToken,
rewardTokenPrice: 0,
earnedAmount: initTokenAmountFromCallResult(uni, earnedAmountState),
rewardRate: individualRewardRate,
totalRewardRate: totalRewardRate,
stakedAmount: stakedAmount,
totalStakedAmount: totalStakedAmount,
baseToken: stakingInfo.baseToken,
getHypotheticalRewardRate,
pair: stakingInfo.pair,
rate: stakingInfo.rate,
oneYearFeeAPY: 0,
oneDayFee: 0,
accountFee: 0,
stakingTokenPair,
});
}
return memo;
},
[],
);
}, [
balances,
chainId,
earnedAmounts,
info,
rewardsAddresses,
totalSupplies,
uni,
stakingPairs,
]).filter((stakingInfo) =>
filter && filter.isStaked
? stakingInfo.stakedAmount && stakingInfo.stakedAmount.greaterThan('0')
: true,
);
}