From d02ea83f1df9c56481dd71ae16e557a60be27d13 Mon Sep 17 00:00:00 2001 From: bartosz-lipinski <264380+bartosz-lipinski@users.noreply.github.com> Date: Tue, 1 Dec 2020 00:14:35 -0600 Subject: [PATCH] feat: add slider support --- src/actions/deposit.tsx | 13 +------ src/components/DepositInput/index.tsx | 48 ++++++++++++++++++++--- src/components/WithdrawInput/index.tsx | 53 +++++++++++++++++--------- src/constants/index.tsx | 1 + src/constants/marks.ts | 7 ++++ src/contexts/lending.tsx | 11 +++--- src/contexts/market.tsx | 4 +- src/hooks/index.ts | 1 + src/hooks/useCollateralBalance.ts | 1 + src/hooks/useSliderInput.ts | 37 ++++++++++++++++++ src/models/lending/reserve.ts | 8 ++-- src/views/borrow/item.tsx | 5 +-- src/views/dashboard/index.tsx | 6 +-- src/views/home/index.tsx | 10 +++-- 14 files changed, 147 insertions(+), 58 deletions(-) create mode 100644 src/constants/marks.ts create mode 100644 src/hooks/useSliderInput.ts diff --git a/src/actions/deposit.tsx b/src/actions/deposit.tsx index e2cfe1b..76e4290 100644 --- a/src/actions/deposit.tsx +++ b/src/actions/deposit.tsx @@ -11,20 +11,18 @@ import { initReserveInstruction, LendingReserve, } from "./../models/lending"; -import { AccountLayout, MintInfo, Token } from "@solana/spl-token"; +import { AccountLayout, Token } from "@solana/spl-token"; import { LENDING_PROGRAM_ID, TOKEN_PROGRAM_ID } from "../constants/ids"; import { createUninitializedAccount, ensureSplAccount, findOrCreateAccountByMint, } from "./account"; -import { cache, MintParser, ParsedAccount } from "../contexts/accounts"; import { TokenAccount } from "../models"; -import { toLamports } from "../utils/utils"; export const deposit = async ( from: TokenAccount, - amount: number, + amountLamports: number, reserve: LendingReserve, reserveAddress: PublicKey, connection: Connection, @@ -55,13 +53,6 @@ export const deposit = async ( LENDING_PROGRAM_ID ); - const mint = (await cache.query( - connection, - reserve.liquidityMint, - MintParser - )) as ParsedAccount; - const amountLamports = toLamports(amount, mint?.info); - const fromAccount = ensureSplAccount( instructions, cleanupInstructions, diff --git a/src/components/DepositInput/index.tsx b/src/components/DepositInput/index.tsx index 548c7c2..f0706d0 100644 --- a/src/components/DepositInput/index.tsx +++ b/src/components/DepositInput/index.tsx @@ -1,8 +1,13 @@ import React, { useCallback, useState } from "react"; -import { useTokenName, useUserBalance } from "../../hooks"; +import { + InputType, + useSliderInput, + useTokenName, + useUserBalance, +} from "../../hooks"; import { LendingReserve } from "../../models/lending"; import { TokenIcon } from "../TokenIcon"; -import { Button, Card, Spin } from "antd"; +import { Button, Card, Slider, Spin } from "antd"; import { NumericInput } from "../Input/numeric"; import { useConnection } from "../../contexts/connection"; import { useWallet } from "../../contexts/wallet"; @@ -11,6 +16,7 @@ import { PublicKey } from "@solana/web3.js"; import "./style.less"; import { LoadingOutlined } from "@ant-design/icons"; import { ActionConfirmation } from "./../ActionConfirmation"; +import { marks } from "../../constants"; const antIcon = ; @@ -21,7 +27,6 @@ export const DepositInput = (props: { }) => { const connection = useConnection(); const { wallet } = useWallet(); - const [value, setValue] = useState(""); const [pendingTx, setPendingTx] = useState(false); const [showConfirmation, setShowConfirmation] = useState(false); @@ -29,9 +34,24 @@ export const DepositInput = (props: { const address = props.address; const name = useTokenName(reserve?.liquidityMint); - const { accounts: fromAccounts } = useUserBalance(reserve?.liquidityMint); + const { accounts: fromAccounts, balance, balanceLamports } = useUserBalance( + reserve?.liquidityMint + ); // const collateralBalance = useUserBalance(reserve?.collateralMint); + const convert = useCallback( + (val: string | number) => { + if (typeof val === "string") { + return (parseFloat(val) / balance) * 100; + } else { + return ((val * balance) / 100).toFixed(2); + } + }, + [balance] + ); + + const { value, setValue, mark, setMark, type } = useSliderInput(convert); + const onDeposit = useCallback(() => { setPendingTx(true); @@ -39,7 +59,9 @@ export const DepositInput = (props: { try { await deposit( fromAccounts[0], - parseFloat(value), + type === InputType.Slider + ? (mark * balanceLamports) / 100 + : Math.ceil(balanceLamports * (parseFloat(value) / balance)), reserve, address, connection, @@ -54,7 +76,19 @@ export const DepositInput = (props: { setPendingTx(false); } })(); - }, [connection, wallet, value, reserve, fromAccounts, address]); + }, [ + connection, + setValue, + balanceLamports, + balance, + wallet, + value, + mark, + type, + reserve, + fromAccounts, + address, + ]); const bodyStyle: React.CSSProperties = { display: "flex", @@ -98,6 +132,8 @@ export const DepositInput = (props: {
{name}
+ +