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

feat: keep3r sponsor #27

Open
wants to merge 14 commits into
base: dev
Choose a base branch
from
168 changes: 168 additions & 0 deletions solidity/contracts/periphery/Keep3rSponsor.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.19;

import {EnumerableSet} from 'openzeppelin/utils/structs/EnumerableSet.sol';

import {IKeep3rSponsor, IKeep3rV2, IKeep3rHelper} from '../../interfaces/periphery/IKeep3rSponsor.sol';
import {IAutomationVault, IOpenRelay} from '../../interfaces/relays/IOpenRelay.sol';

/**
* @title Keep3rSponsor
* @notice This contract managed by Keep3r Network will sponsor some execution in determined jobs
*/

contract Keep3rSponsor is IKeep3rSponsor {
using EnumerableSet for EnumerableSet.AddressSet;

/// @inheritdoc IKeep3rSponsor
IKeep3rV2 public immutable KEEP3R_V2;

/// @inheritdoc IKeep3rSponsor
IKeep3rHelper public immutable KEEP3R_HELPER;

/// @inheritdoc IKeep3rSponsor
uint32 public immutable BASE = 10_000;

/// @inheritdoc IKeep3rSponsor
uint256 public bonus = 15_000;

/// @inheritdoc IKeep3rSponsor
IOpenRelay public openRelay;

/// @inheritdoc IKeep3rSponsor
address public owner;

/// @inheritdoc IKeep3rSponsor
address public pendingOwner;

/// @inheritdoc IKeep3rSponsor
address public feeRecipient;

/**
* @notice List of sponsored jobs
*/
EnumerableSet.AddressSet internal _sponsoredJobs;

/**
* @param _owner The address of the owner
* @param _feeRecipient The address of the fee recipient
* @param _openRelay The address of the open relay
* @param _keep3rV2 The address of the keep3rV2
* @param _keep3rHelper The address of the keep3rHelper
*/
constructor(
address _owner,
address _feeRecipient,
IOpenRelay _openRelay,
IKeep3rV2 _keep3rV2,
IKeep3rHelper _keep3rHelper
) {
owner = _owner;
feeRecipient = _feeRecipient;
openRelay = _openRelay;
KEEP3R_V2 = _keep3rV2;
KEEP3R_HELPER = _keep3rHelper;
}

/// @inheritdoc IKeep3rSponsor
function getSponsoredJobs() external view returns (address[] memory _sponsoredJobsList) {
_sponsoredJobsList = _sponsoredJobs.values();
}

/// @inheritdoc IKeep3rSponsor
function changeOwner(address _pendingOwner) external onlyOwner {
pendingOwner = _pendingOwner;
emit ChangeOwner(_pendingOwner);
}

/// @inheritdoc IKeep3rSponsor
function acceptOwner() external onlyPendingOwner {
pendingOwner = address(0);
owner = msg.sender;
emit AcceptOwner(msg.sender);
}

/// @inheritdoc IKeep3rSponsor
function setFeeRecipient(address _feeRecipient) external onlyOwner {
feeRecipient = _feeRecipient;
emit FeeRecipientSetted(_feeRecipient);
}

/// @inheritdoc IKeep3rSponsor
function setOpenRelay(IOpenRelay _openRelay) external onlyOwner {
openRelay = _openRelay;
emit OpenRelaySetted(_openRelay);
}

/// @inheritdoc IKeep3rSponsor
function setBonus(uint256 _bonus) external onlyOwner {
bonus = _bonus;
Copy link
Member

Choose a reason for hiding this comment

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

Add a check. Can't be lower than 10_000

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

emit BonusSetted(_bonus);
}

/// @inheritdoc IKeep3rSponsor
function addSponsoredJobs(address[] calldata _jobs) public onlyOwner {
for (uint256 _i; _i < _jobs.length;) {
_sponsoredJobs.add(_jobs[_i]);
emit ApproveSponsoredJob(_jobs[_i]);

unchecked {
++_i;
}
}
}

/// @inheritdoc IKeep3rSponsor
function deleteSponsoredJobs(address[] calldata _jobs) public onlyOwner {
for (uint256 _i; _i < _jobs.length;) {
_sponsoredJobs.remove(_jobs[_i]);

emit DeleteSponsoredJob(_jobs[_i]);
unchecked {
++_i;
}
}
}

function exec(IAutomationVault _automationVault, IAutomationVault.ExecData[] calldata _execData) external {
if (_execData.length == 0) revert Keep3rSponsor_NoJobs();

// The first call to `isKeeper` ensures the caller is a valid keeper
bool _isKeeper = KEEP3R_V2.isKeeper(msg.sender);
if (!_isKeeper) revert Keep3rSponsor_NotKeeper();
uint256 _initialGas = gasleft();

for (uint256 _i; _i < _execData.length;) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe move this up a line so we don't include this check as payment for the keeper

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

if (!_sponsoredJobs.contains(_execData[_i].job)) revert Keep3rSponsor_JobNotSponsored();

unchecked {
++_i;
}
}

openRelay.exec(_automationVault, _execData, feeRecipient);

uint256 _gasAfterWork = gasleft();
uint256 _reward = IKeep3rHelper(KEEP3R_HELPER).getRewardAmountFor(msg.sender, _initialGas - _gasAfterWork);
_reward = (_reward * bonus) / BASE;
KEEP3R_V2.bondedPayment(msg.sender, _reward);
}

/**
* @notice Checks that the caller is the owner
*/
modifier onlyOwner() {
address _owner = owner;
if (msg.sender != _owner) revert Keep3rSponsor_OnlyOwner();
_;
}

/**
* @notice Checks that the caller is the pending owner
*/
modifier onlyPendingOwner() {
address _pendingOwner = pendingOwner;
if (msg.sender != _pendingOwner) revert Keep3rSponsor_OnlyPendingOwner();
_;
}
}
201 changes: 201 additions & 0 deletions solidity/interfaces/periphery/IKeep3rSponsor.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.19;

import {IAutomationVault, IOpenRelay} from '../../interfaces/relays/IOpenRelay.sol';
import {IKeep3rV2} from '../../interfaces/external/IKeep3rV2.sol';
import {IKeep3rHelper} from '../../interfaces/external/IKeep3rHelper.sol';

interface IKeep3rSponsor {
/*///////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/

/**
* @notice Emitted when a job is executed
* @param _job The address of the job
*/
event JobExecuted(address _job);

/**
* @notice Emitted when the owner is proposed to change
* @param _pendingOwner The address that is being proposed
*/
event ChangeOwner(address indexed _pendingOwner);

/**
* @notice Emitted when the owner is accepted
* @param _owner The address of the new owner
*/
event AcceptOwner(address indexed _owner);

/**
* @notice Emitted when the fee recipient is setted
* @param _feeRecipient The address of the new fee recipient
*/
event FeeRecipientSetted(address indexed _feeRecipient);

/**
* @notice Emitted when the open relay is setted
* @param _openRelay The address of the new open relay
*/
event OpenRelaySetted(IOpenRelay indexed _openRelay);

/**
* @notice Emitted when the bonus is setted
* @param _bonus The sponsored bonus
*/
event BonusSetted(uint256 indexed _bonus);

/**
* @notice Emitted when a sponsored job is approved
* @param _job The address of the sponsored job
*/
event ApproveSponsoredJob(address indexed _job);

/**
* @notice Emitted when a sponsored job is deleted
* @param _job job The address of the sponsored job
*/
event DeleteSponsoredJob(address indexed _job);

/*///////////////////////////////////////////////////////////////
ERRORS
//////////////////////////////////////////////////////////////*/

/**
* @notice Thrown when the job executed is not in the list of sponsored jobs
*/
error Keep3rSponsor_JobNotSponsored();

/**
* @notice Thrown when the caller is not the owner
*/
error Keep3rSponsor_OnlyOwner();

/**
* @notice Thrown when the caller is not the pending owner
*/
error Keep3rSponsor_OnlyPendingOwner();

/**
* @notice Thrown when the caller is not a keeper
*/
error Keep3rSponsor_NotKeeper();

/**
* @notice Thrown when the exec data is empty
*/
error Keep3rSponsor_NoJobs();

/*///////////////////////////////////////////////////////////////
VIEW FUNCTIONS
//////////////////////////////////////////////////////////////*/

/**
* @notice Returns the keep3rV2 contract
* @return _keep3rV2 The address of the keep3rV2 contract
*/
function KEEP3R_V2() external view returns (IKeep3rV2 _keep3rV2);

/**
* @notice Returns the keep3r helper contract
* @return _keep3rHelper The address of the keep3r helper contract
*/
function KEEP3R_HELPER() external view returns (IKeep3rHelper _keep3rHelper);

/**
* @notice Returns the base
* @return _base The base
*/
function BASE() external view returns (uint32 _base);

/**
* @notice Returns the bonus
* @dev The bonus is in base 10_000
* @return _bonus The bonus
*/
function bonus() external view returns (uint256 _bonus);

/**
* @notice Returns the open relay
* @return _openRelay The address of the open relay
*/
function openRelay() external view returns (IOpenRelay _openRelay);

/**
* @notice Returns the owner address
* @return _owner The address of the owner
*/
function owner() external view returns (address _owner);

/**
* @notice Returns the pending owner address
* @return _pendingOwner The address of the pending owner
*/
function pendingOwner() external view returns (address _pendingOwner);

/**
* @notice Returns the fee recipient address
* @return _feeRecipient The address of the fee recipient
*/
function feeRecipient() external view returns (address _feeRecipient);

/**
* @notice Returns the list of the sponsored jobs
* @return _sponsoredJobsList The list of the sponsored jobs
*/
function getSponsoredJobs() external returns (address[] memory _sponsoredJobsList);

/*///////////////////////////////////////////////////////////////
EXTERNAL FUNCTIONS
//////////////////////////////////////////////////////////////*/

/**
* @notice Propose a new owner for the contract
* @dev The new owner will need to accept the ownership before it is transferred
* @param _pendingOwner The address of the new owner
*/
function changeOwner(address _pendingOwner) external;

/**
* @notice Accepts the ownership of the contract
*/
function acceptOwner() external;

/**
* @notice Sets the fee recipient who will receive the payment of the open relay
* @param _feeRecipient The address of the fee recipient
*/
function setFeeRecipient(address _feeRecipient) external;

/**
* @notice Sets the open relay
* @param _openRelay The address of the open relay
*/
function setOpenRelay(IOpenRelay _openRelay) external;

/**
* @notice Sets the bonus
* @param _bonus The bonus
*/
function setBonus(uint256 _bonus) external;

/**
* @notice Adds a job to the sponsored list
* @param _jobs List of jobs to add
*/
function addSponsoredJobs(address[] calldata _jobs) external;

/**
* @notice Removes a job from the sponsored list
* @param _jobs List of jobs to remove
*/
function deleteSponsoredJobs(address[] calldata _jobs) external;

/**
* @notice Execute an open relay which will execute the jobs and will manage the payment to the fee data receivers
* @param _automationVault The automation vault that will be executed
* @param _execData The array of exec data
*/
function exec(IAutomationVault _automationVault, IAutomationVault.ExecData[] calldata _execData) external;
}
2 changes: 1 addition & 1 deletion solidity/script/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ abstract contract Deploy is Script {
IKeep3rRelay public keep3rRelay;
IKeep3rBondedRelay public keep3rBondedRelay;

// Metadata
// Periphery contracts
IXKeeperMetadata public xKeeperMetadata;

// External contracts
Expand Down
Loading
Loading