Skip to content

Commit

Permalink
Add forked provider infrastructure and simple TradeModule / ExchangeA…
Browse files Browse the repository at this point in the history
…dapter tests (#64)
  • Loading branch information
cgewecke authored Jun 10, 2021
1 parent 31d67e5 commit 465a916
Show file tree
Hide file tree
Showing 15 changed files with 598 additions and 23 deletions.
41 changes: 25 additions & 16 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ jobs:
working_directory: ~/set-protocol-v2
steps:
- checkout
- setup_remote_docker:
docker_layer_caching: false
- restore_cache:
key: module-cache-{{ checksum "yarn.lock" }}
- run:
Expand All @@ -37,11 +35,6 @@ jobs:
working_directory: ~/set-protocol-v2
parallelism: 3
steps:
- setup_remote_docker:
docker_layer_caching: false
- run:
name: Fetch solc version
command: docker pull ethereum/solc:0.6.10
- restore_cache:
key: compiled-env-{{ .Environment.CIRCLE_SHA1 }}
- run:
Expand All @@ -54,9 +47,23 @@ jobs:
- run:
name: Hardhat Test
command: |
TEST_FILES="$(circleci tests glob "./test/**/*.spec.ts" | circleci tests split --split-by=timings)"
TEST_FILES="$(circleci tests glob "./test/**/*.spec.ts" | circleci tests split)"
yarn test ${TEST_FILES}
test_forked_network:
docker:
- image: circleci/node:10.16.0
working_directory: ~/set-protocol-v2
steps:
- restore_cache:
key: compiled-env-{{ .Environment.CIRCLE_SHA1 }}
- run:
name: Set Up Environment Variables
command: cp .env.default .env
- run:
name: Hardhat Test
command: yarn test:fork

coverage:
docker:
- image: circleci/node:10.11.0
Expand All @@ -67,11 +74,6 @@ jobs:
# to istanbul-combine in the `report_coverage` job
parallelism: 5
steps:
- setup_remote_docker:
docker_layer_caching: false
- run:
name: Fetch solc version
command: docker pull ethereum/solc:0.6.10
- restore_cache:
key: compiled-env-{{ .Environment.CIRCLE_SHA1 }}
- run:
Expand All @@ -84,7 +86,7 @@ jobs:
name: Coverage
command: |
TEST_FILES="{$(circleci tests glob "./test/**/*.spec.ts" | \
circleci tests split --split-by=timings | xargs | sed -e 's/ /,/g')}"
circleci tests split | xargs | sed -e 's/ /,/g')}"
yarn coverage -- --testfiles "$TEST_FILES"
- run:
name: Save coverage
Expand Down Expand Up @@ -112,9 +114,13 @@ jobs:
- run:
name: Combine coverage reports
command: |
mkdir -p reports
cp -R /tmp/coverage/* .
npx istanbul-combine-updated -r lcov cov_0.json cov_1.json cov_2.json cov_3.json cov_4.json
npx istanbul-combine-updated -r lcov \
cov_0.json \
cov_1.json \
cov_2.json \
cov_3.json \
cov_4.json
- run:
name: Upload coverage
command: |
Expand All @@ -128,6 +134,9 @@ workflows:
- test:
requires:
- checkout_and_compile
- test_forked_network:
requires:
- checkout_and_compile
- coverage:
requires:
- checkout_and_compile
Expand Down
1 change: 1 addition & 0 deletions .env.default
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# These are randomly generated hex so that CircleCI will work
ALCHEMY_TOKEN=fake_alchemy_token
INFURA_TOKEN=799e620c4b39064f7a8cfd8452976ed1
KOVAN_DEPLOY_PRIVATE_KEY=0f3456f7f1ed59aaa29f35f4674a87e754e1055249a2888bdaec3dea49d2e456
STAGING_MAINNET_DEPLOY_PRIVATE_KEY=0f3456f7f1ed59aaa29f35f4674a87e754e1055249a2888bdaec3dea49d2e456
Expand Down
33 changes: 33 additions & 0 deletions contracts/mocks/ForceFunderMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright 2020 Set Labs Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
SPDX-License-Identifier: Apache License, Version 2.0
*/

pragma solidity 0.6.10;


contract ForceFunderMock {
/**
* Convenience method for depositing eth into non-payable contracts
* which the forked provider tests would like to impersonate
* as a message sender.
*
* @param destination destination of eth payment
*/
function fund(address destination) public payable {
selfdestruct(payable(address(destination)));
}
}
31 changes: 28 additions & 3 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require("dotenv").config();

import chalk from "chalk";
import { HardhatUserConfig } from "hardhat/config";
import { privateKeys } from "./utils/wallets";

Expand All @@ -9,6 +10,19 @@ import "solidity-coverage";
import "hardhat-deploy";
import "./tasks";

const forkingConfig = {
url: `https://eth-mainnet.alchemyapi.io/v2/${process.env.ALCHEMY_TOKEN}`,
blockNumber: 12198000,
};

const mochaConfig = {
grep: "@forked-mainnet",
invert: (process.env.FORK) ? false : true,
timeout: (process.env.FORK) ? 50000 : 20000,
} as Mocha.MochaOptions;

checkForkedProviderEnvironment();

const config: HardhatUserConfig = {
solidity: {
version: "0.6.10",
Expand All @@ -21,6 +35,7 @@ const config: HardhatUserConfig = {
},
networks: {
hardhat: {
forking: (process.env.FORK) ? forkingConfig : undefined,
accounts: getHardhatPrivateKeys(),
},
localhost: {
Expand Down Expand Up @@ -54,9 +69,7 @@ const config: HardhatUserConfig = {
outDir: "typechain",
target: "ethers-v5",
},
mocha: {
timeout: 100000,
},
mocha: mochaConfig,
};

function getHardhatPrivateKeys() {
Expand All @@ -69,4 +82,16 @@ function getHardhatPrivateKeys() {
});
}

function checkForkedProviderEnvironment() {
if (process.env.FORK &&
(!process.env.ALCHEMY_TOKEN || process.env.ALCHEMY_TOKEN === "fake_alchemy_token")
) {
console.log(chalk.red(
"You are running forked provider tests with invalid Alchemy credentials.\n" +
"Update your ALCHEMY_TOKEN settings in the `.env` file."
));
process.exit(1);
}
}

export default config;
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
"prepublishOnly": "yarn clean && yarn build:npm",
"rename-extensions": "for f in typechain/*.d.ts; do mv -- \"$f\" \"${f%.d.ts}.ts\"; done",
"test": "npx hardhat test --network localhost",
"test:fork": "FORK=true npx hardhat test",
"test:fork:fast": "NO_COMPILE=true TS_NODE_TRANSPILE_ONLY=1 FORK=true npx hardhat test --no-compile",
"test:clean": "yarn clean && yarn build && yarn test",
"test:fast": "NO_COMPILE=true TS_NODE_TRANSPILE_ONLY=1 npx hardhat test --network localhost --no-compile",
"test:fast:compile": "TS_NODE_TRANSPILE_ONLY=1 npx hardhat test --network localhost",
Expand Down
Loading

0 comments on commit 465a916

Please sign in to comment.