Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APY/APR work #1206

Open
wants to merge 1 commit into
base: feat/VR
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/components/selectors/VariableRate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import styled from 'styled-components';
import { UserContext } from '../../contexts/UserContext';
import { SettingsContext } from '../../contexts/SettingsContext';
import YieldMark from '../logos/YieldMark';
import SkeletonWrap from '../wraps/SkeletonWrap';

const StyledBox = styled(Box)`
-webkit-transition: transform 0.3s ease-in-out;
Expand Down Expand Up @@ -68,7 +69,7 @@ const ShineyBox = styled(Box)`
}
`;

const VariableRate = () => {
const VariableRate = ({ rate }: { rate?: string }) => {
const {
settingsState: { darkMode },
} = useContext(SettingsContext);
Expand Down Expand Up @@ -139,9 +140,13 @@ const VariableRate = () => {

<Box fill align="end" width="small" className="thisHereBox">
<Box style={{ marginTop: 'auto', marginBottom: 'auto' }}>
<Text size="1.5em" color={darkMode ? 'white' : 'black'}>
{2.7} <Text size="small">% {'APR'}</Text>
</Text>
{rate ? (
<Text size="1.5em" color={darkMode ? 'white' : 'black'}>
{parseFloat(rate).toFixed(2)} <Text size="small">% {'APR'}</Text>
</Text>
) : (
<SkeletonWrap width={100} />
)}
</Box>
</Box>
</Box>
Expand Down
10 changes: 8 additions & 2 deletions src/components/views/Borrow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import InfoBite from '../InfoBite';
import NextButton from '../buttons/NextButton';
import TransactButton from '../buttons/TransactButton';
import { useApr } from '../../hooks/useApr';
import { useAprVR } from '../../hooks/useAprVR';
import PositionAvatar from '../PositionAvatar';
import VaultDropSelector from '../selectors/VaultDropSelector';
import { useInputValidation } from '../../hooks/useInputValidation';
Expand Down Expand Up @@ -89,6 +90,8 @@ const Borrow = () => {
const borrow = useBorrow();

const { apr } = useApr(borrowInput, ActionType.BORROW, selectedSeries);
const { apr: aprVR } = useAprVR(borrowInput, ActionType.BORROW);

const { data: assetPair } = useAssetPair(selectedBase?.id, selectedIlk?.id);
const { data: basesVR } = useBasesVR();
const { data: vaultsVR } = useVaultsVR();
Expand Down Expand Up @@ -168,6 +171,7 @@ const Borrow = () => {
const handleMaxAction = (actionCode: ActionCodes) => {
actionCode === ActionCodes.ADD_COLLATERAL && setCollatInput(maxCollateral!);
actionCode === ActionCodes.BORROW && selectedSeries && setBorrowInput(selectedSeries.sharesReserves_!);
actionCode === ActionCodes.BORROW && selectedVR && setBorrowInput(maxDebtVR_!);
logAnalyticsEvent(GA_Event.max_clicked, {
view: GA_View.BORROW,
action_code: actionCode,
Expand Down Expand Up @@ -340,7 +344,9 @@ const Borrow = () => {
placeholder="Enter amount"
value={borrowInput}
onChange={(event: any) =>
setBorrowInput(cleanValue(event.target.value, selectedSeries?.decimals))
setBorrowInput(
cleanValue(event.target.value, selectedSeries?.decimals || selectedBase?.decimals)
)
}
autoFocus={!mobile}
/>
Expand Down Expand Up @@ -370,7 +376,7 @@ const Borrow = () => {
</SectionWrap>
{basesVR?.length && basesVR.includes(selectedBase?.id!) ? (
<SectionWrap title="OR choose a variable rate">
<VariableRate />
<VariableRate rate={aprVR} />
</SectionWrap>
) : null}
</Box>
Expand Down
5 changes: 4 additions & 1 deletion src/components/views/Lend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { useLendHelpersFR } from '../../hooks/viewHelperHooks/useLendHelpers/use
import { useLendHelpersVR } from '../../hooks/viewHelperHooks/useLendHelpers/useLendHelpersVR';
import useVYTokens from '../../hooks/entities/useVYTokens';
import useBasesVR from '../../hooks/views/useBasesVR';
import { useAprVR } from '../../hooks/useAprVR';

const Lend = () => {
const mobile: boolean = useContext<any>(ResponsiveContext) === 'small';
Expand All @@ -65,6 +66,8 @@ const Lend = () => {
const [stepPosition, setStepPosition] = useState<number>(0);
const [stepDisabled, setStepDisabled] = useState<boolean>(true);

const { apr: aprVR } = useAprVR(lendInput, ActionType.LEND);

/* HOOK FNS */
const {
maxLend_: maxLendFR_,
Expand Down Expand Up @@ -214,7 +217,7 @@ const Lend = () => {
</SectionWrap>
{basesVR?.length && basesVR.includes(selectedBase?.id!) ? (
<SectionWrap title="Or lend indefinitely at a variable rate">
<VariableRate />
<VariableRate rate={aprVR} />
</SectionWrap>
) : null}
</Box>
Expand Down
2 changes: 1 addition & 1 deletion src/components/views/LendPosition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ const LendPosition = () => {
/>
<InfoBite
label="APY"
value={`${apy}%`}
value={apy ? `${parseFloat(apy).toFixed(2)}%` : ''}
icon={
<Box height="1em" width="1em">
<Logo image={<FiPercent />} />
Expand Down
46 changes: 46 additions & 0 deletions src/contracts/VRInterestRateOracle.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,32 @@ interface VRInterestRateOracleInterface extends ethers.utils.Interface {
): Result;

events: {
"AccumulationUpdated(bytes6,bytes6,uint256,uint256,uint256)": EventFragment;
"InterestRateParamSet(bytes6,bytes6,uint256,uint256,uint256,uint256,address)": EventFragment;
"InterestRateParamUpdated(bytes6,bytes6,uint256,uint256,uint256,uint256,address)": EventFragment;
"RoleAdminChanged(bytes4,bytes4)": EventFragment;
"RoleGranted(bytes4,address,address)": EventFragment;
"RoleRevoked(bytes4,address,address)": EventFragment;
};

getEvent(nameOrSignatureOrTopic: "AccumulationUpdated"): EventFragment;
getEvent(nameOrSignatureOrTopic: "InterestRateParamSet"): EventFragment;
getEvent(nameOrSignatureOrTopic: "InterestRateParamUpdated"): EventFragment;
getEvent(nameOrSignatureOrTopic: "RoleAdminChanged"): EventFragment;
getEvent(nameOrSignatureOrTopic: "RoleGranted"): EventFragment;
getEvent(nameOrSignatureOrTopic: "RoleRevoked"): EventFragment;
}

export type AccumulationUpdatedEvent = TypedEvent<
[string, string, BigNumber, BigNumber, BigNumber] & {
baseId: string;
kind: string;
accumulated: BigNumber;
lastUpdateTimestamp: BigNumber;
utilizationRate: BigNumber;
}
>;

export type InterestRateParamSetEvent = TypedEvent<
[string, string, BigNumber, BigNumber, BigNumber, BigNumber, string] & {
baseId: string;
Expand Down Expand Up @@ -637,6 +649,40 @@ export class VRInterestRateOracle extends BaseContract {
};

filters: {
"AccumulationUpdated(bytes6,bytes6,uint256,uint256,uint256)"(
baseId?: BytesLike | null,
kind?: BytesLike | null,
accumulated?: null,
lastUpdateTimestamp?: null,
utilizationRate?: null
): TypedEventFilter<
[string, string, BigNumber, BigNumber, BigNumber],
{
baseId: string;
kind: string;
accumulated: BigNumber;
lastUpdateTimestamp: BigNumber;
utilizationRate: BigNumber;
}
>;

AccumulationUpdated(
baseId?: BytesLike | null,
kind?: BytesLike | null,
accumulated?: null,
lastUpdateTimestamp?: null,
utilizationRate?: null
): TypedEventFilter<
[string, string, BigNumber, BigNumber, BigNumber],
{
baseId: string;
kind: string;
accumulated: BigNumber;
lastUpdateTimestamp: BigNumber;
utilizationRate: BigNumber;
}
>;

"InterestRateParamSet(bytes6,bytes6,uint256,uint256,uint256,uint256,address)"(
baseId?: BytesLike | null,
kind?: BytesLike | null,
Expand Down
12 changes: 12 additions & 0 deletions src/contracts/abis/VRInterestRateOracle.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{ "indexed": true, "internalType": "bytes6", "name": "baseId", "type": "bytes6" },
{ "indexed": true, "internalType": "bytes6", "name": "kind", "type": "bytes6" },
{ "indexed": false, "internalType": "uint256", "name": "accumulated", "type": "uint256" },
{ "indexed": false, "internalType": "uint256", "name": "lastUpdateTimestamp", "type": "uint256" },
{ "indexed": false, "internalType": "uint256", "name": "utilizationRate", "type": "uint256" }
],
"name": "AccumulationUpdated",
"type": "event"
},
{
"anonymous": false,
"inputs": [
Expand Down
37 changes: 37 additions & 0 deletions src/contracts/factories/VRInterestRateOracle__factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,43 @@ const _abi = [
stateMutability: "nonpayable",
type: "constructor",
},
{
anonymous: false,
inputs: [
{
indexed: true,
internalType: "bytes6",
name: "baseId",
type: "bytes6",
},
{
indexed: true,
internalType: "bytes6",
name: "kind",
type: "bytes6",
},
{
indexed: false,
internalType: "uint256",
name: "accumulated",
type: "uint256",
},
{
indexed: false,
internalType: "uint256",
name: "lastUpdateTimestamp",
type: "uint256",
},
{
indexed: false,
internalType: "uint256",
name: "utilizationRate",
type: "uint256",
},
],
name: "AccumulationUpdated",
type: "event",
},
{
anonymous: false,
inputs: [
Expand Down
11 changes: 5 additions & 6 deletions src/hooks/entities/useVYTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { BigNumber, ethers } from 'ethers';
import { formatUnits } from 'ethers/lib/utils.js';
import { ChainContext } from '../../contexts/ChainContext';
import { MulticallContext } from '../../contexts/MutlicallContext';
import { useApr } from '../useApr';
import { ActionType } from '../../types';

export interface IVYToken extends ISignable {
id: string; // vyToken address
Expand All @@ -32,7 +30,6 @@ const useVYTokens = () => {
const { address: account } = useAccountPlus();
const { useForkedEnv, provider: forkProvider, forkUrl } = useFork();
const provider = useDefaultProvider();
const { apr } = useApr(undefined, ActionType.LEND, null);

const {
chainState: { assetRootMap },
Expand All @@ -42,7 +39,6 @@ const useVYTokens = () => {
const multicall = useForkedEnv ? forkMulticall : _multicall;

const get = useCallback(async () => {
console.log('getting vyToken data');
return await Array.from(assetRootMap.values())
.map((a) => [a.VYTokenProxyAddress, a.VYTokenAddress]) // get asset's vyTokenProxy addr
.reduce(async (vyTokens, [proxyAddress, address]) => {
Expand All @@ -65,11 +61,14 @@ const useVYTokens = () => {

let vyTokenBaseVal = balance;
try {
vyTokenBaseVal = await proxy.previewRedeem(balance);
vyTokenBaseVal = await proxy.callStatic.previewRedeem(balance);
} catch (e) {
console.log('Error getting vyTokenBaseVal', e);
}

const accumulatedInterestInBase = vyTokenBaseVal.sub(balance);
const accumulatedInterestInBase_ = formatUnits(accumulatedInterestInBase, decimals);

const addr = address.toLowerCase();
const data: IVYToken = {
id: addr,
Expand All @@ -87,7 +86,7 @@ const useVYTokens = () => {
vyTokenBaseVal,
vyTokenBaseVal_: formatUnits(vyTokenBaseVal, decimals),
proxyAddress: proxyAddress.toLowerCase(),
accumulatedInterestInBase_: '0.0',
accumulatedInterestInBase_,
};

return (await vyTokens).set(addr, data);
Expand Down
66 changes: 3 additions & 63 deletions src/hooks/useApr.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import { ethers, BigNumber } from 'ethers';
import { ethers } from 'ethers';
import { useContext, useEffect, useState } from 'react';
import { sellBase, buyBase, calculateAPR, bytesToBytes32 } from '@yield-protocol/ui-math';

import { sellBase, buyBase, calculateAPR } from '@yield-protocol/ui-math';
import { ETH_BASED_ASSETS } from '../config/assets';
import { UserContext } from '../contexts/UserContext';
import { ActionType, ISeries } from '../types';
import { cleanValue } from '../utils/appUtils';
import useTimeTillMaturity from './useTimeTillMaturity';
import { useProvider } from 'wagmi';
import * as contractTypes from '../contracts';

import { VRInterestRateOracle__factory } from '../contracts';
import useContracts from './useContracts';
import { ContractNames } from '../config/contracts';
import { RATE, ZERO_BN, CHI } from '../utils/constants';
import { useConvertValue } from './useConvertValue';
import useFork from './useFork';
import { formatUnits } from 'ethers/lib/utils.js';

/* APR hook calculatess APR, min and max aprs for selected series and BORROW or LEND type */
export const useApr = (input: string | undefined, actionType: ActionType, series: ISeries | null) => {
Expand All @@ -26,9 +15,6 @@ export const useApr = (input: string | undefined, actionType: ActionType, series

/* HOOKS */
const { getTimeTillMaturity, isMature } = useTimeTillMaturity();
const provider = useProvider();
const contracts = useContracts();
const { useForkedEnv, forkStartBlock } = useFork();

const _selectedSeries = series || selectedSeries;
/* Make sure there won't be an underflow */
Expand Down Expand Up @@ -73,53 +59,7 @@ export const useApr = (input: string | undefined, actionType: ActionType, series
// figure out what to do with negative apr on borrow for tv series
const _apr = calculateAPR(baseAmount, preview, _selectedSeries.maturity);
_apr ? setApr(cleanValue(_apr, 2)) : setApr(_selectedSeries.apr);
} else if (selectedBase) {
/* logic for VR */
const cleanedInput = cleanValue(_input || _fallbackInput, selectedBase.decimals);
const baseAmount = ethers.utils.parseUnits(cleanedInput, selectedBase.decimals);

const now = Date.now() + 20000;
// trying to call interest rate oracle
// const interestRateOracleAddr = '0xa60eb553b65284e3a221b958c9115d8e558289bf';

const getAPY = async () => {
try {
const VRCauldron = contracts?.get(ContractNames.VR_CAULDRON) as contractTypes.VRCauldron;
const interestRateOracleAddr = await VRCauldron.rateOracles(selectedBase.id);
const interestRateOracle = VRInterestRateOracle__factory.connect(interestRateOracleAddr, provider);
const joinAddress = selectedBase.joinAddressVR;
// console.log('INTEREST RATE ORACLE', interestRateOracleAddr, interestRateOracle);

let rate: any = ethers.constants.Zero; // TODO - fix this type

if (actionType === 'LEND') {
rate = await interestRateOracle.peek(bytesToBytes32(selectedBase.id, 6), RATE, '0');
}

if (actionType === 'BORROW') {
rate = await interestRateOracle.peek(
bytesToBytes32(selectedBase.id, 6),
bytesToBytes32('0x434849000000', 6), // TODO - make this a constant
'0'
);
}

// console.log('rate in useAPR', rate);

return rate;
} catch (e) {
console.log(`Error getting APY for ${selectedBase.symbol}:`, e);
return ethers.constants.Zero;
}
};

getAPY().then((res) => {
const rate = res.accumulated
? formatUnits(BigNumber.from(res.accumulated.toHexString()), selectedBase.decimals)
: ethers.constants.Zero;

setApr(rate.toString());
});
console.log('baseAmount FR', baseAmount, baseAmount.toString(), _fallbackInput, _input);
}
}, [_selectedSeries, _input, actionType, _fallbackInput, getTimeTillMaturity]);

Expand Down
Loading