diff --git a/src/actions/borrow.tsx b/src/actions/borrow.tsx index 3e12405..195b04f 100644 --- a/src/actions/borrow.tsx +++ b/src/actions/borrow.tsx @@ -125,7 +125,7 @@ export const borrow = async ( signers ) : undefined; - + let amountLamports: number = 0; let fromLamports: number = 0; if (amountType === BorrowAmountType.LiquidityBorrowAmount) { @@ -152,7 +152,6 @@ export const borrow = async ( fromLamports = amountLamports; } - const fromAccount = ensureSplAccount( instructions, finalCleanupInstructions, @@ -202,7 +201,7 @@ export const borrow = async ( fromAccount, wallet.publicKey, fromLamports, - false, + false ); signers.push(transferAuthority); diff --git a/src/components/BorrowInput/index.tsx b/src/components/BorrowInput/index.tsx index 0f411fa..1d16952 100644 --- a/src/components/BorrowInput/index.tsx +++ b/src/components/BorrowInput/index.tsx @@ -1,5 +1,10 @@ import React, { useCallback, useEffect, useMemo, useState } from "react"; -import { useUserBalance, useUserObligationByReserve } from "../../hooks"; +import { + useSliderInput, + useUserBalance, + useUserDeposits, + useUserObligationByReserve, +} from "../../hooks"; import { BorrowAmountType, LendingReserve, @@ -16,8 +21,8 @@ import { ActionConfirmation } from "./../ActionConfirmation"; import { BackButton } from "./../BackButton"; import { ConnectButton } from "../ConnectButton"; import CollateralInput from "../CollateralInput"; -import { ArrowDownOutlined } from "@ant-design/icons"; import { useMidPriceInUSD } from "../../contexts/market"; +import { RiskSlider } from "../RiskSlider"; export const BorrowInput = (props: { className?: string; @@ -25,7 +30,6 @@ export const BorrowInput = (props: { }) => { const connection = useConnection(); const { wallet } = useWallet(); - const [value, setValue] = useState(""); const [collateralValue, setCollateralValue] = useState(""); const [lastTyped, setLastTyped] = useState("collateral"); const [pendingTx, setPendingTx] = useState(false); @@ -43,7 +47,6 @@ export const BorrowInput = (props: { return cache.get(id) as ParsedAccount; }, [collateralReserveKey]); - const borrowPrice = useMidPriceInUSD( borrowReserve.info.liquidityMint.toBase58() ).price; @@ -51,6 +54,30 @@ export const BorrowInput = (props: { collateralReserve?.info.liquidityMint.toBase58() )?.price; + const include = useMemo( + () => new Set([collateralReserve?.pubkey.toBase58()]), + [collateralReserve] + ); + + const exclude = useMemo(() => new Set([]), []); + + const { userDeposits: accountBalance } = useUserDeposits(exclude, include); + const tokenBalance = accountBalance[0]?.info.amount || 0; + + const convert = useCallback( + (val: string | number) => { + const minAmount = Math.min(tokenBalance, Infinity); + if (typeof val === "string") { + return (parseFloat(val) / minAmount) * 100; + } else { + return (val * minAmount) / 100; + } + }, + [tokenBalance] + ); + + const { value, setValue, pct } = useSliderInput(convert); + useEffect(() => { if (collateralReserve && lastTyped === "collateral") { const ltv = borrowReserve.info.config.loanToValueRatio / 100; @@ -71,6 +98,7 @@ export const BorrowInput = (props: { borrowPrice, borrowReserve, collateralValue, + setValue, ]); useEffect(() => { @@ -95,15 +123,13 @@ export const BorrowInput = (props: { value, ]); - const { accounts: fromAccounts } = useUserBalance( - collateralReserve?.info.collateralMint - ); - const { userObligationsByReserve } = useUserObligationByReserve( borrowReserve?.pubkey, collateralReserve?.pubkey ); - + const { accounts: fromAccounts } = useUserBalance( + collateralReserve?.info.collateralMint + ); const onBorrow = useCallback(() => { if (!collateralReserve) { return; @@ -198,7 +224,7 @@ export const BorrowInput = (props: { useFirstReserve={true} /> - +
{ + return ( + + ); +}; diff --git a/src/components/RiskSlider/style.less b/src/components/RiskSlider/style.less new file mode 100644 index 0000000..9cf35e5 --- /dev/null +++ b/src/components/RiskSlider/style.less @@ -0,0 +1,9 @@ +@import "~antd/dist/antd.dark.less"; + +.risk-slider { + color: @text-color-secondary; + + .ant-slider-rail { + background-image: linear-gradient(to right, darkgreen, darkred) !important; + } +} diff --git a/src/constants/labels.ts b/src/constants/labels.ts index 498f5ea..c6816f0 100644 --- a/src/constants/labels.ts +++ b/src/constants/labels.ts @@ -91,4 +91,6 @@ export const LABELS = { NO_DEPOSIT_MESSAGE: "You need to deposit coin of this type into oyster before trading with it on margin.", NO_COLL_TYPE_MESSAGE: "Choose Collateral CCY", + SAFER: "Safer", + RISKIER: "Riskier", }; diff --git a/src/constants/marks.ts b/src/constants/marks.ts index 13afbfb..9fde78a 100644 --- a/src/constants/marks.ts +++ b/src/constants/marks.ts @@ -1,3 +1,5 @@ +import { LABELS } from "./labels"; + export const marks = { 0: "0%", 25: "25%", @@ -5,3 +7,17 @@ export const marks = { 75: "75%", 100: "100%", }; +export const riskMarks = { + 0: { + style: { + color: "darkgreen", + }, + label: LABELS.SAFER, + }, + 100: { + style: { + color: "darkred", + }, + label: LABELS.RISKIER, + }, +};