-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
previewPoints
on contract to compute APY (#390)
* Use `previewPoints` on contract to compute APY * Fix veOGV Amount
- Loading branch information
1 parent
23d101b
commit 58faa97
Showing
7 changed files
with
107 additions
and
78 deletions.
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
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
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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { useEffect, useMemo, useState } from "react"; | ||
import { SECONDS_IN_A_MONTH } from "constants/index"; | ||
import { useStore } from "utils/store"; | ||
import { getRewardsApy } from "./apy"; | ||
import { ethers } from "ethers"; | ||
|
||
const useStakingAPY = (amountStaked, duration) => { | ||
const { contracts, totalBalances } = useStore(); | ||
|
||
const [loading, setLoading] = useState(true); | ||
const [veOgvReceived, setVeOGVReceived] = useState(0); | ||
|
||
const { totalSupplyVeOgvAdjusted } = totalBalances; | ||
|
||
useEffect(() => { | ||
if (!contracts.loaded) return; | ||
|
||
let done = false; | ||
|
||
async function go() { | ||
try { | ||
const [val] = await contracts.OgvStaking.previewPoints( | ||
ethers.utils.parseEther(amountStaked.toString()), | ||
duration * SECONDS_IN_A_MONTH | ||
); | ||
|
||
if (!done) { | ||
setVeOGVReceived(parseFloat(ethers.utils.formatEther(val))); | ||
setLoading(false); | ||
} | ||
} catch (err) { | ||
console.error("Failed to fetch APY", err); | ||
} | ||
} | ||
|
||
go(); | ||
|
||
return () => { | ||
done = true; | ||
}; | ||
}, [amountStaked, duration, contracts.loaded]); | ||
|
||
const stakingAPY = useMemo(() => { | ||
if (!veOgvReceived || !totalSupplyVeOgvAdjusted || !amountStaked) { | ||
return 0; | ||
} | ||
|
||
return getRewardsApy(veOgvReceived, amountStaked, totalSupplyVeOgvAdjusted); | ||
}, [veOgvReceived, amountStaked, totalSupplyVeOgvAdjusted]); | ||
|
||
return { | ||
loading, | ||
veOgvReceived, | ||
stakingAPY, | ||
}; | ||
}; | ||
|
||
export default useStakingAPY; |