-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(governance): user and total veHoney amount
- Loading branch information
Showing
5 changed files
with
833 additions
and
5,579 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,147 +1,153 @@ | ||
import { calcVeHoneyAmount, convert, convertBnTimestampToDate } from "helpers/utils"; | ||
import { createContext, useContext, ReactNode, useState, useMemo } from "react"; | ||
import { | ||
calcVeHoneyAmount, | ||
convert, | ||
convertBnTimestampToDate | ||
} from 'helpers/utils'; | ||
import { createContext, useContext, ReactNode, useState, useMemo } from 'react'; | ||
import { PublicKey } from '@solana/web3.js'; | ||
import { useStake } from 'hooks/useStake'; | ||
import { HONEY_DECIMALS, HONEY_MINT, PHONEY_DECIMALS, PHONEY_MINT } from "helpers/sdk"; | ||
import { useAccounts } from "hooks/useAccounts"; | ||
import config from "../config" | ||
import { | ||
HONEY_DECIMALS, | ||
HONEY_MINT, | ||
PHONEY_DECIMALS, | ||
PHONEY_MINT | ||
} from 'helpers/sdk'; | ||
import { useAccounts } from 'hooks/useAccounts'; | ||
import config from '../config'; | ||
|
||
type govContextType = { | ||
|
||
veHoneyAmount: number; | ||
lockedAmount: number; | ||
lockedPeriodEnd:string | number; | ||
pHoneyAmount: number; | ||
honeyAmount: number; | ||
depositedAmount: number; | ||
lockPeriodHasEnded: boolean; | ||
veHoneyAmount: number; | ||
lockedAmount: number; | ||
lockedPeriodEnd: string | number; | ||
pHoneyAmount: number; | ||
honeyAmount: number; | ||
depositedAmount: number; | ||
lockPeriodHasEnded: boolean; | ||
}; | ||
|
||
const govContextDefaultValues: govContextType = { | ||
|
||
veHoneyAmount: 0, | ||
lockedAmount: 0, | ||
lockedPeriodEnd: "", | ||
pHoneyAmount: 0, | ||
honeyAmount: 0, | ||
depositedAmount: 0, | ||
lockPeriodHasEnded: false | ||
veHoneyAmount: 0, | ||
lockedAmount: 0, | ||
lockedPeriodEnd: '', | ||
pHoneyAmount: 0, | ||
honeyAmount: 0, | ||
depositedAmount: 0, | ||
lockPeriodHasEnded: false | ||
}; | ||
|
||
const GovContext = createContext<govContextType>(govContextDefaultValues); | ||
|
||
export function useGovernance() { | ||
return useContext(GovContext); | ||
return useContext(GovContext); | ||
} | ||
|
||
type Props = { | ||
children: ReactNode; | ||
children: ReactNode; | ||
}; | ||
|
||
export function GovernanceProvider({ children }: Props) { | ||
const STAKE_POOL_ADDRESS = new PublicKey(config.NEXT_PUBLIC_STAKE_POOL_ADDRESS); | ||
const STAKE_POOL_ADDRESS = new PublicKey( | ||
config.NEXT_PUBLIC_STAKE_POOL_ADDRESS | ||
); | ||
const LOCKER_ADDRESS = new PublicKey(config.NEXT_PUBLIC_LOCKER_ADDR); | ||
|
||
const { user, escrow, totalVeHoney } = useStake( | ||
STAKE_POOL_ADDRESS, | ||
LOCKER_ADDRESS | ||
); | ||
|
||
// use token accounts for honey and phoney | ||
const { tokenAccounts } = useAccounts(); | ||
const pHoneyToken = tokenAccounts.find(t => t.info.mint.equals(PHONEY_MINT)); | ||
const honeyToken = tokenAccounts.find(t => t.info.mint.equals(HONEY_MINT)); | ||
|
||
// calculate user veHONEY amount locked | ||
const veHoneyAmount = useMemo(() => { | ||
if (!escrow) { | ||
return 0; | ||
} | ||
return calcVeHoneyAmount( | ||
escrow.escrowStartedAt, | ||
escrow.escrowEndsAt, | ||
escrow.amount | ||
); | ||
}, [escrow]); | ||
|
||
// | ||
const lockedAmount = useMemo(() => { | ||
if (!escrow) { | ||
return 0; | ||
} | ||
|
||
return convert(escrow.amount, HONEY_DECIMALS); | ||
}, [escrow]); | ||
|
||
// locked period end date | ||
const lockedPeriodEnd = useMemo(() => { | ||
if (!escrow) { | ||
return 0; | ||
} | ||
|
||
return convertBnTimestampToDate(escrow.escrowEndsAt); | ||
}, [escrow]); | ||
|
||
// pHONEY amount deposited | ||
const pHoneyAmount = useMemo(() => { | ||
if (!pHoneyToken) { | ||
return 0; | ||
} | ||
|
||
return convert(pHoneyToken.info.amount, PHONEY_DECIMALS); | ||
}, [pHoneyToken]); | ||
|
||
// HONEY amount deposited | ||
const honeyAmount = useMemo(() => { | ||
if (!honeyToken) { | ||
return 0; | ||
} | ||
|
||
return convert(honeyToken.info.amount, HONEY_DECIMALS); | ||
}, [honeyToken]); | ||
|
||
// amount of pHoney deposited by user | ||
const depositedAmount = useMemo(() => { | ||
if (!user) { | ||
return 0; | ||
} | ||
|
||
return convert(user.depositAmount, PHONEY_DECIMALS); | ||
}, [user]); | ||
|
||
// locked period has ended | ||
const lockPeriodHasEnded = useMemo((): boolean => { | ||
if (!escrow) { | ||
return true; | ||
} | ||
const lockEndsTimestamp = convert(escrow.escrowEndsAt, 0) * 1000; | ||
console.log("Lock Ends" + lockEndsTimestamp) | ||
const currentTimestamp = new Date().getTime(); | ||
console.log("Current Time" + currentTimestamp) | ||
// console.log("comparation" + if ) | ||
|
||
if (lockEndsTimestamp >= currentTimestamp) { | ||
return true; | ||
} | ||
return false; | ||
}, [escrow]); | ||
|
||
// the data that's passed to the provider | ||
const value = { | ||
veHoneyAmount, | ||
lockedAmount, | ||
lockedPeriodEnd, | ||
pHoneyAmount, | ||
honeyAmount, | ||
depositedAmount, | ||
lockPeriodHasEnded | ||
}; | ||
|
||
return ( | ||
<> | ||
<GovContext.Provider value={value}> | ||
{children} | ||
</GovContext.Provider> | ||
</> | ||
|
||
const { user, escrow, locker, totalVeHoney } = useStake( | ||
STAKE_POOL_ADDRESS, | ||
LOCKER_ADDRESS | ||
); | ||
|
||
// use token accounts for honey and phoney | ||
const { tokenAccounts } = useAccounts(); | ||
const pHoneyToken = tokenAccounts.find(t => t.info.mint.equals(PHONEY_MINT)); | ||
const honeyToken = tokenAccounts.find(t => t.info.mint.equals(HONEY_MINT)); | ||
|
||
// calculate user's veHONEY amount locked | ||
const veHoneyAmount = useMemo(() => { | ||
if (!escrow) { | ||
return 0; | ||
} | ||
return calcVeHoneyAmount( | ||
escrow.amount, | ||
escrow.escrowStartedAt, | ||
escrow.escrowEndsAt, | ||
locker.params.multiplier, | ||
locker.params.maxStakeDuration, | ||
HONEY_DECIMALS | ||
); | ||
} | ||
}, [escrow]); | ||
|
||
const lockedAmount = useMemo(() => { | ||
if (!escrow) { | ||
return 0; | ||
} | ||
|
||
return convert(escrow.amount, HONEY_DECIMALS); | ||
}, [escrow]); | ||
|
||
// locked period end date | ||
const lockedPeriodEnd = useMemo(() => { | ||
if (!escrow) { | ||
return 0; | ||
} | ||
|
||
return convertBnTimestampToDate(escrow.escrowEndsAt); | ||
}, [escrow]); | ||
|
||
// pHONEY amount deposited | ||
const pHoneyAmount = useMemo(() => { | ||
if (!pHoneyToken) { | ||
return 0; | ||
} | ||
|
||
return convert(pHoneyToken.info.amount, PHONEY_DECIMALS); | ||
}, [pHoneyToken]); | ||
|
||
// HONEY amount deposited | ||
const honeyAmount = useMemo(() => { | ||
if (!honeyToken) { | ||
return 0; | ||
} | ||
|
||
return convert(honeyToken.info.amount, HONEY_DECIMALS); | ||
}, [honeyToken]); | ||
|
||
// amount of pHoney deposited by user | ||
const depositedAmount = useMemo(() => { | ||
if (!user) { | ||
return 0; | ||
} | ||
|
||
return convert(user.depositAmount, PHONEY_DECIMALS); | ||
}, [user]); | ||
|
||
// locked period has ended | ||
const lockPeriodHasEnded = useMemo((): boolean => { | ||
if (!escrow) { | ||
return true; | ||
} | ||
const lockEndsTimestamp = convert(escrow.escrowEndsAt, 0) * 1000; | ||
const currentTimestamp = new Date().getTime(); | ||
|
||
if (lockEndsTimestamp >= currentTimestamp) { | ||
return true; | ||
} | ||
return false; | ||
}, [escrow]); | ||
|
||
// the data that's passed to the provider | ||
const value = { | ||
veHoneyAmount, | ||
lockedAmount, | ||
lockedPeriodEnd, | ||
pHoneyAmount, | ||
honeyAmount, | ||
depositedAmount, | ||
lockPeriodHasEnded | ||
}; | ||
|
||
return ( | ||
<> | ||
<GovContext.Provider value={value}>{children}</GovContext.Provider> | ||
</> | ||
); | ||
} |
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
Oops, something went wrong.
163da5e
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.
Successfully deployed to the following URLs:
honey-frontend-bks5 – ./
honey-frontend-bks5-git-development-honey-labs.vercel.app
honey-frontend-bks5.vercel.app
honey-frontend-bks5-honey-labs.vercel.app