Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
Add code comments to the AmountInput HOC
Browse files Browse the repository at this point in the history
  • Loading branch information
elias-garcia committed Jul 14, 2021
1 parent 1ebe2b5 commit 81d8016
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/views/shared/amount-input/amount-input.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ function AmountInput (Component) {
}
}, [amount, showInFiat, isAmountMoreThanFunds, isAmountCompressedInvalid])

/**
* Converts an amount in tokens to fiat. It takes into account the prefered currency
* of the user.
* @param {BigNumber} tokensAmount - Amount to be converted to fiat
* @returns fiatAmount
*/
function convertAmountToFiat (tokensAmount) {
const fixedTokenAmount = getFixedTokenAmount(tokensAmount.toString(), account.token.decimals)

Expand All @@ -54,12 +60,24 @@ function AmountInput (Component) {
).toFixed(2)
}

/**
* Converts an amount in fiat to tokens.
* @param {Number} fiatAmount - Amount to be converted to tokens
* @returns
*/
function convertAmountToTokens (fiatAmount) {
const tokensAmount = fiatAmount / account.token.USD

return parseUnits(tokensAmount.toFixed(account.token.decimals))
return parseUnits(tokensAmount.toFixed(account.token.decimals), account.token.decimals)
}

/**
* Validates the new amount introduced by the user. It checks:
* 1. The amount is not negative.
* 2. The amount + fees doesn't exceed the account balance.
* 3. The amount is supported by Hermez.
* @param {BigNumber} newAmount - New amount to be checked.
*/
function checkAmountValidity (newAmount) {
const fee = getTransactionFee(transactionType, newAmount, account.token, l2Fee, gasPrice)
const newAmountWithFee = newAmount.add(fee)
Expand All @@ -72,6 +90,12 @@ function AmountInput (Component) {
}
}

/**
* Handles input change events. It's going to check that the input value is a valid
* amount and calculate both the tokens and fiat amounts for a value. It will also
* trigger the validation checks.
* @param {InputEvent} event - Input event
*/
function handleInputChange (event) {
if (INPUT_REGEX.test(event.target.value)) {
if (showInFiat) {
Expand All @@ -96,6 +120,11 @@ function AmountInput (Component) {
}
}

/**
* Handles the "Max" button click. It will calculate the max possible amount that a
* user can send in a transaction based on the account balance. It also takes the fee
* into account (if applicable).
*/
function handleSendAll () {
const maxPossibleAmount = BigNumber.from(account.balance)
const maxAmountWithoutFee = getMaxTxAmount(transactionType, maxPossibleAmount, account.token, l2Fee, gasPrice)
Expand All @@ -113,6 +142,11 @@ function AmountInput (Component) {
checkAmountValidity(maxAmountWithoutFee)
}

/**
* Handles the change between tokens and fiat. It will update the value of the input
* with the appropiate value (fiat if the previous value was tokens or tokens if the
* previous value was fiat).
*/
function handleSwapCurrency () {
const newValue = showInFiat
? getFixedTokenAmount(amount.tokens, account.token.decimals)
Expand Down

0 comments on commit 81d8016

Please sign in to comment.