This project demonstrates interacting with on-chain protocols (like Uniswap).
Currently the following is demonstrated:
- Forking off Mainnet with Ganache
- Forking off of a fork of Mainnet to run tests in parallel (thanks to Jest)
- Buying DAI with ETH from Uniswap
- Checking ETH price in DAI from custom function in
MyDapp.solwhich extends off ofUniswapLiteBase.solwhich uses Uniswap's on-chain contracts
-
Clone the repo or
npx truffle unbox defi -
Run
npm install -
Create a
.envfile with the following contents:MAINNET_NODE_URL=https://mainnet.infura.io/v3/<API_KEY_HERE> PRIV_KEY_TEST=<some private key> PRIV_KEY_DEPLOY=<some other private key>- Get an API key from: https://infura.io/
- Both of the private keys can be any private key you choose:
PRIV_KEY_DEPLOYrefers to an account specifically for deployingPRIV_KEY_TESTrefers to an account specifically for testing
-
Run
npm startto start a local Ganache chain with forked Mainnet state -
Run
npm run migratein order to compile and migrate the project's contracts to this local Ganache chain -
Run
npm testin order to run the tests.- Each test suite will fork off the local Ganache instance (yes, it's a fork of a fork) and create its own Ganache instance in-memory. This is to prevent out-of-order nonce issues since our tests are run in parallel by Jest.
Some information about the test chains:
-
./tests/utils/test-chain.jsstarts a test chain exposed athttp://127.0.0.1:8545and our contracts need to be migrated to it withtruffle migrate(or thenpm run migratescript). -
./tests/utils/test-environment.jsis instantiated for each and every test suite (i.e. test file in our case). It makes another Ganache instance in-memory that is forked off the local test-chain athttp://127.0.0.1:8545
UniswapLiteBase.sol utilizes the interface contracts: IUniswapExchange.sol and IUniswapFactory.sol in order to provide more convenient internal functions.
MyDapp.sol then inherits from UniswapLiteBase.sol to expose convenient outward facing contract functions.