Skip to content

Commit

Permalink
+ add changeRevenuePool function
Browse files Browse the repository at this point in the history
! unit test
  • Loading branch information
tedw-lista committed Sep 13, 2024
1 parent f698879 commit 42d6178
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ typechain-types
cache
artifacts

# foundry
cache_forge

out

.idea
.DS_Store

Expand Down
17 changes: 16 additions & 1 deletion contracts/FlashBuy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ contract FlashBuy is IERC3156FlashBorrower, OwnableUpgradeable {

uint256 constant public MAX_SLIPPAGE = 10000;

// added on 2024/09/12
address public revenuePool;

event RevenuePoolChanged(address indexed newAddress);

// --- Init ---
function initialize(
IERC3156FlashLender lender_,
Expand All @@ -58,8 +63,10 @@ contract FlashBuy is IERC3156FlashBorrower, OwnableUpgradeable {
}

function transfer(address token) external {
require(revenuePool != address(0), "Revenue pool not set");

bool success = IERC20(token).transfer(
owner(),
revenuePool,
IERC20(token).balanceOf(address(this))
);
require(success, "Failed to transfer");
Expand Down Expand Up @@ -156,4 +163,12 @@ contract FlashBuy is IERC3156FlashBorrower, OwnableUpgradeable {
lender.flashLoan(this, token, borrowAm, data);
require(IERC20(token).balanceOf(address(this)) > before, "Flash loan failed");
}

function changeRevenuePool(address _revenuePool) external onlyOwner {
require(_revenuePool != address(0), "Invalid zero address");
require(_revenuePool != revenuePool, "Already set");

revenuePool = _revenuePool;
emit RevenuePoolChanged(_revenuePool);
}
}
2 changes: 1 addition & 1 deletion lib/forge-std
Submodule forge-std updated from 978ac6 to 1714be
86 changes: 86 additions & 0 deletions test/foundry/FlashBuy.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.10;

import "forge-std/Test.sol";
import "forge-std/console.sol";
import "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
import "../../contracts/FlashBuy.sol";
import "../../contracts/LisUSD.sol";


contract FlashBuyTest is Test {
address proxyAdminOwner = address(0x2A11AA);

address admin;

address lisUSD;

FlashBuy flashBuy;

function setUp() public {
admin = msg.sender;
lisUSD = address(new LisUSD());

FlashBuy flashBuyImpl = new FlashBuy();
TransparentUpgradeableProxy flashBuyProxy = new TransparentUpgradeableProxy(
address(flashBuyImpl),
proxyAdminOwner,
abi.encodeWithSignature(
"initialize(address,address,address)",
address(0xffff), address(0xeeee), address(0xdddd)
)
);

flashBuy = FlashBuy(address(flashBuyProxy));
flashBuy.transferOwnership(admin);
}

function test_setup() public {
assertTrue(address(flashBuy) != address(0));
assertEq(address(0xffff), address(flashBuy.lender()));
}

function test_changeRevenuePool_ok() public {
assertEq(address(0), flashBuy.revenuePool());

vm.startPrank(admin);
flashBuy.changeRevenuePool(address(0x1234));
vm.stopPrank();

assertEq(address(0x1234), flashBuy.revenuePool());
}

function test_changeRevenuePool_acl() public {
assertEq(address(0), flashBuy.revenuePool());

vm.startPrank(address(0xffff));
vm.expectRevert("Ownable: caller is not the owner");
flashBuy.changeRevenuePool(address(0x1234));
vm.stopPrank();

assertEq(address(0), flashBuy.revenuePool());
}

function test_transfer_invalid_pool() public {
deal(lisUSD, address(flashBuy), 123e18);

assertEq(123e18, IERC20(lisUSD).balanceOf(address(flashBuy)));

vm.expectRevert("Revenue pool not set");
flashBuy.transfer(lisUSD);

assertEq(123e18, IERC20(lisUSD).balanceOf(address(flashBuy)));
}

function test_transfer_ok() public {
test_changeRevenuePool_ok();

deal(lisUSD, address(flashBuy), 123e18);
assertEq(123e18, IERC20(lisUSD).balanceOf(address(flashBuy)));

flashBuy.transfer(lisUSD);

assertEq(0, IERC20(lisUSD).balanceOf(address(flashBuy)));
assertEq(123e18, IERC20(lisUSD).balanceOf(address(0x1234)));
}
}

0 comments on commit 42d6178

Please sign in to comment.