Skip to content

Commit

Permalink
fix(governance): user and total veHoney amount
Browse files Browse the repository at this point in the history
  • Loading branch information
gonwi committed Sep 30, 2022
1 parent fce46cc commit 163da5e
Show file tree
Hide file tree
Showing 5 changed files with 833 additions and 5,579 deletions.
260 changes: 133 additions & 127 deletions contexts/GovernanceProvider.tsx
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>
</>
);
}
4 changes: 4 additions & 0 deletions helpers/sdk/vehoney.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,8 @@ export class VeHoneyClient extends ClientBase<VeHoney> {
async getAllEscrowAccounts() {
return this.program.account.escrow.all();
}

async getAllLockerAccounts() {
return this.program.account.locker.all();
}
}
34 changes: 22 additions & 12 deletions helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,30 +75,40 @@ export const convertBnTimestampToDate = (amount: anchor.BN): string => {

const date = new Date(timestamp * 1000);

const formattedTime = date.toLocaleDateString('en-us', { year:"numeric", month:"short", day:"numeric"})
const formattedTime = date.toLocaleDateString('en-us', {
year: 'numeric',
month: 'short',
day: 'numeric'
});
return formattedTime;
};

export const calcVeHoneyAmount = (
honeyAmount: anchor.BN,
startTimestamp: anchor.BN,
endTimestamp: anchor.BN,
honeyAmount: anchor.BN,
decimals: number = 6
multiplier: number,
maxStakeDuration: anchor.BN,
decimals: number
): number => {
const timestampStart = new anchor.BN(startTimestamp).toNumber();
const timestampEnd = new anchor.BN(endTimestamp).toNumber();
const honeyAmountHuman = convert(honeyAmount, decimals);
// const timestampStart = new anchor.BN(startTimestamp).toNumber();
// const timestampEnd = new anchor.BN(endTimestamp).toNumber();
// const honeyAmountHuman = convert(honeyAmount, decimals);

// const startDate = new Date(timestampStart).getTime();
// const endDate = new Date(timestampEnd).getTime();

// const vestingPeriod = endDate - startDate;

const startDate = new Date(timestampStart).getTime();
const endDate = new Date(timestampEnd).getTime();
// const vestingPeriodToWeek = Math.floor(vestingPeriod / (3600 * 24 * 7));

const vestingPeriod = endDate - startDate;
// const veHoneyAmount = honeyAmountHuman * (vestingPeriodToWeek * (1 / 208));

const vestingPeriodToWeek = Math.floor(vestingPeriod / (3600 * 24 * 7));
const duration = endTimestamp.sub(startTimestamp)
const veHoneyAmount = honeyAmount.mul(duration).muln(multiplier).div(maxStakeDuration).divn(10 ** decimals)

const veHoneyAmount = honeyAmountHuman * (vestingPeriodToWeek * (1 / 208));

return veHoneyAmount;
return new anchor.BN(veHoneyAmount).toNumber();;
};

export const convertToBN = (
Expand Down
15 changes: 13 additions & 2 deletions hooks/useStake.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,24 @@ export const useStake = (stakePool: PublicKey, locker: PublicKey) => {

const totalVeHoney = useCallback(async () => {
const allEscrowAccounts = await vc?.getAllEscrowAccounts();
const lockerAcc = await vc?.fetchLocker(locker);

let totalHoney: number = 0;

allEscrowAccounts?.forEach(escrow => {
allEscrowAccounts?.forEach(async escrow => {
const pubKey = escrow.publicKey;
const amount = escrow.account.amount;
const startDate = escrow.account.escrowStartedAt;
const endDate = escrow.account.escrowEndsAt;
const userVeHoneyAmount = calcVeHoneyAmount(startDate, endDate, amount);

const userVeHoneyAmount = calcVeHoneyAmount(
amount,
startDate,
endDate,
lockerAcc!.params.multiplier,
lockerAcc!.params.maxStakeDuration,
HONEY_DECIMALS
);

totalHoney += userVeHoneyAmount;
});
Expand Down
Loading

1 comment on commit 163da5e

@vercel
Copy link

@vercel vercel bot commented on 163da5e Oct 13, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.