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

Fix Review #1

wants to merge 2 commits into from

Conversation

sherlock-admin
Copy link
Contributor

Fix Review of

Repo: gfx-labs/oku-custom-order-types
Commit Hash: bc8d82dad10a3e426dc0fded79d5278ef23581c7

@@ -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.

@@ -2,16 +2,17 @@
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";
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
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.

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";

///@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
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
///@notice This contract should not hold any user funds, only collected fees
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.


///@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;

///@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.

@@ -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.

@@ -27,6 +28,25 @@ contract AutomationMaster is IAutomationMaster, Ownable {
///each token must have a registered oracle in order to be tradable
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.

@@ -27,6 +28,25 @@ contract AutomationMaster is IAutomationMaster, Ownable {
///each token must have a registered oracle in order to be tradable
mapping(IERC20 => IPythRelay) public oracles;
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.

Comment on lines +31 to +32
mapping(address => uint96) private nonces;

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 +33 to +36
constructor(address owner){
_transferOwnership(owner);
}

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 +37 to +49
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);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

STOP_LIMIT_CONTRACT.pause(pause);
BRACKET_CONTRACT.pause(pause);
oracleLessContract.pause(pause);
}

///@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.

@@ -87,9 +107,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.

Comment on lines +110 to +113
function generateOrderId(
address sender
) external override returns (uint96) {
uint96 nonce = nonces[sender]++;
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 override returns (uint96) {
uint96 nonce = nonces[sender]++;
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.

Comment on lines +115 to +117
keccak256(
abi.encodePacked(sender, nonce, blockhash(block.number - 1))
)
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))
)
);
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 167 to 168
///@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
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 +169 to +172
function checkMinOrderSize(
IERC20 tokenIn,
uint256 amountIn
) external view override {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -151,28 +179,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.

@@ -151,28 +179,32 @@ contract AutomationMaster is IAutomationMaster, Ownable {

///@notice check upkeep on all order types
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.

@@ -151,28 +179,32 @@ contract AutomationMaster is IAutomationMaster, Ownable {

///@notice check upkeep on all order types
function checkUpkeep(
bytes calldata
bytes calldata checkData
)
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.

Comment on lines +190 to +192
(upkeepNeeded, performData) = STOP_LIMIT_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.

(upkeepNeeded, performData) = STOP_LIMIT_CONTRACT.checkUpkeep("0x");
(upkeepNeeded, performData) = STOP_LIMIT_CONTRACT.checkUpkeep(
checkData
);
if (upkeepNeeded) {
return (true, performData);
}

//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.

if (upkeepNeeded) {
return (true, performData);
}

//check bracket order
(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.

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
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 +205 to +207
function performUpkeep(
bytes calldata performData
) external override whenNotPaused {
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,13 +10,14 @@ import "../interfaces/openzeppelin/Ownable.sol";
import "../interfaces/openzeppelin/IERC20.sol";
import "../interfaces/openzeppelin/SafeERC20.sol";
import "../interfaces/openzeppelin/ReentrancyGuard.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 +205 to +206
//emit event
emit OrderCancelled(order.orderId);
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 false;
//emit event
emit OrderCancelled(order.orderId);
}
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 +216 to 217
uint96 pendingOrderIdx,
bool permit,
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 218 to +224
bytes calldata permitPayload
) internal {
//fetch order
Order memory order = orders[orderId];

require(
order.orderId == pendingOrderIds[pendingOrderIdx],
"order doesn't exist"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

require(
order.orderId == pendingOrderIds[pendingOrderIdx],
"order doesn't exist"
);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

require(
order.orderId == pendingOrderIds[pendingOrderIdx],
"order doesn't exist"
);
require(msg.sender == order.recipient, "only order owner");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -207,6 +249,7 @@ contract OracleLess is IOracleLess, Ownable, ReentrancyGuard {
//refund position partially
order.tokenIn.safeTransfer(order.recipient, amountInDelta);
}
require(newAmountIn != 0, "newAmountIn == 0");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -207,6 +249,7 @@ contract OracleLess is IOracleLess, Ownable, ReentrancyGuard {
//refund position partially
order.tokenIn.safeTransfer(order.recipient, amountInDelta);
}
require(newAmountIn != 0, "newAmountIn == 0");
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

target,
(order.tokenIn.allowance(address(this), target))
);

uint256 finalTokenIn = order.tokenIn.balanceOf(address(this));
require(finalTokenIn >= initialTokenIn - order.amountIn, "over spend");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -8,10 +8,11 @@ import "../interfaces/openzeppelin/Ownable.sol";
import "../interfaces/openzeppelin/IERC20.sol";
import "../interfaces/openzeppelin/SafeERC20.sol";
import "../interfaces/openzeppelin/ReentrancyGuard.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.

@@ -8,10 +8,11 @@ import "../interfaces/openzeppelin/Ownable.sol";
import "../interfaces/openzeppelin/IERC20.sol";
import "../interfaces/openzeppelin/SafeERC20.sol";
import "../interfaces/openzeppelin/ReentrancyGuard.sol";
import "../interfaces/openzeppelin/Pausable.sol";

///@notice This contract owns and handles all logic associated with STOP_LIMIT orders
///STOP_LIMIT orders create a new Bracket order order with the same order ID once filled
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 logic associated with STOP_LIMIT orders
///STOP_LIMIT orders create a new Bracket order order with the same order ID once filled
contract StopLimit is Ownable, IStopLimit, ReentrancyGuard {
contract StopLimit is Ownable, IStopLimit, ReentrancyGuard, 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.

@@ -25,11 +26,25 @@ contract StopLimit is Ownable, IStopLimit, ReentrancyGuard {
constructor(
IAutomationMaster _master,
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 +29 to +30
IPermit2 _permit2,
address owner
Copy link
Contributor Author

Choose a reason for hiding this comment

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

) {
MASTER = _master;
BRACKET_CONTRACT = _bracket;
permit2 = _permit2;
_transferOwnership(owner);
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 +102 to 104
) external override nonReentrant whenNotPaused {
MasterUpkeepData memory data = abi.decode(
performData,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

address(BRACKET_CONTRACT),
(order.tokenIn.allowance(address(this), address(BRACKET_CONTRACT)))
);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

address(BRACKET_CONTRACT),
order.tokenIn,
order.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.

@@ -158,19 +195,22 @@ contract StopLimit is Ownable, IStopLimit, ReentrancyGuard {
bool swapOnFill,
bool permit,
bytes calldata permitPayload
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 +198 to 203
) external override nonReentrant whenNotPaused {
if (permit) {
require(amountIn < type(uint160).max, "uint160 overflow");
handlePermit(
recipient,
msg.sender,
permitPayload,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

handlePermit(
recipient,
msg.sender,
permitPayload,
uint160(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.

}

MASTER.checkMinOrderSize(tokenIn, amountIn);

_createOrder(
stopLimitPrice,
takeProfit,
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 permitPayload
) external override nonReentrant {
) external override nonReentrant whenNotPaused {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

require(
_amountInDelta < type(uint160).max,
"uint160 overflow"
);
handlePermit(
order.recipient,
permitPayload,
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 allow administrator to cancel any order
///@notice once cancelled, any funds assocaiated with the order are returned to the order recipient
///@notice only pending orders can be cancelled
function adminCancelOrder(
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 +360 to 362
_cancelOrder(order, pendingOrderIdx);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -348,37 +419,31 @@ contract StopLimit is Ownable, IStopLimit, ReentrancyGuard {
recipient: recipient,
direction: MASTER.getExchangeRate(tokenIn, tokenOut) >
stopLimitPrice, //compare to stop price for this order's direction
bracketDirection: bracketDirection,
swapOnFill: swapOnFill
});
pendingOrderIds.push(uint96(orderId));
//emit
emit OrderCreated(orderId);
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 +431 to +432
//remove from pending array
pendingOrderIds = ArrayMutation.removeFromArray(
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 handle signature and acquisition of asset with permit2
function handlePermit(
address owner,
address tokenOwner,
bytes calldata permitPayload,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

);
}
permit2.permit(tokenOwner, payload.permitSingle, payload.signature);
permit2.transferFrom(tokenOwner, address(this), amount, token);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant