Skip to content

Commit c8be75b

Browse files
authored
Merge pull request #98 from lista-dao/feature/buyback-v3
fea: add withdraw methods for buyback
2 parents 017d7c3 + 3af173e commit c8be75b

4 files changed

Lines changed: 70 additions & 0 deletions

File tree

contracts/buyback/Buyback.sol

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,25 @@ 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+
require(
268+
tokenInWhitelist[_token] || _token == SWAP_NATIVE_TOKEN_ADDRESS || _token == tokenOut,
269+
"Token not whitelisted"
270+
);
271+
require(_amount > 0, "Invalid amount");
272+
if (_token == SWAP_NATIVE_TOKEN_ADDRESS) {
273+
(bool success, ) = payable(receiver).call{ value: _amount }("");
274+
require(success, "Withdraw failed");
275+
} else {
276+
IERC20(_token).safeTransfer(receiver, _amount);
277+
}
278+
}
279+
261280
/**
262281
* @dev pause the contract
263282
*/

contracts/dao/ListaAutoBuyback.sol

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,22 @@ 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+
require(tokenWhitelist[_token] || _token == SWAP_NATIVE_TOKEN_ADDRESS, "Token not whitelisted");
193+
require(_amount > 0, "Invalid amount");
194+
if (_token == SWAP_NATIVE_TOKEN_ADDRESS) {
195+
(bool success, ) = payable(defaultReceiver).call{ value: _amount }("");
196+
require(success, "Withdraw failed");
197+
} else {
198+
IERC20(_token).safeTransfer(defaultReceiver, _amount);
199+
}
200+
}
201+
186202
function changeDefaultReceiver(address _receiver) external onlyRole(DEFAULT_ADMIN_ROLE) {
187203
require(_receiver != address(0), "_receiver is the zero address");
188204
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(buyback.SWAP_NATIVE_TOKEN_ADDRESS(), 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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,23 @@ 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+
vm.startPrank(admin);
233+
listaAutoBuyBack.setTokenWhitelist(lisUSD, true);
234+
vm.stopPrank();
235+
236+
uint256 lisUSDBefore = IERC20(lisUSD).balanceOf(listaAutoBuyBack.defaultReceiver());
237+
uint256 bnbBefore = listaAutoBuyBack.defaultReceiver().balance;
238+
239+
vm.startPrank(bot);
240+
listaAutoBuyBack.withdraw(lisUSD, 1 ether);
241+
assertEq(IERC20(lisUSD).balanceOf(listaAutoBuyBack.defaultReceiver()) - lisUSDBefore, 1 ether, "lisUSD withdraw failed");
242+
243+
listaAutoBuyBack.withdraw(listaAutoBuyBack.SWAP_NATIVE_TOKEN_ADDRESS(), 1 ether);
244+
assertEq(listaAutoBuyBack.defaultReceiver().balance - bnbBefore, 1 ether, "BNB withdraw failed");
245+
vm.stopPrank();
246+
}
228247
}

0 commit comments

Comments
 (0)