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

Test/new interest lens #127

Open
wants to merge 8 commits into
base: test/derby-market
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
16 changes: 14 additions & 2 deletions components/BorrowForm/BorrowForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ import {
import { useGetBorrowAmount } from '../../hooks/useCoupon';
import { borrow } from '../../hooks/useHerc20';
import { queryKeys } from '../../helpers/queryHelper';
import { usePositions } from '../../hooks/useCollection';
import { useMarket, usePositions } from '../../hooks/useCollection';
import { fetchAllowance } from 'helpers/utils';
import { collections } from '../../constants/NFTCollections';

const {
format: f,
Expand Down Expand Up @@ -116,6 +117,17 @@ const BorrowForm = (props: BorrowProps) => {
HERC20ContractAddress,
unit
);

const collection = collections.find(
(collection) => collection.HERC20ContractAddress === HERC20ContractAddress
);

const [marketData, isLoadingMarketData] = useMarket(
currentUser,
collection ? [collection] : [],
htokenHelperContractAddress
);

const newAdditionalDebt = valueUnderlying * (1 + borrowFee);
const newTotalDebt = newAdditionalDebt ? borrowedValue + newAdditionalDebt : borrowedValue;
/* end initial all financial value here */
Expand Down Expand Up @@ -387,7 +399,7 @@ const BorrowForm = (props: BorrowProps) => {
</a>
</span>
}
value={fp(5, 2)}
value={fp(marketData[0].borrowRate, 2)}
/>
</div>
<div className={cs(stylesBorrow.balance, styles.col)}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC } from 'react';
import { FC, useContext } from 'react';
import { BorrowPositionCardProps } from '../types';
import * as styles from './BorrowPositionCard.css';
import HexaBoxContainer from '../../HexaBoxContainer/HexaBoxContainer';
Expand All @@ -13,11 +13,15 @@ import { useGetMaxBorrowableAmount, useGetNFTPrice } from 'hooks/useHtokenHelper
import { getContractsByHTokenAddr } from 'helpers/generalHelper';
import { useGetCollateralFactor } from 'hooks/useHivemind';
import { useGetBorrowAmount } from 'hooks/useCoupon';
import { collections } from 'constants/NFTCollections';
import { useMarket } from 'hooks/useCollection';
import { UserContext } from 'contexts/userContext';

const { formatShortName: fsn, formatPercent: fp } = formatNumber;

export const BorrowPositionCard: FC<BorrowPositionCardProps> = ({ position, onSelect }) => {
const selectedNFTId = useLoanFlowStore((state) => state.NFTId);
const { currentUser } = useContext(UserContext);

const {
nftContractAddress,
Expand Down Expand Up @@ -53,6 +57,16 @@ export const BorrowPositionCard: FC<BorrowPositionCardProps> = ({ position, onSe
const nftValue = nftPrice.price;
const MAX_LTV = maxBorrow / nftValue;

const collection = collections.find(
(collection) => collection.HERC20ContractAddress === position.HERC20ContractAddr
);

const [marketData, isLoadingMarketData] = useMarket(
currentUser,
collection ? [collection] : [],
htokenHelperContractAddress
);

return (
<div
className={c(styles.positionCard, {
Expand Down Expand Up @@ -105,7 +119,7 @@ export const BorrowPositionCard: FC<BorrowPositionCardProps> = ({ position, onSe
</div>
}
/>
<InfoBlock title="IR" value={fp(5, 2)} />
<InfoBlock title="IR" value={fp(marketData[0].borrowRate, 2)} />
</div>
<div className={styles.divider} />
<BorrowPositionCardSlider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ export const LendPositionCard: FC<LendPositionCardProps> = ({ position, onSelect
<div className={styles.arrowIcon} />
</div>
<div className={styles.positionValues}>
<InfoBlock title="IR" value={<span className={styles.irValue}>{fp(position.rate)}</span>} />
<InfoBlock
title="IR"
value={<span className={styles.irValue}>{fp(position.rate, 2)}</span>}
/>
<InfoBlock
title="Your Deposit"
value={fsn(userTotalDeposits < 0 ? 0 : userTotalDeposits, formatDecimals)}
Expand Down
88 changes: 44 additions & 44 deletions constants/NFTCollections.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ import { envName } from './service';
function getCollections() {
if (envName == 'dev') {
return [
{
name: 'Y00TS',
icon: '/nfts/y00ts.png',
erc20Name: 'WETH',
erc20Icon: '/erc20/EthIcon.png',
formatDecimals: 2,
unit: 'ether',
ERC721ContractAddress: '0x670fd103b1a08628e9557cD66B87DeD841115190',
ERC20ContractAddress: '0x7ceb23fd6bc0add59e62ac25578270cff1b9f619',
HERC20ContractAddress: '0x13e3b746e89fd9693222fd377521d874e0e1ae5b'
},
{
name: 'LAND',
icon: '/nfts/sandbox.png',
erc20Name: 'WETH',
erc20Icon: '/erc20/EthIcon.png',
unit: 'ether',
ERC721ContractAddress: '0x9d305a42A3975Ee4c1C57555BeD5919889DCE63F',
ERC20ContractAddress: '0x7ceb23fd6bc0add59e62ac25578270cff1b9f619',
HERC20ContractAddress: '0x9e826d1427c15ff6f41521e9811fd526f7717a2a'
},
// {
// name: 'Y00TS',
// icon: '/nfts/y00ts.png',
// erc20Name: 'WETH',
// erc20Icon: '/erc20/EthIcon.png',
// formatDecimals: 2,
// unit: 'ether',
// ERC721ContractAddress: '0x670fd103b1a08628e9557cD66B87DeD841115190',
// ERC20ContractAddress: '0x7ceb23fd6bc0add59e62ac25578270cff1b9f619',
// HERC20ContractAddress: '0x13e3b746e89fd9693222fd377521d874e0e1ae5b'
// },
// {
// name: 'LAND',
// icon: '/nfts/sandbox.png',
// erc20Name: 'WETH',
// erc20Icon: '/erc20/EthIcon.png',
// unit: 'ether',
// ERC721ContractAddress: '0x9d305a42A3975Ee4c1C57555BeD5919889DCE63F',
// ERC20ContractAddress: '0x7ceb23fd6bc0add59e62ac25578270cff1b9f619',
// HERC20ContractAddress: '0x9e826d1427c15ff6f41521e9811fd526f7717a2a'
// },
{
name: 'Chicken-Derby',
icon: '/nfts/derby.png',
Expand Down Expand Up @@ -59,27 +59,27 @@ function getCollections() {
} else if (envName == 'prod') {
// change this prod market when we go to prod
return [
{
name: 'Y00TS',
icon: '/nfts/y00ts.png',
erc20Name: 'WETH',
erc20Icon: '/erc20/EthIcon.png',
formatDecimals: 2,
unit: 'ether',
ERC721ContractAddress: '0x670fd103b1a08628e9557cD66B87DeD841115190',
ERC20ContractAddress: '0x7ceb23fd6bc0add59e62ac25578270cff1b9f619',
HERC20ContractAddress: '0x13e3b746e89fd9693222fd377521d874e0e1ae5b'
},
{
name: 'LAND',
icon: '/nfts/sandbox.png',
erc20Name: 'WETH',
erc20Icon: '/erc20/EthIcon.png',
unit: 'ether',
ERC721ContractAddress: '0x9d305a42A3975Ee4c1C57555BeD5919889DCE63F',
ERC20ContractAddress: '0x7ceb23fd6bc0add59e62ac25578270cff1b9f619',
HERC20ContractAddress: '0x9e826d1427c15ff6f41521e9811fd526f7717a2a'
},
// {
// name: 'Y00TS',
// icon: '/nfts/y00ts.png',
// erc20Name: 'WETH',
// erc20Icon: '/erc20/EthIcon.png',
// formatDecimals: 2,
// unit: 'ether',
// ERC721ContractAddress: '0x670fd103b1a08628e9557cD66B87DeD841115190',
// ERC20ContractAddress: '0x7ceb23fd6bc0add59e62ac25578270cff1b9f619',
// HERC20ContractAddress: '0x13e3b746e89fd9693222fd377521d874e0e1ae5b'
// },
// {
// name: 'LAND',
// icon: '/nfts/sandbox.png',
// erc20Name: 'WETH',
// erc20Icon: '/erc20/EthIcon.png',
// unit: 'ether',
// ERC721ContractAddress: '0x9d305a42A3975Ee4c1C57555BeD5919889DCE63F',
// ERC20ContractAddress: '0x7ceb23fd6bc0add59e62ac25578270cff1b9f619',
// HERC20ContractAddress: '0x9e826d1427c15ff6f41521e9811fd526f7717a2a'
// },
{
name: 'Chicken-Derby',
icon: '/nfts/derby.png',
Expand Down Expand Up @@ -120,15 +120,15 @@ function getCollections() {
function getHelperContract() {
if (envName == 'dev') {
const contract: helperContract = {
htokenHelperContractAddress: '0xBdB3F78722DC37C864bFB5787F2A7D62bcb29Fbb',
htokenHelperContractAddress: '0x80478086f9fb2462e7a35eec6e9a683b25153b37',
hivemindContractAddress: '0x9a1EDb903B058298dd0b06f52876d9D45358B7cB',
oracleContractAddress: '0xe236c45c8C3B0065F96cA62Dc5fd4759974161BC',
marketContractAddress: '0xf7AC7E1fF22cc0e71964A2730d4e2835146F5aBE'
};
return contract;
} else if (envName == 'prod') {
const contract: helperContract = {
htokenHelperContractAddress: '0xBdB3F78722DC37C864bFB5787F2A7D62bcb29Fbb',
htokenHelperContractAddress: '0x80478086f9fb2462e7a35eec6e9a683b25153b37',
hivemindContractAddress: '0x9a1EDb903B058298dd0b06f52876d9D45358B7cB',
oracleContractAddress: '0xe236c45c8C3B0065F96cA62Dc5fd4759974161BC',
marketContractAddress: '0xf7AC7E1fF22cc0e71964A2730d4e2835146F5aBE'
Expand Down
19 changes: 10 additions & 9 deletions helpers/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,6 @@ export const formatAddress = (address: any) => {
export const RoundHalfDown = (val: number, decimals: number = 2): number => {
return Math.floor(val * 10 ** decimals) / 10 ** decimals;
};
/**
* @description calculates interest rate for lend market
* @params baserate which is default rate provided by contract | total market supplied | total market available
* @returns interest rate for lend market
*/
export const interestRateLend = (baseRate: number, supplied: string, available: string) => {
const rate = baseRate * ((parseFloat(supplied) - parseFloat(available)) / parseFloat(supplied));
return rate > 0 ? rate : 0;
};

/**
* @description filter selected position from positions array
Expand All @@ -61,6 +52,16 @@ export const fetchAllowance = (positions: MarketTablePosition[], NFTId: string)
}
};

// TODO: no calculation needed, can now be fetched from lens/helper contract [2]
/**
* @description calculates interest rate for lend market
* @params baserate which is default rate provided by contract | total market supplied | total market available
* @returns interest rate for lend market
*/
export const interestRateLend = (baseRate: number, supplied: string, available: string) => {
const rate = baseRate * ((parseFloat(supplied) - parseFloat(available)) / parseFloat(supplied));
return rate > 0 ? rate : 0;
};
/**
* @description return interest rate for specific collection
* @params positions
Expand Down
21 changes: 15 additions & 6 deletions hooks/useCollection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ const defaultMarketData: MarketTableRow = {
icon: '',
erc20Icon: '',
formatDecimals: 0,
rate: 0,
lendRate: 0,
borrowRate: 0,
available: 0,
supplied: 0
};
Expand All @@ -59,7 +60,8 @@ const defaultLiquidationData: LiquidateTableRow = {

const defaultMarket: marketData = {
HERC20ContractAddress: '',
interestRate: 0,
supplyInterestRate: '0',
borrowInterestRate: '0',
supplied: '0',
available: '0'
};
Expand Down Expand Up @@ -87,7 +89,8 @@ export function useMarket(
icon: collection.icon,
erc20Icon: collection.erc20Icon,
formatDecimals: collection.formatDecimals,
rate: marketData.interestRate,
lendRate: parseFloat(marketData.supplyInterestRate),
borrowRate: parseFloat(marketData.borrowInterestRate),
available: parseFloat(marketData.available),
supplied: parseFloat(marketData.supplied)
};
Expand All @@ -101,7 +104,8 @@ export function useMarket(
icon: collection.icon,
erc20Icon: collection.erc20Icon,
formatDecimals: collection.formatDecimals,
rate: 0,
borrowRate: 0,
lendRate: 0,
available: 0,
supplied: 0
};
Expand Down Expand Up @@ -357,7 +361,8 @@ export function useLend(
console.error(e);
const result: marketData = {
HERC20ContractAddress: collection.HERC20ContractAddress,
interestRate: 0,
borrowInterestRate: '0',
supplyInterestRate: '0',
supplied: '0',
available: '0'
};
Expand Down Expand Up @@ -389,7 +394,11 @@ export function useLend(
formatDecimals: formatDecimals ?? 3,
available: parseFloat(marketData.available),
supplied: parseFloat(marketData.supplied),
rate: interestRateLend(marketData.interestRate, marketData.supplied, marketData.available)
rate: interestRateLend(
Number(marketData.supplyInterestRate),
marketData.supplied,
marketData.available
)
};
return result;
});
Expand Down
12 changes: 9 additions & 3 deletions hooks/useDashBoard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ export function useLendUserPositions(
const walletPublicKey: string = user?.address || '';
const defaultMarket: marketData = {
HERC20ContractAddress: '',
interestRate: 0,
borrowInterestRate: '0',
supplyInterestRate: '0',
supplied: '0',
available: '0'
};
Expand Down Expand Up @@ -203,7 +204,8 @@ export function useLendUserPositions(
console.error(e);
const result: marketData = {
HERC20ContractAddress: HERC20ContractAddress,
interestRate: 0,
borrowInterestRate: '0',
supplyInterestRate: '0',
supplied: '0',
available: '0'
};
Expand All @@ -229,7 +231,11 @@ export function useLendUserPositions(
const data = lendData[0];
position.supplied = parseFloat(data.supplied);
position.available = parseFloat(data.available);
position.rate = interestRateLend(data.interestRate, data.supplied, data.available);
position.rate = interestRateLend(
parseFloat(data.supplyInterestRate),
data.supplied,
data.available
);
}
return position;
});
Expand Down
13 changes: 8 additions & 5 deletions hooks/useHtokenHelper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,8 @@ export function useGetUserCoupons(

export interface marketData {
HERC20ContractAddress: string;
interestRate: number;
borrowInterestRate: string;
supplyInterestRate: string;
supplied: string;
available: string;
}
Expand All @@ -558,12 +559,14 @@ export async function getMarketData(
// @ts-ignore
const response = await Moralis.EvmApi.utils.runContractFunction(options);
const result: any = response.result;
const interestRate = result[0] as number;
const supplied = result[1] as string;
const available = result[2] as string;
const supplyInterestRate = result[2] as string;
const borrowInterestRate = result[3] as string;
const supplied = result[4] as string;
const available = result[5] as string;
const resultData: marketData = {
HERC20ContractAddress: HERC20ContractAddress,
interestRate: interestRate / 1000.0,
borrowInterestRate: fromWei(borrowInterestRate, unit),
supplyInterestRate: fromWei(supplyInterestRate, unit),
supplied: fromWei(supplied, unit),
available: fromWei(available, unit)
};
Expand Down
10 changes: 5 additions & 5 deletions pages/borrow/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,13 @@ const Markets: NextPage = () => {
</div>
);
},
dataIndex: 'rate',
dataIndex: 'borrowRate',
hidden: windowWidth < TABLET_BP,
sorter: (a: MarketTableRow, b: MarketTableRow) => a.rate - b.rate,
render: (rate: number) => {
sorter: (a: MarketTableRow, b: MarketTableRow) => a.borrowRate - b.borrowRate,
render: (borrowRate: number, row: MarketTableRow) => {
return (
<div className={c(style.rateCell, style.borrowRate)}>
{fp(rate / (showWeeklyRates ? 52 : 1), showWeeklyRates ? 3 : 2)}
{fp(borrowRate / (showWeeklyRates ? 52 : 1), showWeeklyRates ? 3 : 2)}
</div>
);
}
Expand Down Expand Up @@ -313,7 +313,7 @@ const Markets: NextPage = () => {

<HoneyTableRow>
<div className={c(style.rateCell, style.borrowRate)}>
{fp(row.rate / (showWeeklyRates ? 52 : 1), showWeeklyRates ? 3 : 2)}
{fp(row.borrowRate / (showWeeklyRates ? 52 : 1), showWeeklyRates ? 3 : 2)}
</div>
<div className={style.availableCell}>{fs(row.supplied)}</div>
<div className={style.availableCell}>{fs(row.available)}</div>
Expand Down
Loading