Skip to content

Commit e2ed864

Browse files
committed
fea: add withdraw methods for buyback
1 parent 395064d commit e2ed864

4 files changed

Lines changed: 60 additions & 0 deletions

File tree

contracts/buyback/Buyback.sol

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,20 @@ contract Buyback is
258258
emit EmergencyWithdraw(_token, _amount);
259259
}
260260

261+
/**
262+
* @dev allows bot to withdraw tokens to the receiver address
263+
* @param _token token address
264+
* @param _amount token amount
265+
*/
266+
function withdraw(address _token, uint256 _amount) external onlyRole(BOT) {
267+
if (_token == address(0)) {
268+
(bool success, ) = payable(receiver).call{ value: _amount }("");
269+
require(success, "Withdraw failed");
270+
} else {
271+
IERC20(_token).safeTransfer(receiver, _amount);
272+
}
273+
}
274+
261275
/**
262276
* @dev pause the contract
263277
*/

contracts/dao/ListaAutoBuyback.sol

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,20 @@ contract ListaAutoBuyback is Initializable, AccessControlUpgradeable {
183183
emit AdminTransfer(_token, _amount);
184184
}
185185

186+
/**
187+
* @dev allow bot to withdraw token to the defaultReceiver address
188+
* @param _token token address
189+
* @param _amount token amount
190+
*/
191+
function withdraw(address _token, uint256 _amount) external onlyRole(BOT) {
192+
if (_token == address(0)) {
193+
(bool success, ) = payable(defaultReceiver).call{ value: _amount }("");
194+
require(success, "Withdraw failed");
195+
} else {
196+
IERC20(_token).safeTransfer(defaultReceiver, _amount);
197+
}
198+
}
199+
186200
function changeDefaultReceiver(address _receiver) external onlyRole(DEFAULT_ADMIN_ROLE) {
187201
require(_receiver != address(0), "_receiver is the zero address");
188202
require(_receiver != defaultReceiver, "_receiver is the same");

test/buyback/Buyback.t.sol

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,20 @@ contract BuybackTest is Test {
358358
buyback.buyback(pancakeRouter, buyback.SWAP_NATIVE_TOKEN_ADDRESS(), tokenOut, 1 ether, 0, data);
359359
vm.stopPrank();
360360
}
361+
362+
function test_withdraw() public {
363+
deal(tokenIn, address(buyback), 1 ether);
364+
vm.deal(address(buyback), 1 ether);
365+
366+
uint256 lisUSDBefore = IERC20(tokenIn).balanceOf(buyback.receiver());
367+
uint256 bnbBefore = buyback.receiver().balance;
368+
369+
vm.startPrank(bot);
370+
buyback.withdraw(tokenIn, 1 ether);
371+
assertEq(IERC20(tokenIn).balanceOf(buyback.receiver()) - lisUSDBefore, 1 ether, "lisUSD withdraw failed");
372+
373+
buyback.withdraw(address(0), 1 ether);
374+
assertEq(buyback.receiver().balance - bnbBefore, 1 ether, "BNB withdraw failed");
375+
vm.stopPrank();
376+
}
361377
}

test/dao/ListaAutoBuyback.t.sol

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,20 @@ contract ListaAutoBuyBackTest is Test {
225225
listaAutoBuyBack.buyback(pancakeRouter, listaAutoBuyBack.SWAP_NATIVE_TOKEN_ADDRESS(), USDT, 1 ether, 0, data);
226226
vm.stopPrank();
227227
}
228+
229+
function test_withdraw() public {
230+
deal(lisUSD, address(listaAutoBuyBack), 1 ether);
231+
vm.deal(address(listaAutoBuyBack), 1 ether);
232+
233+
uint256 lisUSDBefore = IERC20(lisUSD).balanceOf(listaAutoBuyBack.defaultReceiver());
234+
uint256 bnbBefore = listaAutoBuyBack.defaultReceiver().balance;
235+
236+
vm.startPrank(bot);
237+
listaAutoBuyBack.withdraw(lisUSD, 1 ether);
238+
assertEq(IERC20(lisUSD).balanceOf(listaAutoBuyBack.defaultReceiver()) - lisUSDBefore, 1 ether, "lisUSD withdraw failed");
239+
240+
listaAutoBuyBack.withdraw(address(0), 1 ether);
241+
assertEq(listaAutoBuyBack.defaultReceiver().balance - bnbBefore, 1 ether, "BNB withdraw failed");
242+
vm.stopPrank();
243+
}
228244
}

0 commit comments

Comments
 (0)