diff --git a/README.md b/README.md index d2d4c24..c2780eb 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ ## ⚠️ Warning - + Any content produced by Solana, or developer resources that Solana provides, are for educational and inspiration purposes only. Solana does not encourage, induce or sanction the deployment of any such applications in violation of applicable laws or regulations. diff --git a/src/actions/account.ts b/src/actions/account.ts index 7e6e15c..daf99b7 100644 --- a/src/actions/account.ts +++ b/src/actions/account.ts @@ -1,8 +1,17 @@ -import { AccountLayout, MintLayout, Token } from '@solana/spl-token'; -import { Account, PublicKey, SystemProgram, TransactionInstruction } from '@solana/web3.js'; -import { LENDING_PROGRAM_ID, TOKEN_PROGRAM_ID, WRAPPED_SOL_MINT } from '../utils/ids'; -import { LendingObligationLayout, TokenAccount } from '../models'; -import { cache, TokenAccountParser } from './../contexts/accounts'; +import { AccountLayout, MintLayout, Token } from "@solana/spl-token"; +import { + Account, + PublicKey, + SystemProgram, + TransactionInstruction, +} from "@solana/web3.js"; +import { + LENDING_PROGRAM_ID, + TOKEN_PROGRAM_ID, + WRAPPED_SOL_MINT, +} from "../utils/ids"; +import { LendingObligationLayout, TokenAccount } from "../models"; +import { cache, TokenAccountParser } from "./../contexts/accounts"; export function ensureSplAccount( instructions: TransactionInstruction[], @@ -16,11 +25,31 @@ export function ensureSplAccount( return toCheck.pubkey; } - const account = createUninitializedAccount(instructions, payer, amount, signers); + const account = createUninitializedAccount( + instructions, + payer, + amount, + signers + ); - instructions.push(Token.createInitAccountInstruction(TOKEN_PROGRAM_ID, WRAPPED_SOL_MINT, account, payer)); + instructions.push( + Token.createInitAccountInstruction( + TOKEN_PROGRAM_ID, + WRAPPED_SOL_MINT, + account, + payer + ) + ); - cleanupInstructions.push(Token.createCloseAccountInstruction(TOKEN_PROGRAM_ID, account, payer, payer, [])); + cleanupInstructions.push( + Token.createCloseAccountInstruction( + TOKEN_PROGRAM_ID, + account, + payer, + payer, + [] + ) + ); return account; } @@ -125,9 +154,16 @@ export function createTokenAccount( owner: PublicKey, signers: Account[] ) { - const account = createUninitializedAccount(instructions, payer, accountRentExempt, signers); + const account = createUninitializedAccount( + instructions, + payer, + accountRentExempt, + signers + ); - instructions.push(Token.createInitAccountInstruction(TOKEN_PROGRAM_ID, mint, account, owner)); + instructions.push( + Token.createInitAccountInstruction(TOKEN_PROGRAM_ID, mint, account, owner) + ); return account; } @@ -161,10 +197,25 @@ export function findOrCreateAccountByMint( toAccount = account.pubkey; } else { // creating depositor pool account - toAccount = createTokenAccount(instructions, payer, accountRentExempt, mint, owner, signers); + toAccount = createTokenAccount( + instructions, + payer, + accountRentExempt, + mint, + owner, + signers + ); if (isWrappedSol) { - cleanupInstructions.push(Token.createCloseAccountInstruction(TOKEN_PROGRAM_ID, toAccount, payer, payer, [])); + cleanupInstructions.push( + Token.createCloseAccountInstruction( + TOKEN_PROGRAM_ID, + toAccount, + payer, + payer, + [] + ) + ); } } diff --git a/src/actions/borrow.tsx b/src/actions/borrow.tsx index 220b7c8..f6dd589 100644 --- a/src/actions/borrow.tsx +++ b/src/actions/borrow.tsx @@ -6,7 +6,10 @@ import { } from "@solana/web3.js"; import { sendTransaction } from "../contexts/connection"; import { notify } from "../utils/notifications"; -import { LendingReserve } from "./../models/lending/reserve"; +import { + accrueInterestInstruction, + LendingReserve, +} from "./../models/lending/reserve"; import { AccountLayout, MintInfo, MintLayout } from "@solana/spl-token"; import { LENDING_PROGRAM_ID, LEND_HOST_FEE_ADDRESS } from "../utils/ids"; import { @@ -16,8 +19,8 @@ import { createUninitializedObligation, ensureSplAccount, findOrCreateAccountByMint, -} from './account'; -import { cache, MintParser, ParsedAccount } from '../contexts/accounts'; +} from "./account"; +import { cache, MintParser, ParsedAccount } from "../contexts/accounts"; import { TokenAccount, LendingObligationLayout, @@ -26,6 +29,7 @@ import { BorrowAmountType, LendingObligation, approve, + initObligationInstruction, } from "../models"; import { toLamports } from "../utils/utils"; @@ -46,38 +50,52 @@ export const borrow = async ( obligationAccount?: PublicKey ) => { notify({ - message: 'Borrowing funds...', - description: 'Please review transactions to approve.', - type: 'warn', + message: "Borrowing funds...", + description: "Please review transactions to approve.", + type: "warn", }); let signers: Account[] = []; let instructions: TransactionInstruction[] = []; let cleanupInstructions: TransactionInstruction[] = []; - const accountRentExempt = await connection.getMinimumBalanceForRentExemption(AccountLayout.span); + const [authority] = await PublicKey.findProgramAddress( + [depositReserve.info.lendingMarket.toBuffer()], + LENDING_PROGRAM_ID + ); + + const accountRentExempt = await connection.getMinimumBalanceForRentExemption( + AccountLayout.span + ); const obligation = existingObligation ? existingObligation.pubkey : createUninitializedObligation( - instructions, - wallet.publicKey, - await connection.getMinimumBalanceForRentExemption(LendingObligationLayout.span), - signers - ); + instructions, + wallet.publicKey, + await connection.getMinimumBalanceForRentExemption( + LendingObligationLayout.span + ), + signers + ); const obligationMint = existingObligation ? existingObligation.info.tokenMint : createUninitializedMint( - instructions, - wallet.publicKey, - await connection.getMinimumBalanceForRentExemption(MintLayout.span), - signers - ); + instructions, + wallet.publicKey, + await connection.getMinimumBalanceForRentExemption(MintLayout.span), + signers + ); const obligationTokenOutput = obligationAccount ? obligationAccount - : createUninitializedAccount(instructions, wallet.publicKey, accountRentExempt, signers); + : createUninitializedAccount( + instructions, + wallet.publicKey, + accountRentExempt, + signers + ); let toAccount = await findOrCreateAccountByMint( wallet.publicKey, @@ -89,32 +107,57 @@ export const borrow = async ( signers ); + if (!obligationAccount) { + instructions.push( + initObligationInstruction( + depositReserve.pubkey, + borrowReserve.pubkey, + obligation, + obligationMint, + obligationTokenOutput, + wallet.publicKey, + depositReserve.info.lendingMarket, + authority + ) + ); + } + + // Creates host fee account if it doesn't exsist + let hostFeeReceiver = LEND_HOST_FEE_ADDRESS + ? findOrCreateAccountByMint( + wallet.publicKey, + LEND_HOST_FEE_ADDRESS, + instructions, + cleanupInstructions, + accountRentExempt, + depositReserve.info.collateralMint, + signers + ) + : undefined; + if (instructions.length > 0) { // create all accounts in one transaction - let tx = await sendTransaction(connection, wallet, instructions, [...signers]); + let tx = await sendTransaction(connection, wallet, instructions, [ + ...signers, + ]); notify({ - message: 'Obligation accounts created', + message: "Obligation accounts created", description: `Transaction ${tx}`, - type: 'success', + type: "success", }); } notify({ - message: 'Borrowing funds...', - description: 'Please review transactions to approve.', - type: 'warn', + message: "Borrowing funds...", + description: "Please review transactions to approve.", + type: "warn", }); signers = []; instructions = []; cleanupInstructions = []; - const [authority] = await PublicKey.findProgramAddress( - [depositReserve.info.lendingMarket.toBuffer()], - LENDING_PROGRAM_ID - ); - let amountLamports: number = 0; let fromLamports: number = 0; if (amountType === BorrowAmountType.LiquidityBorrowAmount) { @@ -124,15 +167,19 @@ export const borrow = async ( fromLamports = approvedAmount - accountRentExempt; - const mint = (await cache.query(connection, borrowReserve.info.liquidityMint, MintParser)) as ParsedAccount< - MintInfo - >; + const mint = (await cache.query( + connection, + borrowReserve.info.liquidityMint, + MintParser + )) as ParsedAccount; amountLamports = toLamports(amount, mint?.info); } else if (amountType === BorrowAmountType.CollateralDepositAmount) { - const mint = (await cache.query(connection, depositReserve.info.collateralMint, MintParser)) as ParsedAccount< - MintInfo - >; + const mint = (await cache.query( + connection, + depositReserve.info.collateralMint, + MintParser + )) as ParsedAccount; amountLamports = toLamports(amount, mint?.info); fromLamports = amountLamports; } @@ -165,27 +212,27 @@ export const borrow = async ( throw new Error(`Dex market doesn't exist.`); } - const market = cache.get(depositReserve.info.lendingMarket) as ParsedAccount; - const dexOrderBookSide = market.info.quoteMint.equals(depositReserve.info.liquidityMint) + const market = cache.get(depositReserve.info.lendingMarket) as ParsedAccount< + LendingMarket + >; + const dexOrderBookSide = market.info.quoteMint.equals( + depositReserve.info.liquidityMint + ) ? dexMarket?.info.asks : dexMarket?.info.bids; - const memory = createTempMemoryAccount(instructions, wallet.publicKey, signers, LENDING_PROGRAM_ID); + const memory = createTempMemoryAccount( + instructions, + wallet.publicKey, + signers, + LENDING_PROGRAM_ID + ); - // Creates host fee account if it doesn't exsist - let hostFeeReceiver = LEND_HOST_FEE_ADDRESS - ? findOrCreateAccountByMint( - wallet.publicKey, - LEND_HOST_FEE_ADDRESS, - instructions, - cleanupInstructions, - accountRentExempt, - depositReserve.info.collateralMint, - signers - ) - : undefined; + instructions.push( + accrueInterestInstruction(depositReserve.pubkey, borrowReserve.pubkey) + ); - // deposit + // borrow instructions.push( borrowInstruction( amountLamports, @@ -202,7 +249,6 @@ export const borrow = async ( obligation, obligationMint, obligationTokenOutput, - wallet.publicKey, depositReserve.info.lendingMarket, authority, @@ -213,19 +259,25 @@ export const borrow = async ( memory, - hostFeeReceiver, + hostFeeReceiver ) ); try { - let tx = await sendTransaction(connection, wallet, instructions.concat(cleanupInstructions), signers, true); + let tx = await sendTransaction( + connection, + wallet, + instructions.concat(cleanupInstructions), + signers, + true + ); notify({ - message: 'Funds borrowed.', - type: 'success', + message: "Funds borrowed.", + type: "success", description: `Transaction - ${tx}`, }); - } catch { - // TODO: + } catch (ex) { + console.error(ex); throw new Error(); } }; diff --git a/src/actions/deposit.tsx b/src/actions/deposit.tsx index 454f3a2..a1a700a 100644 --- a/src/actions/deposit.tsx +++ b/src/actions/deposit.tsx @@ -7,6 +7,7 @@ import { import { sendTransaction } from "../contexts/connection"; import { notify } from "../utils/notifications"; import { + accrueInterestInstruction, depositInstruction, initReserveInstruction, LendingReserve, @@ -29,9 +30,9 @@ export const deposit = async ( wallet: any ) => { notify({ - message: 'Depositing funds...', - description: 'Please review transactions to approve.', - type: 'warn', + message: "Depositing funds...", + description: "Please review transactions to approve.", + type: "warn", }); const isInitalized = true; // TODO: finish reserve init @@ -41,7 +42,9 @@ export const deposit = async ( const instructions: TransactionInstruction[] = []; const cleanupInstructions: TransactionInstruction[] = []; - const accountRentExempt = await connection.getMinimumBalanceForRentExemption(AccountLayout.span); + const accountRentExempt = await connection.getMinimumBalanceForRentExemption( + AccountLayout.span + ); const [authority] = await PublicKey.findProgramAddress( [reserve.lendingMarket.toBuffer()], // which account should be authority @@ -81,10 +84,17 @@ export const deposit = async ( signers ); } else { - toAccount = createUninitializedAccount(instructions, wallet.publicKey, accountRentExempt, signers); + toAccount = createUninitializedAccount( + instructions, + wallet.publicKey, + accountRentExempt, + signers + ); } if (isInitalized) { + instructions.push(accrueInterestInstruction(reserveAddress)); + // deposit instructions.push( depositInstruction( @@ -122,11 +132,17 @@ export const deposit = async ( } try { - let tx = await sendTransaction(connection, wallet, instructions.concat(cleanupInstructions), signers, true); + let tx = await sendTransaction( + connection, + wallet, + instructions.concat(cleanupInstructions), + signers, + true + ); notify({ - message: 'Funds deposited.', - type: 'success', + message: "Funds deposited.", + type: "success", description: `Transaction - ${tx}`, }); } catch { diff --git a/src/actions/liquidate.tsx b/src/actions/liquidate.tsx index d9f82c1..b52fa18 100644 --- a/src/actions/liquidate.tsx +++ b/src/actions/liquidate.tsx @@ -6,12 +6,24 @@ import { } from "@solana/web3.js"; import { sendTransaction } from "../contexts/connection"; import { notify } from "../utils/notifications"; -import { LendingReserve } from "./../models/lending/reserve"; +import { + accrueInterestInstruction, + LendingReserve, +} from "./../models/lending/reserve"; import { liquidateInstruction } from "./../models/lending/liquidate"; import { AccountLayout } from "@solana/spl-token"; import { LENDING_PROGRAM_ID } from "../utils/ids"; -import { createTempMemoryAccount, ensureSplAccount, findOrCreateAccountByMint } from "./account"; -import { approve, LendingMarket, LendingObligation, TokenAccount } from "../models"; +import { + createTempMemoryAccount, + ensureSplAccount, + findOrCreateAccountByMint, +} from "./account"; +import { + approve, + LendingMarket, + LendingObligation, + TokenAccount, +} from "../models"; import { cache, ParsedAccount } from "../contexts/accounts"; export const liquidate = async ( @@ -28,9 +40,9 @@ export const liquidate = async ( withdrawReserve: ParsedAccount ) => { notify({ - message: 'Repaying funds...', - description: 'Please review transactions to approve.', - type: 'warn', + message: "Repaying funds...", + description: "Please review transactions to approve.", + type: "warn", }); // user from account @@ -38,7 +50,9 @@ export const liquidate = async ( const instructions: TransactionInstruction[] = []; const cleanupInstructions: TransactionInstruction[] = []; - const accountRentExempt = await connection.getMinimumBalanceForRentExemption(AccountLayout.span); + const accountRentExempt = await connection.getMinimumBalanceForRentExemption( + AccountLayout.span + ); const [authority] = await PublicKey.findProgramAddress( [repayReserve.info.lendingMarket.toBuffer()], @@ -84,13 +98,26 @@ export const liquidate = async ( throw new Error(`Dex market doesn't exist.`); } - const market = cache.get(withdrawReserve.info.lendingMarket) as ParsedAccount; + const market = cache.get(withdrawReserve.info.lendingMarket) as ParsedAccount< + LendingMarket + >; - const dexOrderBookSide = market.info.quoteMint.equals(repayReserve.info.liquidityMint) + const dexOrderBookSide = market.info.quoteMint.equals( + repayReserve.info.liquidityMint + ) ? dexMarket?.info.asks : dexMarket?.info.bids; - const memory = createTempMemoryAccount(instructions, wallet.publicKey, signers, LENDING_PROGRAM_ID); + const memory = createTempMemoryAccount( + instructions, + wallet.publicKey, + signers, + LENDING_PROGRAM_ID + ); + + instructions.push( + accrueInterestInstruction(repayReserve.pubkey, withdrawReserve.pubkey) + ); instructions.push( liquidateInstruction( @@ -111,11 +138,17 @@ export const liquidate = async ( ) ); - let tx = await sendTransaction(connection, wallet, instructions.concat(cleanupInstructions), signers, true); + let tx = await sendTransaction( + connection, + wallet, + instructions.concat(cleanupInstructions), + signers, + true + ); notify({ - message: 'Funds liquidated.', - type: 'success', + message: "Funds liquidated.", + type: "success", description: `Transaction - ${tx}`, }); }; diff --git a/src/actions/repay.tsx b/src/actions/repay.tsx index e4365d6..1d5d80b 100644 --- a/src/actions/repay.tsx +++ b/src/actions/repay.tsx @@ -6,7 +6,10 @@ import { } from "@solana/web3.js"; import { sendTransaction } from "../contexts/connection"; import { notify } from "../utils/notifications"; -import { LendingReserve } from "./../models/lending/reserve"; +import { + accrueInterestInstruction, + LendingReserve, +} from "./../models/lending/reserve"; import { repayInstruction } from "./../models/lending/repay"; import { AccountLayout, Token, NATIVE_MINT } from "@solana/spl-token"; import { LENDING_PROGRAM_ID, TOKEN_PROGRAM_ID } from "../utils/ids"; @@ -31,9 +34,9 @@ export const repay = async ( wallet: any ) => { notify({ - message: 'Repaying funds...', - description: 'Please review transactions to approve.', - type: 'warn', + message: "Repaying funds...", + description: "Please review transactions to approve.", + type: "warn", }); // user from account @@ -41,7 +44,9 @@ export const repay = async ( const instructions: TransactionInstruction[] = []; const cleanupInstructions: TransactionInstruction[] = []; - const accountRentExempt = await connection.getMinimumBalanceForRentExemption(AccountLayout.span); + const accountRentExempt = await connection.getMinimumBalanceForRentExemption( + AccountLayout.span + ); const [authority] = await PublicKey.findProgramAddress( [repayReserve.info.lendingMarket.toBuffer()], @@ -49,9 +54,27 @@ export const repay = async ( ); let fromAccount = from.pubkey; - if (wallet.publicKey.equals(fromAccount) && repayReserve.info.liquidityMint.equals(NATIVE_MINT)) { - fromAccount = createTokenAccount(instructions, wallet.publicKey, accountRentExempt + repayAmount, NATIVE_MINT, wallet.publicKey, signers); - cleanupInstructions.push(Token.createCloseAccountInstruction(TOKEN_PROGRAM_ID, fromAccount, wallet.publicKey, wallet.publicKey, [])); + if ( + wallet.publicKey.equals(fromAccount) && + repayReserve.info.liquidityMint.equals(NATIVE_MINT) + ) { + fromAccount = createTokenAccount( + instructions, + wallet.publicKey, + accountRentExempt + repayAmount, + NATIVE_MINT, + wallet.publicKey, + signers + ); + cleanupInstructions.push( + Token.createCloseAccountInstruction( + TOKEN_PROGRAM_ID, + fromAccount, + wallet.publicKey, + wallet.publicKey, + [] + ) + ); } // create approval for transfer transactions @@ -84,10 +107,12 @@ export const repay = async ( obligationToken.info.amount.toNumber(), // reuse transfer authority - transferAuthority.publicKey, + transferAuthority.publicKey ); - // TODO: add obligation + instructions.push( + accrueInterestInstruction(repayReserve.pubkey, withdrawReserve.pubkey) + ); instructions.push( repayInstruction( @@ -103,15 +128,21 @@ export const repay = async ( obligationToken.pubkey, repayReserve.info.lendingMarket, authority, - transferAuthority.publicKey, + transferAuthority.publicKey ) ); - let tx = await sendTransaction(connection, wallet, instructions.concat(cleanupInstructions), signers, true); + let tx = await sendTransaction( + connection, + wallet, + instructions.concat(cleanupInstructions), + signers, + true + ); notify({ - message: 'Funds repaid.', - type: 'success', + message: "Funds repaid.", + type: "success", description: `Transaction - ${tx}`, }); }; diff --git a/src/actions/withdraw.tsx b/src/actions/withdraw.tsx index 2e3455c..4ee4745 100644 --- a/src/actions/withdraw.tsx +++ b/src/actions/withdraw.tsx @@ -6,7 +6,11 @@ import { } from "@solana/web3.js"; import { sendTransaction } from "../contexts/connection"; import { notify } from "../utils/notifications"; -import { LendingReserve, withdrawInstruction } from "./../models/lending"; +import { + accrueInterestInstruction, + LendingReserve, + withdrawInstruction, +} from "./../models/lending"; import { AccountLayout } from "@solana/spl-token"; import { LENDING_PROGRAM_ID } from "../utils/ids"; import { findOrCreateAccountByMint } from "./account"; @@ -21,9 +25,9 @@ export const withdraw = async ( wallet: any ) => { notify({ - message: 'Withdrawing funds...', - description: 'Please review transactions to approve.', - type: 'warn', + message: "Withdrawing funds...", + description: "Please review transactions to approve.", + type: "warn", }); // user from account @@ -31,19 +35,24 @@ export const withdraw = async ( const instructions: TransactionInstruction[] = []; const cleanupInstructions: TransactionInstruction[] = []; - const accountRentExempt = await connection.getMinimumBalanceForRentExemption(AccountLayout.span); + const accountRentExempt = await connection.getMinimumBalanceForRentExemption( + AccountLayout.span + ); - const [authority] = await PublicKey.findProgramAddress([reserve.lendingMarket.toBuffer()], LENDING_PROGRAM_ID); + const [authority] = await PublicKey.findProgramAddress( + [reserve.lendingMarket.toBuffer()], + LENDING_PROGRAM_ID + ); const fromAccount = from.pubkey; // create approval for transfer transactions const transferAuthority = approve( - instructions, - cleanupInstructions, - fromAccount, - wallet.publicKey, - amountLamports + instructions, + cleanupInstructions, + fromAccount, + wallet.publicKey, + amountLamports ); signers.push(transferAuthority); @@ -59,6 +68,8 @@ export const withdraw = async ( signers ); + instructions.push(accrueInterestInstruction(reserveAddress)); + instructions.push( withdrawInstruction( amountLamports, @@ -69,16 +80,22 @@ export const withdraw = async ( reserve.liquiditySupply, reserve.lendingMarket, authority, - transferAuthority.publicKey, + transferAuthority.publicKey ) ); try { - let tx = await sendTransaction(connection, wallet, instructions.concat(cleanupInstructions), signers, true); + let tx = await sendTransaction( + connection, + wallet, + instructions.concat(cleanupInstructions), + signers, + true + ); notify({ - message: 'Funds deposited.', - type: 'success', + message: "Funds deposited.", + type: "success", description: `Transaction - ${tx}`, }); } catch { diff --git a/src/components/BarChartStatistic/index.tsx b/src/components/BarChartStatistic/index.tsx index 3b3e336..fd7ab6b 100644 --- a/src/components/BarChartStatistic/index.tsx +++ b/src/components/BarChartStatistic/index.tsx @@ -1,7 +1,7 @@ import { Statistic } from "antd"; -import React, { } from "react"; +import React from "react"; -export const BarChartStatistic = (props: { +export const BarChartStatistic = (props: { items: T[]; title?: string; name: (item: T) => string; @@ -9,34 +9,47 @@ export const BarChartStatistic = (props: { getPct: (item: T) => number; }) => { const colors = [ - '#003f5c', - '#2f4b7c', - '#665191', - '#a05195', - '#d45087', - '#f95d6a', - '#ff7c43', - '#ffa600', + "#003f5c", + "#2f4b7c", + "#665191", + "#a05195", + "#d45087", + "#f95d6a", + "#ff7c43", + "#ffa600", ].reverse(); return ( - -
- {props.items.map((item, i) => -
( +
+ {props.items.map((item, i) => ( +
- {props.name(item)} -
)} -
} - > - + width: `${100 * props.getPct(item)}%`, + backgroundColor: + (props.color && props.color(item)) || + colors[i % props.items.length], + }} + > + {props.name(item)} +
+ ))} +
+ )} + >
); }; diff --git a/src/components/BorrowInput/index.tsx b/src/components/BorrowInput/index.tsx index f0c6740..dbdd27a 100644 --- a/src/components/BorrowInput/index.tsx +++ b/src/components/BorrowInput/index.tsx @@ -157,7 +157,9 @@ export const BorrowInput = (props: { loading={pendingTx} disabled={fromAccounts.length === 0} > - {fromAccounts.length === 0 ? LABELS.NO_DEPOSITS : LABELS.BORROW_ACTION} + {fromAccounts.length === 0 + ? LABELS.NO_DEPOSITS + : LABELS.BORROW_ACTION} diff --git a/src/components/CollateralInput/index.tsx b/src/components/CollateralInput/index.tsx index 059c1b8..a29ed4d 100644 --- a/src/components/CollateralInput/index.tsx +++ b/src/components/CollateralInput/index.tsx @@ -1,14 +1,18 @@ -import React, { useEffect, useState } from 'react'; -import { cache, ParsedAccount } from '../../contexts/accounts'; -import { useConnectionConfig } from '../../contexts/connection'; -import { useLendingReserves, useUserDeposits } from '../../hooks'; -import { LendingReserve, LendingMarket, LendingReserveParser } from '../../models'; -import { getTokenName } from '../../utils/utils'; -import { Card, Select } from 'antd'; -import { TokenIcon } from '../TokenIcon'; -import { NumericInput } from '../Input/numeric'; -import './style.less'; -import { TokenDisplay } from '../TokenDisplay'; +import React, { useEffect, useState } from "react"; +import { cache, ParsedAccount } from "../../contexts/accounts"; +import { useConnectionConfig } from "../../contexts/connection"; +import { useLendingReserves, useUserDeposits } from "../../hooks"; +import { + LendingReserve, + LendingMarket, + LendingReserveParser, +} from "../../models"; +import { getTokenName } from "../../utils/utils"; +import { Card, Select } from "antd"; +import { TokenIcon } from "../TokenIcon"; +import { NumericInput } from "../Input/numeric"; +import "./style.less"; +import { TokenDisplay } from "../TokenDisplay"; const { Option } = Select; @@ -30,36 +34,49 @@ export default function CollateralInput(props: { const { tokenMap } = useConnectionConfig(); const [collateralReserve, setCollateralReserve] = useState(); const [balance, setBalance] = useState(0); - const [lastAmount, setLastAmount] = useState(''); + const [lastAmount, setLastAmount] = useState(""); const userDeposits = useUserDeposits(); useEffect(() => { - const id: string = cache.byParser(LendingReserveParser).find((acc) => acc === collateralReserve) || ''; + const id: string = + cache + .byParser(LendingReserveParser) + .find((acc) => acc === collateralReserve) || ""; const parser = cache.get(id) as ParsedAccount; if (parser) { const collateralDeposit = userDeposits.userDeposits.find( - (u) => u.reserve.info.liquidityMint.toBase58() === parser.info.liquidityMint.toBase58() + (u) => + u.reserve.info.liquidityMint.toBase58() === + parser.info.liquidityMint.toBase58() ); if (collateralDeposit) setBalance(collateralDeposit.info.amount); else setBalance(0); } }, [collateralReserve, userDeposits]); - const market = cache.get(props.reserve.lendingMarket) as ParsedAccount; + const market = cache.get(props.reserve.lendingMarket) as ParsedAccount< + LendingMarket + >; if (!market) return null; - const onlyQuoteAllowed = !props.reserve?.liquidityMint?.equals(market?.info?.quoteMint); + const onlyQuoteAllowed = !props.reserve?.liquidityMint?.equals( + market?.info?.quoteMint + ); const renderReserveAccounts = reserveAccounts .filter((reserve) => reserve.info !== props.reserve) - .filter((reserve) => !onlyQuoteAllowed || reserve.info.liquidityMint.equals(market.info.quoteMint)) + .filter( + (reserve) => + !onlyQuoteAllowed || + reserve.info.liquidityMint.equals(market.info.quoteMint) + ) .map((reserve) => { const mint = reserve.info.liquidityMint.toBase58(); const address = reserve.pubkey.toBase58(); const name = getTokenName(tokenMap, mint); return (