Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: block adding too unbalanced #92

Merged
merged 14 commits into from
Oct 24, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { isUnhandledAddPriceImpactError } from '@repo/lib/modules/price-impact/p
import { useModalWithPoolRedirect } from '../../useModalWithPoolRedirect'
import { getPoolTokens } from '../../pool.helpers'
import { useUserSettings } from '@repo/lib/modules/user/settings/UserSettingsProvider'
import { isUnbalancedAddErrorMessage } from '@repo/lib/shared/utils/error-filters'

export type UseAddLiquidityResponse = ReturnType<typeof _useAddLiquidity>
export const AddLiquidityContext = createContext<UseAddLiquidityResponse | null>(null)
Expand Down Expand Up @@ -152,6 +153,7 @@ export function _useAddLiquidity(urlTxHash?: Hash) {
[areEmptyAmounts(humanAmountsIn), 'You must specify one or more token amounts'],
[hasValidationErrors, 'Errors in token inputs'],
[needsToAcceptHighPI, 'Accept high price impact first'],
[isUnbalancedAddErrorMessage(priceImpactQuery.error), 'Unbalanced join'],
[simulationQuery.isLoading, 'Fetching quote...'],
[simulationQuery.isError, 'Error fetching quote'],
[priceImpactQuery.isLoading, 'Fetching price impact...'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function AddLiquidityMainForm() {
proportionalSlippage,
slippage,
setProportionalSlippage,
humanAmountsIn,
} = useAddLiquidity()

const nextBtn = useRef(null)
Expand Down
6 changes: 5 additions & 1 deletion packages/lib/modules/price-impact/price-impact.utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isUnbalancedAddErrorMessage } from '@repo/lib/shared/utils/error-filters'
import { bn } from '@repo/lib/shared/utils/numbers'
import BigNumber from 'bignumber.js'

Expand All @@ -18,9 +19,12 @@ export function isUnhandledAddPriceImpactError(error: Error | null): boolean {
}

export function cannotCalculatePriceImpactError(error: Error | null): boolean {
if (error && error.name === 'ContractFunctionExecutionError') {
const hasUnbalancedAddError = isUnbalancedAddErrorMessage(error)

if (error && error.name === 'ContractFunctionExecutionError' && !hasUnbalancedAddError) {
return true
}

return false
}

Expand Down
16 changes: 16 additions & 0 deletions packages/lib/shared/components/errors/GenericError.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
isNotEnoughGasError,
isPausedError,
isTooManyRequestsError,
isUnbalancedAddError,
isUserRejectedError,
isViemHttpFetchError,
} from '../../utils/error-filters'
Expand All @@ -22,6 +23,7 @@ export function GenericError({ error: _error, customErrorName, ...rest }: Props)
const error = ensureError(_error)
if (isUserRejectedError(error)) return null
const errorName = customErrorName ? `${customErrorName} (${error.name})` : error.name

if (isViemHttpFetchError(_error)) {
return (
<ErrorAlert title={customErrorName} {...rest}>
Expand All @@ -34,6 +36,7 @@ export function GenericError({ error: _error, customErrorName, ...rest }: Props)
</ErrorAlert>
)
}

if (isPausedError(_error)) {
return (
<ErrorAlert title={customErrorName} {...rest}>
Expand All @@ -45,6 +48,7 @@ export function GenericError({ error: _error, customErrorName, ...rest }: Props)
</ErrorAlert>
)
}

if (isTooManyRequestsError(_error)) {
return (
<ErrorAlert title={customErrorName} {...rest}>
Expand All @@ -56,6 +60,7 @@ export function GenericError({ error: _error, customErrorName, ...rest }: Props)
</ErrorAlert>
)
}

if (isNotEnoughGasError(_error)) {
return (
<ErrorAlert title={customErrorName} {...rest}>
Expand All @@ -67,6 +72,17 @@ export function GenericError({ error: _error, customErrorName, ...rest }: Props)
</ErrorAlert>
)
}

if (isUnbalancedAddError(_error)) {
return (
<ErrorAlert title={customErrorName} {...rest}>
<Text color="black" variant="secondary">
Your input(s) would excessively unbalance the pool. Please adjust to be more proportional.
</Text>
</ErrorAlert>
)
}

const errorMessage = error?.shortMessage || error.message

if (errorMessage === 'RPC Request failed.' || errorMessage === 'An unknown RPC error occurred.') {
Expand Down
12 changes: 12 additions & 0 deletions packages/lib/shared/utils/error-filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,15 @@ export function isPausedError(error?: Error | null): boolean {
export function isPausedErrorMessage(errorMessage: string): boolean {
return errorMessage.includes('reverted with the following reason:\nBAL#402\n')
}

export function isUnbalancedAddError(error?: Error | null): boolean {
if (!error) return false
return isUnbalancedAddErrorMessage(error)
}

export function isUnbalancedAddErrorMessage(error: Error | null): boolean {
const errorStrings = ['BAL#304', 'queryAddLiquidityUnbalanced'] // [v2 error, v3 error]
const hasErrors = (errorString: string) => error?.message.includes(errorString)

return errorStrings.some(hasErrors)
}
Loading