Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NuCypher tvl #86

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/hooks/useFetchTvl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
useKeepAssetPoolContract,
useTStakingContract,
useKeepTokenStakingContract,
useContract,
} from "../web3/hooks"
import { useETHData } from "./useETHData"
import { useToken } from "./useToken"
Expand All @@ -20,6 +21,8 @@ interface TVLRawData {
keepCoveragePoolTVL: string
keepStakingTVL: string
tStakingTVL: string
stakedNU: string
ethInNuNetwork: string
// TODO: add PRE and NU TVL
}

Expand All @@ -38,8 +41,29 @@ const initialState = {
keepCoveragePoolTVL: "0",
keepStakingTVL: "0",
tStakingTVL: "0",
stakedNU: "0",
ethInNuNetwork: "0",
}

// TODO: Get contract abi from the package and figure out how to use these
// contracts on ropste and local network. This only works on mainnet.
const ESCROW_ABI = [
{
inputs: [],
name: "currentPeriodSupply",
outputs: [{ internalType: "uint128", name: "", type: "uint128" }],
stateMutability: "view",
type: "function",
},
]

const ESCROW_ADDRESS = "0xbbD3C0C794F40c4f993B03F65343aCC6fcfCb2e2"
const useNuStakingEscrowContract = () => {
return useContract(ESCROW_ADDRESS, ESCROW_ABI)
}

const WORK_LOCK_ADDRESS = "0xe9778E69a961e64d3cdBB34CF6778281d34667c2"

export const useFetchTvl = (): [TVLData, () => Promise<TVLRawData>] => {
const [rawData, setRawData] = useState<TVLRawData>(initialState)
const {
Expand All @@ -48,12 +72,16 @@ export const useFetchTvl = (): [TVLData, () => Promise<TVLRawData>] => {
keepCoveragePoolTVL,
keepStakingTVL,
tStakingTVL,
stakedNU,
ethInNuNetwork,
} = rawData

const eth = useETHData()
const keep = useToken(Token.Keep)
const tbtc = useToken(Token.TBTC)
const t = useToken(Token.T)
const nu = useToken(Token.Nu)
const nuStakingEscrow = useNuStakingEscrowContract()
const keepBonding = useKeepBondingContract()
const multicall = useMulticallContract()
const keepAssetPool = useKeepAssetPoolContract()
Expand Down Expand Up @@ -81,6 +109,24 @@ export const useFetchTvl = (): [TVLData, () => Promise<TVLRawData>] => {
method: "balanceOf",
args: [tTokenStaking?.address],
},
{
contract: nu.contract!,
method: "totalSupply",
},
{
contract: nuStakingEscrow!,
method: "currentPeriodSupply",
},
{
contract: nu.contract!,
method: "balanceOf",
args: [nuStakingEscrow?.address],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you clarify why we're tracking here the NU balance of staking escrow?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH I implemented based on the https://github.com/nucypher/nucypher-monitor/blob/main/monitor/dashboard.py#L146-L163. Any tips on how to calculate NU TVL correctly would be appreciated 🙂

},
{
contract: multicall!,
method: "getEthBalance",
args: [WORK_LOCK_ADDRESS],
},
])

const fetchTVLData = useCallback(async () => {
Expand All @@ -93,14 +139,28 @@ export const useFetchTvl = (): [TVLData, () => Promise<TVLRawData>] => {
coveragePoolTvl,
keepStaking,
tStaking,
nuTotalSupply,
nuCurrenctPeriodSupply,
nuInEscrow,
ethInNuNetwork,
] = chainData.map((amount: string) => formatUnits(amount.toString()))

const stakedNU = FixedNumber.fromString(nuInEscrow)
.subUnsafe(
FixedNumber.fromString(nuTotalSupply).subUnsafe(
FixedNumber.fromString(nuCurrenctPeriodSupply)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This equation seems a bit complex and I'm not sure if this yields a correct figure. Depending on what's the goal, i think there are other endpoints that are simpler as input.

)
)
.toString()

const data: TVLRawData = {
ecdsaTVL: ethInKeepBonding,
tbtcTVL: tbtcTokenTotalSupply,
keepCoveragePoolTVL: coveragePoolTvl,
keepStakingTVL: keepStaking,
tStakingTVL: tStaking,
stakedNU,
ethInNuNetwork,
}
setRawData(data)

Expand All @@ -121,17 +181,23 @@ export const useFetchTvl = (): [TVLData, () => Promise<TVLRawData>] => {

const tStaking = toUsdBalance(tStakingTVL, t.usdConversion)

const stakedNuInUSD = toUsdBalance(stakedNU, nu.usdConversion)
const ethInNu = toUsdBalance(ethInNuNetwork, eth.usdPrice)
const totalNu = stakedNuInUSD.addUnsafe(ethInNu)

return {
ecdsa: ecdsa.toString(),
tbtc: tbtcUSD.toString(),
keepCoveragePool: keepCoveragePool.toString(),
keepStaking: keepStaking.toString(),
tStaking: tStaking.toString(),
nu: totalNu.toString(),
total: ecdsa
.addUnsafe(tbtcUSD)
.addUnsafe(keepCoveragePool)
.addUnsafe(keepStaking)
.addUnsafe(tStaking)
.addUnsafe(totalNu)
.toString(),
} as TVLData
}, [
Expand All @@ -144,6 +210,9 @@ export const useFetchTvl = (): [TVLData, () => Promise<TVLRawData>] => {
keep.usdConversion,
tbtc.usdConversion,
t.usdConversion,
stakedNU,
ethInNuNetwork,
nu.usdConversion,
])

return [data, fetchTVLData]
Expand Down