Skip to content

Commit 8ffc8d2

Browse files
committed
feat(web): pnk allowance and stake
1 parent 9ea3247 commit 8ffc8d2

File tree

3 files changed

+103
-35
lines changed

3 files changed

+103
-35
lines changed

web/src/hooks/queries/usePNKAllowance.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import { CONTRACTS } from "utils/getContract";
66
export const usePNKAllowance = (user?: string | null) => {
77
const pnkContract = useConnectedContract("PNK") as PNK;
88
return useSWR(
9-
() => (pnkContract && user ? `PNKBalance${user}` : false),
9+
() => (pnkContract && user ? `PNKAllowance{user}` : false),
1010
async () => {
11-
console.log("allowance query");
1211
if (pnkContract && user) {
13-
return await pnkContract.allowance(user, CONTRACTS["PNK"].address);
12+
return await pnkContract.allowance(
13+
user,
14+
CONTRACTS["KlerosCore"].address
15+
);
1416
} else {
1517
return undefined;
1618
}

web/src/hooks/queries/usePNKBalance.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ export const usePNKBalance = (user?: string | null) => {
77
return useSWR(
88
() => (pnkContract && user ? `PNKBalance${user}` : false),
99
async () => {
10-
console.log("balance query");
1110
if (pnkContract && user) {
12-
return await pnkContract.balanceOf(user);
11+
const balance = await pnkContract.balanceOf(user);
12+
return balance;
1313
} else {
1414
return undefined;
1515
}

web/src/pages/Courts/CourtDetails/StakeModal.tsx

Lines changed: 96 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import React, { useState } from "react";
1+
import React, { useState, useMemo } from "react";
22
import styled from "styled-components";
33
import { useParams } from "react-router-dom";
44
import Modal from "react-modal";
55
import { utils, BigNumber } from "ethers";
66
import { Field, Button } from "@kleros/ui-components-library";
7+
import { PNK } from "@kleros/kleros-v2-contracts/typechain-types/src/arbitration/mock/PNK";
78
import { KlerosCore } from "@kleros/kleros-v2-contracts/typechain-types/src/arbitration/KlerosCore";
89
import { useWeb3 } from "hooks/useWeb3";
910
import { useConnectedContract } from "hooks/useConnectedContract";
@@ -14,12 +15,13 @@ const StakeModal: React.FC<{ isOpen: boolean; close: () => void }> = ({
1415
isOpen,
1516
close,
1617
}) => {
17-
const { id } = useParams();
18-
const klerosCore = useConnectedContract("KlerosCore") as KlerosCore;
1918
const [isSending, setIsSending] = useState(false);
2019
const [amount, setAmount] = useState("");
20+
const parsedAmount = useMemo(
21+
() => (amount === "" ? BigNumber.from(0) : utils.parseUnits(amount, 18)),
22+
[amount]
23+
);
2124
const { account } = useWeb3();
22-
const { data: balance } = usePNKBalance(account);
2325
const { data: allowance } = usePNKAllowance(account);
2426
return (
2527
<StyledModal {...{ isOpen }}>
@@ -39,37 +41,101 @@ const StakeModal: React.FC<{ isOpen: boolean; close: () => void }> = ({
3941
text="Return"
4042
onClick={close}
4143
/>
42-
<Button
43-
text={"Stake"}
44-
isLoading={isSending}
45-
disabled={
46-
isSending ||
47-
allowance !== "" ||
48-
!balance ||
49-
utils.parseUnits(amount, 18).gt(balance)
50-
}
51-
onClick={() => {
52-
if (typeof id !== "undefined" && amount !== "") {
53-
setIsSending(true);
54-
klerosCore
55-
.setStake(utils.parseUnits(amount, 18), id)
56-
.then(
57-
async (tx) =>
58-
await tx.wait().then(() => {
59-
setAmount("");
60-
close();
61-
})
62-
)
63-
.catch()
64-
.finally(() => setIsSending(false));
65-
}
66-
}}
67-
/>
44+
{allowance && allowance.lt(parsedAmount) ? (
45+
<AllowanceButton {...{ parsedAmount, isSending, setIsSending }} />
46+
) : (
47+
<StakeButton
48+
{...{ parsedAmount, setAmount, isSending, setIsSending, close }}
49+
/>
50+
)}
6851
</ButtonArea>
6952
</StyledModal>
7053
);
7154
};
7255

56+
interface IAllowanceButton {
57+
parsedAmount: BigNumber;
58+
isSending: boolean;
59+
setIsSending: (arg0: boolean) => void;
60+
}
61+
62+
const AllowanceButton: React.FC<IAllowanceButton> = ({
63+
parsedAmount,
64+
isSending,
65+
setIsSending,
66+
}) => {
67+
const { id } = useParams();
68+
const { account } = useWeb3();
69+
const { data: allowance } = usePNKAllowance(account);
70+
const { data: balance } = usePNKBalance(account);
71+
const klerosCore = useConnectedContract("KlerosCore") as KlerosCore;
72+
const pnk = useConnectedContract("PNK") as PNK;
73+
return (
74+
<Button
75+
text={"Allow PNK"}
76+
isLoading={isSending}
77+
disabled={
78+
!balance ||
79+
isSending ||
80+
parsedAmount.eq(BigNumber.from(0)) ||
81+
parsedAmount.gt(balance)
82+
}
83+
onClick={() => {
84+
setIsSending(true);
85+
pnk
86+
.increaseAllowance(klerosCore.address, parsedAmount.sub(allowance!))
87+
.then(
88+
async (tx) =>
89+
await tx.wait().then(() => {
90+
console.log("nice!");
91+
})
92+
)
93+
.catch()
94+
.finally(() => setIsSending(false));
95+
}}
96+
/>
97+
);
98+
};
99+
100+
interface IStakeButton extends IAllowanceButton {
101+
setAmount: (arg0: string) => void;
102+
close: () => void;
103+
}
104+
105+
const StakeButton: React.FC<IStakeButton> = ({
106+
parsedAmount,
107+
setAmount,
108+
isSending,
109+
setIsSending,
110+
close,
111+
}) => {
112+
const { id } = useParams();
113+
const klerosCore = useConnectedContract("KlerosCore") as KlerosCore;
114+
return (
115+
<Button
116+
text={"Stake"}
117+
isLoading={isSending}
118+
disabled={isSending || parsedAmount.lte(BigNumber.from(0))}
119+
onClick={() => {
120+
if (typeof id !== "undefined") {
121+
setIsSending(true);
122+
klerosCore
123+
.setStake(id, parsedAmount)
124+
.then(
125+
async (tx) =>
126+
await tx.wait().then(() => {
127+
setAmount("");
128+
close();
129+
})
130+
)
131+
.catch()
132+
.finally(() => setIsSending(false));
133+
}
134+
}}
135+
/>
136+
);
137+
};
138+
73139
const StyledModal = styled(Modal)`
74140
position: absolute;
75141
top: 50%;

0 commit comments

Comments
 (0)