I am looking for a way to create an automated test suite with Truffle that can test my smart contract's interactions with Uniswap V2. The Uniswap docs briefly mention testing with Truffle but do not provide any examples. I am looking to test it using a mainnet fork with ganache.
I'm guessing it's a similar process to the accepted answer for this question, but I'm specifically looking for a way to do it using Truffle and web3.js.
As an example, if I were testing the following contract:
pragma solidity ^0.6.6;
interface IUniswap {
function swapExactETHForTokens(
uint amountOutMin,
address[] calldata path,
address to,
uint deadline)
external
payable
returns (uint[] memory amounts);
function WETH() external pure returns (address);
}
contract MyContract {
IUniswap uniswap;
constructor(address _uniswap) public {
uniswap = IUniswap(_uniswap);
}
function swapExactETHForTokens(uint amountOutMin, address token) external payable {
address[] memory path = new address[](2);
path[0] = uniswap.WETH();
path[1] = token;
uniswap.swapExactETHForTokens{value: msg.value}(
amountOutMin,
path,
msg.sender,
now
);
}
}
How would I create a unit test to verify that swapExactETHForTokens()
swaps ETH for say, DAI? For the value of _uniswap
I've been using UniswapV2Router02.
Any help would be greatly appreciated.
I ended up using Hardhat/Ethers.js anyway, just because of how easy it is to set up a mainnet fork and run an automated test suite. I provided an answer here explaining the steps required to set up a mainnet fork, and reused the example contract in this question (complete with a test).
To answer this question specifically though, Hardhat has a plugin that supports testing with Truffle/Web3js, so this way you can still use Truffle/Web3js for writing your tests/contracts, but use Hardhat's mainnet fork feature for interacting with other contracts already deployed on the mainnet.