2023-05-17 19:43:52 +03:00
|
|
|
import { BigNumber } from '@ethersproject/bignumber'
|
2023-07-28 22:48:12 +03:00
|
|
|
import { CurrencyAmount } from '@uniswap/sdk-core'
|
2023-05-22 19:02:54 +03:00
|
|
|
|
2023-06-07 19:29:23 +03:00
|
|
|
import { DEFAULT_DEADLINE_FROM_NOW } from '../../../src/constants/misc'
|
2023-07-28 22:48:12 +03:00
|
|
|
import { DAI, USDC_MAINNET } from '../../../src/constants/tokens'
|
2023-05-22 19:02:54 +03:00
|
|
|
import { getBalance, getTestSelector } from '../../utils'
|
|
|
|
|
|
|
|
describe('Swap errors', () => {
|
|
|
|
it('wallet rejection', () => {
|
2023-08-02 08:13:19 +03:00
|
|
|
cy.visit(`/swap?inputCurrency=ETH&outputCurrency=${USDC_MAINNET.address}`)
|
2023-05-22 19:02:54 +03:00
|
|
|
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', () => {
|
2023-08-02 08:13:19 +03:00
|
|
|
cy.visit(`/swap?inputCurrency=ETH&outputCurrency=${USDC_MAINNET.address}`)
|
2023-05-22 19:02:54 +03:00
|
|
|
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')
|
2023-07-15 00:46:59 +03:00
|
|
|
cy.contains('Swap 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-07-28 22:48:12 +03:00
|
|
|
it('slippage failure', () => {
|
2023-08-02 08:13:19 +03:00
|
|
|
cy.visit(`/swap?inputCurrency=${USDC_MAINNET.address}&outputCurrency=${DAI.address}`)
|
2023-07-28 22:48:12 +03:00
|
|
|
cy.hardhat({ automine: false }).then(async (hardhat) => {
|
|
|
|
await hardhat.fund(hardhat.wallet, CurrencyAmount.fromRawAmount(USDC_MAINNET, 500e6))
|
|
|
|
await hardhat.mine()
|
|
|
|
await Promise.all([
|
|
|
|
hardhat.approval.setTokenAllowanceForPermit2({ owner: hardhat.wallet, token: USDC_MAINNET }),
|
|
|
|
hardhat.approval.setPermit2Allowance({ owner: hardhat.wallet, token: USDC_MAINNET }),
|
|
|
|
])
|
|
|
|
await hardhat.mine()
|
|
|
|
})
|
|
|
|
|
|
|
|
getBalance(DAI).then((initialBalance) => {
|
2023-05-22 19:02:54 +03:00
|
|
|
// 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')
|
2023-07-15 00:46:59 +03:00
|
|
|
cy.contains('Swap submitted')
|
2023-07-28 22:48:12 +03:00
|
|
|
if (i === 0) {
|
|
|
|
cy.get(getTestSelector('confirmation-close-icon')).click()
|
|
|
|
}
|
2023-06-07 19:29:23 +03:00
|
|
|
}
|
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-07-28 22:48:12 +03:00
|
|
|
cy.contains('Swap failed')
|
|
|
|
|
|
|
|
// Verify only 1 transaction occurred
|
2023-06-07 19:29:23 +03:00
|
|
|
cy.get(getTestSelector('web3-status-connected')).should('not.contain', 'Pending')
|
2023-07-28 22:48:12 +03:00
|
|
|
cy.get(getTestSelector('popups')).contains('Swapped')
|
2023-06-07 19:29:23 +03:00
|
|
|
cy.get(getTestSelector('popups')).contains('Swap failed')
|
2023-08-02 08:13:19 +03:00
|
|
|
getBalance(DAI).should('be.closeTo', initialBalance + 200, 1)
|
2023-05-22 19:02:54 +03:00
|
|
|
})
|
2023-05-17 19:43:52 +03:00
|
|
|
})
|
|
|
|
})
|