-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: added additional validations to the WhaleInput (#534)
--------- Co-authored-by: nick134 <[email protected]>
- Loading branch information
1 parent
2d2577e
commit 5fce06a
Showing
3 changed files
with
69 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* General use validation function for when the user supplies a positive decimal value. | ||
* For example it is used with token and slippage amounts. | ||
* @param input The user supplied value | ||
* @returns Validated string that has invalid patterns removed | ||
*/ | ||
export const amountInputValidation = (input: string): string => { | ||
const amount = input. | ||
// Limiting the number of characters to be 32 | ||
slice(0, 32). | ||
// Disallowing leading decimal place and multiple zeros before decimal place | ||
replace(/^(?:\.|0{2,}\.)/u, '0.'). | ||
// Eliminating multiple leading zeros before the numbers between 1-9 | ||
replace(/^0+(?=[0-9])/u, '') | ||
// Ensuring multiple decimal points can't be used | ||
return input.indexOf('.') !== amount.lastIndexOf('.') ? | ||
amount.slice(0, amount.indexOf('.') + 1) + amount.slice(amount.indexOf('.')).replaceAll('.', '') | ||
: amount; | ||
} |