-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
062cadf
commit 1b1b9ab
Showing
4 changed files
with
43 additions
and
26 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 |
---|---|---|
@@ -1,8 +1 @@ | ||
export const BITCOIN_MIN_AMOUNT = "1000000" // 0.01 BTC | ||
|
||
export const ERRORS = { | ||
REQUIRED: "Required.", | ||
EXCEEDED_VALUE: "The amount exceeds your current balance.", | ||
INSUFFICIENT_VALUE: (minValue: string) => | ||
`The minimum amount must be at least ${minValue} BTC.`, | ||
} |
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,35 @@ | ||
import { BITCOIN_MIN_AMOUNT } from "../constants" | ||
import { formatSatoshiAmount } from "./numbers" | ||
|
||
const ERRORS = { | ||
REQUIRED: "Required.", | ||
EXCEEDED_VALUE: "The amount exceeds your current balance.", | ||
INSUFFICIENT_VALUE: (minValue: string) => | ||
`The minimum amount must be at least ${minValue} BTC.`, | ||
} | ||
|
||
export function getErrorsObj<T>(errors: { [key in keyof T]: string }) { | ||
return (Object.keys(errors) as Array<keyof T>).every((name) => !errors[name]) | ||
? {} | ||
: errors | ||
} | ||
|
||
export function validateTokenAmount( | ||
value: string, | ||
tokenBalance: string, | ||
): string | undefined { | ||
if (!value) return ERRORS.REQUIRED | ||
|
||
const valueInBI = BigInt(value) | ||
const maxValueInBI = BigInt(tokenBalance) | ||
const minValueInBI = BigInt(BITCOIN_MIN_AMOUNT) | ||
|
||
const isMaximumValueExceeded = valueInBI > maxValueInBI | ||
const isMinimumValueFulfilled = valueInBI >= minValueInBI | ||
|
||
if (isMaximumValueExceeded) return ERRORS.EXCEEDED_VALUE | ||
if (!isMinimumValueFulfilled) | ||
return ERRORS.INSUFFICIENT_VALUE(formatSatoshiAmount(BITCOIN_MIN_AMOUNT)) | ||
|
||
return undefined | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./numbers" | ||
export * from "./address" | ||
export * from "./forms" |