Contract Name:
OrderBookReader
Contract Source Code:
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.12 <0.9.0;
interface IOrderBookForReader {
function getSwapOrder(address _account, uint256 _orderIndex)
external
view
returns (
address path0,
address path1,
address path2,
uint256 amountIn,
uint256 minOut,
uint256 triggerRatio,
bool triggerAboveThreshold,
bool shouldUnwrap,
uint256 executionFee
);
function getIncreaseOrder(address _account, uint256 _orderIndex)
external
view
returns (
address purchaseToken,
uint256 purchaseTokenAmount,
address collateralToken,
address indexToken,
uint256 sizeDelta,
bool isLong,
uint256 triggerPrice,
bool triggerAboveThreshold,
uint256 executionFee
);
function getDecreaseOrder(address _account, uint256 _orderIndex)
external
view
returns (
address collateralToken,
uint256 collateralDelta,
address indexToken,
uint256 sizeDelta,
bool isLong,
uint256 triggerPrice,
bool triggerAboveThreshold,
uint256 executionFee
);
function getDecreaseOrderV2(address _account, uint256 _orderIndex)
external
view
returns (
address collateralToken,
address receiveToken,
uint256 collateralDelta,
address indexToken,
uint256 sizeDelta,
bool isLong,
uint256 triggerPrice,
bool triggerAboveThreshold,
uint256 executionFee
);
}
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
/**
* @dev Wrappers over Solidity's arithmetic operations with added overflow
* checks.
*
* Arithmetic operations in Solidity wrap on overflow. This can easily result
* in bugs, because programmers usually assume that an overflow raises an
* error, which is the standard behavior in high level programming languages.
* `SafeMath` restores this intuition by reverting the transaction when an
* operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*/
library SafeMath {
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
*
* - Addition cannot overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return sub(a, b, "SafeMath: subtraction overflow");
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
require(b <= a, errorMessage);
uint256 c = a - b;
return c;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
*
* - Multiplication cannot overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return div(a, b, "SafeMath: division by zero");
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts with custom message on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
require(b > 0, errorMessage);
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return mod(a, b, "SafeMath: modulo by zero");
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts with custom message when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(
uint256 a,
uint256 b,
string memory errorMessage
) internal pure returns (uint256) {
require(b != 0, errorMessage);
return a % b;
}
}
// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;
import "../libraries/math/SafeMath.sol";
import "../core/interfaces/IOrderBookForReader.sol";
contract OrderBookReader {
using SafeMath for uint256;
struct Vars {
uint256 i;
uint256 index;
address account;
uint256 uintLength;
uint256 addressLength;
}
function getIncreaseOrders(
address payable _orderBookAddress,
address _account,
uint256[] memory _indices
) external view returns (uint256[] memory, address[] memory) {
Vars memory vars = Vars(0, 0, _account, 5, 3);
uint256[] memory uintProps = new uint256[](vars.uintLength * _indices.length);
address[] memory addressProps = new address[](vars.addressLength * _indices.length);
IOrderBookForReader orderBook = IOrderBookForReader(_orderBookAddress);
while (vars.i < _indices.length) {
vars.index = _indices[vars.i];
(
address purchaseToken,
uint256 purchaseTokenAmount,
address collateralToken,
address indexToken,
uint256 sizeDelta,
bool isLong,
uint256 triggerPrice,
bool triggerAboveThreshold,
) = // uint256 executionFee
orderBook.getIncreaseOrder(vars.account, vars.index);
uintProps[vars.i * vars.uintLength] = uint256(purchaseTokenAmount);
uintProps[vars.i * vars.uintLength + 1] = uint256(sizeDelta);
uintProps[vars.i * vars.uintLength + 2] = uint256(isLong ? 1 : 0);
uintProps[vars.i * vars.uintLength + 3] = uint256(triggerPrice);
uintProps[vars.i * vars.uintLength + 4] = uint256(triggerAboveThreshold ? 1 : 0);
addressProps[vars.i * vars.addressLength] = (purchaseToken);
addressProps[vars.i * vars.addressLength + 1] = (collateralToken);
addressProps[vars.i * vars.addressLength + 2] = (indexToken);
vars.i++;
}
return (uintProps, addressProps);
}
function getDecreaseOrders(
address payable _orderBookAddress,
address _account,
uint256[] memory _indices
) external view returns (uint256[] memory, address[] memory) {
Vars memory vars = Vars(0, 0, _account, 5, 2);
uint256[] memory uintProps = new uint256[](vars.uintLength * _indices.length);
address[] memory addressProps = new address[](vars.addressLength * _indices.length);
IOrderBookForReader orderBook = IOrderBookForReader(_orderBookAddress);
while (vars.i < _indices.length) {
vars.index = _indices[vars.i];
(address collateralToken, uint256 collateralDelta, address indexToken, uint256 sizeDelta, bool isLong, uint256 triggerPrice, bool triggerAboveThreshold, ) = // uint256 executionFee
orderBook.getDecreaseOrder(vars.account, vars.index);
uintProps[vars.i * vars.uintLength] = uint256(collateralDelta);
uintProps[vars.i * vars.uintLength + 1] = uint256(sizeDelta);
uintProps[vars.i * vars.uintLength + 2] = uint256(isLong ? 1 : 0);
uintProps[vars.i * vars.uintLength + 3] = uint256(triggerPrice);
uintProps[vars.i * vars.uintLength + 4] = uint256(triggerAboveThreshold ? 1 : 0);
addressProps[vars.i * vars.addressLength] = (collateralToken);
addressProps[vars.i * vars.addressLength + 1] = (indexToken);
vars.i++;
}
return (uintProps, addressProps);
}
function getDecreaseOrdersV2(
address payable _orderBookAddress,
address _account,
uint256[] memory _indices
) external view returns (uint256[] memory, address[] memory) {
Vars memory vars = Vars(0, 0, _account, 5, 3);
uint256[] memory uintProps = new uint256[](vars.uintLength * _indices.length);
address[] memory addressProps = new address[](vars.addressLength * _indices.length);
IOrderBookForReader orderBook = IOrderBookForReader(_orderBookAddress);
while (vars.i < _indices.length) {
vars.index = _indices[vars.i];
(address collateralToken,address receiveToken, uint256 collateralDelta, address indexToken, uint256 sizeDelta, bool isLong, uint256 triggerPrice, bool triggerAboveThreshold, ) = // uint256 executionFee
orderBook.getDecreaseOrderV2(vars.account, vars.index);
uintProps[vars.i * vars.uintLength] = uint256(collateralDelta);
uintProps[vars.i * vars.uintLength + 1] = uint256(sizeDelta);
uintProps[vars.i * vars.uintLength + 2] = uint256(isLong ? 1 : 0);
uintProps[vars.i * vars.uintLength + 3] = uint256(triggerPrice);
uintProps[vars.i * vars.uintLength + 4] = uint256(triggerAboveThreshold ? 1 : 0);
addressProps[vars.i * vars.addressLength] = (collateralToken);
addressProps[vars.i * vars.addressLength + 1] = (indexToken);
addressProps[vars.i * vars.addressLength + 2] = (receiveToken);
vars.i++;
}
return (uintProps, addressProps);
}
function getSwapOrders(
address payable _orderBookAddress,
address _account,
uint256[] memory _indices
) external view returns (uint256[] memory, address[] memory) {
Vars memory vars = Vars(0, 0, _account, 5, 3);
uint256[] memory uintProps = new uint256[](vars.uintLength * _indices.length);
address[] memory addressProps = new address[](vars.addressLength * _indices.length);
IOrderBookForReader orderBook = IOrderBookForReader(_orderBookAddress);
while (vars.i < _indices.length) {
vars.index = _indices[vars.i];
(address path0, address path1, address path2, uint256 amountIn, uint256 minOut, uint256 triggerRatio, bool triggerAboveThreshold, bool shouldUnwrap, ) = // uint256 executionFee
orderBook.getSwapOrder(vars.account, vars.index);
uintProps[vars.i * vars.uintLength] = uint256(amountIn);
uintProps[vars.i * vars.uintLength + 1] = uint256(minOut);
uintProps[vars.i * vars.uintLength + 2] = uint256(triggerRatio);
uintProps[vars.i * vars.uintLength + 3] = uint256(triggerAboveThreshold ? 1 : 0);
uintProps[vars.i * vars.uintLength + 4] = uint256(shouldUnwrap ? 1 : 0);
addressProps[vars.i * vars.addressLength] = (path0);
addressProps[vars.i * vars.addressLength + 1] = (path1);
addressProps[vars.i * vars.addressLength + 2] = (path2);
vars.i++;
}
return (uintProps, addressProps);
}
}