-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(swap): price impact calculation, beta tag (#2273)
Co-authored-by: tomiir <rocchitomas@gmail.com> Co-authored-by: Sven <38101365+svenvoskamp@users.noreply.github.com> Co-authored-by: Sven <fr.sven.fr@gmail.com>
- Loading branch information
1 parent
6476885
commit 3effe36
Showing
21 changed files
with
433 additions
and
265 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
export const InputUtil = { | ||
/** | ||
* Custom key down event optimized for numeric inputs which is used on the swap | ||
* @param event | ||
* @param value | ||
* @param onChange | ||
*/ | ||
numericInputKeyDown( | ||
event: KeyboardEvent, | ||
currentValue: string | undefined, | ||
onChange: (value: string) => void | ||
) { | ||
const allowedKeys = [ | ||
'Backspace', | ||
'Meta', | ||
'Ctrl', | ||
'a', | ||
'A', | ||
'c', | ||
'C', | ||
'x', | ||
'X', | ||
'v', | ||
'V', | ||
'ArrowLeft', | ||
'ArrowRight', | ||
'Tab' | ||
] | ||
const controlPressed = event.metaKey || event.ctrlKey | ||
const selectAll = event.key === 'a' || event.key === 'A' | ||
const copyKey = event.key === 'c' || event.key === 'C' | ||
const pasteKey = event.key === 'v' || event.key === 'V' | ||
const cutKey = event.key === 'x' || event.key === 'X' | ||
|
||
const isComma = event.key === ',' | ||
const isDot = event.key === '.' | ||
const isNumericKey = event.key >= '0' && event.key <= '9' | ||
|
||
// If command/ctrl key is not pressed, doesn't allow for a, c, v | ||
if (!controlPressed && (selectAll || copyKey || pasteKey || cutKey)) { | ||
event.preventDefault() | ||
} | ||
|
||
// If current value is zero, and zero is pressed, prevent the zero from being added again | ||
if (currentValue === '0' && !isComma && !isDot && event.key === '0') { | ||
event.preventDefault() | ||
} | ||
|
||
// If current value is zero and any numeric key is pressed, replace the zero with the number | ||
if (currentValue === '0' && isNumericKey) { | ||
onChange(event.key) | ||
event.preventDefault() | ||
} | ||
|
||
if (isComma || isDot) { | ||
// If the first character is a dot or comma, add a zero before it | ||
if (!currentValue) { | ||
onChange('0.') | ||
event.preventDefault() | ||
} | ||
|
||
// If the current value already has a dot or comma, prevent the new one from being added | ||
if (currentValue?.includes('.') || currentValue?.includes(',')) { | ||
event.preventDefault() | ||
} | ||
} | ||
|
||
// If the character is not allowed and it's not a dot or comma, prevent it | ||
if (!isNumericKey && !allowedKeys.includes(event.key) && !isDot && !isComma) { | ||
event.preventDefault() | ||
} | ||
} | ||
} |
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
Oops, something went wrong.