Skip to content

Commit

Permalink
Merge pull request #201 from CarmineOptions/fix/crm-staking-input-rou…
Browse files Browse the repository at this point in the history
…nding

fix: handle input as bigint
  • Loading branch information
DaveVodrazka authored Jul 1, 2024
2 parents 233d8cb + 0299ac6 commit f649627
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/components/CarmineStaking/StakeCRM.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const stake = async (

export const StakeCrm = ({ account, carmBalance }: Props) => {
const [inputValue, setInputValue] = useState("");
const [amount, setSelectedAmount] = useState(0n);

const [monthState, setMonthState] = useState(TransactionState.Initial);
const [sixMonthsState, setSixMonthsState] = useState(
Expand All @@ -93,7 +94,6 @@ export const StakeCrm = ({ account, carmBalance }: Props) => {
const [yearState, setYearState] = useState(TransactionState.Initial);

const numCarmBalance = shortInteger(carmBalance, 18);
const amount = longInteger(parseFloat(inputValue), 18);

const handle1month = () => {
setSixMonthsState(TransactionState.Processing);
Expand Down Expand Up @@ -128,16 +128,21 @@ export const StakeCrm = ({ account, carmBalance }: Props) => {
value === "" || /^\d*\.?\d{0,6}$/.test(value) ? value : inputValue;

const num = parseFloat(numericValue);
const long = longInteger(num, 18);

if (num && num > numCarmBalance) {
if (long > carmBalance) {
// cannot set more than holds
return;
}

setInputValue(numericValue);
setSelectedAmount(long);
};

const handleAll = () => setInputValue(numCarmBalance.toString(10));
const handleAll = () => {
setInputValue(numCarmBalance.toString(10));
setSelectedAmount(carmBalance);
};

return (
<div>
Expand Down
12 changes: 8 additions & 4 deletions src/components/CarmineStaking/StakingModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,14 @@ export const stateToClassName = (state: TransactionState) => {
export const StakingModal = ({ account, amount, open, setOpen }: Props) => {
const numCarmBalance = shortInteger(amount, 18);
const [inputValue, setInputValue] = useState(numCarmBalance.toString(10));
const [selectedAmount, setSelectedAmount] = useState(amount);
const [unstakeState, setUnstakeState] = useState(TransactionState.Initial);
const [monthState, setMonthState] = useState(TransactionState.Initial);
const [sixMonthsState, setSixMonthsState] = useState(
TransactionState.Initial
);
const [yearState, setYearState] = useState(TransactionState.Initial);

const selectedAmount = longInteger(parseFloat(inputValue), 18);

const handleClose = () => {
setOpen(false);
setUnstakeState(TransactionState.Initial);
Expand Down Expand Up @@ -171,16 +170,21 @@ export const StakingModal = ({ account, amount, open, setOpen }: Props) => {
value === "" || /^\d*\.?\d{0,6}$/.test(value) ? value : inputValue;

const num = parseFloat(numericValue);
const long = longInteger(num, 18);

if (num && num > numCarmBalance) {
if (long > amount) {
// cannot set more than holds
return;
}

setInputValue(numericValue);
setSelectedAmount(long);
};

const handleAll = () => setInputValue(numCarmBalance.toString(10));
const handleAll = () => {
setInputValue(numCarmBalance.toString(10));
setSelectedAmount(amount);
};

return (
<Dialog
Expand Down

0 comments on commit f649627

Please sign in to comment.