Skip to content

Commit

Permalink
feat: support stroops in borrow
Browse files Browse the repository at this point in the history
  • Loading branch information
kovipu committed Feb 11, 2025
1 parent b62402d commit 66efb3d
Showing 1 changed file with 24 additions and 26 deletions.
50 changes: 24 additions & 26 deletions src/pages/_borrow/BorrowModal/BorrowStep.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type ChangeEvent, useState } from 'react';
import { useState } from 'react';

import { Button } from '@components/Button';
import { CryptoAmountSelector } from '@components/CryptoAmountSelector';
Expand All @@ -9,8 +9,8 @@ import { useLoans } from '@contexts/loan-context';
import { usePools } from '@contexts/pool-context';
import { useWallet } from '@contexts/wallet-context';
import { contractClient as loanManagerClient } from '@contracts/loan_manager';
import { getIntegerPart, isBalanceZero, to7decimals } from '@lib/converters';
import { SCALAR_7, formatAPR, fromCents, toCents } from '@lib/formatting';
import { decimalStringToStroops, isBalanceZero } from '@lib/converters';
import { formatAPR, fromCents, toCents } from '@lib/formatting';
import type { SupportedCurrency } from 'currencies';
import { CURRENCY_BINDINGS, CURRENCY_BINDINGS_ARR, type CurrencyBinding } from 'src/currency-bindings';

Expand All @@ -28,7 +28,7 @@ export const BorrowStep = ({ onClose, currency }: BorrowStepProps) => {
const [isBorrowing, setIsBorrowing] = useState(false);
const [isBorrowingSuccess, setIsBorrowingSuccess] = useState(false);
const [borrowingError, setBorrowingError] = useState<Error | null>(null);
const [loanAmount, setLoanAmount] = useState<string>('0');
const [loanAmount, setLoanAmount] = useState(0n);

const collateralOptions: SupportedCurrency[] = CURRENCY_BINDINGS_ARR.filter((c) => c.ticker !== ticker).map(
({ ticker }) => ticker,
Expand All @@ -39,7 +39,7 @@ export const BorrowStep = ({ onClose, currency }: BorrowStepProps) => {
});

const [collateralTicker, setCollateralTicker] = useState<SupportedCurrency>(initialCollateral ?? 'XLM');
const [collateralAmount, setCollateralAmount] = useState<string>('0');
const [collateralAmount, setCollateralAmount] = useState(0n);

if (!pools || !prices || !walletBalances) return null;

Expand All @@ -51,17 +51,17 @@ export const BorrowStep = ({ onClose, currency }: BorrowStepProps) => {
const loanPrice = prices[ticker];
const collateralPrice = prices[collateralTicker];

const loanAmountCents = loanPrice ? toCents(loanPrice, BigInt(loanAmount) * SCALAR_7) : undefined;
const loanAmountCents = loanPrice ? toCents(loanPrice, loanAmount) : undefined;
const collateralAmountCents = collateralPrice
? toCents(collateralPrice, BigInt(collateralAmount) * SCALAR_7)
? toCents(collateralPrice, collateralAmount)
: undefined;

const healthFactor =
loanAmountCents && loanAmountCents > 0n ? Number(collateralAmountCents) / Number(loanAmountCents) : 0;

const handleClose = () => {
setLoanAmount('0');
setCollateralAmount('0');
setLoanAmount(0n);
setCollateralAmount(0n);
setIsBorrowing(false);
setIsBorrowingSuccess(false);
setBorrowingError(null);
Expand All @@ -85,9 +85,9 @@ export const BorrowStep = ({ onClose, currency }: BorrowStepProps) => {

const tx = await loanManagerClient.create_loan({
user: wallet.address,
borrowed: to7decimals(loanAmount),
borrowed: loanAmount,
borrowed_from: loanCurrencyId,
collateral: to7decimals(collateralAmount),
collateral: collateralAmount,
collateral_from: collateralCurrencyId,
});
await tx.signAndSend({ signTransaction });
Expand All @@ -103,41 +103,39 @@ export const BorrowStep = ({ onClose, currency }: BorrowStepProps) => {
setIsBorrowing(false);
};

const handleLoanAmountChange = (ev: ChangeEvent<HTMLInputElement>) => {
setLoanAmount(ev.target.value);
const handleLoanAmountChange = (stroops: bigint) => {
setLoanAmount(stroops);

if (!loanPrice || !collateralPrice) return;

// Move the collateral to reach the good health threshold
const loanAmountCents = toCents(loanPrice, BigInt(ev.target.value) * SCALAR_7);
const loanAmountCents = toCents(loanPrice, stroops);
const minHealthyCollateralCents = BigInt(Math.ceil(HEALTH_FACTOR_AUTO_THRESHOLD * Number(loanAmountCents) + 100));
const minHealthyCollateral = fromCents(collateralPrice, minHealthyCollateralCents) / SCALAR_7;
if (minHealthyCollateral <= BigInt(maxCollateral)) {
setCollateralAmount(minHealthyCollateral.toString());
const minHealthyCollateral = fromCents(collateralPrice, minHealthyCollateralCents);
if (minHealthyCollateral <= maxCollateral) {
setCollateralAmount(minHealthyCollateral);
} else {
setCollateralAmount(maxCollateral);
}
};

const handleCollateralAmountChange = (ev: ChangeEvent<HTMLInputElement>) => {
setCollateralAmount(ev.target.value);
const handleCollateralAmountChange = (stroops: bigint) => {
setCollateralAmount(stroops);
};

const handleCollateralTickerChange = (ticker: SupportedCurrency) => {
setCollateralTicker(ticker);
setCollateralAmount('0');
setCollateralAmount(0n);
};

const isTrustline = loanBalance.trustLine;

const isBorrowDisabled =
!isTrustline || loanAmount === '0' || collateralAmount === '0' || healthFactor < HEALTH_FACTOR_MIN_THRESHOLD;
!isTrustline || loanAmount === 0n || collateralAmount === 0n || healthFactor < HEALTH_FACTOR_MIN_THRESHOLD;

const maxLoan = (availableBalanceTokens / 10_000_000n).toString();
const maxCollateral = decimalStringToStroops(collateralBalance.trustLine ? collateralBalance.balanceLine.balance : '0');

const maxCollateral = getIntegerPart(collateralBalance.trustLine ? collateralBalance.balanceLine.balance : '0');

const handleSelectMaxLoan = () => setLoanAmount(maxLoan);
const handleSelectMaxLoan = () => setLoanAmount(availableBalanceTokens);

const handleSelectMaxCollateral = () => setCollateralAmount(maxCollateral);

Expand Down Expand Up @@ -179,7 +177,7 @@ export const BorrowStep = ({ onClose, currency }: BorrowStepProps) => {

<p className="font-bold mb-2 mt-6">Amount to borrow</p>
<CryptoAmountSelector
max={maxLoan}
max={availableBalanceTokens}
value={loanAmount}
valueCents={loanAmountCents}
ticker={ticker}
Expand Down

0 comments on commit 66efb3d

Please sign in to comment.