1
- import React , { useState } from "react" ;
1
+ import React , { useState , useMemo } from "react" ;
2
2
import styled from "styled-components" ;
3
3
import { useParams } from "react-router-dom" ;
4
4
import Modal from "react-modal" ;
5
5
import { utils , BigNumber } from "ethers" ;
6
6
import { Field , Button } from "@kleros/ui-components-library" ;
7
+ import { PNK } from "@kleros/kleros-v2-contracts/typechain-types/src/arbitration/mock/PNK" ;
7
8
import { KlerosCore } from "@kleros/kleros-v2-contracts/typechain-types/src/arbitration/KlerosCore" ;
8
9
import { useWeb3 } from "hooks/useWeb3" ;
9
10
import { useConnectedContract } from "hooks/useConnectedContract" ;
@@ -14,12 +15,13 @@ const StakeModal: React.FC<{ isOpen: boolean; close: () => void }> = ({
14
15
isOpen,
15
16
close,
16
17
} ) => {
17
- const { id } = useParams ( ) ;
18
- const klerosCore = useConnectedContract ( "KlerosCore" ) as KlerosCore ;
19
18
const [ isSending , setIsSending ] = useState ( false ) ;
20
19
const [ amount , setAmount ] = useState ( "" ) ;
20
+ const parsedAmount = useMemo (
21
+ ( ) => ( amount === "" ? BigNumber . from ( 0 ) : utils . parseUnits ( amount , 18 ) ) ,
22
+ [ amount ]
23
+ ) ;
21
24
const { account } = useWeb3 ( ) ;
22
- const { data : balance } = usePNKBalance ( account ) ;
23
25
const { data : allowance } = usePNKAllowance ( account ) ;
24
26
return (
25
27
< StyledModal { ...{ isOpen } } >
@@ -39,37 +41,101 @@ const StakeModal: React.FC<{ isOpen: boolean; close: () => void }> = ({
39
41
text = "Return"
40
42
onClick = { close }
41
43
/>
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
+ ) }
68
51
</ ButtonArea >
69
52
</ StyledModal >
70
53
) ;
71
54
} ;
72
55
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
+
73
139
const StyledModal = styled ( Modal ) `
74
140
position: absolute;
75
141
top: 50%;
0 commit comments