Skip to content

Commit

Permalink
feat: query params for batched transfers (#1836)
Browse files Browse the repository at this point in the history
  • Loading branch information
brtkx authored Aug 15, 2024
1 parent bf67aa8 commit 941ae8f
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export function TransferPanel() {
// Both `amount` getter and setter will internally be using `useArbQueryParams` functions
const [{ amount }] = useArbQueryParams()

const setAmount = useSetInputAmount()
const { setAmount, setAmount2 } = useSetInputAmount()

const [tokenImportDialogProps] = useDialog()
const [tokenCheckDialogProps, openTokenCheckDialog] = useDialog()
Expand Down Expand Up @@ -200,6 +200,7 @@ export function TransferPanel() {
function clearAmountInput() {
// clear amount input on transfer panel
setAmount('')
setAmount2('')
}

useImportTokenModal({
Expand Down Expand Up @@ -1028,7 +1029,7 @@ export function TransferPanel() {
'sm:rounded sm:border'
)}
>
<TransferPanelMain amount={amount} errorMessage={errorMessage} />
<TransferPanelMain errorMessage={errorMessage} />
<AdvancedSettings />
<TransferPanelSummary
amount={parseFloat(amount)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,8 @@ export function NetworkListboxPlusBalancesContainer({
}

export function TransferPanelMain({
amount,
errorMessage
}: {
amount: string
errorMessage?: TransferReadinessRichErrorMessage | string
}) {
const [networks] = useNetworks()
Expand Down Expand Up @@ -356,7 +354,6 @@ export function TransferPanelMain({
return (
<div className="flex flex-col pb-6 lg:gap-y-1">
<SourceNetworkBox
amount={amount}
errorMessage={errorMessage}
customFeeTokenBalances={customFeeTokenBalances}
showUsdcSpecificInfo={showUSDCSpecificInfo}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,21 @@ import {
import { ExternalLink } from '../../common/ExternalLink'
import { EstimatedGas } from '../EstimatedGas'
import { TransferPanelMainInput } from '../TransferPanelMainInput'
import { AmountQueryParamEnum } from '../../../hooks/useArbQueryParams'
import {
AmountQueryParamEnum,
useArbQueryParams
} from '../../../hooks/useArbQueryParams'
import { TransferReadinessRichErrorMessage } from '../useTransferReadinessUtils'
import { useMaxAmount } from './useMaxAmount'
import { useSetInputAmount } from '../../../hooks/TransferPanel/useSetInputAmount'
import { isExperimentalFeatureEnabled } from '../../../util'
import { useDialog } from '../../common/Dialog'

export function SourceNetworkBox({
amount,
errorMessage,
customFeeTokenBalances,
showUsdcSpecificInfo
}: {
amount: string
errorMessage: string | TransferReadinessRichErrorMessage | undefined
customFeeTokenBalances: Balances
showUsdcSpecificInfo: boolean
Expand All @@ -57,7 +58,8 @@ export function SourceNetworkBox({
const { ethParentBalance, ethChildBalance } = useBalances()
const selectedTokenBalances = useSelectedTokenBalances()
const nativeCurrency = useNativeCurrency({ provider: childChainProvider })
const setAmount = useSetInputAmount()
const [{ amount, amount2 }] = useArbQueryParams()
const { setAmount, setAmount2 } = useSetInputAmount()
const { maxAmount } = useMaxAmount({
customFeeTokenBalances
})
Expand Down Expand Up @@ -145,6 +147,7 @@ export function SourceNetworkBox({
maxButtonOnClick={maxButtonOnClick}
errorMessage={errorMessage}
value={isMaxAmount ? '' : amount}
onChange={e => setAmount(e.target.value)}
/>

{isExperimentalFeatureEnabled('batch') &&
Expand All @@ -158,9 +161,8 @@ export function SourceNetworkBox({
// eslint-disable-next-line
maxButtonOnClick={() => {}}
errorMessage={undefined}
value={''}
// eslint-disable-next-line
onChange={() => {}}
value={amount2}
onChange={e => setAmount2(e.target.value)}
tokenButtonOptions={{
symbol: nativeCurrency.symbol,
disabled: true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { twMerge } from 'tailwind-merge'
import { useEffect, useMemo } from 'react'
import { useMemo } from 'react'

import { TokenButton, TokenButtonOptions } from './TokenButton'
import { useNetworks } from '../../hooks/useNetworks'
import { useNetworksRelationship } from '../../hooks/useNetworksRelationship'
import { useSelectedTokenBalances } from '../../hooks/TransferPanel/useSelectedTokenBalances'
import { useAppState } from '../../state'
import { useSetInputAmount } from '../../hooks/TransferPanel/useSetInputAmount'
import { countDecimals } from '../../util/NumberUtils'
import { useSelectedTokenDecimals } from '../../hooks/TransferPanel/useSelectedTokenDecimals'
import { useBalances } from '../../hooks/useBalances'
import { TransferReadinessRichErrorMessage } from './useTransferReadinessUtils'
import { ExternalLink } from '../common/ExternalLink'
Expand Down Expand Up @@ -74,17 +71,7 @@ function MaxButton(props: React.ButtonHTMLAttributes<HTMLButtonElement>) {
function TransferPanelInputField(
props: React.InputHTMLAttributes<HTMLInputElement>
) {
const { value = '', onChange, ...rest } = props
const setAmount = useSetInputAmount()
const decimals = useSelectedTokenDecimals()

useEffect(() => {
// if number of decimals of query param value is greater than token decimals,
// truncate the decimals and update the amount query param value
if (countDecimals(String(value)) > decimals) {
setAmount(String(value))
}
}, [value, decimals, setAmount])
const { value = '', ...rest } = props

return (
<input
Expand All @@ -93,10 +80,6 @@ function TransferPanelInputField(
placeholder="Enter amount"
className="h-full w-full bg-transparent px-3 text-xl font-light placeholder:text-gray-dark sm:text-3xl"
value={value}
onChange={event => {
onChange?.(event)
setAmount(event.target.value)
}}
{...rest}
/>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,14 @@ export function useSetInputAmount() {
[decimals, setQueryParams]
)

return setAmount
const setAmount2 = useCallback(
(newAmount: string) => {
const correctDecimalsAmount = truncateExtraDecimals(newAmount, 18)

setQueryParams({ amount2: correctDecimalsAmount })
},
[setQueryParams]
)

return { setAmount, setAmount2 }
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const useArbQueryParams = () => {
sourceChain: ChainParam,
destinationChain: ChainParam,
amount: withDefault(AmountQueryParam, ''), // amount which is filled in Transfer panel
amount2: withDefault(AmountQueryParam, ''), // extra eth to send together with erc20
token: StringParam, // import a new token using a Dialog Box
settingsOpen: withDefault(BooleanParam, false)
})
Expand Down
14 changes: 0 additions & 14 deletions packages/arb-token-bridge-ui/src/util/NumberUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,6 @@ export const formatAmount = <T extends number | BigNumber | undefined>(
)
}

export const countDecimals = (num: number | string) => {
if (Math.floor(Number(num)) === Number(num)) {
return 0
}

const decimalPart = String(num).split('.')[1]

if (typeof decimalPart === 'undefined') {
return 0
}

return decimalPart.length
}

export const truncateExtraDecimals = (amount: string, decimals: number) => {
const decimalPart = amount.split('.')[1]

Expand Down

0 comments on commit 941ae8f

Please sign in to comment.