Skip to content

Commit

Permalink
Merge pull request #2300 from DistributedCollective/feat/zero-tvl
Browse files Browse the repository at this point in the history
Add Zero to TVL stats (#2288)
  • Loading branch information
soulBit authored Jun 30, 2022
2 parents d688562 + 92cf342 commit f838400
Show file tree
Hide file tree
Showing 8 changed files with 3,204 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/app/containers/StatsPage/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export type TvlData = {
totalBtc: number;
totalUsd: number;
};
tvlZero: {
totalBtc: number;
totalUsd: number;
};
total_btc: number;
total_usd: number;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ export const TotalValueLocked: React.FC<ITotalValueLockedProps> = ({
btcValue: data?.tvlAmm?.totalBtc || 0,
usdValue: data?.tvlAmm?.totalUsd || 0,
},
{
contract: t(translations.landingPage.tvl.zero),
btcValue: data?.tvlZero?.totalBtc || 0,
usdValue: data?.tvlZero?.totalUsd || 0,
},
{
contract: t(translations.landingPage.tvl.subProtocol),
btcValue: data?.tvlSubprotocols?.totalBtc || 0,
Expand Down
90 changes: 88 additions & 2 deletions src/app/pages/LandingPage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useRef, useState } from 'react';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { Helmet } from 'react-helmet-async';
import { useTranslation } from 'react-i18next';
import { translations } from 'locales/i18n';
Expand All @@ -19,6 +19,13 @@ import { CryptocurrencyPrices } from './components/CryptocurrencyPrices';
import { IAssets } from './components/CryptocurrencyPrices/types';
import styles from './index.module.scss';
import { IPairsData } from 'types/trading-pairs';
import { numberFromWei } from 'utils/blockchain/math-helpers';
import { bridgeNetwork } from '../BridgeDepositPage/utils/bridge-network';
import { Asset, Chain } from 'types';
import { getContract } from 'utils/blockchain/contract-helpers';
import { useDenominateAssetAmount } from 'app/hooks/trading/useDenominateAssetAmount';
import { useDollarValue } from 'app/hooks/useDollarValue';
import { bignumber } from 'mathjs';

const url = backendUrl[currentChainId];

Expand All @@ -27,7 +34,7 @@ interface ILandingPageProps {
}

export const LandingPage: React.FC<ILandingPageProps> = ({
refreshInterval = 300000,
refreshInterval = 600000,
}) => {
const { t } = useTranslation();

Expand All @@ -37,6 +44,12 @@ export const LandingPage: React.FC<ILandingPageProps> = ({
const [pairsData, setPairsData] = useState<IPairsData>();
const [assetLoading, setAssetLoading] = useState(false);
const [assetData, setAssetData] = useState<IAssets>();
const [zusdDepositsWeiAmount, setZusdDepositsWeiAmount] = useState('0');
const [
rbtcCollateralValueWeiAmount,
setRbtcCollateralValueWeiAmount,
] = useState('0');
const [zeroLoading, setZeroLoading] = useState(false);

const cancelDataRequest = useRef<Canceler>();
const cancelPairsDataRequest = useRef<Canceler>();
Expand All @@ -62,6 +75,36 @@ export const LandingPage: React.FC<ILandingPageProps> = ({
});
}, []);

const getZeroTvlData = useCallback(() => {
setZeroLoading(true);

bridgeNetwork
.multiCall(Chain.RSK, [
{
address: getContract('zero_troveManager').address,
abi: getContract('zero_troveManager').abi,
fnName: 'getEntireSystemColl',
args: [],
key: 'rbtcCollateral',
parser: value => value[0].toString(),
},
{
address: getContract('zero_stabilityPool').address,
abi: getContract('zero_stabilityPool').abi,
fnName: 'getTotalZUSDDeposits',
args: [],
key: 'zusdDeposits',
parser: value => value[0].toString(),
},
])
.then(result => {
setZusdDepositsWeiAmount(result.returnData.zusdDeposits);
setRbtcCollateralValueWeiAmount(result.returnData.rbtcCollateral);
})
.catch(e => console.error(e))
.finally(() => setZeroLoading(false));
}, []);

const getPairsData = useCallback(() => {
setPairsLoading(true);
cancelPairsDataRequest.current && cancelPairsDataRequest.current();
Expand Down Expand Up @@ -105,13 +148,56 @@ export const LandingPage: React.FC<ILandingPageProps> = ({
useInterval(
() => {
getTvlData();
getZeroTvlData();
getPairsData();
getAssetData();
},
refreshInterval,
{ immediate: true },
);

const zusdDepositRbtcValue = useDenominateAssetAmount(
Asset.XUSD, // we cannot use Asset.ZUSD but 1 ZUSD === 1 XUSD
Asset.RBTC,
zusdDepositsWeiAmount,
);

const rbtcCollateralUsdValue = useDollarValue(
Asset.RBTC,
rbtcCollateralValueWeiAmount,
);

useEffect(() => {
if (
!zeroLoading &&
rbtcCollateralValueWeiAmount !== '0' &&
zusdDepositsWeiAmount !== '0'
) {
const zeroTotalRbtc = numberFromWei(
bignumber(zusdDepositRbtcValue.value)
.add(rbtcCollateralValueWeiAmount)
.toString(),
);
const zeroTotalUsd = numberFromWei(
bignumber(zusdDepositsWeiAmount)
.add(rbtcCollateralUsdValue.value)
.toString(),
);
setTvlData(tvlData => ({
...tvlData!,
tvlZero: { totalBtc: zeroTotalRbtc, totalUsd: zeroTotalUsd },
total_btc: (tvlData?.total_btc || 0) + zeroTotalRbtc,
total_usd: (tvlData?.total_usd || 0) + zeroTotalUsd,
}));
}
}, [
rbtcCollateralUsdValue.value,
rbtcCollateralValueWeiAmount,
zeroLoading,
zusdDepositRbtcValue.value,
zusdDepositsWeiAmount,
]);

return (
<>
<Helmet>
Expand Down
7 changes: 4 additions & 3 deletions src/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -2587,10 +2587,11 @@
"btc": "Value (BTC)",
"usd": "Value (USD)",
"protocol": "Protocol Contract",
"lend": "Lending Contract",
"amm": "Amm Contract",
"lend": "Lending Contracts",
"amm": "AMM Contracts",
"zero": "Zero Contracts",
"staked": "Bitocracy Staking Contract",
"subProtocol": "Subprotocol Bonding Curve Contracts",
"subProtocol": "MYNT Bonding Curve Contract",
"subtotal": "Sub total",
"total": "Total"
},
Expand Down
Loading

0 comments on commit f838400

Please sign in to comment.