From 6204d4875b13a165558e7c8c195d2dd4c8d8d6f8 Mon Sep 17 00:00:00 2001 From: Mohan Date: Mon, 4 Nov 2024 13:29:09 +0530 Subject: [PATCH] (launch) fix: Rewards per second (#127) * feat: change from InputNumber to Input * feat: allow only numbers --- .../MyStakingContracts/Create/index.tsx | 19 ++++++++---- .../util-functions/src/lib/formValidations.ts | 30 +++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/apps/launch/components/MyStakingContracts/Create/index.tsx b/apps/launch/components/MyStakingContracts/Create/index.tsx index 9e693597..de0f6bb5 100644 --- a/apps/launch/components/MyStakingContracts/Create/index.tsx +++ b/apps/launch/components/MyStakingContracts/Create/index.tsx @@ -19,7 +19,7 @@ import { mainnet } from 'viem/chains'; import { useAccount } from 'wagmi'; import { CHAIN_NAMES, EXPLORER_URLS, UNICODE_SYMBOLS } from 'libs/util-constants/src'; -import { notifyWarning } from 'libs/util-functions/src'; +import { allowOnlyNumbers, notifyWarning } from 'libs/util-functions/src'; import { ErrorAlert } from 'common-util/ErrorAlert'; import { @@ -145,7 +145,10 @@ export const CreateStakingContract = () => { } = values; try { - const metadataHash = await getIpfsHash({ name: contractName, description }); + const metadataHash = await getIpfsHash({ + name: contractName, + description, + }); const implementation = STAKING_TOKEN_ADDRESSES[chain.id]; const initPayload = getStakingContractInitPayload({ @@ -170,7 +173,11 @@ export const CreateStakingContract = () => { throw new Error('Validation failed'); } - const result = await createStakingContract({ implementation, initPayload, account }); + const result = await createStakingContract({ + implementation, + initPayload, + account, + }); if (result) { const eventLog = result.events?.InstanceCreated?.returnValues; @@ -189,7 +196,7 @@ export const CreateStakingContract = () => { router.push(`/${networkName}/${URL.myStakingContracts}`); } } catch (error) { - console.log(error); + console.error(error); const { message, transactionHash } = getErrorInfo(Feature.CREATE, error as Error); setError({ message, transactionHash }); @@ -254,10 +261,10 @@ export const CreateStakingContract = () => { name="rewardsPerSecond" rules={rulesConfig.rewardsPerSecond.rules} > - diff --git a/libs/util-functions/src/lib/formValidations.ts b/libs/util-functions/src/lib/formValidations.ts index 65e66696..a13420fa 100644 --- a/libs/util-functions/src/lib/formValidations.ts +++ b/libs/util-functions/src/lib/formValidations.ts @@ -37,3 +37,33 @@ export const FORM_VALIDATION: { }, }, }; + +export const allowOnlyNumbers = (e: React.KeyboardEvent) => { + const isCopy = (e.ctrlKey || e.metaKey) && e.key === 'c'; + const isPaste = (e.ctrlKey || e.metaKey) && e.key === 'v'; + const isCut = (e.ctrlKey || e.metaKey) && e.key === 'x'; + const isSelectAll = (e.ctrlKey || e.metaKey) && e.key === 'a'; + + if ( + !/[0-9]/.test(e.key) && + e.key !== 'Backspace' && + e.key !== 'Delete' && + e.key !== 'ArrowLeft' && + e.key !== 'ArrowRight' && + e.key !== 'Tab' && + e.key !== 'Control' && + e.key !== '.' && + !isCopy && + !isPaste && + !isCut && + !isSelectAll + ) { + e.preventDefault(); + } + + // Prevent more than one decimal point + const value = e.currentTarget.value; + if (e.key === '.' && value.includes('.')) { + e.preventDefault(); + } +};