Skip to content

Commit

Permalink
fix: event withdrawn amounts (#7)
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Maldonado <pablo@umaproject.org>
  • Loading branch information
md0x authored Nov 17, 2023
1 parent b0507e9 commit 0e585b1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
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));
}
}

0 comments on commit 0e585b1

Please sign in to comment.