Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: event withdrawn amounts #7

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/HoneyPot.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ contract HoneyPot is Ownable {
emit HoneyPotCreated(msg.sender, currentPrice, msg.value);
}

function _emptyPotForUser(address honeyPotCreator, address recipient) internal {
function _emptyPotForUser(address honeyPotCreator, address recipient) internal returns (uint256 amount) {
HoneyPotDetails storage userPot = honeyPots[honeyPotCreator];

uint256 amount = userPot.balance;
amount = userPot.balance;
userPot.balance = 0; // reset the balance
userPot.liquidationPrice = 0; // reset the liquidation price
Address.sendValue(payable(recipient), amount);
Expand All @@ -58,12 +58,12 @@ contract HoneyPot is Ownable {
require(currentPrice != userPot.liquidationPrice, "Liquidation price reached for this user");
require(userPot.balance > 0, "No balance to withdraw");

_emptyPotForUser(honeyPotCreator, msg.sender);
emit HoneyPotEmptied(honeyPotCreator, msg.sender, userPot.balance);
uint256 withdrawnAmount = _emptyPotForUser(honeyPotCreator, msg.sender);
emit HoneyPotEmptied(honeyPotCreator, msg.sender, withdrawnAmount);
}

function resetPot() external {
_emptyPotForUser(msg.sender, msg.sender);
emit PotReset(msg.sender, honeyPots[msg.sender].balance);
uint256 withdrawnAmount = _emptyPotForUser(msg.sender, msg.sender);
emit PotReset(msg.sender, withdrawnAmount);
}
}
17 changes: 17 additions & 0 deletions test/HoneyPot.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ import {ChronicleMedianSourceMock} from "../src/mock/ChronicleMedianSourceMock.s
contract HoneyPotTest is CommonTest {
event ReceivedEther(address sender, uint256 amount);
event DrainedEther(address to, uint256 amount);
event OracleUpdated(address indexed newOracle);
event HoneyPotCreated(address indexed creator, int256 liquidationPrice, uint256 initialBalance);
event HoneyPotEmptied(address indexed honeyPotCreator, address indexed trigger, uint256 amount);
event PotReset(address indexed owner, uint256 amount);

IAggregatorV3Source chainlink = IAggregatorV3Source(0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419);
IMedian chronicle = IMedian(0x64DE91F5A373Cd4c28de3600cB34C7C6cE410C85);
Expand Down Expand Up @@ -82,6 +86,8 @@ contract HoneyPotTest is CommonTest {
assertTrue(address(this).balance == balanceBefore - honeyPotBalance);

// Reset HoneyPot for the caller
vm.expectEmit(true, true, true, true);
emit PotReset(address(this), honeyPotBalance);
honeyPot.resetPot();
(, uint256 testhoneyPotBalanceReset) = honeyPot.honeyPots(address(this));
assertTrue(testhoneyPotBalanceReset == 0);
Expand All @@ -90,6 +96,9 @@ contract HoneyPotTest is CommonTest {

function testCrackHoneyPot() public {
// Create HoneyPot for the caller
(, int256 currentPrice,,,) = oevShare.latestRoundData();
vm.expectEmit(true, true, true, true);
emit HoneyPotCreated(address(this), currentPrice, honeyPotBalance);
honeyPot.createHoneyPot{value: honeyPotBalance}();
(, uint256 testhoneyPotBalance) = honeyPot.honeyPots(address(this));
assertTrue(testhoneyPotBalance == honeyPotBalance);
Expand All @@ -107,6 +116,8 @@ contract HoneyPotTest is CommonTest {
uint256 liquidatorBalanceBefore = liquidator.balance;

vm.prank(liquidator);
vm.expectEmit(true, true, true, true);
emit HoneyPotEmptied(address(this), liquidator, honeyPotBalance);
honeyPot.emptyHoneyPot(address(this));

uint256 liquidatorBalanceAfter = liquidator.balance;
Expand Down Expand Up @@ -186,4 +197,10 @@ contract HoneyPotTest is CommonTest {
vm.expectRevert("No balance to withdraw");
honeyPot.emptyHoneyPot(address(this));
}

function testSetOracle() public {
vm.expectEmit(true, true, true, true);
emit OracleUpdated(random);
honeyPot.setOracle(IAggregatorV3Source(random));
}
}