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

Fix Review #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
pragma solidity ^0.8.19;

import "./IAutomation.sol";
import "../oracle/IPythRelay.sol";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

import "../libraries/ArrayMutation.sol";
import "../interfaces/openzeppelin/Ownable.sol";
import "../interfaces/openzeppelin/ERC20.sol";
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

import "../interfaces/openzeppelin/IERC20.sol";
import "../interfaces/openzeppelin/SafeERC20.sol";
import "../oracle/IPythRelay.sol";
import "../interfaces/openzeppelin/Pausable.sol";
Copy link
Contributor Author

Choose a reason for hiding this comment

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


Comment on lines +11 to 12
Copy link
Contributor Author

Choose a reason for hiding this comment

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

///@notice This contract owns and handles all of the settings and accounting logic for automated swaps
Copy link
Contributor Author

Choose a reason for hiding this comment

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

///@notice This contract should not hold any user funds, only collected fees
Copy link
Contributor Author

Choose a reason for hiding this comment

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

contract AutomationMaster is IAutomationMaster, Ownable {
contract AutomationMaster is IAutomationMaster, Ownable, Pausable {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

using SafeERC20 for IERC20;
Copy link
Contributor Author

Choose a reason for hiding this comment

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


///@notice maximum pending orders that may exist at a time, limiting the compute requriement for checkUpkeep
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Expand All @@ -27,6 +28,25 @@ contract AutomationMaster is IAutomationMaster, Ownable {
///each token must have a registered oracle in order to be tradable
Copy link
Contributor Author

Choose a reason for hiding this comment

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

mapping(IERC20 => IPythRelay) public oracles;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

mapping(IERC20 => bytes32) public pythIds;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

mapping(address => uint96) private nonces;

Comment on lines +31 to +32
Copy link
Contributor Author

Choose a reason for hiding this comment

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

constructor(address owner){
_transferOwnership(owner);
}

Comment on lines +33 to +36
Copy link
Contributor Author

Choose a reason for hiding this comment

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

function pauseAll(
bool pause,
IOracleLess oracleLessContract
Copy link
Contributor Author

Choose a reason for hiding this comment

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

) external override onlyOwner {
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 (pause) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

_pause();
} else {
Comment on lines +42 to +43
Copy link
Contributor Author

Choose a reason for hiding this comment

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

_unpause();
}
STOP_LIMIT_CONTRACT.pause(pause);
BRACKET_CONTRACT.pause(pause);
oracleLessContract.pause(pause);
}
Comment on lines +37 to +49
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines +47 to +49
Copy link
Contributor Author

Choose a reason for hiding this comment

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


///@notice register Stop Limit and Bracket order contracts
Copy link
Contributor Author

Choose a reason for hiding this comment

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

function registerSubKeepers(
Expand All @@ -38,13 +58,26 @@ contract AutomationMaster is IAutomationMaster, Ownable {
}

///@notice Registered Oracles are expected to return the USD price in 1e8 terms
///@notice to delist a token, the oracle address in the array should be set to address(0x0)
function registerOracle(
IERC20[] calldata _tokens,
IPythRelay[] calldata _oracles
) external onlyOwner {
require(_tokens.length == _oracles.length, "Array Length Mismatch");
for (uint i = 0; i < _tokens.length; i++) {
oracles[_tokens[i]] = _oracles[i];

for (uint256 i = 0; i < _tokens.length; i++) {
IERC20 token = _tokens[i];
IPythRelay oracle = _oracles[i];

oracles[token] = oracle;

if (address(oracle) == address(0x0)) {
// Remove the token from the unique set if oracle is address(0x0)
uniqueTokens.remove(address(token));
} else {
// Add the token to the unique set otherwise
uniqueTokens.add(address(token));
}
}
}

Expand All @@ -61,8 +94,35 @@ contract AutomationMaster is IAutomationMaster, Ownable {
///@notice sweep the entire balance of @param token to the owner
///@notice this contract should not hold funds other than collected fees,
///which are forwarded here after each transaction
function sweep(IERC20 token) external onlyOwner {
token.safeTransfer(owner(), token.balanceOf(address(this)));
function sweep(IERC20 token, address recipient) external onlyOwner {
token.safeTransfer(recipient, token.balanceOf(address(this)));
}

function sweepEther(address payable recipient) external onlyOwner {
uint256 balance = address(this).balance;
require(balance > 0, "No Ether to withdraw");
require(
recipient != address(0),
"Recipient cannot be the zero address"
);

(bool success, ) = recipient.call{value: balance}("");

require(success, "Ether transfer failed");
}

///@notice returns an array of each unique registered token
function getRegisteredTokens()
external
view
override
returns (IERC20[] memory tokens)
{
uint256 length = uniqueTokens.length();
tokens = new IERC20[](length);
for (uint256 i = 0; i < length; i++) {
tokens[i] = IERC20(uniqueTokens.at(i));
}
}

///@notice Registered Oracles are expected to return the USD price in 1e8 terms
Expand All @@ -87,9 +147,14 @@ contract AutomationMaster is IAutomationMaster, Ownable {
}

///@notice generate a random and unique order id
Copy link
Contributor Author

Choose a reason for hiding this comment

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

function generateOrderId(address sender) external view override returns (uint96) {
function generateOrderId(
address sender
) external override returns (uint96) {
uint96 nonce = nonces[sender]++;
Comment on lines +150 to +153
Copy link
Contributor Author

Choose a reason for hiding this comment

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

uint256 hashedValue = uint256(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

keccak256(abi.encodePacked(sender, block.timestamp))
keccak256(
abi.encodePacked(sender, nonce, blockhash(block.number - 1))
)
Comment on lines +155 to +157
Copy link
Contributor Author

Choose a reason for hiding this comment

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

);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

return uint96(hashedValue);
}
Expand Down Expand Up @@ -141,7 +206,10 @@ contract AutomationMaster is IAutomationMaster, Ownable {

///@notice determine if a new order meets the minimum order size requirement
///Value of @param amountIn of @param tokenIn must meed the minimum USD value
Comment on lines 207 to 208
Copy link
Contributor Author

Choose a reason for hiding this comment

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

function checkMinOrderSize(IERC20 tokenIn, uint256 amountIn) external view override {
function checkMinOrderSize(
IERC20 tokenIn,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

uint256 amountIn
) external view override {
Comment on lines +209 to +212
Copy link
Contributor Author

Choose a reason for hiding this comment

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

uint256 currentPrice = oracles[tokenIn].currentValue();
uint256 usdValue = (currentPrice * amountIn) /
Copy link
Contributor Author

Choose a reason for hiding this comment

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

(10 ** ERC20(address(tokenIn)).decimals());
Expand All @@ -151,28 +219,32 @@ contract AutomationMaster is IAutomationMaster, Ownable {

///@notice check upkeep on all order types
Copy link
Contributor Author

Choose a reason for hiding this comment

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

function checkUpkeep(
bytes calldata
bytes calldata checkData
Copy link
Contributor Author

Choose a reason for hiding this comment

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

)
external
view
override
returns (bool upkeepNeeded, bytes memory performData)
{
//check stop limit order
Copy link
Contributor Author

Choose a reason for hiding this comment

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

(upkeepNeeded, performData) = STOP_LIMIT_CONTRACT.checkUpkeep("0x");
(upkeepNeeded, performData) = STOP_LIMIT_CONTRACT.checkUpkeep(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

checkData
);
Comment on lines +230 to +232
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 (upkeepNeeded) {
return (true, performData);
Comment on lines +231 to 234
Copy link
Contributor Author

Choose a reason for hiding this comment

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

}
Copy link
Contributor Author

Choose a reason for hiding this comment

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


//check bracket order
Copy link
Contributor Author

Choose a reason for hiding this comment

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

(upkeepNeeded, performData) = BRACKET_CONTRACT.checkUpkeep("0x");
(upkeepNeeded, performData) = BRACKET_CONTRACT.checkUpkeep(checkData);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines 236 to +238
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 (upkeepNeeded) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

return (true, performData);
}
}

///@notice perform upkeep on any order type
Copy link
Contributor Author

Choose a reason for hiding this comment

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

function performUpkeep(bytes calldata performData) external override {
function performUpkeep(
Comment on lines 243 to +245
Copy link
Contributor Author

Choose a reason for hiding this comment

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

bytes calldata performData
) external override whenNotPaused {
Comment on lines +245 to +247
Copy link
Contributor Author

Choose a reason for hiding this comment

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

//decode into masterUpkeepData
MasterUpkeepData memory data = abi.decode(
performData,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
diff a/oku-custom-order-types/contracts/automatedTrigger/AutomationMaster.sol b/oku-custom-order-types/contracts/automatedTrigger/AutomationMaster.sol (rejected hunks)
@@ -2,17 +2,20 @@
pragma solidity ^0.8.19;

import "./IAutomation.sol";
+import "../oracle/IPythRelay.sol";
import "../libraries/ArrayMutation.sol";
import "../interfaces/openzeppelin/Ownable.sol";
import "../interfaces/openzeppelin/ERC20.sol";
import "../interfaces/openzeppelin/IERC20.sol";
import "../interfaces/openzeppelin/SafeERC20.sol";
-import "../oracle/IPythRelay.sol";
+import "../interfaces/openzeppelin/Pausable.sol";
+import "../interfaces/openzeppelin/EnumerableSet.sol";

///@notice This contract owns and handles all of the settings and accounting logic for automated swaps
///@notice This contract should not hold any user funds, only collected fees
-contract AutomationMaster is IAutomationMaster, Ownable {
+contract AutomationMaster is IAutomationMaster, Ownable, Pausable {
using SafeERC20 for IERC20;
+ using EnumerableSet for EnumerableSet.AddressSet;

///@notice maximum pending orders that may exist at a time, limiting the compute requriement for checkUpkeep
uint16 public maxPendingOrders;
@@ -20,13 +23,71 @@ contract AutomationMaster is IAutomationMaster, Ownable {
///@notice minumum USD value required to create a new order, in 1e8 terms
uint256 public minOrderSize;

+ ///@notice fee to create an order, in order to deter spam
+ uint256 public orderFee;
+
///sub keeper contracts
IStopLimit public STOP_LIMIT_CONTRACT;
IBracket public BRACKET_CONTRACT;

+ //whitelist of possible target contracts to execute swaps
+ mapping(address => bool) public safeTargets;
+
+ //whitelist of wallets allowed to set targets
+ mapping(address => bool) public targetSetters;
+
///each token must have a registered oracle in order to be tradable
mapping(IERC20 => IPythRelay) public oracles;
mapping(IERC20 => bytes32) public pythIds;
+ mapping(address => uint96) private nonces;
+
+ EnumerableSet.AddressSet private uniqueTokens;
+
+ constructor(address owner) {
+ _transferOwnership(owner);
+ }
+
+ receive() external payable {}
+
+ function pauseAll(
+ bool pause,
+ IOracleLess oracleLessContract
+ ) external override onlyOwner {
+ if (pause) {
+ _pause();
+ } else {
+ _unpause();
+ }
+ STOP_LIMIT_CONTRACT.pause(pause);
+ BRACKET_CONTRACT.pause(pause);
+ oracleLessContract.pause(pause);
+ }
+
+ ///@notice set the fee to create / modify orders
+ ///@notice fee is taken from msg.value in the native gas token
+ ///@param _orderFee is in wei
+ function setOrderFee(uint256 _orderFee) external override onlyOwner {
+ orderFee = _orderFee;
+ }
+
+ function whitelistTargetSetter(
+ address wallet,
+ bool canSet
+ ) external onlyOwner {
+ targetSetters[wallet] = canSet;
+ }
+
+ ///@notice toggle each idx in @param targets to be true/false as a valid target
+ function whitelistTargets(address[] calldata targets) external {
+ require(targetSetters[msg.sender], "!Allowed to set targets");
+ for (uint i = 0; i < targets.length; i++) {
+ safeTargets[targets[i]] = !safeTargets[targets[i]];
+ }
+ }
+
+ function validateTarget(address target) external view override {
+ require(safeTargets[target], "Target !Valid");
+ }

///@notice register Stop Limit and Bracket order contracts
function registerSubKeepers(
@@ -87,9 +188,14 @@ contract AutomationMaster is IAutomationMaster, Ownable {
}

///@notice generate a random and unique order id
- function generateOrderId(address sender) external view override returns (uint96) {
+ function generateOrderId(
+ address sender
+ ) external override returns (uint96) {
+ uint96 nonce = nonces[sender]++;
uint256 hashedValue = uint256(
- keccak256(abi.encodePacked(sender, block.timestamp))
+ keccak256(
+ abi.encodePacked(sender, nonce, blockhash(block.number - 1))
+ )
);
return uint96(hashedValue);
}
@@ -141,7 +247,10 @@ contract AutomationMaster is IAutomationMaster, Ownable {

///@notice determine if a new order meets the minimum order size requirement
///Value of @param amountIn of @param tokenIn must meed the minimum USD value
- function checkMinOrderSize(IERC20 tokenIn, uint256 amountIn) external view override {
+ function checkMinOrderSize(
+ IERC20 tokenIn,
+ uint256 amountIn
+ ) external view override {
uint256 currentPrice = oracles[tokenIn].currentValue();
uint256 usdValue = (currentPrice * amountIn) /
(10 ** ERC20(address(tokenIn)).decimals());
@@ -151,7 +260,7 @@ contract AutomationMaster is IAutomationMaster, Ownable {

///@notice check upkeep on all order types
function checkUpkeep(
- bytes calldata
+ bytes calldata checkData
)
external
view
@@ -159,20 +268,24 @@ contract AutomationMaster is IAutomationMaster, Ownable {
returns (bool upkeepNeeded, bytes memory performData)
{
//check stop limit order
- (upkeepNeeded, performData) = STOP_LIMIT_CONTRACT.checkUpkeep("0x");
+ (upkeepNeeded, performData) = STOP_LIMIT_CONTRACT.checkUpkeep(
+ checkData
+ );
if (upkeepNeeded) {
return (true, performData);
}

//check bracket order
- (upkeepNeeded, performData) = BRACKET_CONTRACT.checkUpkeep("0x");
+ (upkeepNeeded, performData) = BRACKET_CONTRACT.checkUpkeep(checkData);
if (upkeepNeeded) {
return (true, performData);
}
}

///@notice perform upkeep on any order type
- function performUpkeep(bytes calldata performData) external override {
+ function performUpkeep(
+ bytes calldata performData
+ ) external override whenNotPaused {
//decode into masterUpkeepData
MasterUpkeepData memory data = abi.decode(
performData,
Loading