feat: add repay, withdraw confirmation

This commit is contained in:
bartosz-lipinski 2020-11-26 20:31:10 -06:00
parent f5637f85a1
commit 1dc3274a0a
4 changed files with 66 additions and 19 deletions

View File

@ -87,6 +87,7 @@ export const BorrowInput = (props: {
: undefined : undefined
); );
setValue("");
setShowConfirmation(true); setShowConfirmation(true);
} catch { } catch {
// TODO: // TODO:

View File

@ -46,6 +46,7 @@ export const DepositInput = (props: {
wallet wallet
); );
setValue("");
setShowConfirmation(true); setShowConfirmation(true);
} catch { } catch {
// TODO: // TODO:

View File

@ -6,7 +6,7 @@ import {
LendingReserveParser, LendingReserveParser,
} from "../../models"; } from "../../models";
import { TokenIcon } from "../TokenIcon"; import { TokenIcon } from "../TokenIcon";
import { Button, Card, Slider } from "antd"; import { Button, Card, Slider, Spin } from "antd";
import { cache, ParsedAccount, useMint } from "../../contexts/accounts"; import { cache, ParsedAccount, useMint } from "../../contexts/accounts";
import { NumericInput } from "../Input/numeric"; import { NumericInput } from "../Input/numeric";
import { useConnection } from "../../contexts/connection"; import { useConnection } from "../../contexts/connection";
@ -21,6 +21,11 @@ import {
toLamports, toLamports,
} from "../../utils/utils"; } from "../../utils/utils";
import { LABELS } from "../../constants"; import { LABELS } from "../../constants";
import { LoadingOutlined } from "@ant-design/icons";
import { ActionConfirmation} from './../ActionConfirmation';
import { BackButton } from "./../BackButton";
const antIcon = <LoadingOutlined style={{ fontSize: 24 }} spin />;
export const RepayInput = (props: { export const RepayInput = (props: {
className?: string; className?: string;
@ -30,6 +35,8 @@ export const RepayInput = (props: {
const connection = useConnection(); const connection = useConnection();
const { wallet } = useWallet(); const { wallet } = useWallet();
const [value, setValue] = useState(""); const [value, setValue] = useState("");
const [pendingTx, setPendingTx] = useState(false);
const [showConfirmation, setShowConfirmation] = useState(false);
const repayReserve = props.reserve; const repayReserve = props.reserve;
const obligation = props.obligation; const obligation = props.obligation;
@ -74,7 +81,12 @@ export const RepayInput = (props: {
return; return;
} }
repay( setPendingTx(true);
(async () => {
try {
await repay(
fromAccounts[0], fromAccounts[0],
lamports, lamports,
obligation, obligation,
@ -84,6 +96,15 @@ export const RepayInput = (props: {
connection, connection,
wallet wallet
); );
setValue("");
setShowConfirmation(true);
} catch {
// TODO:
}finally {
setPendingTx(false);
}
})();
}, [ }, [
lamports, lamports,
connection, connection,
@ -105,6 +126,7 @@ export const RepayInput = (props: {
return ( return (
<Card className={props.className} bodyStyle={bodyStyle}> <Card className={props.className} bodyStyle={bodyStyle}>
{showConfirmation ? <ActionConfirmation onClose={() => setShowConfirmation(false)} /> :
<div <div
style={{ style={{
display: "flex", display: "flex",
@ -134,7 +156,8 @@ export const RepayInput = (props: {
/> />
<div>{name}</div> <div>{name}</div>
</div> </div>
<Slider {/* TODO: finish slider implementation */}
{/* <Slider
marks={marks} marks={marks}
value={mark} value={mark}
onChange={(val: number) => onChange={(val: number) =>
@ -149,7 +172,7 @@ export const RepayInput = (props: {
).toFixed(2) ).toFixed(2)
) )
} }
/> /> */}
<div className="repay-input-title">{LABELS.SELECT_COLLATERAL}</div> <div className="repay-input-title">{LABELS.SELECT_COLLATERAL}</div>
<CollateralSelector <CollateralSelector
reserve={repayReserve.info} reserve={repayReserve.info}
@ -163,8 +186,9 @@ export const RepayInput = (props: {
disabled={fromAccounts.length === 0} disabled={fromAccounts.length === 0}
> >
{LABELS.REPAY_ACTION} {LABELS.REPAY_ACTION}
{pendingTx && <Spin indicator={antIcon} className="action-spinner" />}
</Button> </Button>
</div> </div>}
</Card> </Card>
); );
}; };

View File

@ -6,7 +6,7 @@ import {
} from "../../hooks"; } from "../../hooks";
import { LendingReserve } from "../../models/lending"; import { LendingReserve } from "../../models/lending";
import { TokenIcon } from "../TokenIcon"; import { TokenIcon } from "../TokenIcon";
import { Button, Card } from "antd"; import { Button, Card, Spin } from "antd";
import { NumericInput } from "../Input/numeric"; import { NumericInput } from "../Input/numeric";
import { useConnection } from "../../contexts/connection"; import { useConnection } from "../../contexts/connection";
import { useWallet } from "../../contexts/wallet"; import { useWallet } from "../../contexts/wallet";
@ -14,6 +14,10 @@ import { withdraw } from "../../actions";
import { PublicKey } from "@solana/web3.js"; import { PublicKey } from "@solana/web3.js";
import "./style.less"; import "./style.less";
import { LABELS } from "../../constants"; import { LABELS } from "../../constants";
import { LoadingOutlined } from "@ant-design/icons";
import { ActionConfirmation} from './../ActionConfirmation';
const antIcon = <LoadingOutlined style={{ fontSize: 24 }} spin />;
export const WithdrawInput = (props: { export const WithdrawInput = (props: {
className?: string; className?: string;
@ -23,6 +27,8 @@ export const WithdrawInput = (props: {
const connection = useConnection(); const connection = useConnection();
const { wallet } = useWallet(); const { wallet } = useWallet();
const [value, setValue] = useState(""); const [value, setValue] = useState("");
const [pendingTx, setPendingTx] = useState(false);
const [showConfirmation, setShowConfirmation] = useState(false);
const reserve = props.reserve; const reserve = props.reserve;
const address = props.address; const address = props.address;
@ -37,7 +43,11 @@ export const WithdrawInput = (props: {
); );
const onWithdraw = useCallback(() => { const onWithdraw = useCallback(() => {
withdraw( setPendingTx(true);
(async () => {
try {
await withdraw(
fromAccounts[0], fromAccounts[0],
Math.ceil( Math.ceil(
collateralBalanceLamports * collateralBalanceLamports *
@ -48,6 +58,15 @@ export const WithdrawInput = (props: {
connection, connection,
wallet wallet
); );
setValue("");
setShowConfirmation(true);
} catch {
// TODO:
} finally {
setPendingTx(false);
}
})();
}, [ }, [
connection, connection,
wallet, wallet,
@ -69,6 +88,7 @@ export const WithdrawInput = (props: {
return ( return (
<Card className={props.className} bodyStyle={bodyStyle}> <Card className={props.className} bodyStyle={bodyStyle}>
{showConfirmation ? <ActionConfirmation onClose={() => setShowConfirmation(false)} /> :
<div <div
style={{ style={{
display: "flex", display: "flex",
@ -99,11 +119,12 @@ export const WithdrawInput = (props: {
<Button <Button
type="primary" type="primary"
onClick={onWithdraw} onClick={onWithdraw}
disabled={fromAccounts.length === 0} disabled={fromAccounts.length === 0 || pendingTx}
> >
{LABELS.WITHDRAW_ACTION} {LABELS.WITHDRAW_ACTION}
{pendingTx && <Spin indicator={antIcon} className="action-spinner" />}
</Button> </Button>
</div> </div>}
</Card> </Card>
); );
}; };