Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
[PAY-1794] Check correct balance in withdrawal flow (#4056)
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondjacobson authored Sep 8, 2023
1 parent 6827827 commit d548f80
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
20 changes: 18 additions & 2 deletions packages/common/src/utils/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export const formatUSDCWeiToUSDString = (amount: StringUSDC, precision = 2) => {
}

/**
* Formats a USDC BN (full precision) to a number suitable for display as a dollar amount.
* Formats a USDC BN (full precision) to a number of dollars.
* Note: will lose precision by rounding _up_ to nearest cent.
*/
export const formatUSDCWeiToCeilingDollarNumber = (amount: BNUSDC) => {
Expand All @@ -177,7 +177,23 @@ export const formatUSDCWeiToCeilingDollarNumber = (amount: BNUSDC) => {
}

/**
* Formats a USDC BN (full precision) to a number of cents
* Formats a USDC BN (full precision) to a number of cents.
* Note: will lose precision by rounding _up_ to nearest cent.
*/
export const formatUSDCWeiToCeilingCentsNumber = (amount: BNUSDC) => {
return ceilingBNUSDCToNearestCent(amount).div(BN_USDC_CENT_WEI).toNumber()
}

/**
* Formats a USDC BN (full precision) to a number of dollars.
* Note: will lose precision by rounding _down_ to nearest cent.
*/
export const formatUSDCWeiToFloorDollarNumber = (amount: BNUSDC) => {
return floorBNUSDCToNearestCent(amount).div(BN_USDC_CENT_WEI).toNumber() / 100
}

/**
* Formats a USDC BN (full precision) to a number of cents.
* Note: will lose precision by rounding _down_ to nearest cent.
*/
export const formatUSDCWeiToFloorCentsNumber = (amount: BNUSDC) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
BNUSDC,
useWithdrawUSDCModal,
WithdrawUSDCModalPages,
formatUSDCWeiToFloorCentsNumber
formatUSDCWeiToFloorDollarNumber
} from '@audius/common'
import {
HarmonyButton,
Expand Down Expand Up @@ -58,8 +58,9 @@ export const EnterTransferDetails = () => {
const { data: balance } = useUSDCBalance()
const { setData } = useWithdrawUSDCModal()

const balanceNumber =
formatUSDCWeiToFloorCentsNumber((balance ?? new BN(0)) as BNUSDC) / 100
const balanceNumber = formatUSDCWeiToFloorDollarNumber(
(balance ?? new BN(0)) as BNUSDC
)
const balanceFormatted = formatCurrencyBalance(balanceNumber)

const [
Expand All @@ -86,7 +87,7 @@ export const EnterTransferDetails = () => {
[setHumanizedValue, setAmountTouched]
)

const [, { error: addressError }] = useField(ADDRESS)
const [{ value: address }, { error: addressError }] = useField(ADDRESS)

const handleContinue = useCallback(() => {
setData({ page: WithdrawUSDCModalPages.CONFIRM_TRANSFER_DETAILS })
Expand Down Expand Up @@ -136,7 +137,7 @@ export const EnterTransferDetails = () => {
size={HarmonyButtonSize.DEFAULT}
fullWidth
text={messages.continue}
disabled={amountError || addressError}
disabled={amountError || addressError || !address}
onClick={handleContinue}
/>
<Hint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
useUSDCBalance,
formatCurrencyBalance,
BNUSDC,
formatUSDCWeiToFloorCentsNumber
formatUSDCWeiToFloorDollarNumber
} from '@audius/common'
import BN from 'bn.js'
import { useField } from 'formik'
Expand All @@ -27,8 +27,9 @@ const messages = {

export const TransferInProgress = () => {
const { data: balance } = useUSDCBalance()
const balanceNumber =
formatUSDCWeiToFloorCentsNumber((balance ?? new BN(0)) as BNUSDC) / 100
const balanceNumber = formatUSDCWeiToFloorDollarNumber(
(balance ?? new BN(0)) as BNUSDC
)
const balanceFormatted = formatCurrencyBalance(balanceNumber)

const [{ value: amountValue }] = useField(AMOUNT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
formatCurrencyBalance,
BNUSDC,
useWithdrawUSDCModal,
formatUSDCWeiToFloorCentsNumber
formatUSDCWeiToFloorDollarNumber
} from '@audius/common'
import {
HarmonyPlainButton,
Expand Down Expand Up @@ -46,8 +46,9 @@ const openExplorer = (signature: string) => {
export const TransferSuccessful = () => {
const { data: balance } = useUSDCBalance()
const { data: modalData } = useWithdrawUSDCModal()
const balanceNumber =
formatUSDCWeiToFloorCentsNumber((balance ?? new BN(0)) as BNUSDC) / 100
const balanceNumber = formatUSDCWeiToFloorDollarNumber(
(balance ?? new BN(0)) as BNUSDC
)
const balanceFormatted = formatCurrencyBalance(balanceNumber)

const [{ value: amountValue }] = useField(AMOUNT)
Expand Down
5 changes: 3 additions & 2 deletions packages/web/src/store/application/ui/withdraw-usdc/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
getContext,
TOKEN_LISTING_MAP,
getUserbankAccountInfo,
BNUSDC
BNUSDC,
formatUSDCWeiToFloorDollarNumber
} from '@audius/common'
import {
ASSOCIATED_TOKEN_PROGRAM_ID,
Expand Down Expand Up @@ -236,7 +237,7 @@ function* doWithdrawUSDC({
const latestBalance = (accountInfo?.amount ?? new BN(0)) as BNUSDC
withdrawalAmount = Math.min(
withdrawalAmount,
latestBalance.toNumber()
formatUSDCWeiToFloorDollarNumber(latestBalance)
)
}

Expand Down

0 comments on commit d548f80

Please sign in to comment.