collateral calculation from borrow input

This commit is contained in:
juan 2021-01-24 12:30:29 -05:00
parent 60a47aa013
commit d69762d1b8
1 changed files with 19 additions and 1 deletions

View File

@ -31,6 +31,7 @@ export const BorrowInput = (props: {
const { wallet } = useWallet();
const [value, setValue] = useState("");
const [collateralValue, setCollateralValue] = useState("");
const [lastTyped, setLastTyped] = useState("collateral");
const [pendingTx, setPendingTx] = useState(false);
const [showConfirmation, setShowConfirmation] = useState(false);
@ -51,7 +52,7 @@ export const BorrowInput = (props: {
const collateralPrice = useMidPriceInUSD(collateralReserve?.info.liquidityMint.toBase58())?.price;
useEffect(() => {
if (collateralReserve) {
if (collateralReserve && lastTyped == "collateral") {
const ltv = borrowReserve.info.config.loanToValueRatio / 100;
if (collateralValue) {
@ -65,6 +66,21 @@ export const BorrowInput = (props: {
}
}, [collateralReserve, collateralPrice, borrowPrice, borrowReserve, collateralValue])
useEffect(() => {
if (collateralReserve && lastTyped == "borrow") {
const ltv = borrowReserve.info.config.loanToValueRatio / 100;
if (value) {
const nValue = parseFloat(value);
const borrowInUSD = nValue * borrowPrice;
const collateralAmount = (borrowInUSD / ltv) / collateralPrice
setCollateralValue(collateralAmount.toString())
} else {
setCollateralValue("")
}
}
}, [lastTyped, collateralReserve, collateralPrice, borrowPrice, borrowReserve, value])
const name = useTokenName(borrowReserve?.info.liquidityMint);
const { accounts: fromAccounts } = useUserBalance(
collateralReserve?.info.collateralMint
@ -153,6 +169,7 @@ export const BorrowInput = (props: {
amount={parseFloat(collateralValue) || 0}
onInputChange={(val: number | null) => {
setCollateralValue(val?.toString() || "")
setLastTyped("collateral")
}}
onCollateralReserve={(key) => {
setCollateralReserveKey(key);
@ -167,6 +184,7 @@ export const BorrowInput = (props: {
amount={parseFloat(value) || 0}
onInputChange={(val: number | null) => {
setValue(val?.toString() || "")
setLastTyped("borrow")
}}
disabled={true}
hideBalance={true}