2023-05-17 19:43:52 +03:00
|
|
|
import { BigNumber } from '@ethersproject/bignumber'
|
2023-05-22 19:02:54 +03:00
|
|
|
import { SupportedChainId } from '@uniswap/sdk-core'
|
|
|
|
|
2023-06-07 19:29:23 +03:00
|
|
|
import { DEFAULT_DEADLINE_FROM_NOW } from '../../../src/constants/misc'
|
2023-05-22 19:02:54 +03:00
|
|
|
import { UNI, USDC_MAINNET } from '../../../src/constants/tokens'
|
|
|
|
import { getBalance, getTestSelector } from '../../utils'
|
|
|
|
|
|
|
|
const UNI_MAINNET = UNI[SupportedChainId.MAINNET]
|
|
|
|
|
|
|
|
describe('Swap errors', () => {
|
|
|
|
it('wallet rejection', () => {
|
|
|
|
cy.visit(`/swap?inputCurrency=ETH&outputCurrency=${USDC_MAINNET.address}`, { ethereum: 'hardhat' })
|
|
|
|
cy.hardhat().then((hardhat) => {
|
|
|
|
// Stub the wallet to reject any transaction.
|
|
|
|
cy.stub(hardhat.wallet, 'sendTransaction').log(false).rejects(new Error('user cancelled'))
|
|
|
|
|
2023-06-07 19:29:23 +03:00
|
|
|
// Enter amount to swap
|
|
|
|
cy.get('#swap-currency-output .token-amount-input').type('1').should('have.value', '1')
|
2023-05-22 19:02:54 +03:00
|
|
|
cy.get('#swap-currency-input .token-amount-input').should('not.have.value', '')
|
2023-06-07 19:29:23 +03:00
|
|
|
|
|
|
|
// Submit transaction
|
2023-05-22 19:02:54 +03:00
|
|
|
cy.get('#swap-button').click()
|
2023-06-07 19:29:23 +03:00
|
|
|
cy.contains('Confirm swap').click()
|
|
|
|
cy.wait('@eth_estimateGas')
|
2023-05-22 19:02:54 +03:00
|
|
|
|
2023-06-07 19:29:23 +03:00
|
|
|
// Verify rejection
|
|
|
|
cy.contains('Review swap')
|
|
|
|
cy.contains('Confirm swap')
|
2023-05-22 19:02:54 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('transaction past deadline', () => {
|
|
|
|
cy.visit(`/swap?inputCurrency=ETH&outputCurrency=${USDC_MAINNET.address}`, { ethereum: 'hardhat' })
|
|
|
|
cy.hardhat({ automine: false })
|
|
|
|
getBalance(USDC_MAINNET).then((initialBalance) => {
|
2023-06-07 19:29:23 +03:00
|
|
|
// Enter amount to swap
|
|
|
|
cy.get('#swap-currency-output .token-amount-input').type('1').should('have.value', '1')
|
2023-05-22 19:02:54 +03:00
|
|
|
cy.get('#swap-currency-input .token-amount-input').should('not.have.value', '')
|
2023-06-07 19:29:23 +03:00
|
|
|
|
|
|
|
// Submit transaction
|
2023-05-22 19:02:54 +03:00
|
|
|
cy.get('#swap-button').click()
|
2023-06-07 19:29:23 +03:00
|
|
|
cy.contains('Confirm swap').click()
|
|
|
|
cy.wait('@eth_estimateGas').wait('@eth_sendRawTransaction').wait('@eth_getTransactionReceipt')
|
|
|
|
cy.contains('Transaction submitted')
|
2023-05-25 23:49:27 +03:00
|
|
|
cy.get(getTestSelector('confirmation-close-icon')).click()
|
2023-05-22 19:02:54 +03:00
|
|
|
cy.get(getTestSelector('web3-status-connected')).should('contain', '1 Pending')
|
|
|
|
|
2023-06-07 19:29:23 +03:00
|
|
|
// Mine transaction
|
|
|
|
cy.hardhat().then(async (hardhat) => {
|
|
|
|
// Remove the transaction from the mempool, so that it doesn't fail but it is past the deadline.
|
|
|
|
// This should result in it being removed from pending transactions, without a failure notificiation.
|
|
|
|
const transactions = await hardhat.send('eth_pendingTransactions', [])
|
|
|
|
await hardhat.send('hardhat_dropTransaction', [transactions[0].hash])
|
|
|
|
// Mine past the deadline
|
|
|
|
await hardhat.mine(1, DEFAULT_DEADLINE_FROM_NOW + 1)
|
|
|
|
})
|
|
|
|
cy.wait('@eth_getTransactionReceipt')
|
2023-05-22 19:02:54 +03:00
|
|
|
|
2023-06-07 19:29:23 +03:00
|
|
|
// Verify transaction did not occur
|
|
|
|
cy.get(getTestSelector('web3-status-connected')).should('not.contain', 'Pending')
|
|
|
|
cy.get(getTestSelector('popups')).should('not.contain', 'Swap failed')
|
|
|
|
cy.get('#swap-currency-output').contains(`Balance: ${initialBalance}`)
|
2023-05-22 19:02:54 +03:00
|
|
|
getBalance(USDC_MAINNET).should('eq', initialBalance)
|
|
|
|
})
|
2023-05-17 19:43:52 +03:00
|
|
|
})
|
|
|
|
|
2023-05-22 19:02:54 +03:00
|
|
|
it('slippage failure', () => {
|
|
|
|
cy.visit(`/swap?inputCurrency=ETH&outputCurrency=${UNI_MAINNET.address}`, { ethereum: 'hardhat' })
|
|
|
|
cy.hardhat({ automine: false })
|
|
|
|
getBalance(USDC_MAINNET).then((initialBalance) => {
|
|
|
|
// Gas estimation fails for this transaction (that would normally fail), so we stub it.
|
|
|
|
cy.hardhat().then((hardhat) => {
|
|
|
|
const send = cy.stub(hardhat.provider, 'send').log(false)
|
|
|
|
send.withArgs('eth_estimateGas').resolves(BigNumber.from(2_000_000))
|
|
|
|
send.callThrough()
|
2023-05-17 19:43:52 +03:00
|
|
|
})
|
2023-05-22 19:02:54 +03:00
|
|
|
|
|
|
|
// Set slippage to a very low value.
|
|
|
|
cy.get(getTestSelector('open-settings-dialog-button')).click()
|
|
|
|
cy.get(getTestSelector('max-slippage-settings')).click()
|
|
|
|
cy.get(getTestSelector('slippage-input')).clear().type('0.01')
|
2023-06-07 19:29:23 +03:00
|
|
|
cy.get('body').click('topRight') // close modal
|
2023-05-22 19:02:54 +03:00
|
|
|
cy.get(getTestSelector('slippage-input')).should('not.exist')
|
|
|
|
|
2023-06-07 19:29:23 +03:00
|
|
|
// Submit 2 transactions
|
|
|
|
for (let i = 0; i < 2; i++) {
|
|
|
|
cy.get('#swap-currency-input .token-amount-input').type('200').should('have.value', '200')
|
|
|
|
cy.get('#swap-currency-output .token-amount-input').should('not.have.value', '')
|
|
|
|
cy.get('#swap-button').click()
|
|
|
|
cy.contains('Confirm swap').click()
|
|
|
|
cy.wait('@eth_sendRawTransaction').wait('@eth_getTransactionReceipt')
|
|
|
|
cy.contains('Transaction submitted')
|
|
|
|
cy.get(getTestSelector('confirmation-close-icon')).click()
|
|
|
|
}
|
2023-05-22 19:02:54 +03:00
|
|
|
cy.get(getTestSelector('web3-status-connected')).should('contain', '2 Pending')
|
|
|
|
|
2023-06-07 19:29:23 +03:00
|
|
|
// Mine transactions
|
|
|
|
cy.hardhat().then((hardhat) => hardhat.mine())
|
|
|
|
cy.wait('@eth_getTransactionReceipt')
|
2023-05-22 19:02:54 +03:00
|
|
|
|
2023-06-07 19:29:23 +03:00
|
|
|
// Verify transaction did not occur
|
|
|
|
cy.get(getTestSelector('web3-status-connected')).should('not.contain', 'Pending')
|
|
|
|
cy.get(getTestSelector('popups')).contains('Swap failed')
|
|
|
|
getBalance(UNI_MAINNET).should('eq', initialBalance)
|
2023-05-22 19:02:54 +03:00
|
|
|
})
|
2023-05-17 19:43:52 +03:00
|
|
|
})
|
|
|
|
})
|