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

fix: pool swaps when actionable tokens are not ready #111

Merged
merged 4 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/lib/modules/swap/SwapForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { CompactTokenSelectModal } from '../tokens/TokenSelectModal/TokenSelectL
import { PoolSwapCard } from './PoolSwapCard'
import { isSameAddress } from '@repo/lib/shared/utils/addresses'
import { isPoolSwapAllowed } from '../pool/pool.helpers'
import { supportsNestedActions } from '../pool/actions/LiquidityActionHelpers'

type Props = {
redirectToPoolPage?: () => void // Only used for pool swaps
Expand Down Expand Up @@ -115,21 +116,24 @@ export function SwapForm({ redirectToPoolPage }: Props) {
tokenSelectKey === 'tokenIn' &&
isSameAddress(tokenAddress, tokenOut.address)
) {
return switchTokens()
if (tokenOut.address) return switchTokens()
return setTokenIn(tokenAddress)
}
if (
tokens.length === 2 &&
tokenSelectKey === 'tokenOut' &&
isSameAddress(tokenAddress, tokenIn.address)
) {
return switchTokens()
if (tokenIn.address) return switchTokens()
return setTokenOut(tokenAddress)
}

if (!token) return

if (
pool &&
tokenSelectKey === 'tokenIn' &&
supportsNestedActions(pool) &&
!isPoolSwapAllowed(pool, tokenAddress, tokenOut.address)
) {
setTokenIn(tokenAddress)
Expand All @@ -140,6 +144,7 @@ export function SwapForm({ redirectToPoolPage }: Props) {
if (
pool &&
tokenSelectKey === 'tokenOut' &&
supportsNestedActions(pool) &&
!isPoolSwapAllowed(pool, tokenAddress, tokenIn.address)
) {
setTokenIn('' as Address)
Expand Down
13 changes: 10 additions & 3 deletions packages/lib/modules/swap/SwapProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
} from './wrap.helpers'
import { Pool } from '../pool/PoolProvider'
import { getChildTokens, getStandardRootTokens, isStandardRootToken } from '../pool/pool.helpers'
import { supportsNestedActions } from '../pool/actions/LiquidityActionHelpers'

export type UseSwapResponse = ReturnType<typeof _useSwap>
export const SwapContext = createContext<UseSwapResponse | null>(null)
Expand Down Expand Up @@ -485,10 +486,16 @@ export function _useSwap({ poolActionableTokens, pool, pathParams }: SwapProvide
function setInitialPoolSwapState(pool: Pool) {
const { tokenIn } = pathParams
setInitialTokenIn(tokenIn)
if (isStandardRootToken(pool, tokenIn as Address)) {
setInitialTokenOut(getChildTokens(pool, poolActionableTokens)[0].address)

if (supportsNestedActions(pool)) {
if (isStandardRootToken(pool, tokenIn as Address)) {
setInitialTokenOut(getChildTokens(pool, poolActionableTokens)[0].address)
} else {
setInitialTokenOut(getStandardRootTokens(pool, poolActionableTokens)[0].address)
}
} else {
setInitialTokenOut(getStandardRootTokens(pool, poolActionableTokens)[0].address)
// Does not support nested actions:
setInitialTokenOut(poolActionableTokens?.[1]?.address)
}
resetSwapAmounts()
}
Expand Down
Loading