Skip to content

Commit a2ed358

Browse files
authored
Merge pull request #1585 from kleros/fix/staking-input-behaviour
Fix/staking input behaviour
2 parents 5f0d234 + 713dd63 commit a2ed358

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

web/src/components/NumberInputField.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,19 @@ export const NumberInputField: React.FC<INumberInputField> = ({
5656
<Container {...{ className }}>
5757
{isEditing ? (
5858
<StyledField
59-
type="number"
59+
type="text"
60+
onInput={(e) => {
61+
const value = e.currentTarget.value.replace(/[^0-9.]/g, "");
62+
63+
e.currentTarget.value = formatter ? formatter(value) : value;
64+
return e;
65+
}}
6066
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
6167
onChange?.(event.target.value);
6268
}}
6369
onBlur={toggleEditing}
64-
{...{ value, placeholder, message, variant }}
70+
value={formatter ? formatter(value ?? "0") : value}
71+
{...{ placeholder, message, variant }}
6572
/>
6673
) : (
6774
<StyledField

web/src/pages/Courts/CourtDetails/StakePanel/InputDisplay.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useMemo } from "react";
1+
import React, { useState, useMemo, useEffect } from "react";
22
import styled from "styled-components";
33

44
import { useParams } from "react-router-dom";
@@ -68,11 +68,10 @@ const InputDisplay: React.FC<IInputDisplay> = ({
6868
setAmount,
6969
}) => {
7070
const [debouncedAmount, setDebouncedAmount] = useState("");
71+
const [errorMsg, setErrorMsg] = useState<string | undefined>();
7172
useDebounce(() => setDebouncedAmount(amount), 500, [amount]);
7273
const parsedAmount = useParsedAmount(uncommify(debouncedAmount) as `${number}`);
7374

74-
const [errorMsg, setErrorMsg] = useState<string | undefined>();
75-
7675
const { id } = useParams();
7776
const { address } = useAccount();
7877
const { data: balance } = usePnkBalanceOf({
@@ -89,6 +88,18 @@ const InputDisplay: React.FC<IInputDisplay> = ({
8988
const parsedStake = formatPNK(jurorBalance?.[2] || 0n, 0, true);
9089
const isStaking = useMemo(() => action === ActionType.stake, [action]);
9190

91+
useEffect(() => {
92+
if (parsedAmount > 0n && balance === 0n && isStaking) {
93+
setErrorMsg("You need a non-zero PNK balance to stake");
94+
} else if (isStaking && balance && parsedAmount > balance) {
95+
setErrorMsg("Insufficient balance to stake this amount");
96+
} else if (!isStaking && jurorBalance && parsedAmount > jurorBalance[2]) {
97+
setErrorMsg("Insufficient staked amount to withdraw this amount");
98+
} else {
99+
setErrorMsg(undefined);
100+
}
101+
}, [parsedAmount, isStaking, balance, jurorBalance]);
102+
92103
return (
93104
<>
94105
<LabelArea>
@@ -112,7 +123,7 @@ const InputDisplay: React.FC<IInputDisplay> = ({
112123
placeholder={isStaking ? "Amount to stake" : "Amount to withdraw"}
113124
message={errorMsg ?? undefined}
114125
variant={!isUndefined(errorMsg) ? "error" : "info"}
115-
formatter={(number: string) => commify(roundNumberDown(Number(number)))}
126+
formatter={(number: string) => (number !== "" ? commify(roundNumberDown(Number(number))) : "")}
116127
/>
117128
<EnsureChainContainer>
118129
<StakeWithdrawButton
@@ -123,7 +134,6 @@ const InputDisplay: React.FC<IInputDisplay> = ({
123134
isSending,
124135
setIsSending,
125136
setIsPopupOpen,
126-
setErrorMsg,
127137
}}
128138
/>
129139
</EnsureChainContainer>

web/src/pages/Courts/CourtDetails/StakePanel/StakeWithdrawButton.tsx

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useMemo, useEffect } from "react";
1+
import React, { useMemo } from "react";
22

33
import { useParams } from "react-router-dom";
44
import { useAccount, usePublicClient } from "wagmi";
@@ -34,7 +34,6 @@ interface IActionButton {
3434
setIsSending: (arg0: boolean) => void;
3535
setAmount: (arg0: string) => void;
3636
setIsPopupOpen: (arg0: boolean) => void;
37-
setErrorMsg: (arg0: string | undefined) => void;
3837
}
3938

4039
const StakeWithdrawButton: React.FC<IActionButton> = ({
@@ -43,7 +42,6 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({
4342
isSending,
4443
setIsSending,
4544
setIsPopupOpen,
46-
setErrorMsg,
4745
}) => {
4846
const { id } = useParams();
4947
const { address } = useAccount();
@@ -82,7 +80,7 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({
8280
return 0n;
8381
}, [jurorBalance, parsedAmount, isAllowance, isStaking]);
8482

85-
const { config: increaseAllowanceConfig, error: allowanceError } = usePreparePnkIncreaseAllowance({
83+
const { config: increaseAllowanceConfig } = usePreparePnkIncreaseAllowance({
8684
enabled: isAllowance && !isUndefined(klerosCore) && !isUndefined(targetStake) && !isUndefined(allowance),
8785
args: [klerosCore?.address, BigInt(targetStake ?? 0) - BigInt(allowance ?? 0)],
8886
});
@@ -99,7 +97,7 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({
9997
};
10098

10199
const { config: setStakeConfig, error: setStakeError } = usePrepareKlerosCoreSetStake({
102-
enabled: !isUndefined(targetStake) && !isUndefined(id) && !isAllowance,
100+
enabled: !isUndefined(targetStake) && !isUndefined(id) && !isAllowance && parsedAmount !== 0n,
103101
args: [BigInt(id ?? 0), targetStake],
104102
});
105103
const { writeAsync: setStake } = useKlerosCoreSetStake(setStakeConfig);
@@ -114,14 +112,6 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({
114112
}
115113
};
116114

117-
useEffect(() => {
118-
if (isAllowance) {
119-
setErrorMsg(allowanceError?.shortMessage);
120-
} else {
121-
setErrorMsg(setStakeError?.shortMessage);
122-
}
123-
}, [allowanceError, setStakeError, isAllowance, isStaking, setErrorMsg]);
124-
125115
const buttonProps = {
126116
[ActionType.allowance]: {
127117
text: "Allow PNK",
@@ -161,4 +151,4 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({
161151
);
162152
};
163153

164-
export default StakeWithdrawButton;
154+
export default StakeWithdrawButton;

0 commit comments

Comments
 (0)