From 9730fe8dae80276582457629abce8a5a776e7719 Mon Sep 17 00:00:00 2001 From: Justin Starry Date: Thu, 26 Nov 2020 17:11:40 +0800 Subject: [PATCH] fix: typos and apy calculation --- .../ReserveUtilizationChart/index.tsx | 2 +- src/components/SideReserveOverview/index.tsx | 14 +++++----- src/constants/labels.ts | 7 +++-- src/hooks/useCollateralBalance.ts | 2 +- src/models/lending/borrow.ts | 27 ++++++++++--------- src/models/lending/deposit.ts | 6 ++--- src/models/lending/reserve.ts | 8 +++--- src/views/borrow/index.tsx | 2 +- src/views/borrow/item.tsx | 4 +-- src/views/dashboard/index.tsx | 2 +- src/views/home/index.tsx | 4 +-- src/views/home/item.tsx | 10 +++---- 12 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/components/ReserveUtilizationChart/index.tsx b/src/components/ReserveUtilizationChart/index.tsx index e37032e..129653e 100644 --- a/src/components/ReserveUtilizationChart/index.tsx +++ b/src/components/ReserveUtilizationChart/index.tsx @@ -23,7 +23,7 @@ export const ReserveUtilizationChart = (props: { reserve: LendingReserve }) => { const liquidityMint = useMint(props.reserve.liquidityMint); const avilableLiquidity = fromLamports( - props.reserve.availableLiqudity.toNumber(), + props.reserve.availableLiquidity.toNumber(), liquidityMint ); diff --git a/src/components/SideReserveOverview/index.tsx b/src/components/SideReserveOverview/index.tsx index b43765e..0abc7d4 100644 --- a/src/components/SideReserveOverview/index.tsx +++ b/src/components/SideReserveOverview/index.tsx @@ -1,6 +1,6 @@ import React from "react"; import { useTokenName } from "./../../hooks"; -import { calculateBorrowAPR, calculateDepositAPY, calculateUtilizationRatio, LendingReserve } from "../../models/lending"; +import { calculateBorrowAPY, calculateDepositAPY, calculateUtilizationRatio, LendingReserve } from "../../models/lending"; import { TokenIcon } from "../../components/TokenIcon"; import { formatNumber, formatPct, fromLamports, wadToLamports } from "../../utils/utils"; import { Card, Typography } from "antd"; @@ -25,13 +25,13 @@ export const SideReserveOverview = (props: { const name = useTokenName(reserve?.liquidityMint); const liquidityMint = useMint(reserve.liquidityMint); - const availableLiqudity = fromLamports( - reserve.availableLiqudity.toNumber(), + const availableLiquidity = fromLamports( + reserve.availableLiquidity.toNumber(), liquidityMint ); const depositApy = calculateDepositAPY(reserve); - const borrowApr = calculateBorrowAPR(reserve); + const borrowApr = calculateBorrowAPY(reserve); const utilizationRate = calculateUtilizationRatio(reserve); const liquidiationThreshold = reserve.config.optimalUtilizationRate / 100; @@ -44,7 +44,7 @@ export const SideReserveOverview = (props: { <>
- {LABELS.TABLE_TITLE_DEPOSIT_APR}: + {LABELS.TABLE_TITLE_DEPOSIT_APY}:
{formatPct.format(depositApy)}
@@ -80,7 +80,7 @@ export const SideReserveOverview = (props: { <>
- {LABELS.TABLE_TITLE_BORROW_APR}: + {LABELS.TABLE_TITLE_BORROW_APY}:
{formatPct.format(borrowApr)}
@@ -122,7 +122,7 @@ export const SideReserveOverview = (props: { Available liquidity:
- {formatNumber.format(availableLiqudity)} {name} + {formatNumber.format(availableLiquidity)} {name}
diff --git a/src/constants/labels.ts b/src/constants/labels.ts index 098d192..91e6ab9 100644 --- a/src/constants/labels.ts +++ b/src/constants/labels.ts @@ -25,13 +25,12 @@ export const LABELS = { TABLE_TITLE_LOAN_BALANCE: "Your loan balance", TABLE_TITLE_DEPOSIT_BALANCE: "Your deposit balance", TABLE_TITLE_APY: "APY", - TABLE_TITLE_APR: "APR", - TABLE_TITLE_BORROW_APR: "Borrow APR", - TABLE_TITLE_DEPOSIT_APR: "Deposit APY", + TABLE_TITLE_BORROW_APY: "Borrow APY", + TABLE_TITLE_DEPOSIT_APY: "Deposit APY", TABLE_TITLE_TOTAL_BORROWED: "Total Borrowed", TABLE_TITLE_MARKET_SIZE: "Market Size", TABLE_TITLE_ACTION: "Action", - TABLE_TITLE_MAX_BORROW: "Available fro you", + TABLE_TITLE_MAX_BORROW: "Available for you", DASHBOARD_TITLE_LOANS: "Loans", DASHBOARD_TITLE_DEPOSITS: "Deposits", WITHDRAW_ACTION: "Withdraw", diff --git a/src/hooks/useCollateralBalance.ts b/src/hooks/useCollateralBalance.ts index 74b8a3e..8994593 100644 --- a/src/hooks/useCollateralBalance.ts +++ b/src/hooks/useCollateralBalance.ts @@ -15,7 +15,7 @@ export function useCollateralBalance( ); const collateralRatioLamports = - (reserve?.availableLiqudity.toNumber() || 0) * + (reserve?.availableLiquidity.toNumber() || 0) * (balanceLamports / (reserve?.collateralMintSupply.toNumber() || 1)); return { diff --git a/src/models/lending/borrow.ts b/src/models/lending/borrow.ts index a4ded96..744ce75 100644 --- a/src/models/lending/borrow.ts +++ b/src/models/lending/borrow.ts @@ -111,27 +111,28 @@ export const borrowInstruction = ( // deposit APY utilization currentUtilizationRate * borrowAPY -export const calculateBorrowAPR = (reserve: LendingReserve) => { +export const calculateBorrowAPY = (reserve: LendingReserve) => { const totalBorrows = reserve.borrowedLiquidityWad.div(WAD).toNumber(); const currentUtilization = - totalBorrows / (reserve.availableLiqudity.toNumber() + totalBorrows); - const optimalUtilization = reserve.config.optimalUtilizationRate; - let borrowAPR; - if (currentUtilization < optimalUtilization) { - const normalized_factor = currentUtilization / optimalUtilization; + totalBorrows / (reserve.availableLiquidity.toNumber() + totalBorrows); + const optimalUtilization = reserve.config.optimalUtilizationRate / 100; + + let borrowAPY; + if (optimalUtilization == 1.0 || currentUtilization < optimalUtilization) { + const normalizedFactor = currentUtilization / optimalUtilization; const optimalBorrowRate = reserve.config.optimalBorrowRate / 100; const minBorrowRate = reserve.config.minBorrowRate / 100; - borrowAPR = - normalized_factor * (optimalBorrowRate - minBorrowRate) + minBorrowRate; + borrowAPY = + normalizedFactor * (optimalBorrowRate - minBorrowRate) + minBorrowRate; } else { - const normalized_factor = - (currentUtilization - optimalUtilization) / (100 - optimalUtilization); + const normalizedFactor = + (currentUtilization - optimalUtilization) / (1 - optimalUtilization); const optimalBorrowRate = reserve.config.optimalBorrowRate / 100; const maxBorrowRate = reserve.config.maxBorrowRate / 100; - borrowAPR = - normalized_factor * (maxBorrowRate - optimalBorrowRate) + + borrowAPY = + normalizedFactor * (maxBorrowRate - optimalBorrowRate) + optimalBorrowRate; } - return borrowAPR; + return borrowAPY; }; diff --git a/src/models/lending/deposit.ts b/src/models/lending/deposit.ts index 4e74bb1..c2312e7 100644 --- a/src/models/lending/deposit.ts +++ b/src/models/lending/deposit.ts @@ -8,7 +8,7 @@ import * as BufferLayout from "buffer-layout"; import { WAD } from "../../constants"; import { LENDING_PROGRAM_ID, TOKEN_PROGRAM_ID } from "../../constants/ids"; import * as Layout from "./../../utils/layout"; -import { calculateBorrowAPR } from "./borrow"; +import { calculateBorrowAPY } from "./borrow"; import { LendingInstruction } from "./lending"; import { LendingReserve } from "./reserve"; @@ -66,8 +66,8 @@ export const depositInstruction = ( export const calculateDepositAPY = (reserve: LendingReserve) => { const totalBorrows = reserve.borrowedLiquidityWad.div(WAD).toNumber(); const currentUtilization = - totalBorrows / (reserve.availableLiqudity.toNumber() + totalBorrows); + totalBorrows / (reserve.availableLiquidity.toNumber() + totalBorrows); - const borrowAPY = calculateBorrowAPR(reserve); + const borrowAPY = calculateBorrowAPY(reserve); return currentUtilization * borrowAPY; }; diff --git a/src/models/lending/reserve.ts b/src/models/lending/reserve.ts index 878732e..4e4a484 100644 --- a/src/models/lending/reserve.ts +++ b/src/models/lending/reserve.ts @@ -48,7 +48,7 @@ export const LendingReserveLayout: typeof BufferLayout.Structure = BufferLayout. Layout.uint128("cumulativeBorrowRateWad"), Layout.uint128("borrowedLiquidityWad"), - Layout.uint64("availableLiqudity"), + Layout.uint64("availableLiquidity"), Layout.uint64("collateralMintSupply"), ] ); @@ -86,7 +86,7 @@ export interface LendingReserve { cumulativeBorrowRateWad: BN; borrowedLiquidityWad: BN; - availableLiqudity: BN; + availableLiquidity: BN; collateralMintSupply: BN; // Layout.uint128("cumulative_borrow_rate"), @@ -171,6 +171,6 @@ export const initReserveInstruction = ( }; export const calculateUtilizationRatio = (reserve: LendingReserve) => { - return reserve.availableLiqudity.toNumber() / - (reserve.availableLiqudity.toNumber() + wadToLamports(reserve.borrowedLiquidityWad).toNumber()); + return reserve.availableLiquidity.toNumber() / + (reserve.availableLiquidity.toNumber() + wadToLamports(reserve.borrowedLiquidityWad).toNumber()); } \ No newline at end of file diff --git a/src/views/borrow/index.tsx b/src/views/borrow/index.tsx index a78846a..ae4b5d1 100644 --- a/src/views/borrow/index.tsx +++ b/src/views/borrow/index.tsx @@ -11,7 +11,7 @@ export const BorrowView = () => {
{LABELS.TABLE_TITLE_ASSET}
{LABELS.TABLE_TITLE_MAX_BORROW}
-
{LABELS.TABLE_TITLE_APR}
+
{LABELS.TABLE_TITLE_APY}
{LABELS.TABLE_TITLE_ACTION}
{reserveAccounts.map((account) => ( diff --git a/src/views/borrow/item.tsx b/src/views/borrow/item.tsx index c9b83d7..fd9c393 100644 --- a/src/views/borrow/item.tsx +++ b/src/views/borrow/item.tsx @@ -1,6 +1,6 @@ import React from "react"; import { useCollateralBalance, useTokenName } from "../../hooks"; -import { calculateBorrowAPR, LendingReserve } from "../../models/lending"; +import { calculateBorrowAPY, LendingReserve } from "../../models/lending"; import { TokenIcon } from "../../components/TokenIcon"; import { formatNumber, formatPct } from "../../utils/utils"; import { Button, Card } from "antd"; @@ -17,7 +17,7 @@ export const BorrowItem = (props: { // TODO: calculate avilable amount... based on total owned collateral across all the reserves const { balance: collateralBalance } = useCollateralBalance(props.reserve); - const apr = calculateBorrowAPR(props.reserve); + const apr = calculateBorrowAPY(props.reserve); return ( diff --git a/src/views/dashboard/index.tsx b/src/views/dashboard/index.tsx index 9337857..f27ccac 100644 --- a/src/views/dashboard/index.tsx +++ b/src/views/dashboard/index.tsx @@ -31,7 +31,7 @@ export const DashboardView = () => {
{LABELS.TABLE_TITLE_ASSET}
{LABELS.TABLE_TITLE_LOAN_BALANCE}
-
{LABELS.TABLE_TITLE_APR}
+
{LABELS.TABLE_TITLE_APY}
{LABELS.TABLE_TITLE_ACTION}
)} diff --git a/src/views/home/index.tsx b/src/views/home/index.tsx index 40e2229..fe62b97 100644 --- a/src/views/home/index.tsx +++ b/src/views/home/index.tsx @@ -15,8 +15,8 @@ export const HomeView = () => {
{LABELS.TABLE_TITLE_ASSET}
{LABELS.TABLE_TITLE_MARKET_SIZE}
{LABELS.TABLE_TITLE_TOTAL_BORROWED}
-
{LABELS.TABLE_TITLE_DEPOSIT_APR}
-
{LABELS.TABLE_TITLE_BORROW_APR}
+
{LABELS.TABLE_TITLE_DEPOSIT_APY}
+
{LABELS.TABLE_TITLE_BORROW_APY}
{reserveAccounts.map((account) => ( diff --git a/src/views/home/item.tsx b/src/views/home/item.tsx index c2d8ecc..3767341 100644 --- a/src/views/home/item.tsx +++ b/src/views/home/item.tsx @@ -1,7 +1,7 @@ import React, { useMemo } from "react"; import { useTokenName } from "../../hooks"; import { - calculateBorrowAPR, + calculateBorrowAPY, calculateDepositAPY, LendingReserve, } from "../../models/lending"; @@ -25,8 +25,8 @@ export const LendingReserveItem = (props: { const liquidityMint = useMint(props.reserve.liquidityMint); - const availableLiqudity = fromLamports( - props.reserve.availableLiqudity.toNumber(), + const availableLiquidity = fromLamports( + props.reserve.availableLiquidity.toNumber(), liquidityMint ); @@ -39,7 +39,7 @@ export const LendingReserveItem = (props: { [props.reserve, liquidityMint] ); - const borrowAPY = useMemo(() => calculateBorrowAPR(props.reserve), [ + const borrowAPY = useMemo(() => calculateBorrowAPY(props.reserve), [ props.reserve, ]); @@ -47,7 +47,7 @@ export const LendingReserveItem = (props: { props.reserve, ]); - const marketSize = availableLiqudity + totalBorrows; + const marketSize = availableLiquidity + totalBorrows; return (