Skip to content

Commit 7faa9b9

Browse files
committed
add withdrawer role
1 parent 2d08c9c commit 7faa9b9

File tree

6 files changed

+36
-11
lines changed

6 files changed

+36
-11
lines changed

src/automators/AutoExit.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ contract AutoExit is Automator {
3232
uint64 token1SlippageX64
3333
);
3434

35-
constructor(INonfungiblePositionManager _npm, address _operator, uint32 _TWAPSeconds, uint16 _maxTWAPTickDifference, uint64 _protocolRewardX64, address[] memory _swapRouterOptions)
36-
Automator(_npm, _operator, _TWAPSeconds, _maxTWAPTickDifference, _protocolRewardX64, _swapRouterOptions) {
35+
constructor(INonfungiblePositionManager _npm, address _operator, address _withdrawer, uint32 _TWAPSeconds, uint16 _maxTWAPTickDifference, uint64 _protocolRewardX64, address[] memory _swapRouterOptions)
36+
Automator(_npm, _operator, _withdrawer, _TWAPSeconds, _maxTWAPTickDifference, _protocolRewardX64, _swapRouterOptions) {
3737
}
3838

3939
// define how stoploss / limit should be handled

src/automators/AutoRange.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ contract AutoRange is Automator {
2727
uint64 token1SlippageX64
2828
);
2929

30-
constructor(INonfungiblePositionManager _npm, address _operator, uint32 _TWAPSeconds, uint16 _maxTWAPTickDifference, uint64 _protocolRewardX64, address[] memory _swapRouterOptions)
31-
Automator(_npm, _operator, _TWAPSeconds, _maxTWAPTickDifference, _protocolRewardX64, _swapRouterOptions) {
30+
constructor(INonfungiblePositionManager _npm, address _operator, address _withdrawer, uint32 _TWAPSeconds, uint16 _maxTWAPTickDifference, uint64 _protocolRewardX64, address[] memory _swapRouterOptions)
31+
Automator(_npm, _operator, _withdrawer, _TWAPSeconds, _maxTWAPTickDifference, _protocolRewardX64, _swapRouterOptions) {
3232
}
3333

3434
// defines when and how a position can be changed by operator

src/automators/Automator.sol

+25-3
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,18 @@ abstract contract Automator is Ownable {
4444

4545
// admin events
4646
event OperatorChanged(address newOperator, bool active);
47+
event WithdrawerChanged(address newWithdrawer);
4748
event TWAPConfigChanged(uint32 TWAPSeconds, uint16 maxTWAPTickDifference);
4849
event SwapRouterChanged(uint8 swapRouterIndex);
4950

5051
// configurable by owner
5152
mapping(address => bool) public operators;
53+
address public withdrawer;
5254
uint32 public TWAPSeconds;
5355
uint16 public maxTWAPTickDifference;
5456
uint8 public swapRouterIndex; // default is 0
5557

56-
constructor(INonfungiblePositionManager npm, address _operator, uint32 _TWAPSeconds, uint16 _maxTWAPTickDifference, uint64 _protocolRewardX64, address[] memory _swapRouterOptions) {
58+
constructor(INonfungiblePositionManager npm, address _operator, address _withdrawer, uint32 _TWAPSeconds, uint16 _maxTWAPTickDifference, uint64 _protocolRewardX64, address[] memory _swapRouterOptions) {
5759

5860
nonfungiblePositionManager = npm;
5961
weth = IWETH9(npm.WETH9());
@@ -67,6 +69,7 @@ abstract contract Automator is Ownable {
6769
emit SwapRouterChanged(0);
6870

6971
setOperator(_operator, true);
72+
setWithdrawer(_withdrawer);
7073

7174
setTWAPConfig(_maxTWAPTickDifference, _TWAPSeconds);
7275

@@ -88,6 +91,15 @@ abstract contract Automator is Ownable {
8891
swapRouterIndex = _swapRouterIndex;
8992
}
9093

94+
/**
95+
* @notice Owner controlled function to set withdrawer address
96+
* @param _withdrawer withdrawer
97+
*/
98+
function setWithdrawer(address _withdrawer) public onlyOwner {
99+
emit WithdrawerChanged(_withdrawer);
100+
withdrawer = _withdrawer;
101+
}
102+
91103
/**
92104
* @notice Owner controlled function to activate/deactivate operator address
93105
* @param _operator operator
@@ -119,7 +131,12 @@ abstract contract Automator is Ownable {
119131
* @param tokens Addresses of tokens to withdraw
120132
* @param to Address to send to
121133
*/
122-
function withdrawBalances(address[] calldata tokens, address to) external onlyOwner {
134+
function withdrawBalances(address[] calldata tokens, address to) external {
135+
136+
if (msg.sender != withdrawer) {
137+
revert Unauthorized();
138+
}
139+
123140
uint i;
124141
uint count = tokens.length;
125142
for(;i < count;++i) {
@@ -134,7 +151,12 @@ abstract contract Automator is Ownable {
134151
* @notice Withdraws ETH balance
135152
* @param to Address to send to
136153
*/
137-
function withdrawETH(address to) external onlyOwner {
154+
function withdrawETH(address to) external {
155+
156+
if (msg.sender != withdrawer) {
157+
revert Unauthorized();
158+
}
159+
138160
uint256 balance = address(this).balance;
139161
if (balance > 0) {
140162
(bool sent,) = to.call{value: balance}("");

test/IntegrationTestBase.sol

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ abstract contract IntegrationTestBase is Test {
1919

2020
address constant WHALE_ACCOUNT = 0xF977814e90dA44bFA03b6295A0616a897441aceC;
2121
address constant OPERATOR_ACCOUNT = 0xF977814e90dA44bFA03b6295A0616a897441aceC;
22+
address constant WITHDRAWER_ACCOUNT = 0xF977814e90dA44bFA03b6295A0616a897441aceC;
2223

2324

2425
address FACTORY = 0x1F98431c8aD98523631AE4a59f267346ea31F984;

test/integration/automators/AutoExit.t.sol

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ contract AutoExitTest is IntegrationTestBase {
1111

1212
function setUp() external {
1313
_setupBase();
14-
autoExit = new AutoExit(NPM, OPERATOR_ACCOUNT, 60, 100, uint64(Q64 / 400), _getSwapRouterOptions());
14+
autoExit = new AutoExit(NPM, OPERATOR_ACCOUNT, WITHDRAWER_ACCOUNT, 60, 100, uint64(Q64 / 400), _getSwapRouterOptions());
1515
}
1616

1717
function _setConfig(
@@ -104,6 +104,7 @@ contract AutoExitTest is IntegrationTestBase {
104104
address[] memory addresses = new address[](2);
105105
addresses[0] = address(DAI);
106106
addresses[1] = address(USDC);
107+
vm.prank(WITHDRAWER_ACCOUNT);
107108
autoExit.withdrawBalances(addresses, address(this));
108109
uint balanceAfter = DAI.balanceOf(address(this));
109110

@@ -136,6 +137,7 @@ contract AutoExitTest is IntegrationTestBase {
136137
// protocol fee
137138
balanceBefore = USDC.balanceOf(address(this));
138139

140+
vm.prank(WITHDRAWER_ACCOUNT);
139141
autoExit.withdrawBalances(addresses, address(this));
140142

141143
balanceAfter = USDC.balanceOf(address(this));
@@ -257,7 +259,7 @@ contract AutoExitTest is IntegrationTestBase {
257259
function testOracleCheck() external {
258260

259261
// create range adjustor with more strict oracle config
260-
autoExit = new AutoExit(NPM, OPERATOR_ACCOUNT, 60 * 30, 4, uint64(Q64 / 400), _getSwapRouterOptions());
262+
autoExit = new AutoExit(NPM, OPERATOR_ACCOUNT, WITHDRAWER_ACCOUNT, 60 * 30, 4, uint64(Q64 / 400), _getSwapRouterOptions());
261263

262264
vm.prank(TEST_NFT_2_ACCOUNT);
263265
NPM.setApprovalForAll(address(autoExit), true);

test/integration/automators/AutoRange.t.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ contract AutoRangeTest is IntegrationTestBase {
1313

1414
function setUp() external {
1515
_setupBase();
16-
autoRange = new AutoRange(NPM, OPERATOR_ACCOUNT, 60, 100, uint64(Q64 / 400), _getSwapRouterOptions());
16+
autoRange = new AutoRange(NPM, OPERATOR_ACCOUNT, WITHDRAWER_ACCOUNT, 60, 100, uint64(Q64 / 400), _getSwapRouterOptions());
1717
}
1818

1919
function testSetTWAPSeconds() external {
@@ -330,7 +330,7 @@ contract AutoRangeTest is IntegrationTestBase {
330330
function testOracleCheck() external {
331331

332332
// create range adjustor with more strict oracle config
333-
autoRange = new AutoRange(NPM, OPERATOR_ACCOUNT, 60 * 30, 4, uint64(Q64 / 400), _getSwapRouterOptions());
333+
autoRange = new AutoRange(NPM, OPERATOR_ACCOUNT, WITHDRAWER_ACCOUNT, 60 * 30, 4, uint64(Q64 / 400), _getSwapRouterOptions());
334334

335335
vm.prank(TEST_NFT_2_ACCOUNT);
336336
NPM.setApprovalForAll(address(autoRange), true);

0 commit comments

Comments
 (0)