-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAngstromRouterAndHook.sol
More file actions
219 lines (192 loc) · 7.61 KB
/
AngstromRouterAndHook.sol
File metadata and controls
219 lines (192 loc) · 7.61 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
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.24;
import { IPermit2 } from "permit2/src/interfaces/IPermit2.sol";
import { IBatchRouterQueries } from "@balancer-labs/v3-interfaces/contracts/vault/IBatchRouterQueries.sol";
import { IWETH } from "@balancer-labs/v3-interfaces/contracts/solidity-utils/misc/IWETH.sol";
import { IBatchRouter } from "@balancer-labs/v3-interfaces/contracts/vault/IBatchRouter.sol";
import { IVault } from "@balancer-labs/v3-interfaces/contracts/vault/IVault.sol";
import {
SwapPathExactAmountIn,
SwapPathExactAmountOut,
SwapExactInHookParams,
SwapExactOutHookParams
} from "@balancer-labs/v3-interfaces/contracts/vault/BatchRouterTypes.sol";
import { SingletonAuthentication } from "@balancer-labs/v3-vault/contracts/SingletonAuthentication.sol";
import { BatchRouterHooks } from "@balancer-labs/v3-vault/contracts/BatchRouterHooks.sol";
import { RouterCommon } from "@balancer-labs/v3-vault/contracts/RouterCommon.sol";
contract AngstromRouterAndHook is IBatchRouter, BatchRouterHooks, SingletonAuthentication {
uint256 internal _lastUnlockBlockNumber;
error OnlyOncePerBlock();
error NotNode();
mapping(address => bool) internal _nodes;
constructor(
IVault vault,
IWETH weth,
IPermit2 permit2,
string memory routerVersion
) BatchRouterHooks(vault, weth, permit2, routerVersion) SingletonAuthentication(vault) {
// solhint-disable-previous-line no-empty-blocks
}
/***************************************************************************
Swaps
***************************************************************************/
/// @inheritdoc IBatchRouter
function swapExactIn(
SwapPathExactAmountIn[] memory paths,
uint256 deadline,
bool wethIsEth,
bytes calldata userData
)
external
payable
saveSender(msg.sender)
returns (uint256[] memory pathAmountsOut, address[] memory tokensOut, uint256[] memory amountsOut)
{
// Unlocks the Angstrom network in this block. If the Angstrom network is already unlocked, reverts.
_unlockAngstrom();
return
abi.decode(
_vault.unlock(
abi.encodeCall(
BatchRouterHooks.swapExactInHook,
SwapExactInHookParams({
sender: msg.sender,
paths: paths,
deadline: deadline,
wethIsEth: wethIsEth,
userData: userData
})
)
),
(uint256[], address[], uint256[])
);
}
/// @inheritdoc IBatchRouter
function swapExactOut(
SwapPathExactAmountOut[] memory paths,
uint256 deadline,
bool wethIsEth,
bytes calldata userData
)
external
payable
saveSender(msg.sender)
returns (uint256[] memory pathAmountsIn, address[] memory tokensIn, uint256[] memory amountsIn)
{
// Unlocks the Angstrom Network in this block. If the Angstrom Network is already unlocked, reverts.
_unlockAngstrom();
return
abi.decode(
_vault.unlock(
abi.encodeCall(
BatchRouterHooks.swapExactOutHook,
SwapExactOutHookParams({
sender: msg.sender,
paths: paths,
deadline: deadline,
wethIsEth: wethIsEth,
userData: userData
})
)
),
(uint256[], address[], uint256[])
);
}
/***************************************************************************
Queries
***************************************************************************/
/// @inheritdoc IBatchRouterQueries
function querySwapExactIn(
SwapPathExactAmountIn[] memory paths,
address sender,
bytes calldata userData
)
external
saveSender(sender)
returns (uint256[] memory pathAmountsOut, address[] memory tokensOut, uint256[] memory amountsOut)
{
for (uint256 i = 0; i < paths.length; ++i) {
paths[i].minAmountOut = 0;
}
return
abi.decode(
_vault.quote(
abi.encodeCall(
BatchRouterHooks.querySwapExactInHook,
SwapExactInHookParams({
sender: address(this),
paths: paths,
deadline: type(uint256).max,
wethIsEth: false,
userData: userData
})
)
),
(uint256[], address[], uint256[])
);
}
/// @inheritdoc IBatchRouterQueries
function querySwapExactOut(
SwapPathExactAmountOut[] memory paths,
address sender,
bytes calldata userData
)
external
saveSender(sender)
returns (uint256[] memory pathAmountsIn, address[] memory tokensIn, uint256[] memory amountsIn)
{
for (uint256 i = 0; i < paths.length; ++i) {
paths[i].maxAmountIn = _MAX_AMOUNT;
}
return
abi.decode(
_vault.quote(
abi.encodeCall(
BatchRouterHooks.querySwapExactOutHook,
SwapExactOutHookParams({
sender: address(this),
paths: paths,
deadline: type(uint256).max,
wethIsEth: false,
userData: userData
})
)
),
(uint256[], address[], uint256[])
);
}
/***************************************************************************
Nodes Management
***************************************************************************/
function toggleNodes(address[] memory nodes) external authenticate {
for (uint256 i = 0; i < nodes.length; i++) {
_nodes[nodes[i]] = !_nodes[nodes[i]];
}
}
/***************************************************************************
Getters
***************************************************************************/
function getVault() public view override(RouterCommon, SingletonAuthentication) returns (IVault) {
return _vault;
}
/***************************************************************************
Private Functions
***************************************************************************/
function _unlockAngstrom() internal {
// The router can only be unlocked once per block.
if (_isAngstromUnlocked()) {
revert OnlyOncePerBlock();
}
// The "unlocker" must be a registered node of the Angstrom network.
if (_isNode(msg.sender) == false) {
revert NotNode();
}
_lastUnlockBlockNumber = block.number;
}
function _isNode(address account) internal view returns (bool) {
return _nodes[account];
}
function _isAngstromUnlocked() internal view returns (bool) {
return _lastUnlockBlockNumber == block.number;
}
}