feat: update lending accounts

This commit is contained in:
bartosz-lipinski 2020-11-18 22:48:09 -06:00
parent 3a83cb9d67
commit 8eceeba1cb
2 changed files with 18 additions and 20 deletions

View File

@ -4,6 +4,7 @@ import { LENDING_PROGRAM_ID } from "./../constants/ids";
import { LendingReserveLayout, LendingMarketLayout, LendingMarket, LendingMarketParser, isLendingReserve, isLendingMarket, LendingReserveParser } from "./../models/lending";
import { cache, getMultipleAccounts } from "./accounts";
import { AccountInfo, PublicKey } from "@solana/web3.js";
import { isForInStatement } from "typescript";
export interface LendingContextState {
@ -32,19 +33,21 @@ export const useLending = () => {
const connection = useConnection();
const [lendingAccounts, setLendingAccounts] = useState<any[]>([]);
const processAccount = useCallback((item) => {
if (isLendingReserve(item.account)) {
return cache.add(item.pubkey.toBase58(), item.account, LendingReserveParser);
} else if (isLendingMarket(item.account)) {
return cache.add(item.pubkey.toBase58(), item.account, LendingMarketParser);
}
}, []);
// initial query
useEffect(() => {
setLendingAccounts([]);
const queryLendingAccounts = async () => {
const accounts = (await connection.getProgramAccounts(LENDING_PROGRAM_ID))
.map((item) => {
if (isLendingReserve(item.account)) {
return cache.add(item.pubkey.toBase58(), item.account, LendingReserveParser);
} else if (isLendingMarket(item.account)) {
return cache.add(item.pubkey.toBase58(), item.account, LendingMarketParser);
}
})
.map(processAccount)
.filter(item => item !== undefined);
console.log(accounts);
@ -86,16 +89,11 @@ export const useLending = () => {
LENDING_PROGRAM_ID,
async (info) => {
const id = (info.accountId as unknown) as string;
if (info.accountInfo.data.length === LendingReserveLayout.span) {
const account = info.accountInfo;
const updated = {
data: LendingReserveLayout.decode(account.data),
account: account,
pubkey: new PublicKey(id),
};
// TODO: update cache and raise events
}
const item = {
pubkey: new PublicKey(id),
account: info.accountInfo,
};
processAccount(item);
},
"singleGossip"
);

View File

@ -32,6 +32,8 @@ export const DepositAddView = () => {
console.log(`utlization: ${reserve.maxUtilizationRate}`)
console.log(`cumulativeBorrowRate: ${reserve.cumulativeBorrowRate.toString()}`)
console.log(`totalBorrows: ${reserve.totalBorrows.toString()}`)
console.log(`totalLiquidity: ${reserve.totalLiquidity.toString()}`)
console.log(`lendingMarket: ${reserve.lendingMarket.toBase58()}`);
const lendingMarket = await cache.get(reserve.lendingMarket);
@ -44,8 +46,6 @@ export const DepositAddView = () => {
})();
}, [lendingReserve])
console.log(fromAccounts);
const onDeposit = useCallback(() => {
if (!lendingReserve || !reserve) {
return;
@ -58,7 +58,7 @@ export const DepositAddView = () => {
lendingReserve.pubkey,
connection,
wallet);
}, [value, reserve, fromAccounts]);
}, [value, reserve, fromAccounts, lendingReserve]);
return <Card title={(
<h2 style={{ display: 'flex', alignItems: 'center', width: 400 }}>