-
Notifications
You must be signed in to change notification settings - Fork 48
chore: migrate E2E buyEth tests to Foundry #212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
meetmangukiya
wants to merge
4
commits into
cowprotocol:mfw78-patch-1
from
meetmangukiya:e2e-migrate-buy-eth
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| // SPDX-License-Identifier: LGPL-3.0-or-later | ||
| pragma solidity ^0.8; | ||
|
|
||
| import {Vm} from "forge-std/Vm.sol"; | ||
|
|
||
| import {IERC20} from "src/contracts/interfaces/IERC20.sol"; | ||
| import {GPv2Transfer} from "src/contracts/libraries/GPv2Transfer.sol"; | ||
|
|
||
| import { | ||
| GPv2Interaction, GPv2Order, GPv2Signing, SettlementEncoder | ||
| } from "test/libraries/encoders/SettlementEncoder.sol"; | ||
| import {Registry, TokenRegistry} from "test/libraries/encoders/TokenRegistry.sol"; | ||
|
|
||
| import {Helper} from "./Helper.sol"; | ||
|
|
||
| interface IUSDT { | ||
| function getOwner() external view returns (address); | ||
| function issue(uint256) external; | ||
| // approve and transfer doesn't return the bool for USDT | ||
| function approve(address, uint256) external; | ||
| function transfer(address, uint256) external; | ||
| } | ||
|
|
||
| IUSDT constant USDT = IUSDT(0xdAC17F958D2ee523a2206206994597C13D831ec7); | ||
| IERC20 constant WETH = IERC20(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2); | ||
|
|
||
| using SettlementEncoder for SettlementEncoder.State; | ||
| using TokenRegistry for TokenRegistry.State; | ||
| using TokenRegistry for Registry; | ||
|
|
||
| contract BuyEthTest is Helper(true) { | ||
| // Settle a trivial batch between two overlapping trades: | ||
| // | ||
| // /----(1. SELL 1 WETH for USDT if p(WETH) >= 1100)----\ | ||
| // | v | ||
| // [USDT] [(W)ETH] | ||
| // ^ | | ||
| // \-----(2. BUY 1 ETH for USDT if p(WETH) <= 1200)-----/ | ||
| function test_should_unwrap_weth_for_eth_buy_orders() external { | ||
| Vm.Wallet memory trader1 = vm.createWallet("trader1"); | ||
| Vm.Wallet memory trader2 = vm.createWallet("trader2"); | ||
|
|
||
| // give some weth to trader1 | ||
| deal(address(WETH), trader1.addr, 1.001 ether); | ||
| // approve weth for trading on the vault | ||
| vm.prank(trader1.addr); | ||
| WETH.approve(vaultRelayer, type(uint256).max); | ||
| // place the weth to usdt swap order | ||
| encoder.signEncodeTrade( | ||
| vm, | ||
| trader1, | ||
| GPv2Order.Data({ | ||
| sellToken: WETH, | ||
| buyToken: IERC20(address(USDT)), | ||
| receiver: trader1.addr, | ||
| sellAmount: 1 ether, | ||
| buyAmount: 1100e6, | ||
| validTo: 0xffffffff, | ||
| appData: bytes32(uint256(1)), | ||
| feeAmount: 0.001 ether, | ||
| kind: GPv2Order.KIND_SELL, | ||
| partiallyFillable: false, | ||
| sellTokenBalance: GPv2Order.BALANCE_ERC20, | ||
| buyTokenBalance: GPv2Order.BALANCE_ERC20 | ||
| }), | ||
| domainSeparator, | ||
| GPv2Signing.Scheme.Eip712, | ||
| 0 | ||
| ); | ||
|
|
||
| // give some usdt to trader2 | ||
| _mintUsdt(trader2.addr, 1201.2e6); | ||
| // approve usdt for trading on the vault | ||
| vm.prank(trader2.addr); | ||
| USDT.approve(vaultRelayer, type(uint256).max); | ||
| // place the usdt to eth swap order | ||
| encoder.signEncodeTrade( | ||
| vm, | ||
| trader2, | ||
| GPv2Order.Data({ | ||
| sellToken: IERC20(address(USDT)), | ||
| buyToken: IERC20(GPv2Transfer.BUY_ETH_ADDRESS), | ||
| receiver: trader2.addr, | ||
| sellAmount: 1200e6, | ||
| buyAmount: 1 ether, | ||
| validTo: 0xffffffff, | ||
| appData: bytes32(uint256(1)), | ||
| feeAmount: 1.2e6, | ||
| kind: GPv2Order.KIND_BUY, | ||
| partiallyFillable: false, | ||
| sellTokenBalance: GPv2Order.BALANCE_ERC20, | ||
| buyTokenBalance: GPv2Order.BALANCE_ERC20 | ||
| }), | ||
| domainSeparator, | ||
| GPv2Signing.Scheme.Eip712, | ||
| 0 | ||
| ); | ||
|
|
||
| // encode the weth withdraw interaction | ||
| encoder.addInteraction( | ||
| GPv2Interaction.Data({ | ||
| target: address(WETH), | ||
| value: 0, | ||
| callData: abi.encodeWithSignature("withdraw(uint256)", 1 ether) | ||
| }), | ||
| SettlementEncoder.InteractionStage.INTRA | ||
| ); | ||
|
|
||
| // set the token prices | ||
| IERC20[] memory tokens = new IERC20[](3); | ||
| tokens[0] = WETH; | ||
| tokens[1] = IERC20(GPv2Transfer.BUY_ETH_ADDRESS); | ||
| tokens[2] = IERC20(address(USDT)); | ||
| uint256[] memory prices = new uint256[](3); | ||
| prices[0] = 1150e6; | ||
| prices[1] = 1150e6; | ||
| prices[2] = 1 ether; | ||
| encoder.tokenRegistry.tokenRegistry().setPrices(tokens, prices); | ||
|
|
||
| SettlementEncoder.EncodedSettlement memory encodedSettlement = encoder.encode(settlement); | ||
|
|
||
| uint256 trader2InitialBalance = trader2.addr.balance; | ||
| vm.prank(solver); | ||
| settle(encodedSettlement); | ||
| assertEq( | ||
| WETH.balanceOf(address(settlement)), | ||
| 0.001 ether, | ||
| "settlement contract's weth balance from trade fee not as expected" | ||
| ); // the fee | ||
| assertEq(WETH.balanceOf(trader1.addr), 0, "trader1 weth balance is not 0"); | ||
| assertEq( | ||
| trader2.addr.balance, trader2InitialBalance + 1 ether, "trader2 eth balance did not increase as expected" | ||
| ); | ||
| } | ||
|
|
||
| function _mintUsdt(address receiver, uint256 amt) internal { | ||
| address owner = USDT.getOwner(); | ||
| vm.startPrank(owner); | ||
| USDT.issue(amt); | ||
| USDT.transfer(receiver, amt); | ||
| vm.stopPrank(); | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.