-
Notifications
You must be signed in to change notification settings - Fork 39
Complex Orders #175
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
Open
Drypto13
wants to merge
102
commits into
development
Choose a base branch
from
complex-orders
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Complex Orders #175
Changes from all commits
Commits
Show all changes
102 commits
Select commit
Hold shift + click to select a range
02c39c9
dex selector with curve and univ3 implementations
Drypto13 cbfb1e0
Implementation fixes
Drypto13 03739d9
Init commit
Drypto13 54dcb26
passed tests
Drypto13 d830a61
Merge branch 'development' into dex-selection
Drypto13 040e8bc
Formatting
Drypto13 a7af5e0
Test Case
Drypto13 fdd5fc8
standardized inputs
Drypto13 d3f0f24
further cleanup
Drypto13 9ef3570
Formatting Specification
Drypto13 9f26413
edge case handling
Drypto13 6398bdf
resolve conflicts
Drypto13 ac8ad8f
cleanup
Drypto13 fd45c17
Update IBZx.sol
Drypto13 ee5bb2d
Update IToken.sol
Drypto13 dc3f347
Merge branch 'interest-model-redesign' into dex-selection
Drypto13 ce89686
Update LoanTokenLogicStandard.sol
Drypto13 9e765fb
Update IToken.sol
Drypto13 717cb79
Update SwapsExternal.sol
Drypto13 0551ad2
Update LoanTokenLogicStandard.sol
Drypto13 835f00d
Update IToken.sol
Drypto13 5cfc9aa
Merge branch 'development' into dex-selection
Drypto13 fcd9ccf
Flags
Drypto13 204237e
Update Flags.sol
Drypto13 ea2ee32
Merge branch 'development' into dex-selection
RomanHiden 4f3a50f
Merge branch 'interest-model-redesign' into dex-selection
RomanHiden f8a0d44
formated Flags
RomanHiden 34f7dec
formated loan opening l115
RomanHiden 78478fe
formated loan opening l124
RomanHiden 436fc66
formated swap external
RomanHiden ef25f84
Merge branch 'interest-model-redesign' into dex-selection
RomanHiden 1c8bf9d
minor var rename
RomanHiden a8d2a7d
optimized deployment cost
RomanHiden 4b9ad1f
moar optimizations
RomanHiden e1e27b7
revert changes
Drypto13 0424099
name changes
Drypto13 80939fc
Major refactor and partial re-design
Drypto13 1e97bf3
interface version changes
Drypto13 260689e
Merge branch 'dex-selection' into complex-orders
Drypto13 321b422
Format Compliant Calls
Drypto13 787d043
Update DexRecords.sol
Drypto13 8f60b2c
Update SwapsExternal.sol
Drypto13 929999e
Update SwapsUser.sol
Drypto13 c6dc85e
Formatting
Drypto13 f07ec3f
Merge branch 'dex-selection' into complex-orders
Drypto13 dfff2b6
Re-design on order storage
Drypto13 7b9feac
Merge branch 'interest-model-redesign' into complex-orders
Drypto13 ee6af34
Delete BytesLib.sol
Drypto13 709025a
cleanup
Drypto13 11c618f
optimizations
Drypto13 9e28dfa
minor fixes and limited test case
Drypto13 4355ece
edge case handling and minor improvements
Drypto13 b00403c
pausable guardian for orderbook contracts
Drypto13 4fbab92
Merge branch 'interest-model-redesign' into complex-orders
Drypto13 57229db
delegated manager upgrade with test cases
Drypto13 415ccab
minor improvements
Drypto13 dd6e3ed
some more optimizations
Drypto13 d32e767
bug fix
Drypto13 3d174b2
polygon test deployment
Drypto13 f8931ed
Merge branch 'interest-model-redesign' into complex-orders
Drypto13 d66921b
minor re-design for price execution
Drypto13 b10e127
minor fixes and interface update
Drypto13 55d0ad1
updated env
Drypto13 39c8647
partial re-structure for chainlink keeper contracts
Drypto13 f982b3b
full comments on interface
Drypto13 1ce6760
updated test case
Drypto13 ff496b1
keeper fix
Drypto13 962551e
min order size includes leverage
Drypto13 03095c9
minor fix on order sizing
Drypto13 46d77cb
changes
Drypto13 b1d5b85
Update LoanOpenings.sol
Drypto13 e1bf5db
Update IDexRecords.sol
Drypto13 a0a27d1
Merge branch 'interest-model-redesign' into complex-orders
Drypto13 6c18d32
Delete OrderBookEvents.sol
Drypto13 3239004
upgraded logic and keeper deployments
Drypto13 399f953
new deployment
Drypto13 e789bf8
user pays gas costs
Drypto13 bd6ce1a
remove minimum
Drypto13 1620d57
add checks and min trade size
Drypto13 ea95788
minor updates
Drypto13 7ff5634
Merge branch 'interest-model-redesign' into complex-orders
Drypto13 aeee948
passed tests and fixes for issues
Drypto13 ef8e6e2
fix(set-matic): case issue in script
bced548
interface update
Drypto13 af58be1
Merge branch 'development' into complex-orders
Drypto13 fd738b9
Merge branch 'development' into complex-orders
Drypto13 c82d33a
function name changes
Drypto13 610dc8a
avoid revert due to failure
Drypto13 3184123
use updated getSwapExpectedReturn
Drypto13 fa899da
minor fix
Drypto13 da33a86
minor bug fixes
Drypto13 f597cbd
min amount received on stop orders
Drypto13 aee96fa
Update OrderBook.sol
Drypto13 e0a0249
test cases
Drypto13 02174a5
Update IOrderBook.sol
Drypto13 db36955
Merge branch 'development' into complex-orders
Drypto13 e58a301
Merge branch 'development' into complex-orders
Drypto13 0d109ee
Merge branch 'development' into complex-orders
Drypto13 ee955af
OpenZeppelin version adjustment
Drypto13 bc99756
gas price fix
Drypto13 ab62376
Remove approve requirement for gas fee
Drypto13 3eceeba
Merge branch 'development' into complex-orders
0xosama File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,4 +59,4 @@ contract PausableGuardian_0_8 is Ownable { | |
guardian := sload(Pausable_GuardianAddress) | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
pragma solidity ^0.8.0; | ||
import "../IOrderBook.sol"; | ||
|
||
contract OrderBookEvents { | ||
event OrderCancelled(address indexed trader, bytes32 orderID); | ||
event OrderPlaced( | ||
address indexed trader, | ||
IOrderBook.OrderType indexed OrderType, | ||
uint256 indexed execPrice, | ||
bytes32 orderID, | ||
address collateralTokenAddress, | ||
address loanTokenAddress | ||
); | ||
event OrderExecuted(address indexed trader, bytes32 orderID); | ||
event OrderAmended( | ||
address indexed trader, | ||
IOrderBook.OrderType indexed OrderType, | ||
uint256 indexed execPrice, | ||
bytes32 orderID, | ||
address collateralTokenAddress, | ||
address loanTokenAddress | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
pragma solidity ^0.8.0; | ||
|
||
interface IOrderBook { | ||
enum OrderType { | ||
LIMIT_OPEN, | ||
LIMIT_CLOSE, | ||
MARKET_STOP | ||
} | ||
|
||
enum OrderStatus { | ||
ACTIVE, | ||
CANCELLED, | ||
EXECUTED | ||
} | ||
|
||
/* | ||
Used values for different order types: | ||
LIMIT_OPEN: | ||
loanID | ||
orderID | ||
amountReceived | ||
leverage | ||
loanTokenAmount | ||
collateralTokenAmount | ||
trader | ||
iToken | ||
loanTokenAddress | ||
base | ||
orderType | ||
status | ||
timeTillExpiration | ||
loanDataBytes | ||
LIMIT_CLOSE and MARKET_STOP: | ||
loanID | ||
orderID | ||
amountReceived | ||
loanTokenAmount | ||
collateralTokenAmount | ||
trader | ||
iToken | ||
loanTokenAddress | ||
base | ||
orderType | ||
status | ||
timeTillExpiration | ||
loanDataBytes | ||
*/ | ||
struct Order { | ||
bytes32 loanID; //ID of the loan on OOKI protocol | ||
bytes32 orderID; //order ID | ||
uint256 amountReceived; //amount received from the trade executing. Denominated in base for limit open and loanTokenAddress for limit close and market stop | ||
uint256 leverage; //leverage amount | ||
uint256 loanTokenAmount; //loan token amount denominated in loanTokenAddress | ||
uint256 collateralTokenAmount; //collateral token amount denominated in base | ||
address trader; //trader placing order | ||
address iToken; //iToken being interacted with | ||
address loanTokenAddress; //loan token | ||
address base; //collateral token | ||
OrderType orderType; //order type | ||
OrderStatus status; //order status | ||
uint64 timeTillExpiration; //Time till expiration. Useful for GTD and time-based cancellation | ||
bytes loanDataBytes; //data passed for margin trades | ||
} | ||
|
||
/// Returns proxy owner | ||
/// @return owner Contract owner | ||
function owner() external view returns(address owner); | ||
|
||
/// Returns guardian | ||
/// @return guardian Protocol guardian address | ||
function getGuardian() external view returns(address guardian); | ||
|
||
/// Returns Deposits contract address | ||
/// @return vault Deposits Contract | ||
function VAULT() external view returns(address vault); | ||
|
||
/// Returns Protocol contract address | ||
/// @return protocol ooki protocol contract | ||
function PROTOCOL() external view returns(address protocol); | ||
|
||
/// Returns minimum trade size in USDC | ||
/// @return size USDC amount | ||
function MIN_AMOUNT_IN_USDC() external view returns(uint256 size); | ||
|
||
/// Places new Order | ||
/// @param order Order Struct | ||
function placeOrder(Order calldata order) external; | ||
|
||
/// Amends Order | ||
/// @param order Order Struct | ||
function amendOrder(Order calldata order) external; | ||
|
||
/// Cancels Order | ||
/// @param orderID ID of order to be canceled | ||
function cancelOrder(bytes32 orderID) external; | ||
|
||
/// Cancels Order | ||
/// @param orderID ID of order to be canceled | ||
function cancelOrderProtocol(bytes32 orderID) external returns (uint256); | ||
|
||
/// Force cancels order | ||
/// @param orderID ID of order to be canceled | ||
function cancelOrderGuardian(bytes32 orderID) external; | ||
|
||
/// Changes stop type between index and dex price | ||
/// @param stopType true = index, false = dex price | ||
function changeStopType(bool stopType) external; | ||
|
||
/// Set price feed contract address | ||
/// @param newFeed new price feed contract | ||
function setPriceFeed(address newFeed) external; | ||
|
||
/// Set gas price to be used for incentives (if price feed does not already contain it) | ||
/// @param gasPrice gas price in gwei | ||
function setGasPrice(uint256 gasPrice) external; | ||
|
||
/// Return price feed contract address | ||
/// @return priceFeed Price Feed Contract Address | ||
function priceFeed() external view returns (address priceFeed); | ||
|
||
/// Returns gas price used for incentive calculations | ||
/// @return gasPrice gas price in gwei | ||
function getGasPrice() external view returns (uint256 gasPrice); | ||
|
||
/// Deposit Gas Token to pay out incentives for orders to be executed | ||
/// @param amount when depositing wrapped token, this is amount to be deposited (leave as 0 if sending native token) | ||
function depositGasFeeToken(uint256 amount) external payable; | ||
|
||
/// Withdraw Gas Token (received as native token) | ||
/// @param amount amount to be withdrawn | ||
function withdrawGasFeeToken(uint256 amount) external; | ||
|
||
/// Return amount received through a specified swap | ||
/// @param srcToken source token address | ||
/// @param destToken destination token address | ||
/// @param payload loanDataBytes passed for margin trades | ||
/// @param amountIn amount in for the swap | ||
function getDexRate(address srcToken, address destToken, bytes calldata payload, uint256 amountIn) external returns(uint256); | ||
|
||
/// Checks if order is able to be cleared from books due to failing to meet all requirements | ||
/// @param orderID order ID | ||
function clearOrder(bytes32 orderID) external view returns (bool); | ||
|
||
/// Returns list of orders that are up to be cleared. Used for Chainlink Keepers | ||
/// @param start starting index | ||
/// @param end ending index | ||
/// @return hasOrders true if the payload contains any orders | ||
/// @return payload bytes32[] encoded with the order IDs up for clearing from books | ||
function getClearOrderList(uint start, uint end) external view returns (bool hasOrders, bytes memory payload); | ||
|
||
/// Returns an order ID available for execution. Used for Chainlink Keepers | ||
/// @param start starting index | ||
/// @param end ending index | ||
/// @return ID order ID up for execution. If equal to 0 there is no order ID up for execution in the specified index range | ||
function getExecuteOrder(uint start, uint end) external returns (bytes32 ID); | ||
|
||
/// Checks if order meets requirements for execution | ||
/// @param orderID order ID of order being checked | ||
function prelimCheck(bytes32 orderID) external returns (bool); | ||
|
||
/// Returns oracle rate for a swap | ||
/// @param srcToken source token address | ||
/// @param destToken destination token address | ||
/// @param amount swap amount | ||
function queryRateReturn(address srcToken, address destToken, uint256 amount) external view returns(uint256); | ||
|
||
/// Checks if dex rate is within acceptable bounds from oracle rate | ||
/// @param srcToken source token address | ||
/// @param destToken destination token address | ||
/// @param payload loanDataBytes used for margin trade | ||
function priceCheck(address srcToken, address destToken, bytes calldata payload) external returns(bool); | ||
|
||
/// Executes Order | ||
/// @param orderID order ID | ||
/// @return incentiveAmountReceived amount received in gas token from exeuction of order | ||
function executeOrder(bytes32 orderID) external returns(uint256 incentiveAmountReceived); | ||
|
||
/// sets token allowances | ||
/// @param spenders addresses that will be given allowance | ||
/// @param tokens token addresses | ||
function adjustAllowance(address[] calldata spenders, address[] calldata tokens) external; | ||
|
||
/// revokes token allowances | ||
/// @param spenders addresses that will have allowance revoked | ||
/// @param tokens token addresses | ||
function revokeAllowance(address[] calldata spenders, address[] calldata tokens) external; | ||
|
||
/// Retrieves active orders for a trader | ||
/// @param trader address of trader | ||
function getUserOrders(address trader) external view returns (Order[] memory); | ||
|
||
/// Retrieves active orders for a trader | ||
/// @param trader address of trader | ||
/// @param start starting index | ||
/// @param end ending index | ||
function getUserOrdersLimited(address trader, uint256 start, uint256 end) external view returns (Order[] memory); | ||
|
||
/// Retrieves order corresponding to an order ID | ||
/// @param orderID order ID | ||
function getOrderByOrderID(bytes32 orderID) external view returns (Order memory); | ||
|
||
/// Retrieves active order IDs for a trader | ||
/// @param trader address of trader | ||
function getUserOrderIDs(address trader) external view returns (bytes32[] memory); | ||
|
||
/// Returns total active orders count for a trader | ||
/// @param trader address of trader | ||
function getUserOrdersCount(address trader) external view returns (uint256); | ||
|
||
/// Returns total active orders count | ||
function getGlobalOrdersCount() external view returns (uint256); | ||
|
||
/// Returns total active order IDs | ||
function getGlobalOrderIDs() external view returns (bytes32[] memory); | ||
|
||
/// Returns total active orders | ||
function getGlobalOrders() external view returns (Order[] memory); | ||
|
||
/// Returns active order IDs | ||
/// @param start starting index | ||
/// @param end ending index | ||
function getGlobalOrderIDsLimited(uint256 start, uint256 end) external view returns (bytes32[] memory); | ||
|
||
/// Returns active orders | ||
/// @param start starting index | ||
/// @param end ending index | ||
function getGlobalOrdersLimited(uint256 start, uint256 end) external view returns (Order[] memory); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
pragma solidity ^0.8.0; | ||
import "../IOrderBook.sol"; | ||
import "@openzeppelin-4.7.0/token/ERC20/extensions/IERC20Metadata.sol"; | ||
import "../../governance/PausableGuardian_0_8.sol"; | ||
|
||
contract OrderKeeper is PausableGuardian_0_8 { | ||
address public implementation; | ||
IERC20Metadata public constant WRAPPED_TOKEN = IERC20Metadata(0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270); | ||
IOrderBook public orderBook; | ||
|
||
function checkUpkeep(bytes calldata checkData) | ||
external | ||
returns (bool upkeepNeeded, bytes memory performData) | ||
{ | ||
(uint256 start, uint256 end) = abi.decode(checkData, (uint256, uint256)); | ||
uint256 orderIDLength = orderBook.getGlobalOrdersCount(); | ||
if (start > orderIDLength) { | ||
return (upkeepNeeded, performData); | ||
} | ||
if(end > orderIDLength) { | ||
end = orderIDLength; | ||
} | ||
bytes32 orderIDForExec = orderBook | ||
.getExecuteOrder(start, end); | ||
return (orderIDForExec != 0, abi.encode(orderIDForExec)); | ||
} | ||
|
||
function performUpkeep(bytes calldata performData) external pausable { | ||
bytes32 orderId = abi.decode(performData, (bytes32)); | ||
//emit OrderExecuted(trader,orderId); | ||
try orderBook.executeOrder(orderId) { | ||
|
||
} catch(bytes memory){} catch Error (string memory) {} | ||
} | ||
|
||
function setOrderBook(IOrderBook contractAddress) external onlyOwner { | ||
orderBook = contractAddress; | ||
} | ||
|
||
function withdrawIncentivesReceived(address receiver) external onlyOwner { | ||
WRAPPED_TOKEN.transfer(receiver, WRAPPED_TOKEN.balanceOf(address(this))); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
pragma solidity ^0.8.0; | ||
import "../IOrderBook.sol"; | ||
import "@openzeppelin-4.7.0/token/ERC20/extensions/IERC20Metadata.sol"; | ||
import "../../governance/PausableGuardian_0_8.sol"; | ||
|
||
contract OrderKeeperClear is PausableGuardian_0_8 { | ||
Drypto13 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
address public implementation; | ||
IERC20Metadata public constant WRAPPED_TOKEN = IERC20Metadata(0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270); | ||
IOrderBook public orderBook; | ||
|
||
function checkUpkeep(bytes calldata checkData) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so probably you will need multiple upkeep jobs in case that check does not fit into single blcok. thats why |
||
external | ||
view | ||
returns (bool upkeepNeeded, bytes memory performData) | ||
{ | ||
(uint256 start, uint256 end) = abi.decode(checkData, (uint256, uint256)); | ||
uint256 orderIDLength = orderBook.getGlobalOrdersCount(); | ||
if (start > orderIDLength) { | ||
return (upkeepNeeded, performData); | ||
} | ||
if(end > orderIDLength) { | ||
end = orderIDLength; | ||
} | ||
return orderBook.getClearOrderList(start, end); | ||
} | ||
|
||
function performUpkeep(bytes calldata performData) external pausable { | ||
bytes32[] memory orderId = abi.decode(performData, (bytes32[])); | ||
//emit OrderExecuted(trader,orderId); | ||
for (uint i;i<orderId.length;) { | ||
if(orderId[i]==0) { | ||
unchecked { ++i; } | ||
continue; | ||
} | ||
orderBook.cancelOrderProtocol(orderId[i]); | ||
unchecked { ++i; } | ||
} | ||
} | ||
|
||
function setOrderBook(IOrderBook contractAddress) external onlyOwner { | ||
orderBook = contractAddress; | ||
} | ||
|
||
function withdrawIncentivesReceived(address receiver) external onlyOwner { | ||
WRAPPED_TOKEN.transfer(receiver, WRAPPED_TOKEN.balanceOf(address(this))); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.