Skip to content

Commit

Permalink
fix: price
Browse files Browse the repository at this point in the history
  • Loading branch information
Majorfi committed Oct 17, 2023
1 parent c4b0ef6 commit 6ac94a9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
11 changes: 6 additions & 5 deletions apps/veyfi/components/RedeemTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {validateAllowance, validateAmount} from '@veYFI/utils/validations';
import {Button} from '@yearn-finance/web-lib/components/Button';
import {useWeb3} from '@yearn-finance/web-lib/contexts/useWeb3';
import {toAddress} from '@yearn-finance/web-lib/utils/address';
import {ETH_TOKEN_ADDRESS} from '@yearn-finance/web-lib/utils/constants';
import {ETH_TOKEN_ADDRESS, YFI_ADDRESS} from '@yearn-finance/web-lib/utils/constants';
import {toNormalizedBN} from '@yearn-finance/web-lib/utils/format.bigNumber';
import {formatCounterValue} from '@yearn-finance/web-lib/utils/format.value';
import {handleInputChangeEventValue} from '@yearn-finance/web-lib/utils/handlers/handleInputChangeEventValue';
Expand All @@ -24,16 +24,17 @@ export function RedeemTab(): ReactElement {
const [redeemAmount, set_redeemAmount] = useState(toNormalizedBN(0));
const {provider, address, isActive} = useWeb3();
const {refresh: refreshBalances} = useWallet();
const {getRequiredEth, price: optionPrice, position: dYFIBalance, allowances, refresh} = useOption();
const {getRequiredEth, position: dYFIBalance, allowances, refresh, dYFIPrice} = useOption();
const clearLockAmount = (): void => set_redeemAmount(toNormalizedBN(0));
const refreshData = useCallback((): unknown => Promise.all([refresh(), refreshBalances()]), [refresh, refreshBalances]);
const onTxSuccess = useCallback((): unknown => Promise.all([refreshData(), clearLockAmount()]), [refreshData]);
const ethBalance = useBalance(ETH_TOKEN_ADDRESS);
const dYFIPrice = useTokenPrice(VEYFI_DYFI_ADDRESS);
const yfiPrice = useTokenPrice(YFI_ADDRESS);
const [approveRedeemStatus, set_approveRedeemStatus] = useState(defaultTxStatus);
const [redeemStatus, set_redeemStatus] = useState(defaultTxStatus);
const [ethRequired, set_ethRequired] = useState(toNormalizedBN(0));


useAsyncTrigger(async (): Promise<void> => {
const result = await getRequiredEth(redeemAmount.raw);
set_ethRequired(toNormalizedBN(result));
Expand Down Expand Up @@ -101,7 +102,7 @@ export function RedeemTab(): ReactElement {
<AmountInput
label={'You have dYFI'}
amount={dYFIBalance}
legend={formatCounterValue(dYFIBalance.normalized, optionPrice ?? 0)}
legend={formatCounterValue(dYFIBalance.normalized, dYFIPrice)}
disabled
/>
<AmountInput
Expand All @@ -111,7 +112,7 @@ export function RedeemTab(): ReactElement {
onAmountChange={onChangeInput}
onLegendClick={(): void => set_redeemAmount(dYFIBalance)}
onMaxClick={(): void => set_redeemAmount(dYFIBalance)}
legend={formatCounterValue(redeemAmount.normalized, dYFIPrice)}
legend={formatCounterValue(redeemAmount.normalized, yfiPrice)}
error={redeemAmountError}
/>
<AmountInput
Expand Down
5 changes: 2 additions & 3 deletions apps/veyfi/components/RewardsTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {Button} from '@yearn-finance/web-lib/components/Button';
import {useWeb3} from '@yearn-finance/web-lib/contexts/useWeb3';
import {toAddress, truncateHex} from '@yearn-finance/web-lib/utils/address';
import {toBigInt, toNormalizedBN} from '@yearn-finance/web-lib/utils/format.bigNumber';
import {formatAmount} from '@yearn-finance/web-lib/utils/format.number';
import {formatCounterValue} from '@yearn-finance/web-lib/utils/format.value';
import {defaultTxStatus} from '@yearn-finance/web-lib/utils/web3/transaction';
import {AmountInput} from '@common/components/AmountInput';
Expand All @@ -21,7 +20,7 @@ export function RewardsTab(): ReactElement {
const [selectedGauge, set_selectedGauge] = useState<TDropdownOption>();
const {provider, isActive} = useWeb3();
const {gaugeAddresses, gaugesMap, positionsMap, refresh: refreshGauges} = useGauge();
const {price: optionPrice} = useOption();
const {dYFIPrice} = useOption();
const {vaults} = useYearn();
const refreshData = useCallback((): unknown => Promise.all([refreshGauges()]), [refreshGauges]);
const [claimStatus, set_claimStatus] = useState(defaultTxStatus);
Expand Down Expand Up @@ -72,7 +71,7 @@ export function RewardsTab(): ReactElement {
<AmountInput
label={'Unclaimed rewards (dYFI)'}
amount={selectedGaugeRewards}
legend={formatCounterValue(formatAmount(selectedGaugeRewards.normalized, 2, 2), optionPrice ?? 0)}
legend={formatCounterValue(selectedGaugeRewards.normalized, dYFIPrice)}
disabled
/>
<Button
Expand Down
35 changes: 17 additions & 18 deletions apps/veyfi/contexts/useOption.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {VEYFI_CHAIN_ID, VEYFI_DYFI_ADDRESS,VEYFI_OPTIONS_ADDRESS} from '@veYFI/u
import {erc20ABI, readContract} from '@wagmi/core';
import {useWeb3} from '@yearn-finance/web-lib/contexts/useWeb3';
import {allowanceKey} from '@yearn-finance/web-lib/utils/address';
import {BIG_ZERO, ETH_TOKEN_ADDRESS, YFI_ADDRESS} from '@yearn-finance/web-lib/utils/constants';
import {toBigInt, toNormalizedBN, toNormalizedValue} from '@yearn-finance/web-lib/utils/format.bigNumber';
import {BIG_ZERO, YFI_ADDRESS} from '@yearn-finance/web-lib/utils/constants';
import {toNormalizedBN} from '@yearn-finance/web-lib/utils/format.bigNumber';
import {useAsyncTrigger} from '@common/hooks/useAsyncEffect';
import {useTokenPrice} from '@common/hooks/useTokenPrice';

Expand All @@ -17,15 +17,15 @@ import type {TNormalizedBN} from '@common/types/types';

export type TOptionContext = {
getRequiredEth: (amount: bigint) => Promise<bigint>,
price: number | undefined,
dYFIPrice: number,
position: TNormalizedBN,
allowances: TDict<bigint>,
refresh: () => void,
}

const defaultProps: TOptionContext = {
getRequiredEth: async (): Promise<bigint> => BIG_ZERO,
price: undefined,
dYFIPrice: 0,
position: toNormalizedBN(0),
allowances: {},
refresh: (): void => undefined
Expand All @@ -34,11 +34,10 @@ const defaultProps: TOptionContext = {
const OptionContext = createContext<TOptionContext>(defaultProps);
export const OptionContextApp = memo(function OptionContextApp({children}: {children: ReactElement}): ReactElement {
const {address: userAddress, isActive} = useWeb3();
const [price, set_price] = useState<number | undefined>(undefined);
const [dYFIPrice, set_dYFIPrice] = useState<number>(0);
const [position, set_position] = useState<TNormalizedBN>(toNormalizedBN(0));
const [allowances, set_allowances] = useState<TDict<bigint>>({});
const yfiPrice = useTokenPrice(YFI_ADDRESS);
const ethPrice = useTokenPrice(ETH_TOKEN_ADDRESS);

const getRequiredEth = useCallback(async (amount: bigint): Promise<bigint> => {
return readContract({
Expand All @@ -51,16 +50,16 @@ export const OptionContextApp = memo(function OptionContextApp({children}: {chil
}, []);

const refreshPrice = useAsyncTrigger(async (): Promise<void> => {
console.log(ethPrice, yfiPrice);
if(!ethPrice || !yfiPrice) {
return undefined;
}
const oneOption = toBigInt(1e18);
const requiredEthPerOption = await getRequiredEth(oneOption);
const requiredEthValuePerOption = toNormalizedValue(requiredEthPerOption, 18) * ethPrice;
const pricePerOption = yfiPrice - requiredEthValuePerOption;
set_price(pricePerOption);
}, [ethPrice, yfiPrice, getRequiredEth]);
const discountRaw = await readContract({
address: VEYFI_OPTIONS_ADDRESS,
abi: VEYFI_OPTIONS_ABI,
functionName: 'discount',
chainId: VEYFI_CHAIN_ID
});
const discount = toNormalizedBN(discountRaw);
const dYFIPrice = yfiPrice * Number(discount?.normalized || 0);
set_dYFIPrice(dYFIPrice);
}, [yfiPrice]);

const refreshPositions = useAsyncTrigger(async (): Promise<void> => {
if (!isActive || !userAddress) {
Expand Down Expand Up @@ -103,11 +102,11 @@ export const OptionContextApp = memo(function OptionContextApp({children}: {chil

const contextValue = useDeepCompareMemo((): TOptionContext => ({
getRequiredEth,
price,
dYFIPrice,
position,
allowances: allowances ?? {},
refresh
}), [allowances, getRequiredEth, position, price, refresh]);
}), [allowances, getRequiredEth, position, dYFIPrice, refresh]);

return (
<OptionContext.Provider value={contextValue}>
Expand Down

0 comments on commit 6ac94a9

Please sign in to comment.