From 135e61982ff0b5fed78bb68e30bef7b857ed2e4e Mon Sep 17 00:00:00 2001 From: bartosz-lipinski <264380+bartosz-lipinski@users.noreply.github.com> Date: Mon, 25 Jan 2021 20:58:57 -0600 Subject: [PATCH] fix: item keys --- src/actions/borrow.tsx | 77 +++++++++++++----------- src/actions/repay.tsx | 2 +- src/components/BorrowInput/index.tsx | 2 +- src/components/RepayInput/index.tsx | 31 +++++----- src/components/WithdrawInput/index.tsx | 2 +- src/constants/labels.ts | 4 +- src/contexts/accounts.tsx | 4 ++ src/models/account.ts | 9 ++- src/views/borrow/index.tsx | 2 +- src/views/dashboard/deposit/index.tsx | 2 +- src/views/dashboard/index.tsx | 32 +++++----- src/views/dashboard/obligation/index.tsx | 4 +- src/views/deposit/view/index.tsx | 2 +- src/views/home/index.tsx | 1 + 14 files changed, 96 insertions(+), 78 deletions(-) diff --git a/src/actions/borrow.tsx b/src/actions/borrow.tsx index f6dd589..0045551 100644 --- a/src/actions/borrow.tsx +++ b/src/actions/borrow.tsx @@ -58,6 +58,7 @@ export const borrow = async ( let signers: Account[] = []; let instructions: TransactionInstruction[] = []; let cleanupInstructions: TransactionInstruction[] = []; + let finalCleanupInstructions: TransactionInstruction[] = []; const [authority] = await PublicKey.findProgramAddress( [depositReserve.info.lendingMarket.toBuffer()], @@ -97,16 +98,6 @@ export const borrow = async ( signers ); - let toAccount = await findOrCreateAccountByMint( - wallet.publicKey, - wallet.publicKey, - instructions, - cleanupInstructions, - accountRentExempt, - borrowReserve.info.liquidityMint, - signers - ); - if (!obligationAccount) { instructions.push( initObligationInstruction( @@ -128,35 +119,14 @@ export const borrow = async ( 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, - ]); - - notify({ - message: "Obligation accounts created", - description: `Transaction ${tx}`, - type: "success", - }); - } - - notify({ - message: "Borrowing funds...", - description: "Please review transactions to approve.", - type: "warn", - }); - - signers = []; - instructions = []; - cleanupInstructions = []; + let amountLamports: number = 0; let fromLamports: number = 0; @@ -184,22 +154,58 @@ export const borrow = async ( fromLamports = amountLamports; } + const fromAccount = ensureSplAccount( instructions, - cleanupInstructions, + finalCleanupInstructions, from, wallet.publicKey, fromLamports + accountRentExempt, signers ); + let toAccount = await findOrCreateAccountByMint( + wallet.publicKey, + wallet.publicKey, + instructions, + finalCleanupInstructions, + accountRentExempt, + borrowReserve.info.liquidityMint, + signers + ); + + if (instructions.length > 0) { + // create all accounts in one transaction + let tx = await sendTransaction(connection, wallet, instructions, [ + ...signers, + ]); + + notify({ + message: "Obligation accounts created", + description: `Transaction ${tx}`, + type: "success", + }); + } + + notify({ + message: "Borrowing funds...", + description: "Please review transactions to approve.", + type: "warn", + }); + + signers = []; + instructions = []; + cleanupInstructions = [...finalCleanupInstructions]; + + // create approval for transfer transactions const transferAuthority = approve( instructions, cleanupInstructions, fromAccount, wallet.publicKey, - fromLamports + fromLamports, + false, ); signers.push(transferAuthority); @@ -231,7 +237,6 @@ export const borrow = async ( instructions.push( accrueInterestInstruction(depositReserve.pubkey, borrowReserve.pubkey) ); - // borrow instructions.push( borrowInstruction( diff --git a/src/actions/repay.tsx b/src/actions/repay.tsx index 1d5d80b..72cea28 100644 --- a/src/actions/repay.tsx +++ b/src/actions/repay.tsx @@ -105,7 +105,7 @@ export const repay = async ( obligationToken.pubkey, wallet.publicKey, obligationToken.info.amount.toNumber(), - + true, // reuse transfer authority transferAuthority.publicKey ); diff --git a/src/components/BorrowInput/index.tsx b/src/components/BorrowInput/index.tsx index 83dceba..1d03e5e 100644 --- a/src/components/BorrowInput/index.tsx +++ b/src/components/BorrowInput/index.tsx @@ -225,7 +225,7 @@ export const BorrowInput = (props: { disabled={fromAccounts.length === 0} > {fromAccounts.length === 0 - ? LABELS.NO_DEPOSITS + ? LABELS.NO_COLLATERAL : LABELS.BORROW_ACTION} diff --git a/src/components/RepayInput/index.tsx b/src/components/RepayInput/index.tsx index f0b7538..315ee2f 100644 --- a/src/components/RepayInput/index.tsx +++ b/src/components/RepayInput/index.tsx @@ -110,19 +110,19 @@ export const RepayInput = (props: { } })(); }, [ - pct, - value, borrowAmount, borrowAmountLamports, - type, - connection, - wallet, - obligation, collateralReserve, - repayReserve, + connection, fromAccounts, + obligation, obligationAccount, + pct, + repayReserve, setValue, + type, + value, + wallet, ]); const collateralPrice = useMidPriceInUSD( @@ -142,11 +142,11 @@ export const RepayInput = (props: { } } }, [ - lastTyped, - collateralReserve, - obligation.info.collateralInQuote, - collateralPrice, borrowAmount, + collateralPrice, + collateralReserve, + lastTyped, + obligation.info.collateralInQuote, value, ]); @@ -164,12 +164,13 @@ export const RepayInput = (props: { } } }, [ - lastTyped, - collateralReserve, - obligation.info.collateralInQuote, - collateralPrice, borrowAmount, + collateralPrice, + collateralReserve, collateralValue, + lastTyped, + obligation.info.collateralInQuote, + setValue, ]); const bodyStyle: React.CSSProperties = { diff --git a/src/components/WithdrawInput/index.tsx b/src/components/WithdrawInput/index.tsx index 3b902db..b19779d 100644 --- a/src/components/WithdrawInput/index.tsx +++ b/src/components/WithdrawInput/index.tsx @@ -147,7 +147,7 @@ export const WithdrawInput = (props: { disabled={fromAccounts.length === 0} > {fromAccounts.length === 0 - ? LABELS.NO_DEPOSITS + ? LABELS.NO_COLLATERAL : LABELS.WITHDRAW_ACTION} diff --git a/src/constants/labels.ts b/src/constants/labels.ts index 70d58b1..009443f 100644 --- a/src/constants/labels.ts +++ b/src/constants/labels.ts @@ -37,7 +37,9 @@ export const LABELS = { COLLATERAL: "Collateral", BORROW_QUESTION: "How much would you like to borrow?", BORROW_ACTION: "Borrow", - NO_DEPOSITS: "No collateral", + NO_COLLATERAL: "No collateral", + NO_DEPOSITS: "No deposits", + NO_LOANS: "No loans", LIQUIDATE_ACTION: "Liquidate", LIQUIDATE_NO_LOANS: "There are no loans to liquidate.", TABLE_TITLE_ASSET: "Asset", diff --git a/src/contexts/accounts.tsx b/src/contexts/accounts.tsx index 7ab2659..26fe0a2 100644 --- a/src/contexts/accounts.tsx +++ b/src/contexts/accounts.tsx @@ -148,6 +148,10 @@ export const cache = { obj: AccountInfo, parser?: AccountParser ) => { + if(obj.data.length === 0) { + return; + } + const address = typeof id === "string" ? id : id?.toBase58(); const deserialize = parser ? parser : keyToAccountParser.get(address); if (!deserialize) { diff --git a/src/models/account.ts b/src/models/account.ts index 6c959b1..c9fa53d 100644 --- a/src/models/account.ts +++ b/src/models/account.ts @@ -20,6 +20,7 @@ export function approve( account: PublicKey, owner: PublicKey, amount: number, + autoRevoke = true, // if delegate is not passed ephemeral transfer authority is used delegate?: PublicKey @@ -38,9 +39,11 @@ export function approve( ) ); - cleanupInstructions.push( - Token.createRevokeInstruction(tokenProgram, account, owner, []) - ); + if(autoRevoke) { + cleanupInstructions.push( + Token.createRevokeInstruction(tokenProgram, account, owner, []) + ); + } return transferAuthority; } diff --git a/src/views/borrow/index.tsx b/src/views/borrow/index.tsx index 896ea34..e3f13a8 100644 --- a/src/views/borrow/index.tsx +++ b/src/views/borrow/index.tsx @@ -18,7 +18,7 @@ export const BorrowView = () => {
{reserveAccounts.map((account) => ( - + ))} diff --git a/src/views/dashboard/deposit/index.tsx b/src/views/dashboard/deposit/index.tsx index e765e81..cdb590d 100644 --- a/src/views/dashboard/deposit/index.tsx +++ b/src/views/dashboard/deposit/index.tsx @@ -33,7 +33,7 @@ export const DashboardDeposits = () => {
{userDeposits.map((deposit) => ( - + ))} ); diff --git a/src/views/dashboard/index.tsx b/src/views/dashboard/index.tsx index 4eb8b6f..bdac203 100644 --- a/src/views/dashboard/index.tsx +++ b/src/views/dashboard/index.tsx @@ -1,4 +1,4 @@ -import { Col, Row } from "antd"; +import { Card, Col, Row } from "antd"; import React from "react"; import { GUTTER, LABELS } from "../../constants"; import { useWallet } from "../../contexts/wallet"; @@ -14,7 +14,7 @@ export const DashboardView = () => { return (
- {!connected && ( + {!connected ? (
{ /> {LABELS.DASHBOARD_INFO}
- )} - {connected && userDeposits.length === 0 && userObligations.length === 0 && ( + ): + userDeposits.length === 0 && userObligations.length === 0 ? + (
{ /> {LABELS.NO_LOANS_NO_DEPOSITS}
- )} - {connected && ( + ): ( - {userDeposits.length > 0 && ( - - - - )} - {userObligations.length > 0 && ( - - - - )} + + {userDeposits.length > 0 ? + : + {LABELS.NO_DEPOSITS} } + + + {userObligations.length > 0 ? + : + {LABELS.NO_LOANS} } + )}
diff --git a/src/views/dashboard/obligation/index.tsx b/src/views/dashboard/obligation/index.tsx index f1320cc..3671df9 100644 --- a/src/views/dashboard/obligation/index.tsx +++ b/src/views/dashboard/obligation/index.tsx @@ -35,7 +35,9 @@ export const DashboardObligations = () => {
{userObligations.map((item) => { - return ; + return ; })} ); diff --git a/src/views/deposit/view/index.tsx b/src/views/deposit/view/index.tsx index 8271b02..63d746b 100644 --- a/src/views/deposit/view/index.tsx +++ b/src/views/deposit/view/index.tsx @@ -17,7 +17,7 @@ export const DepositView = () => {
{reserveAccounts.map((account) => ( - + ))} diff --git a/src/views/home/index.tsx b/src/views/home/index.tsx index 0991b7f..0c7ffc2 100644 --- a/src/views/home/index.tsx +++ b/src/views/home/index.tsx @@ -142,6 +142,7 @@ export const HomeView = () => { {reserveAccounts.map((account) => (