fix: item keys

This commit is contained in:
bartosz-lipinski 2021-01-25 20:58:57 -06:00
parent dcb1e6c9a1
commit 135e61982f
14 changed files with 96 additions and 78 deletions

View File

@ -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(

View File

@ -105,7 +105,7 @@ export const repay = async (
obligationToken.pubkey,
wallet.publicKey,
obligationToken.info.amount.toNumber(),
true,
// reuse transfer authority
transferAuthority.publicKey
);

View File

@ -225,7 +225,7 @@ export const BorrowInput = (props: {
disabled={fromAccounts.length === 0}
>
{fromAccounts.length === 0
? LABELS.NO_DEPOSITS
? LABELS.NO_COLLATERAL
: LABELS.BORROW_ACTION}
</ConnectButton>
<BackButton />

View File

@ -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 = {

View File

@ -147,7 +147,7 @@ export const WithdrawInput = (props: {
disabled={fromAccounts.length === 0}
>
{fromAccounts.length === 0
? LABELS.NO_DEPOSITS
? LABELS.NO_COLLATERAL
: LABELS.WITHDRAW_ACTION}
</ConnectButton>
</div>

View File

@ -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",

View File

@ -148,6 +148,10 @@ export const cache = {
obj: AccountInfo<Buffer>,
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) {

View File

@ -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;
}

View File

@ -18,7 +18,7 @@ export const BorrowView = () => {
<div></div>
</div>
{reserveAccounts.map((account) => (
<BorrowItem reserve={account.info} address={account.pubkey} />
<BorrowItem key={account.pubkey.toBase58()} reserve={account.info} address={account.pubkey} />
))}
</Card>
</div>

View File

@ -33,7 +33,7 @@ export const DashboardDeposits = () => {
<div></div>
</div>
{userDeposits.map((deposit) => (
<DepositItem userDeposit={deposit} />
<DepositItem key={deposit.account.pubkey.toBase58()} userDeposit={deposit} />
))}
</Card>
);

View File

@ -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 (
<div className="dashboard-container">
{!connected && (
{!connected ? (
<div className="dashboard-info">
<img
src="splash.svg"
@ -23,8 +23,9 @@ export const DashboardView = () => {
/>
{LABELS.DASHBOARD_INFO}
</div>
)}
{connected && userDeposits.length === 0 && userObligations.length === 0 && (
):
userDeposits.length === 0 && userObligations.length === 0 ?
(
<div className="dashboard-info">
<img
src="splash.svg"
@ -33,19 +34,18 @@ export const DashboardView = () => {
/>
{LABELS.NO_LOANS_NO_DEPOSITS}
</div>
)}
{connected && (
): (
<Row gutter={GUTTER}>
{userDeposits.length > 0 && (
<Col md={24} xl={12} span={24}>
<DashboardDeposits />
</Col>
)}
{userObligations.length > 0 && (
<Col md={24} xl={12} span={24}>
<DashboardObligations />
</Col>
)}
<Col md={24} xl={12} span={24}>
{userDeposits.length > 0 ?
<DashboardDeposits /> :
<Card>{LABELS.NO_DEPOSITS}</Card> }
</Col>
<Col md={24} xl={12} span={24}>
{userObligations.length > 0 ?
<DashboardObligations /> :
<Card>{LABELS.NO_LOANS}</Card> }
</Col>
</Row>
)}
</div>

View File

@ -35,7 +35,9 @@ export const DashboardObligations = () => {
<div></div>
</div>
{userObligations.map((item) => {
return <ObligationItem obligation={item.obligation} />;
return <ObligationItem
key={item.obligation.account.pubkey.toBase58()}
obligation={item.obligation} />;
})}
</Card>
);

View File

@ -17,7 +17,7 @@ export const DepositView = () => {
<div></div>
</div>
{reserveAccounts.map((account) => (
<ReserveItem reserve={account.info} address={account.pubkey} />
<ReserveItem key={account.pubkey.toBase58()} reserve={account.info} address={account.pubkey} />
))}
</Card>
</div>

View File

@ -142,6 +142,7 @@ export const HomeView = () => {
</div>
{reserveAccounts.map((account) => (
<LendingReserveItem
key={account.pubkey.toBase58()}
reserve={account.info}
address={account.pubkey}
item={totals.items.find(