diff --git a/cypress/e2e/swap.test.ts b/cypress/e2e/swap.test.ts index 1fbfc77364..269925b333 100644 --- a/cypress/e2e/swap.test.ts +++ b/cypress/e2e/swap.test.ts @@ -1,10 +1,8 @@ import { USDC_MAINNET } from '../../src/constants/tokens' import { WETH_GOERLI } from '../fixtures/constants' -import { HardhatProvider } from '../support/hardhat' import { getTestSelector } from '../utils' describe('Swap', () => { - let hardhat: HardhatProvider const verifyAmount = (field: 'input' | 'output', amountText: string | null) => { if (amountText === null) { cy.get(`#swap-currency-${field} .token-amount-input`).should('not.have.value') @@ -46,9 +44,7 @@ describe('Swap', () => { } before(() => { - cy.visit('/swap', { ethereum: 'hardhat' }).then((window) => { - hardhat = window.hardhat - }) + cy.visit('/swap', { ethereum: 'hardhat' }) }) it('starts with ETH selected by default', () => { @@ -81,35 +77,33 @@ describe('Swap', () => { it('can swap ETH for USDC', () => { const TOKEN_ADDRESS = USDC_MAINNET.address const BALANCE_INCREMENT = 1 - cy.visit('/swap', { ethereum: 'hardhat' }) - .then((window) => { - hardhat = window.hardhat - }) - .then(() => hardhat.utils.getBalance(hardhat.wallet.address, USDC_MAINNET)) - .then((balance) => Number(balance.toFixed(1))) - .then((initialBalance) => { - cy.get('#swap-currency-output .open-currency-select-button').click() - cy.get(getTestSelector('token-search-input')).clear().type(TOKEN_ADDRESS) - cy.contains('USDC').click() - cy.get('#swap-currency-output .token-amount-input').clear().type(BALANCE_INCREMENT.toString()) - cy.get('#swap-currency-input .token-amount-input').should('not.equal', '') - cy.get('#swap-button').click() - cy.get('#confirm-swap-or-send').click() - cy.get(getTestSelector('dismiss-tx-confirmation')).click() + cy.hardhat().then((hardhat) => { + cy.then(() => hardhat.getBalance(hardhat.wallet.address, USDC_MAINNET)) + .then((balance) => Number(balance.toFixed(1))) + .then((initialBalance) => { + cy.get('#swap-currency-output .open-currency-select-button').click() + cy.get(getTestSelector('token-search-input')).clear().type(TOKEN_ADDRESS) + cy.contains('USDC').click() + cy.get('#swap-currency-output .token-amount-input').clear().type(BALANCE_INCREMENT.toString()) + cy.get('#swap-currency-input .token-amount-input').should('not.equal', '') + cy.get('#swap-button').click() + cy.get('#confirm-swap-or-send').click() + cy.get(getTestSelector('dismiss-tx-confirmation')).click() - cy.then(() => hardhat.send('hardhat_mine', ['0x1', '0xc'])).then(() => { - // ui check - cy.get('#swap-currency-output [data-testid="balance-text"]').should( - 'have.text', - `Balance: ${initialBalance + BALANCE_INCREMENT}` - ) + cy.then(() => hardhat.provider.send('hardhat_mine', ['0x1', '0xc'])).then(() => { + // ui check + cy.get('#swap-currency-output [data-testid="balance-text"]').should( + 'have.text', + `Balance: ${initialBalance + BALANCE_INCREMENT}` + ) - // chain state check - cy.then(() => hardhat.utils.getBalance(hardhat.wallet.address, USDC_MAINNET)) - .then((balance) => Number(balance.toFixed(1))) - .should('eq', initialBalance + BALANCE_INCREMENT) + // chain state check + cy.then(() => hardhat.getBalance(hardhat.wallet.address, USDC_MAINNET)) + .then((balance) => Number(balance.toFixed(1))) + .should('eq', initialBalance + BALANCE_INCREMENT) + }) }) - }) + }) }) it('should have the correct default input/output and token selection should work', () => { @@ -162,21 +156,22 @@ describe('Swap', () => { }) it('should render and dismiss the wallet rejection modal', () => { - cy.visit('/swap', { ethereum: 'hardhat' }).then((window) => { - hardhat = window.hardhat - cy.stub(hardhat.wallet, 'sendTransaction').rejects(new Error('user cancelled')) + cy.visit('/swap', { ethereum: 'hardhat' }) + .hardhat() + .then((hardhat) => { + cy.stub(hardhat.wallet, 'sendTransaction').rejects(new Error('user cancelled')) - cy.get('#swap-currency-output .open-currency-select-button').click() - cy.get(getTestSelector('token-search-input')).clear().type(USDC_MAINNET.address) - cy.contains('USDC').click() - cy.get('#swap-currency-output .token-amount-input').clear().type('1') - cy.get('#swap-currency-input .token-amount-input').should('not.equal', '') - cy.get('#swap-button').click() - cy.get('#confirm-swap-or-send').click() - cy.contains('Transaction rejected').should('exist') - cy.contains('Dismiss').click() - cy.contains('Transaction rejected').should('not.exist') - }) + cy.get('#swap-currency-output .open-currency-select-button').click() + cy.get(getTestSelector('token-search-input')).clear().type(USDC_MAINNET.address) + cy.contains('USDC').click() + cy.get('#swap-currency-output .token-amount-input').clear().type('1') + cy.get('#swap-currency-input .token-amount-input').should('not.equal', '') + cy.get('#swap-button').click() + cy.get('#confirm-swap-or-send').click() + cy.contains('Transaction rejected').should('exist') + cy.contains('Dismiss').click() + cy.contains('Transaction rejected').should('not.exist') + }) }) it('Opens and closes the settings menu', () => { diff --git a/cypress/support/e2e.ts b/cypress/support/e2e.ts index 84ee1b9bc2..751c566ede 100644 --- a/cypress/support/e2e.ts +++ b/cypress/support/e2e.ts @@ -6,23 +6,21 @@ // *********************************************************** import '@cypress/code-coverage/support' +import 'cypress-hardhat/lib/browser' import { Eip1193Bridge } from '@ethersproject/experimental/lib/eip1193-bridge' import assert from 'assert' -import { Network } from 'cypress-hardhat/lib/browser' import { FeatureFlag } from '../../src/featureFlags/flags/featureFlags' import { UserState } from '../../src/state/user/reducer' import { CONNECTED_WALLET_USER_STATE } from '../utils/user-state' import { injected } from './ethereum' -import { HardhatProvider } from './hardhat' declare global { // eslint-disable-next-line @typescript-eslint/no-namespace namespace Cypress { interface ApplicationWindow { ethereum: Eip1193Bridge - hardhat: HardhatProvider } interface VisitOptions { serviceWorker?: true @@ -39,10 +37,6 @@ declare global { */ userState?: Partial } - // eslint-disable-next-line @typescript-eslint/no-unused-vars - interface Chainable { - task(event: 'hardhat'): Chainable - } } } @@ -59,8 +53,8 @@ Cypress.Commands.overwrite( return cy .intercept('/service-worker.js', options?.serviceWorker ? undefined : { statusCode: 404 }) - .task('hardhat') - .then((network) => + .provider() + .then((provider) => original({ ...options, url: hashUrl, @@ -84,9 +78,7 @@ Cypress.Commands.overwrite( // Inject the mock ethereum provider. if (options?.ethereum === 'hardhat') { - // The provider is exposed via hardhat to allow mocking / network manipulation. - win.hardhat = new HardhatProvider(network) - win.ethereum = win.hardhat + win.ethereum = provider } else { win.ethereum = injected } diff --git a/cypress/support/hardhat.ts b/cypress/support/hardhat.ts deleted file mode 100644 index d1dacc928a..0000000000 --- a/cypress/support/hardhat.ts +++ /dev/null @@ -1,81 +0,0 @@ -import { Eip1193Bridge } from '@ethersproject/experimental/lib/eip1193-bridge' -import { JsonRpcProvider } from '@ethersproject/providers' -import { Wallet } from '@ethersproject/wallet' -import { HardhatUtils, Network } from 'cypress-hardhat/lib/browser' - -export class HardhatProvider extends Eip1193Bridge { - readonly utils: HardhatUtils - readonly chainId: string - readonly wallet: Wallet - - isMetaMask = true - - constructor(network: Network) { - const utils = new HardhatUtils(network) - const wallet = new Wallet(utils.account.privateKey, utils.provider) - super(wallet, utils.provider) - - this.utils = utils - this.chainId = `0x${network.chainId.toString(16)}` - this.wallet = wallet - } - - async sendAsync(...args: any[]) { - return this.send(...args) - } - - async send(...args: any[]) { - console.debug('hardhat:send', ...args) - - // Parse callback form. - const isCallbackForm = typeof args[0] === 'object' && typeof args[1] === 'function' - let callback = (error: Error | null, result?: { result: T }) => { - if (error) throw error - return result?.result - } - let method - let params - if (isCallbackForm) { - callback = args[1] - method = args[0].method - params = args[0].params - } else { - method = args[0] - params = args[1] - } - - let result - try { - switch (method) { - case 'eth_requestAccounts': - case 'eth_accounts': - result = [this.wallet.address] - break - case 'eth_chainId': - result = this.chainId - break - case 'eth_sendTransaction': { - // Eip1193Bridge doesn't support .gas and .from directly, so we massage it to satisfy ethers' expectations. - // See https://github.com/ethers-io/ethers.js/issues/1683. - params[0].gasLimit = params[0].gas - delete params[0].gas - delete params[0].from - - const req = JsonRpcProvider.hexlifyTransaction(params[0]) - req.gasLimit = req.gas - delete req.gas - - result = (await this.signer.sendTransaction(req)).hash - break - } - default: - result = await super.send(method, params) - } - console.debug('hardhat:receive', method, result) - return callback(null, { result }) - } catch (error) { - console.debug('hardhat:error', method, error) - return callback(error as Error) - } - } -} diff --git a/package.json b/package.json index e0220d8b74..133e588266 100644 --- a/package.json +++ b/package.json @@ -103,7 +103,7 @@ "@vanilla-extract/webpack-plugin": "^2.1.11", "babel-plugin-istanbul": "^6.1.1", "cypress": "10.3.1", - "cypress-hardhat": "^1.0.1", + "cypress-hardhat": "^2.0.0", "env-cmd": "^10.1.0", "eslint": "^7.11.0", "eslint-plugin-import": "^2.27", diff --git a/yarn.lock b/yarn.lock index 9f81313e5c..9a60172294 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9046,10 +9046,10 @@ cyclist@^1.0.1: resolved "https://registry.yarnpkg.com/cyclist/-/cyclist-1.0.1.tgz#596e9698fd0c80e12038c2b82d6eb1b35b6224d9" integrity sha1-WW6WmP0MgOEgOMK4LW6xs1tiJNk= -cypress-hardhat@^1.0.1: - version "1.0.1" - resolved "https://registry.yarnpkg.com/cypress-hardhat/-/cypress-hardhat-1.0.1.tgz#11b86653282769dadc0bd0c65ca41011865b0762" - integrity sha512-eGD7fNM8BXShXEsDbO/m2jv9mx7jHs44bnuWKYxO29ySXX5Soz9+AFYelhzKDvh/T+MJy1YqApPbif9+PNA++g== +cypress-hardhat@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/cypress-hardhat/-/cypress-hardhat-2.0.0.tgz#98f07370270ec7c754d35f77c73b216d9f924abb" + integrity sha512-YLLVZa/15CBo7mmu5JuIGAPg4jLbSYTw/LR690tx90WEiIaPH9diHzWAE41wB+cvmgM7fGwhKnKEEeE1s1DWKg== cypress@*, cypress@10.3.1: version "10.3.1"