Skip to content

Commit

Permalink
More Swap testing with hardhat (#3968)
Browse files Browse the repository at this point in the history
* Played around with Hardhat

* Remove API key

* Clean up PR

* Add more timeout

* Move out code

* Revert hardhat config .ts to .js

* Remove commented code

* Add whitespace
  • Loading branch information
FrederikBolding authored May 21, 2021
1 parent 8d1b7c2 commit 9fba72e
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 3 deletions.
16 changes: 15 additions & 1 deletion __tests__/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const FIXTURE_SEND_ADDRESS = '0x4bbeEB066eD09B7AEd07bF39EEe0460DFa261520';

const FIXTURE_SEND_AMOUNT = '0.001';

const FIXTURE_WEB3_ADDRESS = '0xc6d5a3c98ec9073b54fa0969957bd582e8d874bf ';
const FIXTURE_WEB3_ADDRESS = '0xc6d5a3c98ec9073b54fa0969957bd582e8d874bf';

const FIXTURE_MYC_STORAGE_KEY = 'MYC_Storage';

Expand Down Expand Up @@ -163,6 +163,20 @@ const FIXTURE_HARDHAT = {
uuid: '356a192b-7913-504c-9457-4d18c28d46e6',
balance: '9998866308480000000000',
mtime: 1621347441875
},
{
ticker: 'DAI',
name: 'DAI Stablecoin',
decimal: 18,
support: {},
social: {},
networkId: 'Ethereum',
type: 'erc20',
isCustom: false,
contractAddress: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
uuid: 'e1f698bf-cb85-5405-b563-14774af14bf1',
balance: '9998866308480000000000',
mtime: 1621347441875
}
],
transactions: [],
Expand Down
52 changes: 52 additions & 0 deletions __tests__/hardhat-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Contract } from '@ethersproject/contracts';
import { JsonRpcProvider } from '@ethersproject/providers';

import { FIXTURE_WEB3_ADDRESS } from './fixtures';

export const resetFork = async () => {
const provider = new JsonRpcProvider('http://127.0.0.1:8546/');

await provider.send('hardhat_reset', [
{
forking: {
jsonRpcUrl: `https://eth-mainnet.alchemyapi.io/v2/${process.env.ALCHEMY_API_KEY}`
}
}
]);
};

// Transfers DAI to the test address
export const setupDAI = async () => {
const provider = new JsonRpcProvider('http://127.0.0.1:8546/');

await provider.send('hardhat_impersonateAccount', ['0xf977814e90da44bfa03b6295a0616a897441acec']);

const signer = await provider.getSigner('0xf977814e90da44bfa03b6295a0616a897441acec');

const abi = [
// Read-Only Functions
'function balanceOf(address owner) view returns (uint256)',
'function decimals() view returns (uint8)',
'function symbol() view returns (string)',

// Authenticated Functions
'function transfer(address to, uint amount) returns (boolean)',

// Events
'event Transfer(address indexed from, address indexed to, uint amount)'
];

// send ERC20
const erc20 = new Contract('0x6b175474e89094c44da98b954eedeac495271d0f', abi, signer);
const tx = await erc20.populateTransaction.transfer(
FIXTURE_WEB3_ADDRESS,
'100000000000000000000'
);
const sent = await signer.sendTransaction(tx);

await sent.wait();

await provider.send('hardhat_stopImpersonatingAccount', [
'0xf977814e90da44bfa03b6295a0616a897441acec'
]);
};
7 changes: 7 additions & 0 deletions __tests__/swap-page.po.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getByTestId } from '@testing-library/testcafe';
import { Selector, t } from 'testcafe';

import BasePage from './base-page.po';
Expand All @@ -17,6 +18,12 @@ export default class SwapPage extends BasePage {
await t.typeText(Selector('input[name="swap-from"]').parent(), FIXTURE_SEND_AMOUNT);
}

async fillFormERC20() {
await t.click(getByTestId('asset-selector-option-ETH'));
await t.click(getByTestId('asset-selector-option-DAI'));
await t.typeText(Selector('input[name="swap-from"]').parent(), FIXTURE_SEND_AMOUNT);
}

async setupMock() {
await setupEthereumMock(FIXTURE_HARDHAT_PRIVATE_KEY, 1);
}
Expand Down
46 changes: 44 additions & 2 deletions __tests__/swap.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { getByTestId, queryAllByTestId, queryByText } from '@testing-library/testcafe';
import {
getByTestId,
queryAllByTestId,
queryAllByText,
queryByText
} from '@testing-library/testcafe';

import { injectLS } from './clientScripts';
import { FIXTURE_HARDHAT, FIXTURES_CONST, PAGES } from './fixtures';
import { resetFork, setupDAI } from './hardhat-utils';
import SwapPage from './swap-page.po';
import { findByTKey } from './translation-utils';

Expand All @@ -11,9 +17,10 @@ fixture('Swap')
.clientScripts({ content: injectLS(FIXTURE_HARDHAT) })
.page(PAGES.SWAP);

test('can do a swap', async (t) => {
test('can do an ETH swap', async (t) => {
await swapPage.waitPageLoaded();
await swapPage.setupMock();
await resetFork();

await swapPage.fillForm();
await t.wait(FIXTURES_CONST.TIMEOUT);
Expand All @@ -31,3 +38,38 @@ test('can do a swap', async (t) => {
.expect(queryAllByTestId('SUCCESS').with({ timeout: FIXTURES_CONST.HARDHAT_TIMEOUT }).exists)
.ok({ timeout: FIXTURES_CONST.HARDHAT_TIMEOUT });
});

test('can do an ERC20 swap', async (t) => {
await swapPage.waitPageLoaded();
await swapPage.setupMock();
await resetFork();
await setupDAI();

await swapPage.fillFormERC20();
await t.wait(FIXTURES_CONST.TIMEOUT);

const button = await getByTestId('confirm-swap');
await t.click(button);

const approve = await queryAllByText(findByTKey('APPROVE_SWAP'))
.with({
timeout: FIXTURES_CONST.HARDHAT_TIMEOUT
})
.nth(1);
await t.expect(approve.exists).ok({ timeout: FIXTURES_CONST.HARDHAT_TIMEOUT });
await t.click(approve);

await t.wait(FIXTURES_CONST.HARDHAT_TIMEOUT);

const send = await queryByText(findByTKey('CONFIRM_TRANSACTION')).with({
timeout: FIXTURES_CONST.HARDHAT_TIMEOUT
});
await t.expect(send.exists).ok({ timeout: FIXTURES_CONST.HARDHAT_TIMEOUT });
await t.click(send);

await t.wait(FIXTURES_CONST.HARDHAT_TIMEOUT);

await t
.expect(queryAllByTestId('SUCCESS').count)
.eql(2, { timeout: FIXTURES_CONST.HARDHAT_TIMEOUT });
});
1 change: 1 addition & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { HardhatUserConfig } from 'hardhat/config';
import '@nomiclabs/hardhat-ethers';

const config: HardhatUserConfig = {
networks: {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@ethersproject/bignumber": "5.1.1",
"@ethersproject/bytes": "5.1.0",
"@ethersproject/constants": "5.1.0",
"@ethersproject/contracts": "5.1.1",
"@ethersproject/providers": "5.1.2",
"@ethersproject/transactions": "5.1.1",
"@ethersproject/units": "5.1.0",
Expand Down
31 changes: 31 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,21 @@
rustbn.js "~0.2.0"
util.promisify "^1.0.1"

"@ethersproject/abi@^5.1.0":
version "5.1.2"
resolved "https://registry.yarnpkg.com/@ethersproject/abi/-/abi-5.1.2.tgz#a8e75cd0455e6dc9e4861c3d1c22bbe436c1d775"
integrity sha512-uMhoQVPX0UtfzTpekYQSEUcJGDgsJ25ifz+SV6PDETWaUFhcR8RNgb1QPTASP13inW8r6iy0/Xdq9D5hK2pNvA==
dependencies:
"@ethersproject/address" "^5.1.0"
"@ethersproject/bignumber" "^5.1.0"
"@ethersproject/bytes" "^5.1.0"
"@ethersproject/constants" "^5.1.0"
"@ethersproject/hash" "^5.1.0"
"@ethersproject/keccak256" "^5.1.0"
"@ethersproject/logger" "^5.1.0"
"@ethersproject/properties" "^5.1.0"
"@ethersproject/strings" "^5.1.0"

"@ethersproject/[email protected]", "@ethersproject/abstract-provider@^5.1.0":
version "5.1.0"
resolved "https://registry.yarnpkg.com/@ethersproject/abstract-provider/-/abstract-provider-5.1.0.tgz#1f24c56cda5524ef4ed3cfc562a01d6b6f8eeb0b"
Expand Down Expand Up @@ -1453,6 +1468,22 @@
dependencies:
"@ethersproject/bignumber" "^5.1.0"

"@ethersproject/[email protected]":
version "5.1.1"
resolved "https://registry.yarnpkg.com/@ethersproject/contracts/-/contracts-5.1.1.tgz#c66cb6d618fcbd73e20a6b808e8f768b2b781d0b"
integrity sha512-6WwktLJ0DFWU8pDkgH4IGttQHhQN4SnwKFu9h+QYVe48VGWtbDu4W8/q/7QA1u/HWlWMrKxqawPiZUJj0UMvOw==
dependencies:
"@ethersproject/abi" "^5.1.0"
"@ethersproject/abstract-provider" "^5.1.0"
"@ethersproject/abstract-signer" "^5.1.0"
"@ethersproject/address" "^5.1.0"
"@ethersproject/bignumber" "^5.1.0"
"@ethersproject/bytes" "^5.1.0"
"@ethersproject/constants" "^5.1.0"
"@ethersproject/logger" "^5.1.0"
"@ethersproject/properties" "^5.1.0"
"@ethersproject/transactions" "^5.1.0"

"@ethersproject/hash@^5.1.0":
version "5.1.0"
resolved "https://registry.yarnpkg.com/@ethersproject/hash/-/hash-5.1.0.tgz#40961d64837d57f580b7b055e0d74174876d891e"
Expand Down

0 comments on commit 9fba72e

Please sign in to comment.