-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor test lib and add tests (#12664)
- Loading branch information
Showing
22 changed files
with
142 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Operator_cancelRequest:test_Success(uint96) (runs: 256, μ: 306103, ~: 306096) | ||
Operator_cancelRequest:test_afterSuccessfulRequestSucess(uint96) (runs: 256, μ: 384781, ~: 389554) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
contracts/src/v0.8/operatorforwarder/dev/test/operator.t.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.19; | ||
|
||
import {Test} from "forge-std/Test.sol"; | ||
import {Operator} from "../Operator.sol"; | ||
import {ChainlinkClientHelper} from "./testhelpers/ChainlinkClientHelper.sol"; | ||
import {LinkToken} from "../../../shared/token/ERC677/LinkToken.sol"; | ||
|
||
contract Operator_cancelRequest is Test { | ||
address public s_link; | ||
ChainlinkClientHelper public s_client; | ||
Operator public s_operator; | ||
|
||
function setUp() public { | ||
s_link = address(new LinkToken()); | ||
s_client = new ChainlinkClientHelper(s_link); | ||
|
||
address[] memory auth = new address[](1); | ||
auth[0] = address(this); | ||
s_operator = new Operator(s_link, address(this)); | ||
s_operator.setAuthorizedSenders(auth); | ||
} | ||
|
||
function test_Success(uint96 payment) public { | ||
payment = uint96(bound(payment, 1, type(uint96).max)); | ||
deal(s_link, address(s_client), payment); | ||
// We're going to cancel one request and fulfil the other | ||
bytes32 requestIdToCancel = s_client.sendRequest(address(s_operator), payment); | ||
|
||
// Nothing withdrawable | ||
// 1 payment in escrow | ||
// Client has zero link | ||
assertEq(s_operator.withdrawable(), 0); | ||
assertEq(LinkToken(s_link).balanceOf(address(s_operator)), payment); | ||
assertEq(LinkToken(s_link).balanceOf(address(s_client)), 0); | ||
|
||
// Advance time so we can cancel | ||
uint256 expiration = block.timestamp + s_operator.EXPIRYTIME(); | ||
vm.warp(expiration + 1); | ||
s_client.cancelRequest(requestIdToCancel, payment, expiration); | ||
|
||
// 1 payment has been returned due to the cancellation. | ||
assertEq(s_operator.withdrawable(), 0); | ||
assertEq(LinkToken(s_link).balanceOf(address(s_operator)), 0); | ||
assertEq(LinkToken(s_link).balanceOf(address(s_client)), payment); | ||
} | ||
|
||
function test_afterSuccessfulRequestSucess(uint96 payment) public { | ||
payment = uint96(bound(payment, 1, type(uint96).max) / 2); | ||
deal(s_link, address(s_client), 2 * payment); | ||
|
||
// Initial state, client has 2 payments, zero in escrow, zero in the operator, zeero withdrawable | ||
assertEq(s_operator.withdrawable(), 0); | ||
assertEq(LinkToken(s_link).balanceOf(address(s_operator)), 0); | ||
assertEq(LinkToken(s_link).balanceOf(address(s_client)), 2 * payment); | ||
|
||
// We're going to cancel one request and fulfil the other | ||
bytes32 requestId = s_client.sendRequest(address(s_operator), payment); | ||
bytes32 requestIdToCancel = s_client.sendRequest(address(s_operator), payment); | ||
|
||
// Nothing withdrawable | ||
// Operator now has the 2 payments in escrow | ||
// Client has zero payments | ||
assertEq(s_operator.withdrawable(), 0); | ||
assertEq(LinkToken(s_link).balanceOf(address(s_operator)), 2 * payment); | ||
assertEq(LinkToken(s_link).balanceOf(address(s_client)), 0); | ||
|
||
// Fulfill one request | ||
uint256 expiration = block.timestamp + s_operator.EXPIRYTIME(); | ||
s_operator.fulfillOracleRequest( | ||
requestId, | ||
payment, | ||
address(s_client), | ||
s_client.FULFILSELECTOR(), | ||
expiration, | ||
bytes32(hex"01") | ||
); | ||
// 1 payment withdrawable from fulfilling `requestId`, 1 payment in escrow | ||
assertEq(s_operator.withdrawable(), payment); | ||
assertEq(LinkToken(s_link).balanceOf(address(s_operator)), 2 * payment); | ||
assertEq(LinkToken(s_link).balanceOf(address(s_client)), 0); | ||
|
||
// Advance time so we can cancel | ||
vm.warp(expiration + 1); | ||
s_client.cancelRequest(requestIdToCancel, payment, expiration); | ||
|
||
// 1 payment has been returned due to the cancellation, 1 payment should be withdrawable | ||
assertEq(s_operator.withdrawable(), payment); | ||
assertEq(LinkToken(s_link).balanceOf(address(s_operator)), payment); | ||
assertEq(LinkToken(s_link).balanceOf(address(s_client)), payment); | ||
|
||
// Withdraw the remaining payment | ||
s_operator.withdraw(address(s_client), payment); | ||
|
||
// End state is exactly the same as the initial state. | ||
assertEq(s_operator.withdrawable(), 0); | ||
assertEq(LinkToken(s_link).balanceOf(address(s_operator)), 0); | ||
assertEq(LinkToken(s_link).balanceOf(address(s_client)), 2 * payment); | ||
} | ||
} |
File renamed without changes.
22 changes: 22 additions & 0 deletions
22
contracts/src/v0.8/operatorforwarder/dev/test/testhelpers/ChainlinkClientHelper.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
import {ChainlinkClient} from "../../../../ChainlinkClient.sol"; | ||
|
||
contract ChainlinkClientHelper is ChainlinkClient { | ||
bytes4 public constant FULFILSELECTOR = this.fulfill.selector; | ||
|
||
constructor(address link) { | ||
_setChainlinkToken(link); | ||
} | ||
|
||
function sendRequest(address op, uint256 payment) external returns (bytes32) { | ||
return _sendChainlinkRequestTo(op, _buildOperatorRequest(bytes32(hex"10"), FULFILSELECTOR), payment); | ||
} | ||
|
||
function cancelRequest(bytes32 requestId, uint256 payment, uint256 expiration) external { | ||
_cancelChainlinkRequest(requestId, payment, this.fulfill.selector, expiration); | ||
} | ||
|
||
function fulfill(bytes32) external {} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
core/gethwrappers/generated/operator_factory/operator_factory.go
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.