Skip to content

Commit

Permalink
fix: validate inputs after userAddress changes (#460)
Browse files Browse the repository at this point in the history
* fix: validate inputs after userAddress changes

* fix: cant scale input amount crash

* chore: remove console.log
  • Loading branch information
agualis authored Jan 17, 2025
1 parent 97c7715 commit 569ebf2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
19 changes: 13 additions & 6 deletions packages/lib/modules/swap/SwapProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,12 @@ export function _useSwap({ poolActionableTokens, pool, pathParams }: SwapProvide
...state,
tokenIn: {
...state.tokenIn,
amount,
scaledAmount: scaleTokenAmount(amount, tokenInInfo),
/*
When copy-pasting a swap URL with a token amount, the tokenInInfo can be undefined
so we set amount as zero instead of crashing the app
*/
amount: tokenInInfo ? amount : '0',
scaledAmount: tokenInInfo ? scaleTokenAmount(amount, tokenInInfo) : 0n,
},
}

Expand All @@ -313,8 +317,12 @@ export function _useSwap({ poolActionableTokens, pool, pathParams }: SwapProvide
...state,
tokenOut: {
...state.tokenOut,
amount,
scaledAmount: scaleTokenAmount(amount, tokenOutInfo),
/*
When copy-pasting a swap URL with a token amount, the tokenOutInfo can be undefined
so we set amount as zero instead of crashing the app
*/
amount: tokenOutInfo ? amount : '0',
scaledAmount: tokenOutInfo ? scaleTokenAmount(amount, tokenOutInfo) : 0n,
},
}

Expand Down Expand Up @@ -398,9 +406,8 @@ export function _useSwap({ poolActionableTokens, pool, pathParams }: SwapProvide
window.history.replaceState({}, '', newPath.join(''))
}

function scaleTokenAmount(amount: string, token: ApiToken | undefined): bigint {
function scaleTokenAmount(amount: string, token: ApiToken): bigint {
if (amount === '') return parseUnits('0', 18)
if (!token) throw new Error('Cant scale amount without token metadata')
return parseUnits(amount, token.decimals)
}

Expand Down
4 changes: 3 additions & 1 deletion packages/lib/modules/tokens/TokenInput/TokenInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { useIsMounted } from '@repo/lib/shared/hooks/useIsMounted'
import { isNativeAsset } from '@repo/lib/shared/utils/addresses'
import { getPriceImpactLabel } from '../../price-impact/price-impact.utils'
import { ApiToken } from '../token.types'
import { useUserAccount } from '../../web3/UserAccountProvider'

type TokenInputSelectorProps = {
token: ApiToken | undefined
Expand Down Expand Up @@ -205,6 +206,7 @@ export const TokenInput = forwardRef(
}: InputProps & Props,
ref
) => {
const { userAddress } = useUserAccount()
const { isBalancesLoading } = useTokenBalances()

const [inputTitle, setInputTitle] = useState<string>('')
Expand Down Expand Up @@ -234,7 +236,7 @@ export const TokenInput = forwardRef(
validateInput(value || '')
setInputTitle(value || '')
}
}, [value, token?.address, isBalancesLoading])
}, [value, token?.address, isBalancesLoading, userAddress])

return (
<Box
Expand Down

0 comments on commit 569ebf2

Please sign in to comment.