Skip to content

Commit

Permalink
add withdrawer role
Browse files Browse the repository at this point in the history
  • Loading branch information
kalinbas committed Jul 11, 2023
1 parent 2d08c9c commit 7faa9b9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/automators/AutoExit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ contract AutoExit is Automator {
uint64 token1SlippageX64
);

constructor(INonfungiblePositionManager _npm, address _operator, uint32 _TWAPSeconds, uint16 _maxTWAPTickDifference, uint64 _protocolRewardX64, address[] memory _swapRouterOptions)
Automator(_npm, _operator, _TWAPSeconds, _maxTWAPTickDifference, _protocolRewardX64, _swapRouterOptions) {
constructor(INonfungiblePositionManager _npm, address _operator, address _withdrawer, uint32 _TWAPSeconds, uint16 _maxTWAPTickDifference, uint64 _protocolRewardX64, address[] memory _swapRouterOptions)
Automator(_npm, _operator, _withdrawer, _TWAPSeconds, _maxTWAPTickDifference, _protocolRewardX64, _swapRouterOptions) {
}

// define how stoploss / limit should be handled
Expand Down
4 changes: 2 additions & 2 deletions src/automators/AutoRange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ contract AutoRange is Automator {
uint64 token1SlippageX64
);

constructor(INonfungiblePositionManager _npm, address _operator, uint32 _TWAPSeconds, uint16 _maxTWAPTickDifference, uint64 _protocolRewardX64, address[] memory _swapRouterOptions)
Automator(_npm, _operator, _TWAPSeconds, _maxTWAPTickDifference, _protocolRewardX64, _swapRouterOptions) {
constructor(INonfungiblePositionManager _npm, address _operator, address _withdrawer, uint32 _TWAPSeconds, uint16 _maxTWAPTickDifference, uint64 _protocolRewardX64, address[] memory _swapRouterOptions)
Automator(_npm, _operator, _withdrawer, _TWAPSeconds, _maxTWAPTickDifference, _protocolRewardX64, _swapRouterOptions) {
}

// defines when and how a position can be changed by operator
Expand Down
28 changes: 25 additions & 3 deletions src/automators/Automator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,18 @@ abstract contract Automator is Ownable {

// admin events
event OperatorChanged(address newOperator, bool active);
event WithdrawerChanged(address newWithdrawer);
event TWAPConfigChanged(uint32 TWAPSeconds, uint16 maxTWAPTickDifference);
event SwapRouterChanged(uint8 swapRouterIndex);

// configurable by owner
mapping(address => bool) public operators;
address public withdrawer;
uint32 public TWAPSeconds;
uint16 public maxTWAPTickDifference;
uint8 public swapRouterIndex; // default is 0

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

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

setOperator(_operator, true);
setWithdrawer(_withdrawer);

setTWAPConfig(_maxTWAPTickDifference, _TWAPSeconds);

Expand All @@ -88,6 +91,15 @@ abstract contract Automator is Ownable {
swapRouterIndex = _swapRouterIndex;
}

/**
* @notice Owner controlled function to set withdrawer address
* @param _withdrawer withdrawer
*/
function setWithdrawer(address _withdrawer) public onlyOwner {
emit WithdrawerChanged(_withdrawer);
withdrawer = _withdrawer;
}

/**
* @notice Owner controlled function to activate/deactivate operator address
* @param _operator operator
Expand Down Expand Up @@ -119,7 +131,12 @@ abstract contract Automator is Ownable {
* @param tokens Addresses of tokens to withdraw
* @param to Address to send to
*/
function withdrawBalances(address[] calldata tokens, address to) external onlyOwner {
function withdrawBalances(address[] calldata tokens, address to) external {

if (msg.sender != withdrawer) {
revert Unauthorized();
}

uint i;
uint count = tokens.length;
for(;i < count;++i) {
Expand All @@ -134,7 +151,12 @@ abstract contract Automator is Ownable {
* @notice Withdraws ETH balance
* @param to Address to send to
*/
function withdrawETH(address to) external onlyOwner {
function withdrawETH(address to) external {

if (msg.sender != withdrawer) {
revert Unauthorized();
}

uint256 balance = address(this).balance;
if (balance > 0) {
(bool sent,) = to.call{value: balance}("");
Expand Down
1 change: 1 addition & 0 deletions test/IntegrationTestBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ abstract contract IntegrationTestBase is Test {

address constant WHALE_ACCOUNT = 0xF977814e90dA44bFA03b6295A0616a897441aceC;
address constant OPERATOR_ACCOUNT = 0xF977814e90dA44bFA03b6295A0616a897441aceC;
address constant WITHDRAWER_ACCOUNT = 0xF977814e90dA44bFA03b6295A0616a897441aceC;


address FACTORY = 0x1F98431c8aD98523631AE4a59f267346ea31F984;
Expand Down
6 changes: 4 additions & 2 deletions test/integration/automators/AutoExit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ contract AutoExitTest is IntegrationTestBase {

function setUp() external {
_setupBase();
autoExit = new AutoExit(NPM, OPERATOR_ACCOUNT, 60, 100, uint64(Q64 / 400), _getSwapRouterOptions());
autoExit = new AutoExit(NPM, OPERATOR_ACCOUNT, WITHDRAWER_ACCOUNT, 60, 100, uint64(Q64 / 400), _getSwapRouterOptions());
}

function _setConfig(
Expand Down Expand Up @@ -104,6 +104,7 @@ contract AutoExitTest is IntegrationTestBase {
address[] memory addresses = new address[](2);
addresses[0] = address(DAI);
addresses[1] = address(USDC);
vm.prank(WITHDRAWER_ACCOUNT);
autoExit.withdrawBalances(addresses, address(this));
uint balanceAfter = DAI.balanceOf(address(this));

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

vm.prank(WITHDRAWER_ACCOUNT);
autoExit.withdrawBalances(addresses, address(this));

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

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

vm.prank(TEST_NFT_2_ACCOUNT);
NPM.setApprovalForAll(address(autoExit), true);
Expand Down
4 changes: 2 additions & 2 deletions test/integration/automators/AutoRange.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ contract AutoRangeTest is IntegrationTestBase {

function setUp() external {
_setupBase();
autoRange = new AutoRange(NPM, OPERATOR_ACCOUNT, 60, 100, uint64(Q64 / 400), _getSwapRouterOptions());
autoRange = new AutoRange(NPM, OPERATOR_ACCOUNT, WITHDRAWER_ACCOUNT, 60, 100, uint64(Q64 / 400), _getSwapRouterOptions());
}

function testSetTWAPSeconds() external {
Expand Down Expand Up @@ -330,7 +330,7 @@ contract AutoRangeTest is IntegrationTestBase {
function testOracleCheck() external {

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

vm.prank(TEST_NFT_2_ACCOUNT);
NPM.setApprovalForAll(address(autoRange), true);
Expand Down

0 comments on commit 7faa9b9

Please sign in to comment.