import { useEffect , useState } from "react" ; import { Jar } from "./useFetchJars" ; import { ChainName } from "containers/config" ; import { PickleCore } from "./usePickleCore" ; import { Prices } from "../Prices" ; export interface JarApy { [ k ] : number ; } const getCompoundingAPY = ( apr ) => { return 100 * ( Math . pow ( 1 + apr / 365 , 365 ) - 1 ) ; } ; export interface JarWithAPY extends Jar { totalAPY : number ; apr : number ; lp : number ; APYs : Array < JarApy > ; } type Input = Array < Jar > | null ; type Output = { jarsWithAPY : Array < JarWithAPY > | null ; } ; export const useJarWithAPY = ( network , jars ) : Output => { const { pickleCore } = PickleCore . useContainer ( ) ; const { prices } = Prices . useContainer ( ) ; const [ jarsWithAPY , setJarsWithAPY ] = useState < Array < JarWithAPY > | null > ( null ) ; const calculateJarAPYs = ( jar ) => { if ( pickleCore ) { const aprStats = pickleCore . assets . jars . filter ( ( pFJar ) => jar . apiKey === pFJar . details . apiKey ) [ 0 ] ?. aprStats ; let lp = 0 ; if ( aprStats ) { const componentsAPYs = aprStats . components . map ( ( component ) => { const apr = ! isNaN ( component . apr ) ? + component . apr : 0 ; if ( component . name . toLowerCase ( ) === "lp" ) { lp = apr ; } return { [ component . name ] : component . compoundable ? getCompoundingAPY ( component . apr / 100 ) : apr , } ; } ) ; return { APYs : componentsAPYs , apr : aprStats ? aprStats . apr : 0 , totalAPY : aprStats ? aprStats . apy : 0 , lp : lp , } ; } } return [ ] ; } ; const calculateAPY = async ( ) => { if ( jars && pickleCore ) { const results = jars . map ( ( jar ) => { interface JarData { APYs : JarApy [ ] ; apr : number ; totalAPY : number ; lp : number ; } const jarData = < JarData > calculateJarAPYs ( jar ) ; return { ... jar , ... jarData , } ; } ) ; setJarsWithAPY ( results ) ; } } ; useEffect ( ( ) => { calculateAPY ( ) ; } , [ jars ?. length , network , pickleCore , prices ] ) ; return { jarsWithAPY } ; } ;