Skip to content

Commit

Permalink
chore: update rp contract to use gelato functions
Browse files Browse the repository at this point in the history
Signed-off-by: Tomás Migone <[email protected]>
  • Loading branch information
tmigone committed Dec 5, 2023
1 parent 43e6ce2 commit 05df85f
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 183 deletions.
5 changes: 3 additions & 2 deletions addresses.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"GraphToken": "0x5c946740441C12510a167B447B7dE565C20b9E3C",
"L1GraphTokenGateway": "0xc82fF7b51c3e593D709BA3dE1b3a0d233D1DEca1",
"governor": "0xc6B8Dd6163a0d6E3Ff37Eb4306DeD0069FE4bF59",
"BillingConnector": "0x1c133bF1A059F682910d9bf81a09931742ec7311"
"BillingConnector": "0x1c133bF1A059F682910d9bf81a09931742ec7311",
"GelatoAutomate": "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0"
},
"137": {
"Billing": "0x10829DB618E6F520Fa3A01c75bC6dDf8722fA9fE"
Expand All @@ -33,7 +34,7 @@
"governor": "0xc6B8Dd6163a0d6E3Ff37Eb4306DeD0069FE4bF59",
"Billing": "0x2da5DF23869B05b6641C91297b7025029112abA7",
"BanxaWrapper": "0xDc4787B4c822a36F72A0084B02dA213A7291C35B",
"GelatoAutomate": "0xa5f9b728ecEB9A1F6FCC89dcc2eFd810bA4Dec41",
"GelatoAutomate": "0x2A6C106ae13B558BB9E2Ec64Bd2f1f7BEFF3A5E0",
"RecurringPayments": "0x68becb01A921855D2b85B2952ca10f1801b518eD"
}
}
43 changes: 6 additions & 37 deletions contracts/GelatoManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,6 @@ contract GelatoManager is AutomateTaskCreator, Governed {
*/
event MaxGasPriceSet(uint256 maxGasPrice);

/**
* @dev Emitted when funds are deposited into the Gelato treasury
* @param depositor User making the deposit
* @param amount Amount being deposited
*/
event TreasuryFundsDeposited(address indexed depositor, uint256 amount);

/**
* @dev Emitted when funds are withdrawn from the Gelato treasury
* @param recepient Recepient receiving the funds
* @param amount Amount being withdrawn
*/
event TreasuryFundsWithdrawn(address indexed recepient, uint256 amount);

/**
* @dev Emitted when a resolver task is created in Gelato Network
* @param taskId The id of the task
Expand Down Expand Up @@ -74,31 +60,10 @@ contract GelatoManager is AutomateTaskCreator, Governed {
address _automate,
address _governor,
uint256 _maxGasPrice
) AutomateTaskCreator(_automate, _governor) Governed(_governor) {
) AutomateTaskCreator(_automate) Governed(_governor) {
_setMaxGasPrice(_maxGasPrice);
}

/**
* @notice Deposit eth into the Gelato Network treasury
* This function is NOT meant to be used by the end user, only by the contract operator
* Any funds deposited via this call will be used to pay for Gelato Network tasks
*/
function deposit() external payable {
if (msg.value == 0) revert InvalidDepositAmount();
taskTreasury.depositFunds{ value: msg.value }(address(this), ETH, msg.value);
emit TreasuryFundsDeposited(msg.sender, msg.value);
}

/**
* @notice Withdraw eth from the Gelato Network treasury
* @param recepient Recepient receiving the funds
* @param amount Amount being withdrawn
*/
function withdraw(address recepient, uint256 amount) external onlyGovernor {
taskTreasury.withdrawFunds(payable(recepient), ETH, amount);
emit TreasuryFundsWithdrawn(recepient, amount);
}

/**
* @notice Sets the maximum gas price for task execution
* @param newGasPrice The updated value for `maxGasPrice`
Expand Down Expand Up @@ -131,9 +96,13 @@ contract GelatoManager is AutomateTaskCreator, Governed {
address execAddress,
bytes memory execDataOrSelector
) internal returns (bytes32) {
ModuleData memory moduleData = ModuleData({ modules: new Module[](1), args: new bytes[](1) });
ModuleData memory moduleData = ModuleData({ modules: new Module[](2), args: new bytes[](2) });

moduleData.modules[0] = Module.RESOLVER;
moduleData.modules[1] = Module.PROXY;

moduleData.args[0] = _resolverModuleArg(resolverAddress, resolverData);
moduleData.args[1] = _proxyModuleArg();

bytes32 taskId = _createTask(execAddress, execDataOrSelector, moduleData, address(0));
emit ResolverTaskCreated(taskId);
Expand Down
18 changes: 12 additions & 6 deletions contracts/gelato/AutomateReady.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import "./Types.sol";
abstract contract AutomateReady {
IAutomate public immutable automate;
address public immutable dedicatedMsgSender;
address private immutable _gelato;
address private immutable feeCollector;
address internal constant ETH = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
address private constant OPS_PROXY_FACTORY = 0xC815dB16D4be6ddf2685C201937905aBf338F5D7;

/**
* @dev
Expand All @@ -33,8 +32,15 @@ abstract contract AutomateReady {
*/
constructor(address _automate, address _taskCreator) {
automate = IAutomate(_automate);
_gelato = IAutomate(_automate).gelato();
(dedicatedMsgSender, ) = IOpsProxyFactory(OPS_PROXY_FACTORY).getProxyOf(_taskCreator);
IGelato gelato = IGelato(IAutomate(_automate).gelato());

feeCollector = gelato.feeCollector();

address proxyModuleAddress = IAutomate(_automate).taskModuleAddresses(Module.PROXY);

address opsProxyFactoryAddress = IProxyModule(proxyModuleAddress).opsProxyFactory();

(dedicatedMsgSender, ) = IOpsProxyFactory(opsProxyFactoryAddress).getProxyOf(_taskCreator);
}

/**
Expand All @@ -45,10 +51,10 @@ abstract contract AutomateReady {
*/
function _transfer(uint256 _fee, address _feeToken) internal {
if (_feeToken == ETH) {
(bool success, ) = _gelato.call{ value: _fee }("");
(bool success, ) = feeCollector.call{ value: _fee }("");
require(success, "_transfer: ETH transfer failed");
} else {
SafeERC20.safeTransfer(IERC20(_feeToken), _gelato, _fee);
SafeERC20.safeTransfer(IERC20(_feeToken), feeCollector, _fee);
}
}

Expand Down
77 changes: 54 additions & 23 deletions contracts/gelato/AutomateTaskCreator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,35 @@
pragma solidity ^0.8.14;

import "./AutomateReady.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

/**
* @dev Inherit this contract to allow your smart contract
* to be a task creator and create tasks.
*/
//solhint-disable const-name-snakecase
//solhint-disable no-empty-blocks
abstract contract AutomateTaskCreator is AutomateReady {
using SafeERC20 for IERC20;

address public immutable fundsOwner;
ITaskTreasuryUpgradable public immutable taskTreasury;
IGelato1Balance public constant gelato1Balance = IGelato1Balance(0x7506C12a824d73D9b08564d5Afc22c949434755e);

constructor(address _automate, address _fundsOwner) AutomateReady(_automate, address(this)) {
fundsOwner = _fundsOwner;
taskTreasury = automate.taskTreasury();
}

/**
* @dev
* Withdraw funds from this contract's Gelato balance to fundsOwner.
*/
function withdrawFunds(uint256 _amount, address _token) external {
require(msg.sender == fundsOwner, "Only funds owner can withdraw funds");

taskTreasury.withdrawFunds(payable(fundsOwner), _token, _amount);
}
constructor(address _automate) AutomateReady(_automate, address(this)) {}

function _depositFunds(uint256 _amount, address _token) internal {
uint256 ethValue = _token == ETH ? _amount : 0;
taskTreasury.depositFunds{ value: ethValue }(address(this), _token, _amount);
function _depositFunds1Balance(uint256 _amount, address _token, address _sponsor) internal {
if (_token == ETH) {
///@dev Only deposit ETH on goerli for now.
require(block.chainid == 5, "Only deposit ETH on goerli");
gelato1Balance.depositNative{ value: _amount }(_sponsor);
} else {
///@dev Only deposit USDC on polygon for now.
require(
block.chainid == 137 && _token == address(0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174),
"Only deposit USDC on polygon"
);
IERC20(_token).approve(address(gelato1Balance), _amount);
gelato1Balance.depositToken(_sponsor, _token, _amount);
}
}

function _createTask(
Expand All @@ -53,15 +53,46 @@ abstract contract AutomateTaskCreator is AutomateReady {
return abi.encode(_resolverAddress, _resolverData);
}

function _timeModuleArg(uint256 _startTime, uint256 _interval) internal pure returns (bytes memory) {
return abi.encode(uint128(_startTime), uint128(_interval));
}

function _proxyModuleArg() internal pure returns (bytes memory) {
return bytes("");
}

function _singleExecModuleArg() internal pure returns (bytes memory) {
return bytes("");
}

function _web3FunctionModuleArg(
string memory _web3FunctionHash,
bytes memory _web3FunctionArgsHex
) internal pure returns (bytes memory) {
return abi.encode(_web3FunctionHash, _web3FunctionArgsHex);
}

function _timeTriggerModuleArg(uint128 _start, uint128 _interval) internal pure returns (bytes memory) {
bytes memory triggerConfig = abi.encode(_start, _interval);

return abi.encode(TriggerType.TIME, triggerConfig);
}

function _cronTriggerModuleArg(string memory _expression) internal pure returns (bytes memory) {
bytes memory triggerConfig = abi.encode(_expression);

return abi.encode(TriggerType.CRON, triggerConfig);
}

function _eventTriggerModuleArg(
address _address,
bytes32[][] memory _topics,
uint256 _blockConfirmations
) internal pure returns (bytes memory) {
bytes memory triggerConfig = abi.encode(_address, _topics, _blockConfirmations);

return abi.encode(TriggerType.EVENT, triggerConfig);
}

function _blockTriggerModuleArg() internal pure returns (bytes memory) {
bytes memory triggerConfig = abi.encode(bytes(""));

return abi.encode(TriggerType.BLOCK, triggerConfig);
}
}
31 changes: 24 additions & 7 deletions contracts/gelato/Types.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@ pragma solidity ^0.8.12;

enum Module {
RESOLVER,
TIME,
DEPRECATED_TIME,
PROXY,
SINGLE_EXEC
SINGLE_EXEC,
WEB3_FUNCTION,
TRIGGER
}

enum TriggerType {
TIME,
CRON,
EVENT,
BLOCK
}

struct ModuleData {
Expand All @@ -27,15 +36,23 @@ interface IAutomate {

function gelato() external view returns (address payable);

function taskTreasury() external view returns (ITaskTreasuryUpgradable);
function taskModuleAddresses(Module) external view returns (address);
}

interface ITaskTreasuryUpgradable {
function depositFunds(address receiver, address token, uint256 amount) external payable;

function withdrawFunds(address payable receiver, address token, uint256 amount) external;
interface IProxyModule {
function opsProxyFactory() external view returns (address);
}

interface IOpsProxyFactory {
function getProxyOf(address account) external view returns (address, bool);
}

interface IGelato1Balance {
function depositNative(address _sponsor) external payable;

function depositToken(address _sponsor, address _token, uint256 _amount) external;
}

interface IGelato {
function feeCollector() external view returns (address);
}
16 changes: 0 additions & 16 deletions contracts/tests/TaskTreasuryMock.sol

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.16;

import "../gelato/Types.sol";
import "../../gelato/Types.sol";

/**
* @title Mock for the Gelato Automate contract
*/
contract AutomateMock {
address internal gelatoAddr;
address internal treasuryAddr;
address public gelato;
address public feeCollector;
address public taskModuleAddress;

constructor(address _gelato, address _treasury) {
gelatoAddr = _gelato;
treasuryAddr = _treasury;
constructor(address _gelato, address _taskModuleAddress) {
gelato = _gelato;
taskModuleAddress = _taskModuleAddress;
feeCollector = address(0); // For this mock we don't care about the feeCollector
}

function gelato() external view returns (address) {
return gelatoAddr;
}

function taskTreasury() external view returns (address) {
return treasuryAddr;
function taskModuleAddresses(Module) external view returns (address) {
return taskModuleAddress;
}

/// @dev This is NOT how Gelato computes task IDs
Expand All @@ -34,7 +32,7 @@ contract AutomateMock {
return task;
}

function cancelTask(bytes32 _taskId) external pure returns (bool) {
function cancelTask(bytes32) external pure returns (bool) {
return true;
}
}
14 changes: 14 additions & 0 deletions contracts/tests/gelato/GelatoMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.16;

import { IGelato } from "../../gelato/Types.sol";

/**
* @title Mock for the Gelato main contract
*/
contract GelatoMock is IGelato {
// We don't care about the feeCollector for this mock
function feeCollector() external view returns (address) {
return address(0);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.16;

import { IOpsProxyFactory } from "../gelato/Types.sol";
import { IOpsProxyFactory } from "../../gelato/Types.sol";

/**
* @title Mock for the Gelato OpsProxyFactory contract
Expand Down
19 changes: 19 additions & 0 deletions contracts/tests/gelato/ProxyModuleMock.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.16;

import { IProxyModule } from "../../gelato/Types.sol";

/**
* @title Mock for the Gelato OpsProxyFactory contract
*/
contract ProxyModuleMock is IProxyModule {
address internal opsProxyFactoryAddress;

constructor(address _opsProxyFactoryAddress) {
opsProxyFactoryAddress = _opsProxyFactoryAddress;
}

function opsProxyFactory() external view returns (address) {
return opsProxyFactoryAddress;
}
}
Loading

0 comments on commit 05df85f

Please sign in to comment.