Skip to content

Commit

Permalink
feat: updates
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Maldonado <pablo@umaproject.org>
  • Loading branch information
md0x committed Oct 24, 2023
1 parent b24a376 commit 954fe9c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
40 changes: 20 additions & 20 deletions src/HoneyPot.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
pragma solidity 0.8.17;

import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {HoneyPotOEVShare} from "./HoneyPotOEVShare.sol";
import {Address} from "@openzeppelin/contracts/utils/Address.sol";
import {IAggregatorV3Source} from "oev-contracts/interfaces/chainlink/IAggregatorV3Source.sol";

contract HoneyPot is Ownable {
struct HoneyPotDetails {
uint256 liquidationPrice;
int256 liquidationPrice;
uint256 balance;
}

mapping(address => HoneyPotDetails) public honeyPots;
HoneyPotOEVShare public oracle;
IAggregatorV3Source public oracle; // OEV Share serving as a Chainlink oracle

// Declare events
event OracleUpdated(address indexed newOracle);
event HoneyPotCreated(
address indexed creator,
uint256 liquidationPrice,
int256 liquidationPrice,
uint256 initialBalance
);
event HoneyPotEmptied(
Expand All @@ -27,55 +27,55 @@ contract HoneyPot is Ownable {
);
event PotReset(address indexed owner, uint256 amount);

constructor(HoneyPotOEVShare _oracle) {
constructor(IAggregatorV3Source _oracle) {
oracle = _oracle;
}

function setOracle(HoneyPotOEVShare _oracle) external onlyOwner {
function setOracle(IAggregatorV3Source _oracle) external onlyOwner {
oracle = _oracle;
emit OracleUpdated(address(_oracle)); // Emit event
emit OracleUpdated(address(_oracle));
}

function createHoneyPot(uint _liquidationPrice) external payable {
function createHoneyPot(int256 _liquidationPrice) external payable {
require(
honeyPots[msg.sender].liquidationPrice == 0,
"Liquidation price already set for this user"
);
require(_liquidationPrice > 0, "Liquidation price cannot be zero");

honeyPots[msg.sender].liquidationPrice = _liquidationPrice;
honeyPots[msg.sender].balance = msg.value; // add the sent ether to the user's honey pot balance
honeyPots[msg.sender].balance = msg.value;

emit HoneyPotCreated(msg.sender, _liquidationPrice, msg.value); // Emit event
emit HoneyPotCreated(msg.sender, _liquidationPrice, msg.value);
}

function emptyHoneyPot(address honeyPotCreator) external {
int256 currentPrice = oracle.latestAnswer();
(, int256 currentPrice, , , ) = oracle.latestRoundData();
require(currentPrice >= 0, "Invalid price from oracle");

HoneyPotDetails storage userPot = honeyPots[honeyPotCreator];

require(
uint256(currentPrice) != userPot.liquidationPrice,
currentPrice != userPot.liquidationPrice,
"Liquidation price reached for this user"
);

uint256 amount = userPot.balance;
userPot.balance = 0; // reset the balance
userPot.liquidationPrice = 0; // reset the liquidation price. There was a mistake in the original, missing the assignment.
payable(msg.sender).transfer(amount);
userPot.liquidationPrice = 0; // reset the liquidation price
Address.sendValue(payable(msg.sender), amount);

emit HoneyPotEmptied(honeyPotCreator, msg.sender, amount); // Emit event
emit HoneyPotEmptied(honeyPotCreator, msg.sender, amount);
}

function resetPot() external onlyOwner {
function resetPot() external {
HoneyPotDetails storage userPot = honeyPots[msg.sender];

userPot.liquidationPrice = 0;
userPot.liquidationPrice = 0; // reset the liquidation price
uint256 amount = userPot.balance;
userPot.balance = 0; // reset the balance
payable(msg.sender).transfer(amount);
Address.sendValue(payable(msg.sender), amount);

emit PotReset(msg.sender, amount); // Emit event
emit PotReset(msg.sender, amount);
}
}
15 changes: 7 additions & 8 deletions test/HoneyPot.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ contract HoneyPotTest is CommonTest {
8
);

honeyPot = new HoneyPot(oevShare);
honeyPot = new HoneyPot(IAggregatorV3Source(address(oevShare)));
_whitelistOnChronicle();
oevShare.setUnlocker(address(this), true);
}
Expand Down Expand Up @@ -72,9 +72,8 @@ contract HoneyPotTest is CommonTest {
uint256 balanceBefore = address(this).balance;

// Create HoneyPot for the caller
honeyPot.createHoneyPot{value: honeyPotBalance}(
uint256(oevShare.latestAnswer())
);
(, int256 latestAnswer, , , ) = oevShare.latestRoundData();
honeyPot.createHoneyPot{value: honeyPotBalance}(latestAnswer);

(, uint256 testhoneyPotBalance) = honeyPot.honeyPots(address(this));
assertTrue(testhoneyPotBalance == honeyPotBalance);
Expand All @@ -91,8 +90,9 @@ contract HoneyPotTest is CommonTest {

function testCrackHoneyPot() public {
// Create HoneyPot for the caller
(, int256 latestAnswer, , , ) = oevShare.latestRoundData();
honeyPot.createHoneyPot{value: honeyPotBalance}(
uint256(oevShare.latestAnswer())
latestAnswer
);
(, uint256 testhoneyPotBalance) = honeyPot.honeyPots(address(this));
assertTrue(testhoneyPotBalance == honeyPotBalance);
Expand All @@ -119,9 +119,8 @@ contract HoneyPotTest is CommonTest {
);

// Create HoneyPot can be called again
honeyPot.createHoneyPot{value: honeyPotBalance}(
uint256(oevShare.latestAnswer())
);
(, int256 latestAnswerNew, , , ) = oevShare.latestRoundData();
honeyPot.createHoneyPot{value: honeyPotBalance}(latestAnswerNew);
(, uint256 testhoneyPotBalanceTwo) = honeyPot.honeyPots(address(this));
assertTrue(testhoneyPotBalanceTwo == honeyPotBalance);
}
Expand Down

0 comments on commit 954fe9c

Please sign in to comment.