diff --git a/components/BorrowForm/BorrowForm.tsx b/components/BorrowForm/BorrowForm.tsx index 7ec3b52..7174bfd 100644 --- a/components/BorrowForm/BorrowForm.tsx +++ b/components/BorrowForm/BorrowForm.tsx @@ -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, @@ -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 */ @@ -387,7 +399,7 @@ const BorrowForm = (props: BorrowProps) => { } - value={fp(5, 2)} + value={fp(marketData[0].borrowRate, 2)} />
diff --git a/components/HoneyCardsGrid/BorrowPositionCard/BorrowPositionCard.tsx b/components/HoneyCardsGrid/BorrowPositionCard/BorrowPositionCard.tsx index f731289..0e6d01b 100644 --- a/components/HoneyCardsGrid/BorrowPositionCard/BorrowPositionCard.tsx +++ b/components/HoneyCardsGrid/BorrowPositionCard/BorrowPositionCard.tsx @@ -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'; @@ -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 = ({ position, onSelect }) => { const selectedNFTId = useLoanFlowStore((state) => state.NFTId); + const { currentUser } = useContext(UserContext); const { nftContractAddress, @@ -53,6 +57,16 @@ export const BorrowPositionCard: FC = ({ 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 (
= ({ position, onSe
} /> - +
= ({ position, onSelect
- {fp(position.rate)}} /> + {fp(position.rate, 2)}} + /> { 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 @@ -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 diff --git a/hooks/useCollection.tsx b/hooks/useCollection.tsx index 87cec00..369150b 100644 --- a/hooks/useCollection.tsx +++ b/hooks/useCollection.tsx @@ -40,7 +40,8 @@ const defaultMarketData: MarketTableRow = { icon: '', erc20Icon: '', formatDecimals: 0, - rate: 0, + lendRate: 0, + borrowRate: 0, available: 0, supplied: 0 }; @@ -59,7 +60,8 @@ const defaultLiquidationData: LiquidateTableRow = { const defaultMarket: marketData = { HERC20ContractAddress: '', - interestRate: 0, + supplyInterestRate: '0', + borrowInterestRate: '0', supplied: '0', available: '0' }; @@ -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) }; @@ -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 }; @@ -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' }; @@ -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; }); diff --git a/hooks/useDashBoard.tsx b/hooks/useDashBoard.tsx index f74eb73..ff95168 100644 --- a/hooks/useDashBoard.tsx +++ b/hooks/useDashBoard.tsx @@ -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' }; @@ -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' }; @@ -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; }); diff --git a/hooks/useHtokenHelper.tsx b/hooks/useHtokenHelper.tsx index ff1f818..958e6aa 100644 --- a/hooks/useHtokenHelper.tsx +++ b/hooks/useHtokenHelper.tsx @@ -536,7 +536,8 @@ export function useGetUserCoupons( export interface marketData { HERC20ContractAddress: string; - interestRate: number; + borrowInterestRate: string; + supplyInterestRate: string; supplied: string; available: string; } @@ -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) }; diff --git a/pages/borrow/index.tsx b/pages/borrow/index.tsx index 672cc99..ed6105b 100644 --- a/pages/borrow/index.tsx +++ b/pages/borrow/index.tsx @@ -207,13 +207,13 @@ const Markets: NextPage = () => {
); }, - 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 (
- {fp(rate / (showWeeklyRates ? 52 : 1), showWeeklyRates ? 3 : 2)} + {fp(borrowRate / (showWeeklyRates ? 52 : 1), showWeeklyRates ? 3 : 2)}
); } @@ -313,7 +313,7 @@ const Markets: NextPage = () => {
- {fp(row.rate / (showWeeklyRates ? 52 : 1), showWeeklyRates ? 3 : 2)} + {fp(row.borrowRate / (showWeeklyRates ? 52 : 1), showWeeklyRates ? 3 : 2)}
{fs(row.supplied)}
{fs(row.available)}
diff --git a/public/dev/abi/htokenHelper.json b/public/dev/abi/htokenHelper.json index b413e64..8976fb7 100644 --- a/public/dev/abi/htokenHelper.json +++ b/public/dev/abi/htokenHelper.json @@ -1,9 +1,4 @@ [ - { - "inputs": [], - "name": "WrongParams", - "type": "error" - }, { "inputs": [], "name": "DENOMINATOR", @@ -354,6 +349,21 @@ ], "name": "getFrontendMarketData", "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "", + "type": "uint256" + }, { "internalType": "uint256", "name": "", @@ -568,30 +578,6 @@ "stateMutability": "view", "type": "function" }, - { - "inputs": [ - { - "internalType": "uint256", - "name": "_id", - "type": "uint256" - }, - { - "internalType": "address", - "name": "_hTokenAddress", - "type": "address" - } - ], - "name": "uri", - "outputs": [ - { - "internalType": "string", - "name": "", - "type": "string" - } - ], - "stateMutability": "view", - "type": "function" - }, { "inputs": [], "name": "version", diff --git a/public/prod/abi/htokenHelper.json b/public/prod/abi/htokenHelper.json index b413e64..8976fb7 100644 --- a/public/prod/abi/htokenHelper.json +++ b/public/prod/abi/htokenHelper.json @@ -1,9 +1,4 @@ [ - { - "inputs": [], - "name": "WrongParams", - "type": "error" - }, { "inputs": [], "name": "DENOMINATOR", @@ -354,6 +349,21 @@ ], "name": "getFrontendMarketData", "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "", + "type": "uint256" + }, { "internalType": "uint256", "name": "", @@ -568,30 +578,6 @@ "stateMutability": "view", "type": "function" }, - { - "inputs": [ - { - "internalType": "uint256", - "name": "_id", - "type": "uint256" - }, - { - "internalType": "address", - "name": "_hTokenAddress", - "type": "address" - } - ], - "name": "uri", - "outputs": [ - { - "internalType": "string", - "name": "", - "type": "string" - } - ], - "stateMutability": "view", - "type": "function" - }, { "inputs": [], "name": "version", diff --git a/types/markets.ts b/types/markets.ts index 1543b8f..aae2b1e 100644 --- a/types/markets.ts +++ b/types/markets.ts @@ -5,7 +5,8 @@ export type MarketTableRow = { name: string; icon: string; erc20Icon: string; - rate: number; + borrowRate: number; + lendRate: number; available: number; supplied: number; formatDecimals: number;