feat: query obligations

This commit is contained in:
bartosz-lipinski 2020-11-21 23:53:43 -06:00
parent ab975de0b8
commit e925116ade
11 changed files with 67 additions and 36 deletions

View File

@ -108,7 +108,7 @@ export const borrow = async (
cleanupInstructions = [];
const [authority] = await PublicKey.findProgramAddress(
[depositReserve.lendingMarket.toBuffer()],
[depositReserve.lendingMarket.toBuffer()],
LENDING_PROGRAM_ID
);

View File

@ -6,12 +6,8 @@ import {
} from "@solana/web3.js";
import { sendTransaction } from "../contexts/connection";
import { notify } from "../utils/notifications";
import {
LendingReserve,
} from "./../models/lending/reserve";
import {
repayInstruction,
} from "./../models/lending/repay";
import { LendingReserve } from "./../models/lending/reserve";
import { repayInstruction } from "./../models/lending/repay";
import { AccountLayout, Token } from "@solana/spl-token";
import { LENDING_PROGRAM_ID, TOKEN_PROGRAM_ID } from "../constants/ids";
import { findOrCreateAccountByMint } from "./account";
@ -49,7 +45,7 @@ export const repay = async (
);
const [authority] = await PublicKey.findProgramAddress(
[repayReserve.lendingMarket.toBuffer()],
[repayReserve.lendingMarket.toBuffer()],
LENDING_PROGRAM_ID
);

View File

@ -39,7 +39,7 @@ export const withdraw = async (
);
const [authority] = await PublicKey.findProgramAddress(
[reserve.lendingMarket.toBuffer()],
[reserve.lendingMarket.toBuffer()],
LENDING_PROGRAM_ID
);

View File

@ -1,6 +1,10 @@
import React, { useCallback, useMemo, useState } from "react";
import { useTokenName, useUserBalance } from "../../hooks";
import { LendingObligation, LendingReserve, LendingReserveParser } from "../../models";
import {
LendingObligation,
LendingReserve,
LendingReserveParser,
} from "../../models";
import { TokenIcon } from "../TokenIcon";
import { Button, Card } from "antd";
import { cache, ParsedAccount } from "../../contexts/accounts";
@ -15,7 +19,7 @@ import "./style.less";
export const RepayInput = (props: {
className?: string;
reserve: LendingReserve;
obligation: LendingObligation;
obligation?: string;
address: PublicKey;
}) => {
const connection = useConnection();
@ -24,7 +28,7 @@ export const RepayInput = (props: {
const repayReserve = props.reserve;
const repayReserveAddress = props.address;
const obligation = props.obligation;
const obligation = props.obligation;
const [collateralReserveMint, setCollateralReserveMint] = useState<string>();
@ -43,6 +47,13 @@ export const RepayInput = (props: {
);
// const collateralBalance = useUserBalance(reserve?.collateralMint);
const { userObligations } =
// TODO:
if(!obligation) {
}
const onReoay = useCallback(() => {
if (!collateralReserve) {
return;
@ -51,7 +62,7 @@ export const RepayInput = (props: {
repay(
fromAccounts[0],
parseFloat(value),
obligation,
obligation as any,
repayReserve,
repayReserveAddress,
collateralReserve.info,

View File

@ -53,11 +53,11 @@ export const useLending = () => {
item.account,
LendingMarketParser
);
}else if (isLendingObligation(item.account)) {
} else if (isLendingObligation(item.account)) {
return cache.add(
item.pubkey.toBase58(),
item.account,
LendingObligationParser,
LendingObligationParser
);
}
}, []);

View File

@ -6,3 +6,4 @@ export * from "./useUserBalance";
export * from "./useCollateralBalance";
export * from "./useLendingObligations";
export * from "./useUserObligations";
export * from "./useUserObligationByReserve";

View File

@ -0,0 +1,19 @@
import { useMemo } from "react";
import { useUserObligations } from "./useUserObligations";
import { PublicKey } from "@solana/web3.js";
export function useUserObligationByReserve(reserve: PublicKey) {
const { userObligations } = useUserObligations();
const userObligationsByReserve = useMemo(
() =>
userObligations.filter((item) =>
item.oblication.info.borrowReserve.equals(reserve)
),
[reserve, userObligations]
);
return {
userObligationsByReserve,
};
}

View File

@ -8,33 +8,33 @@ export function useUserObligations() {
const { obligations } = useLendingObligations();
const accountsByMint = useMemo(() => {
return userAccounts.reduce((res, acc) => {
const id = acc.info.mint.toBase58();
res.set(id, [...(res.get(id) || []), acc]);
return res;
}, new Map<string, TokenAccount[]>())
},
[userAccounts]);
return userAccounts.reduce((res, acc) => {
const id = acc.info.mint.toBase58();
res.set(id, [...(res.get(id) || []), acc]);
return res;
}, new Map<string, TokenAccount[]>());
}, [userAccounts]);
const userObligations = useMemo(() => {
if(accountsByMint.size === 0) {
if (accountsByMint.size === 0) {
return [];
}
return obligations
.filter((acc) => accountsByMint.get(acc.info.tokenMint.toBase58()) !== undefined)
.map(ob => {
.filter(
(acc) => accountsByMint.get(acc.info.tokenMint.toBase58()) !== undefined
)
.map((ob) => {
return {
oblication: ob,
userAccounts: [...accountsByMint.get(ob.info.tokenMint.toBase58())],
// TODO: add total borrowed amount?
}
};
});
}, [accountsByMint, obligations]);
return {
userObligations
userObligations,
};
}

View File

@ -7,8 +7,7 @@ export const DashboardView = () => {
return (
<div className="flexColumn">
DASHBOARD: TODO: 1. Add deposits 2. Add obligations
{userObligations.map(item => {
{userObligations.map((item) => {
return <div>{item?.oblication.info.borrowAmount.toString()}</div>;
})}
</div>

View File

@ -3,13 +3,21 @@ import { Button, Card } from "antd";
import { useConnection } from "../../contexts/connection";
import { useWallet } from "../../contexts/wallet";
import { LAMPORTS_PER_SOL } from "@solana/web3.js";
import { notify } from "../../utils/notifications";
export const FaucetView = () => {
const connection = useConnection();
const { wallet } = useWallet();
const airdrop = useCallback(() => {
connection.requestAirdrop(wallet.publicKey, 1 * LAMPORTS_PER_SOL);
connection
.requestAirdrop(wallet.publicKey, 2 * LAMPORTS_PER_SOL)
.then(() => {
notify({
message: "Account funded.",
type: "success",
});
});
}, [wallet, connection]);
const bodyStyle: React.CSSProperties = {

View File

@ -12,13 +12,10 @@ import "./style.less";
import { LendingObligation } from "../../models";
export const RepayReserveView = () => {
const { id } = useParams<{ id: string }>();
const { id, obligation } = useParams<{ id: string, obligation?: string }>();
const lendingReserve = useLendingReserve(id);
const reserve = lendingReserve?.info;
// TODO: query for lending obligation
const ob: LendingObligation = {} as any;
if (!reserve || !lendingReserve) {
return null;
}
@ -29,7 +26,7 @@ export const RepayReserveView = () => {
<RepayInput
className="repay-reserve-item repay-reserve-item-left"
reserve={reserve}
obligation={ob}
obligation={obligation}
address={lendingReserve.pubkey}
/>
<SideReserveOverview