Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write happy path tests for automation23 registrar registerUpkeep() #12474

Merged
merged 3 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity 0.8.19;
import {BaseTest} from "./BaseTest.t.sol";
import {IAutomationRegistryMaster2_3} from "../interfaces/v2_3/IAutomationRegistryMaster2_3.sol";
import {AutomationRegistrar2_3} from "../v2_3/AutomationRegistrar2_3.sol";
import {IERC20} from "../../../vendor/openzeppelin-solidity/v4.8.3/contracts/token/ERC20/IERC20.sol";

// forge test --match-path src/v0.8/automation/dev/test/AutomationRegistrar2_3.t.sol

Expand All @@ -14,7 +15,116 @@ contract SetUp is BaseTest {
function setUp() public override {
super.setUp();
(registry, registrar) = deployAndConfigureAll();
vm.stopPrank(); // reset identity at the start of each test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

}
}

contract OnTokenTransfer is SetUp {}
contract RegisterUpkeep is SetUp {
function testLink_autoApproveOff_happy() external {
vm.startPrank(UPKEEP_ADMIN);

uint96 amount = uint96(registrar.getMinimumRegistrationAmount(IERC20(address(linkToken))));
linkToken.approve(address(registrar), amount);

registrar.registerUpkeep(
AutomationRegistrar2_3.RegistrationParams({
upkeepContract: address(TARGET1),
amount: amount,
adminAddress: UPKEEP_ADMIN,
gasLimit: 10_000,
triggerType: 0,
billingToken: IERC20(address(linkToken)),
name: "foobar",
encryptedEmail: "",
checkData: bytes("check data"),
triggerConfig: "",
offchainConfig: ""
})
);

assertEq(linkToken.balanceOf(address(registrar)), amount);
assertEq(registry.getNumUpkeeps(), 0);
}

function testUSDToken_autoApproveOff_happy() external {
vm.startPrank(UPKEEP_ADMIN);

uint96 amount = uint96(registrar.getMinimumRegistrationAmount(mockERC20));
mockERC20.approve(address(registrar), amount);

registrar.registerUpkeep(
AutomationRegistrar2_3.RegistrationParams({
upkeepContract: address(TARGET1),
amount: amount,
adminAddress: UPKEEP_ADMIN,
gasLimit: 10_000,
triggerType: 0,
billingToken: mockERC20,
name: "foobar",
encryptedEmail: "",
checkData: bytes("check data"),
triggerConfig: "",
offchainConfig: ""
})
);

assertEq(mockERC20.balanceOf(address(registrar)), amount);
assertEq(registry.getNumUpkeeps(), 0);
}

function testLink_autoApproveOn_happy() external {
registrar.setTriggerConfig(0, AutomationRegistrar2_3.AutoApproveType.ENABLED_ALL, 1000);

vm.startPrank(UPKEEP_ADMIN);
uint96 amount = uint96(registrar.getMinimumRegistrationAmount(IERC20(address(linkToken))));
linkToken.approve(address(registrar), amount);

registrar.registerUpkeep(
AutomationRegistrar2_3.RegistrationParams({
upkeepContract: address(TARGET1),
amount: amount,
adminAddress: UPKEEP_ADMIN,
gasLimit: 10_000,
triggerType: 0,
billingToken: IERC20(address(linkToken)),
name: "foobar",
encryptedEmail: "",
checkData: bytes("check data"),
triggerConfig: "",
offchainConfig: ""
})
);

assertEq(linkToken.balanceOf(address(registrar)), 0);
assertEq(linkToken.balanceOf(address(registry)), amount);
assertEq(registry.getNumUpkeeps(), 1);
}

function testUSDToken_autoApproveOn_happy() external {
registrar.setTriggerConfig(0, AutomationRegistrar2_3.AutoApproveType.ENABLED_ALL, 1000);

vm.startPrank(UPKEEP_ADMIN);
uint96 amount = uint96(registrar.getMinimumRegistrationAmount(mockERC20));
mockERC20.approve(address(registrar), amount);

registrar.registerUpkeep(
AutomationRegistrar2_3.RegistrationParams({
upkeepContract: address(TARGET1),
amount: amount,
adminAddress: UPKEEP_ADMIN,
gasLimit: 10_000,
triggerType: 0,
billingToken: mockERC20,
name: "foobar",
encryptedEmail: "",
checkData: bytes("check data"),
triggerConfig: "",
offchainConfig: ""
})
);

assertEq(mockERC20.balanceOf(address(registrar)), 0);
assertEq(mockERC20.balanceOf(address(registry)), amount);
assertEq(registry.getNumUpkeeps(), 1);
}
}
19 changes: 19 additions & 0 deletions contracts/src/v0.8/automation/dev/test/BaseTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {IAutomationRegistryMaster2_3, AutomationRegistryBase2_3} from "../interf
import {AutomationRegistrar2_3} from "../v2_3/AutomationRegistrar2_3.sol";
import {ChainModuleBase} from "../../chains/ChainModuleBase.sol";
import {IERC20} from "../../../vendor/openzeppelin-solidity/v4.8.3/contracts/token/ERC20/IERC20.sol";
import {MockUpkeep} from "../../mocks/MockUpkeep.sol";

/**
* @title BaseTest provides basic test setup procedures and dependancies for use by other
Expand All @@ -35,11 +36,15 @@ contract BaseTest is Test {
MockV3Aggregator internal NATIVE_USD_FEED;
MockV3Aggregator internal USDTOKEN_USD_FEED;
MockV3Aggregator internal FAST_GAS_FEED;
MockUpkeep internal TARGET1;
MockUpkeep internal TARGET2;

// roles
address internal constant OWNER = address(uint160(uint256(keccak256("OWNER"))));
address internal constant UPKEEP_ADMIN = address(uint160(uint256(keccak256("UPKEEP_ADMIN"))));
address internal constant FINANCE_ADMIN = address(uint160(uint256(keccak256("FINANCE_ADMIN"))));
address internal constant STRANGER = address(uint160(uint256(keccak256("STRANGER"))));
address internal constant BROKE_USER = address(uint160(uint256(keccak256("BROKE_USER")))); // do not mint to this address

// nodes
uint256 internal constant SIGNING_KEY0 = 0x7b2e97fe057e6de99d6872a2ef2abf52c9b4469bc848c2465ac3fcd8d336e81d;
Expand All @@ -60,6 +65,9 @@ contract BaseTest is Test {
USDTOKEN_USD_FEED = new MockV3Aggregator(8, 100_000_000); // $1
FAST_GAS_FEED = new MockV3Aggregator(0, 1_000_000_000); // 1 gwei

TARGET1 = new MockUpkeep();
TARGET2 = new MockUpkeep();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, TARGET2 is not used anywhere.

Code LGTM. let me know when you are ready, will stamp again.


SIGNERS[0] = vm.addr(SIGNING_KEY0); //0xc110458BE52CaA6bB68E66969C3218A4D9Db0211
SIGNERS[1] = vm.addr(SIGNING_KEY1); //0xc110a19c08f1da7F5FfB281dc93630923F8E3719
SIGNERS[2] = vm.addr(SIGNING_KEY2); //0xc110fdF6e8fD679C7Cc11602d1cd829211A18e9b
Expand All @@ -70,6 +78,17 @@ contract BaseTest is Test {
TRANSMITTERS[2] = address(uint160(uint256(keccak256("TRANSMITTER3"))));
TRANSMITTERS[3] = address(uint160(uint256(keccak256("TRANSMITTER4"))));

// mint funds
vm.deal(UPKEEP_ADMIN, 10 ether);
vm.deal(FINANCE_ADMIN, 10 ether);
vm.deal(STRANGER, 10 ether);
linkToken.mint(UPKEEP_ADMIN, 1000e18);
linkToken.mint(FINANCE_ADMIN, 1000e18);
linkToken.mint(STRANGER, 1000e18);
mockERC20.mint(UPKEEP_ADMIN, 1000e18);
mockERC20.mint(FINANCE_ADMIN, 1000e18);
mockERC20.mint(STRANGER, 1000e18);

vm.stopPrank();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,13 @@ contract AutomationRegistryLogicB2_3 is AutomationRegistryBase2_3 {
return UPKEEP_VERSION_BASE;
}

/**
* @notice gets the number of upkeeps on the registry
*/
function getNumUpkeeps() external view returns (uint256) {
return s_upkeepIDs.length();
}

/**
* @notice read all of the details about an upkeep
* @dev this function may be deprecated in a future version of automation in favor of individual
Expand Down
53 changes: 53 additions & 0 deletions contracts/src/v0.8/automation/mocks/MockUpkeep.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract MockUpkeep {
bool public shouldCheckRevert;
bool public shouldPerformRevert;
bool public checkResult = true;
bytes public performData;
uint256 public checkGasToBurn;
uint256 public performGasToBurn;

event UpkeepPerformedWith(bytes upkeepData);
error CheckRevert();
error PerformRevert();

function setShouldCheckRevert(bool value) public {
shouldCheckRevert = value;
}

function setShouldPerformRevert(bool value) public {
shouldPerformRevert = value;
}

function setCheckResult(bool value) public {
checkResult = value;
}

function setPerformData(bytes calldata data) public {
performData = data;
}

function setCheckGasToBurn(uint256 value) public {
checkGasToBurn = value;
}

function setPerformGasToBurn(uint256 value) public {
performGasToBurn = value;
}

function checkUpkeep(bytes calldata) external view returns (bool callable, bytes memory executedata) {
if (shouldCheckRevert) revert CheckRevert();
uint256 startGas = gasleft();
while (startGas - gasleft() < checkGasToBurn) {} // burn gas
return (checkResult, performData);
}

function performUpkeep(bytes calldata data) external {
if (shouldPerformRevert) revert PerformRevert();
uint256 startGas = gasleft();
while (startGas - gasleft() < performGasToBurn) {} // burn gas
emit UpkeepPerformedWith(data);
}
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ automation_registrar_wrapper2_3: ../../contracts/solc/v0.8.19/AutomationRegistra
automation_registry_logic_a_wrapper_2_2: ../../contracts/solc/v0.8.19/AutomationRegistryLogicA2_2/AutomationRegistryLogicA2_2.abi ../../contracts/solc/v0.8.19/AutomationRegistryLogicA2_2/AutomationRegistryLogicA2_2.bin 58d09c16be20a6d3f70be6d06299ed61415b7796c71f176d87ce015e1294e029
automation_registry_logic_a_wrapper_2_3: ../../contracts/solc/v0.8.19/AutomationRegistryLogicA2_3/AutomationRegistryLogicA2_3.abi ../../contracts/solc/v0.8.19/AutomationRegistryLogicA2_3/AutomationRegistryLogicA2_3.bin 638b3d1bfb9d5883065c976ea69186380600464058fdf4a367814804b7107bdd
automation_registry_logic_b_wrapper_2_2: ../../contracts/solc/v0.8.19/AutomationRegistryLogicB2_2/AutomationRegistryLogicB2_2.abi ../../contracts/solc/v0.8.19/AutomationRegistryLogicB2_2/AutomationRegistryLogicB2_2.bin a6d33dfbbfb0ff253eb59a51f4f6d6d4c22ea5ec95aae52d25d49a312b37a22f
automation_registry_logic_b_wrapper_2_3: ../../contracts/solc/v0.8.19/AutomationRegistryLogicB2_3/AutomationRegistryLogicB2_3.abi ../../contracts/solc/v0.8.19/AutomationRegistryLogicB2_3/AutomationRegistryLogicB2_3.bin 7e3cb25e8279cf90ec7794740b1261b5ba00a757cc7d7547a6054936a20a5d72
automation_registry_logic_b_wrapper_2_3: ../../contracts/solc/v0.8.19/AutomationRegistryLogicB2_3/AutomationRegistryLogicB2_3.abi ../../contracts/solc/v0.8.19/AutomationRegistryLogicB2_3/AutomationRegistryLogicB2_3.bin c09f58674b6522c36c356849fe827b87a0422a14c02debd04392966eee7a620b
automation_registry_wrapper_2_2: ../../contracts/solc/v0.8.19/AutomationRegistry2_2/AutomationRegistry2_2.abi ../../contracts/solc/v0.8.19/AutomationRegistry2_2/AutomationRegistry2_2.bin eca1187a878b622ef3fced041a28a4229d45dd797d95630838ff6351b6afc437
automation_registry_wrapper_2_3: ../../contracts/solc/v0.8.19/AutomationRegistry2_3/AutomationRegistry2_3.abi ../../contracts/solc/v0.8.19/AutomationRegistry2_3/AutomationRegistry2_3.bin 478970464d5e8ebec2d88c1114da2a90d96abfd6c2f3147042050190003567a8
automation_utils_2_1: ../../contracts/solc/v0.8.16/AutomationUtils2_1/AutomationUtils2_1.abi ../../contracts/solc/v0.8.16/AutomationUtils2_1/AutomationUtils2_1.bin 815b17b63f15d26a0274b962eefad98cdee4ec897ead58688bbb8e2470e585f5
Expand All @@ -34,7 +34,7 @@ flux_aggregator_wrapper: ../../contracts/solc/v0.6/FluxAggregator/FluxAggregator
gas_wrapper: ../../contracts/solc/v0.8.6/KeeperRegistryCheckUpkeepGasUsageWrapper1_2/KeeperRegistryCheckUpkeepGasUsageWrapper1_2.abi ../../contracts/solc/v0.8.6/KeeperRegistryCheckUpkeepGasUsageWrapper1_2/KeeperRegistryCheckUpkeepGasUsageWrapper1_2.bin 4a5dcdac486d18fcd58e3488c15c1710ae76b977556a3f3191bd269a4bc75723
gas_wrapper_mock: ../../contracts/solc/v0.8.6/KeeperRegistryCheckUpkeepGasUsageWrapper1_2Mock/KeeperRegistryCheckUpkeepGasUsageWrapper1_2Mock.abi ../../contracts/solc/v0.8.6/KeeperRegistryCheckUpkeepGasUsageWrapper1_2Mock/KeeperRegistryCheckUpkeepGasUsageWrapper1_2Mock.bin a9b08f18da59125c6fc305855710241f3d35161b8b9f3e3f635a7b1d5c6da9c8
i_automation_registry_master_wrapper_2_2: ../../contracts/solc/v0.8.19/IAutomationRegistryMaster/IAutomationRegistryMaster.abi ../../contracts/solc/v0.8.19/IAutomationRegistryMaster/IAutomationRegistryMaster.bin 9ff7087179f89f9b05964ebc3e71332fce11f1b8e85058f7b16b3bc0dd6fb96b
i_automation_registry_master_wrapper_2_3: ../../contracts/solc/v0.8.19/IAutomationRegistryMaster2_3/IAutomationRegistryMaster2_3.abi ../../contracts/solc/v0.8.19/IAutomationRegistryMaster2_3/IAutomationRegistryMaster2_3.bin b4f059e91382d2ce43fffebb80e5947189291c5d8835df4c10195d0d8c249267
i_automation_registry_master_wrapper_2_3: ../../contracts/solc/v0.8.19/IAutomationRegistryMaster2_3/IAutomationRegistryMaster2_3.abi ../../contracts/solc/v0.8.19/IAutomationRegistryMaster2_3/IAutomationRegistryMaster2_3.bin 764293c8f84889c08294d24155e8d6705a61c485e0bd249a0824c8983d6164bc
i_automation_v21_plus_common: ../../contracts/solc/v0.8.19/IAutomationV21PlusCommon/IAutomationV21PlusCommon.abi ../../contracts/solc/v0.8.19/IAutomationV21PlusCommon/IAutomationV21PlusCommon.bin e8a601ec382c0a2e83c49759de13b0622b5e04e6b95901e96a1e9504329e594c
i_chain_module: ../../contracts/solc/v0.8.19/IChainModule/IChainModule.abi ../../contracts/solc/v0.8.19/IChainModule/IChainModule.bin 383611981c86c70522f41b8750719faacc7d7933a22849d5004799ebef3371fa
i_keeper_registry_master_wrapper_2_1: ../../contracts/solc/v0.8.16/IKeeperRegistryMaster/IKeeperRegistryMaster.abi ../../contracts/solc/v0.8.16/IKeeperRegistryMaster/IKeeperRegistryMaster.bin ee0f150b3afbab2df3d24ff3f4c87851efa635da30db04cd1f70cb4e185a1781
Expand Down
Loading