Skip to content

Commit e835daa

Browse files
cgeweckerichardliang
authored andcommitted
Add forked provider infrastructure and simple TradeModule / ExchangeAdapter tests (#64)
1 parent 8942768 commit e835daa

File tree

15 files changed

+598
-23
lines changed

15 files changed

+598
-23
lines changed

.circleci/config.yml

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ jobs:
1010
working_directory: ~/set-protocol-v2
1111
steps:
1212
- checkout
13-
- setup_remote_docker:
14-
docker_layer_caching: false
1513
- restore_cache:
1614
key: module-cache-{{ checksum "yarn.lock" }}
1715
- run:
@@ -37,11 +35,6 @@ jobs:
3735
working_directory: ~/set-protocol-v2
3836
parallelism: 3
3937
steps:
40-
- setup_remote_docker:
41-
docker_layer_caching: false
42-
- run:
43-
name: Fetch solc version
44-
command: docker pull ethereum/solc:0.6.10
4538
- restore_cache:
4639
key: compiled-env-{{ .Environment.CIRCLE_SHA1 }}
4740
- run:
@@ -54,9 +47,23 @@ jobs:
5447
- run:
5548
name: Hardhat Test
5649
command: |
57-
TEST_FILES="$(circleci tests glob "./test/**/*.spec.ts" | circleci tests split --split-by=timings)"
50+
TEST_FILES="$(circleci tests glob "./test/**/*.spec.ts" | circleci tests split)"
5851
yarn test ${TEST_FILES}
5952
53+
test_forked_network:
54+
docker:
55+
- image: circleci/node:10.16.0
56+
working_directory: ~/set-protocol-v2
57+
steps:
58+
- restore_cache:
59+
key: compiled-env-{{ .Environment.CIRCLE_SHA1 }}
60+
- run:
61+
name: Set Up Environment Variables
62+
command: cp .env.default .env
63+
- run:
64+
name: Hardhat Test
65+
command: yarn test:fork
66+
6067
coverage:
6168
docker:
6269
- image: circleci/node:10.11.0
@@ -67,11 +74,6 @@ jobs:
6774
# to istanbul-combine in the `report_coverage` job
6875
parallelism: 5
6976
steps:
70-
- setup_remote_docker:
71-
docker_layer_caching: false
72-
- run:
73-
name: Fetch solc version
74-
command: docker pull ethereum/solc:0.6.10
7577
- restore_cache:
7678
key: compiled-env-{{ .Environment.CIRCLE_SHA1 }}
7779
- run:
@@ -84,7 +86,7 @@ jobs:
8486
name: Coverage
8587
command: |
8688
TEST_FILES="{$(circleci tests glob "./test/**/*.spec.ts" | \
87-
circleci tests split --split-by=timings | xargs | sed -e 's/ /,/g')}"
89+
circleci tests split | xargs | sed -e 's/ /,/g')}"
8890
yarn coverage -- --testfiles "$TEST_FILES"
8991
- run:
9092
name: Save coverage
@@ -112,9 +114,13 @@ jobs:
112114
- run:
113115
name: Combine coverage reports
114116
command: |
115-
mkdir -p reports
116117
cp -R /tmp/coverage/* .
117-
npx istanbul-combine-updated -r lcov cov_0.json cov_1.json cov_2.json cov_3.json cov_4.json
118+
npx istanbul-combine-updated -r lcov \
119+
cov_0.json \
120+
cov_1.json \
121+
cov_2.json \
122+
cov_3.json \
123+
cov_4.json
118124
- run:
119125
name: Upload coverage
120126
command: |
@@ -128,6 +134,9 @@ workflows:
128134
- test:
129135
requires:
130136
- checkout_and_compile
137+
- test_forked_network:
138+
requires:
139+
- checkout_and_compile
131140
- coverage:
132141
requires:
133142
- checkout_and_compile

.env.default

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# These are randomly generated hex so that CircleCI will work
2+
ALCHEMY_TOKEN=fake_alchemy_token
23
INFURA_TOKEN=799e620c4b39064f7a8cfd8452976ed1
34
KOVAN_DEPLOY_PRIVATE_KEY=0f3456f7f1ed59aaa29f35f4674a87e754e1055249a2888bdaec3dea49d2e456
45
STAGING_MAINNET_DEPLOY_PRIVATE_KEY=0f3456f7f1ed59aaa29f35f4674a87e754e1055249a2888bdaec3dea49d2e456

contracts/mocks/ForceFunderMock.sol

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Copyright 2020 Set Labs Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
16+
SPDX-License-Identifier: Apache License, Version 2.0
17+
*/
18+
19+
pragma solidity 0.6.10;
20+
21+
22+
contract ForceFunderMock {
23+
/**
24+
* Convenience method for depositing eth into non-payable contracts
25+
* which the forked provider tests would like to impersonate
26+
* as a message sender.
27+
*
28+
* @param destination destination of eth payment
29+
*/
30+
function fund(address destination) public payable {
31+
selfdestruct(payable(address(destination)));
32+
}
33+
}

hardhat.config.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require("dotenv").config();
22

3+
import chalk from "chalk";
34
import { HardhatUserConfig } from "hardhat/config";
45
import { privateKeys } from "./utils/wallets";
56

@@ -9,6 +10,19 @@ import "solidity-coverage";
910
import "hardhat-deploy";
1011
import "./tasks";
1112

13+
const forkingConfig = {
14+
url: `https://eth-mainnet.alchemyapi.io/v2/${process.env.ALCHEMY_TOKEN}`,
15+
blockNumber: 12198000,
16+
};
17+
18+
const mochaConfig = {
19+
grep: "@forked-mainnet",
20+
invert: (process.env.FORK) ? false : true,
21+
timeout: (process.env.FORK) ? 50000 : 20000,
22+
} as Mocha.MochaOptions;
23+
24+
checkForkedProviderEnvironment();
25+
1226
const config: HardhatUserConfig = {
1327
solidity: {
1428
version: "0.6.10",
@@ -21,6 +35,7 @@ const config: HardhatUserConfig = {
2135
},
2236
networks: {
2337
hardhat: {
38+
forking: (process.env.FORK) ? forkingConfig : undefined,
2439
accounts: getHardhatPrivateKeys(),
2540
},
2641
localhost: {
@@ -54,9 +69,7 @@ const config: HardhatUserConfig = {
5469
outDir: "typechain",
5570
target: "ethers-v5",
5671
},
57-
mocha: {
58-
timeout: 100000,
59-
},
72+
mocha: mochaConfig,
6073
};
6174

6275
function getHardhatPrivateKeys() {
@@ -69,4 +82,16 @@ function getHardhatPrivateKeys() {
6982
});
7083
}
7184

85+
function checkForkedProviderEnvironment() {
86+
if (process.env.FORK &&
87+
(!process.env.ALCHEMY_TOKEN || process.env.ALCHEMY_TOKEN === "fake_alchemy_token")
88+
) {
89+
console.log(chalk.red(
90+
"You are running forked provider tests with invalid Alchemy credentials.\n" +
91+
"Update your ALCHEMY_TOKEN settings in the `.env` file."
92+
));
93+
process.exit(1);
94+
}
95+
}
96+
7297
export default config;

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
"prepublishOnly": "yarn clean && yarn build:npm",
3737
"rename-extensions": "for f in typechain/*.d.ts; do mv -- \"$f\" \"${f%.d.ts}.ts\"; done",
3838
"test": "npx hardhat test --network localhost",
39+
"test:fork": "FORK=true npx hardhat test",
40+
"test:fork:fast": "NO_COMPILE=true TS_NODE_TRANSPILE_ONLY=1 FORK=true npx hardhat test --no-compile",
3941
"test:clean": "yarn clean && yarn build && yarn test",
4042
"test:fast": "NO_COMPILE=true TS_NODE_TRANSPILE_ONLY=1 npx hardhat test --network localhost --no-compile",
4143
"test:fast:compile": "TS_NODE_TRANSPILE_ONLY=1 npx hardhat test --network localhost",

0 commit comments

Comments
 (0)