-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwapScript.s.sol
More file actions
259 lines (229 loc) · 11.6 KB
/
SwapScript.s.sol
File metadata and controls
259 lines (229 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "forge-std/Script.sol";
import {Actions} from "@uniswap/v4-periphery/src/libraries/Actions.sol";
import {CommonBase} from "forge-std/Base.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {INetwork} from "./INetwork.sol";
import {IV4Router} from "@uniswap/v4-periphery/src/interfaces/IV4Router.sol";
import {NetworkSelector} from "./NetworkSelector.sol";
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {PredicateMessage} from "@predicate/interfaces/IPredicateClient.sol";
import {Script} from "forge-std/Script.sol";
import {StdChains} from "forge-std/StdChains.sol";
import {StdCheatsSafe} from "forge-std/StdCheats.sol";
import {StdUtils} from "forge-std/StdUtils.sol";
import {Currency} from "@uniswap/v4-core/src/types/Currency.sol";
import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {V4SwapRouter} from "../../src/V4SwapRouter.sol";
contract SwapScript is Script {
enum Case {
SWAP_USDC_FOR_USDL_EXACT_IN,
SWAP_USDL_FOR_USDC_EXACT_IN,
SWAP_USDL_FOR_USDC_EXACT_OUT
}
uint24 lpFee = 0; // 0.30%
int24 tickSpacing = 60;
Currency private _currency0;
Currency private _currency1;
INetwork private _env;
address private _autowrapperHookAddress;
V4SwapRouter private _swapRouter;
string private _case;
function _init() internal {
bool networkExists = vm.envExists("NETWORK");
bool autoWrapperHookAddress = vm.envExists("AUTO_WRAPPER_HOOK_ADDRESS");
bool swapRouterAddressExists = vm.envExists("SWAP_ROUTER_ADDRESS");
bool caseExists = vm.envExists("CASE");
require(
networkExists && autoWrapperHookAddress && swapRouterAddressExists && caseExists,
"All environment variables must be set if any are specified"
);
string memory _network = vm.envString("NETWORK");
_env = new NetworkSelector().select(_network);
_autowrapperHookAddress = vm.envAddress("AUTO_WRAPPER_HOOK_ADDRESS");
_swapRouter = V4SwapRouter(vm.envAddress("SWAP_ROUTER_ADDRESS"));
_case = vm.envString("CASE");
}
function _stringToCase(
string memory _case
) internal returns (Case) {
bytes32 _caseHash = keccak256(abi.encodePacked(_case));
if (_caseHash == keccak256(abi.encodePacked("SWAP_USDC_FOR_USDL_EXACT_IN"))) {
return Case.SWAP_USDC_FOR_USDL_EXACT_IN;
} else if (_caseHash == keccak256(abi.encodePacked("SWAP_USDL_FOR_USDC_EXACT_IN"))) {
return Case.SWAP_USDL_FOR_USDC_EXACT_IN;
} else if (_caseHash == keccak256(abi.encodePacked("SWAP_USDL_FOR_USDC_EXACT_OUT"))) {
return Case.SWAP_USDL_FOR_USDC_EXACT_OUT;
}
revert("Invalid case input");
}
function run() public {
_init();
INetwork.TokenConfig memory tokenConfig = _env.tokenConfig();
vm.label(address(_swapRouter), "SWAP_ROUTER_CONTRACT");
vm.label(_autowrapperHookAddress, "AUTOWRAPPER_HOOK_CONTRACT");
vm.label(Currency.unwrap(tokenConfig.wUSDL), "WRAPPED_USDL_TOKEN");
vm.label(Currency.unwrap(tokenConfig.USDL), "USDL_TOKEN");
vm.label(Currency.unwrap(tokenConfig.USDC), "USDC_TOKEN");
vm.label(address(0x000000000004444c5dc75cB358380D2e3dE08A90), "POOLMANAGER");
vm.label(address(0xf6f4A30EeF7cf51Ed4Ee1415fB3bFDAf3694B0d2), "SERVICEMANAGER_CONTRACT");
_tokenApprovals();
Case _case = _stringToCase(_case);
if (_case == Case.SWAP_USDC_FOR_USDL_EXACT_IN) {
swapUSDCForUSDLExactIn();
} else if (_case == Case.SWAP_USDL_FOR_USDC_EXACT_IN) {
swapUSDLForUSDCExactIn();
} else if (_case == Case.SWAP_USDL_FOR_USDC_EXACT_OUT) {
swapUSDLForUSDCExactOut();
} else {
revert("Invalid case input");
}
}
function _tokenApprovals() internal {
INetwork.TokenConfig memory tokenConfig = _env.tokenConfig();
IERC20 token0 = IERC20(Currency.unwrap(tokenConfig.USDC));
IERC20 token1 = IERC20(Currency.unwrap(tokenConfig.USDL));
vm.startBroadcast();
token0.approve(address(_swapRouter), type(uint256).max);
token1.approve(address(_swapRouter), type(uint256).max);
vm.stopBroadcast();
}
function swapUSDCForUSDLExactIn() public {
uint256 balanceBeforeSwapUSDC = IERC20(Currency.unwrap(_env.tokenConfig().USDC)).balanceOf(msg.sender);
uint256 balanceBeforeSwapUSDL = IERC20(Currency.unwrap(_env.tokenConfig().USDL)).balanceOf(msg.sender);
INetwork.TokenConfig memory tokenConfig = _env.tokenConfig();
uint128 amountIn = 1e6; // 1 USDC
uint128 amountOutMin = 1e17; // accepts min 0.1 USDL out
PoolKey memory key = PoolKey({
currency0: tokenConfig.USDC,
currency1: tokenConfig.USDL,
fee: lpFee,
tickSpacing: tickSpacing,
hooks: IHooks(_autowrapperHookAddress)
});
IV4Router.ExactInputSingleParams memory swapParams = IV4Router.ExactInputSingleParams({
poolKey: key,
zeroForOne: true,
amountIn: amountIn,
amountOutMinimum: amountOutMin,
hookData: abi.encode("0x")
});
bytes memory actions =
abi.encodePacked(uint8(Actions.SWAP_EXACT_IN_SINGLE), uint8(Actions.SETTLE_ALL), uint8(Actions.TAKE_ALL));
bytes[] memory params = new bytes[](3);
params[0] = abi.encode(swapParams); // swap params
params[1] = abi.encode(key.currency0, amountIn); // settle currency0
params[2] = abi.encode(key.currency1, amountOutMin); // take currency1
vm.startBroadcast();
_swapRouter.execute(abi.encode(actions, params));
vm.stopBroadcast();
uint256 balanceAfterSwapUSDC = IERC20(Currency.unwrap(_env.tokenConfig().USDC)).balanceOf(msg.sender);
uint256 balanceAfterSwapUSDL = IERC20(Currency.unwrap(_env.tokenConfig().USDL)).balanceOf(msg.sender);
console.log("======================================");
console.log(" Exact-In Swap: 1 USDC to USDL ");
console.log("======================================");
console.log(" Balances Before Swap:");
console.log(" USDC: %s", balanceBeforeSwapUSDC / 1e6);
console.log(" USDL: %s", balanceBeforeSwapUSDL / 1e18);
console.log("--------------------------------------");
console.log(" Balances After Swap:");
console.log(" USDC: %s", balanceAfterSwapUSDC / 1e6);
console.log(" USDL: %s", balanceAfterSwapUSDL / 1e18);
console.log("======================================");
}
function swapUSDLForUSDCExactIn() public {
uint256 balanceBeforeSwapUSDC = IERC20(Currency.unwrap(_env.tokenConfig().USDC)).balanceOf(msg.sender);
uint256 balanceBeforeSwapUSDL = IERC20(Currency.unwrap(_env.tokenConfig().USDL)).balanceOf(msg.sender);
INetwork.TokenConfig memory tokenConfig = _env.tokenConfig();
uint128 amountIn = 1e18; // 1 USDL
uint128 amountOutMin = 9e5; // accepts min 0.9 USDC out
PoolKey memory key = PoolKey({
currency0: tokenConfig.USDC,
currency1: tokenConfig.USDL,
fee: lpFee,
tickSpacing: tickSpacing,
hooks: IHooks(_autowrapperHookAddress)
});
IV4Router.ExactInputSingleParams memory swapParams = IV4Router.ExactInputSingleParams({
poolKey: key,
zeroForOne: false,
amountIn: amountIn,
amountOutMinimum: amountOutMin,
hookData: abi.encode("0x")
});
bytes memory actions = abi.encodePacked(
uint8(Actions.SETTLE), uint8(Actions.SWAP_EXACT_IN_SINGLE), uint8(Actions.TAKE_ALL), uint8(Actions.TAKE_ALL)
);
bytes[] memory params = new bytes[](4);
params[0] = abi.encode(key.currency1, amountIn + 1, true); // settle currency1
params[1] = abi.encode(swapParams); // swap params
params[2] = abi.encode(key.currency0, amountOutMin); // take currency0
params[3] = abi.encode(key.currency1, 0); // take currency1
vm.startBroadcast();
_swapRouter.execute(abi.encode(actions, params));
vm.stopBroadcast();
uint256 balanceAfterSwapUSDC = IERC20(Currency.unwrap(_env.tokenConfig().USDC)).balanceOf(msg.sender);
uint256 balanceAfterSwapUSDL = IERC20(Currency.unwrap(_env.tokenConfig().USDL)).balanceOf(msg.sender);
console.log("======================================");
console.log(" Exact-In Swap: 1 USDL to USDC ");
console.log("======================================");
console.log(" Balances Before Swap:");
console.log(" USDC: %s", balanceBeforeSwapUSDC / 1e6);
console.log(" USDL: %s", balanceBeforeSwapUSDL / 1e18);
console.log("--------------------------------------");
console.log(" Balances After Swap:");
console.log(" USDC: %s", balanceAfterSwapUSDC / 1e6);
console.log(" USDL: %s", balanceAfterSwapUSDL / 1e18);
console.log("======================================");
}
function swapUSDLForUSDCExactOut() public {
uint256 balanceBeforeSwapUSDC = IERC20(Currency.unwrap(_env.tokenConfig().USDC)).balanceOf(msg.sender);
uint256 balanceBeforeSwapUSDL = IERC20(Currency.unwrap(_env.tokenConfig().USDL)).balanceOf(msg.sender);
INetwork.TokenConfig memory tokenConfig = _env.tokenConfig();
uint128 amountOut = 1e6; // 1 USDC
uint128 amountInMax = 2e18; // accepts max 2 USDL in
PoolKey memory key = PoolKey({
currency0: tokenConfig.USDC,
currency1: tokenConfig.USDL,
fee: lpFee,
tickSpacing: tickSpacing,
hooks: IHooks(_autowrapperHookAddress)
});
IV4Router.ExactOutputSingleParams memory swapParams = IV4Router.ExactOutputSingleParams({
poolKey: key,
zeroForOne: false,
amountOut: amountOut,
amountInMaximum: amountInMax,
hookData: abi.encode("0x")
});
bytes memory actions = abi.encodePacked(
uint8(Actions.SETTLE),
uint8(Actions.SWAP_EXACT_OUT_SINGLE),
uint8(Actions.TAKE_ALL),
uint8(Actions.TAKE_ALL)
);
bytes[] memory params = new bytes[](4);
params[0] = abi.encode(key.currency1, amountInMax, true); // settle currency1
params[1] = abi.encode(swapParams); // swap params
params[2] = abi.encode(key.currency0, amountOut); // take currency0
params[3] = abi.encode(key.currency1, 0); // take currency1
vm.startBroadcast();
_swapRouter.execute(abi.encode(actions, params));
vm.stopBroadcast();
uint256 balanceAfterSwapUSDC = IERC20(Currency.unwrap(_env.tokenConfig().USDC)).balanceOf(msg.sender);
uint256 balanceAfterSwapUSDL = IERC20(Currency.unwrap(_env.tokenConfig().USDL)).balanceOf(msg.sender);
console.log("======================================");
console.log(" Exact-Out Swap: USDL for 1 USDC ");
console.log("======================================");
console.log(" Balances Before Swap:");
console.log(" USDC: %s", balanceBeforeSwapUSDC / 1e6);
console.log(" USDL: %s", balanceBeforeSwapUSDL / 1e18);
console.log("--------------------------------------");
console.log(" Balances After Swap:");
console.log(" USDC: %s", balanceAfterSwapUSDC / 1e6);
console.log(" USDL: %s", balanceAfterSwapUSDL / 1e18);
console.log("======================================");
}
}