Skip to content

Commit

Permalink
test: add mock oracle test
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Maldonado <[email protected]>
  • Loading branch information
md0x committed Feb 13, 2024
1 parent f3749cf commit 9e777d3
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions test/HoneyPot.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {HoneyPot} from "../src/HoneyPot.sol";
import {HoneyPotDAO} from "../src/HoneyPotDAO.sol";
import {ChainlinkOvalImmutable, IAggregatorV3Source} from "oval-quickstart/ChainlinkOvalImmutable.sol";

import {MockV3Aggregator} from "../src/mock/MockV3Aggregator.sol";

contract HoneyPotTest is CommonTest {
event ReceivedEther(address sender, uint256 amount);
event DrainedEther(address to, uint256 amount);
Expand Down Expand Up @@ -106,6 +108,42 @@ contract HoneyPotTest is CommonTest {
assertTrue(testhoneyPotBalanceTwo == honeyPotBalance);
}

function testCrackHoneyPotWithMockOracle() public {
// Setup mock oracle
MockV3Aggregator mock = new MockV3Aggregator(8, 100000000000);
address[] memory unlockers = new address[](1);
unlockers[0] = address(this);
oracle = new ChainlinkOvalImmutable(IAggregatorV3Source(address(mock)), 8, 3, 10, unlockers);
honeyPot = new HoneyPot(IAggregatorV3Source(address(oracle)));

// Create HoneyPot for the caller
(, int256 currentPrice,,,) = oracle.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);

// Simulate price change
int256 newAnswer = (currentPrice * 103) / 100;
bytes32[] memory empty = new bytes32[](0);
mock.transmit(abi.encode(newAnswer), empty, empty, bytes32(0));

// Unlock the latest value
oracle.unlockLatestValue();

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;

assertTrue(liquidatorBalanceAfter == liquidatorBalanceBefore + honeyPotBalance);
}

function testHoneyPotDAO() public {
vm.expectEmit(true, true, true, true);
emit ReceivedEther(address(this), 1 ether);
Expand Down

0 comments on commit 9e777d3

Please sign in to comment.