From 9e777d3b2a7024098b6909357340c287c786ba53 Mon Sep 17 00:00:00 2001 From: Pablo Maldonado Date: Tue, 13 Feb 2024 19:41:31 +0000 Subject: [PATCH] test: add mock oracle test Signed-off-by: Pablo Maldonado --- test/HoneyPot.sol | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/HoneyPot.sol b/test/HoneyPot.sol index b6f7e9d..fb09ef4 100644 --- a/test/HoneyPot.sol +++ b/test/HoneyPot.sol @@ -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); @@ -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);