Added swap input style to repay page

This commit is contained in:
juan 2021-01-24 15:19:44 -05:00
parent d69762d1b8
commit 1b14c2641b
4 changed files with 69 additions and 37 deletions

View File

@ -1,6 +1,5 @@
import React, {useCallback, useEffect, useMemo, useState} from "react"; import React, {useCallback, useEffect, useMemo, useState} from "react";
import { import {
useTokenName,
useUserBalance, useUserBalance,
useUserObligationByReserve, useUserObligationByReserve,
} from "../../hooks"; } from "../../hooks";
@ -52,7 +51,7 @@ export const BorrowInput = (props: {
const collateralPrice = useMidPriceInUSD(collateralReserve?.info.liquidityMint.toBase58())?.price; const collateralPrice = useMidPriceInUSD(collateralReserve?.info.liquidityMint.toBase58())?.price;
useEffect(() => { useEffect(() => {
if (collateralReserve && lastTyped == "collateral") { if (collateralReserve && lastTyped === "collateral") {
const ltv = borrowReserve.info.config.loanToValueRatio / 100; const ltv = borrowReserve.info.config.loanToValueRatio / 100;
if (collateralValue) { if (collateralValue) {
@ -64,10 +63,10 @@ export const BorrowInput = (props: {
setValue("") setValue("")
} }
} }
}, [collateralReserve, collateralPrice, borrowPrice, borrowReserve, collateralValue]) }, [lastTyped, collateralReserve, collateralPrice, borrowPrice, borrowReserve, collateralValue])
useEffect(() => { useEffect(() => {
if (collateralReserve && lastTyped == "borrow") { if (collateralReserve && lastTyped === "borrow") {
const ltv = borrowReserve.info.config.loanToValueRatio / 100; const ltv = borrowReserve.info.config.loanToValueRatio / 100;
if (value) { if (value) {
@ -81,7 +80,6 @@ export const BorrowInput = (props: {
} }
}, [lastTyped, collateralReserve, collateralPrice, borrowPrice, borrowReserve, value]) }, [lastTyped, collateralReserve, collateralPrice, borrowPrice, borrowReserve, value])
const name = useTokenName(borrowReserve?.info.liquidityMint);
const { accounts: fromAccounts } = useUserBalance( const { accounts: fromAccounts } = useUserBalance(
collateralReserve?.info.collateralMint collateralReserve?.info.collateralMint
); );
@ -164,7 +162,7 @@ export const BorrowInput = (props: {
> >
<div style={{ display: 'flex', flexDirection: 'row', justifyContent: 'space-evenly', alignItems: 'center' }}> <div style={{ display: 'flex', flexDirection: 'row', justifyContent: 'space-evenly', alignItems: 'center' }}>
<CollateralInput <CollateralInput
title='Collateral' title='Collateral (estimated)'
reserve={borrowReserve.info} reserve={borrowReserve.info}
amount={parseFloat(collateralValue) || 0} amount={parseFloat(collateralValue) || 0}
onInputChange={(val: number | null) => { onInputChange={(val: number | null) => {

View File

@ -1,38 +1,38 @@
import React, { useCallback, useState } from "react"; import React, {useCallback, useEffect, useState} from "react";
import { import {
EnrichedLendingObligation, EnrichedLendingObligation,
InputType, InputType,
useAccountByMint, useAccountByMint,
useSliderInput, useSliderInput,
useTokenName,
useUserBalance, useUserBalance,
} from "../../hooks"; } from "../../hooks";
import { LendingReserve } from "../../models"; import { LendingReserve } from "../../models";
import { TokenIcon } from "../TokenIcon";
import { Card, Slider } from "antd"; import { Card, Slider } from "antd";
import { ParsedAccount, useMint } from "../../contexts/accounts"; import { ParsedAccount, useMint } from "../../contexts/accounts";
import { NumericInput } from "../Input/numeric";
import { useConnection } from "../../contexts/connection"; import { useConnection } from "../../contexts/connection";
import { useWallet } from "../../contexts/wallet"; import { useWallet } from "../../contexts/wallet";
import { repay } from "../../actions"; import { repay } from "../../actions";
import { CollateralSelector } from "./../CollateralSelector";
import "./style.less"; import "./style.less";
import { LABELS, marks } from "../../constants"; import { LABELS, marks } from "../../constants";
import { ActionConfirmation } from "./../ActionConfirmation"; import { ActionConfirmation } from "./../ActionConfirmation";
import { fromLamports, wadToLamports } from "../../utils/utils"; import { fromLamports, wadToLamports } from "../../utils/utils";
import { notify } from "../../utils/notifications"; import { notify } from "../../utils/notifications";
import { ConnectButton } from "../ConnectButton"; import { ConnectButton } from "../ConnectButton";
import CollateralInput from "../CollateralInput";
import {useMidPriceInUSD} from "../../contexts/market";
export const RepayInput = (props: { export const RepayInput = (props: {
className?: string; className?: string;
borrowReserve: ParsedAccount<LendingReserve>; borrowReserve: ParsedAccount<LendingReserve>;
collateralReserve?: ParsedAccount<LendingReserve>; collateralReserve: ParsedAccount<LendingReserve>;
obligation: EnrichedLendingObligation; obligation: EnrichedLendingObligation;
}) => { }) => {
const connection = useConnection(); const connection = useConnection();
const { wallet } = useWallet(); const { wallet } = useWallet();
const [lastTyped, setLastTyped] = useState("repay");
const [pendingTx, setPendingTx] = useState(false); const [pendingTx, setPendingTx] = useState(false);
const [showConfirmation, setShowConfirmation] = useState(false); const [showConfirmation, setShowConfirmation] = useState(false);
const [collateralValue, setCollateralValue] = useState("");
const repayReserve = props.borrowReserve; const repayReserve = props.borrowReserve;
const obligation = props.obligation; const obligation = props.obligation;
@ -45,7 +45,6 @@ export const RepayInput = (props: {
const borrowAmount = fromLamports(borrowAmountLamports, liquidityMint); const borrowAmount = fromLamports(borrowAmountLamports, liquidityMint);
const collateralReserve = props.collateralReserve; const collateralReserve = props.collateralReserve;
const name = useTokenName(repayReserve?.info.liquidityMint);
const { accounts: fromAccounts } = useUserBalance( const { accounts: fromAccounts } = useUserBalance(
repayReserve.info.liquidityMint repayReserve.info.liquidityMint
); );
@ -54,10 +53,11 @@ export const RepayInput = (props: {
const convert = useCallback( const convert = useCallback(
(val: string | number) => { (val: string | number) => {
setLastTyped("repay")
if (typeof val === "string") { if (typeof val === "string") {
return (parseFloat(val) / borrowAmount) * 100; return (parseFloat(val) / borrowAmount) * 100;
} else { } else {
return ((val * borrowAmount) / 100).toFixed(2); return ((val * borrowAmount) / 100);
} }
}, },
[borrowAmount] [borrowAmount]
@ -85,7 +85,6 @@ export const RepayInput = (props: {
: Math.ceil( : Math.ceil(
borrowAmountLamports * (parseFloat(value) / borrowAmount) borrowAmountLamports * (parseFloat(value) / borrowAmount)
); );
await repay( await repay(
fromAccounts[0], fromAccounts[0],
toRepayLamports, toRepayLamports,
@ -98,6 +97,7 @@ export const RepayInput = (props: {
); );
setValue(""); setValue("");
setCollateralValue("")
setShowConfirmation(true); setShowConfirmation(true);
} catch (error) { } catch (error) {
notify({ notify({
@ -125,6 +125,36 @@ export const RepayInput = (props: {
setValue, setValue,
]); ]);
const collateralPrice = useMidPriceInUSD(collateralReserve?.info.liquidityMint.toBase58())?.price;
useEffect(() => {
if (collateralReserve && lastTyped === "repay") {
const collateralInQuote = obligation.info.collateralInQuote;
const collateral = collateralInQuote * collateralPrice
if (value) {
const borrowRatio = (parseFloat(value) / borrowAmount) * 100
const collateralAmount = (borrowRatio * collateral) / 100
setCollateralValue(collateralAmount.toString())
} else {
setCollateralValue("")
}
}
}, [lastTyped, collateralReserve, obligation.info.collateralInQuote, collateralPrice, borrowAmount, value])
useEffect(() => {
if (collateralReserve && lastTyped === "collateral") {
const collateralInQuote = obligation.info.collateralInQuote;
const collateral = collateralInQuote * collateralPrice
if (collateralValue) {
const collateralRatio = (parseFloat(collateralValue) / collateral) * 100
const borrowValue = (collateralRatio * borrowAmount) / 100
setValue(borrowValue.toString())
} else {
setValue("")
}
}
}, [lastTyped, collateralReserve, obligation.info.collateralInQuote, collateralPrice, borrowAmount, collateralValue])
const bodyStyle: React.CSSProperties = { const bodyStyle: React.CSSProperties = {
display: "flex", display: "flex",
flex: 1, flex: 1,
@ -145,33 +175,36 @@ export const RepayInput = (props: {
justifyContent: "space-around", justifyContent: "space-around",
}} }}
> >
<div className="repay-input-title">{LABELS.REPAY_QUESTION}</div> <div style={{ display: 'flex', flexDirection: 'row', justifyContent: 'space-evenly', alignItems: 'center' }}>
<div className="token-input"> <CollateralInput
<TokenIcon mintAddress={repayReserve?.info.liquidityMint} /> title='Repay Amount'
<NumericInput reserve={repayReserve.info}
value={value} amount={parseFloat(value) || 0}
onChange={setValue} onInputChange={(val: number | null) => {
autoFocus={true} setValue(val?.toString() || "")
style={{ setLastTyped("repay")
fontSize: 20,
boxShadow: "none",
borderColor: "transparent",
outline: "transparent",
}} }}
placeholder="0.00" disabled={true}
hideBalance={true}
/> />
<div>{name}</div>
</div> </div>
<Slider marks={marks} value={pct} onChange={setPct} /> <Slider marks={marks} value={pct} onChange={setPct} />
<div className="repay-input-title">{LABELS.COLLATERAL}</div> <div style={{ display: 'flex', flexDirection: 'row', justifyContent: 'space-evenly', alignItems: 'center' }}>
<CollateralSelector <CollateralInput
reserve={repayReserve.info} title='Collateral Amount (estimated)'
collateralReserve={collateralReserve?.pubkey.toBase58()} reserve={collateralReserve?.info}
disabled={true} amount={parseFloat(collateralValue) || 0}
/> onInputChange={(val: number | null) => {
setCollateralValue(val?.toString() || "")
setLastTyped("collateral")
}}
disabled={true}
hideBalance={true}
/>
</div>
<ConnectButton <ConnectButton
type="primary" type="primary"
size="large"
onClick={onRepay} onClick={onRepay}
loading={pendingTx} loading={pendingTx}
disabled={fromAccounts.length === 0} disabled={fromAccounts.length === 0}

View File

@ -275,6 +275,7 @@ export const useEnrichedPools = (pools: PoolInfo[]) => {
}; };
// Do not add pools here, causes a really bad infinite rendering loop. Use poolKeys instead. // Do not add pools here, causes a really bad infinite rendering loop. Use poolKeys instead.
}, [ }, [
pools,
tokenMap, tokenMap,
dailyVolume, dailyVolume,
poolKeys, poolKeys,

View File

@ -37,7 +37,7 @@ export const RepayReserveView = () => {
const reserve = lendingReserve?.info; const reserve = lendingReserve?.info;
if (!reserve || !lendingReserve || !lendingObligation) { if (!reserve || !lendingReserve || !lendingObligation || !repayReserve) {
return null; return null;
} }