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 signers: Account[] = [];
let instructions: TransactionInstruction[] = []; let instructions: TransactionInstruction[] = [];
let cleanupInstructions: TransactionInstruction[] = []; let cleanupInstructions: TransactionInstruction[] = [];
let finalCleanupInstructions: TransactionInstruction[] = [];
const [authority] = await PublicKey.findProgramAddress( const [authority] = await PublicKey.findProgramAddress(
[depositReserve.info.lendingMarket.toBuffer()], [depositReserve.info.lendingMarket.toBuffer()],
@ -97,16 +98,6 @@ export const borrow = async (
signers signers
); );
let toAccount = await findOrCreateAccountByMint(
wallet.publicKey,
wallet.publicKey,
instructions,
cleanupInstructions,
accountRentExempt,
borrowReserve.info.liquidityMint,
signers
);
if (!obligationAccount) { if (!obligationAccount) {
instructions.push( instructions.push(
initObligationInstruction( initObligationInstruction(
@ -128,35 +119,14 @@ export const borrow = async (
wallet.publicKey, wallet.publicKey,
LEND_HOST_FEE_ADDRESS, LEND_HOST_FEE_ADDRESS,
instructions, instructions,
cleanupInstructions, [],
accountRentExempt, accountRentExempt,
depositReserve.info.collateralMint, depositReserve.info.collateralMint,
signers signers
) )
: undefined; : 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 amountLamports: number = 0;
let fromLamports: number = 0; let fromLamports: number = 0;
@ -184,22 +154,58 @@ export const borrow = async (
fromLamports = amountLamports; fromLamports = amountLamports;
} }
const fromAccount = ensureSplAccount( const fromAccount = ensureSplAccount(
instructions, instructions,
cleanupInstructions, finalCleanupInstructions,
from, from,
wallet.publicKey, wallet.publicKey,
fromLamports + accountRentExempt, fromLamports + accountRentExempt,
signers 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 // create approval for transfer transactions
const transferAuthority = approve( const transferAuthority = approve(
instructions, instructions,
cleanupInstructions, cleanupInstructions,
fromAccount, fromAccount,
wallet.publicKey, wallet.publicKey,
fromLamports fromLamports,
false,
); );
signers.push(transferAuthority); signers.push(transferAuthority);
@ -231,7 +237,6 @@ export const borrow = async (
instructions.push( instructions.push(
accrueInterestInstruction(depositReserve.pubkey, borrowReserve.pubkey) accrueInterestInstruction(depositReserve.pubkey, borrowReserve.pubkey)
); );
// borrow // borrow
instructions.push( instructions.push(
borrowInstruction( borrowInstruction(

View File

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

View File

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

View File

@ -110,19 +110,19 @@ export const RepayInput = (props: {
} }
})(); })();
}, [ }, [
pct,
value,
borrowAmount, borrowAmount,
borrowAmountLamports, borrowAmountLamports,
type,
connection,
wallet,
obligation,
collateralReserve, collateralReserve,
repayReserve, connection,
fromAccounts, fromAccounts,
obligation,
obligationAccount, obligationAccount,
pct,
repayReserve,
setValue, setValue,
type,
value,
wallet,
]); ]);
const collateralPrice = useMidPriceInUSD( const collateralPrice = useMidPriceInUSD(
@ -142,11 +142,11 @@ export const RepayInput = (props: {
} }
} }
}, [ }, [
lastTyped,
collateralReserve,
obligation.info.collateralInQuote,
collateralPrice,
borrowAmount, borrowAmount,
collateralPrice,
collateralReserve,
lastTyped,
obligation.info.collateralInQuote,
value, value,
]); ]);
@ -164,12 +164,13 @@ export const RepayInput = (props: {
} }
} }
}, [ }, [
lastTyped,
collateralReserve,
obligation.info.collateralInQuote,
collateralPrice,
borrowAmount, borrowAmount,
collateralPrice,
collateralReserve,
collateralValue, collateralValue,
lastTyped,
obligation.info.collateralInQuote,
setValue,
]); ]);
const bodyStyle: React.CSSProperties = { const bodyStyle: React.CSSProperties = {

View File

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

View File

@ -37,7 +37,9 @@ export const LABELS = {
COLLATERAL: "Collateral", COLLATERAL: "Collateral",
BORROW_QUESTION: "How much would you like to borrow?", BORROW_QUESTION: "How much would you like to borrow?",
BORROW_ACTION: "Borrow", BORROW_ACTION: "Borrow",
NO_DEPOSITS: "No collateral", NO_COLLATERAL: "No collateral",
NO_DEPOSITS: "No deposits",
NO_LOANS: "No loans",
LIQUIDATE_ACTION: "Liquidate", LIQUIDATE_ACTION: "Liquidate",
LIQUIDATE_NO_LOANS: "There are no loans to liquidate.", LIQUIDATE_NO_LOANS: "There are no loans to liquidate.",
TABLE_TITLE_ASSET: "Asset", TABLE_TITLE_ASSET: "Asset",

View File

@ -148,6 +148,10 @@ export const cache = {
obj: AccountInfo<Buffer>, obj: AccountInfo<Buffer>,
parser?: AccountParser parser?: AccountParser
) => { ) => {
if(obj.data.length === 0) {
return;
}
const address = typeof id === "string" ? id : id?.toBase58(); const address = typeof id === "string" ? id : id?.toBase58();
const deserialize = parser ? parser : keyToAccountParser.get(address); const deserialize = parser ? parser : keyToAccountParser.get(address);
if (!deserialize) { if (!deserialize) {

View File

@ -20,6 +20,7 @@ export function approve(
account: PublicKey, account: PublicKey,
owner: PublicKey, owner: PublicKey,
amount: number, amount: number,
autoRevoke = true,
// if delegate is not passed ephemeral transfer authority is used // if delegate is not passed ephemeral transfer authority is used
delegate?: PublicKey delegate?: PublicKey
@ -38,9 +39,11 @@ export function approve(
) )
); );
if(autoRevoke) {
cleanupInstructions.push( cleanupInstructions.push(
Token.createRevokeInstruction(tokenProgram, account, owner, []) Token.createRevokeInstruction(tokenProgram, account, owner, [])
); );
}
return transferAuthority; return transferAuthority;
} }

View File

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

View File

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

View File

@ -1,4 +1,4 @@
import { Col, Row } from "antd"; import { Card, Col, Row } from "antd";
import React from "react"; import React from "react";
import { GUTTER, LABELS } from "../../constants"; import { GUTTER, LABELS } from "../../constants";
import { useWallet } from "../../contexts/wallet"; import { useWallet } from "../../contexts/wallet";
@ -14,7 +14,7 @@ export const DashboardView = () => {
return ( return (
<div className="dashboard-container"> <div className="dashboard-container">
{!connected && ( {!connected ? (
<div className="dashboard-info"> <div className="dashboard-info">
<img <img
src="splash.svg" src="splash.svg"
@ -23,8 +23,9 @@ export const DashboardView = () => {
/> />
{LABELS.DASHBOARD_INFO} {LABELS.DASHBOARD_INFO}
</div> </div>
)} ):
{connected && userDeposits.length === 0 && userObligations.length === 0 && ( userDeposits.length === 0 && userObligations.length === 0 ?
(
<div className="dashboard-info"> <div className="dashboard-info">
<img <img
src="splash.svg" src="splash.svg"
@ -33,19 +34,18 @@ export const DashboardView = () => {
/> />
{LABELS.NO_LOANS_NO_DEPOSITS} {LABELS.NO_LOANS_NO_DEPOSITS}
</div> </div>
)} ): (
{connected && (
<Row gutter={GUTTER}> <Row gutter={GUTTER}>
{userDeposits.length > 0 && (
<Col md={24} xl={12} span={24}> <Col md={24} xl={12} span={24}>
<DashboardDeposits /> {userDeposits.length > 0 ?
<DashboardDeposits /> :
<Card>{LABELS.NO_DEPOSITS}</Card> }
</Col> </Col>
)}
{userObligations.length > 0 && (
<Col md={24} xl={12} span={24}> <Col md={24} xl={12} span={24}>
<DashboardObligations /> {userObligations.length > 0 ?
<DashboardObligations /> :
<Card>{LABELS.NO_LOANS}</Card> }
</Col> </Col>
)}
</Row> </Row>
)} )}
</div> </div>

View File

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

View File

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

View File

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