Skip to content

Commit

Permalink
✅ Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JaredBorders committed Dec 4, 2023
1 parent c4f6614 commit dca5f4c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 23 deletions.
63 changes: 53 additions & 10 deletions test/EIP7412.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,73 @@
pragma solidity 0.8.20;

import {EIP7412} from "src/utils/EIP7412.sol";
import {
EIP7412Mock,
EIP7412MockRefund,
EIP7412MockRevert
} from "test/utils/mocks/EIP7412Mock.sol";
import {SynthetixMock} from "test/utils/mocks/SynthetixMock.sol";
import {Test} from "lib/forge-std/src/Test.sol";

contract EIP7412Test is Test, SynthetixMock {
EIP7412Mock eip7412Mock;
EIP7412MockRefund eip7412MockRefund;
EIP7412MockRevert eip7412MockRevert;

uint256 amount = 1 ether;

function setUp() public {
eip7412Mock = new EIP7412Mock();
eip7412MockRefund = new EIP7412MockRefund();
eip7412MockRevert = new EIP7412MockRevert();
}

function test_fulfillOracleQuery(bytes calldata signedOffchainData)
public
{
address payable mock_eip7412_implementer = payable(address(0xE19));
EIP7412 eip7412 = new EIP7412();

uint256 preBalance = address(this).balance;
uint preBalanceeip7412Mock = address(eip7412Mock).balance;

eip7412.fulfillOracleQuery{value: amount}(
payable(address(eip7412Mock)), signedOffchainData
);

assertLt(address(this).balance, preBalance);
assertEq(address(eip7412Mock).balance, preBalanceeip7412Mock + amount);
}

function test_fulfillOracleQuery_refund(bytes calldata signedOffchainData)
public
{
EIP7412 eip7412 = new EIP7412();

mock_fulfillOracleQuery(mock_eip7412_implementer, signedOffchainData);
uint256 preBalance = address(this).balance;

// refunds are not supported
vm.expectRevert("EIP7412MockRefund");

eip7412.fulfillOracleQuery{value: amount}(
payable(address(eip7412MockRefund)), signedOffchainData
);

assert(address(this).balance == preBalance);
}

function test_fulfillOracleQuery_revert(bytes calldata signedOffchainData)
public
{
EIP7412 eip7412 = new EIP7412();

uint256 preBalance = address(this).balance;

(bool success,) = address(eip7412).call{value: 5 wei}(
abi.encodeWithSelector(
EIP7412.fulfillOracleQuery.selector,
mock_eip7412_implementer,
signedOffchainData
)
vm.expectRevert("EIP7412MockRevert");

eip7412.fulfillOracleQuery{value: amount}(
payable(address(eip7412MockRevert)), signedOffchainData
);

assertTrue(success);
assertLt(address(this).balance, preBalance);
assert(address(this).balance == preBalance);
}
}
19 changes: 19 additions & 0 deletions test/utils/mocks/EIP7412Mock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.20;

contract EIP7412Mock {
function fulfillOracleQuery(bytes calldata) external payable {}
}

contract EIP7412MockRefund {
function fulfillOracleQuery(bytes calldata) external payable {
(bool success,) = msg.sender.call{value: msg.value}("");
require(success, "EIP7412MockRefund");
}
}

contract EIP7412MockRevert {
function fulfillOracleQuery(bytes calldata) external payable {
revert("EIP7412MockRevert");
}
}
13 changes: 0 additions & 13 deletions test/utils/mocks/SynthetixMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,6 @@ contract SynthetixMock is Test {
);
}

function mock_fulfillOracleQuery(
address EIP7412Implementer,
bytes calldata signedOffchainData
) public {
vm.mockCall(
EIP7412Implementer,
abi.encodeWithSelector(
EIP7412.fulfillOracleQuery.selector, signedOffchainData
),
abi.encode()
);
}

function mock_getAccountOwner(
address perpsMarketProxy,
uint128 accountId,
Expand Down

0 comments on commit dca5f4c

Please sign in to comment.