1- import React , { useState } from "react" ;
1+ import React , { useState , useMemo } from "react" ;
22import styled from "styled-components" ;
33import { useParams } from "react-router-dom" ;
44import Modal from "react-modal" ;
55import { utils , BigNumber } from "ethers" ;
66import { Field , Button } from "@kleros/ui-components-library" ;
7+ import { PNK } from "@kleros/kleros-v2-contracts/typechain-types/src/arbitration/mock/PNK" ;
78import { KlerosCore } from "@kleros/kleros-v2-contracts/typechain-types/src/arbitration/KlerosCore" ;
89import { useWeb3 } from "hooks/useWeb3" ;
910import { 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+
73139const StyledModal = styled ( Modal ) `
74140 position: absolute;
75141 top: 50%;
0 commit comments