|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +/* |
| 3 | + Copyright 2023 ZeroEx Intl. |
| 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 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + Unless required by applicable law or agreed to in writing, software |
| 9 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + See the License for the specific language governing permissions and |
| 12 | + limitations under the License. |
| 13 | +*/ |
| 14 | + |
| 15 | +pragma solidity ^0.6; |
| 16 | +pragma experimental ABIEncoderV2; |
| 17 | + |
| 18 | +import {Test} from "forge-std/Test.sol"; |
| 19 | +import {IERC20Token} from "@0x/contracts-erc20/src/IERC20Token.sol"; |
| 20 | +import {IEtherToken} from "@0x/contracts-erc20/src/IEtherToken.sol"; |
| 21 | +import {WETH9V06} from "@0x/contracts-erc20/src/v06/WETH9V06.sol"; |
| 22 | +import {IFlashWallet} from "src/external/IFlashWallet.sol"; |
| 23 | +import {LibERC20Transformer} from "src/transformers/LibERC20Transformer.sol"; |
| 24 | +import {LibNativeOrder} from "src/features/libs/LibNativeOrder.sol"; |
| 25 | +import {LibSignature} from "src/features/libs/LibSignature.sol"; |
| 26 | +import {IMultiplexFeature} from "src/features/interfaces/IMultiplexFeature.sol"; |
| 27 | +import {ITransformERC20Feature} from "src/features/interfaces/ITransformERC20Feature.sol"; |
| 28 | +import {TestUtils} from "utils/TestUtils.sol"; |
| 29 | +import {DeployZeroEx} from "utils/DeployZeroEx.sol"; |
| 30 | +import {TestMintTokenERC20Transformer} from "../../contracts/test/TestMintTokenERC20Transformer.sol"; |
| 31 | +import {TestMintableERC20Token} from "../../contracts/test/tokens/TestMintableERC20Token.sol"; |
| 32 | +import {TestUniswapV2Factory} from "../../contracts/test/integration/TestUniswapV2Factory.sol"; |
| 33 | +import {TestUniswapV2Pool} from "../../contracts/test/integration/TestUniswapV2Pool.sol"; |
| 34 | +import {TestUniswapV3Factory} from "../../contracts/test/integration/TestUniswapV3Factory.sol"; |
| 35 | +import {TestUniswapV3Pool} from "../../contracts/test/integration/TestUniswapV3Pool.sol"; |
| 36 | +import {TestLiquidityProvider} from "../../contracts/test/integration/TestLiquidityProvider.sol"; |
| 37 | + |
| 38 | +contract LocalTest is Test, TestUtils { |
| 39 | + uint24 internal constant UNIV3_POOL_FEE = 1234; |
| 40 | + |
| 41 | + DeployZeroEx.ZeroExDeployed internal zeroExDeployed; |
| 42 | + IFlashWallet internal flashWallet; |
| 43 | + IERC20Token internal shib; |
| 44 | + IERC20Token internal dai; |
| 45 | + IERC20Token internal zrx; |
| 46 | + IEtherToken internal weth; |
| 47 | + |
| 48 | + TestUniswapV2Factory internal sushiFactory; |
| 49 | + TestUniswapV2Factory internal uniV2Factory; |
| 50 | + TestUniswapV3Factory internal uniV3Factory; |
| 51 | + TestLiquidityProvider internal liquidityProvider; |
| 52 | + uint256 internal transformerNonce; |
| 53 | + |
| 54 | + address internal signerAddress; |
| 55 | + uint256 internal signerKey; |
| 56 | + |
| 57 | + function _infiniteApprovals() private { |
| 58 | + shib.approve(address(zeroExDeployed.zeroEx), type(uint256).max); |
| 59 | + dai.approve(address(zeroExDeployed.zeroEx), type(uint256).max); |
| 60 | + zrx.approve(address(zeroExDeployed.zeroEx), type(uint256).max); |
| 61 | + weth.approve(address(zeroExDeployed.zeroEx), type(uint256).max); |
| 62 | + } |
| 63 | + |
| 64 | + function setUp() public { |
| 65 | + (signerAddress, signerKey) = _getSigner(); |
| 66 | + |
| 67 | + sushiFactory = new TestUniswapV2Factory(); |
| 68 | + uniV2Factory = new TestUniswapV2Factory(); |
| 69 | + uniV3Factory = new TestUniswapV3Factory(); |
| 70 | + liquidityProvider = new TestLiquidityProvider(); |
| 71 | + |
| 72 | + zeroExDeployed = new DeployZeroEx( |
| 73 | + DeployZeroEx.ZeroExDeployConfiguration({ |
| 74 | + uniswapFactory: address(uniV2Factory), |
| 75 | + sushiswapFactory: address(sushiFactory), |
| 76 | + uniswapV3Factory: address(uniV3Factory), |
| 77 | + uniswapPairInitCodeHash: uniV2Factory.POOL_INIT_CODE_HASH(), |
| 78 | + sushiswapPairInitCodeHash: sushiFactory.POOL_INIT_CODE_HASH(), |
| 79 | + uniswapV3PoolInitCodeHash: uniV3Factory.POOL_INIT_CODE_HASH(), |
| 80 | + logDeployed: false |
| 81 | + }) |
| 82 | + ).deployZeroEx(); |
| 83 | + |
| 84 | + transformerNonce = zeroExDeployed.transformerDeployer.nonce(); |
| 85 | + vm.prank(zeroExDeployed.transformerDeployer.authorities(0)); |
| 86 | + zeroExDeployed.transformerDeployer.deploy(type(TestMintTokenERC20Transformer).creationCode); |
| 87 | + |
| 88 | + flashWallet = zeroExDeployed.zeroEx.getTransformWallet(); |
| 89 | + |
| 90 | + shib = IERC20Token(address(new TestMintableERC20Token())); |
| 91 | + dai = IERC20Token(address(new TestMintableERC20Token())); |
| 92 | + zrx = IERC20Token(address(new TestMintableERC20Token())); |
| 93 | + weth = zeroExDeployed.weth; |
| 94 | + |
| 95 | + _infiniteApprovals(); |
| 96 | + vm.startPrank(signerAddress); |
| 97 | + _infiniteApprovals(); |
| 98 | + vm.stopPrank(); |
| 99 | + |
| 100 | + vm.deal(address(this), 10e18); |
| 101 | + } |
| 102 | + |
| 103 | + function _mintTo(address token, address recipient, uint256 amount) internal { |
| 104 | + if (token == address(weth)) { |
| 105 | + IEtherToken(token).deposit{value: amount}(); |
| 106 | + WETH9V06(payable(token)).transfer(recipient, amount); |
| 107 | + } else { |
| 108 | + TestMintableERC20Token(token).mint(recipient, amount); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + function _createUniswapV2Pool( |
| 113 | + TestUniswapV2Factory factory, |
| 114 | + IERC20Token tokenA, |
| 115 | + IERC20Token tokenB, |
| 116 | + uint112 balanceA, |
| 117 | + uint112 balanceB |
| 118 | + ) internal returns (address poolAddress) { |
| 119 | + TestUniswapV2Pool pool = factory.createPool(tokenA, tokenB); |
| 120 | + _mintTo(address(tokenA), address(pool), balanceA); |
| 121 | + _mintTo(address(tokenB), address(pool), balanceB); |
| 122 | + |
| 123 | + (uint112 balance0, uint112 balance1) = tokenA < tokenB ? (balanceA, balanceB) : (balanceB, balanceA); |
| 124 | + pool.setReserves(balance0, balance1, 0); |
| 125 | + |
| 126 | + return address(pool); |
| 127 | + } |
| 128 | + |
| 129 | + function _createUniswapV3Pool( |
| 130 | + TestUniswapV3Factory factory, |
| 131 | + IERC20Token tokenA, |
| 132 | + IERC20Token tokenB, |
| 133 | + uint112 balanceA, |
| 134 | + uint112 balanceB |
| 135 | + ) internal returns (address poolAddress) { |
| 136 | + poolAddress = address(factory.createPool(tokenA, tokenB, UNIV3_POOL_FEE)); |
| 137 | + _mintTo(address(tokenA), poolAddress, balanceA); |
| 138 | + _mintTo(address(tokenB), poolAddress, balanceB); |
| 139 | + } |
| 140 | +} |
0 commit comments