Skip to content

Commit

Permalink
test(swap): test algorand pool availability disclaimers
Browse files Browse the repository at this point in the history
  • Loading branch information
envin3 committed Apr 4, 2024
1 parent 4ac21c9 commit 08d8d01
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-camels-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ptokens-dapp-v2': patch
---

fix endpoint update and algorand pool availability warnings.
File renamed without changes.
73 changes: 71 additions & 2 deletions src/components/pages/swap/__test__/Swap.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ import { ThemeContext } from 'styled-components'
import { describe, expect, it, vi } from 'vitest'

import { PBTC_ON_ETH_MAINNET, PNT_ON_BSC_MAINNET, PNT_ON_ETH_MAINNET } from '../../../../constants'
import * as UseSwapInfo from '../../../../hooks/use-swap-info'
import swapAssets from '../../../../settings/swap-assets'
import { getDefaultSelection } from '../../../../store/swap/utils/default-selection'
import * as feeUtils from '../../../../utils/fee'
import * as AssetListModal from '../../../organisms/assetListModal/AssetListModal'
import * as SwapInfo from '../../../organisms/swapInfo/SwapInfo'
import Swap from '../Swap'

const Wrapper = ({ asset, originBlockchain, destBlockchain }) => {
const Wrapper = ({ asset, originBlockchain, destBlockchain, algorand_from_assetid, algorand_to_assetid }) => {
const ThemeContextMock = {}
const [button, setButton] = useState({})
const [wallets] = useState({ eth: {}, bsc: {} })
const [wallets] = useState({ eth: {}, bsc: {}, algorand: {} })
const updateSwapButton = useCallback((_text, _disabled = false) => {
setButton({ text: _text, disabled: _disabled })
}, [])
Expand All @@ -26,6 +27,8 @@ const Wrapper = ({ asset, originBlockchain, destBlockchain }) => {
v2selection.asset = asset
v2selection.from = originBlockchain
v2selection.to = destBlockchain
v2selection.algorand_from_assetid = algorand_from_assetid
v2selection.algorand_to_assetid = algorand_to_assetid
}
const [assets] = useState([...swapAssets, ...getDefaultSelection(swapAssets, v2selection)])
return (
Expand Down Expand Up @@ -81,6 +84,72 @@ describe('Swap', async () => {
expect(swapButton).toBeDisabled()
})

it('Should prevent USDT swap from Algorand if pool is not reachable', async () => {
vi.spyOn(UseSwapInfo, 'useSwapInfo').mockReturnValue({ eta: '1', poolAmount: undefined })
vi.spyOn(SwapInfo, 'default').mockImplementation(() => <div data-testid="swap-info" />)
vi.spyOn(feeUtils, 'getSwapFees').mockResolvedValue({ basisPoints: 15, networkFee: 1e18, minProtocolFee: 2e18 })
render(<Wrapper asset="usdt" originBlockchain="algorand" destBlockchain="eth" algorand_from_assetid={'312769'} />)
await waitFor(() => expect(screen.getByText(/Unknown liquidity/)).toBeInTheDocument())
const [swapButton] = screen.getAllByRole('button')
const [fromInput, toInput] = screen.getAllByRole('textbox')
await UserEvent.type(fromInput, '1')
expect(fromInput).toHaveAttribute('value', '1')
expect(toInput).toHaveAttribute('value', '-2')
expect(swapButton).toHaveTextContent('Unknown liquidity')
expect(screen.getByText(/It is currently not possible to check the/)).toBeInTheDocument()
expect(swapButton).toBeDisabled()
})

it('Should continue and warn USDT swap from Algorand if pool liquidity is insufficient', async () => {
vi.spyOn(UseSwapInfo, 'useSwapInfo').mockReturnValue({ eta: '1', poolAmount: '1000' })
vi.spyOn(SwapInfo, 'default').mockImplementation(() => <div data-testid="swap-info" />)
vi.spyOn(feeUtils, 'getSwapFees').mockResolvedValue({ basisPoints: 15, networkFee: 1e18, minProtocolFee: 2e18 })
render(<Wrapper asset="usdt" originBlockchain="algorand" destBlockchain="eth" algorand_from_assetid={'312769'} />)
await waitFor(() => expect(screen.getByText(/Enter an amount/)).toBeInTheDocument())
const [swapButton] = screen.getAllByRole('button')
const [fromInput, toInput] = screen.getAllByRole('textbox')
await UserEvent.type(fromInput, '10000')
expect(fromInput).toHaveAttribute('value', '10,000')
expect(toInput).toHaveAttribute('value', '9,984')
expect(swapButton).toHaveTextContent('Insufficient liquidity')
expect(screen.getByText(/There is not enough liquidity in the stableswap/)).toBeInTheDocument()
expect(swapButton).toBeDisabled()
})

it('Should continue and warn USDT swap to Algorand if pool is not reachable', async () => {
vi.spyOn(UseSwapInfo, 'useSwapInfo').mockReturnValue({ eta: '1', poolAmount: undefined })
vi.spyOn(SwapInfo, 'default').mockImplementation(() => <div data-testid="swap-info" />)
vi.spyOn(feeUtils, 'getSwapFees').mockResolvedValue({ basisPoints: 15, networkFee: 1e18, minProtocolFee: 2e18 })
render(<Wrapper asset="usdt" originBlockchain="eth" destBlockchain="algorand" algorand_to_assetid={'312769'} />)
await waitFor(() => expect(screen.getByText(/Enter an amount/)).toBeInTheDocument())
const [swapButton] = screen.getAllByRole('button')
const [fromInput, toInput] = screen.getAllByRole('textbox')
await UserEvent.type(fromInput, '100')
expect(fromInput).toHaveAttribute('value', '100')
expect(toInput).toHaveAttribute('value', '97')
expect(swapButton).toHaveTextContent('Connect Wallet')
expect(screen.getByText(/It is currently not possible to check the/)).toBeInTheDocument()
expect(swapButton).toBeEnabled()
})

it('Should continue and warn USDT swap to Algorand if pool liquidity is insufficient', async () => {
vi.spyOn(UseSwapInfo, 'useSwapInfo').mockReturnValue({ eta: '1', poolAmount: '1000' })
vi.spyOn(SwapInfo, 'default').mockImplementation(() => <div data-testid="swap-info" />)
vi.spyOn(feeUtils, 'getSwapFees').mockResolvedValue({ basisPoints: 15, networkFee: 1e18, minProtocolFee: 2e18 })
render(<Wrapper asset="usdt" originBlockchain="eth" destBlockchain="algorand" algorand_to_assetid={'312769'} />)
await waitFor(() => expect(screen.getByText(/Enter an amount/)).toBeInTheDocument())
const [swapButton] = screen.getAllByRole('button')
const [fromInput, toInput] = screen.getAllByRole('textbox')
await UserEvent.type(fromInput, '10000')
expect(fromInput).toHaveAttribute('value', '10,000')
expect(toInput).toHaveAttribute('value', '9,984')
expect(swapButton).toHaveTextContent('Connect Wallet')
expect(
screen.getByText(/Due to insufficient liquidity it may not be possible to process a swap of this size/)
).toBeInTheDocument()
expect(swapButton).toBeEnabled()
})

it('Should continue with balance null', async () => {
vi.spyOn(SwapInfo, 'default').mockImplementation(() => <div data-testid="swap-info" />)
vi.spyOn(feeUtils, 'getSwapFees').mockResolvedValue({ basisPoints: 15, networkFee: 1e18, minProtocolFee: 2e18 })
Expand Down

0 comments on commit 08d8d01

Please sign in to comment.