-
Notifications
You must be signed in to change notification settings - Fork 27
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
r-czajkowski
wants to merge
3
commits into
main
Choose a base branch
from
nu-tvl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
NuCypher tvl #86
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import { | |
useKeepAssetPoolContract, | ||
useTStakingContract, | ||
useKeepTokenStakingContract, | ||
useContract, | ||
} from "../web3/hooks" | ||
import { useETHData } from "./useETHData" | ||
import { useToken } from "./useToken" | ||
|
@@ -20,6 +21,8 @@ interface TVLRawData { | |
keepCoveragePoolTVL: string | ||
keepStakingTVL: string | ||
tStakingTVL: string | ||
stakedNU: string | ||
ethInNuNetwork: string | ||
// TODO: add PRE and NU TVL | ||
} | ||
|
||
|
@@ -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 { | ||
|
@@ -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() | ||
|
@@ -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], | ||
}, | ||
{ | ||
contract: multicall!, | ||
method: "getEthBalance", | ||
args: [WORK_LOCK_ADDRESS], | ||
}, | ||
]) | ||
|
||
const fetchTVLData = useCallback(async () => { | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
||
|
@@ -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 | ||
}, [ | ||
|
@@ -144,6 +210,9 @@ export const useFetchTvl = (): [TVLData, () => Promise<TVLRawData>] => { | |
keep.usdConversion, | ||
tbtc.usdConversion, | ||
t.usdConversion, | ||
stakedNU, | ||
ethInNuNetwork, | ||
nu.usdConversion, | ||
]) | ||
|
||
return [data, fetchTVLData] | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 🙂