utils#returnStakingInfo TypeScript Examples
The following examples show how to use
utils#returnStakingInfo.
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: RewardSlider.tsx From interface-v2 with GNU General Public License v3.0 | 5 votes |
RewardSlider: React.FC = () => {
const classes = useStyles();
const theme = useTheme();
const { chainId } = useActiveWeb3React();
const tabletWindowSize = useMediaQuery(theme.breakpoints.down('md'));
const mobileWindowSize = useMediaQuery(theme.breakpoints.down('sm'));
const rewardItems = useStakingInfo(null, 0, 5);
const [bulkPairs, setBulkPairs] = useState<any>(null);
useEffect(() => {
if (chainId) {
const stakingPairLists =
returnStakingInfo()
[chainId]?.slice(0, 5)
.map((item) => item.pair) ?? [];
getBulkPairData(stakingPairLists).then((data) => setBulkPairs(data));
}
}, [chainId]);
const stakingAPYs = useMemo(() => {
if (bulkPairs && rewardItems.length > 0) {
return rewardItems.map((info: StakingInfo) => {
const oneDayVolume = bulkPairs[info.pair]?.oneDayVolumeUSD;
const reserveUSD = bulkPairs[info.pair]?.reserveUSD;
if (oneDayVolume && reserveUSD) {
return getOneYearFee(oneDayVolume, reserveUSD);
} else {
return 0;
}
});
} else {
return [];
}
}, [bulkPairs, rewardItems]);
const rewardSliderSettings = {
dots: false,
infinite: true,
speed: 500,
slidesToShow: mobileWindowSize ? 1 : tabletWindowSize ? 2 : 3,
slidesToScroll: 1,
nextArrow: <ChevronRightIcon />,
prevArrow: <ChevronLeftIcon />,
};
return (
<Slider {...rewardSliderSettings} className={classes.rewardsSlider}>
{rewardItems.map((item, index) => (
<RewardSliderItem
key={index}
stakingAPY={stakingAPYs[index]}
info={item}
/>
))}
</Slider>
);
}
Example #2
Source File: FarmPage.tsx From interface-v2 with GNU General Public License v3.0 | 5 votes |
FarmPage: React.FC = () => {
const classes = useStyles();
const { chainId } = useActiveWeb3React();
const [bulkPairs, setBulkPairs] = useState<any>(null);
const [farmIndex, setFarmIndex] = useState(
GlobalConst.farmIndex.LPFARM_INDEX,
);
useEffect(() => {
if (chainId) {
const stakingPairLists =
returnStakingInfo()[chainId]?.map((item) => item.pair) ?? [];
const stakingOldPairLists =
returnStakingInfo('old')[chainId]?.map((item) => item.pair) ?? [];
const dualPairLists =
returnDualStakingInfo()[chainId]?.map((item) => item.pair) ?? [];
const pairLists = stakingPairLists
.concat(stakingOldPairLists)
.concat(dualPairLists);
getBulkPairData(pairLists).then((data) => setBulkPairs(data));
}
return () => setBulkPairs(null);
}, [chainId]);
const farmCategories = [
{
text: 'LP Mining',
onClick: () => setFarmIndex(GlobalConst.farmIndex.LPFARM_INDEX),
condition: farmIndex === GlobalConst.farmIndex.LPFARM_INDEX,
},
{
text: 'Dual Mining',
onClick: () => setFarmIndex(GlobalConst.farmIndex.DUALFARM_INDEX),
condition: farmIndex === GlobalConst.farmIndex.DUALFARM_INDEX,
},
];
return (
<Box width='100%' mb={3} id='farmPage'>
<Box
display='flex'
alignItems='flex-start'
justifyContent='space-between'
width='100%'
mb={2}
>
<Box mr={2}>
<Typography variant='h4'>Farm</Typography>
</Box>
<Box className={classes.helpWrapper}>
<Typography variant='body2'>Help</Typography>
<HelpIcon />
</Box>
</Box>
<CustomSwitch
width={300}
height={48}
items={farmCategories}
isLarge={true}
/>
<Box my={2}>
<FarmRewards bulkPairs={bulkPairs} farmIndex={farmIndex} />
</Box>
<Box className={classes.dragonWrapper}>
<FarmsList bulkPairs={bulkPairs} farmIndex={farmIndex} />
</Box>
</Box>
);
}
Example #3
Source File: hooks.ts From interface-v2 with GNU General Public License v3.0 | 5 votes |
export function useTotalRewardsDistributed() {
const { chainId } = useActiveWeb3React();
const syrupRewardsInfo = chainId ? returnSyrupInfo()[chainId] ?? [] : [];
const dualStakingRewardsInfo = chainId
? returnDualStakingInfo()[chainId] ?? []
: [];
const stakingRewardsInfo = chainId ? returnStakingInfo()[chainId] ?? [] : [];
const syrupTokenPairs = usePairs(
syrupRewardsInfo.map((item) => [
unwrappedToken(item.token),
unwrappedToken(item.baseToken),
]),
);
const syrupUSDBaseTokenPrices = useUSDCPrices(
syrupRewardsInfo.map((item) => unwrappedToken(item.baseToken)),
);
const syrupRewardsUSD = syrupRewardsInfo.reduce((total, item, index) => {
const [, syrupTokenPair] = syrupTokenPairs[index];
const tokenPairPrice = syrupTokenPair?.priceOf(item.token);
const usdPriceBaseToken = syrupUSDBaseTokenPrices[index];
const priceOfRewardTokenInUSD =
Number(tokenPairPrice?.toSignificant()) *
Number(usdPriceBaseToken?.toSignificant());
return total + priceOfRewardTokenInUSD * item.rate;
}, 0);
const rewardTokenAPrices = useUSDCPricesToken(
dualStakingRewardsInfo.map((item) => item.rewardTokenA),
);
const rewardTokenBPrices = useUSDCPricesToken(
dualStakingRewardsInfo.map((item) => item.rewardTokenB),
);
const dualStakingRewardsUSD = dualStakingRewardsInfo.reduce(
(total, item, index) =>
total +
item.rateA * rewardTokenAPrices[index] +
item.rateB * rewardTokenBPrices[index],
0,
);
const rewardTokenPrices = useUSDCPricesToken(
stakingRewardsInfo.map((item) => item.rewardToken),
);
const stakingRewardsUSD = stakingRewardsInfo.reduce(
(total, item, index) => total + item.rate * rewardTokenPrices[index],
0,
);
return syrupRewardsUSD + dualStakingRewardsUSD + stakingRewardsUSD;
}
Example #4
Source File: hooks.ts From interface-v2 with GNU General Public License v3.0 | 5 votes |
export function useUSDRewardsandFees(isLPFarm: boolean, bulkPairData: any) {
const { chainId } = useActiveWeb3React();
const dualStakingRewardsInfo =
chainId && !isLPFarm ? returnDualStakingInfo()[chainId] ?? [] : [];
const stakingRewardsInfo =
chainId && isLPFarm ? returnStakingInfo()[chainId] ?? [] : [];
const rewardsInfos = isLPFarm ? stakingRewardsInfo : dualStakingRewardsInfo;
const rewardsAddresses = useMemo(
() => rewardsInfos.map(({ stakingRewardAddress }) => stakingRewardAddress),
[rewardsInfos],
);
const stakingRewardTokens = stakingRewardsInfo.map(
(item) => item.rewardToken,
);
const stakingRewardTokenPrices = useUSDCPricesToken(stakingRewardTokens);
const dualStakingRewardTokenAPrices = useUSDCPricesToken(
dualStakingRewardsInfo.map((item) => item.rewardTokenA),
);
const dualStakingRewardTokenBPrices = useUSDCPricesToken(
dualStakingRewardsInfo.map((item) => item.rewardTokenB),
);
const rewardPairs = useMemo(() => rewardsInfos.map(({ pair }) => pair), [
rewardsInfos,
]);
const totalSupplies = useMultipleContractSingleData(
rewardsAddresses,
isLPFarm ? STAKING_REWARDS_INTERFACE : STAKING_DUAL_REWARDS_INTERFACE,
'totalSupply',
);
let rewardsUSD: number | null = null;
if (isLPFarm) {
rewardsUSD = stakingRewardsInfo.reduce(
(total, item, index) =>
total + item.rate * stakingRewardTokenPrices[index],
0,
);
} else {
rewardsUSD = dualStakingRewardsInfo.reduce(
(total, item, index) =>
total +
item.rateA * dualStakingRewardTokenAPrices[index] +
item.rateB * dualStakingRewardTokenBPrices[index],
0,
);
}
const stakingFees = bulkPairData
? rewardPairs.reduce((total, pair, index) => {
const oneYearFeeAPY = Number(bulkPairData[pair]?.oneDayVolumeUSD ?? 0);
const totalSupplyState = totalSupplies[index];
if (oneYearFeeAPY) {
const totalSupply = web3.utils.toWei(
pairs[pair]?.totalSupply,
'ether',
);
const ratio =
Number(totalSupplyState.result?.[0].toString()) /
Number(totalSupply);
const oneDayFee =
oneYearFeeAPY * GlobalConst.utils.FEEPERCENT * ratio;
return total + oneDayFee;
} else {
return total;
}
}, 0)
: null;
return { rewardsUSD, stakingFees };
}
Example #5
Source File: FarmRewards.tsx From interface-v2 with GNU General Public License v3.0 | 4 votes |
FarmRewards: React.FC<{ farmIndex: number; bulkPairs: any }> = ({
farmIndex,
bulkPairs,
}) => {
const { palette, breakpoints } = useTheme();
const { chainId } = useActiveWeb3React();
const isMobile = useMediaQuery(breakpoints.down('xs'));
const farmData = useUSDRewardsandFees(
farmIndex === GlobalConst.farmIndex.LPFARM_INDEX,
bulkPairs,
);
const dQuickRewardSum = useMemo(() => {
if (!chainId) return 0;
const stakingData = returnStakingInfo()[chainId] ?? [];
const rewardSum = stakingData.reduce((total, item) => total + item.rate, 0);
return rewardSum;
}, [chainId]);
const getRewardsSection = (isLPFarm: boolean) => (
<>
<Box
width={isMobile ? 1 : isLPFarm ? 1 / 3 : 1 / 2}
p={1.5}
borderRight={isMobile ? 'none' : `1px solid ${palette.divider}`}
textAlign='center'
>
<Box mb={1}>
<Typography variant='caption' color='textSecondary'>
Total Rewards
</Typography>
</Box>
{farmData.rewardsUSD ? (
<Typography variant='subtitle2' style={{ fontWeight: 600 }}>
${farmData.rewardsUSD.toLocaleString()} / Day
</Typography>
) : (
<Skeleton width='100%' height='28px' />
)}
</Box>
<Box
width={isMobile ? 1 : isLPFarm ? 1 / 3 : 1 / 2}
p={1.5}
textAlign='center'
>
<Box mb={1}>
<Typography variant='caption' color='textSecondary'>
Fees [24h]
</Typography>
</Box>
{farmData.stakingFees ? (
<Typography variant='subtitle2' style={{ fontWeight: 600 }}>
${farmData.stakingFees.toLocaleString()}
</Typography>
) : (
<Skeleton width='100%' height='28px' />
)}
</Box>
</>
);
return (
<Box
display='flex'
flexWrap='wrap'
my={2}
borderRadius={10}
py={1.5}
bgcolor={palette.secondary.dark}
>
{farmIndex === GlobalConst.farmIndex.LPFARM_INDEX && (
<>
<Box
width={isMobile ? 1 : 1 / 3}
py={1.5}
borderRight={`1px solid ${palette.divider}`}
textAlign='center'
>
<Box mb={1}>
<Typography variant='caption' color='textSecondary'>
Reward Rate
</Typography>
</Box>
<Typography variant='subtitle2' style={{ fontWeight: 600 }}>
{dQuickRewardSum.toLocaleString()} dQuick / Day
</Typography>
</Box>
{getRewardsSection(true)}
</>
)}
{farmIndex === GlobalConst.farmIndex.DUALFARM_INDEX &&
getRewardsSection(false)}
</Box>
);
}
Example #6
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 #7
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,
);
}