Skip to content

Commit

Permalink
debug: remove initial amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
agualis committed Sep 5, 2024
1 parent 770951b commit 7b20234
Showing 1 changed file with 4 additions and 60 deletions.
64 changes: 4 additions & 60 deletions lib/modules/swap/SwapProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,18 @@ import { isSameAddress } from '@/lib/shared/utils/addresses'
import { useMandatoryContext } from '@/lib/shared/utils/contexts'
import { isDisabledWithReason } from '@/lib/shared/utils/functions/isDisabledWithReason'
import { bn } from '@/lib/shared/utils/numbers'
import { ApolloClient, useApolloClient, useReactiveVar } from '@apollo/client'
import { HumanAmount } from '@balancer/sdk'
import { useReactiveVar } from '@apollo/client'
import { useDisclosure } from '@chakra-ui/react'
import { invert } from 'lodash'
import { PropsWithChildren, createContext, useEffect, useMemo, useState } from 'react'
import { PropsWithChildren, createContext, useEffect, useState } from 'react'
import { Address, Hash, isAddress, parseUnits } from 'viem'
import { ChainSlug, slugToChainMap } from '../pool/pool.utils'
import { useTokenBalances } from '../tokens/TokenBalancesProvider'
import { useTokenInputsValidation } from '../tokens/TokenInputsValidationProvider'
import { useTokens } from '../tokens/TokensProvider'
import { useUserAccount } from '../web3/UserAccountProvider'
import { emptyAddress } from '../web3/contracts/wagmi-helpers'
import { AuraBalSwapHandler } from './handlers/AuraBalSwap.handler'
import { DefaultSwapHandler } from './handlers/DefaultSwap.handler'
import { NativeWrapHandler } from './handlers/NativeWrap.handler'
import { SwapHandler } from './handlers/Swap.handler'
import { isAuraBalSwap } from './swap.helpers'
import { SwapState } from './swap.types'
import {
getWrapHandlerClass,
getWrapperForBaseToken,
isNativeWrap,
isSupportedWrap,
} from './wrap.helpers'

export type UseSwapResponse = ReturnType<typeof _useSwap>
export const SwapContext = createContext<UseSwapResponse | null>(null)
Expand Down Expand Up @@ -68,21 +56,18 @@ export function _useSwap({ urlTxHash, ...pathParams }: PathParams) {
)

const swapState = useReactiveVar(swapStateVar)
const [needsToAcceptHighPI, setNeedsToAcceptHighPI] = useState(false)
const [tokenSelectKey, setTokenSelectKey] = useState<'tokenIn' | 'tokenOut'>('tokenIn')
const [initUserChain, setInitUserChain] = useState<GqlChain | undefined>(undefined)

const { isConnected } = useUserAccount()
const { chain: walletChain } = useNetworkConfig()
const { getToken, getTokensByChain, usdValueForToken } = useTokens()
const { tokens, setTokens } = useTokenBalances()
const { getToken } = useTokens()
const { tokens } = useTokenBalances()
const { hasValidationErrors } = useTokenInputsValidation()

const networkConfig = getNetworkConfig(swapState.selectedChain)
const previewModalDisclosure = useDisclosure()

const client = useApolloClient()

const isTokenInSet = swapState.tokenIn.address !== emptyAddress
const isTokenOutSet = swapState.tokenOut.address !== emptyAddress

Expand Down Expand Up @@ -218,24 +203,6 @@ export function _useSwap({ urlTxHash, ...pathParams }: PathParams) {
}
}

function resetSwapAmounts() {
const state = swapStateVar()

swapStateVar({
...state,
tokenIn: {
...state.tokenIn,
amount: '',
scaledAmount: BigInt(0),
},
tokenOut: {
...state.tokenOut,
amount: '',
scaledAmount: BigInt(0),
},
})
}

function setDefaultTokens() {
swapStateVar(getDefaultTokenState(swapState.selectedChain))
}
Expand Down Expand Up @@ -279,14 +246,6 @@ export function _useSwap({ urlTxHash, ...pathParams }: PathParams) {
setSelectedChain(_chain)
}

function setInitialAmounts(slugAmountIn?: string, slugAmountOut?: string) {
if (slugAmountIn && !slugAmountOut && bn(slugAmountIn).gt(0)) {
setTokenInAmount(slugAmountIn as HumanAmount)
} else if (slugAmountOut && bn(slugAmountOut).gt(0)) {
setTokenOutAmount(slugAmountOut as HumanAmount)
} else resetSwapAmounts()
}

// Set state on initial load
useEffect(() => {
if (urlTxHash) return
Expand All @@ -296,7 +255,6 @@ export function _useSwap({ urlTxHash, ...pathParams }: PathParams) {
setInitialChain(chain)
setInitialTokenIn(tokenIn)
setInitialTokenOut(tokenOut)
setInitialAmounts(amountIn, amountOut)

if (!swapState.tokenIn.address && !swapState.tokenOut.address) setDefaultTokens()
}, [])
Expand All @@ -310,21 +268,9 @@ export function _useSwap({ urlTxHash, ...pathParams }: PathParams) {
}
}, [walletChain])

// Check if tokenOut is a base wrap token and set tokenIn as the wrapped token.
useEffect(() => {
const wrapper = getWrapperForBaseToken(swapState.tokenOut.address, swapState.selectedChain)
if (wrapper) setTokenIn(wrapper.wrappedToken)
}, [swapState.tokenOut.address])

// Update selecteable tokens when the chain changes
useEffect(() => {
setTokens(getTokensByChain(swapState.selectedChain))
}, [swapState.selectedChain])

const { isDisabled, disabledReason } = isDisabledWithReason(
[!isConnected, LABELS.walletNotConnected],
[!validAmountOut, 'Invalid amount out'],
[needsToAcceptHighPI, 'Accept high price impact first'],
[hasValidationErrors, 'Invalid input']
)

Expand All @@ -337,15 +283,13 @@ export function _useSwap({ urlTxHash, ...pathParams }: PathParams) {
isDisabled,
disabledReason,
previewModalDisclosure,
resetSwapAmounts,
setTokenSelectKey,
setSelectedChain,
setTokenInAmount,
setTokenOutAmount,
setTokenIn,
setTokenOut,
switchTokens,
setNeedsToAcceptHighPI,
}
}

Expand Down

0 comments on commit 7b20234

Please sign in to comment.