Contract 0xac66E962A1C52B8a3B32AF432a60fFDBc99ebD0b

 
Txn Hash Method
Block
From
To
Value [Txn Fee]
0xe5722f1bd98b743146dc942aeaec72159380a00549c10ff38dd33ea3b8fbfd38Liquidate1157852862023-07-28 12:31:14244 days 14 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000386940.1
0xeeb802d5eb945fa945cf7aa53d98abfd87e67f740a7a5603cf6a3ffcb14eb669Liquidate1155890862023-07-27 22:14:34245 days 4 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000583820.1
0x5cdba47305e92ef67ae9cfaf42c098dc89ac46bbd5569a15641ac0344b83a68cLiquidate1152261802023-07-26 19:53:02246 days 6 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000734140.1
0x85f33efc13406179438e879a91f6373072c951975af012f87a02a9f942b6f1e9Liquidate1114240762023-07-15 9:49:20257 days 16 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000260240.1
0xdce35826bf7f057d3910acf12c5ff5ae0264d5766ff8875b07849c95836fba34Liquidate1108575132023-07-13 17:40:09259 days 8 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000839280.31797
0xd98e5a07d5489f02ede2a0ba1fc163e97f6bc1fd875b5559a8ca6f3cb5b420abLiquidate1083844592023-07-06 8:45:56266 days 17 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000832540.1
0xa331f8dc5b3b57275d98e2e553c447a0f62f07fd2178029b36f2f48cbe34ab88Liquidate1083566382023-07-06 6:46:48266 days 19 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000880170.1
0x8b62b56c9542772161d0e2ad5632ea2778a0cb323f77a985819765432cffd46aLiquidate1075022962023-07-03 15:49:38269 days 10 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000783470.1
0xc9912cd72f36e56f472dce08b9ffb1baa9a0bd488d9da23b967b76925e9e5c52Liquidate1074377422023-07-03 11:20:11269 days 15 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000599180.1
0xb5fdc73d413d142e40d51fc8be3499094f60ff5cf358ce6463273d62a5654e0fLiquidate1073797322023-07-03 7:17:58269 days 19 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000576080.1
0x26e4d3193167dc62ce7c77e806e9a50382a1965b3779542841e65a3d2b9bab34Liquidate1072607382023-07-02 22:36:14270 days 4 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000557250.1
0xbc48d9c46314f8f78535de9e3d26f66ebd388ed576d4c5a3e0f6ce8415b25a20Liquidate1072604972023-07-02 22:35:10270 days 4 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000363630.1
0x3bd3425037f57be8ff5c1e5e0cf2f929948af8245b08896d49308767d9521c75Liquidate1072588332023-07-02 22:28:05270 days 4 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000810190.16195
0x2955d41e7d63104c5d18497abf09c2216652865a36c4e433a200c83ecde517e1Liquidate1067583972023-07-01 10:17:07271 days 16 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.00058670.1
0xaf4d0a2ce186b04f9477bd342f7f6ce8a9ad4ed10f5c61f16744ff16ebfd362dLiquidate1065237592023-06-30 17:20:57272 days 9 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000457580.1
0xce2aa6b81a0b5666446e5fcf63c2ba5fd5ec19056ae4867bd69e77a5f20e20c9Liquidate1064702662023-06-30 13:37:32272 days 13 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000863730.38527
0x491f3f55b7769792ab58bf0daf031f7b4390eb805d60632dc6b400bcb3000255Liquidate1064414552023-06-30 11:37:37272 days 15 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000289440.1
0x1d171ce9406f94b83d9d1fcb3f227e0668ad9404c517f1d7a96453efb13615cdLiquidate1064412252023-06-30 11:36:39272 days 15 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000288830.1
0xa1ced4125e1dce0f933259ae6e289776c2d2c7d53c652ccbc9b1cf95051c9305Liquidate1064412132023-06-30 11:36:36272 days 15 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000297370.1
0x2510518d69ceafe1e8b5bac4de34ab5a0e5b5a408172664a7b578de3bdc434e6Liquidate1064235272023-06-30 10:23:02272 days 16 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000291870.1
0x767e8e80cfb6f8844c9e155417fabb380172b09840055779a87b4db49071dc55Liquidate1064180932023-06-30 10:00:30272 days 16 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000290920.1
0xbd870169b8d020c0f5fe163b30b4955a97bd208c52bb2141b848ca3678ef0849Liquidate1059251492023-06-28 23:07:52274 days 3 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000564780.1
0xddb184a7dcd004cc72072b08a39c781f245263d98e068cff628fee0432cd08c2Liquidate1057710002023-06-28 12:17:08274 days 14 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000509010.1
0x0a0bdd62fb845bc9e29c1484afe52defd4c5d4445707fe7703681fb228529cc7Liquidate1049687862023-06-26 3:16:48276 days 23 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000502530.1
0x359dd12b2277bfff64598cf55eda333934e97a5ecdcf5516c975e69aaa23327cLiquidate1049676622023-06-26 3:11:51276 days 23 hrs ago0x1ff6b8e1192eb0369006bbad76da9068b68961b2 IN  0xac66e962a1c52b8a3b32af432a60ffdbc99ebd0b0 ETH0.000450220.1
[ Download CSV Export 
Parent Txn Hash Block From To Value
Loading

Contract Source Code Verified (Exact Match)

Contract Name:
LiquidatorProxyV4WithGenericTrader

Compiler Version
v0.5.16+commit.9c3226ce

Optimization Enabled:
Yes with 10000 runs

Other Settings:
default evmVersion
File 1 of 43 : LiquidatorProxyV4WithGenericTrader.sol
/*

    Copyright 2022 Dolomite.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { ReentrancyGuard } from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";

import { IDolomiteMargin } from "../../protocol/interfaces/IDolomiteMargin.sol";

import { Account } from "../../protocol/lib/Account.sol";
import { Actions } from "../../protocol/lib/Actions.sol";
import { Require } from "../../protocol/lib/Require.sol";
import { Types } from "../../protocol/lib/Types.sol";

import { GenericTraderProxyBase } from "../helpers/GenericTraderProxyBase.sol";
import { HasLiquidatorRegistry } from "../helpers/HasLiquidatorRegistry.sol";
import { LiquidatorProxyBase } from "../helpers/LiquidatorProxyBase.sol";

import { IExpiry } from "../interfaces/IExpiry.sol";
import { IIsolationModeUnwrapperTrader } from "../interfaces/IIsolationModeUnwrapperTrader.sol";
import { IIsolationModeWrapperTrader } from "../interfaces/IIsolationModeWrapperTrader.sol";

import { AccountActionLib } from "../lib/AccountActionLib.sol";

import { LiquidatorProxyV2WithExternalLiquidity } from "./LiquidatorProxyV2WithExternalLiquidity.sol";


/**
 * @title LiquidatorProxyV4WithGenericTrader
 * @author Dolomite
 *
 * Contract for liquidating accounts in DolomiteMargin using generic traders. This contract should presumably work with
 * any liquidation strategy due to its generic implementation. As such, tremendous care should be taken to ensure that
 * the `traders` array passed to the `liquidate` function is correct and will not result in any unexpected behavior
 * for special assets like IsolationMode tokens.
 */
contract LiquidatorProxyV4WithGenericTrader is
    HasLiquidatorRegistry,
    LiquidatorProxyBase,
    GenericTraderProxyBase,
    ReentrancyGuard
{

    // ============ Constants ============

    bytes32 private constant FILE = "LiquidatorProxyV4";

    // ============ Storage ============

    IExpiry public EXPIRY;
    IDolomiteMargin public DOLOMITE_MARGIN;

    // ============ Constructor ============

    constructor (
        address _expiryProxy,
        address _dolomiteMargin,
        address _liquidatorAssetRegistry
    )
    public
    HasLiquidatorRegistry(
        _liquidatorAssetRegistry
    )
    {
        EXPIRY = IExpiry(_expiryProxy);
        DOLOMITE_MARGIN = IDolomiteMargin(_dolomiteMargin);
    }

    // ============ External Functions ============

    function liquidate(
        Account.Info memory _solidAccount,
        Account.Info memory _liquidAccount,
        uint256[] memory _marketIdsPath,
        uint256[] memory _amountWeisPath,
        TraderParam[] memory _tradersPath,
        Account.Info[] memory _makerAccounts,
        uint256 _expiry
    )
        public
        nonReentrant
    {
        GenericTraderProxyCache memory genericCache = GenericTraderProxyCache({
            dolomiteMargin: DOLOMITE_MARGIN,
            // unused for this function
            isMarginDeposit: false,
            // unused for this function
            otherAccountNumber: 0,
            // traders go right after the liquid account ("other account")
            traderAccountStartIndex: 2,
            actionsCursor: 0,
            // unused for this function
            inputBalanceWeiBeforeOperate: Types.zeroWei(),
            // unused for this function
            outputBalanceWeiBeforeOperate: Types.zeroWei(),
            // unused for this function
            transferBalanceWeiBeforeOperate: Types.zeroWei()
        });
        _validateMarketIdPath(_marketIdsPath);
        _validateAmountWeisPath(_marketIdsPath, _amountWeisPath);
        _validateTraderParams(
            genericCache,
            _marketIdsPath,
            _makerAccounts,
            _tradersPath
        );
        _validateAmountForFirstIndex(genericCache, _marketIdsPath[0], _amountWeisPath[0]);

        // put all values that will not change into a single struct
        LiquidatorProxyConstants memory constants;
        constants.dolomiteMargin = genericCache.dolomiteMargin;
        constants.solidAccount = _solidAccount;
        constants.liquidAccount = _liquidAccount;
        constants.heldMarket = _marketIdsPath[0];
        constants.owedMarket = _marketIdsPath[_marketIdsPath.length - 1];

        _checkConstants(constants, _expiry);
        _validateAssetForLiquidation(constants.heldMarket);
        _validateAssetForLiquidation(constants.owedMarket);

        constants.liquidMarkets = constants.dolomiteMargin.getAccountMarketsWithBalances(constants.liquidAccount);
        constants.markets = _getMarketInfos(
            constants.dolomiteMargin,
            constants.dolomiteMargin.getAccountMarketsWithBalances(_solidAccount),
            constants.liquidMarkets
        );
        constants.expiryProxy = _expiry > 0 ? EXPIRY: IExpiry(address(0)); // don't read EXPIRY; it's not needed
        constants.expiry = uint32(_expiry);

        LiquidatorProxyCache memory liquidatorCache = _initializeCache(constants);

        // validate the msg.sender and that the liquidAccount can be liquidated
        _checkBasicRequirements(constants);

        // get the max liquidation amount
        _calculateAndSetMaxLiquidationAmount(liquidatorCache);

        _calculateAndSetActualLiquidationAmount(_amountWeisPath, liquidatorCache);

        Account.Info[] memory accounts = _getAccounts(
            genericCache,
            _makerAccounts,
            _solidAccount.owner,
            _solidAccount.number
        );
        // the call to _getAccounts leaves accounts[1] null because it fills in the traders starting at the
        // `traderAccountCursor` index
        accounts[1] = _liquidAccount;

        Actions.ActionArgs[] memory actions = new Actions.ActionArgs[](
            /* liquidationActionsLength = */ 1 + _getActionsLengthForTraderParams(_tradersPath)
        );
        _appendLiquidationAction(
            actions,
            constants,
            liquidatorCache,
            genericCache
        );
        _appendTraderActions(
            accounts,
            actions,
            genericCache,
            _marketIdsPath,
            _amountWeisPath,
            _tradersPath
        );

        genericCache.dolomiteMargin.operate(accounts, actions);
    }

    function otherAccountIndex() public pure returns (uint256) {
        return 1;
    }

    // ============ Internal Functions ============

    function _validateAmountForFirstIndex(
        GenericTraderProxyCache memory _cache,
        uint256 _marketId,
        uint256 _amountWei
    ) internal view {
        if (_isIsolationModeMarket(_cache, _marketId)) {
            // For liquidations, the asset amount must match the amount of collateral transferred from liquid account
            // to solid account. This is done via always selling the max amount of held collateral in the amountWeisPath
            // variable.
            Require.that(
                _amountWei == uint256(-1),
                FILE,
                "Invalid amount for IsolationMode"
            );
        }
    }

    function _appendLiquidationAction(
        Actions.ActionArgs[] memory _actions,
        LiquidatorProxyConstants memory _constants,
        LiquidatorProxyCache memory _liquidatorCache,
        GenericTraderProxyCache memory _genericCache
    )
        internal
        pure
    {
        // solidAccountId is always at index 0, liquidAccountId is always at index 1
        if (_constants.expiry > 0) {
            _actions[_genericCache.actionsCursor++] = AccountActionLib.encodeExpiryLiquidateAction(
                /* _solidAccountId = */ 0,
                /* _liquidAccountId = */ 1,
                _constants.owedMarket,
                _constants.heldMarket,
                address(_constants.expiryProxy),
                _constants.expiry,
                _liquidatorCache.solidHeldUpdateWithReward,
                _liquidatorCache.owedWeiToLiquidate,
                _liquidatorCache.flipMarketsForExpiration
            );
        } else {
            _actions[_genericCache.actionsCursor++] = AccountActionLib.encodeLiquidateAction(
                /* _solidAccountId = */ 0,
                /* _liquidAccountId = */ 1,
                _constants.owedMarket,
                _constants.heldMarket,
                _liquidatorCache.owedWeiToLiquidate
            );
        }
    }
}

File 2 of 43 : ReentrancyGuard.sol
pragma solidity ^0.5.0;

/**
 * @dev Contract module that helps prevent reentrant calls to a function.
 *
 * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
 * available, which can be applied to functions to make sure there are no nested
 * (reentrant) calls to them.
 *
 * Note that because there is a single `nonReentrant` guard, functions marked as
 * `nonReentrant` may not call one another. This can be worked around by making
 * those functions `private`, and then adding `external` `nonReentrant` entry
 * points to them.
 *
 * TIP: If you would like to learn more about reentrancy and alternative ways
 * to protect against it, check out our blog post
 * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
 *
 * _Since v2.5.0:_ this module is now much more gas efficient, given net gas
 * metering changes introduced in the Istanbul hardfork.
 */
contract ReentrancyGuard {
    bool private _notEntered;

    constructor () internal {
        // Storing an initial non-zero value makes deployment a bit more
        // expensive, but in exchange the refund on every call to nonReentrant
        // will be lower in amount. Since refunds are capped to a percetange of
        // the total transaction's gas, it is best to keep them low in cases
        // like this one, to increase the likelihood of the full refund coming
        // into effect.
        _notEntered = true;
    }

    /**
     * @dev Prevents a contract from calling itself, directly or indirectly.
     * Calling a `nonReentrant` function from another `nonReentrant`
     * function is not supported. It is possible to prevent this from happening
     * by making the `nonReentrant` function external, and make it call a
     * `private` function that does the actual work.
     */
    modifier nonReentrant() {
        // On the first call to nonReentrant, _notEntered will be true
        require(_notEntered, "ReentrancyGuard: reentrant call");

        // Any calls to nonReentrant after this point will fail
        _notEntered = false;

        _;

        // By storing the original value once again, a refund is triggered (see
        // https://eips.ethereum.org/EIPS/eip-2200)
        _notEntered = true;
    }
}

File 3 of 43 : Address.sol
pragma solidity ^0.5.5;

/**
 * @dev Collection of functions related to the address type
 */
library Address {
    /**
     * @dev Returns true if `account` is a contract.
     *
     * [IMPORTANT]
     * ====
     * It is unsafe to assume that an address for which this function returns
     * false is an externally-owned account (EOA) and not a contract.
     *
     * Among others, `isContract` will return false for the following 
     * types of addresses:
     *
     *  - an externally-owned account
     *  - a contract in construction
     *  - an address where a contract will be created
     *  - an address where a contract lived, but was destroyed
     * ====
     */
    function isContract(address account) internal view returns (bool) {
        // According to EIP-1052, 0x0 is the value returned for not-yet created accounts
        // and 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 is returned
        // for accounts without code, i.e. `keccak256('')`
        bytes32 codehash;
        bytes32 accountHash = 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470;
        // solhint-disable-next-line no-inline-assembly
        assembly { codehash := extcodehash(account) }
        return (codehash != accountHash && codehash != 0x0);
    }

    /**
     * @dev Converts an `address` into `address payable`. Note that this is
     * simply a type cast: the actual underlying value is not changed.
     *
     * _Available since v2.4.0._
     */
    function toPayable(address account) internal pure returns (address payable) {
        return address(uint160(account));
    }

    /**
     * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
     * `recipient`, forwarding all available gas and reverting on errors.
     *
     * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
     * of certain opcodes, possibly making contracts go over the 2300 gas limit
     * imposed by `transfer`, making them unable to receive funds via
     * `transfer`. {sendValue} removes this limitation.
     *
     * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
     *
     * IMPORTANT: because control is transferred to `recipient`, care must be
     * taken to not create reentrancy vulnerabilities. Consider using
     * {ReentrancyGuard} or the
     * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
     *
     * _Available since v2.4.0._
     */
    function sendValue(address payable recipient, uint256 amount) internal {
        require(address(this).balance >= amount, "Address: insufficient balance");

        // solhint-disable-next-line avoid-call-value
        (bool success, ) = recipient.call.value(amount)("");
        require(success, "Address: unable to send value, recipient may have reverted");
    }
}

File 4 of 43 : SafeERC20.sol
pragma solidity ^0.5.0;

import "./IERC20.sol";
import "../../math/SafeMath.sol";
import "../../utils/Address.sol";

/**
 * @title SafeERC20
 * @dev Wrappers around ERC20 operations that throw on failure (when the token
 * contract returns false). Tokens that return no value (and instead revert or
 * throw on failure) are also supported, non-reverting calls are assumed to be
 * successful.
 * To use this library you can add a `using SafeERC20 for ERC20;` statement to your contract,
 * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
 */
library SafeERC20 {
    using SafeMath for uint256;
    using Address for address;

    function safeTransfer(IERC20 token, address to, uint256 value) internal {
        callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
    }

    function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
        callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
    }

    function safeApprove(IERC20 token, address spender, uint256 value) internal {
        // safeApprove should only be called when setting an initial allowance,
        // or when resetting it to zero. To increase and decrease it, use
        // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
        // solhint-disable-next-line max-line-length
        require((value == 0) || (token.allowance(address(this), spender) == 0),
            "SafeERC20: approve from non-zero to non-zero allowance"
        );
        callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
    }

    function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
        uint256 newAllowance = token.allowance(address(this), spender).add(value);
        callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
    }

    function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
        uint256 newAllowance = token.allowance(address(this), spender).sub(value, "SafeERC20: decreased allowance below zero");
        callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
    }

    /**
     * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
     * on the return value: the return value is optional (but if data is returned, it must not be false).
     * @param token The token targeted by the call.
     * @param data The call data (encoded using abi.encode or one of its variants).
     */
    function callOptionalReturn(IERC20 token, bytes memory data) private {
        // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
        // we're implementing it ourselves.

        // A Solidity high level call has three parts:
        //  1. The target address is checked to verify it contains contract code
        //  2. The call itself is made, and success asserted
        //  3. The return value is decoded, which in turn checks the size of the returned data.
        // solhint-disable-next-line max-line-length
        require(address(token).isContract(), "SafeERC20: call to non-contract");

        // solhint-disable-next-line avoid-low-level-calls
        (bool success, bytes memory returndata) = address(token).call(data);
        require(success, "SafeERC20: low-level call failed");

        if (returndata.length > 0) { // Return data is optional
            // solhint-disable-next-line max-line-length
            require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
        }
    }
}

File 5 of 43 : IERC20.sol
pragma solidity ^0.5.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP. Does not include
 * the optional functions; to access them see {ERC20Detailed}.
 */
interface IERC20 {
    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `recipient`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `sender` to `recipient` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);
}

File 6 of 43 : SafeMath.sol
pragma solidity ^0.5.0;

/**
 * @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.
     *
     * _Available since v2.4.0._
     */
    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.
     *
     * _Available since v2.4.0._
     */
    function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        // Solidity only automatically asserts when dividing by 0
        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.
     *
     * _Available since v2.4.0._
     */
    function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b != 0, errorMessage);
        return a % b;
    }
}

File 7 of 43 : Types.sol
/*

    Copyright 2019 dYdX Trading Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol";
import { DolomiteMarginMath } from "./DolomiteMarginMath.sol";


/**
 * @title Types
 * @author dYdX
 *
 * Library for interacting with the basic structs used in DolomiteMargin
 */
library Types {
    using DolomiteMarginMath for uint256;

    // ============ Permission ============

    struct OperatorArg {
        address operator;
        bool trusted;
    }

    // ============ AssetAmount ============

    enum AssetDenomination {
        Wei, // the amount is denominated in wei
        Par  // the amount is denominated in par
    }

    enum AssetReference {
        Delta, // the amount is given as a delta from the current value
        Target // the amount is given as an exact number to end up at
    }

    struct AssetAmount {
        bool sign; // true if positive
        AssetDenomination denomination;
        AssetReference ref;
        uint256 value;
    }

    // ============ Par (Principal Amount) ============

    // Total borrow and supply values for a market
    struct TotalPar {
        uint128 borrow;
        uint128 supply;
    }

    // Individual principal amount for an account
    struct Par {
        bool sign; // true if positive
        uint128 value;
    }

    function zeroPar()
        internal
        pure
        returns (Par memory)
    {
        return Par({
            sign: false,
            value: 0
        });
    }

    function sub(
        Par memory a,
        Par memory b
    )
        internal
        pure
        returns (Par memory)
    {
        return add(a, negative(b));
    }

    function add(
        Par memory a,
        Par memory b
    )
        internal
        pure
        returns (Par memory)
    {
        Par memory result;
        if (a.sign == b.sign) {
            result.sign = a.sign;
            result.value = SafeMath.add(a.value, b.value).to128();
        } else {
            if (a.value >= b.value) {
                result.sign = a.sign;
                result.value = SafeMath.sub(a.value, b.value).to128();
            } else {
                result.sign = b.sign;
                result.value = SafeMath.sub(b.value, a.value).to128();
            }
        }
        return result;
    }

    function equals(
        Par memory a,
        Par memory b
    )
        internal
        pure
        returns (bool)
    {
        if (a.value == b.value) {
            if (a.value == 0) {
                return true;
            }
            return a.sign == b.sign;
        }
        return false;
    }

    function negative(
        Par memory a
    )
        internal
        pure
        returns (Par memory)
    {
        return Par({
            sign: !a.sign,
            value: a.value
        });
    }

    function isNegative(
        Par memory a
    )
        internal
        pure
        returns (bool)
    {
        return !a.sign && a.value > 0;
    }

    function isPositive(
        Par memory a
    )
        internal
        pure
        returns (bool)
    {
        return a.sign && a.value > 0;
    }

    function isZero(
        Par memory a
    )
        internal
        pure
        returns (bool)
    {
        return a.value == 0;
    }

    function isLessThanZero(
        Par memory a
    )
        internal
        pure
        returns (bool)
    {
        return a.value > 0 && !a.sign;
    }

    function isGreaterThanOrEqualToZero(
        Par memory a
    )
        internal
        pure
        returns (bool)
    {
        return isZero(a) || a.sign;
    }

    // ============ Wei (Token Amount) ============

    // Individual token amount for an account
    struct Wei {
        bool sign; // true if positive
        uint256 value;
    }

    function zeroWei()
        internal
        pure
        returns (Wei memory)
    {
        return Wei({
            sign: false,
            value: 0
        });
    }

    function sub(
        Wei memory a,
        Wei memory b
    )
        internal
        pure
        returns (Wei memory)
    {
        return add(a, negative(b));
    }

    function add(
        Wei memory a,
        Wei memory b
    )
        internal
        pure
        returns (Wei memory)
    {
        Wei memory result;
        if (a.sign == b.sign) {
            result.sign = a.sign;
            result.value = SafeMath.add(a.value, b.value);
        } else {
            if (a.value >= b.value) {
                result.sign = a.sign;
                result.value = SafeMath.sub(a.value, b.value);
            } else {
                result.sign = b.sign;
                result.value = SafeMath.sub(b.value, a.value);
            }
        }
        return result;
    }

    function equals(
        Wei memory a,
        Wei memory b
    )
        internal
        pure
        returns (bool)
    {
        if (a.value == b.value) {
            if (a.value == 0) {
                return true;
            }
            return a.sign == b.sign;
        }
        return false;
    }

    function negative(
        Wei memory a
    )
        internal
        pure
        returns (Wei memory)
    {
        return Wei({
            sign: !a.sign,
            value: a.value
        });
    }

    function isNegative(
        Wei memory a
    )
        internal
        pure
        returns (bool)
    {
        return !a.sign && a.value > 0;
    }

    function isPositive(
        Wei memory a
    )
        internal
        pure
        returns (bool)
    {
        return a.sign && a.value > 0;
    }

    function isZero(
        Wei memory a
    )
        internal
        pure
        returns (bool)
    {
        return a.value == 0;
    }
}

File 8 of 43 : Token.sol
/*

    Copyright 2019 dYdX Trading Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { IERC20Detailed } from "../interfaces/IERC20Detailed.sol";


/**
 * @title Token
 * @author dYdX
 *
 * This library contains basic functions for interacting with ERC20 tokens. Modified to work with
 * tokens that don't adhere strictly to the ERC20 standard (for example tokens that don't return a
 * boolean value on success).
 */
library Token {

    // ============ Library Functions ============

    function transfer(
        address token,
        address to,
        uint256 amount
    )
        internal
    {
        if (amount == 0 || to == address(this)) {
            return;
        }

        _callOptionalReturn(
            token,
            abi.encodeWithSelector(IERC20Detailed(token).transfer.selector, to, amount),
            "Token: transfer failed"
        );
    }

    function transferFrom(
        address token,
        address from,
        address to,
        uint256 amount
    )
        internal
    {
        if (amount == 0 || to == from) {
            return;
        }

        // solium-disable arg-overflow
        _callOptionalReturn(
            token,
            abi.encodeWithSelector(IERC20Detailed(token).transferFrom.selector, from, to, amount),
            "Token: transferFrom failed"
        );
        // solium-enable arg-overflow
    }

    // ============ Private Functions ============

    function _callOptionalReturn(address token, bytes memory data, string memory error) private {
        // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
        // we're implementing it ourselves.

        // A Solidity high level call has three parts:
        // 1. The target address is checked to contain contract code. Not needed since tokens are manually added
        // 2. The call itself is made, and success asserted
        // 3. The return value is decoded, which in turn checks the size of the returned data.

        // solium-disable-next-line security/no-low-level-calls
        (bool success, bytes memory returnData) = token.call(data);
        require(success, error);

        if (returnData.length > 0) {
            // Return data is optional
            require(abi.decode(returnData, (bool)), error);
        }
    }

}

File 9 of 43 : Time.sol
/*

    Copyright 2019 dYdX Trading Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { DolomiteMarginMath } from "./DolomiteMarginMath.sol";


/**
 * @title Time
 * @author dYdX
 *
 * Library for dealing with time, assuming timestamps fit within 32 bits (valid until year 2106)
 */
library Time {

    // ============ Library Functions ============

    function currentTime()
        internal
        view
        returns (uint32)
    {
        return DolomiteMarginMath.to32(block.timestamp);
    }
}

File 10 of 43 : Storage.sol
/*

    Copyright 2019 dYdX Trading Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol";
import { Account } from "./Account.sol";
import { Bits } from "./Bits.sol";
import { Cache } from "./Cache.sol";
import { Decimal } from "./Decimal.sol";
import { Interest } from "./Interest.sol";
import { EnumerableSet } from "./EnumerableSet.sol";
import { DolomiteMarginMath } from "./DolomiteMarginMath.sol";
import { Monetary } from "./Monetary.sol";
import { Require } from "./Require.sol";
import { Time } from "./Time.sol";
import { Token } from "./Token.sol";
import { Types } from "./Types.sol";
import { IERC20Detailed } from "../interfaces/IERC20Detailed.sol";
import { IInterestSetter } from "../interfaces/IInterestSetter.sol";
import { IPriceOracle } from "../interfaces/IPriceOracle.sol";


/**
 * @title Storage
 * @author dYdX
 *
 * Functions for reading, writing, and verifying state in DolomiteMargin
 */
library Storage {
    using Cache for Cache.MarketCache;
    using Storage for Storage.State;
    using DolomiteMarginMath for uint256;
    using Types for Types.Par;
    using Types for Types.Wei;
    using SafeMath for uint256;
    using EnumerableSet for EnumerableSet.Set;

    // ============ Constants ============

    bytes32 private constant FILE = "Storage";

    // ============ Structs ============

    // All information necessary for tracking a market
    struct Market {
        // Contract address of the associated ERC20 token
        address token;

        // Whether additional borrows are allowed for this market
        bool isClosing;

        // Whether this market can be removed and its ID can be recycled and reused
        bool isRecyclable;

        // Total aggregated supply and borrow amount of the entire market
        Types.TotalPar totalPar;

        // Interest index of the market
        Interest.Index index;

        // Contract address of the price oracle for this market
        IPriceOracle priceOracle;

        // Contract address of the interest setter for this market
        IInterestSetter interestSetter;

        // Multiplier on the marginRatio for this market, IE 5% (0.05 * 1e18). This number increases the market's
        // required collateralization by: reducing the user's supplied value (in terms of dollars) for this market and
        // increasing its borrowed value. This is done through the following operation:
        // `suppliedWei = suppliedWei + (assetValueForThisMarket / (1 + marginPremium))`
        // This number increases the user's borrowed wei by multiplying it by:
        // `borrowedWei = borrowedWei + (assetValueForThisMarket * (1 + marginPremium))`
        Decimal.D256 marginPremium;

        // Multiplier on the liquidationSpread for this market, IE 20% (0.2 * 1e18). This number increases the
        // `liquidationSpread` using the following formula:
        // `liquidationSpread = liquidationSpread * (1 + spreadPremium)`
        // NOTE: This formula is applied up to two times - one for each market whose spreadPremium is greater than 0
        // (when performing a liquidation between two markets)
        Decimal.D256 spreadPremium;

        // The maximum amount that can be held by the protocol. This allows the protocol to cap any additional risk
        // that is inferred by allowing borrowing against low-cap or assets with increased volatility. Setting this
        // value to 0 is analogous to having no limit. This value can never be below 0.
        Types.Wei maxWei;
    }

    // The global risk parameters that govern the health and security of the system
    struct RiskParams {
        // Required ratio of over-collateralization
        Decimal.D256 marginRatio;

        // Percentage penalty incurred by liquidated accounts
        Decimal.D256 liquidationSpread;

        // Percentage of the borrower's interest fee that gets passed to the suppliers
        Decimal.D256 earningsRate;

        // The minimum absolute borrow value of an account
        // There must be sufficient incentivize to liquidate undercollateralized accounts
        Monetary.Value minBorrowedValue;

        // The maximum number of markets a user can have a non-zero balance for a given account.
        uint256 accountMaxNumberOfMarketsWithBalances;
    }

    // The maximum RiskParam values that can be set
    struct RiskLimits {
        // The highest that the ratio can be for liquidating under-water accounts
        uint64 marginRatioMax;
        // The highest that the liquidation rewards can be when a liquidator liquidates an account
        uint64 liquidationSpreadMax;
        // The highest that the supply APR can be for a market, as a proportion of the borrow rate. Meaning, a rate of
        // 100% (1e18) would give suppliers all of the interest that borrowers are paying. A rate of 90% would give
        // suppliers 90% of the interest that borrowers pay.
        uint64 earningsRateMax;
        // The highest min margin ratio premium that can be applied to a particular market. Meaning, a value of 100%
        // (1e18) would require borrowers to maintain an extra 100% collateral to maintain a healthy margin ratio. This
        // value works by increasing the debt owed and decreasing the supply held for the particular market by this
        // amount, plus 1e18 (since a value of 10% needs to be applied as `decimal.plusOne`)
        uint64 marginPremiumMax;
        // The highest liquidation reward that can be applied to a particular market. This percentage is applied
        // in addition to the liquidation spread in `RiskParams`. Meaning a value of 1e18 is 100%. It is calculated as:
        // `liquidationSpread * Decimal.onePlus(spreadPremium)`
        uint64 spreadPremiumMax;
        uint128 minBorrowedValueMax;
    }

    // The entire storage state of DolomiteMargin
    struct State {
        // number of markets
        uint256 numMarkets;

        // marketId => Market
        mapping (uint256 => Market) markets;

        // token address => marketId
        mapping (address => uint256) tokenToMarketId;

        // Linked list from marketId to the next recycled market id
        mapping(uint256 => uint256) recycledMarketIds;

        // owner => account number => Account
        mapping (address => mapping (uint256 => Account.Storage)) accounts;

        // Addresses that can control other users accounts
        mapping (address => mapping (address => bool)) operators;

        // Addresses that can control all users accounts
        mapping (address => bool) globalOperators;

        // Addresses of auto traders that can only be called by global operators. IE for expirations
        mapping (address => bool) specialAutoTraders;

        // mutable risk parameters of the system
        RiskParams riskParams;

        // immutable risk limits of the system
        RiskLimits riskLimits;
    }

    // ============ Functions ============

    function getToken(
        Storage.State storage state,
        uint256 marketId
    )
        internal
        view
        returns (address)
    {
        return state.markets[marketId].token;
    }

    function getTotalPar(
        Storage.State storage state,
        uint256 marketId
    )
        internal
        view
        returns (Types.TotalPar memory)
    {
        return state.markets[marketId].totalPar;
    }

    function getMaxWei(
        Storage.State storage state,
        uint256 marketId
    )
        internal
        view
        returns (Types.Wei memory)
    {
        return state.markets[marketId].maxWei;
    }

    function getIndex(
        Storage.State storage state,
        uint256 marketId
    )
        internal
        view
        returns (Interest.Index memory)
    {
        return state.markets[marketId].index;
    }

    function getNumExcessTokens(
        Storage.State storage state,
        uint256 marketId
    )
        internal
        view
        returns (Types.Wei memory)
    {
        Interest.Index memory index = state.getIndex(marketId);
        Types.TotalPar memory totalPar = state.getTotalPar(marketId);

        address token = state.getToken(marketId);

        Types.Wei memory balanceWei = Types.Wei({
            sign: true,
            value: IERC20Detailed(token).balanceOf(address(this))
        });

        (
            Types.Wei memory supplyWei,
            Types.Wei memory borrowWei
        ) = Interest.totalParToWei(totalPar, index);

        // borrowWei is negative, so subtracting it makes the value more positive
        return balanceWei.sub(borrowWei).sub(supplyWei);
    }

    function getStatus(
        Storage.State storage state,
        Account.Info memory account
    )
        internal
        view
        returns (Account.Status)
    {
        return state.accounts[account.owner][account.number].status;
    }

    function getPar(
        Storage.State storage state,
        Account.Info memory account,
        uint256 marketId
    )
        internal
        view
        returns (Types.Par memory)
    {
        return state.accounts[account.owner][account.number].balances[marketId];
    }

    function getWei(
        Storage.State storage state,
        Account.Info memory account,
        uint256 marketId,
        Interest.Index memory index
    )
        internal
        view
        returns (Types.Wei memory)
    {
        Types.Par memory par = state.getPar(account, marketId);

        if (par.isZero()) {
            return Types.zeroWei();
        }

        return Interest.parToWei(par, index);
    }

    function getMarketsWithBalancesSet(
        Storage.State storage state,
        Account.Info memory account
    )
    internal
    view
    returns (EnumerableSet.Set storage)
    {
        return state.accounts[account.owner][account.number].marketsWithNonZeroBalanceSet;
    }

    function getMarketsWithBalances(
        Storage.State storage state,
        Account.Info memory account
    )
    internal
    view
    returns (uint256[] memory)
    {
        return state.accounts[account.owner][account.number].marketsWithNonZeroBalanceSet.values();
    }

    function getAccountMarketWithBalanceAtIndex(
        Storage.State storage state,
        Account.Info memory account,
        uint256 index
    )
    internal
    view
    returns (uint256)
    {
        return state.accounts[account.owner][account.number].marketsWithNonZeroBalanceSet.getAtIndex(index);
    }

    function getNumberOfMarketsWithBalances(
        Storage.State storage state,
        Account.Info memory account
    )
    internal
    view
    returns (uint256)
    {
        return state.accounts[account.owner][account.number].marketsWithNonZeroBalanceSet.length();
    }

    function getAccountNumberOfMarketsWithDebt(
        Storage.State storage state,
        Account.Info memory account
    )
    internal
    view
    returns (uint256)
    {
        return state.accounts[account.owner][account.number].numberOfMarketsWithDebt;
    }

    function getLiquidationSpreadForPair(
        Storage.State storage state,
        uint256 heldMarketId,
        uint256 owedMarketId
    )
        internal
        view
        returns (Decimal.D256 memory)
    {
        uint256 result = state.riskParams.liquidationSpread.value;
        result = Decimal.mul(result, Decimal.onePlus(state.markets[heldMarketId].spreadPremium));
        result = Decimal.mul(result, Decimal.onePlus(state.markets[owedMarketId].spreadPremium));
        return Decimal.D256({
            value: result
        });
    }

    function fetchNewIndex(
        Storage.State storage state,
        uint256 marketId,
        Interest.Index memory index
    )
        internal
        view
        returns (Interest.Index memory)
    {
        Interest.Rate memory rate = state.fetchInterestRate(marketId, index);

        return Interest.calculateNewIndex(
            index,
            rate,
            state.getTotalPar(marketId),
            state.riskParams.earningsRate
        );
    }

    function fetchInterestRate(
        Storage.State storage state,
        uint256 marketId,
        Interest.Index memory index
    )
        internal
        view
        returns (Interest.Rate memory)
    {
        Types.TotalPar memory totalPar = state.getTotalPar(marketId);
        (
            Types.Wei memory supplyWei,
            Types.Wei memory borrowWei
        ) = Interest.totalParToWei(totalPar, index);

        Interest.Rate memory rate = state.markets[marketId].interestSetter.getInterestRate(
            state.getToken(marketId),
            borrowWei.value,
            supplyWei.value
        );

        return rate;
    }

    function fetchPrice(
        Storage.State storage state,
        uint256 marketId,
        address token
    )
        internal
        view
        returns (Monetary.Price memory)
    {
        IPriceOracle oracle = IPriceOracle(state.markets[marketId].priceOracle);
        Monetary.Price memory price = oracle.getPrice(token);
        Require.that(
            price.value != 0,
            FILE,
            "Price cannot be zero",
            marketId
        );
        return price;
    }

    function getAccountValues(
        Storage.State storage state,
        Account.Info memory account,
        Cache.MarketCache memory cache,
        bool adjustForLiquidity
    )
        internal
        view
        returns (Monetary.Value memory, Monetary.Value memory)
    {
        Monetary.Value memory supplyValue;
        Monetary.Value memory borrowValue;

        uint256 numMarkets = cache.getNumMarkets();
        for (uint256 i = 0; i < numMarkets; i++) {
            Types.Wei memory userWei = state.getWei(account, cache.getAtIndex(i).marketId, cache.getAtIndex(i).index);

            if (userWei.isZero()) {
                continue;
            }

            uint256 assetValue = userWei.value.mul(cache.getAtIndex(i).price.value);
            Decimal.D256 memory adjust = Decimal.one();
            if (adjustForLiquidity) {
                adjust = Decimal.onePlus(state.markets[cache.getAtIndex(i).marketId].marginPremium);
            }

            if (userWei.sign) {
                supplyValue.value = supplyValue.value.add(Decimal.div(assetValue, adjust));
            } else {
                borrowValue.value = borrowValue.value.add(Decimal.mul(assetValue, adjust));
            }
        }

        return (supplyValue, borrowValue);
    }

    function isCollateralized(
        Storage.State storage state,
        Account.Info memory account,
        Cache.MarketCache memory cache,
        bool requireMinBorrow
    )
        internal
        view
        returns (bool)
    {
        if (state.getAccountNumberOfMarketsWithDebt(account) == 0) {
            // The user does not have a balance with a borrow amount, so they must be collateralized
            return true;
        }

        // get account values (adjusted for liquidity)
        (
            Monetary.Value memory supplyValue,
            Monetary.Value memory borrowValue
        ) = state.getAccountValues(account, cache, /* adjustForLiquidity = */ true);

        if (requireMinBorrow) {
            Require.that(
                borrowValue.value >= state.riskParams.minBorrowedValue.value,
                FILE,
                "Borrow value too low",
                account.owner,
                account.number
            );
        }

        uint256 requiredMargin = Decimal.mul(borrowValue.value, state.riskParams.marginRatio);

        return supplyValue.value >= borrowValue.value.add(requiredMargin);
    }

    function isGlobalOperator(
        Storage.State storage state,
        address operator
    )
        internal
        view
        returns (bool)
    {
        return state.globalOperators[operator];
    }

    function isAutoTraderSpecial(
        Storage.State storage state,
        address autoTrader
    )
        internal
        view
        returns (bool)
    {
        return state.specialAutoTraders[autoTrader];
    }

    function isLocalOperator(
        Storage.State storage state,
        address owner,
        address operator
    )
        internal
        view
        returns (bool)
    {
        return state.operators[owner][operator];
    }

    function requireIsGlobalOperator(
        Storage.State storage state,
        address operator
    )
        internal
        view
    {
        bool isValidOperator = state.isGlobalOperator(operator);

        Require.that(
            isValidOperator,
            FILE,
            "Unpermissioned global operator",
            operator
        );
    }

    function requireIsOperator(
        Storage.State storage state,
        Account.Info memory account,
        address operator
    )
        internal
        view
    {
        bool isValidOperator =
            operator == account.owner
            || state.isGlobalOperator(operator)
            || state.isLocalOperator(account.owner, operator);

        Require.that(
            isValidOperator,
            FILE,
            "Unpermissioned operator",
            operator
        );
    }

    /**
     * Determine and set an account's balance based on the intended balance change. Return the
     * equivalent amount in wei
     */
    function getNewParAndDeltaWei(
        Storage.State storage state,
        Account.Info memory account,
        uint256 marketId,
        Interest.Index memory index,
        Types.AssetAmount memory amount
    )
        internal
        view
        returns (Types.Par memory, Types.Wei memory)
    {
        Types.Par memory oldPar = state.getPar(account, marketId);

        if (amount.value == 0 && amount.ref == Types.AssetReference.Delta) {
            return (oldPar, Types.zeroWei());
        }

        Types.Wei memory oldWei = Interest.parToWei(oldPar, index);
        Types.Par memory newPar;
        Types.Wei memory deltaWei;

        if (amount.denomination == Types.AssetDenomination.Wei) {
            deltaWei = Types.Wei({
                sign: amount.sign,
                value: amount.value
            });
            if (amount.ref == Types.AssetReference.Target) {
                deltaWei = deltaWei.sub(oldWei);
            }
            newPar = Interest.weiToPar(oldWei.add(deltaWei), index);
        } else { // AssetDenomination.Par
            newPar = Types.Par({
                sign: amount.sign,
                value: amount.value.to128()
            });
            if (amount.ref == Types.AssetReference.Delta) {
                newPar = oldPar.add(newPar);
            }
            deltaWei = Interest.parToWei(newPar, index).sub(oldWei);
        }

        return (newPar, deltaWei);
    }

    function getNewParAndDeltaWeiForLiquidation(
        Storage.State storage state,
        Account.Info memory account,
        uint256 marketId,
        Interest.Index memory index,
        Types.AssetAmount memory amount
    )
        internal
        view
        returns (Types.Par memory, Types.Wei memory)
    {
        Types.Par memory oldPar = state.getPar(account, marketId);

        Require.that(
            !oldPar.isPositive(),
            FILE,
            "Owed balance cannot be positive",
            account.owner,
            account.number
        );

        (
            Types.Par memory newPar,
            Types.Wei memory deltaWei
        ) = state.getNewParAndDeltaWei(
            account,
            marketId,
            index,
            amount
        );

        // if attempting to over-repay the owed asset, bound it by the maximum
        if (newPar.isPositive()) {
            newPar = Types.zeroPar();
            deltaWei = state.getWei(account, marketId, index).negative();
        }

        Require.that(
            !deltaWei.isNegative() && oldPar.value >= newPar.value,
            FILE,
            "Owed balance cannot increase",
            account.owner,
            account.number
        );

        // if not paying back enough wei to repay any par, then bound wei to zero
        if (oldPar.equals(newPar)) {
            deltaWei = Types.zeroWei();
        }

        return (newPar, deltaWei);
    }

    function isVaporizable(
        Storage.State storage state,
        Account.Info memory account,
        Cache.MarketCache memory cache
    )
        internal
        view
        returns (bool)
    {
        bool hasNegative = false;
        uint256 numMarkets = cache.getNumMarkets();
        for (uint256 i = 0; i < numMarkets; i++) {
            Types.Par memory par = state.getPar(account, cache.getAtIndex(i).marketId);
            if (par.isZero()) {
                continue;
            } else if (par.sign) {
                return false;
            } else {
                hasNegative = true;
            }
        }
        return hasNegative;
    }

    // =============== Setter Functions ===============

    function updateIndex(
        Storage.State storage state,
        uint256 marketId
    )
        internal
        returns (Interest.Index memory)
    {
        Interest.Index memory index = state.getIndex(marketId);
        if (index.lastUpdate == Time.currentTime()) {
            return index;
        }
        return state.markets[marketId].index = state.fetchNewIndex(marketId, index);
    }

    function setStatus(
        Storage.State storage state,
        Account.Info memory account,
        Account.Status status
    )
        internal
    {
        state.accounts[account.owner][account.number].status = status;
    }

    function setPar(
        Storage.State storage state,
        Account.Info memory account,
        uint256 marketId,
        Types.Par memory newPar
    )
        internal
    {
        Types.Par memory oldPar = state.getPar(account, marketId);

        if (Types.equals(oldPar, newPar)) {
            // GUARD statement
            return;
        }

        // updateTotalPar
        Types.TotalPar memory totalPar = state.getTotalPar(marketId);

        // roll-back oldPar
        if (oldPar.sign) {
            totalPar.supply = uint256(totalPar.supply).sub(oldPar.value).to128();
        } else {
            totalPar.borrow = uint256(totalPar.borrow).sub(oldPar.value).to128();
        }

        // roll-forward newPar
        if (newPar.sign) {
            totalPar.supply = uint256(totalPar.supply).add(newPar.value).to128();
        } else {
            totalPar.borrow = uint256(totalPar.borrow).add(newPar.value).to128();
        }

        if (oldPar.isLessThanZero() && newPar.isGreaterThanOrEqualToZero()) {
            // user went from borrowing to repaying or positive
            state.accounts[account.owner][account.number].numberOfMarketsWithDebt -= 1;
        } else if (oldPar.isGreaterThanOrEqualToZero() && newPar.isLessThanZero()) {
            // user went from zero or positive to borrowing
            state.accounts[account.owner][account.number].numberOfMarketsWithDebt += 1;
        }

        if (newPar.isZero() && (!oldPar.isZero())) {
            // User went from a non-zero balance to zero. Remove the market from the set.
            state.accounts[account.owner][account.number].marketsWithNonZeroBalanceSet.remove(marketId);
        } else if ((!newPar.isZero()) && oldPar.isZero()) {
            // User went from zero to non-zero. Add the market to the set.
            state.accounts[account.owner][account.number].marketsWithNonZeroBalanceSet.add(marketId);
        }

        state.markets[marketId].totalPar = totalPar;
        state.accounts[account.owner][account.number].balances[marketId] = newPar;
    }

    /**
     * Determine and set an account's balance based on a change in wei
     */
    function setParFromDeltaWei(
        Storage.State storage state,
        Account.Info memory account,
        uint256 marketId,
        Interest.Index memory index,
        Types.Wei memory deltaWei
    )
        internal
    {
        if (deltaWei.isZero()) {
            return;
        }
        Types.Wei memory oldWei = state.getWei(account, marketId, index);
        Types.Wei memory newWei = oldWei.add(deltaWei);
        Types.Par memory newPar = Interest.weiToPar(newWei, index);
        state.setPar(
            account,
            marketId,
            newPar
        );
    }

    /**
     * Initializes the cache using the set bits
     */
    function initializeCache(
        Storage.State storage state,
        Cache.MarketCache memory cache
    ) internal view {
        cache.markets = new Cache.MarketInfo[](cache.marketsLength);
        uint counter = 0;

        // Really neat byproduct of iterating through a bitmap using the least significant bit, where each set flag
        // represents the marketId, --> the initialized `cache.markets` array is sorted in O(n)!
        // Meaning, this function call is O(n) where `n` is the number of markets in the cache
        for (uint i = 0; i < cache.marketBitmaps.length; i++) {
            uint bitmap = cache.marketBitmaps[i];
            while (bitmap != 0) {
                uint nextSetBit = Bits.getLeastSignificantBit(bitmap);
                uint marketId = Bits.getMarketIdFromBit(i, nextSetBit);
                address token = state.getToken(marketId);
                Types.TotalPar memory totalPar = state.getTotalPar(marketId);
                cache.markets[counter++] = Cache.MarketInfo({
                    marketId: marketId,
                    token: token,
                    isClosing: state.markets[marketId].isClosing,
                    borrowPar: totalPar.borrow,
                    supplyPar: totalPar.supply,
                    index: state.getIndex(marketId),
                    price: state.fetchPrice(marketId, token)
                });

                // unset the set bit
                bitmap = Bits.unsetBit(bitmap, nextSetBit);
            }
            if (counter == cache.marketsLength) {
                break;
            }
        }

        assert(cache.marketsLength == counter);
    }
}

File 11 of 43 : Require.sol
/*

    Copyright 2019 dYdX Trading Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;


/**
 * @title Require
 * @author dYdX
 *
 * Stringifies parameters to pretty-print revert messages. Costs more gas than regular require()
 */
library Require {

    // ============ Constants ============

    uint256 constant ASCII_ZERO = 48; // '0'
    uint256 constant ASCII_RELATIVE_ZERO = 87; // 'a' - 10
    uint256 constant ASCII_LOWER_EX = 120; // 'x'
    bytes2 constant COLON = 0x3a20; // ': '
    bytes2 constant COMMA = 0x2c20; // ', '
    bytes2 constant LPAREN = 0x203c; // ' <'
    byte constant RPAREN = 0x3e; // '>'
    uint256 constant FOUR_BIT_MASK = 0xf;

    // ============ Library Functions ============

    function that(
        bool must,
        bytes32 file,
        bytes32 reason
    )
        internal
        pure
    {
        if (!must) {
            revert(
                string(
                    abi.encodePacked(
                        stringifyTruncated(file),
                        COLON,
                        stringifyTruncated(reason)
                    )
                )
            );
        }
    }

    function that(
        bool must,
        bytes32 file,
        bytes32 reason,
        uint256 payloadA
    )
        internal
        pure
    {
        if (!must) {
            revert(
                string(
                    abi.encodePacked(
                        stringifyTruncated(file),
                        COLON,
                        stringifyTruncated(reason),
                        LPAREN,
                        stringify(payloadA),
                        RPAREN
                    )
                )
            );
        }
    }

    function that(
        bool must,
        bytes32 file,
        bytes32 reason,
        uint256 payloadA,
        uint256 payloadB
    )
        internal
        pure
    {
        if (!must) {
            revert(
                string(
                    abi.encodePacked(
                        stringifyTruncated(file),
                        COLON,
                        stringifyTruncated(reason),
                        LPAREN,
                        stringify(payloadA),
                        COMMA,
                        stringify(payloadB),
                        RPAREN
                    )
                )
            );
        }
    }

    function that(
        bool must,
        bytes32 file,
        bytes32 reason,
        address payloadA
    )
        internal
        pure
    {
        if (!must) {
            revert(
                string(
                    abi.encodePacked(
                        stringifyTruncated(file),
                        COLON,
                        stringifyTruncated(reason),
                        LPAREN,
                        stringify(payloadA),
                        RPAREN
                    )
                )
            );
        }
    }

    function that(
        bool must,
        bytes32 file,
        bytes32 reason,
        address payloadA,
        uint256 payloadB
    )
        internal
        pure
    {
        if (!must) {
            revert(
                string(
                    abi.encodePacked(
                        stringifyTruncated(file),
                        COLON,
                        stringifyTruncated(reason),
                        LPAREN,
                        stringify(payloadA),
                        COMMA,
                        stringify(payloadB),
                        RPAREN
                    )
                )
            );
        }
    }

    function that(
        bool must,
        bytes32 file,
        bytes32 reason,
        address payloadA,
        uint256 payloadB,
        uint256 payloadC
    )
        internal
        pure
    {
        if (!must) {
            revert(
                string(
                    abi.encodePacked(
                        stringifyTruncated(file),
                        COLON,
                        stringifyTruncated(reason),
                        LPAREN,
                        stringify(payloadA),
                        COMMA,
                        stringify(payloadB),
                        COMMA,
                        stringify(payloadC),
                        RPAREN
                    )
                )
            );
        }
    }

    function that(
        bool must,
        bytes32 file,
        bytes32 reason,
        bytes32 payloadA
    )
        internal
        pure
    {
        if (!must) {
            revert(
                string(
                    abi.encodePacked(
                        stringifyTruncated(file),
                        COLON,
                        stringifyTruncated(reason),
                        LPAREN,
                        stringify(payloadA),
                        RPAREN
                    )
                )
            );
        }
    }

    function that(
        bool must,
        bytes32 file,
        bytes32 reason,
        bytes32 payloadA,
        uint256 payloadB,
        uint256 payloadC
    )
        internal
        pure
    {
        if (!must) {
            revert(
                string(
                    abi.encodePacked(
                        stringifyTruncated(file),
                        COLON,
                        stringifyTruncated(reason),
                        LPAREN,
                        stringify(payloadA),
                        COMMA,
                        stringify(payloadB),
                        COMMA,
                        stringify(payloadC),
                        RPAREN
                    )
                )
            );
        }
    }

    // ============ Private Functions ============

    function stringifyTruncated(
        bytes32 input
    )
        internal
        pure
        returns (bytes memory)
    {
        // put the input bytes into the result
        bytes memory result = abi.encodePacked(input);

        // determine the length of the input by finding the location of the last non-zero byte
        for (uint256 i = 32; i > 0; ) {
            // reverse-for-loops with unsigned integer
            /* solium-disable-next-line security/no-modify-for-iter-var */
            i--;

            // find the last non-zero byte in order to determine the length
            if (result[i] != 0) {
                uint256 length = i + 1;

                /* solium-disable-next-line security/no-inline-assembly */
                assembly {
                    mstore(result, length) // r.length = length;
                }

                return result;
            }
        }

        // all bytes are zero
        return new bytes(0);
    }

    function stringify(
        uint256 input
    )
        private
        pure
        returns (bytes memory)
    {
        if (input == 0) {
            return "0";
        }

        // get the final string length
        uint256 j = input;
        uint256 length;
        while (j != 0) {
            length++;
            j /= 10;
        }

        // allocate the string
        bytes memory bstr = new bytes(length);

        // populate the string starting with the least-significant character
        j = input;
        for (uint256 i = length; i > 0; ) {
            // reverse-for-loops with unsigned integer
            /* solium-disable-next-line security/no-modify-for-iter-var */
            i--;

            // take last decimal digit
            bstr[i] = byte(uint8(ASCII_ZERO + (j % 10)));

            // remove the last decimal digit
            j /= 10;
        }

        return bstr;
    }

    function stringify(
        address input
    )
        private
        pure
        returns (bytes memory)
    {
        uint256 z = uint256(input);

        // addresses are "0x" followed by 20 bytes of data which take up 2 characters each
        bytes memory result = new bytes(42);

        // populate the result with "0x"
        result[0] = byte(uint8(ASCII_ZERO));
        result[1] = byte(uint8(ASCII_LOWER_EX));

        // for each byte (starting from the lowest byte), populate the result with two characters
        for (uint256 i = 0; i < 20; i++) {
            // each byte takes two characters
            uint256 shift = i * 2;

            // populate the least-significant character
            result[41 - shift] = char(z & FOUR_BIT_MASK);
            z = z >> 4;

            // populate the most-significant character
            result[40 - shift] = char(z & FOUR_BIT_MASK);
            z = z >> 4;
        }

        return result;
    }

    function stringify(
        bytes32 input
    )
        private
        pure
        returns (bytes memory)
    {
        uint256 z = uint256(input);

        // bytes32 are "0x" followed by 32 bytes of data which take up 2 characters each
        bytes memory result = new bytes(66);

        // populate the result with "0x"
        result[0] = byte(uint8(ASCII_ZERO));
        result[1] = byte(uint8(ASCII_LOWER_EX));

        // for each byte (starting from the lowest byte), populate the result with two characters
        for (uint256 i = 0; i < 32; i++) {
            // each byte takes two characters
            uint256 shift = i * 2;

            // populate the least-significant character
            result[65 - shift] = char(z & FOUR_BIT_MASK);
            z = z >> 4;

            // populate the most-significant character
            result[64 - shift] = char(z & FOUR_BIT_MASK);
            z = z >> 4;
        }

        return result;
    }

    function char(
        uint256 input
    )
        private
        pure
        returns (byte)
    {
        // return ASCII digit (0-9)
        if (input < 10) {
            return byte(uint8(input + ASCII_ZERO));
        }

        // return ASCII letter (a-f)
        return byte(uint8(input + ASCII_RELATIVE_ZERO));
    }
}

File 12 of 43 : Monetary.sol
/*

    Copyright 2019 dYdX Trading Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;


/**
 * @title Monetary
 * @author dYdX
 *
 * Library for types involving money
 */
library Monetary {

    /*
     * The price of a base-unit of an asset. Has `36 - token.decimals` decimals
     */
    struct Price {
        uint256 value;
    }

    /*
     * Total value of an some amount of an asset. Equal to (price * amount). Has 36 decimals.
     */
    struct Value {
        uint256 value;
    }
}

File 13 of 43 : Interest.sol
/*

    Copyright 2019 dYdX Trading Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol";
import { Decimal } from "./Decimal.sol";
import { DolomiteMarginMath } from "./DolomiteMarginMath.sol";
import { Time } from "./Time.sol";
import { Types } from "./Types.sol";


/**
 * @title Interest
 * @author dYdX
 *
 * Library for managing the interest rate and interest indexes of DolomiteMargin
 */
library Interest {
    using DolomiteMarginMath for uint256;
    using SafeMath for uint256;

    // ============ Constants ============

    bytes32 private constant FILE = "Interest";
    uint64 constant BASE = 10**18;

    // ============ Structs ============

    struct Rate {
        uint256 value;
    }

    struct Index {
        uint96 borrow;
        uint96 supply;
        uint32 lastUpdate;
    }

    // ============ Library Functions ============

    /**
     * Get a new market Index based on the old index and market interest rate.
     * Calculate interest for borrowers by using the formula rate * time. Approximates
     * continuously-compounded interest when called frequently, but is much more
     * gas-efficient to calculate. For suppliers, the interest rate is adjusted by the earningsRate,
     * then prorated across all suppliers.
     *
     * @param  index         The old index for a market
     * @param  rate          The current interest rate of the market
     * @param  totalPar      The total supply and borrow par values of the market
     * @param  earningsRate  The portion of the interest that is forwarded to the suppliers
     * @return               The updated index for a market
     */
    function calculateNewIndex(
        Index memory index,
        Rate memory rate,
        Types.TotalPar memory totalPar,
        Decimal.D256 memory earningsRate
    )
        internal
        view
        returns (Index memory)
    {
        (
            Types.Wei memory supplyWei,
            Types.Wei memory borrowWei
        ) = totalParToWei(totalPar, index);

        // get interest increase for borrowers
        uint32 currentTime = Time.currentTime();
        uint256 borrowInterest = rate.value.mul(uint256(currentTime).sub(index.lastUpdate));

        // get interest increase for suppliers
        uint256 supplyInterest;
        if (Types.isZero(supplyWei)) {
            supplyInterest = 0;
        } else {
            supplyInterest = Decimal.mul(borrowInterest, earningsRate);
            if (borrowWei.value < supplyWei.value) {
                // scale down the interest by the amount being supplied. Why? Because interest is only being paid on
                // the borrowWei, which means it's split amongst all of the supplyWei. Scaling it down normalizes it
                // for the suppliers to share what's being paid by borrowers
                supplyInterest = DolomiteMarginMath.getPartial(supplyInterest, borrowWei.value, supplyWei.value);
            }
        }
        assert(supplyInterest <= borrowInterest);

        return Index({
            borrow: DolomiteMarginMath.getPartial(index.borrow, borrowInterest, BASE).add(index.borrow).to96(),
            supply: DolomiteMarginMath.getPartial(index.supply, supplyInterest, BASE).add(index.supply).to96(),
            lastUpdate: currentTime
        });
    }

    function newIndex()
        internal
        view
        returns (Index memory)
    {
        return Index({
            borrow: BASE,
            supply: BASE,
            lastUpdate: Time.currentTime()
        });
    }

    /*
     * Convert a principal amount to a token amount given an index.
     */
    function parToWei(
        Types.Par memory input,
        Index memory index
    )
        internal
        pure
        returns (Types.Wei memory)
    {
        uint256 inputValue = uint256(input.value);
        if (input.sign) {
            return Types.Wei({
                sign: true,
                value: inputValue.getPartialRoundHalfUp(index.supply, BASE)
            });
        } else {
            return Types.Wei({
                sign: false,
                value: inputValue.getPartialRoundHalfUp(index.borrow, BASE)
            });
        }
    }

    /*
     * Convert a token amount to a principal amount given an index.
     */
    function weiToPar(
        Types.Wei memory input,
        Index memory index
    )
        internal
        pure
        returns (Types.Par memory)
    {
        if (input.sign) {
            return Types.Par({
                sign: true,
                value: input.value.getPartialRoundHalfUp(BASE, index.supply).to128()
            });
        } else {
            return Types.Par({
                sign: false,
                value: input.value.getPartialRoundHalfUp(BASE, index.borrow).to128()
            });
        }
    }

    /*
     * Convert the total supply and borrow principal amounts of a market to total supply and borrow
     * token amounts.
     */
    function totalParToWei(
        Types.TotalPar memory totalPar,
        Index memory index
    )
        internal
        pure
        returns (Types.Wei memory, Types.Wei memory)
    {
        Types.Par memory supplyPar = Types.Par({
            sign: true,
            value: totalPar.supply
        });
        Types.Par memory borrowPar = Types.Par({
            sign: false,
            value: totalPar.borrow
        });
        Types.Wei memory supplyWei = parToWei(supplyPar, index);
        Types.Wei memory borrowWei = parToWei(borrowPar, index);
        return (supplyWei, borrowWei);
    }
}

File 14 of 43 : ExcessivelySafeCall.sol
/*

    Copyright 2022 Dolomite.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;


/**
 * @author https://github.com/nomad-xyz
 */
library ExcessivelySafeCall {

    function safeStaticCall(
        address _target,
        bytes4 _selector,
        bytes memory _calldata
    ) internal view returns (bool isSuccess, bytes memory returnData) {
        (isSuccess, returnData) = _target.staticcall(abi.encodeWithSelector(_selector, _calldata));
    }

    /// @notice Use when you _really_ really _really_ don't trust the called
    /// contract. This prevents the called contract from causing reversion of
    /// the caller in as many ways as we can.
    /// @dev The main difference between this and a solidity low-level call is
    /// that we limit the number of bytes that the callee can cause to be
    /// copied to caller memory. This prevents stupid things like malicious
    /// contracts returning 10,000,000 bytes causing a local OOG when copying
    /// to memory.
    /// @param _target The address to call
    /// @param _gas The amount of gas to forward to the remote contract
    /// @param _maxCopy The maximum number of bytes of returndata to copy
    /// to memory.
    /// @param _calldata The data to send to the remote contract
    /// @return success and returndata, as `.call()`. Returndata is capped to
    /// `_maxCopy` bytes.
    function excessivelySafeCall(
        address _target,
        uint256 _gas,
        uint16 _maxCopy,
        bytes memory _calldata
    ) internal returns (bool, bytes memory) {
        // set up for assembly call
        uint256 _toCopy;
        bool _success;
        bytes memory _returndata = new bytes(_maxCopy);
        // dispatch message to recipient
        // by assembly calling "handle" function
        // we call via assembly to avoid mem-copying a very large returnData
        // returned by a malicious contract
        /* solium-disable security/no-inline-assembly */
        assembly {
            _success := call(
                _gas, // gas
                _target, // recipient
                0, // ether value
                add(_calldata, 0x20), // inloc
                mload(_calldata), // inlen
                0, // outloc
                0 // outlen
            )
            // limit our copy to 256 bytes
            _toCopy := returndatasize()
            if gt(_toCopy, _maxCopy) {
                _toCopy := _maxCopy
            }
            // Store the length of the copied bytes
            mstore(_returndata, _toCopy)
            // copy the bytes from returndata[0:_toCopy]
            returndatacopy(add(_returndata, 0x20), 0, _toCopy)
        }
        /* solium-enable security/no-inline-assembly */
        return (_success, _returndata);
    }

    /// @notice Use when you _really_ really _really_ don't trust the called
    /// contract. This prevents the called contract from causing reversion of
    /// the caller in as many ways as we can.
    /// @dev The main difference between this and a solidity low-level call is
    /// that we limit the number of bytes that the callee can cause to be
    /// copied to caller memory. This prevents stupid things like malicious
    /// contracts returning 10,000,000 bytes causing a local OOG when copying
    /// to memory.
    /// @param _target The address to call
    /// @param _gas The amount of gas to forward to the remote contract
    /// @param _maxCopy The maximum number of bytes of returndata to copy
    /// to memory.
    /// @param _calldata The data to send to the remote contract
    /// @return success and returndata, as `.call()`. Returndata is capped to
    /// `_maxCopy` bytes.
    function excessivelySafeStaticCall(
        address _target,
        uint256 _gas,
        uint16 _maxCopy,
        bytes memory _calldata
    ) internal view returns (bool, bytes memory) {
        // set up for assembly call
        uint256 _toCopy;
        bool _success;
        bytes memory _returndata = new bytes(_maxCopy);
        // dispatch message to recipient
        // by assembly calling "handle" function
        // we call via assembly to avoid mem-copying a very large returndata
        // returned by a malicious contract
        /* solium-disable security/no-inline-assembly */
        assembly {
            _success := staticcall(
                _gas, // gas
                _target, // recipient
                add(_calldata, 0x20), // inloc
                mload(_calldata), // inlen
                0, // outloc
                0 // outlen
            )
            // limit our copy to 256 bytes
            _toCopy := returndatasize()
            if gt(_toCopy, _maxCopy) {
                _toCopy := _maxCopy
            }
            // Store the length of the copied bytes
            mstore(_returndata, _toCopy)
            // copy the bytes from returndata[0:_toCopy]
            returndatacopy(add(_returndata, 0x20), 0, _toCopy)
        }
        /* solium-enable security/no-inline-assembly */
        return (_success, _returndata);
    }
}

File 15 of 43 : Events.sol
/*

    Copyright 2019 dYdX Trading Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { Account } from "./Account.sol";
import { Actions } from "./Actions.sol";
import { Cache } from "./Cache.sol";
import { Interest } from "./Interest.sol";
import { Monetary } from "./Monetary.sol";
import { Storage } from "./Storage.sol";
import { Types } from "./Types.sol";


/**
 * @title Events
 * @author dYdX
 *
 * Library to parse and emit logs from which the state of all accounts and indexes can be followed
 */
library Events {
    using Types for Types.Wei;
    using Storage for Storage.State;

    // ============ Events ============

    event LogIndexUpdate(
        uint256 indexed market,
        Interest.Index index
    );

    event LogOraclePrice(
        uint256 indexed market,
        Monetary.Price price
    );

    event LogOperation(
        address sender
    );

    event LogDeposit(
        address indexed accountOwner,
        uint256 accountNumber,
        uint256 market,
        BalanceUpdate update,
        address from
    );

    event LogWithdraw(
        address indexed accountOwner,
        uint256 accountNumber,
        uint256 market,
        BalanceUpdate update,
        address to
    );

    event LogTransfer(
        address indexed accountOneOwner,
        uint256 accountOneNumber,
        address indexed accountTwoOwner,
        uint256 accountTwoNumber,
        uint256 market,
        BalanceUpdate updateOne,
        BalanceUpdate updateTwo
    );

    event LogBuy(
        address indexed accountOwner,
        uint256 accountNumber,
        uint256 takerMarket,
        uint256 makerMarket,
        BalanceUpdate takerUpdate,
        BalanceUpdate makerUpdate,
        address exchangeWrapper
    );

    event LogSell(
        address indexed accountOwner,
        uint256 accountNumber,
        uint256 takerMarket,
        uint256 makerMarket,
        BalanceUpdate takerUpdate,
        BalanceUpdate makerUpdate,
        address exchangeWrapper
    );

    event LogTrade(
        address indexed takerAccountOwner,
        uint256 takerAccountNumber,
        address indexed makerAccountOwner,
        uint256 makerAccountNumber,
        uint256 inputMarket,
        uint256 outputMarket,
        BalanceUpdate takerInputUpdate,
        BalanceUpdate takerOutputUpdate,
        BalanceUpdate makerInputUpdate,
        BalanceUpdate makerOutputUpdate,
        address autoTrader
    );

    event LogCall(
        address indexed accountOwner,
        uint256 accountNumber,
        address callee
    );

    event LogLiquidate(
        address indexed solidAccountOwner,
        uint256 solidAccountNumber,
        address indexed liquidAccountOwner,
        uint256 liquidAccountNumber,
        uint256 heldMarket,
        uint256 owedMarket,
        BalanceUpdate solidHeldUpdate,
        BalanceUpdate solidOwedUpdate,
        BalanceUpdate liquidHeldUpdate,
        BalanceUpdate liquidOwedUpdate
    );

    event LogVaporize(
        address indexed solidAccountOwner,
        uint256 solidAccountNumber,
        address indexed vaporAccountOwner,
        uint256 vaporAccountNumber,
        uint256 heldMarket,
        uint256 owedMarket,
        BalanceUpdate solidHeldUpdate,
        BalanceUpdate solidOwedUpdate,
        BalanceUpdate vaporOwedUpdate
    );

    // ============ Structs ============

    struct BalanceUpdate {
        Types.Wei deltaWei;
        Types.Par newPar;
    }

    // ============ Internal Functions ============

    function logIndexUpdate(
        uint256 marketId,
        Interest.Index memory index
    )
        internal
    {
        emit LogIndexUpdate(
            marketId,
            index
        );
    }

    function logOraclePrice(
        Cache.MarketInfo memory marketInfo
    )
        internal
    {
        emit LogOraclePrice(
            marketInfo.marketId,
            marketInfo.price
        );
    }

    function logOperation()
        internal
    {
        emit LogOperation(msg.sender);
    }

    function logDeposit(
        Storage.State storage state,
        Actions.DepositArgs memory args,
        Types.Wei memory deltaWei
    )
        internal
    {
        emit LogDeposit(
            args.account.owner,
            args.account.number,
            args.market,
            getBalanceUpdate(
                state,
                args.account,
                args.market,
                deltaWei
            ),
            args.from
        );
    }

    function logWithdraw(
        Storage.State storage state,
        Actions.WithdrawArgs memory args,
        Types.Wei memory deltaWei
    )
        internal
    {
        emit LogWithdraw(
            args.account.owner,
            args.account.number,
            args.market,
            getBalanceUpdate(
                state,
                args.account,
                args.market,
                deltaWei
            ),
            args.to
        );
    }

    function logTransfer(
        Storage.State storage state,
        Actions.TransferArgs memory args,
        Types.Wei memory deltaWei
    )
        internal
    {
        emit LogTransfer(
            args.accountOne.owner,
            args.accountOne.number,
            args.accountTwo.owner,
            args.accountTwo.number,
            args.market,
            getBalanceUpdate(
                state,
                args.accountOne,
                args.market,
                deltaWei
            ),
            getBalanceUpdate(
                state,
                args.accountTwo,
                args.market,
                deltaWei.negative()
            )
        );
    }

    function logBuy(
        Storage.State storage state,
        Actions.BuyArgs memory args,
        Types.Wei memory takerWei,
        Types.Wei memory makerWei
    )
        internal
    {
        emit LogBuy(
            args.account.owner,
            args.account.number,
            args.takerMarket,
            args.makerMarket,
            getBalanceUpdate(
                state,
                args.account,
                args.takerMarket,
                takerWei
            ),
            getBalanceUpdate(
                state,
                args.account,
                args.makerMarket,
                makerWei
            ),
            args.exchangeWrapper
        );
    }

    function logSell(
        Storage.State storage state,
        Actions.SellArgs memory args,
        Types.Wei memory takerWei,
        Types.Wei memory makerWei
    )
        internal
    {
        emit LogSell(
            args.account.owner,
            args.account.number,
            args.takerMarket,
            args.makerMarket,
            getBalanceUpdate(
                state,
                args.account,
                args.takerMarket,
                takerWei
            ),
            getBalanceUpdate(
                state,
                args.account,
                args.makerMarket,
                makerWei
            ),
            args.exchangeWrapper
        );
    }

    function logTrade(
        Storage.State storage state,
        Actions.TradeArgs memory args,
        Types.Wei memory inputWei,
        Types.Wei memory outputWei
    )
        internal
    {
        BalanceUpdate[4] memory updates = [
            getBalanceUpdate(
                state,
                args.takerAccount,
                args.inputMarket,
                inputWei.negative()
            ),
            getBalanceUpdate(
                state,
                args.takerAccount,
                args.outputMarket,
                outputWei.negative()
            ),
            getBalanceUpdate(
                state,
                args.makerAccount,
                args.inputMarket,
                inputWei
            ),
            getBalanceUpdate(
                state,
                args.makerAccount,
                args.outputMarket,
                outputWei
            )
        ];

        emit LogTrade(
            args.takerAccount.owner,
            args.takerAccount.number,
            args.makerAccount.owner,
            args.makerAccount.number,
            args.inputMarket,
            args.outputMarket,
            updates[0],
            updates[1],
            updates[2],
            updates[3],
            args.autoTrader
        );
    }

    function logCall(
        Actions.CallArgs memory args
    )
        internal
    {
        emit LogCall(
            args.account.owner,
            args.account.number,
            args.callee
        );
    }

    function logLiquidate(
        Storage.State storage state,
        Actions.LiquidateArgs memory args,
        Types.Wei memory heldWei,
        Types.Wei memory owedWei
    )
        internal
    {
        BalanceUpdate memory solidHeldUpdate = getBalanceUpdate(
            state,
            args.solidAccount,
            args.heldMarket,
            heldWei.negative()
        );
        BalanceUpdate memory solidOwedUpdate = getBalanceUpdate(
            state,
            args.solidAccount,
            args.owedMarket,
            owedWei.negative()
        );
        BalanceUpdate memory liquidHeldUpdate = getBalanceUpdate(
            state,
            args.liquidAccount,
            args.heldMarket,
            heldWei
        );
        BalanceUpdate memory liquidOwedUpdate = getBalanceUpdate(
            state,
            args.liquidAccount,
            args.owedMarket,
            owedWei
        );

        emit LogLiquidate(
            args.solidAccount.owner,
            args.solidAccount.number,
            args.liquidAccount.owner,
            args.liquidAccount.number,
            args.heldMarket,
            args.owedMarket,
            solidHeldUpdate,
            solidOwedUpdate,
            liquidHeldUpdate,
            liquidOwedUpdate
        );
    }

    function logVaporize(
        Storage.State storage state,
        Actions.VaporizeArgs memory args,
        Types.Wei memory heldWei,
        Types.Wei memory owedWei,
        Types.Wei memory excessWei
    )
        internal
    {
        BalanceUpdate memory solidHeldUpdate = getBalanceUpdate(
            state,
            args.solidAccount,
            args.heldMarket,
            heldWei.negative()
        );
        BalanceUpdate memory solidOwedUpdate = getBalanceUpdate(
            state,
            args.solidAccount,
            args.owedMarket,
            owedWei.negative()
        );
        BalanceUpdate memory vaporOwedUpdate = getBalanceUpdate(
            state,
            args.vaporAccount,
            args.owedMarket,
            owedWei.add(excessWei)
        );

        emit LogVaporize(
            args.solidAccount.owner,
            args.solidAccount.number,
            args.vaporAccount.owner,
            args.vaporAccount.number,
            args.heldMarket,
            args.owedMarket,
            solidHeldUpdate,
            solidOwedUpdate,
            vaporOwedUpdate
        );
    }

    // ============ Private Functions ============

    function getBalanceUpdate(
        Storage.State storage state,
        Account.Info memory account,
        uint256 market,
        Types.Wei memory deltaWei
    )
        private
        view
        returns (BalanceUpdate memory)
    {
        return BalanceUpdate({
            deltaWei: deltaWei,
            newPar: state.getPar(account, market)
        });
    }
}

File 16 of 43 : EnumerableSet.sol
/*

    Copyright 2021 Dolomite.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;


library EnumerableSet {

    struct Set {
        // Storage of set values
        uint256[] _values;
        // Value to the index in `_values` array, plus 1 because index 0 means a value is not in the set.
        mapping(uint256 => uint256) _valueToIndexMap;
    }

    /**
    * @dev Add a value to a set. O(1).
     *
     * @return true if the value was added to the set, that is if it was not already present.
     */
    function add(Set storage set, uint256 value) internal returns (bool) {
        if (!contains(set, value)) {
            set._values.push(value);
            // The value is stored at length-1, but we add 1 to all indexes
            // and use 0 as a sentinel value
            set._valueToIndexMap[value] = set._values.length;
            return true;
        } else {
            return false;
        }
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * @return true if the value was removed from the set, that is if it was present.
     */
    function remove(Set storage set, uint256 value) internal returns (bool) {
        // We read and store the value's index to prevent multiple reads from the same storage slot
        uint256 valueIndex = set._valueToIndexMap[value];

        if (valueIndex != 0) {
            // Equivalent to contains(set, value)
            // To delete an element from the _values array in O(1), we swap the element to delete with the last one in
            // the array, and then remove the last element (sometimes called as 'swap and pop').
            // This modifies the order of the array, as noted in {at}.

            uint256 toDeleteIndex = valueIndex - 1;
            uint256 lastIndex = set._values.length - 1;

            if (lastIndex != toDeleteIndex) {
                uint256 lastValue = set._values[lastIndex];

                // Move the last value to the index where the value to delete is
                set._values[toDeleteIndex] = lastValue;
                // Update the index for the moved value
                set._valueToIndexMap[lastValue] = valueIndex; // Replace lastValue's index to valueIndex
            }

            // Delete the slot where the moved value was stored, which is the last index
            set._values.pop();

            // Delete the index for the deleted slot
            delete set._valueToIndexMap[value];

            return true;
        } else {
            return false;
        }
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function contains(Set storage set, uint256 value) internal view returns (bool) {
        return set._valueToIndexMap[value] != 0;
    }

    /**
     * @dev Returns the number of values on the set. O(1).
     */
    function length(Set storage set) internal view returns (uint256) {
        return set._values.length;
    }

    /**
     * @dev Returns the value at the corresponding index. O(1).
     */
    function getAtIndex(Set storage set, uint256 index) internal view returns (uint256) {
        return set._values[index];
    }

    /**
     * @dev Return the entire set in an array
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function values(Set storage set) internal view returns (uint256[] memory) {
        return set._values;
    }

}

File 17 of 43 : DolomiteMarginMath.sol
/*

    Copyright 2019 dYdX Trading Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol";
import { Require } from "./Require.sol";


/**
 * @title Math
 * @author dYdX
 *
 * Library for non-standard Math functions
 */
library DolomiteMarginMath {
    using SafeMath for uint256;

    // ============ Constants ============

    bytes32 private constant FILE = "Math";

    // ============ Library Functions ============

    /*
     * Return target * (numerator / denominator).
     */
    function getPartial(
        uint256 target,
        uint256 numerator,
        uint256 denominator
    )
        internal
        pure
        returns (uint256)
    {
        return target.mul(numerator).div(denominator);
    }

    /*
     * Return target * (numerator / denominator), but rounded half-up. Meaning, a result of 101.1 rounds to 102
     * instead of 101.
     */
    function getPartialRoundUp(
        uint256 target,
        uint256 numerator,
        uint256 denominator
    )
        internal
        pure
        returns (uint256)
    {
        if (target == 0 || numerator == 0) {
            // SafeMath will check for zero denominator
            return SafeMath.div(0, denominator);
        }
        return target.mul(numerator).sub(1).div(denominator).add(1);
    }

    /*
     * Return target * (numerator / denominator), but rounded half-up. Meaning, a result of 101.5 rounds to 102
     * instead of 101.
     */
    function getPartialRoundHalfUp(
        uint256 target,
        uint256 numerator,
        uint256 denominator
    )
        internal
        pure
        returns (uint256)
    {
        if (target == 0 || numerator == 0) {
            // SafeMath will check for zero denominator
            return SafeMath.div(0, denominator);
        }
        uint result = target.mul(numerator);
        // round the denominator comparator up to ensure a fair comparison is done on the `result`'s modulo.
        // For example, 51 / 103 == 0; 51 % 103 == 51; ((103 - 1) / 2) + 1 == 52; 51 < 52, therefore no round up
        return result.div(denominator).add(result.mod(denominator) >= denominator.sub(1).div(2).add(1) ? 1 : 0);
    }

    function to128(
        uint256 number
    )
        internal
        pure
        returns (uint128)
    {
        uint128 result = uint128(number);
        Require.that(
            result == number,
            FILE,
            "Unsafe cast to uint128",
            number
        );
        return result;
    }

    function to96(
        uint256 number
    )
        internal
        pure
        returns (uint96)
    {
        uint96 result = uint96(number);
        Require.that(
            result == number,
            FILE,
            "Unsafe cast to uint96",
            number
        );
        return result;
    }

    function to32(
        uint256 number
    )
        internal
        pure
        returns (uint32)
    {
        uint32 result = uint32(number);
        Require.that(
            result == number,
            FILE,
            "Unsafe cast to uint32",
            number
        );
        return result;
    }
}

File 18 of 43 : Decimal.sol
/*

    Copyright 2019 dYdX Trading Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol";
import { DolomiteMarginMath } from "./DolomiteMarginMath.sol";


/**
 * @title Decimal
 * @author dYdX
 *
 * Library that defines a fixed-point number with 18 decimal places.
 */
library Decimal {
    using SafeMath for uint256;

    // ============ Constants ============

    uint256 constant BASE = 10**18;

    // ============ Structs ============

    struct D256 {
        uint256 value;
    }

    // ============ Functions ============

    function one()
        internal
        pure
        returns (D256 memory)
    {
        return D256({ value: BASE });
    }

    function onePlus(
        D256 memory d
    )
        internal
        pure
        returns (D256 memory)
    {
        return D256({ value: d.value.add(BASE) });
    }

    function mul(
        uint256 target,
        D256 memory d
    )
        internal
        pure
        returns (uint256)
    {
        return DolomiteMarginMath.getPartial(target, d.value, BASE);
    }

    function div(
        uint256 target,
        D256 memory d
    )
        internal
        pure
        returns (uint256)
    {
        return DolomiteMarginMath.getPartial(target, BASE, d.value);
    }
}

File 19 of 43 : Cache.sol
/*

    Copyright 2019 dYdX Trading Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { Bits } from "./Bits.sol";
import { Interest } from "./Interest.sol";
import { Monetary } from "./Monetary.sol";
import { Require } from "./Require.sol";


/**
 * @title Cache
 * @author dYdX
 *
 * Library for caching information about markets
 */
library Cache {

    // ============ Constants ============

    bytes32 private constant FILE = "Cache";

    // ============ Structs ============

    struct MarketInfo {
        uint marketId;
        address token;
        bool isClosing;
        uint128 borrowPar;
        uint128 supplyPar;
        Interest.Index index;
        Monetary.Price price;
    }

    struct MarketCache {
        MarketInfo[] markets;
        uint256[] marketBitmaps;
        uint256 marketsLength;
    }

    // ============ Setter Functions ============

    /**
     * Initialize an empty cache for some given number of total markets.
     */
    function create(
        uint256 numMarkets
    )
        internal
        pure
        returns (MarketCache memory)
    {
        return MarketCache({
            markets: new MarketInfo[](0),
            marketBitmaps: Bits.createBitmaps(numMarkets),
            marketsLength: 0
        });
    }

    // ============ Getter Functions ============

    function getNumMarkets(
        MarketCache memory cache
    )
        internal
        pure
        returns (uint256)
    {
        return cache.markets.length;
    }

    function hasMarket(
        MarketCache memory cache,
        uint256 marketId
    )
        internal
        pure
        returns (bool)
    {
        return Bits.hasBit(cache.marketBitmaps, marketId);
    }

    function get(
        MarketCache memory cache,
        uint256 marketId
    )
        internal
        pure
        returns (MarketInfo memory)
    {
        Require.that(
            cache.markets.length > 0,
            FILE,
            "not initialized"
        );
        return _getInternal(
            cache.markets,
            0,
            cache.marketsLength,
            marketId
        );
    }

    function set(
        MarketCache memory cache,
        uint256 marketId
    )
        internal
        pure
    {
        // Devs should not be able to call this function once the `markets` array has been initialized (non-zero length)
        Require.that(
            cache.markets.length == 0,
            FILE,
            "already initialized"
        );

        Bits.setBit(cache.marketBitmaps, marketId);

        cache.marketsLength += 1;
    }

    function getAtIndex(
        MarketCache memory cache,
        uint256 index
    )
        internal
        pure
        returns (MarketInfo memory)
    {
        Require.that(
            index < cache.markets.length,
            FILE,
            "invalid index",
            index,
            cache.markets.length
        );
        return cache.markets[index];
    }

    // ============ Private Functions ============

    function _getInternal(
        MarketInfo[] memory data,
        uint beginInclusive,
        uint endExclusive,
        uint marketId
    ) private pure returns (MarketInfo memory) {
        uint len = endExclusive - beginInclusive;
        // If length equals 0 OR length equals 1 but the item wasn't found, revert
        assert(!(len == 0 || (len == 1 && data[beginInclusive].marketId != marketId)));

        uint mid = beginInclusive + len / 2;
        uint midMarketId = data[mid].marketId;
        if (marketId < midMarketId) {
            return _getInternal(
                data,
                beginInclusive,
                mid,
                marketId
            );
        } else if (marketId > midMarketId) {
            return _getInternal(
                data,
                mid + 1,
                endExclusive,
                marketId
            );
        } else {
            return data[mid];
        }
    }

}

File 20 of 43 : Bits.sol
/*

    Copyright 2019 dYdX Trading Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { Require } from "./Require.sol";


/**
 * @title Bits
 * @author Dolomite
 *
 * Library for caching information about markets
 */
library Bits {

    // ============ Constants ============

    uint internal constant ONE = 1;
    uint256 internal constant MAX_UINT_BITS = 256;

    // ============ Functions ============

    function createBitmaps(uint maxLength) internal pure returns (uint[] memory) {
        return new uint[]((maxLength / MAX_UINT_BITS) + ONE);
    }

    function getMarketIdFromBit(
        uint index,
        uint bit
    ) internal pure returns (uint) {
        return (MAX_UINT_BITS * index) + bit;
    }

    function setBit(
        uint[] memory bitmaps,
        uint marketId
    ) internal pure {
        uint bucketIndex = marketId / MAX_UINT_BITS;
        uint indexFromRight = marketId % MAX_UINT_BITS;
        bitmaps[bucketIndex] |= (ONE << indexFromRight);
    }

    function hasBit(
        uint[] memory bitmaps,
        uint marketId
    ) internal pure returns (bool) {
        uint bucketIndex = marketId / MAX_UINT_BITS;
        uint indexFromRight = marketId % MAX_UINT_BITS;
        uint bit = bitmaps[bucketIndex] & (ONE << indexFromRight);
        return bit > 0;
    }

    function unsetBit(
        uint bitmap,
        uint bit
    ) internal pure returns (uint) {
        return bitmap - (ONE << bit);
    }

    // solium-disable security/no-assign-params
    function getLeastSignificantBit(uint256 x) internal pure returns (uint) {
        // gas usage peaks at 350 per call

        uint lsb = 255;

        if (x & uint128(-1) > 0) {
            lsb -= 128;
        } else {
            x >>= 128;
        }

        if (x & uint64(-1) > 0) {
            lsb -= 64;
        } else {
            x >>= 64;
        }

        if (x & uint32(-1) > 0) {
            lsb -= 32;
        } else {
            x >>= 32;
        }

        if (x & uint16(-1) > 0) {
            lsb -= 16;
        } else {
            x >>= 16;
        }

        if (x & uint8(-1) > 0) {
            lsb -= 8;
        } else {
            x >>= 8;
        }

        if (x & 0xf > 0) {
            lsb -= 4;
        } else {
            x >>= 4;
        }

        if (x & 0x3 > 0) {
            lsb -= 2;
        } else {
            x >>= 2;
            // solium-enable security/no-assign-params
        }

        if (x & 0x1 > 0) {
            lsb -= 1;
        }

        return lsb;
    }
}

File 21 of 43 : Actions.sol
/*

    Copyright 2019 dYdX Trading Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { Account } from "./Account.sol";
import { Types } from "./Types.sol";


/**
 * @title Actions
 * @author dYdX
 *
 * Library that defines and parses valid Actions
 */
library Actions {

    // ============ Constants ============

    bytes32 private constant FILE = "Actions";

    // ============ Enums ============

    enum ActionType {
        Deposit,   // supply tokens
        Withdraw,  // borrow tokens
        Transfer,  // transfer balance between accounts
        Buy,       // buy an amount of some token (externally)
        Sell,      // sell an amount of some token (externally)
        Trade,     // trade tokens against another account
        Liquidate, // liquidate an undercollateralized or expiring account
        Vaporize,  // use excess tokens to zero-out a completely negative account
        Call       // send arbitrary data to an address
    }

    enum AccountLayout {
        OnePrimary,
        TwoPrimary,
        PrimaryAndSecondary
    }

    enum MarketLayout {
        ZeroMarkets,
        OneMarket,
        TwoMarkets
    }

    // ============ Structs ============

    /*
     * Arguments that are passed to DolomiteMargin in an ordered list as part of a single operation.
     * Each ActionArgs has an actionType which specifies which action struct that this data will be
     * parsed into before being processed.
     */
    struct ActionArgs {
        ActionType actionType;
        uint256 accountId;
        Types.AssetAmount amount;
        uint256 primaryMarketId;
        uint256 secondaryMarketId;
        address otherAddress;
        uint256 otherAccountId;
        bytes data;
    }

    // ============ Action Types ============

    /*
     * Moves tokens from an address to DolomiteMargin. Can either repay a borrow or provide additional supply.
     */
    struct DepositArgs {
        Types.AssetAmount amount;
        Account.Info account;
        uint256 market;
        address from;
    }

    /*
     * Moves tokens from DolomiteMargin to another address. Can either borrow tokens or reduce the amount
     * previously supplied.
     */
    struct WithdrawArgs {
        Types.AssetAmount amount;
        Account.Info account;
        uint256 market;
        address to;
    }

    /*
     * Transfers balance between two accounts. The msg.sender must be an operator for both accounts.
     * The amount field applies to accountOne.
     * This action does not require any token movement since the trade is done internally to DolomiteMargin.
     */
    struct TransferArgs {
        Types.AssetAmount amount;
        Account.Info accountOne;
        Account.Info accountTwo;
        uint256 market;
    }

    /*
     * Acquires a certain amount of tokens by spending other tokens. Sends takerMarket tokens to the
     * specified exchangeWrapper contract and expects makerMarket tokens in return. The amount field
     * applies to the makerMarket.
     */
    struct BuyArgs {
        Types.AssetAmount amount;
        Account.Info account;
        uint256 makerMarket;
        uint256 takerMarket;
        address exchangeWrapper;
        bytes orderData;
    }

    /*
     * Spends a certain amount of tokens to acquire other tokens. Sends takerMarket tokens to the
     * specified exchangeWrapper and expects makerMarket tokens in return. The amount field applies
     * to the takerMarket.
     */
    struct SellArgs {
        Types.AssetAmount amount;
        Account.Info account;
        uint256 takerMarket;
        uint256 makerMarket;
        address exchangeWrapper;
        bytes orderData;
    }

    /*
     * Trades balances between two accounts using any external contract that implements the
     * AutoTrader interface. The AutoTrader contract must be an operator for the makerAccount (for
     * which it is trading on-behalf-of). The amount field applies to the makerAccount and the
     * inputMarket. This proposed change to the makerAccount is passed to the AutoTrader which will
     * quote a change for the makerAccount in the outputMarket (or will disallow the trade).
     * This action does not require any token movement since the trade is done internally to DolomiteMargin.
     */
    struct TradeArgs {
        Types.AssetAmount amount;
        Account.Info takerAccount;
        Account.Info makerAccount;
        uint256 inputMarket;
        uint256 outputMarket;
        address autoTrader;
        bytes tradeData;
    }

    /*
     * Each account must maintain a certain margin-ratio (specified globally). If the account falls
     * below this margin-ratio, it can be liquidated by any other account. This allows anyone else
     * (arbitrageurs) to repay any borrowed asset (owedMarket) of the liquidating account in
     * exchange for any collateral asset (heldMarket) of the liquidAccount. The ratio is determined
     * by the price ratio (given by the oracles) plus a spread (specified globally). Liquidating an
     * account also sets a flag on the account that the account is being liquidated. This allows
     * anyone to continue liquidating the account until there are no more borrows being taken by the
     * liquidating account. Liquidators do not have to liquidate the entire account all at once but
     * can liquidate as much as they choose. The liquidating flag allows liquidators to continue
     * liquidating the account even if it becomes collateralized through partial liquidation or
     * price movement.
     */
    struct LiquidateArgs {
        Types.AssetAmount amount;
        Account.Info solidAccount;
        Account.Info liquidAccount;
        uint256 owedMarket;
        uint256 heldMarket;
    }

    /*
     * Similar to liquidate, but vaporAccounts are accounts that have only negative balances remaining. The arbitrageur
     * pays back the negative asset (owedMarket) of the vaporAccount in exchange for a collateral asset (heldMarket) at
     * a favorable spread. However, since the liquidAccount has no collateral assets, the collateral must come from
     * DolomiteMargin's excess tokens.
     */
    struct VaporizeArgs {
        Types.AssetAmount amount;
        Account.Info solidAccount;
        Account.Info vaporAccount;
        uint256 owedMarket;
        uint256 heldMarket;
    }

    /*
     * Passes arbitrary bytes of data to an external contract that implements the Callee interface.
     * Does not change any asset amounts. This function may be useful for setting certain variables
     * on layer-two contracts for certain accounts without having to make a separate Ethereum
     * transaction for doing so. Also, the second-layer contracts can ensure that the call is coming
     * from an operator of the particular account.
     */
    struct CallArgs {
        Account.Info account;
        address callee;
        bytes data;
    }

    // ============ Helper Functions ============

    function getMarketLayout(
        ActionType actionType
    )
        internal
        pure
        returns (MarketLayout)
    {
        if (
            actionType == Actions.ActionType.Deposit
            || actionType == Actions.ActionType.Withdraw
            || actionType == Actions.ActionType.Transfer
        ) {
            return MarketLayout.OneMarket;
        }
        else if (actionType == Actions.ActionType.Call) {
            return MarketLayout.ZeroMarkets;
        }
        return MarketLayout.TwoMarkets;
    }

    function getAccountLayout(
        ActionType actionType
    )
        internal
        pure
        returns (AccountLayout)
    {
        if (
            actionType == Actions.ActionType.Transfer
            || actionType == Actions.ActionType.Trade
        ) {
            return AccountLayout.TwoPrimary;
        } else if (
            actionType == Actions.ActionType.Liquidate
            || actionType == Actions.ActionType.Vaporize
        ) {
            return AccountLayout.PrimaryAndSecondary;
        }
        return AccountLayout.OnePrimary;
    }

    // ============ Parsing Functions ============

    function parseDepositArgs(
        Account.Info[] memory accounts,
        ActionArgs memory args
    )
        internal
        pure
        returns (DepositArgs memory)
    {
        return DepositArgs({
            amount: args.amount,
            account: accounts[args.accountId],
            market: args.primaryMarketId,
            from: args.otherAddress
        });
    }

    function parseWithdrawArgs(
        Account.Info[] memory accounts,
        ActionArgs memory args
    )
        internal
        pure
        returns (WithdrawArgs memory)
    {
        return WithdrawArgs({
            amount: args.amount,
            account: accounts[args.accountId],
            market: args.primaryMarketId,
            to: args.otherAddress
        });
    }

    function parseTransferArgs(
        Account.Info[] memory accounts,
        ActionArgs memory args
    )
        internal
        pure
        returns (TransferArgs memory)
    {
        return TransferArgs({
            amount: args.amount,
            accountOne: accounts[args.accountId],
            accountTwo: accounts[args.otherAccountId],
            market: args.primaryMarketId
        });
    }

    function parseBuyArgs(
        Account.Info[] memory accounts,
        ActionArgs memory args
    )
        internal
        pure
        returns (BuyArgs memory)
    {
        return BuyArgs({
            amount: args.amount,
            account: accounts[args.accountId],
            makerMarket: args.primaryMarketId,
            takerMarket: args.secondaryMarketId,
            exchangeWrapper: args.otherAddress,
            orderData: args.data
        });
    }

    function parseSellArgs(
        Account.Info[] memory accounts,
        ActionArgs memory args
    )
        internal
        pure
        returns (SellArgs memory)
    {
        return SellArgs({
            amount: args.amount,
            account: accounts[args.accountId],
            takerMarket: args.primaryMarketId,
            makerMarket: args.secondaryMarketId,
            exchangeWrapper: args.otherAddress,
            orderData: args.data
        });
    }

    function parseTradeArgs(
        Account.Info[] memory accounts,
        ActionArgs memory args
    )
        internal
        pure
        returns (TradeArgs memory)
    {
        return TradeArgs({
            amount: args.amount,
            takerAccount: accounts[args.accountId],
            makerAccount: accounts[args.otherAccountId],
            inputMarket: args.primaryMarketId,
            outputMarket: args.secondaryMarketId,
            autoTrader: args.otherAddress,
            tradeData: args.data
        });
    }

    function parseLiquidateArgs(
        Account.Info[] memory accounts,
        ActionArgs memory args
    )
        internal
        pure
        returns (LiquidateArgs memory)
    {
        return LiquidateArgs({
            amount: args.amount,
            solidAccount: accounts[args.accountId],
            liquidAccount: accounts[args.otherAccountId],
            owedMarket: args.primaryMarketId,
            heldMarket: args.secondaryMarketId
        });
    }

    function parseVaporizeArgs(
        Account.Info[] memory accounts,
        ActionArgs memory args
    )
        internal
        pure
        returns (VaporizeArgs memory)
    {
        return VaporizeArgs({
            amount: args.amount,
            solidAccount: accounts[args.accountId],
            vaporAccount: accounts[args.otherAccountId],
            owedMarket: args.primaryMarketId,
            heldMarket: args.secondaryMarketId
        });
    }

    function parseCallArgs(
        Account.Info[] memory accounts,
        ActionArgs memory args
    )
        internal
        pure
        returns (CallArgs memory)
    {
        return CallArgs({
            account: accounts[args.accountId],
            callee: args.otherAddress,
            data: args.data
        });
    }
}

File 22 of 43 : Account.sol
/*

    Copyright 2019 dYdX Trading Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { Types } from "./Types.sol";
import { EnumerableSet } from "./EnumerableSet.sol";


/**
 * @title Account
 * @author dYdX
 *
 * Library of structs and functions that represent an account
 */
library Account {
    // ============ Enums ============

    /*
     * Most-recently-cached account status.
     *
     * Normal: Can only be liquidated if the account values are violating the global margin-ratio.
     * Liquid: Can be liquidated no matter the account values.
     *         Can be vaporized if there are no more positive account values.
     * Vapor:  Has only negative (or zeroed) account values. Can be vaporized.
     *
     */
    enum Status {
        Normal,
        Liquid,
        Vapor
    }

    // ============ Structs ============

    // Represents the unique key that specifies an account
    struct Info {
        address owner;  // The address that owns the account
        uint256 number; // A nonce that allows a single address to control many accounts
    }

    // The complete storage for any account
    struct Storage {
        Status status;
        uint32 numberOfMarketsWithDebt;
        EnumerableSet.Set marketsWithNonZeroBalanceSet;
        mapping (uint256 => Types.Par) balances; // Mapping from marketId to principal
    }

    // ============ Library Functions ============

    function equals(
        Info memory a,
        Info memory b
    )
        internal
        pure
        returns (bool)
    {
        return a.owner == b.owner && a.number == b.number;
    }
}

File 23 of 43 : IPriceOracle.sol
/*

    Copyright 2019 dYdX Trading Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { Monetary } from "../lib/Monetary.sol";


/**
 * @title IPriceOracle
 * @author dYdX
 *
 * Interface that Price Oracles for DolomiteMargin must implement in order to report prices.
 */
contract IPriceOracle {

    // ============ Constants ============

    uint256 public constant ONE_DOLLAR = 10 ** 36;

    // ============ Public Functions ============

    /**
     * Get the price of a token
     *
     * @param  token  The ERC20 token address of the market
     * @return        The USD price of a base unit of the token, then multiplied by 10^36.
     *                So a USD-stable coin with 18 decimal places would return 10^18.
     *                This is the price of the base unit rather than the price of a "human-readable"
     *                token amount. Every ERC20 may have a different number of decimals.
     */
    function getPrice(
        address token
    )
        public
        view
        returns (Monetary.Price memory);
}

File 24 of 43 : IInterestSetter.sol
/*

    Copyright 2019 dYdX Trading Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { Interest } from "../lib/Interest.sol";


/**
 * @title IInterestSetter
 * @author dYdX
 *
 * Interface that Interest Setters for DolomiteMargin must implement in order to report interest rates.
 */
interface IInterestSetter {

    // ============ Public Functions ============

    /**
     * Get the interest rate of a token given some borrowed and supplied amounts
     *
     * @param  token        The address of the ERC20 token for the market
     * @param  borrowWei    The total borrowed token amount for the market
     * @param  supplyWei    The total supplied token amount for the market
     * @return              The interest rate per second
     */
    function getInterestRate(
        address token,
        uint256 borrowWei,
        uint256 supplyWei
    )
        external
        view
        returns (Interest.Rate memory);
}

File 25 of 43 : IExchangeWrapper.sol
/*

    Copyright 2019 dYdX Trading Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;


/**
 * @title IExchangeWrapper
 * @author dYdX
 *
 * Interface that Exchange Wrappers for DolomiteMargin must implement in order to trade ERC20 tokens.
 */
interface IExchangeWrapper {

    // ============ Public Functions ============

    /**
     * Exchange some amount of takerToken for makerToken.
     *
     * @param  _tradeOriginator     Address of the initiator of the trade (however, this value
     *                              cannot always be trusted as it is set at the discretion of the
     *                              msg.sender)
     * @param  _receiver            Address to set allowance on once the trade has completed
     * @param  _makerToken          The token to receive (target asset; IE path[path.length - 1])
     * @param  _takerToken          The token to pay (originator asset; IE path[0])
     * @param  _requestedFillAmount Amount of takerToken being paid
     * @param  _orderData           Arbitrary bytes data for any information to pass to the exchange
     * @return                      The amount of makerToken received
     */
    function exchange(
        address _tradeOriginator,
        address _receiver,
        address _makerToken,
        address _takerToken,
        uint256 _requestedFillAmount,
        bytes calldata _orderData
    )
        external
        returns (uint256);

    /**
     * Get amount of takerToken required to buy a certain amount of makerToken for a given trade.
     * Should match the takerToken amount used in exchangeForAmount. If the order cannot provide
     * exactly desiredMakerToken, then it must return the price to buy the minimum amount greater
     * than desiredMakerToken
     *
     * @param  _makerToken          The token to receive (target asset; IE path[path.length - 1])
     * @param  _takerToken          The token to pay (originator asset; IE path[0])
     * @param  _desiredMakerToken   Amount of `_makerToken` requested
     * @param  _orderData           Arbitrary bytes data for any information to pass to the exchange
     * @return                      Amount of `_takerToken` the needed to complete the exchange
     */
    function getExchangeCost(
        address _makerToken,
        address _takerToken,
        uint256 _desiredMakerToken,
        bytes calldata _orderData
    )
        external
        view
        returns (uint256);
}

File 26 of 43 : IERC20Detailed.sol
/*

    Copyright 2019 dYdX Trading Inc.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";


/**
 * @title IERC20
 * @author dYdX
 *
 * Interface for using ERC20 Tokens. We have to use a special interface to call ERC20 functions so
 * that we don't automatically revert when calling non-compliant tokens that have no return value for
 * transfer(), transferFrom(), or approve().
 */
contract IERC20Detailed is IERC20 {

    function name() external view returns (string memory);

    function symbol() external view returns (string memory);

    function decimals() external view returns (uint8);
}

File 27 of 43 : IDolomiteMargin.sol
/*

    Copyright 2021 Dolomite.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity >=0.5.0;
pragma experimental ABIEncoderV2;

import { IInterestSetter } from "../interfaces/IInterestSetter.sol";
import { IPriceOracle } from "../interfaces/IPriceOracle.sol";

import { Account } from "../lib/Account.sol";
import { Actions } from "../lib/Actions.sol";
import { Decimal } from "../lib/Decimal.sol";
import { Interest } from "../lib/Interest.sol";
import { Monetary } from "../lib/Monetary.sol";
import { Storage } from "../lib/Storage.sol";
import { Types } from "../lib/Types.sol";

interface IDolomiteMargin {

    // ============ Getters for Markets ============

    /**
     * Get the ERC20 token address for a market.
     *
     * @param  token    The token to query
     * @return          The token's marketId if the token is valid
     */
    function getMarketIdByTokenAddress(
        address token
    ) external view returns (uint256);

    /**
     * Get the ERC20 token address for a market.
     *
     * @param  marketId  The market to query
     * @return           The token address
     */
    function getMarketTokenAddress(
        uint256 marketId
    ) external view returns (address);

    /**
     * Return true if a particular market is in closing mode. Additional borrows cannot be taken
     * from a market that is closing.
     *
     * @param  marketId  The market to query
     * @return           True if the market is closing
     */
    function getMarketIsClosing(
        uint256 marketId
    )
    external
    view
    returns (bool);

    /**
     * Get the price of the token for a market.
     *
     * @param  marketId  The market to query
     * @return           The price of each atomic unit of the token
     */
    function getMarketPrice(
        uint256 marketId
    ) external view returns (Monetary.Price memory);

    /**
     * Get the total number of markets.
     *
     * @return  The number of markets
     */
    function getNumMarkets() external view returns (uint256);

    /**
     * Get the total principal amounts (borrowed and supplied) for a market.
     *
     * @param  marketId  The market to query
     * @return           The total principal amounts
     */
    function getMarketTotalPar(
        uint256 marketId
    ) external view returns (Types.TotalPar memory);

    /**
     * Get the most recently cached interest index for a market.
     *
     * @param  marketId  The market to query
     * @return           The most recent index
     */
    function getMarketCachedIndex(
        uint256 marketId
    ) external view returns (Interest.Index memory);

    /**
     * Get the interest index for a market if it were to be updated right now.
     *
     * @param  marketId  The market to query
     * @return           The estimated current index
     */
    function getMarketCurrentIndex(
        uint256 marketId
    ) external view returns (Interest.Index memory);

    /**
     * Get the price oracle address for a market.
     *
     * @param  marketId  The market to query
     * @return           The price oracle address
     */
    function getMarketPriceOracle(
        uint256 marketId
    ) external view returns (IPriceOracle);

    /**
     * Get the interest-setter address for a market.
     *
     * @param  marketId  The market to query
     * @return           The interest-setter address
     */
    function getMarketInterestSetter(
        uint256 marketId
    ) external view returns (IInterestSetter);

    /**
     * Get the margin premium for a market. A margin premium makes it so that any positions that
     * include the market require a higher collateralization to avoid being liquidated.
     *
     * @param  marketId  The market to query
     * @return           The market's margin premium
     */
    function getMarketMarginPremium(
        uint256 marketId
    ) external view returns (Decimal.D256 memory);

    /**
     * Get the spread premium for a market. A spread premium makes it so that any liquidations
     * that include the market have a higher spread than the global default.
     *
     * @param  marketId  The market to query
     * @return           The market's spread premium
     */
    function getMarketSpreadPremium(
        uint256 marketId
    ) external view returns (Decimal.D256 memory);

    /**
     * Return true if this market can be removed and its ID can be recycled and reused
     *
     * @param  marketId  The market to query
     * @return           True if the market is recyclable
     */
    function getMarketIsRecyclable(
        uint256 marketId
    ) external view returns (bool);

    /**
     * Gets the recyclable markets, up to `n` length. If `n` is greater than the length of the list, 0's are returned
     * for the empty slots.
     *
     * @param  n    The number of markets to get, bounded by the linked list being smaller than `n`
     * @return      The list of recyclable markets, in the same order held by the linked list
     */
    function getRecyclableMarkets(
        uint256 n
    ) external view returns (uint[] memory);

    /**
     * Get the current borrower interest rate for a market.
     *
     * @param  marketId  The market to query
     * @return           The current interest rate
     */
    function getMarketInterestRate(
        uint256 marketId
    ) external view returns (Interest.Rate memory);

    /**
     * Get basic information about a particular market.
     *
     * @param  marketId  The market to query
     * @return           A Storage.Market struct with the current state of the market
     */
    function getMarket(
        uint256 marketId
    ) external view returns (Storage.Market memory);

    /**
     * Get comprehensive information about a particular market.
     *
     * @param  marketId  The market to query
     * @return           A tuple containing the values:
     *                    - A Storage.Market struct with the current state of the market
     *                    - The current estimated interest index
     *                    - The current token price
     *                    - The current market interest rate
     */
    function getMarketWithInfo(
        uint256 marketId
    )
    external
    view
    returns (
        Storage.Market memory,
        Interest.Index memory,
        Monetary.Price memory,
        Interest.Rate memory
    );

    /**
     * Get the number of excess tokens for a market. The number of excess tokens is calculated by taking the current
     * number of tokens held in DolomiteMargin, adding the number of tokens owed to DolomiteMargin by borrowers, and
     * subtracting the number of tokens owed to suppliers by DolomiteMargin.
     *
     * @param  marketId  The market to query
     * @return           The number of excess tokens
     */
    function getNumExcessTokens(
        uint256 marketId
    ) external view returns (Types.Wei memory);

    // ============ Getters for Accounts ============

    /**
     * Get the principal value for a particular account and market.
     *
     * @param  account   The account to query
     * @param  marketId  The market to query
     * @return           The principal value
     */
    function getAccountPar(
        Account.Info calldata account,
        uint256 marketId
    ) external view returns (Types.Par memory);

    /**
     * Get the principal value for a particular account and market, with no check the market is valid. Meaning, markets
     * that don't exist return 0.
     *
     * @param  account   The account to query
     * @param  marketId  The market to query
     * @return           The principal value
     */
    function getAccountParNoMarketCheck(
        Account.Info calldata account,
        uint256 marketId
    ) external view returns (Types.Par memory);

    /**
     * Get the token balance for a particular account and market.
     *
     * @param  account   The account to query
     * @param  marketId  The market to query
     * @return           The token amount
     */
    function getAccountWei(
        Account.Info calldata account,
        uint256 marketId
    ) external view returns (Types.Wei memory);

    /**
     * Get the status of an account (Normal, Liquidating, or Vaporizing).
     *
     * @param  account  The account to query
     * @return          The account's status
     */
    function getAccountStatus(
        Account.Info calldata account
    ) external view returns (Account.Status);

    /**
     * Get a list of markets that have a non-zero balance for an account
     *
     * @param  account  The account to query
     * @return          The non-sorted marketIds with non-zero balance for the account.
     */
    function getAccountMarketsWithBalances(
        Account.Info calldata account
    ) external view returns (uint256[] memory);

    /**
     * Get the number of markets that have a non-zero balance for an account
     *
     * @param  account  The account to query
     * @return          The non-sorted marketIds with non-zero balance for the account.
     */
    function getAccountNumberOfMarketsWithBalances(
        Account.Info calldata account
    ) external view returns (uint256);

    /**
     * Get the marketId for an account's market with a non-zero balance at the given index
     *
     * @param  account  The account to query
     * @return          The non-sorted marketIds with non-zero balance for the account.
     */
    function getAccountMarketWithBalanceAtIndex(
        Account.Info calldata account,
        uint256 index
    ) external view returns (uint256);

    /**
     * Get the number of markets with which an account has a negative balance.
     *
     * @param  account  The account to query
     * @return          The non-sorted marketIds with non-zero balance for the account.
     */
    function getAccountNumberOfMarketsWithDebt(
        Account.Info calldata account
    ) external view returns (uint256);

    /**
     * Get the total supplied and total borrowed value of an account.
     *
     * @param  account  The account to query
     * @return          The following values:
     *                   - The supplied value of the account
     *                   - The borrowed value of the account
     */
    function getAccountValues(
        Account.Info calldata account
    ) external view returns (Monetary.Value memory, Monetary.Value memory);

    /**
     * Get the total supplied and total borrowed values of an account adjusted by the marginPremium
     * of each market. Supplied values are divided by (1 + marginPremium) for each market and
     * borrowed values are multiplied by (1 + marginPremium) for each market. Comparing these
     * adjusted values gives the margin-ratio of the account which will be compared to the global
     * margin-ratio when determining if the account can be liquidated.
     *
     * @param  account  The account to query
     * @return          The following values:
     *                   - The supplied value of the account (adjusted for marginPremium)
     *                   - The borrowed value of the account (adjusted for marginPremium)
     */
    function getAdjustedAccountValues(
        Account.Info calldata account
    ) external view returns (Monetary.Value memory, Monetary.Value memory);

    /**
     * Get an account's summary for each market.
     *
     * @param  account  The account to query
     * @return          The following values:
     *                   - The market IDs for each market
     *                   - The ERC20 token address for each market
     *                   - The account's principal value for each market
     *                   - The account's (supplied or borrowed) number of tokens for each market
     */
    function getAccountBalances(
        Account.Info calldata account
    ) external view returns (uint[] memory, address[] memory, Types.Par[] memory, Types.Wei[] memory);

    // ============ Getters for Account Permissions ============

    /**
     * Return true if a particular address is approved as an operator for an owner's accounts.
     * Approved operators can act on the accounts of the owner as if it were the operator's own.
     *
     * @param  owner     The owner of the accounts
     * @param  operator  The possible operator
     * @return           True if operator is approved for owner's accounts
     */
    function getIsLocalOperator(
        address owner,
        address operator
    ) external view returns (bool);

    /**
     * Return true if a particular address is approved as a global operator. Such an address can
     * act on any account as if it were the operator's own.
     *
     * @param  operator  The address to query
     * @return           True if operator is a global operator
     */
    function getIsGlobalOperator(
        address operator
    ) external view returns (bool);

    /**
     * Checks if the autoTrader can only be called invoked by a global operator
     *
     * @param autoTrader    The trader that should be checked for special call privileges.
     */
    function getIsAutoTraderSpecial(address autoTrader) external view returns (bool);

    // ============ Getters for Risk Params ============

    /**
     * Get the global minimum margin-ratio that every position must maintain to prevent being
     * liquidated.
     *
     * @return  The global margin-ratio
     */
    function getMarginRatio() external view returns (Decimal.D256 memory);

    /**
     * Get the global liquidation spread. This is the spread between oracle prices that incentivizes
     * the liquidation of risky positions.
     *
     * @return  The global liquidation spread
     */
    function getLiquidationSpread() external view returns (Decimal.D256 memory);

    /**
     * Get the adjusted liquidation spread for some market pair. This is equal to the global
     * liquidation spread multiplied by (1 + spreadPremium) for each of the two markets.
     *
     * @param  heldMarketId  The market for which the account has collateral
     * @param  owedMarketId  The market for which the account has borrowed tokens
     * @return               The adjusted liquidation spread
     */
    function getLiquidationSpreadForPair(
        uint256 heldMarketId,
        uint256 owedMarketId
    ) external view returns (Decimal.D256 memory);

    /**
     * Get the global earnings-rate variable that determines what percentage of the interest paid
     * by borrowers gets passed-on to suppliers.
     *
     * @return  The global earnings rate
     */
    function getEarningsRate() external view returns (Decimal.D256 memory);

    /**
     * Get the global minimum-borrow value which is the minimum value of any new borrow on DolomiteMargin.
     *
     * @return  The global minimum borrow value
     */
    function getMinBorrowedValue() external view returns (Monetary.Value memory);

    /**
     * Get all risk parameters in a single struct.
     *
     * @return  All global risk parameters
     */
    function getRiskParams() external view returns (Storage.RiskParams memory);

    /**
     * Get all risk parameter limits in a single struct. These are the maximum limits at which the
     * risk parameters can be set by the admin of DolomiteMargin.
     *
     * @return  All global risk parameter limits
     */
    function getRiskLimits() external view returns (Storage.RiskLimits memory);

    // ============ Write Functions ============

    /**
     * The main entry-point to DolomiteMargin that allows users and contracts to manage accounts.
     * Take one or more actions on one or more accounts. The msg.sender must be the owner or
     * operator of all accounts except for those being liquidated, vaporized, or traded with.
     * One call to operate() is considered a singular "operation". Account collateralization is
     * ensured only after the completion of the entire operation.
     *
     * @param  accounts  A list of all accounts that will be used in this operation. Cannot contain
     *                   duplicates. In each action, the relevant account will be referred-to by its
     *                   index in the list.
     * @param  actions   An ordered list of all actions that will be taken in this operation. The
     *                   actions will be processed in order.
     */
    function operate(
        Account.Info[] calldata accounts,
        Actions.ActionArgs[] calldata actions
    ) external;

    /**
     * Approves/disapproves any number of operators. An operator is an external address that has the
     * same permissions to manipulate an account as the owner of the account. Operators are simply
     * addresses and therefore may either be externally-owned Ethereum accounts OR smart contracts.
     *
     * Operators are also able to act as AutoTrader contracts on behalf of the account owner if the
     * operator is a smart contract and implements the IAutoTrader interface.
     *
     * @param  args  A list of OperatorArgs which have an address and a boolean. The boolean value
     *               denotes whether to approve (true) or revoke approval (false) for that address.
     */
    function setOperators(
        Types.OperatorArg[] calldata args
    ) external;

    // =========================================
    // ============ Owner Functions ============
    // =========================================

    // ============ Token Functions ============

    /**
     * Withdraw an ERC20 token for which there is an associated market. Only excess tokens can be withdrawn. The number
     * of excess tokens is calculated by taking the current number of tokens held in DolomiteMargin, adding the number
     * of tokens owed to DolomiteMargin by borrowers, and subtracting the number of tokens owed to suppliers by
     * DolomiteMargin.
     */
    function ownerWithdrawExcessTokens(
        uint256 marketId,
        address recipient
    )
    external
    returns (uint256);

    /**
     * Withdraw an ERC20 token for which there is no associated market.
     */
    function ownerWithdrawUnsupportedTokens(
        address token,
        address recipient
    )
    external
    returns (uint256);

    // ============ Market Functions ============

    /**
     * Sets the number of non-zero balances an account may have within the same `accountIndex`. This ensures a user
     * cannot DOS the system by filling their account with non-zero balances (which linearly increases gas costs when
     * checking collateralization) and disallowing themselves to close the position, because the number of gas units
     * needed to process their transaction exceed the block's gas limit. In turn, this would  prevent the user from also
     * being liquidated, causing the all of the capital to be "stuck" in the position.
     *
     * Lowering this number does not "freeze" user accounts that have more than the new limit of balances, because this
     * variable is enforced by checking the users number of non-zero balances against the max or if it sizes down before
     * each transaction finishes.
     */
    function ownerSetAccountMaxNumberOfMarketsWithBalances(
        uint256 accountMaxNumberOfMarketsWithBalances
    )
    external;

    /**
     * Add a new market to DolomiteMargin. Must be for a previously-unsupported ERC20 token.
     */
    function ownerAddMarket(
        address token,
        IPriceOracle priceOracle,
        IInterestSetter interestSetter,
        Decimal.D256 calldata marginPremium,
        Decimal.D256 calldata spreadPremium,
        uint256 maxWei,
        bool isClosing,
        bool isRecyclable
    )
    external;

    /**
     * Removes a market from DolomiteMargin, sends any remaining tokens in this contract to `salvager` and invokes the
     * recyclable callback
     */
    function ownerRemoveMarkets(
        uint[] calldata marketIds,
        address salvager
    )
    external;

    /**
     * Set (or unset) the status of a market to "closing". The borrowedValue of a market cannot increase while its
     * status is "closing".
     */
    function ownerSetIsClosing(
        uint256 marketId,
        bool isClosing
    )
    external;

    /**
     * Set the price oracle for a market.
     */
    function ownerSetPriceOracle(
        uint256 marketId,
        IPriceOracle priceOracle
    )
    external;

    /**
     * Set the interest-setter for a market.
     */
    function ownerSetInterestSetter(
        uint256 marketId,
        IInterestSetter interestSetter
    )
    external;

    /**
     * Set a premium on the minimum margin-ratio for a market. This makes it so that any positions that include this
     * market require a higher collateralization to avoid being liquidated.
     */
    function ownerSetMarginPremium(
        uint256 marketId,
        Decimal.D256 calldata marginPremium
    )
    external;

    /**
     * Sets the maximum supply wei for a given `marketId`.
     */
    function ownerSetMaxWei(
        uint256 marketId,
        uint256 maxWei
    )
    external;

    /**
     * Set a premium on the liquidation spread for a market. This makes it so that any liquidations that include this
     * market have a higher spread than the global default.
     */
    function ownerSetSpreadPremium(
        uint256 marketId,
        Decimal.D256 calldata spreadPremium
    )
    external;

    // ============ Risk Functions ============

    /**
     * Set the global minimum margin-ratio that every position must maintain to prevent being liquidated.
     */
    function ownerSetMarginRatio(
        Decimal.D256 calldata ratio
    )
    external;

    /**
     * Set the global liquidation spread. This is the spread between oracle prices that incentivizes the liquidation of
     * risky positions.
     */
    function ownerSetLiquidationSpread(
        Decimal.D256 calldata spread
    )
    external;

    /**
     * Set the global earnings-rate variable that determines what percentage of the interest paid by borrowers gets
     * passed-on to suppliers.
     */
    function ownerSetEarningsRate(
        Decimal.D256 calldata earningsRate
    )
    external;

    /**
     * Set the global minimum-borrow value which is the minimum value of any new borrow on DolomiteMargin.
     */
    function ownerSetMinBorrowedValue(
        Monetary.Value calldata minBorrowedValue
    )
    external;

    // ============ Global Operator Functions ============

    /**
     * Approve (or disapprove) an address that is permissioned to be an operator for all accounts in DolomiteMargin.
     * Intended only to approve smart-contracts.
     */
    function ownerSetGlobalOperator(
        address operator,
        bool approved
    )
    external;

    /**
     * Approve (or disapprove) an auto trader that can only be called by a global operator. IE for expirations
     */
    function ownerSetAutoTraderSpecial(
        address autoTrader,
        bool special
    )
    external;

    // ============ Owner Functions ============

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() external view returns (address);

    /**
     * @dev Returns true if the caller is the current owner.
     */
    function isOwner() external view returns (bool);

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions anymore. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby removing any functionality that is only available to the owner.
     */
    function renounceOwnership() external;

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) external;
}

File 28 of 43 : ParaswapTrader.sol
/*

    Copyright 2022 Dolomite.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { ReentrancyGuard } from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";

import { IDolomiteMargin } from "../../protocol/interfaces/IDolomiteMargin.sol";
import { IExchangeWrapper } from "../../protocol/interfaces/IExchangeWrapper.sol";

import { Account } from "../../protocol/lib/Account.sol";
import { Actions } from "../../protocol/lib/Actions.sol";
import { Decimal } from "../../protocol/lib/Decimal.sol";
import { Interest } from "../../protocol/lib/Interest.sol";
import { DolomiteMarginMath } from "../../protocol/lib/DolomiteMarginMath.sol";
import { Monetary } from "../../protocol/lib/Monetary.sol";
import { Require } from "../../protocol/lib/Require.sol";
import { Time } from "../../protocol/lib/Time.sol";
import { Types } from "../../protocol/lib/Types.sol";

import { OnlyDolomiteMargin } from "../helpers/OnlyDolomiteMargin.sol";
import { ERC20Lib } from "../lib/ERC20Lib.sol";


/**
 * @title ParaswapTrader
 * @author Dolomite
 *
 * Contract for performing an external trade with Paraswap.
 */
contract ParaswapTrader is OnlyDolomiteMargin, IExchangeWrapper {

    // ============ Constants ============

    bytes32 private constant FILE = "ParaswapTrader";

    // ============ Storage ============

    address public PARASWAP_AUGUSTUS_ROUTER;
    address public PARASWAP_TRANSFER_PROXY;

    // ============ Constructor ============

    constructor(
        address _paraswapAugustusRouter,
        address _paraswapTransferProxy,
        address _dolomiteMargin
    )
        public
        OnlyDolomiteMargin(_dolomiteMargin)
    {
        PARASWAP_AUGUSTUS_ROUTER = _paraswapAugustusRouter;
        PARASWAP_TRANSFER_PROXY = _paraswapTransferProxy;
    }

    // ============ Public Functions ============

    function exchange(
        address /* _tradeOriginator */,
        address _receiver,
        address _makerToken,
        address _takerToken,
        uint256 _requestedFillAmount,
        bytes calldata _orderData
    )
    external
    onlyDolomiteMargin(msg.sender)
    returns (uint256) {
        ERC20Lib.checkAllowanceAndApprove(_takerToken, PARASWAP_TRANSFER_PROXY, _requestedFillAmount);

        (uint256 minAmountOutWei, bytes memory paraswapCallData) = abi.decode(_orderData, (uint256, bytes));

        _callAndCheckSuccess(paraswapCallData);

        uint256 amount = IERC20(_makerToken).balanceOf(address(this));

        Require.that(
            amount >= minAmountOutWei,
            FILE,
            "insufficient output amount",
            amount,
            minAmountOutWei
        );

        ERC20Lib.checkAllowanceAndApprove(_makerToken, _receiver, amount);

        return amount;
    }

    function getExchangeCost(
        address,
        address,
        uint256,
        bytes calldata
    )
    external
    view
    returns (uint256) {
        revert(string(abi.encodePacked(Require.stringifyTruncated(FILE), "::getExchangeCost: not implemented")));
    }

    // ============ Private Functions ============

    function _callAndCheckSuccess(bytes memory _paraswapCallData) internal {
        // solium-disable-next-line security/no-low-level-calls
        (bool success, bytes memory result) = PARASWAP_AUGUSTUS_ROUTER.call(_paraswapCallData);
        if (!success) {
            if (result.length < 68) {
                revert(string(abi.encodePacked(Require.stringifyTruncated(FILE), ": revert")));
            } else {
                // solium-disable-next-line security/no-inline-assembly
                assembly {
                    result := add(result, 0x04)
                }
                revert(string(abi.encodePacked(Require.stringifyTruncated(FILE), ": ", abi.decode(result, (string)))));
            }
        }
    }
}

File 29 of 43 : LiquidatorProxyV2WithExternalLiquidity.sol
/*

    Copyright 2022 Dolomite.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { ReentrancyGuard } from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";

import { IDolomiteMargin } from "../../protocol/interfaces/IDolomiteMargin.sol";

import { Account } from "../../protocol/lib/Account.sol";
import { Actions } from "../../protocol/lib/Actions.sol";
import { Require } from "../../protocol/lib/Require.sol";
import { Types } from "../../protocol/lib/Types.sol";

import { HasLiquidatorRegistry } from "../helpers/HasLiquidatorRegistry.sol";
import { LiquidatorProxyBase } from "../helpers/LiquidatorProxyBase.sol";

import { IExpiry } from "../interfaces/IExpiry.sol";

import { AccountActionLib } from "../lib/AccountActionLib.sol";

import { ParaswapTrader } from "../traders/ParaswapTrader.sol";


/**
 * @title LiquidatorProxyV2WithExternalLiquidity
 * @author Dolomite
 *
 * Contract for liquidating other accounts in DolomiteMargin and atomically selling off collateral via Paraswap
 * liquidity aggregation
 */
contract LiquidatorProxyV2WithExternalLiquidity is ReentrancyGuard, ParaswapTrader, LiquidatorProxyBase {

    // ============ Constants ============

    bytes32 private constant FILE = "LiquidatorProxyV2";

    // ============ Storage ============

    IExpiry public EXPIRY_PROXY;

    // ============ Constructor ============

    constructor (
        address _expiryProxy,
        address _paraswapAugustusRouter,
        address _paraswapTransferProxy,
        address _dolomiteMargin,
        address _liquidatorAssetRegistry
    )
        public
        ParaswapTrader(
            _paraswapAugustusRouter,
            _paraswapTransferProxy,
            _dolomiteMargin
        )
        HasLiquidatorRegistry(
            _liquidatorAssetRegistry
        )
    {
        EXPIRY_PROXY = IExpiry(_expiryProxy);
    }

    // ============ Public Functions ============

    /**
     * Liquidate liquidAccount using solidAccount. This contract and the msg.sender to this contract must both be
     * operators for the solidAccount.
     *
     * @param _solidAccount                 The account that will do the liquidating
     * @param _liquidAccount                The account that will be liquidated
     * @param _owedMarket                   The owed market whose borrowed value will be added to `owedWeiToLiquidate`
     * @param _heldMarket                   The held market whose collateral will be recovered to take on the debt of
     *                                      `owedMarket`
     * @param _expiry                       The time at which the position expires, if this liquidation is for closing
     *                                      an expired position. Else, 0.
     * @param _paraswapCallData             The calldata to be passed along to Paraswap's router for liquidation
     */
    function liquidate(
        Account.Info memory _solidAccount,
        Account.Info memory _liquidAccount,
        uint256 _owedMarket,
        uint256 _heldMarket,
        uint256 _expiry,
        bytes memory _paraswapCallData
    )
        public
        nonReentrant
        requireIsAssetWhitelistedForLiquidation(_heldMarket)
        requireIsAssetWhitelistedForLiquidation(_owedMarket)
    {
        // put all values that will not change into a single struct
        LiquidatorProxyConstants memory constants;
        constants.dolomiteMargin = DOLOMITE_MARGIN;
        constants.solidAccount = _solidAccount;
        constants.liquidAccount = _liquidAccount;
        constants.heldMarket = _heldMarket;
        constants.owedMarket = _owedMarket;

        _checkConstants(constants, _expiry);

        constants.liquidMarkets = constants.dolomiteMargin.getAccountMarketsWithBalances(_liquidAccount);
        constants.markets = _getMarketInfos(
            constants.dolomiteMargin,
            constants.dolomiteMargin.getAccountMarketsWithBalances(_solidAccount),
            constants.liquidMarkets
        );
        constants.expiryProxy = _expiry > 0 ? EXPIRY_PROXY: IExpiry(address(0)); // don't read EXPIRY; it's not needed
        constants.expiry = uint32(_expiry);

        LiquidatorProxyCache memory cache = _initializeCache(constants);

        // validate the msg.sender and that the liquidAccount can be liquidated
        _checkBasicRequirements(constants);

        // get the max liquidation amount
        _calculateAndSetMaxLiquidationAmount(cache);

        Account.Info[] memory accounts = _constructAccountsArray(constants);

        // execute the liquidations
        constants.dolomiteMargin.operate(
            accounts,
            _constructActionsArray(
                constants,
                cache,
                /* _solidAccountId = */ 0, // solium-disable-line indentation
                /* _liquidAccount = */ 1, // solium-disable-line indentation
                _paraswapCallData
            )
        );
    }

    // ============ Internal Functions ============

    function _constructAccountsArray(
        LiquidatorProxyConstants memory _constants
    )
    internal
    pure
    returns (Account.Info[] memory)
    {
        Account.Info[] memory accounts = new Account.Info[](2);
        accounts[0] = _constants.solidAccount;
        accounts[1] = _constants.liquidAccount;
        return accounts;
    }

    function _constructActionsArray(
        LiquidatorProxyConstants memory _constants,
        LiquidatorProxyCache memory _cache,
        uint256 _solidAccountId,
        uint256 _liquidAccountId,
        bytes memory _paraswapCallData
    )
    internal
    view
    returns (Actions.ActionArgs[] memory)
    {
        Actions.ActionArgs[] memory actions = new Actions.ActionArgs[](2);

        if (_constants.expiry > 0) {
            // First action is a trade for closing the expired account
            // accountId is solidAccount; otherAccountId is liquidAccount
            actions[0] = AccountActionLib.encodeExpiryLiquidateAction(
                _solidAccountId,
                _liquidAccountId,
                _constants.owedMarket,
                _constants.heldMarket,
                address(_constants.expiryProxy),
                _constants.expiry,
                _cache.solidHeldUpdateWithReward,
                _cache.owedWeiToLiquidate,
                _cache.flipMarketsForExpiration
            );
        } else {
            // First action is a liquidation
            // accountId is solidAccount; otherAccountId is liquidAccount
            actions[0] = AccountActionLib.encodeLiquidateAction(
                _solidAccountId,
                _liquidAccountId,
                _constants.owedMarket,
                _constants.heldMarket,
                _cache.owedWeiToLiquidate
            );
        }

        actions[1] = AccountActionLib.encodeExternalSellAction(
            _solidAccountId,
            _constants.heldMarket,
            _constants.owedMarket,
            /* _trader = */ address(this), // solium-disable-line indentation
            _cache.solidHeldUpdateWithReward,
            _cache.owedWeiToLiquidate,
            _paraswapCallData
        );

        return actions;
    }
}

File 30 of 43 : ERC20Lib.sol
/*

    Copyright 2022 Dolomite.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

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


/**
 * @title ERC20Helper
 * @author Dolomite
 *
 * Library contract for reusable token actions
 */
library ERC20Lib {
    using SafeERC20 for IERC20;

    // ============ Functions ============

    function checkAllowanceAndApprove(
        address token,
        address spender,
        uint256 amount
    ) internal {
        if (IERC20(token).allowance(address(this), spender) < amount) {
            IERC20(token).safeApprove(spender, uint(- 1));
        }
    }
}

File 31 of 43 : AccountBalanceLib.sol
/*

    Copyright 2022 Dolomite.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { IDolomiteMargin } from "../../protocol/interfaces/IDolomiteMargin.sol";

import { Account } from "../../protocol/lib/Account.sol";
import { Require } from "../../protocol/lib/Require.sol";
import { Types } from "../../protocol/lib/Types.sol";


/**
 * @title AccountBalanceLib
 * @author Dolomite
 *
 * Library contract that checks a user's balance after transaction to be non-negative
 */
library AccountBalanceLib {
    using Types for Types.Par;

    // ============ Constants ============

    bytes32 private constant FILE = "AccountBalanceLib";

    // ============ Types ============

    /// Checks that either BOTH, FROM, or TO accounts all have non-negative balances
    enum BalanceCheckFlag {
        Both,
        From,
        To,
        None
    }

    // ============ Functions ============

    /**
     *  Checks that the account's balance is non-negative. Reverts if the check fails
     */
    function verifyBalanceIsNonNegative(
        IDolomiteMargin dolomiteMargin,
        address _owner,
        uint256 _accountIndex,
        uint256 _marketId
    ) internal view {
        Account.Info memory account = Account.Info(_owner, _accountIndex);
        Types.Par memory par = dolomiteMargin.getAccountPar(account, _marketId);
        Require.that(
            par.isPositive() || par.isZero(),
            FILE,
            "account cannot go negative",
            _owner,
            _accountIndex,
            _marketId
        );
    }
}

File 32 of 43 : AccountActionLib.sol
/*

    Copyright 2022 Dolomite.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { IDolomiteMargin } from "../../protocol/interfaces/IDolomiteMargin.sol";

import { Account } from "../../protocol/lib/Account.sol";
import { Actions } from "../../protocol/lib/Actions.sol";
import { Require } from "../../protocol/lib/Require.sol";
import { Types } from "../../protocol/lib/Types.sol";

import { IExpiry } from "../interfaces/IExpiry.sol";

import { AccountBalanceLib } from "./AccountBalanceLib.sol";


/**
 * @title AccountActionLib
 * @author Dolomite
 *
 * Library contract that makes specific actions easy to call
 */
library AccountActionLib {

    // ============ Constants ============

    bytes32 private constant FILE = "AccountActionLib";

    uint256 constant ALL = uint256(-1);

    // ============ Functions ============

    function all() internal pure returns (uint256) {
        return ALL;
    }

    // ========================= Operation Functions =========================

    function deposit(
        IDolomiteMargin _dolomiteMargin,
        address _accountOwner,
        address _fromAccount,
        uint256 _toAccountNumber,
        uint256 _marketId,
        Types.AssetAmount memory _amount
    ) internal {
        Account.Info[] memory accounts = new Account.Info[](1);
        accounts[0] = Account.Info({
            owner: _accountOwner,
            number: _toAccountNumber
        });

        Actions.ActionArgs[] memory actions = new Actions.ActionArgs[](1);
        actions[0] = encodeDepositAction(
            /* _accountId = */ 0, // solium-disable-line indentation
            _marketId,
            _amount,
            _fromAccount
        );

        _dolomiteMargin.operate(accounts, actions);
    }

    /**
     *  Withdraws `_marketId` from `_fromAccount` to `_toAccount`
     */
    function withdraw(
        IDolomiteMargin _dolomiteMargin,
        address _accountOwner,
        uint256 _fromAccountNumber,
        address _toAccount,
        uint256 _marketId,
        Types.AssetAmount memory _amount,
        AccountBalanceLib.BalanceCheckFlag _balanceCheckFlag
    ) internal {
        Account.Info[] memory accounts = new Account.Info[](1);
        accounts[0] = Account.Info({
            owner: _accountOwner,
            number: _fromAccountNumber
        });

        Actions.ActionArgs[] memory actions = new Actions.ActionArgs[](1);
        actions[0] = encodeWithdrawalAction(
            /* _accountId = */ 0, // solium-disable-line indentation
            _marketId,
            _amount,
            _toAccount
        );

        _dolomiteMargin.operate(accounts, actions);

        if (
            _balanceCheckFlag == AccountBalanceLib.BalanceCheckFlag.Both
            || _balanceCheckFlag == AccountBalanceLib.BalanceCheckFlag.From
        ) {
            AccountBalanceLib.verifyBalanceIsNonNegative(
                _dolomiteMargin,
                accounts[0].owner,
                _fromAccountNumber,
                _marketId
            );
        }
    }

    /**
     * Transfers `_marketId` from `_fromAccount` to `_toAccount`
     */
    function transfer(
        IDolomiteMargin _dolomiteMargin,
        address _fromAccountOwner,
        uint256 _fromAccountNumber,
        address _toAccountOwner,
        uint256 _toAccountNumber,
        uint256 _marketId,
        Types.AssetAmount memory _amount,
        AccountBalanceLib.BalanceCheckFlag _balanceCheckFlag
    ) internal {
        Account.Info[] memory accounts = new Account.Info[](2);
        accounts[0] = Account.Info({
            owner: _fromAccountOwner,
            number: _fromAccountNumber
        });
        accounts[1] = Account.Info({
            owner: _toAccountOwner,
            number: _toAccountNumber
        });

        Actions.ActionArgs[] memory actions = new Actions.ActionArgs[](1);
        actions[0] = Actions.ActionArgs({
            actionType: Actions.ActionType.Transfer,
            accountId: 0,
            amount: _amount,
            primaryMarketId: _marketId,
            secondaryMarketId: 0,
            otherAddress: address(0),
            otherAccountId: 1,
            data: bytes("")
        });

        _dolomiteMargin.operate(accounts, actions);

        if (
            _balanceCheckFlag == AccountBalanceLib.BalanceCheckFlag.Both
            || _balanceCheckFlag == AccountBalanceLib.BalanceCheckFlag.From
        ) {
            AccountBalanceLib.verifyBalanceIsNonNegative(
                _dolomiteMargin,
                _fromAccountOwner,
                _fromAccountNumber,
                _marketId
            );
        }

        if (
            _balanceCheckFlag == AccountBalanceLib.BalanceCheckFlag.Both
            || _balanceCheckFlag == AccountBalanceLib.BalanceCheckFlag.To
        ) {
            AccountBalanceLib.verifyBalanceIsNonNegative(
                _dolomiteMargin,
                _toAccountOwner,
                _toAccountNumber,
                _marketId
            );
        }
    }

    // ========================= Encoding Functions =========================

    function encodeCallAction(
        uint256 _accountId,
        address _callee,
        bytes memory _callData
    ) internal pure returns (Actions.ActionArgs memory) {
        return Actions.ActionArgs({
            actionType : Actions.ActionType.Call,
            accountId : _accountId,
            // solium-disable-next-line arg-overflow
            amount : Types.AssetAmount(true, Types.AssetDenomination.Wei, Types.AssetReference.Delta, 0),
            primaryMarketId : 0,
            secondaryMarketId : 0,
            otherAddress : _callee,
            otherAccountId : 0,
            data : _callData
        });
    }

    function encodeDepositAction(
        uint256 _accountId,
        uint256 _marketId,
        Types.AssetAmount memory _amount,
        address _fromAccount
    ) internal pure returns (Actions.ActionArgs memory) {
        return Actions.ActionArgs({
            actionType: Actions.ActionType.Deposit,
            accountId: _accountId,
            amount: _amount,
            primaryMarketId: _marketId,
            secondaryMarketId: 0,
            otherAddress: _fromAccount,
            otherAccountId: 0,
            data: bytes("")
        });
    }

    function encodeExpirationAction(
        Account.Info memory _account,
        uint256 _accountId,
        uint256 _owedMarketId,
        address _expiry,
        uint256 _expiryTimeDelta
    ) internal pure returns (Actions.ActionArgs memory) {
        Require.that(
            _expiryTimeDelta == uint32(_expiryTimeDelta),
            FILE,
            "invalid expiry time"
        );

        IExpiry.SetExpiryArg[] memory expiryArgs = new IExpiry.SetExpiryArg[](1);
        expiryArgs[0] = IExpiry.SetExpiryArg({
            account : _account,
            marketId : _owedMarketId,
            timeDelta : uint32(_expiryTimeDelta),
            forceUpdate : true
        });

        return encodeCallAction(
            _accountId,
            _expiry,
            abi.encode(IExpiry.CallFunctionType.SetExpiry, expiryArgs)
        );
    }

    function encodeExpiryLiquidateAction(
        uint256 _solidAccountId,
        uint256 _liquidAccountId,
        uint256 _owedMarketId,
        uint256 _heldMarketId,
        address _expiryProxy,
        uint32 _expiry,
        uint256 _solidHeldUpdateWithReward,
        uint256 _owedWeiToLiquidate,
        bool _flipMarkets
    ) internal pure returns (Actions.ActionArgs memory) {
        Types.AssetAmount memory assetAmount;
        if (!_flipMarkets) {
            // Make the amount positive so the liquid account's owedMarket goes up (gets repaid).
            assetAmount = Types.AssetAmount({
                sign: true,
                denomination: Types.AssetDenomination.Wei,
                ref: Types.AssetReference.Delta,
                value: _owedWeiToLiquidate
            });
        } else {
            assert(_flipMarkets);
            // Make the amount negative so the liquid account's heldMarket goes down (gets spent to repay owedMarket).
            assetAmount = Types.AssetAmount({
                sign: false,
                denomination: Types.AssetDenomination.Wei,
                ref: Types.AssetReference.Delta,
                value: _solidHeldUpdateWithReward
            });
        }

        return Actions.ActionArgs({
            actionType: Actions.ActionType.Trade,
            accountId: _solidAccountId,
            amount: assetAmount,
            primaryMarketId: !_flipMarkets ? _owedMarketId : _heldMarketId,
            secondaryMarketId: !_flipMarkets ? _heldMarketId : _owedMarketId,
            otherAddress: _expiryProxy,
            otherAccountId: _liquidAccountId,
            data: abi.encode(_owedMarketId, _expiry)
        });
    }

    function encodeInternalTradeAction(
        uint256 _fromAccountId,
        uint256 _toAccountId,
        uint256 _primaryMarketId,
        uint256 _secondaryMarketId,
        address _traderAddress,
        uint256 _amountInWei,
        uint256 _amountOutMinWei
    ) internal pure returns (Actions.ActionArgs memory) {
        return Actions.ActionArgs({
            actionType : Actions.ActionType.Trade,
            accountId : _fromAccountId,
            // solium-disable-next-line arg-overflow
            amount : Types.AssetAmount(true, Types.AssetDenomination.Wei, Types.AssetReference.Delta, _amountInWei),
            primaryMarketId : _primaryMarketId,
            secondaryMarketId : _secondaryMarketId,
            otherAddress : _traderAddress,
            otherAccountId : _toAccountId,
            data : abi.encode(_amountOutMinWei)
        });
    }

    function encodeInternalTradeActionWithCustomData(
        uint256 _fromAccountId,
        uint256 _toAccountId,
        uint256 _primaryMarketId,
        uint256 _secondaryMarketId,
        address _traderAddress,
        uint256 _amountInWei,
        bytes memory _orderData
    ) internal pure returns (Actions.ActionArgs memory) {
        return Actions.ActionArgs({
            actionType : Actions.ActionType.Trade,
            accountId : _fromAccountId,
            // solium-disable-next-line arg-overflow
            amount : Types.AssetAmount(true, Types.AssetDenomination.Wei, Types.AssetReference.Delta, _amountInWei),
            primaryMarketId : _primaryMarketId,
            secondaryMarketId : _secondaryMarketId,
            otherAddress : _traderAddress,
            otherAccountId : _toAccountId,
            data : _orderData
        });
    }

    function encodeLiquidateAction(
        uint256 _solidAccountId,
        uint256 _liquidAccountId,
        uint256 _owedMarketId,
        uint256 _heldMarketId,
        uint256 _owedWeiToLiquidate
    ) internal pure returns (Actions.ActionArgs memory) {
        return Actions.ActionArgs({
            actionType: Actions.ActionType.Liquidate,
            accountId: _solidAccountId,
            amount: Types.AssetAmount({
                sign: true,
                denomination: Types.AssetDenomination.Wei,
                ref: Types.AssetReference.Delta,
                value: _owedWeiToLiquidate
            }),
            primaryMarketId: _owedMarketId,
            secondaryMarketId: _heldMarketId,
            otherAddress: address(0),
            otherAccountId: _liquidAccountId,
            data: new bytes(0)
        });
    }

    function encodeExternalSellAction(
        uint256 _fromAccountId,
        uint256 _primaryMarketId,
        uint256 _secondaryMarketId,
        address _trader,
        uint256 _amountInWei,
        uint256 _amountOutMinWei,
        bytes memory _orderData
    ) internal pure returns (Actions.ActionArgs memory) {
        return Actions.ActionArgs({
            actionType : Actions.ActionType.Sell,
            accountId : _fromAccountId,
            // solium-disable-next-line arg-overflow
            amount : Types.AssetAmount({
                sign : false,
                denomination : Types.AssetDenomination.Wei,
                ref : _amountInWei == ALL ? Types.AssetReference.Target : Types.AssetReference.Delta,
                value : _amountInWei == ALL ? 0 : _amountInWei
            }),
            primaryMarketId : _primaryMarketId,
            secondaryMarketId : _secondaryMarketId,
            otherAddress : _trader,
            otherAccountId : 0,
            data : abi.encode(_amountOutMinWei, _orderData)
        });
    }

    function encodeTransferAction(
        uint256 _fromAccountId,
        uint256 _toAccountId,
        uint256 _marketId,
        uint256 _amountWei
    ) internal pure returns (Actions.ActionArgs memory) {
        Types.AssetAmount memory assetAmount;
        if (_amountWei == uint(- 1)) {
            assetAmount = Types.AssetAmount(
                true,
                Types.AssetDenomination.Wei,
                Types.AssetReference.Target,
                0
            );
        } else {
            assetAmount = Types.AssetAmount(
                false,
                Types.AssetDenomination.Wei,
                Types.AssetReference.Delta,
                _amountWei
            );
        }
        return Actions.ActionArgs({
            actionType : Actions.ActionType.Transfer,
            accountId : _fromAccountId,
            amount : assetAmount,
            primaryMarketId : _marketId,
            secondaryMarketId : 0,
            otherAddress : address(0),
            otherAccountId : _toAccountId,
            data : bytes("")
        });
    }

    function encodeWithdrawalAction(
        uint256 _accountId,
        uint256 _marketId,
        Types.AssetAmount memory _amount,
        address _toAccount
    ) internal pure returns (Actions.ActionArgs memory) {
        return Actions.ActionArgs({
            actionType: Actions.ActionType.Withdraw,
            accountId: _accountId,
            amount: _amount,
            primaryMarketId: _marketId,
            secondaryMarketId: 0,
            otherAddress: _toAccount,
            otherAccountId: 0,
            data: bytes("")
        });
    }
}

File 33 of 43 : IMarginPositionRegistry.sol
/*

    Copyright 2023 Dolomite.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { Events } from "../../protocol/lib/Events.sol";


/**
 * @title IMarginPositionRegistry
 * @author Dolomite
 *
 * An implementation for an upgradeable proxy for emitting margin position-related events. Useful for indexing margin
 * positions from a singular address.
 */
interface IMarginPositionRegistry {

    // ============ Events ============

    /**
     * @notice This is emitted when a margin position is initially opened
     *
     * @param accountOwner          The address of the account that opened the position
     * @param accountNumber         The account number of the account that opened the position
     * @param inputToken            The token that was sold to purchase the collateral. This should be the owed token
     * @param outputToken           The token that was purchased with the debt. This should be the held token
     * @param depositToken          The token that was deposited as collateral. This should be the held token
     * @param inputBalanceUpdate    The amount of inputToken that was sold to purchase the outputToken
     * @param outputBalanceUpdate   The amount of outputToken that was purchased with the inputToken
     * @param marginDepositUpdate   The amount of depositToken that was deposited as collateral
     */
    event MarginPositionOpen(
        address indexed accountOwner,
        uint256 indexed accountNumber,
        address inputToken,
        address outputToken,
        address depositToken,
        Events.BalanceUpdate inputBalanceUpdate,
        Events.BalanceUpdate outputBalanceUpdate,
        Events.BalanceUpdate marginDepositUpdate
    );

    /**
     * @notice This is emitted when a margin position is (partially) closed
     *
     * @param accountOwner              The address of the account that opened the position
     * @param accountNumber             The account number of the account that opened the position
     * @param inputToken                The token that was sold to purchase the debt. This should be the held token
     * @param outputToken               The token that was purchased with the collateral. This should be the owed token
     * @param withdrawalToken           The token that was withdrawn as collateral. This should be the held token
     * @param inputBalanceUpdate        The amount of inputToken that was sold to purchase the outputToken
     * @param outputBalanceUpdate       The amount of outputToken that was purchased with the inputToken
     * @param marginWithdrawalUpdate    The amount of withdrawalToken that was deposited as collateral
     */
    event MarginPositionClose(
        address indexed accountOwner,
        uint256 indexed accountNumber,
        address inputToken,
        address outputToken,
        address withdrawalToken,
        Events.BalanceUpdate inputBalanceUpdate,
        Events.BalanceUpdate outputBalanceUpdate,
        Events.BalanceUpdate marginWithdrawalUpdate
    );

    // ============ Functions ============

    /**
     * @notice Emits a MarginPositionOpen event
     *
     * @param _accountOwner          The address of the account that opened the position
     * @param _accountNumber         The account number of the account that opened the position
     * @param _inputToken            The token that was sold to purchase the collateral. This should be the owed token
     * @param _outputToken           The token that was purchased with the debt. This should be the held token
     * @param _depositToken          The token that was deposited as collateral. This should be the held token
     * @param _inputBalanceUpdate    The amount of inputToken that was sold to purchase the outputToken
     * @param _outputBalanceUpdate   The amount of outputToken that was purchased with the inputToken
     * @param _marginDepositUpdate   The amount of depositToken that was deposited as collateral
     */
    function emitMarginPositionOpen(
        address _accountOwner,
        uint256 _accountNumber,
        address _inputToken,
        address _outputToken,
        address _depositToken,
        Events.BalanceUpdate calldata _inputBalanceUpdate,
        Events.BalanceUpdate calldata _outputBalanceUpdate,
        Events.BalanceUpdate calldata _marginDepositUpdate
    )
    external;

    /**
     * @notice Emits a MarginPositionClose event
     *
     * @param _accountOwner             The address of the account that opened the position
     * @param _accountNumber            The account number of the account that opened the position
     * @param _inputToken               The token that was sold to purchase the debt. This should be the held token
     * @param _outputToken              The token that was purchased with the collateral. This should be the owed token
     * @param _withdrawalToken          The token that was withdrawn as collateral. This should be the held token
     * @param _inputBalanceUpdate       The amount of inputToken that was sold to purchase the outputToken
     * @param _outputBalanceUpdate      The amount of outputToken that was purchased with the inputToken
     * @param _marginWithdrawalUpdate   The amount of withdrawalToken that was deposited as collateral
     */
    function emitMarginPositionClose(
        address _accountOwner,
        uint256 _accountNumber,
        address _inputToken,
        address _outputToken,
        address _withdrawalToken,
        Events.BalanceUpdate calldata _inputBalanceUpdate,
        Events.BalanceUpdate calldata _outputBalanceUpdate,
        Events.BalanceUpdate calldata _marginWithdrawalUpdate
    )
    external;
}

File 34 of 43 : ILiquidatorAssetRegistry.sol
/*

    Copyright 2022 Dolomite.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;


/**
 * @title   ILiquidatorAssetRegistry
 * @author  Dolomite
 *
 * Interface for a registry that tracks which assets can be liquidated and by each contract
 */
interface ILiquidatorAssetRegistry {

    // ============== Events ==============

    event LiquidatorAddedToWhitelist(
        uint256 indexed marketId,
        address indexed liquidator
    );

    event LiquidatorRemovedFromWhitelist(
        uint256 indexed marketId,
        address indexed liquidator
    );

    // ========== Public Functions ==========

    /**
     * @param _marketId     The market ID of the asset
     * @param _liquidator   The address of the liquidator to add
     */
    function ownerAddLiquidatorToAssetWhitelist(
        uint256 _marketId,
        address _liquidator
    )
    external;

    /**
     * @param _marketId     The market ID of the asset
     * @param _liquidator   The address of the liquidator to remove
     */
    function ownerRemoveLiquidatorFromAssetWhitelist(
        uint256 _marketId,
        address _liquidator
    )
    external;

    /**
     * @param _marketId    The market ID of the asset to check
     * @return  An array of whitelisted liquidators for the asset. An empty array is returned if any liquidator can be
     *          used for this asset
     */
    function getLiquidatorsForAsset(
        uint256 _marketId
    )
    external view returns (address[] memory);

    /**
     * @param _marketId     The market ID of the asset to check
     * @param _liquidator   The address of the liquidator to check
     * @return              True if the liquidator is whitelisted for the asset, false otherwise. Returns true if there
     *                      are no whitelisted liquidators for the asset.
     */
    function isAssetWhitelistedForLiquidation(
        uint256 _marketId,
        address _liquidator
    )
    external view returns (bool);
}

File 35 of 43 : IIsolationModeWrapperTrader.sol
/*

    Copyright 2022 Dolomite.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { Actions } from "../../protocol/lib/Actions.sol";

import { IExchangeWrapper } from "../../protocol/interfaces/IExchangeWrapper.sol";


/**
 * @title   IIsolationModeWrapperTrader
 * @author  Dolomite
 *
 * Interface for a contract that can convert a token into an isolation mode token.
 */
contract IIsolationModeWrapperTrader is IExchangeWrapper {

    /**
     * @return The isolation mode token that this contract can wrap (the output token)
     */
    function token() external view returns (address);

    /**
     * @return True if the `_inputToken` is a valid input token for this contract, to be wrapped into `token()`
     */
    function isValidInputToken(address _inputToken) external view returns (bool);

    /**
     * @return  The number of Actions used to wrap a valid input token into the this wrapper's Isolation Mode token.
     */
    function actionsLength() external pure returns (uint256);

    /**
     * @notice  Creates the necessary actions for selling the `_inputMarket` into `_outputMarket`. Note, the
     *          `_outputMarket` should be equal to `token()` and `_inputMarket` should be validated to be a correct
     *           market that can be transformed into `token()`.
     *
     * @param _primaryAccountId     The index of the account (according the Accounts[] array) that is performing the
     *                              sell.
     * @param _otherAccountId       The index of the account (according the Accounts[] array) that is being liquidated.
     *                              This is set to `_primaryAccountId` if a liquidation is not occurring.
     * @param _primaryAccountOwner  The address of the owner of the account that is performing the sell.
     * @param _otherAccountOwner    The address of the owner of the account that is being liquidated. This is set to
     *                              `_primaryAccountOwner` if a liquidation is not occurring.
     * @param _outputMarket         The market that is being outputted by the wrapping, should be equal to `token().
     * @param _inputMarket          The market that is being used to wrap into `token()`.
     * @param _minOutputAmount      The min amount of `_outputMarket` that must be outputted by the wrapping.
     * @param _inputAmount          The amount of the `_inputMarket` that the _primaryAccountId must sell.
     * @param _orderData            The calldata to pass through to any external sales that occur.
     * @return                      The actions that will be executed to wrap the `_inputMarket` into `_outputMarket`.
     */
    function createActionsForWrapping(
        uint256 _primaryAccountId,
        uint256 _otherAccountId,
        address _primaryAccountOwner,
        address _otherAccountOwner,
        uint256 _outputMarket,
        uint256 _inputMarket,
        uint256 _minOutputAmount,
        uint256 _inputAmount,
        bytes calldata _orderData
    )
        external
        view
        returns (Actions.ActionArgs[] memory);
}

File 36 of 43 : IIsolationModeUnwrapperTrader.sol
/*

    Copyright 2022 Dolomite.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { Actions } from "../../protocol/lib/Actions.sol";

import { IExchangeWrapper } from "../../protocol/interfaces/IExchangeWrapper.sol";


/**
 * @title   IIsolationModeUnwrapperTrader
 * @author  Dolomite
 *
 * Interface for a contract that can convert an isolation mode token into an underlying component token.
 */
contract IIsolationModeUnwrapperTrader is IExchangeWrapper {

    /**
     * @return The isolation mode token that this contract can unwrap (the input token).
     */
    function token() external view returns (address);

    /**
     * @return True if the `_outputToken` is a valid output token for this contract, to be unwrapped by `token()`.
     */
    function isValidOutputToken(address _outputToken) external view returns (bool);

    /**
     * @return  The number of Actions used to unwrap the liquidity token.
     */
    function actionsLength() external pure returns (uint256);

    /**
     * @notice  Creates the necessary actions for selling the `_inputMarket` into `_outputMarket`. Note, the
     *          `_inputMarket` should be equal to `token()` and `_outputMarket` should be validated to be a correct
     *           market that can be transformed into `token()`.
     *
     * @param _primaryAccountId     The index of the account (according the Accounts[] array) that is performing the
     *                              sell.
     * @param _otherAccountId       The index of the account (according the Accounts[] array) that is being liquidated.
     *                              This is set to `_primaryAccountId` if a liquidation is not occurring.
     * @param _primaryAccountOwner  The address of the owner of the account that is performing the sell.
     * @param _otherAccountOwner    The address of the owner of the account that is being liquidated. This is set to
     *                              `_primaryAccountOwner` if a liquidation is not occurring.
     * @param _outputMarket         The market that is being outputted by the unwrapping.
     * @param _inputMarket          The market that is being unwrapped, should be equal to `token()`.
     * @param _minOutputAmount      The min amount of `_outputMarket` that must be outputted by the unwrapping.
     * @param _inputAmount          The amount of the `_inputMarket` that the _primaryAccountId must sell.
     * @param _orderData            The calldata to pass through to any external sales that occur.
     * @return                      The actions that will be executed to unwrap the `_inputMarket` into `_outputMarket`.
     */
    function createActionsForUnwrapping(
        uint256 _primaryAccountId,
        uint256 _otherAccountId,
        address _primaryAccountOwner,
        address _otherAccountOwner,
        uint256 _outputMarket,
        uint256 _inputMarket,
        uint256 _minOutputAmount,
        uint256 _inputAmount,
        bytes calldata _orderData
    )
        external
        view
        returns (Actions.ActionArgs[] memory);
}

File 37 of 43 : IIsolationModeToken.sol
/*

    Copyright 2023 Dolomite.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;


/**
 * @title   IIsolationModeToken
 * @author  Dolomite
 *
 * @notice  Interface for an `IsolationMode` token (implemented in Modules repository:
 *          https://github.com/dolomite-exchange/dolomite-margin-modules)
 */
interface IIsolationModeToken {

    // ========== Public Functions ==========

    /**
     * @notice  A token converter is used to convert this underlying token into a Dolomite-compatible one for deposit
     *          or withdrawal or vice-versa. Token converters are trusted contracts that are whitelisted by Dolomite,
     *          malicious ones would be able to mess with the accounting or misappropriate a user's funds in their proxy
     *          vault. Token converters can come in the form of "wrappers" or "unwrappers"
     *
     * @return  True if the token converter is currently enabled for use by this contract.
     */
    function isTokenConverterTrusted(address _tokenConverter) external view returns (bool);

    /**
     * @return True if the token is an isolation mode asset, false otherwise.
     */
    function isIsolationAsset() external view returns (bool);
}

File 38 of 43 : IGenericTraderProxyBase.sol
/*

    Copyright 2023 Dolomite.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { IDolomiteMargin } from "../../protocol/interfaces/IDolomiteMargin.sol";

import { Types } from "../../protocol/lib/Types.sol";


/**
 * @title IGenericTraderProxyBase
 * @author Dolomite
 *
 * Base contract interface for implementing a generic trader proxy for trading assets using any trader from msg.sender
 */
interface IGenericTraderProxyBase {

    // ============ Enums ============

    enum TraderType {
        /// @dev    The trade will be conducted using external liquidity, using an `ActionType.Sell` or `ActionType.Buy`
        ///         action.
        ExternalLiquidity,
        /// @dev    The trade will be conducted using internal liquidity, using an `ActionType.Trade` action.
        InternalLiquidity,
        /// @dev    The trade will be conducted using external liquidity using an `ActionType.Sell` or `ActionType.Buy`
        ///         action. If this TradeType is used, the trader must be validated using
        ///         the `IIsolationModeToken#isTokenConverterTrusted` function on the IsolationMode token.
        IsolationModeUnwrapper,
        /// @dev    The trade will be conducted using external liquidity using an `ActionType.Sell` or `ActionType.Buy`
        ///         action. If this TradeType is used, the trader must be validated using
        ///         the `IIsolationModeToken#isTokenConverterTrusted` function on the IsolationMode token.
        IsolationModeWrapper
    }

    // ============ Structs ============

    struct TraderParam {
        /// @dev The type of trade to conduct
        TraderType traderType;
        /// @dev    The index into the `_makerAccounts` array of the maker account to trade with. Should be set to 0 if
        ///         the traderType is not `TraderType.InternalLiquidity`.
        uint256 makerAccountIndex;
        /// @dev The address of IAutoTrader or IExchangeWrapper that will be used to conduct the trade.
        address trader;
        /// @dev The data that will be passed through to the trader contract.
        bytes tradeData;
    }

    struct GenericTraderProxyCache {
        IDolomiteMargin dolomiteMargin;
        /// @dev    True if the user is making a margin deposit, false if they are withdrawing. False if the variable is
        ///         unused too.
        bool isMarginDeposit;
        /// @dev    The other account number that is not `_traderAccountNumber`. Only used for TransferCollateralParams.
        uint256 otherAccountNumber;
        /// @dev    The index into the account array at which traders start.
        uint256 traderAccountStartIndex;
        /// @dev    The cursor for the looping through the operation's actions.
        uint256 actionsCursor;
        /// @dev    The balance of `inputMarket` that the trader has before the call to `dolomiteMargin.operate`
        Types.Wei inputBalanceWeiBeforeOperate;
        /// @dev    The balance of `outputMarket` that the trader has before the call to `dolomiteMargin.operate`
        Types.Wei outputBalanceWeiBeforeOperate;
        /// @dev    The balance of `transferMarket` that the trader has before the call to `dolomiteMargin.operate`
        Types.Wei transferBalanceWeiBeforeOperate;
    }
}

File 39 of 43 : IExpiry.sol
/*

    Copyright 2021 Dolomite.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { Account } from "../../protocol/lib/Account.sol";
import { Monetary } from "../../protocol/lib/Monetary.sol";


/**
 * @title IExpiry
 * @author Dolomite
 */
contract IExpiry {

    // ============ Enums ============

    enum CallFunctionType {
        SetExpiry,
        SetApproval
    }

    // ============ Structs ============

    struct SetExpiryArg {
        Account.Info account;
        uint256 marketId;
        uint32 timeDelta;
        bool forceUpdate;
    }

    struct SetApprovalArg {
        address sender;
        uint32 minTimeDelta;
    }

    function getSpreadAdjustedPrices(
        uint256 heldMarketId,
        uint256 owedMarketId,
        uint32 expiry
    )
        public
        view
        returns (Monetary.Price memory heldPrice, Monetary.Price memory owedPriceAdj);

    function getExpiry(
        Account.Info memory account,
        uint256 marketId
    )
        public
        view
        returns (uint32);

}

File 40 of 43 : OnlyDolomiteMargin.sol
/*

    Copyright 2022 Dolomite.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { IDolomiteMargin } from "../../protocol/interfaces/IDolomiteMargin.sol";
import { Require } from "../../protocol/lib/Require.sol";


/**
 * @title OnlyDolomiteMargin
 * @author Dolomite
 *
 * Inheritable contract that restricts the calling of certain functions to DolomiteMargin only
 */
contract OnlyDolomiteMargin {

    // ============ Constants ============

    bytes32 private constant FILE = "OnlyDolomiteMargin";

    // ============ Storage ============

    IDolomiteMargin public DOLOMITE_MARGIN;

    // ============ Constructor ============

    constructor (
        address _dolomiteMargin
    )
        public
    {
        DOLOMITE_MARGIN = IDolomiteMargin(_dolomiteMargin);
    }

    // ============ Modifiers ============

    modifier onlyDolomiteMargin(address _from) {
        Require.that(
            _from == address(DOLOMITE_MARGIN),
            FILE,
            "Only Dolomite can call function",
            _from
        );
        _;
    }

    modifier onlyDolomiteMarginOwner(address _from) {
        Require.that(
            _from == DOLOMITE_MARGIN.owner(),
            FILE,
            "Only Dolomite owner can call",
            _from
        );
        _;
    }

    modifier onlyGlobalOperator(address _from) {
        Require.that(
            DOLOMITE_MARGIN.getIsGlobalOperator(_from),
            FILE,
            "Only global operator can call",
            _from
        );
        _;
    }
}

File 41 of 43 : LiquidatorProxyBase.sol
/*

    Copyright 2022 Dolomite.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol";

import { IDolomiteMargin } from "../../protocol/interfaces/IDolomiteMargin.sol";
import { IExpiry } from "../interfaces/IExpiry.sol";
import { ILiquidatorAssetRegistry } from "../interfaces/ILiquidatorAssetRegistry.sol";

import { Account } from "../../protocol/lib/Account.sol";
import { Bits } from "../../protocol/lib/Bits.sol";
import { Decimal } from "../../protocol/lib/Decimal.sol";
import { DolomiteMarginMath } from "../../protocol/lib/DolomiteMarginMath.sol";
import { Interest } from "../../protocol/lib/Interest.sol";
import { Monetary } from "../../protocol/lib/Monetary.sol";
import { Require } from "../../protocol/lib/Require.sol";
import { Time } from "../../protocol/lib/Time.sol";
import { Types } from "../../protocol/lib/Types.sol";

import { HasLiquidatorRegistry } from "./HasLiquidatorRegistry.sol";


/**
 * @title LiquidatorProxyBase
 * @author Dolomite
 *
 * Inheritable contract that allows sharing code across different liquidator proxy contracts
 */
contract LiquidatorProxyBase is HasLiquidatorRegistry {
    using SafeMath for uint;
    using Types for Types.Par;

    // ============ Constants ============

    bytes32 private constant FILE = "LiquidatorProxyBase";
    uint256 private constant MAX_UINT_BITS = 256;
    uint256 private constant ONE = 1;

    // ============ Structs ============

    struct MarketInfo {
        uint256 marketId;
        Monetary.Price price;
        Interest.Index index;
    }

    // ============ Structs ============

    struct LiquidatorProxyConstants {
        IDolomiteMargin dolomiteMargin;
        Account.Info solidAccount;
        Account.Info liquidAccount;
        uint256 heldMarket;
        uint256 owedMarket;
        MarketInfo[] markets;
        uint256[] liquidMarkets;
        IExpiry expiryProxy;
        uint32 expiry;
    }

    struct LiquidatorProxyCache {
        // mutable
        uint256 owedWeiToLiquidate;
        // The amount of heldMarket the solidAccount will receive. Includes the liquidation reward. Useful as the
        // `amountIn` for a trade
        uint256 solidHeldUpdateWithReward;
        Types.Wei solidHeldWei;
        Types.Wei solidOwedWei;
        Types.Wei liquidHeldWei;
        Types.Wei liquidOwedWei;
        // This exists purely for expirations. If the amount being repaid is meant to be ALL but the value of the debt
        // is greater than the value of the collateral, then we need to flip the markets in the trade for the Target=0
        // encoding of the Amount. There's a rounding issue otherwise because amounts are calculated differently for
        // trades vs. liquidations
        bool flipMarketsForExpiration;

        // immutable
        uint256 heldPrice;
        uint256 owedPrice;
        uint256 owedPriceAdj;
    }

    // ============ Internal Functions ============

    modifier requireIsAssetWhitelistedForLiquidation(uint256 _marketId) {
        _validateAssetForLiquidation(_marketId);
        _;
    }

    modifier requireIsAssetsWhitelistedForLiquidation(uint256[] memory _marketIds) {
        _validateAssetsForLiquidation(_marketIds);
        _;
    }

    // ============ Internal Functions ============

    function _validateAssetForLiquidation(uint256 _marketId) internal view {
        Require.that(
            LIQUIDATOR_ASSET_REGISTRY.isAssetWhitelistedForLiquidation(_marketId, address(this)),
            FILE,
            "Asset not whitelisted",
            _marketId
        );
    }

    function _validateAssetsForLiquidation(uint256[] memory _marketIds) internal view {
        ILiquidatorAssetRegistry liquidatorAssetRegistry = LIQUIDATOR_ASSET_REGISTRY;
        for (uint256 i = 0; i < _marketIds.length; i++) {
            Require.that(
                liquidatorAssetRegistry.isAssetWhitelistedForLiquidation(_marketIds[i], address(this)),
                FILE,
                "Asset not whitelisted",
                _marketIds[i]
            );
        }
    }

    /**
     * Pre-populates cache values for some pair of markets.
     */
    function _initializeCache(
        LiquidatorProxyConstants memory _constants
    )
    internal
    view
    returns (LiquidatorProxyCache memory)
    {
        MarketInfo memory heldMarketInfo = _binarySearch(_constants.markets, _constants.heldMarket);
        MarketInfo memory owedMarketInfo = _binarySearch(_constants.markets, _constants.owedMarket);

        uint256 owedPriceAdj;
        if (_constants.expiry > 0) {
            (, Monetary.Price memory owedPricePrice) = _constants.expiryProxy.getSpreadAdjustedPrices(
                _constants.heldMarket,
                _constants.owedMarket,
                _constants.expiry
            );
            owedPriceAdj = owedPricePrice.value;
        } else {
            Decimal.D256 memory spread = _constants.dolomiteMargin.getLiquidationSpreadForPair(
                _constants.heldMarket,
                _constants.owedMarket
            );
            owedPriceAdj = owedMarketInfo.price.value.add(Decimal.mul(owedMarketInfo.price.value, spread));
        }

        return LiquidatorProxyCache({
            owedWeiToLiquidate: 0,
            solidHeldUpdateWithReward: 0,
            solidHeldWei: Interest.parToWei(
                _constants.dolomiteMargin.getAccountPar(_constants.solidAccount, _constants.heldMarket),
                heldMarketInfo.index
            ),
            solidOwedWei: Interest.parToWei(
                _constants.dolomiteMargin.getAccountPar(_constants.solidAccount, _constants.owedMarket),
                owedMarketInfo.index
            ),
            liquidHeldWei: Interest.parToWei(
                _constants.dolomiteMargin.getAccountPar(_constants.liquidAccount, _constants.heldMarket),
                heldMarketInfo.index
            ),
            liquidOwedWei: Interest.parToWei(
                _constants.dolomiteMargin.getAccountPar(_constants.liquidAccount, _constants.owedMarket),
                owedMarketInfo.index
            ),
            flipMarketsForExpiration: false,
            heldPrice: heldMarketInfo.price.value,
            owedPrice: owedMarketInfo.price.value,
            owedPriceAdj: owedPriceAdj
        });
    }

    /**
     * Make some basic checks before attempting to liquidate an account.
     *  - Require that the msg.sender has the permission to use the liquidator account
     *  - Require that the liquid account is liquidatable based on the accounts global value (all assets held and owed,
     *    not just what's being liquidated)
     */
    function _checkConstants(
        LiquidatorProxyConstants memory _constants,
        uint256 _expiry
    )
    internal
    view
    {
        // panic if the developer didn't set these variables already
        assert(address(_constants.dolomiteMargin) != address(0));
        assert(_constants.solidAccount.owner != address(0));
        assert(_constants.liquidAccount.owner != address(0));

        Require.that(
            _constants.owedMarket != _constants.heldMarket,
            FILE,
            "Owed market equals held market",
            _constants.owedMarket
        );

        Require.that(
            !_constants.dolomiteMargin.getAccountPar(_constants.liquidAccount, _constants.owedMarket).isPositive(),
            FILE,
            "Owed market cannot be positive",
            _constants.owedMarket
        );

        Require.that(
            _constants.dolomiteMargin.getAccountPar(_constants.liquidAccount, _constants.heldMarket).isPositive(),
            FILE,
            "Held market cannot be negative",
            _constants.heldMarket
        );

        Require.that(
            uint32(_expiry) == _expiry,
            FILE,
            "Expiry overflows",
            _expiry
        );

        Require.that(
            _expiry <= Time.currentTime(),
            FILE,
            "Borrow not yet expired",
            _expiry
        );
    }

    /**
     * Make some basic checks before attempting to liquidate an account.
     *  - Require that the msg.sender has the permission to use the solid account
     *  - Require that the liquid account is liquidatable if using an expiry
     */
    function _checkBasicRequirements(
        LiquidatorProxyConstants memory _constants
    )
    internal
    view
    {
        // check credentials for msg.sender
        Require.that(
            _constants.solidAccount.owner == msg.sender
                || _constants.dolomiteMargin.getIsLocalOperator(_constants.solidAccount.owner, msg.sender),
            FILE,
            "Sender not operator",
            msg.sender
        );

        if (_constants.expiry != 0) {
            // check the expiration is valid; to get here we already know constants.expiry != 0
            uint32 expiry = _constants.expiryProxy.getExpiry(_constants.liquidAccount, _constants.owedMarket);
            Require.that(
                expiry == _constants.expiry,
                FILE,
                "Expiry mismatch",
                expiry,
                _constants.expiry
            );
        }
    }

    /**
     * Calculate the maximum amount that can be liquidated on `liquidAccount`
     */
    function _calculateAndSetMaxLiquidationAmount(
        LiquidatorProxyCache memory _cache
    )
        internal
        pure
    {
        uint256 liquidHeldValue = _cache.heldPrice.mul(_cache.liquidHeldWei.value);
        uint256 liquidOwedValue = _cache.owedPriceAdj.mul(_cache.liquidOwedWei.value);
        if (liquidHeldValue < liquidOwedValue) {
            // The held collateral is worth less than the adjusted debt
            _cache.solidHeldUpdateWithReward = _cache.liquidHeldWei.value;
            _cache.owedWeiToLiquidate = DolomiteMarginMath.getPartialRoundUp(
                _cache.liquidHeldWei.value,
                _cache.heldPrice,
                _cache.owedPriceAdj
            );
            _cache.flipMarketsForExpiration = true;
        } else {
            _cache.solidHeldUpdateWithReward = DolomiteMarginMath.getPartial(
                _cache.liquidOwedWei.value,
                _cache.owedPriceAdj,
                _cache.heldPrice
            );
            _cache.owedWeiToLiquidate = _cache.liquidOwedWei.value;
        }
    }

    function _calculateAndSetActualLiquidationAmount(
        uint256[] memory _amountWeisForSellActionsPath,
        LiquidatorProxyCache memory _cache
    )
        internal
        pure
    {
        // at this point, _cache.owedWeiToLiquidate should be the max amount that can be liquidated on the user.
        assert(_cache.owedWeiToLiquidate > 0); // assert it was initialized

        uint256 desiredLiquidationOwedAmount = _amountWeisForSellActionsPath[_amountWeisForSellActionsPath.length - 1];
        if (
            desiredLiquidationOwedAmount < _cache.owedWeiToLiquidate
            && desiredLiquidationOwedAmount.mul(_cache.owedPriceAdj) < _cache.heldPrice.mul(_cache.liquidHeldWei.value)
        ) {
            // The user wants to liquidate less than the max amount, and the held collateral is worth more than the
            // desired debt to liquidate
            _cache.owedWeiToLiquidate = desiredLiquidationOwedAmount;
            _cache.solidHeldUpdateWithReward = DolomiteMarginMath.getPartial(
                desiredLiquidationOwedAmount,
                _cache.owedPriceAdj,
                _cache.heldPrice
            );
        }

        if (_amountWeisForSellActionsPath[0] == uint(-1)) {
            // This is analogous to saying "sell all of the collateral I receive from the liquidation"
            _amountWeisForSellActionsPath[0] = _cache.solidHeldUpdateWithReward;
        }

        if (_amountWeisForSellActionsPath[_amountWeisForSellActionsPath.length - 1] == uint(-1)) {
            // minOutputAmount is equal to the value at `length - 1` of the array. The amount being liquidated should
            // always be covered by the sale of assets if the value was set to uint(-1). Setting the value to uint(-1)
            // is analogous to saying "liquidate all"
            _amountWeisForSellActionsPath[_amountWeisForSellActionsPath.length - 1] = _cache.owedWeiToLiquidate;
        }
    }

    /**
     * Returns true if the supplyValue over-collateralizes the borrowValue by the ratio.
     */
    function _isCollateralized(
        uint256 supplyValue,
        uint256 borrowValue,
        Decimal.D256 memory ratio
    )
    internal
    pure
    returns (bool)
    {
        uint256 requiredMargin = Decimal.mul(borrowValue, ratio);
        return supplyValue >= borrowValue.add(requiredMargin);
    }

    /**
     * Gets the current total supplyValue and borrowValue for some account. Takes into account what
     * the current index will be once updated.
     */
    function _getAccountValues(
        IDolomiteMargin dolomiteMargin,
        MarketInfo[] memory marketInfos,
        Account.Info memory account,
        uint256[] memory marketIds
    )
    internal
    view
    returns (
        Monetary.Value memory,
        Monetary.Value memory
    )
    {
        return _getAccountValues(
            dolomiteMargin,
            marketInfos,
            account,
            marketIds,
            /* adjustForMarginPremiums = */ false // solium-disable-line indentation
        );
    }

    /**
     * Gets the adjusted current total supplyValue and borrowValue for some account. Takes into account what
     * the current index will be once updated and the margin premium.
     */
    function _getAdjustedAccountValues(
        IDolomiteMargin dolomiteMargin,
        MarketInfo[] memory marketInfos,
        Account.Info memory account,
        uint256[] memory marketIds
    )
    internal
    view
    returns (
        Monetary.Value memory,
        Monetary.Value memory
    )
    {
        return _getAccountValues(
            dolomiteMargin,
            marketInfos,
            account,
            marketIds,
            /* adjustForMarginPremiums = */ true // solium-disable-line indentation
        );
    }

    function _getMarketInfos(
        IDolomiteMargin dolomiteMargin,
        uint256[] memory solidMarkets,
        uint256[] memory liquidMarkets
    ) internal view returns (MarketInfo[] memory) {
        uint[] memory marketBitmaps = Bits.createBitmaps(dolomiteMargin.getNumMarkets());
        uint marketsLength = 0;
        marketsLength = _addMarketsToBitmap(solidMarkets, marketBitmaps, marketsLength);
        marketsLength = _addMarketsToBitmap(liquidMarkets, marketBitmaps, marketsLength);

        uint counter = 0;
        MarketInfo[] memory marketInfos = new MarketInfo[](marketsLength);
        for (uint i = 0; i < marketBitmaps.length; i++) {
            uint bitmap = marketBitmaps[i];
            while (bitmap != 0) {
                uint nextSetBit = Bits.getLeastSignificantBit(bitmap);
                uint marketId = Bits.getMarketIdFromBit(i, nextSetBit);

                marketInfos[counter++] = MarketInfo({
                    marketId: marketId,
                    price: dolomiteMargin.getMarketPrice(marketId),
                    index: dolomiteMargin.getMarketCurrentIndex(marketId)
                });

                // unset the set bit
                bitmap = Bits.unsetBit(bitmap, nextSetBit);
            }
            if (counter == marketsLength) {
                break;
            }
        }

        return marketInfos;
    }

    function _binarySearch(
        MarketInfo[] memory markets,
        uint marketId
    ) internal pure returns (MarketInfo memory) {
        return _binarySearch(
            markets,
            0,
            markets.length,
            marketId
        );
    }

    // ============ Private Functions ============

    function _getAccountValues(
        IDolomiteMargin dolomiteMargin,
        MarketInfo[] memory marketInfos,
        Account.Info memory account,
        uint256[] memory marketIds,
        bool adjustForMarginPremiums
    )
    private
    view
    returns (
        Monetary.Value memory,
        Monetary.Value memory
    )
    {
        Monetary.Value memory supplyValue;
        Monetary.Value memory borrowValue;

        for (uint256 i = 0; i < marketIds.length; i++) {
            Types.Par memory par = dolomiteMargin.getAccountPar(account, marketIds[i]);
            MarketInfo memory marketInfo = _binarySearch(marketInfos, marketIds[i]);
            Types.Wei memory userWei = Interest.parToWei(par, marketInfo.index);
            uint256 assetValue = userWei.value.mul(marketInfo.price.value);
            Decimal.D256 memory marginPremium = Decimal.one();
            if (adjustForMarginPremiums) {
                marginPremium = Decimal.onePlus(dolomiteMargin.getMarketMarginPremium(marketIds[i]));
            }
            if (userWei.sign) {
                supplyValue.value = supplyValue.value.add(Decimal.div(assetValue, marginPremium));
            } else {
                borrowValue.value = borrowValue.value.add(Decimal.mul(assetValue, marginPremium));
            }
        }

        return (supplyValue, borrowValue);
    }

    // solium-disable-next-line security/no-assign-params
    function _addMarketsToBitmap(
        uint256[] memory markets,
        uint256[] memory bitmaps,
        uint marketsLength
    ) private pure returns (uint) {
        for (uint i = 0; i < markets.length; i++) {
            if (!Bits.hasBit(bitmaps, markets[i])) {
                Bits.setBit(bitmaps, markets[i]);
                marketsLength += 1;
            }
        }
        return marketsLength;
    }

    function _binarySearch(
        MarketInfo[] memory markets,
        uint beginInclusive,
        uint endExclusive,
        uint marketId
    ) private pure returns (MarketInfo memory) {
        uint len = endExclusive - beginInclusive;
        if (len == 0 || (len == 1 && markets[beginInclusive].marketId != marketId)) {
            revert("LiquidatorProxyBase: Market not found");
        }

        uint mid = beginInclusive + len / 2;
        uint midMarketId = markets[mid].marketId;
        if (marketId < midMarketId) {
            return _binarySearch(
                markets,
                beginInclusive,
                mid,
                marketId
            );
        } else if (marketId > midMarketId) {
            return _binarySearch(
                markets,
                mid + 1,
                endExclusive,
                marketId
            );
        } else {
            return markets[mid];
        }
    }

}

File 42 of 43 : HasLiquidatorRegistry.sol
/*

    Copyright 2022 Dolomite.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;

import { ILiquidatorAssetRegistry } from "../interfaces/ILiquidatorAssetRegistry.sol";


contract HasLiquidatorRegistry {

    // ============ Constants ============

    bytes32 private constant FILE = "HasLiquidatorRegistry";

    // ============ Storage ============

    ILiquidatorAssetRegistry public LIQUIDATOR_ASSET_REGISTRY;

    // ============ Constructors ============

    constructor(
        address _liquidatorAssetRegistry
    )
    public
    {
        LIQUIDATOR_ASSET_REGISTRY = ILiquidatorAssetRegistry(_liquidatorAssetRegistry);
    }

}

File 43 of 43 : GenericTraderProxyBase.sol
/*

    Copyright 2023 Dolomite.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

pragma solidity ^0.5.7;
pragma experimental ABIEncoderV2;

import { ReentrancyGuard } from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";

import { IDolomiteMargin } from "../../protocol/interfaces/IDolomiteMargin.sol";

import { Account } from "../../protocol/lib/Account.sol";
import { Actions } from "../../protocol/lib/Actions.sol";
import { Events } from "../../protocol/lib/Events.sol";
import { ExcessivelySafeCall } from "../../protocol/lib/ExcessivelySafeCall.sol";
import { Require } from "../../protocol/lib/Require.sol";

import { OnlyDolomiteMargin } from "../helpers/OnlyDolomiteMargin.sol";

import { IExpiry } from "../interfaces/IExpiry.sol";
import { IGenericTraderProxyBase } from "../interfaces/IGenericTraderProxyBase.sol";
import { IIsolationModeToken } from "../interfaces/IIsolationModeToken.sol";
import { ILiquidatorAssetRegistry } from "../interfaces/ILiquidatorAssetRegistry.sol";
import { IIsolationModeUnwrapperTrader } from "../interfaces/IIsolationModeUnwrapperTrader.sol";
import { IIsolationModeWrapperTrader } from "../interfaces/IIsolationModeWrapperTrader.sol";
import { IMarginPositionRegistry } from "../interfaces/IMarginPositionRegistry.sol";

import { AccountActionLib } from "../lib/AccountActionLib.sol";


/**
 * @title   GenericTraderProxyBase
 * @author  Dolomite
 *
 * @dev Base contract with validation and utilities for trading any asset from an account
 */
contract GenericTraderProxyBase is IGenericTraderProxyBase {

    // ============ Constants ============

    bytes32 private constant FILE = "GenericTraderProxyBase";

    /// @dev The index of the trade account in the accounts array (for executing an operation)
    uint256 private constant TRADE_ACCOUNT_INDEX = 0;

    // ============ Internal Functions ============

    function _validateMarketIdPath(
        uint256[] memory _marketIdsPath
    ) internal pure {
        Require.that(
            _marketIdsPath.length >= 2,
            FILE,
            "Invalid market path length"
        );

        Require.that(
            _marketIdsPath[0] != _marketIdsPath[_marketIdsPath.length - 1],
            FILE,
            "Duplicate markets in path"
        );
    }

    function _validateAmountWeisPath(
        uint256[] memory _marketIdsPath,
        uint256[] memory _amountWeisPath
    )
        internal
        pure
    {
        Require.that(
            _marketIdsPath.length == _amountWeisPath.length,
            FILE,
            "Invalid amounts path length"
        );

        for (uint256 i = 0; i < _amountWeisPath.length; i++) {
            Require.that(
                _amountWeisPath[i] > 0,
                FILE,
                "Invalid amount at index",
                i
            );
        }
    }

    function _validateTraderParams(
        GenericTraderProxyCache memory _cache,
        uint256[] memory _marketIdsPath,
        Account.Info[] memory _makerAccounts,
        TraderParam[] memory _traderParamsPath
    )
        internal
        view
    {
        Require.that(
            _marketIdsPath.length == _traderParamsPath.length + 1,
            FILE,
            "Invalid traders params length"
        );

        for (uint256 i = 0; i < _traderParamsPath.length; i++) {
            _validateTraderParam(
                _cache,
                _marketIdsPath,
                _makerAccounts,
                _traderParamsPath[i],
                /* _index = */ i // solium-disable-line indentation
            );
        }
    }

    function _validateTraderParam(
        GenericTraderProxyCache memory _cache,
        uint256[] memory _marketIdsPath,
        Account.Info[] memory _makerAccounts,
        TraderParam memory _traderParam,
        uint256 _index
    )
        internal
        view
    {
        Require.that(
            _traderParam.trader != address(0),
            FILE,
            "Invalid trader at index",
            _index
        );

        uint256 marketId = _marketIdsPath[_index];
        uint256 nextMarketId = _marketIdsPath[_index + 1];
        _validateIsolationModeStatusForTraderParam(
            _cache,
            marketId,
            nextMarketId,
            _traderParam
        );
        _validateTraderTypeForTraderParam(
            _cache,
            marketId,
            nextMarketId,
            _traderParam,
            _index
        );
        _validateMakerAccountForTraderParam(
            _makerAccounts,
            _traderParam,
            _index
        );
    }

    function _validateIsolationModeStatusForTraderParam(
        GenericTraderProxyCache memory _cache,
        uint256 _marketId,
        uint256 _nextMarketId,
        TraderParam memory _traderParam
    ) internal view {
        if (_isIsolationModeMarket(_cache, _marketId)) {
            // If the current market is in isolation mode, the trader type must be for isolation mode assets
            Require.that(
                _traderParam.traderType == TraderType.IsolationModeUnwrapper,
                FILE,
                "Invalid isolation mode unwrapper",
                _marketId,
                uint256(uint8(_traderParam.traderType))
            );

            if (_isIsolationModeMarket(_cache, _nextMarketId)) {
                // If the user is unwrapping into an isolation mode asset, the next market must trust this trader
                address isolationModeToken = _cache.dolomiteMargin.getMarketTokenAddress(_nextMarketId);
                Require.that(
                    IIsolationModeToken(isolationModeToken).isTokenConverterTrusted(_traderParam.trader),
                    FILE,
                    "Invalid unwrap sequence",
                    _marketId,
                    _nextMarketId
                );
            }
        } else if (_isIsolationModeMarket(_cache, _nextMarketId)) {
            // If the next market is in isolation mode, the trader must wrap the current asset into the isolation asset.
            Require.that(
                _traderParam.traderType == TraderType.IsolationModeWrapper,
                FILE,
                "Invalid isolation mode wrapper",
                _nextMarketId,
                uint256(uint8(_traderParam.traderType))
            );
        } else {
            // If neither asset is in isolation mode, the trader type must be for non-isolation mode assets
            Require.that(
                _traderParam.traderType == TraderType.ExternalLiquidity
                    || _traderParam.traderType == TraderType.InternalLiquidity,
                FILE,
                "Invalid trader type",
                uint256(uint8(_traderParam.traderType))
            );
        }
    }

    function _validateTraderTypeForTraderParam(
        GenericTraderProxyCache memory _cache,
        uint256 _marketId,
        uint256 _nextMarketId,
        TraderParam memory _traderParam,
        uint256 _index
    ) internal view {
        if (TraderType.IsolationModeUnwrapper == _traderParam.traderType) {
            IIsolationModeUnwrapperTrader unwrapperTrader = IIsolationModeUnwrapperTrader(_traderParam.trader);
            address isolationModeToken = _cache.dolomiteMargin.getMarketTokenAddress(_marketId);
            Require.that(
                unwrapperTrader.token() == isolationModeToken,
                FILE,
                "Invalid input for unwrapper",
                _index,
                _marketId
            );
            Require.that(
                unwrapperTrader.isValidOutputToken(_cache.dolomiteMargin.getMarketTokenAddress(_nextMarketId)),
                FILE,
                "Invalid output for unwrapper",
                _index + 1,
                _nextMarketId
            );
            Require.that(
                IIsolationModeToken(isolationModeToken).isTokenConverterTrusted(_traderParam.trader),
                FILE,
                "Unwrapper trader not enabled",
                _traderParam.trader,
                _marketId
            );
        } else if (TraderType.IsolationModeWrapper == _traderParam.traderType) {
            IIsolationModeWrapperTrader wrapperTrader = IIsolationModeWrapperTrader(_traderParam.trader);
            address isolationModeToken = _cache.dolomiteMargin.getMarketTokenAddress(_nextMarketId);
            Require.that(
                wrapperTrader.isValidInputToken(_cache.dolomiteMargin.getMarketTokenAddress(_marketId)),
                FILE,
                "Invalid input for wrapper",
                _index,
                _marketId
            );
            Require.that(
                wrapperTrader.token() == isolationModeToken,
                FILE,
                "Invalid output for wrapper",
                _index + 1,
                _nextMarketId
            );
            Require.that(
                IIsolationModeToken(isolationModeToken).isTokenConverterTrusted(_traderParam.trader),
                FILE,
                "Wrapper trader not enabled",
                _traderParam.trader,
                _nextMarketId
            );
        }
    }

    function _validateMakerAccountForTraderParam(
        Account.Info[] memory _makerAccounts,
        TraderParam memory _traderParam,
        uint256 _index
    ) internal pure {
        if (TraderType.InternalLiquidity == _traderParam.traderType) {
            // The makerAccountOwner should be set if the traderType is InternalLiquidity
            Require.that(
                _traderParam.makerAccountIndex < _makerAccounts.length
                && _makerAccounts[_traderParam.makerAccountIndex].owner != address(0),
                FILE,
                "Invalid maker account owner",
                _index
            );
        } else {
            // The makerAccountOwner and makerAccountNumber is not used if the traderType is not InternalLiquidity
            Require.that(
                _traderParam.makerAccountIndex == 0,
                FILE,
                "Invalid maker account owner",
                _index
            );
        }
    }

    function _getAccounts(
        GenericTraderProxyCache memory _cache,
        Account.Info[] memory _makerAccounts,
        address _tradeAccountOwner,
        uint256 _tradeAccountNumber
    )
        internal
        pure
        returns (Account.Info[] memory)
    {
        Account.Info[] memory accounts = new Account.Info[](_cache.traderAccountStartIndex + _makerAccounts.length);
        accounts[TRADE_ACCOUNT_INDEX] = Account.Info({
            owner: _tradeAccountOwner,
            number: _tradeAccountNumber
        });
        _appendTradersToAccounts(_cache, _makerAccounts, accounts);
        return accounts;
    }

    function _appendTradersToAccounts(
        GenericTraderProxyCache memory _cache,
        Account.Info[] memory _makerAccounts,
        Account.Info[] memory _accounts
    )
        internal
        pure
    {
        for (uint256 i = 0; i < _makerAccounts.length; i++) {
            _accounts[_cache.traderAccountStartIndex + i] = Account.Info({
                owner: _makerAccounts[i].owner,
                number: _makerAccounts[i].number
            });
        }
    }

    function _getActionsLengthForTraderParams(
        TraderParam[] memory _tradersPath
    )
        internal
        pure
        returns (uint256)
    {
        uint256 actionsLength = 0;
        for (uint256 i = 0; i < _tradersPath.length; i++) {
            if (TraderType.IsolationModeUnwrapper == _tradersPath[i].traderType) {
                actionsLength += IIsolationModeUnwrapperTrader(_tradersPath[i].trader).actionsLength();
            } else if (TraderType.IsolationModeWrapper == _tradersPath[i].traderType) {
                actionsLength += IIsolationModeUnwrapperTrader(_tradersPath[i].trader).actionsLength();
            } else {
                actionsLength += 1;
            }
        }
        return actionsLength;
    }

    function _appendTraderActions(
        Account.Info[] memory _accounts,
        Actions.ActionArgs[] memory _actions,
        GenericTraderProxyCache memory _cache,
        uint256[] memory _marketIdsPath,
        uint256[] memory _amountWeisPath,
        TraderParam[] memory _tradersPath
    )
        internal
        view
    {
        for (uint256 i = 0; i < _tradersPath.length; i++) {
            if (_tradersPath[i].traderType == TraderType.ExternalLiquidity) {
                _actions[_cache.actionsCursor++] = AccountActionLib.encodeExternalSellAction(
                    TRADE_ACCOUNT_INDEX,
                    _marketIdsPath[i],
                    _marketIdsPath[i + 1],
                    _tradersPath[i].trader,
                    _amountWeisPath[i],
                    _amountWeisPath[i + 1],
                    _tradersPath[i].tradeData
                );
            } else if (_tradersPath[i].traderType == TraderType.InternalLiquidity) {
                _actions[_cache.actionsCursor++] = AccountActionLib.encodeInternalTradeActionWithCustomData(
                    TRADE_ACCOUNT_INDEX,
                    /* _makerAccountId = */ _tradersPath[i].makerAccountIndex + _cache.traderAccountStartIndex,
                    _marketIdsPath[i],
                    _marketIdsPath[i + 1],
                    _tradersPath[i].trader,
                    _amountWeisPath[i],
                    _tradersPath[i].tradeData
                );
            } else if (_tradersPath[i].traderType == TraderType.IsolationModeUnwrapper) {
                IIsolationModeUnwrapperTrader unwrapperTrader = IIsolationModeUnwrapperTrader(_tradersPath[i].trader);
                Actions.ActionArgs[] memory unwrapperActions = unwrapperTrader.createActionsForUnwrapping(
                    TRADE_ACCOUNT_INDEX,
                    otherAccountIndex(),
                    _accounts[TRADE_ACCOUNT_INDEX].owner,
                    _accounts[otherAccountIndex()].owner,
                    /* _outputMarketId = */_marketIdsPath[i + 1], // solium-disable-line indentation
                    /* _inputMarketId = */ _marketIdsPath[i], // solium-disable-line indentation
                    /* _minOutputAmount = */ _amountWeisPath[i + 1], // solium-disable-line indentation
                    /* _inputAmount = */ _amountWeisPath[i], // solium-disable-line indentation,
                    _tradersPath[i].tradeData
                );
                for (uint256 j = 0; j < unwrapperActions.length; j++) {
                    _actions[_cache.actionsCursor++] = unwrapperActions[j];
                }
            } else {
                // Panic if the developer messed up the `else` statement here
                assert(_tradersPath[i].traderType == TraderType.IsolationModeWrapper);

                IIsolationModeWrapperTrader wrapperTrader = IIsolationModeWrapperTrader(_tradersPath[i].trader);
                Actions.ActionArgs[] memory wrapperActions = wrapperTrader.createActionsForWrapping(
                    TRADE_ACCOUNT_INDEX,
                    otherAccountIndex(),
                    _accounts[TRADE_ACCOUNT_INDEX].owner,
                    _accounts[otherAccountIndex()].owner,
                    /* _outputMarketId = */ _marketIdsPath[i + 1], // solium-disable-line indentation
                    /* _inputMarketId = */ _marketIdsPath[i], // solium-disable-line indentation
                    /* _minOutputAmount = */ _amountWeisPath[i + 1], // solium-disable-line indentation
                    /* _inputAmount = */ _amountWeisPath[i], // solium-disable-line indentation
                    _tradersPath[i].tradeData
                );
                for (uint256 j = 0; j < wrapperActions.length; j++) {
                    _actions[_cache.actionsCursor++] = wrapperActions[j];
                }
            }
        }
    }

    function _isIsolationModeMarket(
        GenericTraderProxyCache memory _cache,
        uint256 _marketId
    ) internal view returns (bool) {
        (bool isSuccess, bytes memory returnData) = ExcessivelySafeCall.safeStaticCall(
            _cache.dolomiteMargin.getMarketTokenAddress(_marketId),
            IIsolationModeToken(address(0)).isIsolationAsset.selector,
            bytes("")
        );
        return isSuccess && abi.decode(returnData, (bool));
    }

    /**
     * @return  The index of the account that is not the trade account. For the liquidation contract, this is
     *          the account being liquidated. For the GenericTrader contract this is the same as the trader account.
     */
    function otherAccountIndex() public pure returns (uint256);
}

Settings
{
  "remappings": [],
  "optimizer": {
    "enabled": true,
    "runs": 10000
  },
  "evmVersion": "istanbul",
  "libraries": {},
  "outputSelection": {
    "*": {
      "*": [
        "evm.bytecode",
        "evm.deployedBytecode",
        "devdoc",
        "userdoc",
        "metadata",
        "abi"
      ]
    }
  }
}

Contract ABI

[{"inputs":[{"internalType":"address","name":"_expiryProxy","type":"address"},{"internalType":"address","name":"_dolomiteMargin","type":"address"},{"internalType":"address","name":"_liquidatorAssetRegistry","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"constant":true,"inputs":[],"name":"DOLOMITE_MARGIN","outputs":[{"internalType":"contract IDolomiteMargin","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"EXPIRY","outputs":[{"internalType":"contract IExpiry","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"LIQUIDATOR_ASSET_REGISTRY","outputs":[{"internalType":"contract ILiquidatorAssetRegistry","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"components":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"uint256","name":"number","type":"uint256"}],"internalType":"struct Account.Info","name":"_solidAccount","type":"tuple"},{"components":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"uint256","name":"number","type":"uint256"}],"internalType":"struct Account.Info","name":"_liquidAccount","type":"tuple"},{"internalType":"uint256[]","name":"_marketIdsPath","type":"uint256[]"},{"internalType":"uint256[]","name":"_amountWeisPath","type":"uint256[]"},{"components":[{"internalType":"enum IGenericTraderProxyBase.TraderType","name":"traderType","type":"uint8"},{"internalType":"uint256","name":"makerAccountIndex","type":"uint256"},{"internalType":"address","name":"trader","type":"address"},{"internalType":"bytes","name":"tradeData","type":"bytes"}],"internalType":"struct IGenericTraderProxyBase.TraderParam[]","name":"_tradersPath","type":"tuple[]"},{"components":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"uint256","name":"number","type":"uint256"}],"internalType":"struct Account.Info[]","name":"_makerAccounts","type":"tuple[]"},{"internalType":"uint256","name":"_expiry","type":"uint256"}],"name":"liquidate","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"otherAccountIndex","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"}]

60806040523480156200001157600080fd5b506040516200529238038062005292833981016040819052620000349162000095565b60008054600160a01b6001600160a01b03199182166001600160a01b039485161760ff60a01b1916179091556001805482169483169490941790935560028054909316911617905562000115565b80516200008f81620000fb565b92915050565b600080600060608486031215620000ab57600080fd5b6000620000b9868662000082565b9350506020620000cc8682870162000082565b9250506040620000df8682870162000082565b9150509250925092565b60006001600160a01b0382166200008f565b6200010681620000e9565b81146200011257600080fd5b50565b61516d80620001256000396000f3fe608060405234801561001057600080fd5b50600436106100675760003560e01c8063380d1bcf11610050578063380d1bcf1461009f5780634530a2d7146100b4578063cf530f88146100bc57610067565b806315c14a4a1461006c578063261140261461008a575b600080fd5b6100746100c4565b6040516100819190614d6b565b60405180910390f35b61009d610098366004614656565b6100d3565b005b6100a76105ac565b6040516100819190614de2565b6100746105b1565b6100746105c0565b6002546001600160a01b031681565b60005474010000000000000000000000000000000000000000900460ff16610130576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161012790614da9565b60405180910390fd5b600080547fffffffffffffffffffffff00ffffffffffffffffffffffffffffffffffffffff169055610160613d4b565b6040805161010081018252600280546001600160a01b031682526000602083018190529282018390526060820152608081019190915260a081016101a26105cf565b81526020016101af6105cf565b81526020016101bc6105cf565b905290506101c9866105ef565b6101d386866106bb565b6101df81878587610785565b61021281876000815181106101f057fe5b60200260200101518760008151811061020557fe5b6020026020010151610812565b61021a613dac565b81516001600160a01b0316815260208101899052604081018890528651879060009061024257fe5b60200260200101518160600181815250508660018851038151811061026357fe5b602002602001015181608001818152505061027e8184610870565b61028b8160600151610b27565b6102988160800151610b27565b805160408083015190517f0f47fab00000000000000000000000000000000000000000000000000000000081526001600160a01b0390921691630f47fab0916102e391600401614db9565b60006040518083038186803b1580156102fb57600080fd5b505afa15801561030f573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f1916820160405261033791908101906145c7565b60c082015280516040517f0f47fab00000000000000000000000000000000000000000000000000000000081526103e691906001600160a01b03821690630f47fab090610388908e90600401614db9565b60006040518083038186803b1580156103a057600080fd5b505afa1580156103b4573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526103dc91908101906145c7565b8360c00151610c0c565b60a0820152826103f7576000610404565b6001546001600160a01b03165b6001600160a01b031660e082015263ffffffff8316610100820152610427613e1c565b61043082610e94565b905061043b82611262565b61044481611473565b61044e878261152d565b606061046484878d600001518e6020015161163c565b9050898160018151811061047457fe5b6020026020010181905250606061048a886116cf565b6001016040519080825280602002602001820160405280156104c657816020015b6104b3613e89565b8152602001906001900390816104ab5790505b5090506104d5818585886117e6565b6104e38282878d8d8d611898565b84516040517fa67a6a450000000000000000000000000000000000000000000000000000000081526001600160a01b039091169063a67a6a459061052d9085908590600401614d35565b600060405180830381600087803b15801561054757600080fd5b505af115801561055b573d6000803e3d6000fd5b5050600080547fffffffffffffffffffffff00ffffffffffffffffffffffffffffffffffffffff16740100000000000000000000000000000000000000001790555050505050505050505050505050565b600190565b6001546001600160a01b031681565b6000546001600160a01b031681565b6105d7613edb565b50604080518082019091526000808252602082015290565b61063f6002825110157f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c6964206d61726b65742070617468206c656e677468000000000000611e07565b6106b88160018351038151811061065257fe5b60200260200101518260008151811061066757fe5b602002602001015114157f47656e6572696354726164657250726f787942617365000000000000000000007f4475706c6963617465206d61726b65747320696e207061746800000000000000611e07565b50565b61070a81518351147f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c696420616d6f756e74732070617468206c656e6774680000000000611e07565b60005b815181101561078057610778600083838151811061072757fe5b6020026020010151117f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c696420616d6f756e7420617420696e64657800000000000000000084611e91565b60010161070d565b505050565b6107d781516001018451147f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c6964207472616465727320706172616d73206c656e677468000000611e07565b60005b815181101561080b576108038585858585815181106107f557fe5b602002602001015185611f29565b6001016107da565b5050505050565b61081c8383611fdc565b156107805761078060001982147f4c697175696461746f7250726f787956340000000000000000000000000000007f496e76616c696420616d6f756e7420666f722049736f6c6174696f6e4d6f6465611e07565b81516001600160a01b031661088157fe5b6020820151516001600160a01b031661089657fe5b6040820151516001600160a01b03166108ab57fe5b6109068260600151836080015114157f4c697175696461746f7250726f787942617365000000000000000000000000007f4f776564206d61726b657420657175616c732068656c64206d61726b657400008560800151611e91565b6109e761099a83600001516001600160a01b03166347d1b53c856040015186608001516040518363ffffffff1660e01b8152600401610946929190614dc7565b604080518083038186803b15801561095d57600080fd5b505afa158015610971573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506109959190810190614754565b6120c5565b157f4c697175696461746f7250726f787942617365000000000000000000000000007f4f776564206d61726b65742063616e6e6f7420626520706f73697469766500008560800151611e91565b610a73610a2783600001516001600160a01b03166347d1b53c856040015186606001516040518363ffffffff1660e01b8152600401610946929190614dc7565b7f4c697175696461746f7250726f787942617365000000000000000000000000007f48656c64206d61726b65742063616e6e6f74206265206e6567617469766500008560600151611e91565b610ac7818263ffffffff16147f4c697175696461746f7250726f787942617365000000000000000000000000007f457870697279206f766572666c6f77730000000000000000000000000000000084611e91565b610b23610ad26120ed565b63ffffffff168211157f4c697175696461746f7250726f787942617365000000000000000000000000007f426f72726f77206e6f742079657420657870697265640000000000000000000084611e91565b5050565b6000546040517fd9f476010000000000000000000000000000000000000000000000000000000081526106b8916001600160a01b03169063d9f4760190610b749085903090600401614df0565b60206040518083038186803b158015610b8c57600080fd5b505afa158015610ba0573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250610bc491908101906145fc565b7f4c697175696461746f7250726f787942617365000000000000000000000000007f4173736574206e6f742077686974656c6973746564000000000000000000000084611e91565b606080610c88856001600160a01b031663295c39a56040518163ffffffff1660e01b815260040160206040518083038186803b158015610c4b57600080fd5b505afa158015610c5f573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250610c8391908101906147ac565b6120fd565b90506000610c97858383612136565b9050610ca4848383612136565b90506000809050606082604051908082528060200260200182016040528015610ce757816020015b610cd4613ef2565b815260200190600190039081610ccc5790505b50905060005b8451811015610e86576000858281518110610d0457fe5b602002602001015190505b8015610e6f576000610d2082612199565b90506000610d2e8483612334565b905060405180606001604052808281526020018d6001600160a01b0316638928378e846040518263ffffffff1660e01b8152600401610d6d9190614de2565b60206040518083038186803b158015610d8557600080fd5b505afa158015610d99573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250610dbd919081019061461a565b81526020018d6001600160a01b03166356ea84b2846040518263ffffffff1660e01b8152600401610dee9190614de2565b60606040518083038186803b158015610e0657600080fd5b505afa158015610e1a573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250610e3e9190810190614638565b90528551600188019787918110610e5157fe5b6020026020010181905250610e66838361233f565b92505050610d0f565b84841415610e7d5750610e86565b50600101610ced565b5093505050505b9392505050565b610e9c613e1c565b610ea4613ef2565b610eb68360a001518460600151612348565b9050610ec0613ef2565b610ed28460a001518560800151612348565b905060008085610100015163ffffffff161115610f8c57610ef1613f19565b8560e001516001600160a01b031663b1396542876060015188608001518961010001516040518463ffffffff1660e01b8152600401610f3293929190614ed2565b604080518083038186803b158015610f4957600080fd5b505afa158015610f5d573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250610f819190810190614772565b519250611063915050565b610f94613f19565b8551606087015160808801516040517fd24c48bc0000000000000000000000000000000000000000000000000000000081526001600160a01b039093169263d24c48bc92610fe6929091600401614e2b565b60206040518083038186803b158015610ffe57600080fd5b505afa158015611012573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250611036919081019061461a565b905061105f61104d8460200151600001518361235e565b6020850151519063ffffffff61237716565b9150505b604051806101400160405280600081526020016000815260200161111387600001516001600160a01b03166347d1b53c89602001518a606001516040518363ffffffff1660e01b81526004016110ba929190614dc7565b604080518083038186803b1580156110d157600080fd5b505afa1580156110e5573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506111099190810190614754565b86604001516123b6565b81526020016111ae87600001516001600160a01b03166347d1b53c89602001518a608001516040518363ffffffff1660e01b8152600401611155929190614dc7565b604080518083038186803b15801561116c57600080fd5b505afa158015611180573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506111a49190810190614754565b85604001516123b6565b81526020016111f087600001516001600160a01b03166347d1b53c89604001518a606001516040518363ffffffff1660e01b81526004016110ba929190614dc7565b815260200161123287600001516001600160a01b03166347d1b53c89604001518a608001516040518363ffffffff1660e01b8152600401611155929190614dc7565b8152600060208083019190915294850151516040820152929093015151606083015260809091015290505b919050565b602081015151611363906001600160a01b031633148061131b575081516020830151516040517f3a031bf00000000000000000000000000000000000000000000000000000000081526001600160a01b0390921691633a031bf0916112cb913390600401614d1a565b60206040518083038186803b1580156112e357600080fd5b505afa1580156112f7573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525061131b91908101906145fc565b7f4c697175696461746f7250726f787942617365000000000000000000000000007f53656e646572206e6f74206f70657261746f720000000000000000000000000033612467565b61010081015163ffffffff16156106b85760008160e001516001600160a01b0316631be7dd83836040015184608001516040518363ffffffff1660e01b81526004016113b0929190614dc7565b60206040518083038186803b1580156113c857600080fd5b505afa1580156113dc573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525061140091908101906147ca565b9050610b2382610100015163ffffffff168263ffffffff16147f4c697175696461746f7250726f787942617365000000000000000000000000007f457870697279206d69736d6174636800000000000000000000000000000000008463ffffffff1686610100015163ffffffff166124c9565b60006114948260800151602001518360e0015161258d90919063ffffffff16565b905060006114b88360a001516020015184610120015161258d90919063ffffffff16565b9050808210156114fa57608083015160209081018051918501919091525160e08401516101208501516114ec9291906125e1565b8352600160c0840152610780565b6115168360a00151602001518461012001518560e00151612646565b60208085019190915260a084015101518352505050565b805161153557fe5b60008260018451038151811061154757fe5b60200260200101519050816000015181108015611595575060808201516020015160e083015161157c9163ffffffff61258d16565b61012083015161159390839063ffffffff61258d16565b105b156115b95780825261012082015160e08301516115b3918391612646565b60208301525b600019836000815181106115c957fe5b602002602001015114156115f6578160200151836000815181106115e957fe5b6020026020010181815250505b6000198360018551038151811061160957fe5b6020026020010151141561078057815183518490600019810190811061162b57fe5b602002602001018181525050505050565b606080845186606001510160405190808252806020026020018201604052801561168057816020015b61166d613edb565b8152602001906001900390816116655790505b5090506040518060400160405280856001600160a01b0316815260200184815250816000815181106116ae57fe5b60200260200101819052506116c486868361265c565b90505b949350505050565b600080805b83518110156117df578381815181106116e957fe5b602002602001015160000151600381111561170057fe5b600214156117995783818151811061171457fe5b6020026020010151604001516001600160a01b031663b189111a6040518163ffffffff1660e01b815260040160206040518083038186803b15801561175857600080fd5b505afa15801561176c573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525061179091908101906147ac565b820191506117d7565b8381815181106117a557fe5b60200260200101516000015160038111156117bc57fe5b600314156117d05783818151811061171457fe5b6001820191505b6001016116d4565b5092915050565b61010083015163ffffffff16156118515761182760006001856080015186606001518760e00151886101000151886020015189600001518a60c001516126d9565b848260800180518091906001018152508151811061184157fe5b6020026020010181905250611892565b61186c600060018560800151866060015186600001516127d6565b848260800180518091906001018152508151811061188657fe5b60200260200101819052505b50505050565b60005b8151811015611dfe5760008282815181106118b257fe5b60200260200101516000015160038111156118c957fe5b14156119895761195f60008583815181106118e057fe5b60200260200101518684600101815181106118f757fe5b602002602001015185858151811061190b57fe5b60200260200101516040015187868151811061192357fe5b602002602001015188876001018151811061193a57fe5b602002602001015188888151811061194e57fe5b602002602001015160600151612855565b868660800180518091906001018152508151811061197957fe5b6020026020010181905250611df6565b600182828151811061199757fe5b60200260200101516000015160038111156119ae57fe5b1415611a4b5761195f600086606001518484815181106119ca57fe5b602002602001015160200151018684815181106119e357fe5b60200260200101518785600101815181106119fa57fe5b6020026020010151868681518110611a0e57fe5b602002602001015160400151888781518110611a2657fe5b6020026020010151888881518110611a3a57fe5b602002602001015160600151612920565b6002828281518110611a5957fe5b6020026020010151600001516003811115611a7057fe5b1415611c22576000828281518110611a8457fe5b60200260200101516040015190506060816001600160a01b031663a8c1a94c6000611aad6105ac565b8c600081518110611aba57fe5b6020026020010151600001518d611acf6105ac565b81518110611ad957fe5b6020026020010151600001518b8960010181518110611af457fe5b60200260200101518c8a81518110611b0857fe5b60200260200101518c8b60010181518110611b1f57fe5b60200260200101518d8c81518110611b3357fe5b60200260200101518d8d81518110611b4757fe5b6020026020010151606001516040518a63ffffffff1660e01b8152600401611b7799989796959493929190614e46565b60006040518083038186803b158015611b8f57600080fd5b505afa158015611ba3573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052611bcb9190810190614592565b905060005b8151811015611c1a57818181518110611be557fe5b60200260200101518989608001805180919060010181525081518110611c0757fe5b6020908102919091010152600101611bd0565b505050611df6565b6003828281518110611c3057fe5b6020026020010151600001516003811115611c4757fe5b14611c4e57fe5b6000828281518110611c5c57fe5b60200260200101516040015190506060816001600160a01b031663f9a92b2b6000611c856105ac565b8c600081518110611c9257fe5b6020026020010151600001518d611ca76105ac565b81518110611cb157fe5b6020026020010151600001518b8960010181518110611ccc57fe5b60200260200101518c8a81518110611ce057fe5b60200260200101518c8b60010181518110611cf757fe5b60200260200101518d8c81518110611d0b57fe5b60200260200101518d8d81518110611d1f57fe5b6020026020010151606001516040518a63ffffffff1660e01b8152600401611d4f99989796959493929190614e46565b60006040518083038186803b158015611d6757600080fd5b505afa158015611d7b573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052611da39190810190614592565b905060005b8151811015611df257818181518110611dbd57fe5b60200260200101518989608001805180919060010181525081518110611ddf57fe5b6020908102919091010152600101611da8565b5050505b60010161189b565b50505050505050565b8261078057611e158261299e565b7f3a20000000000000000000000000000000000000000000000000000000000000611e3f8361299e565b604051602001611e5193929190614c0a565b60408051601f19818403018152908290527f08c379a000000000000000000000000000000000000000000000000000000000825261012791600401614d5a565b8361189257611e9f8361299e565b7f3a20000000000000000000000000000000000000000000000000000000000000611ec98461299e565b7f203c000000000000000000000000000000000000000000000000000000000000611ef385612a3a565b604051611e519594939291907f3e0000000000000000000000000000000000000000000000000000000000000090602001614c32565b6040820151611f85906001600160a01b031615157f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c69642074726164657220617420696e64657800000000000000000084611e91565b6000848281518110611f9357fe5b602002602001015190506000858360010181518110611fae57fe5b60200260200101519050611fc487838387612b2b565b611fd18783838787612e1e565b611dfe85858561359f565b600080606061209a85600001516001600160a01b031663062bd3e9866040518263ffffffff1660e01b81526004016120149190614de2565b60206040518083038186803b15801561202c57600080fd5b505afa158015612040573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506120649190810190614574565b6040805160208101909152600081527fed4e3a350000000000000000000000000000000000000000000000000000000090613699565b915091508180156120ba5750808060200190516120ba91908101906145fc565b925050505b92915050565b805160009080156120bf575050602001516fffffffffffffffffffffffffffffffff16151590565b60006120f842613771565b905090565b606060016101008304016040519080825280602002602001820160405280156117df578160200160208202803883390190505092915050565b6000805b8451811015612190576121608486838151811061215357fe5b60200260200101516137c8565b612188576121818486838151811061217457fe5b6020026020010151613808565b6001830192505b60010161213a565b50909392505050565b600060ff6fffffffffffffffffffffffffffffffff8316156121dc577fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80016121e4565b608083901c92505b67ffffffffffffffff83161561221b577fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc001612223565b604083901c92505b63ffffffff83161561223857601f1901612240565b602083901c92505b61ffff831615612271577ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff001612279565b601083901c92505b60ff8316156122a9577ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8016122b1565b600883901c92505b600f8316156122e1577ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc016122e9565b600483901c92505b6003831615612319577ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe01612321565b600283901c92505b60018316156120bf576000190192915050565b610100919091020190565b6001901b900390565b612350613ef2565b610e8d83600085518561383e565b6000610e8d838360000151670de0b6b3a7640000612646565b600082820183811015610e8d576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161012790614d79565b6123be613edb565b602083015183516fffffffffffffffffffffffffffffffff909116901561242a5760408051808201909152600181526020848101519082019061241f9084906bffffffffffffffffffffffff16670de0b6b3a764000063ffffffff61392816565b8152509150506120bf565b60408051808201909152600081528351602082019061241f9084906bffffffffffffffffffffffff16670de0b6b3a764000063ffffffff61392816565b83611892576124758361299e565b7f3a2000000000000000000000000000000000000000000000000000000000000061249f8461299e565b7f203c000000000000000000000000000000000000000000000000000000000000611ef3856139b2565b8461080b576124d78461299e565b7f3a200000000000000000000000000000000000000000000000000000000000006125018561299e565b7f203c00000000000000000000000000000000000000000000000000000000000061252b86612a3a565b7f2c2000000000000000000000000000000000000000000000000000000000000061255587612a3a565b604051611e5197969594939291907f3e0000000000000000000000000000000000000000000000000000000000000090602001614c90565b60008261259c575060006120bf565b828202828482816125a957fe5b0414610e8d576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161012790614d99565b60008315806125ee575082155b15612605576125fe600083613b1c565b9050610e8d565b6116c7600161263a8461262e836126228a8a63ffffffff61258d16565b9063ffffffff613b5e16565b9063ffffffff613b1c16565b9063ffffffff61237716565b60006116c78261262e868663ffffffff61258d16565b60005b825181101561189257604051806040016040528084838151811061267f57fe5b6020026020010151600001516001600160a01b031681526020018483815181106126a557fe5b6020026020010151602001518152508282866060015101815181106126c657fe5b602090810291909101015260010161265f565b6126e1613e89565b6126e9613f2c565b82612719576040805160808101909152600181526020810160008152602001600081526020018590529050612747565b8261272057fe5b60408051608081019091526000808252602082019081526020016000815260200186905290505b60408051610100810190915280600581526020018c815260200182815260200184156127735789612775565b8a5b81526020018415612786578a612788565b895b8152602001886001600160a01b031681526020018b81526020018a886040516020016127b5929190614efa565b60408051601f1981840301815291905290529b9a5050505050505050505050565b6127de613e89565b50604080516101008101825260068152602080820197909752815160808082018452600182526000828a0181905282850181905260608084019690965283850192909252938201959095529182019290925260a0810183905260c0810193909352805191825292810190925260e081019190915290565b61285d613e89565b60408051610100810182526004815260208082018b90528251608081018452600080825292938401929091820190815260200160001988146128a05760006128a3565b60015b60018111156128ae57fe5b815260200160001988146128c257876128c5565b60005b8152508152602001888152602001878152602001866001600160a01b03168152602001600081526020018484604051602001612902929190614e0b565b60408051601f19818403018152919052905298975050505050505050565b612928613e89565b604080516101008101909152806005815260208082018b905260408051608081018252600181529201919081016000815260200160008152602001868152508152602001878152602001868152602001856001600160a01b03168152602001888152602001838152509050979650505050505050565b606080826040516020016129b29190614be9565b60408051601f19818403018152919052905060205b8015612a1f578151600019909101908290829081106129e257fe5b01602001517fff000000000000000000000000000000000000000000000000000000000000001615612a1a576001018152905061125d565b6129c7565b5060408051600080825260208201909252905b509392505050565b606081612a7b575060408051808201909152600181527f3000000000000000000000000000000000000000000000000000000000000000602082015261125d565b8160005b8115612a9357600101600a82049150612a7f565b6060816040519080825280601f01601f191660200182016040528015612ac0576020820181803883390190505b508593509050815b8015612b225760001901600a840660300160f81b828281518110612ae857fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350600a84049350612ac8565b50949350505050565b612b358484611fdc565b15612d2557612ba8600282516003811115612b4c57fe5b147f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c69642069736f6c6174696f6e206d6f646520756e777261707065728685600001516003811115612ba057fe5b60ff166124c9565b612bb28483611fdc565b15612d205783516040517f062bd3e90000000000000000000000000000000000000000000000000000000081526000916001600160a01b03169063062bd3e990612c00908690600401614de2565b60206040518083038186803b158015612c1857600080fd5b505afa158015612c2c573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250612c509190810190614574565b9050612d1e816001600160a01b03166316774ba084604001516040518263ffffffff1660e01b8152600401612c859190614d0c565b60206040518083038186803b158015612c9d57600080fd5b505afa158015612cb1573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250612cd591908101906145fc565b7f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c696420756e777261702073657175656e636500000000000000000087876124c9565b505b611892565b612d2f8483611fdc565b15612d9a57612d20600382516003811115612d4657fe5b147f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c69642069736f6c6174696f6e206d6f6465207772617070657200008585600001516003811115612ba057fe5b611892600082516003811115612dac57fe5b1480612dc45750600182516003811115612dc257fe5b145b7f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c69642074726164657220747970650000000000000000000000000084600001516003811115612e1657fe5b60ff16611e91565b81516003811115612e2b57fe5b600214156131e257604080830151865191517f062bd3e900000000000000000000000000000000000000000000000000000000815290916000916001600160a01b039091169063062bd3e990612e85908990600401614de2565b60206040518083038186803b158015612e9d57600080fd5b505afa158015612eb1573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250612ed59190810190614574565b9050612fa8816001600160a01b0316836001600160a01b031663fc0c546a6040518163ffffffff1660e01b815260040160206040518083038186803b158015612f1d57600080fd5b505afa158015612f31573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250612f559190810190614574565b6001600160a01b0316147f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c696420696e70757420666f7220756e777261707065720000000000868a6124c9565b6130f2826001600160a01b031663f1a1f8fa89600001516001600160a01b031663062bd3e9896040518263ffffffff1660e01b8152600401612fea9190614de2565b60206040518083038186803b15801561300257600080fd5b505afa158015613016573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525061303a9190810190614574565b6040518263ffffffff1660e01b81526004016130569190614d0c565b60206040518083038186803b15801561306e57600080fd5b505afa158015613082573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506130a691908101906145fc565b7f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c6964206f757470757420666f7220756e777261707065720000000086600101896124c9565b60408085015190517f16774ba00000000000000000000000000000000000000000000000000000000081526131db916001600160a01b038416916316774ba09161313e91600401614d0c565b60206040518083038186803b15801561315657600080fd5b505afa15801561316a573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525061318e91908101906145fc565b7f47656e6572696354726164657250726f787942617365000000000000000000007f556e7772617070657220747261646572206e6f7420656e61626c65640000000087604001518a613ba0565b505061080b565b815160038111156131ef57fe5b6003141561080b57604080830151865191517f062bd3e900000000000000000000000000000000000000000000000000000000815290916000916001600160a01b039091169063062bd3e990613249908890600401614de2565b60206040518083038186803b15801561326157600080fd5b505afa158015613275573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506132999190810190614574565b90506133e2826001600160a01b0316639f4a129789600001516001600160a01b031663062bd3e98a6040518263ffffffff1660e01b81526004016132dd9190614de2565b60206040518083038186803b1580156132f557600080fd5b505afa158015613309573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525061332d9190810190614574565b6040518263ffffffff1660e01b81526004016133499190614d0c565b60206040518083038186803b15801561336157600080fd5b505afa158015613375573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525061339991908101906145fc565b7f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c696420696e70757420666f72207772617070657200000000000000868a6124c9565b6134b6816001600160a01b0316836001600160a01b031663fc0c546a6040518163ffffffff1660e01b815260040160206040518083038186803b15801561342857600080fd5b505afa15801561343c573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506134609190810190614574565b6001600160a01b0316147f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c6964206f757470757420666f72207772617070657200000000000086600101896124c9565b60408085015190517f16774ba0000000000000000000000000000000000000000000000000000000008152611dfe916001600160a01b038416916316774ba09161350291600401614d0c565b60206040518083038186803b15801561351a57600080fd5b505afa15801561352e573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525061355291908101906145fc565b7f47656e6572696354726164657250726f787942617365000000000000000000007f5772617070657220747261646572206e6f7420656e61626c6564000000000000876040015189613ba0565b815160038111156135ac57fe5b6001141561364657613641835183602001511080156135f9575060006001600160a01b0316848460200151815181106135e157fe5b6020026020010151600001516001600160a01b031614155b7f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c6964206d616b6572206163636f756e74206f776e6572000000000084611e91565b610780565b61078082602001516000147f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c6964206d616b6572206163636f756e74206f776e6572000000000084611e91565b60006060846001600160a01b031684846040516024016136b99190614d5a565b60408051601f198184030181529181526020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fffffffff000000000000000000000000000000000000000000000000000000009094169390931790925290516137249190614bfe565b600060405180830381855afa9150503d806000811461375f576040519150601f19603f3d011682016040523d82523d6000602084013e613764565b606091505b5090969095509350505050565b6000816120bf63ffffffff821682147f4d617468000000000000000000000000000000000000000000000000000000007f556e73616665206361737420746f2075696e743332000000000000000000000084611e91565b600080610100830490506000610100840690506000816001901b8684815181106137ee57fe5b602002602001015116905060008111935050505092915050565b815161010082049060ff8316906001821b9085908490811061382657fe5b60200260200101818151179150818152505050505050565b613846613ef2565b838303801580613878575080600114801561387857508286868151811061386957fe5b60200260200101516000015114155b156138af576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161012790614d89565b6000600282048601905060008782815181106138c757fe5b6020026020010151600001519050808510156138f3576138e98888848861383e565b93505050506116c7565b8085111561390a576138e98883600101888861383e565b87828151811061391657fe5b602002602001015193505050506116c7565b6000831580613935575082155b15613945576125fe600083613b1c565b6000613957858563ffffffff61258d16565b90506139a9613976600161263a600261262e888463ffffffff613b5e16565b613986838663ffffffff613c0216565b1015613993576000613996565b60015b60ff1661263a838663ffffffff613b1c16565b95945050505050565b60408051602a80825260608281019093526001600160a01b038416918391602082018180388339019050509050603060f81b816000815181106139f157fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350607860f81b81600181518110613a3257fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a90535060005b6014811015612a325760028102613a7d600f8516613c44565b838260290381518110613a8c57fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350600484901c9350613ace600f8516613c44565b838260280381518110613add57fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053505060049290921c91600101613a64565b6000610e8d83836040518060400160405280601a81526020017f536166654d6174683a206469766973696f6e206279207a65726f000000000000815250613c66565b6000610e8d83836040518060400160405280601e81526020017f536166654d6174683a207375627472616374696f6e206f766572666c6f770000815250613cb7565b8461080b57613bae8461299e565b7f3a20000000000000000000000000000000000000000000000000000000000000613bd88561299e565b7f203c00000000000000000000000000000000000000000000000000000000000061252b866139b2565b6000610e8d83836040518060400160405280601881526020017f536166654d6174683a206d6f64756c6f206279207a65726f0000000000000000815250613cfd565b6000600a821015613c5c57506030810160f81b61125d565b5060570160f81b90565b60008183613ca1576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016101279190614d5a565b506000838581613cad57fe5b0495945050505050565b60008184841115613cf5576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016101279190614d5a565b505050900390565b60008183613d38576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016101279190614d5a565b50828481613d4257fe5b06949350505050565b60405180610100016040528060006001600160a01b03168152602001600015158152602001600081526020016000815260200160008152602001613d8d613edb565b8152602001613d9a613edb565b8152602001613da7613edb565b905290565b60405180610120016040528060006001600160a01b03168152602001613dd0613edb565b8152602001613ddd613edb565b81526020016000815260200160008152602001606081526020016060815260200160006001600160a01b03168152602001600063ffffffff1681525090565b6040518061014001604052806000815260200160008152602001613e3e613edb565b8152602001613e4b613edb565b8152602001613e58613edb565b8152602001613e65613edb565b81526020016000151581526020016000815260200160008152602001600081525090565b604080516101008101825260008082526020820152908101613ea9613f2c565b8152602001600081526020016000815260200160006001600160a01b0316815260200160008152602001606081525090565b604080518082019091526000808252602082015290565b604051806060016040528060008152602001613f0c613f19565b8152602001613da7613f55565b6040518060200160405280600081525090565b604080516080810190915260008082526020820190815260200160008152602001600081525090565b604080516060810182526000808252602082018190529181019190915290565b80356120bf816150c2565b80516120bf816150c2565b600082601f830112613f9c57600080fd5b8151613faf613faa82614f3c565b614f15565b81815260209384019390925082018360005b83811015613fed5781518601613fd78882614267565b8452506020928301929190910190600101613fc1565b5050505092915050565b600082601f83011261400857600080fd5b8135614016613faa82614f3c565b9150818183526020840193506020810190508385604084028201111561403b57600080fd5b60005b83811015613fed57816140518882614440565b8452506020909201916040919091019060010161403e565b600082601f83011261407a57600080fd5b8135614088613faa82614f3c565b81815260209384019390925082018360005b83811015613fed57813586016140b088826144c2565b845250602092830192919091019060010161409a565b600082601f8301126140d757600080fd5b81356140e5613faa82614f3c565b9150818183526020840193506020810190508385602084028201111561410a57600080fd5b60005b83811015613fed57816141208882614548565b845250602092830192919091019060010161410d565b600082601f83011261414757600080fd5b8151614155613faa82614f3c565b9150818183526020840193506020810190508385602084028201111561417a57600080fd5b60005b83811015613fed57816141908882614553565b845250602092830192919091019060010161417d565b80516120bf816150d6565b600082601f8301126141c257600080fd5b81356141d0613faa82614f5d565b915080825260208301602083018583830111156141ec57600080fd5b6141f783828461506c565b50505092915050565b600082601f83011261421157600080fd5b815161421f613faa82614f5d565b9150808252602083016020830185838301111561423b57600080fd5b6141f7838284615078565b80516120bf816150df565b80516120bf816150ec565b80356120bf816150f9565b6000610160828403121561427a57600080fd5b614285610100614f15565b905060006142938484614246565b82525060206142a484848301614553565b60208301525060406142b884828501614343565b60408301525060c06142cc84828501614553565b60608301525060e06142e084828501614553565b6080830152506101006142f584828501613f80565b60a08301525061012061430a84828501614553565b60c08301525061014082015167ffffffffffffffff81111561432b57600080fd5b61433784828501614200565b60e08301525092915050565b60006080828403121561435557600080fd5b61435f6080614f15565b9050600061436d84846141a6565b825250602061437e84848301614251565b602083015250604061439284828501614251565b60408301525060606143a684828501614553565b60608301525092915050565b6000602082840312156143c457600080fd5b6143ce6020614f15565b905060006143dc8484614553565b82525092915050565b6000606082840312156143f757600080fd5b6144016060614f15565b9050600061440f8484614569565b825250602061442084848301614569565b60208301525060406144348482850161455e565b60408301525092915050565b60006040828403121561445257600080fd5b61445c6040614f15565b9050600061446a8484613f75565b825250602061447b84848301614548565b60208301525092915050565b60006040828403121561449957600080fd5b6144a36040614f15565b905060006144b184846141a6565b825250602061447b8484830161453d565b6000608082840312156144d457600080fd5b6144de6080614f15565b905060006144ec848461425c565b82525060206144fd84848301614548565b602083015250604061451184828501613f75565b604083015250606082013567ffffffffffffffff81111561453157600080fd5b6143a6848285016141b1565b80516120bf81615106565b80356120bf8161510f565b80516120bf8161510f565b80516120bf81615118565b80516120bf81615121565b60006020828403121561458657600080fd5b60006116c78484613f80565b6000602082840312156145a457600080fd5b815167ffffffffffffffff8111156145bb57600080fd5b6116c784828501613f8b565b6000602082840312156145d957600080fd5b815167ffffffffffffffff8111156145f057600080fd5b6116c784828501614136565b60006020828403121561460e57600080fd5b60006116c784846141a6565b60006020828403121561462c57600080fd5b60006116c784846143b2565b60006060828403121561464a57600080fd5b60006116c784846143e5565b6000806000806000806000610120888a03121561467257600080fd5b600061467e8a8a614440565b975050604061468f8a828b01614440565b965050608088013567ffffffffffffffff8111156146ac57600080fd5b6146b88a828b016140c6565b95505060a088013567ffffffffffffffff8111156146d557600080fd5b6146e18a828b016140c6565b94505060c088013567ffffffffffffffff8111156146fe57600080fd5b61470a8a828b01614069565b93505060e088013567ffffffffffffffff81111561472757600080fd5b6147338a828b01613ff7565b9250506101006147458a828b01614548565b91505092959891949750929550565b60006040828403121561476657600080fd5b60006116c78484614487565b6000806040838503121561478557600080fd5b600061479185856143b2565b92505060206147a2858286016143b2565b9150509250929050565b6000602082840312156147be57600080fd5b60006116c78484614553565b6000602082840312156147dc57600080fd5b60006116c7848461455e565b6000610e8d8383614acb565b60006148008383614bb7565b505060400190565b61481181615044565b82525050565b61481181614f98565b600061482b82614f8b565b6148358185614f8f565b93508360208202850161484785614f85565b8060005b85811015614881578484038952815161486485826147e8565b945061486f83614f85565b60209a909a019992505060010161484b565b5091979650505050505050565b600061489982614f8b565b6148a38185614f8f565b93506148ae83614f85565b8060005b838110156148dc5781516148c688826147f4565b97506148d183614f85565b9250506001016148b2565b509495945050505050565b61481181614fa3565b6148116148fc82614fa8565b614ff2565b6148116148fc82614fcd565b6148116148fc82614ff2565b600061492482614f8b565b61492e8185614f8f565b935061493e818560208601615078565b614947816150a4565b9093019392505050565b600061495c82614f8b565b614966818561125d565b9350614976818560208601615078565b9290920192915050565b6148118161504b565b61481181615056565b61481181615061565b60006149a8601b83614f8f565b7f536166654d6174683a206164646974696f6e206f766572666c6f770000000000815260200192915050565b60006149e1602583614f8f565b7f4c697175696461746f7250726f7879426173653a204d61726b6574206e6f742081527f666f756e64000000000000000000000000000000000000000000000000000000602082015260400192915050565b6000614a40602183614f8f565b7f536166654d6174683a206d756c7469706c69636174696f6e206f766572666c6f81527f7700000000000000000000000000000000000000000000000000000000000000602082015260400192915050565b6000614a9f601f83614f8f565b7f5265656e7472616e637947756172643a207265656e7472616e742063616c6c00815260200192915050565b8051600090610160840190614ae08582614989565b506020830151614af36020860182614bd7565b506040830151614b066040860182614b6d565b506060830151614b1960c0860182614bd7565b506080830151614b2c60e0860182614bd7565b5060a0830151614b40610100860182614817565b5060c0830151614b54610120860182614bd7565b5060e08301518482036101408601526139a98282614919565b80516080830190614b7e84826148e7565b506020820151614b916020850182614992565b506040820151614ba46040850182614992565b5060608201516118926060850182614bd7565b80516040830190614bc88482614817565b50602082015161189260208501825b61481181614ff2565b6148118161502a565b6000614bf5828461490d565b50602001919050565b6000610e8d8284614951565b6000614c168286614951565b9150614c228285614901565b6002820191506139a98284614951565b6000614c3e8289614951565b9150614c4a8288614901565b600282019150614c5a8287614951565b9150614c668286614901565b600282019150614c768285614951565b9150614c8282846148f0565b506001019695505050505050565b6000614c9c828b614951565b9150614ca8828a614901565b600282019150614cb88289614951565b9150614cc48288614901565b600282019150614cd48287614951565b9150614ce08286614901565b600282019150614cf08285614951565b9150614cfc82846148f0565b5060010198975050505050505050565b602081016120bf8284614817565b60408101614d288285614817565b610e8d6020830184614808565b60408082528101614d46818561488e565b905081810360208301526116c78184614820565b60208082528101610e8d8184614919565b602081016120bf8284614980565b602080825281016120bf8161499b565b602080825281016120bf816149d4565b602080825281016120bf81614a33565b602080825281016120bf81614a92565b604081016120bf8284614bb7565b60608101614dd58285614bb7565b610e8d6040830184614bd7565b602081016120bf8284614bd7565b60408101614dfe8285614bd7565b610e8d6020830184614817565b60408101614e198285614bd7565b81810360208301526116c78184614919565b60408101614e398285614bd7565b610e8d6020830184614bd7565b6101208101614e55828c614bd7565b614e62602083018b614bd7565b614e6f604083018a614817565b614e7c6060830189614817565b614e896080830188614bd7565b614e9660a0830187614bd7565b614ea360c0830186614bd7565b614eb060e0830185614bd7565b818103610100830152614ec38184614919565b9b9a5050505050505050505050565b60608101614ee08286614bd7565b614eed6020830185614bd7565b6116c76040830184614be0565b60408101614f088285614bd7565b610e8d6020830184614be0565b60405181810167ffffffffffffffff81118282101715614f3457600080fd5b604052919050565b600067ffffffffffffffff821115614f5357600080fd5b5060209081020190565b600067ffffffffffffffff821115614f7457600080fd5b506020601f91909101601f19160190565b60200190565b5190565b90815260200190565b60006120bf8261501e565b151590565b7fff000000000000000000000000000000000000000000000000000000000000001690565b7fffff0000000000000000000000000000000000000000000000000000000000001690565b90565b8061125d816150ae565b8061125d816150b8565b6fffffffffffffffffffffffffffffffff1690565b6001600160a01b031690565b63ffffffff1690565b6bffffffffffffffffffffffff1690565b60006120bf825b60006120bf82614f98565b60006120bf82614ff5565b60006120bf82614fff565b82818337506000910152565b60005b8381101561509357818101518382015260200161507b565b838111156118925750506000910152565b601f01601f191690565b600981106106b857fe5b600281106106b857fe5b6150cb81614f98565b81146106b857600080fd5b6150cb81614fa3565b600981106106b857600080fd5b600281106106b857600080fd5b600481106106b857600080fd5b6150cb81615009565b6150cb81614ff2565b6150cb8161502a565b6150cb8161503356fea365627a7a72315820c4a4bc4b6deb268035b631577fbfb75627ae00fb5e3c3e97d16c3434fa3abda96c6578706572696d656e74616cf564736f6c63430005100040000000000000000000000000dec1ae3b570ac3c57871bbd7bfeacc807f973bea0000000000000000000000006bd780e7fdf01d77e4d475c821f1e7ae0540907200000000000000000000000010d98759762efac656bd4be7f2f5599208f44fac

Deployed Bytecode

0x608060405234801561001057600080fd5b50600436106100675760003560e01c8063380d1bcf11610050578063380d1bcf1461009f5780634530a2d7146100b4578063cf530f88146100bc57610067565b806315c14a4a1461006c578063261140261461008a575b600080fd5b6100746100c4565b6040516100819190614d6b565b60405180910390f35b61009d610098366004614656565b6100d3565b005b6100a76105ac565b6040516100819190614de2565b6100746105b1565b6100746105c0565b6002546001600160a01b031681565b60005474010000000000000000000000000000000000000000900460ff16610130576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161012790614da9565b60405180910390fd5b600080547fffffffffffffffffffffff00ffffffffffffffffffffffffffffffffffffffff169055610160613d4b565b6040805161010081018252600280546001600160a01b031682526000602083018190529282018390526060820152608081019190915260a081016101a26105cf565b81526020016101af6105cf565b81526020016101bc6105cf565b905290506101c9866105ef565b6101d386866106bb565b6101df81878587610785565b61021281876000815181106101f057fe5b60200260200101518760008151811061020557fe5b6020026020010151610812565b61021a613dac565b81516001600160a01b0316815260208101899052604081018890528651879060009061024257fe5b60200260200101518160600181815250508660018851038151811061026357fe5b602002602001015181608001818152505061027e8184610870565b61028b8160600151610b27565b6102988160800151610b27565b805160408083015190517f0f47fab00000000000000000000000000000000000000000000000000000000081526001600160a01b0390921691630f47fab0916102e391600401614db9565b60006040518083038186803b1580156102fb57600080fd5b505afa15801561030f573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f1916820160405261033791908101906145c7565b60c082015280516040517f0f47fab00000000000000000000000000000000000000000000000000000000081526103e691906001600160a01b03821690630f47fab090610388908e90600401614db9565b60006040518083038186803b1580156103a057600080fd5b505afa1580156103b4573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526103dc91908101906145c7565b8360c00151610c0c565b60a0820152826103f7576000610404565b6001546001600160a01b03165b6001600160a01b031660e082015263ffffffff8316610100820152610427613e1c565b61043082610e94565b905061043b82611262565b61044481611473565b61044e878261152d565b606061046484878d600001518e6020015161163c565b9050898160018151811061047457fe5b6020026020010181905250606061048a886116cf565b6001016040519080825280602002602001820160405280156104c657816020015b6104b3613e89565b8152602001906001900390816104ab5790505b5090506104d5818585886117e6565b6104e38282878d8d8d611898565b84516040517fa67a6a450000000000000000000000000000000000000000000000000000000081526001600160a01b039091169063a67a6a459061052d9085908590600401614d35565b600060405180830381600087803b15801561054757600080fd5b505af115801561055b573d6000803e3d6000fd5b5050600080547fffffffffffffffffffffff00ffffffffffffffffffffffffffffffffffffffff16740100000000000000000000000000000000000000001790555050505050505050505050505050565b600190565b6001546001600160a01b031681565b6000546001600160a01b031681565b6105d7613edb565b50604080518082019091526000808252602082015290565b61063f6002825110157f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c6964206d61726b65742070617468206c656e677468000000000000611e07565b6106b88160018351038151811061065257fe5b60200260200101518260008151811061066757fe5b602002602001015114157f47656e6572696354726164657250726f787942617365000000000000000000007f4475706c6963617465206d61726b65747320696e207061746800000000000000611e07565b50565b61070a81518351147f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c696420616d6f756e74732070617468206c656e6774680000000000611e07565b60005b815181101561078057610778600083838151811061072757fe5b6020026020010151117f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c696420616d6f756e7420617420696e64657800000000000000000084611e91565b60010161070d565b505050565b6107d781516001018451147f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c6964207472616465727320706172616d73206c656e677468000000611e07565b60005b815181101561080b576108038585858585815181106107f557fe5b602002602001015185611f29565b6001016107da565b5050505050565b61081c8383611fdc565b156107805761078060001982147f4c697175696461746f7250726f787956340000000000000000000000000000007f496e76616c696420616d6f756e7420666f722049736f6c6174696f6e4d6f6465611e07565b81516001600160a01b031661088157fe5b6020820151516001600160a01b031661089657fe5b6040820151516001600160a01b03166108ab57fe5b6109068260600151836080015114157f4c697175696461746f7250726f787942617365000000000000000000000000007f4f776564206d61726b657420657175616c732068656c64206d61726b657400008560800151611e91565b6109e761099a83600001516001600160a01b03166347d1b53c856040015186608001516040518363ffffffff1660e01b8152600401610946929190614dc7565b604080518083038186803b15801561095d57600080fd5b505afa158015610971573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506109959190810190614754565b6120c5565b157f4c697175696461746f7250726f787942617365000000000000000000000000007f4f776564206d61726b65742063616e6e6f7420626520706f73697469766500008560800151611e91565b610a73610a2783600001516001600160a01b03166347d1b53c856040015186606001516040518363ffffffff1660e01b8152600401610946929190614dc7565b7f4c697175696461746f7250726f787942617365000000000000000000000000007f48656c64206d61726b65742063616e6e6f74206265206e6567617469766500008560600151611e91565b610ac7818263ffffffff16147f4c697175696461746f7250726f787942617365000000000000000000000000007f457870697279206f766572666c6f77730000000000000000000000000000000084611e91565b610b23610ad26120ed565b63ffffffff168211157f4c697175696461746f7250726f787942617365000000000000000000000000007f426f72726f77206e6f742079657420657870697265640000000000000000000084611e91565b5050565b6000546040517fd9f476010000000000000000000000000000000000000000000000000000000081526106b8916001600160a01b03169063d9f4760190610b749085903090600401614df0565b60206040518083038186803b158015610b8c57600080fd5b505afa158015610ba0573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250610bc491908101906145fc565b7f4c697175696461746f7250726f787942617365000000000000000000000000007f4173736574206e6f742077686974656c6973746564000000000000000000000084611e91565b606080610c88856001600160a01b031663295c39a56040518163ffffffff1660e01b815260040160206040518083038186803b158015610c4b57600080fd5b505afa158015610c5f573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250610c8391908101906147ac565b6120fd565b90506000610c97858383612136565b9050610ca4848383612136565b90506000809050606082604051908082528060200260200182016040528015610ce757816020015b610cd4613ef2565b815260200190600190039081610ccc5790505b50905060005b8451811015610e86576000858281518110610d0457fe5b602002602001015190505b8015610e6f576000610d2082612199565b90506000610d2e8483612334565b905060405180606001604052808281526020018d6001600160a01b0316638928378e846040518263ffffffff1660e01b8152600401610d6d9190614de2565b60206040518083038186803b158015610d8557600080fd5b505afa158015610d99573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250610dbd919081019061461a565b81526020018d6001600160a01b03166356ea84b2846040518263ffffffff1660e01b8152600401610dee9190614de2565b60606040518083038186803b158015610e0657600080fd5b505afa158015610e1a573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250610e3e9190810190614638565b90528551600188019787918110610e5157fe5b6020026020010181905250610e66838361233f565b92505050610d0f565b84841415610e7d5750610e86565b50600101610ced565b5093505050505b9392505050565b610e9c613e1c565b610ea4613ef2565b610eb68360a001518460600151612348565b9050610ec0613ef2565b610ed28460a001518560800151612348565b905060008085610100015163ffffffff161115610f8c57610ef1613f19565b8560e001516001600160a01b031663b1396542876060015188608001518961010001516040518463ffffffff1660e01b8152600401610f3293929190614ed2565b604080518083038186803b158015610f4957600080fd5b505afa158015610f5d573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250610f819190810190614772565b519250611063915050565b610f94613f19565b8551606087015160808801516040517fd24c48bc0000000000000000000000000000000000000000000000000000000081526001600160a01b039093169263d24c48bc92610fe6929091600401614e2b565b60206040518083038186803b158015610ffe57600080fd5b505afa158015611012573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250611036919081019061461a565b905061105f61104d8460200151600001518361235e565b6020850151519063ffffffff61237716565b9150505b604051806101400160405280600081526020016000815260200161111387600001516001600160a01b03166347d1b53c89602001518a606001516040518363ffffffff1660e01b81526004016110ba929190614dc7565b604080518083038186803b1580156110d157600080fd5b505afa1580156110e5573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506111099190810190614754565b86604001516123b6565b81526020016111ae87600001516001600160a01b03166347d1b53c89602001518a608001516040518363ffffffff1660e01b8152600401611155929190614dc7565b604080518083038186803b15801561116c57600080fd5b505afa158015611180573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506111a49190810190614754565b85604001516123b6565b81526020016111f087600001516001600160a01b03166347d1b53c89604001518a606001516040518363ffffffff1660e01b81526004016110ba929190614dc7565b815260200161123287600001516001600160a01b03166347d1b53c89604001518a608001516040518363ffffffff1660e01b8152600401611155929190614dc7565b8152600060208083019190915294850151516040820152929093015151606083015260809091015290505b919050565b602081015151611363906001600160a01b031633148061131b575081516020830151516040517f3a031bf00000000000000000000000000000000000000000000000000000000081526001600160a01b0390921691633a031bf0916112cb913390600401614d1a565b60206040518083038186803b1580156112e357600080fd5b505afa1580156112f7573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525061131b91908101906145fc565b7f4c697175696461746f7250726f787942617365000000000000000000000000007f53656e646572206e6f74206f70657261746f720000000000000000000000000033612467565b61010081015163ffffffff16156106b85760008160e001516001600160a01b0316631be7dd83836040015184608001516040518363ffffffff1660e01b81526004016113b0929190614dc7565b60206040518083038186803b1580156113c857600080fd5b505afa1580156113dc573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525061140091908101906147ca565b9050610b2382610100015163ffffffff168263ffffffff16147f4c697175696461746f7250726f787942617365000000000000000000000000007f457870697279206d69736d6174636800000000000000000000000000000000008463ffffffff1686610100015163ffffffff166124c9565b60006114948260800151602001518360e0015161258d90919063ffffffff16565b905060006114b88360a001516020015184610120015161258d90919063ffffffff16565b9050808210156114fa57608083015160209081018051918501919091525160e08401516101208501516114ec9291906125e1565b8352600160c0840152610780565b6115168360a00151602001518461012001518560e00151612646565b60208085019190915260a084015101518352505050565b805161153557fe5b60008260018451038151811061154757fe5b60200260200101519050816000015181108015611595575060808201516020015160e083015161157c9163ffffffff61258d16565b61012083015161159390839063ffffffff61258d16565b105b156115b95780825261012082015160e08301516115b3918391612646565b60208301525b600019836000815181106115c957fe5b602002602001015114156115f6578160200151836000815181106115e957fe5b6020026020010181815250505b6000198360018551038151811061160957fe5b6020026020010151141561078057815183518490600019810190811061162b57fe5b602002602001018181525050505050565b606080845186606001510160405190808252806020026020018201604052801561168057816020015b61166d613edb565b8152602001906001900390816116655790505b5090506040518060400160405280856001600160a01b0316815260200184815250816000815181106116ae57fe5b60200260200101819052506116c486868361265c565b90505b949350505050565b600080805b83518110156117df578381815181106116e957fe5b602002602001015160000151600381111561170057fe5b600214156117995783818151811061171457fe5b6020026020010151604001516001600160a01b031663b189111a6040518163ffffffff1660e01b815260040160206040518083038186803b15801561175857600080fd5b505afa15801561176c573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525061179091908101906147ac565b820191506117d7565b8381815181106117a557fe5b60200260200101516000015160038111156117bc57fe5b600314156117d05783818151811061171457fe5b6001820191505b6001016116d4565b5092915050565b61010083015163ffffffff16156118515761182760006001856080015186606001518760e00151886101000151886020015189600001518a60c001516126d9565b848260800180518091906001018152508151811061184157fe5b6020026020010181905250611892565b61186c600060018560800151866060015186600001516127d6565b848260800180518091906001018152508151811061188657fe5b60200260200101819052505b50505050565b60005b8151811015611dfe5760008282815181106118b257fe5b60200260200101516000015160038111156118c957fe5b14156119895761195f60008583815181106118e057fe5b60200260200101518684600101815181106118f757fe5b602002602001015185858151811061190b57fe5b60200260200101516040015187868151811061192357fe5b602002602001015188876001018151811061193a57fe5b602002602001015188888151811061194e57fe5b602002602001015160600151612855565b868660800180518091906001018152508151811061197957fe5b6020026020010181905250611df6565b600182828151811061199757fe5b60200260200101516000015160038111156119ae57fe5b1415611a4b5761195f600086606001518484815181106119ca57fe5b602002602001015160200151018684815181106119e357fe5b60200260200101518785600101815181106119fa57fe5b6020026020010151868681518110611a0e57fe5b602002602001015160400151888781518110611a2657fe5b6020026020010151888881518110611a3a57fe5b602002602001015160600151612920565b6002828281518110611a5957fe5b6020026020010151600001516003811115611a7057fe5b1415611c22576000828281518110611a8457fe5b60200260200101516040015190506060816001600160a01b031663a8c1a94c6000611aad6105ac565b8c600081518110611aba57fe5b6020026020010151600001518d611acf6105ac565b81518110611ad957fe5b6020026020010151600001518b8960010181518110611af457fe5b60200260200101518c8a81518110611b0857fe5b60200260200101518c8b60010181518110611b1f57fe5b60200260200101518d8c81518110611b3357fe5b60200260200101518d8d81518110611b4757fe5b6020026020010151606001516040518a63ffffffff1660e01b8152600401611b7799989796959493929190614e46565b60006040518083038186803b158015611b8f57600080fd5b505afa158015611ba3573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052611bcb9190810190614592565b905060005b8151811015611c1a57818181518110611be557fe5b60200260200101518989608001805180919060010181525081518110611c0757fe5b6020908102919091010152600101611bd0565b505050611df6565b6003828281518110611c3057fe5b6020026020010151600001516003811115611c4757fe5b14611c4e57fe5b6000828281518110611c5c57fe5b60200260200101516040015190506060816001600160a01b031663f9a92b2b6000611c856105ac565b8c600081518110611c9257fe5b6020026020010151600001518d611ca76105ac565b81518110611cb157fe5b6020026020010151600001518b8960010181518110611ccc57fe5b60200260200101518c8a81518110611ce057fe5b60200260200101518c8b60010181518110611cf757fe5b60200260200101518d8c81518110611d0b57fe5b60200260200101518d8d81518110611d1f57fe5b6020026020010151606001516040518a63ffffffff1660e01b8152600401611d4f99989796959493929190614e46565b60006040518083038186803b158015611d6757600080fd5b505afa158015611d7b573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052611da39190810190614592565b905060005b8151811015611df257818181518110611dbd57fe5b60200260200101518989608001805180919060010181525081518110611ddf57fe5b6020908102919091010152600101611da8565b5050505b60010161189b565b50505050505050565b8261078057611e158261299e565b7f3a20000000000000000000000000000000000000000000000000000000000000611e3f8361299e565b604051602001611e5193929190614c0a565b60408051601f19818403018152908290527f08c379a000000000000000000000000000000000000000000000000000000000825261012791600401614d5a565b8361189257611e9f8361299e565b7f3a20000000000000000000000000000000000000000000000000000000000000611ec98461299e565b7f203c000000000000000000000000000000000000000000000000000000000000611ef385612a3a565b604051611e519594939291907f3e0000000000000000000000000000000000000000000000000000000000000090602001614c32565b6040820151611f85906001600160a01b031615157f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c69642074726164657220617420696e64657800000000000000000084611e91565b6000848281518110611f9357fe5b602002602001015190506000858360010181518110611fae57fe5b60200260200101519050611fc487838387612b2b565b611fd18783838787612e1e565b611dfe85858561359f565b600080606061209a85600001516001600160a01b031663062bd3e9866040518263ffffffff1660e01b81526004016120149190614de2565b60206040518083038186803b15801561202c57600080fd5b505afa158015612040573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506120649190810190614574565b6040805160208101909152600081527fed4e3a350000000000000000000000000000000000000000000000000000000090613699565b915091508180156120ba5750808060200190516120ba91908101906145fc565b925050505b92915050565b805160009080156120bf575050602001516fffffffffffffffffffffffffffffffff16151590565b60006120f842613771565b905090565b606060016101008304016040519080825280602002602001820160405280156117df578160200160208202803883390190505092915050565b6000805b8451811015612190576121608486838151811061215357fe5b60200260200101516137c8565b612188576121818486838151811061217457fe5b6020026020010151613808565b6001830192505b60010161213a565b50909392505050565b600060ff6fffffffffffffffffffffffffffffffff8316156121dc577fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80016121e4565b608083901c92505b67ffffffffffffffff83161561221b577fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc001612223565b604083901c92505b63ffffffff83161561223857601f1901612240565b602083901c92505b61ffff831615612271577ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff001612279565b601083901c92505b60ff8316156122a9577ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8016122b1565b600883901c92505b600f8316156122e1577ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc016122e9565b600483901c92505b6003831615612319577ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe01612321565b600283901c92505b60018316156120bf576000190192915050565b610100919091020190565b6001901b900390565b612350613ef2565b610e8d83600085518561383e565b6000610e8d838360000151670de0b6b3a7640000612646565b600082820183811015610e8d576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161012790614d79565b6123be613edb565b602083015183516fffffffffffffffffffffffffffffffff909116901561242a5760408051808201909152600181526020848101519082019061241f9084906bffffffffffffffffffffffff16670de0b6b3a764000063ffffffff61392816565b8152509150506120bf565b60408051808201909152600081528351602082019061241f9084906bffffffffffffffffffffffff16670de0b6b3a764000063ffffffff61392816565b83611892576124758361299e565b7f3a2000000000000000000000000000000000000000000000000000000000000061249f8461299e565b7f203c000000000000000000000000000000000000000000000000000000000000611ef3856139b2565b8461080b576124d78461299e565b7f3a200000000000000000000000000000000000000000000000000000000000006125018561299e565b7f203c00000000000000000000000000000000000000000000000000000000000061252b86612a3a565b7f2c2000000000000000000000000000000000000000000000000000000000000061255587612a3a565b604051611e5197969594939291907f3e0000000000000000000000000000000000000000000000000000000000000090602001614c90565b60008261259c575060006120bf565b828202828482816125a957fe5b0414610e8d576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161012790614d99565b60008315806125ee575082155b15612605576125fe600083613b1c565b9050610e8d565b6116c7600161263a8461262e836126228a8a63ffffffff61258d16565b9063ffffffff613b5e16565b9063ffffffff613b1c16565b9063ffffffff61237716565b60006116c78261262e868663ffffffff61258d16565b60005b825181101561189257604051806040016040528084838151811061267f57fe5b6020026020010151600001516001600160a01b031681526020018483815181106126a557fe5b6020026020010151602001518152508282866060015101815181106126c657fe5b602090810291909101015260010161265f565b6126e1613e89565b6126e9613f2c565b82612719576040805160808101909152600181526020810160008152602001600081526020018590529050612747565b8261272057fe5b60408051608081019091526000808252602082019081526020016000815260200186905290505b60408051610100810190915280600581526020018c815260200182815260200184156127735789612775565b8a5b81526020018415612786578a612788565b895b8152602001886001600160a01b031681526020018b81526020018a886040516020016127b5929190614efa565b60408051601f1981840301815291905290529b9a5050505050505050505050565b6127de613e89565b50604080516101008101825260068152602080820197909752815160808082018452600182526000828a0181905282850181905260608084019690965283850192909252938201959095529182019290925260a0810183905260c0810193909352805191825292810190925260e081019190915290565b61285d613e89565b60408051610100810182526004815260208082018b90528251608081018452600080825292938401929091820190815260200160001988146128a05760006128a3565b60015b60018111156128ae57fe5b815260200160001988146128c257876128c5565b60005b8152508152602001888152602001878152602001866001600160a01b03168152602001600081526020018484604051602001612902929190614e0b565b60408051601f19818403018152919052905298975050505050505050565b612928613e89565b604080516101008101909152806005815260208082018b905260408051608081018252600181529201919081016000815260200160008152602001868152508152602001878152602001868152602001856001600160a01b03168152602001888152602001838152509050979650505050505050565b606080826040516020016129b29190614be9565b60408051601f19818403018152919052905060205b8015612a1f578151600019909101908290829081106129e257fe5b01602001517fff000000000000000000000000000000000000000000000000000000000000001615612a1a576001018152905061125d565b6129c7565b5060408051600080825260208201909252905b509392505050565b606081612a7b575060408051808201909152600181527f3000000000000000000000000000000000000000000000000000000000000000602082015261125d565b8160005b8115612a9357600101600a82049150612a7f565b6060816040519080825280601f01601f191660200182016040528015612ac0576020820181803883390190505b508593509050815b8015612b225760001901600a840660300160f81b828281518110612ae857fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350600a84049350612ac8565b50949350505050565b612b358484611fdc565b15612d2557612ba8600282516003811115612b4c57fe5b147f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c69642069736f6c6174696f6e206d6f646520756e777261707065728685600001516003811115612ba057fe5b60ff166124c9565b612bb28483611fdc565b15612d205783516040517f062bd3e90000000000000000000000000000000000000000000000000000000081526000916001600160a01b03169063062bd3e990612c00908690600401614de2565b60206040518083038186803b158015612c1857600080fd5b505afa158015612c2c573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250612c509190810190614574565b9050612d1e816001600160a01b03166316774ba084604001516040518263ffffffff1660e01b8152600401612c859190614d0c565b60206040518083038186803b158015612c9d57600080fd5b505afa158015612cb1573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250612cd591908101906145fc565b7f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c696420756e777261702073657175656e636500000000000000000087876124c9565b505b611892565b612d2f8483611fdc565b15612d9a57612d20600382516003811115612d4657fe5b147f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c69642069736f6c6174696f6e206d6f6465207772617070657200008585600001516003811115612ba057fe5b611892600082516003811115612dac57fe5b1480612dc45750600182516003811115612dc257fe5b145b7f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c69642074726164657220747970650000000000000000000000000084600001516003811115612e1657fe5b60ff16611e91565b81516003811115612e2b57fe5b600214156131e257604080830151865191517f062bd3e900000000000000000000000000000000000000000000000000000000815290916000916001600160a01b039091169063062bd3e990612e85908990600401614de2565b60206040518083038186803b158015612e9d57600080fd5b505afa158015612eb1573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250612ed59190810190614574565b9050612fa8816001600160a01b0316836001600160a01b031663fc0c546a6040518163ffffffff1660e01b815260040160206040518083038186803b158015612f1d57600080fd5b505afa158015612f31573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250612f559190810190614574565b6001600160a01b0316147f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c696420696e70757420666f7220756e777261707065720000000000868a6124c9565b6130f2826001600160a01b031663f1a1f8fa89600001516001600160a01b031663062bd3e9896040518263ffffffff1660e01b8152600401612fea9190614de2565b60206040518083038186803b15801561300257600080fd5b505afa158015613016573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525061303a9190810190614574565b6040518263ffffffff1660e01b81526004016130569190614d0c565b60206040518083038186803b15801561306e57600080fd5b505afa158015613082573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506130a691908101906145fc565b7f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c6964206f757470757420666f7220756e777261707065720000000086600101896124c9565b60408085015190517f16774ba00000000000000000000000000000000000000000000000000000000081526131db916001600160a01b038416916316774ba09161313e91600401614d0c565b60206040518083038186803b15801561315657600080fd5b505afa15801561316a573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525061318e91908101906145fc565b7f47656e6572696354726164657250726f787942617365000000000000000000007f556e7772617070657220747261646572206e6f7420656e61626c65640000000087604001518a613ba0565b505061080b565b815160038111156131ef57fe5b6003141561080b57604080830151865191517f062bd3e900000000000000000000000000000000000000000000000000000000815290916000916001600160a01b039091169063062bd3e990613249908890600401614de2565b60206040518083038186803b15801561326157600080fd5b505afa158015613275573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506132999190810190614574565b90506133e2826001600160a01b0316639f4a129789600001516001600160a01b031663062bd3e98a6040518263ffffffff1660e01b81526004016132dd9190614de2565b60206040518083038186803b1580156132f557600080fd5b505afa158015613309573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525061332d9190810190614574565b6040518263ffffffff1660e01b81526004016133499190614d0c565b60206040518083038186803b15801561336157600080fd5b505afa158015613375573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525061339991908101906145fc565b7f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c696420696e70757420666f72207772617070657200000000000000868a6124c9565b6134b6816001600160a01b0316836001600160a01b031663fc0c546a6040518163ffffffff1660e01b815260040160206040518083038186803b15801561342857600080fd5b505afa15801561343c573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052506134609190810190614574565b6001600160a01b0316147f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c6964206f757470757420666f72207772617070657200000000000086600101896124c9565b60408085015190517f16774ba0000000000000000000000000000000000000000000000000000000008152611dfe916001600160a01b038416916316774ba09161350291600401614d0c565b60206040518083038186803b15801561351a57600080fd5b505afa15801561352e573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525061355291908101906145fc565b7f47656e6572696354726164657250726f787942617365000000000000000000007f5772617070657220747261646572206e6f7420656e61626c6564000000000000876040015189613ba0565b815160038111156135ac57fe5b6001141561364657613641835183602001511080156135f9575060006001600160a01b0316848460200151815181106135e157fe5b6020026020010151600001516001600160a01b031614155b7f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c6964206d616b6572206163636f756e74206f776e6572000000000084611e91565b610780565b61078082602001516000147f47656e6572696354726164657250726f787942617365000000000000000000007f496e76616c6964206d616b6572206163636f756e74206f776e6572000000000084611e91565b60006060846001600160a01b031684846040516024016136b99190614d5a565b60408051601f198184030181529181526020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff167fffffffff000000000000000000000000000000000000000000000000000000009094169390931790925290516137249190614bfe565b600060405180830381855afa9150503d806000811461375f576040519150601f19603f3d011682016040523d82523d6000602084013e613764565b606091505b5090969095509350505050565b6000816120bf63ffffffff821682147f4d617468000000000000000000000000000000000000000000000000000000007f556e73616665206361737420746f2075696e743332000000000000000000000084611e91565b600080610100830490506000610100840690506000816001901b8684815181106137ee57fe5b602002602001015116905060008111935050505092915050565b815161010082049060ff8316906001821b9085908490811061382657fe5b60200260200101818151179150818152505050505050565b613846613ef2565b838303801580613878575080600114801561387857508286868151811061386957fe5b60200260200101516000015114155b156138af576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161012790614d89565b6000600282048601905060008782815181106138c757fe5b6020026020010151600001519050808510156138f3576138e98888848861383e565b93505050506116c7565b8085111561390a576138e98883600101888861383e565b87828151811061391657fe5b602002602001015193505050506116c7565b6000831580613935575082155b15613945576125fe600083613b1c565b6000613957858563ffffffff61258d16565b90506139a9613976600161263a600261262e888463ffffffff613b5e16565b613986838663ffffffff613c0216565b1015613993576000613996565b60015b60ff1661263a838663ffffffff613b1c16565b95945050505050565b60408051602a80825260608281019093526001600160a01b038416918391602082018180388339019050509050603060f81b816000815181106139f157fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350607860f81b81600181518110613a3257fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a90535060005b6014811015612a325760028102613a7d600f8516613c44565b838260290381518110613a8c57fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350600484901c9350613ace600f8516613c44565b838260280381518110613add57fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a9053505060049290921c91600101613a64565b6000610e8d83836040518060400160405280601a81526020017f536166654d6174683a206469766973696f6e206279207a65726f000000000000815250613c66565b6000610e8d83836040518060400160405280601e81526020017f536166654d6174683a207375627472616374696f6e206f766572666c6f770000815250613cb7565b8461080b57613bae8461299e565b7f3a20000000000000000000000000000000000000000000000000000000000000613bd88561299e565b7f203c00000000000000000000000000000000000000000000000000000000000061252b866139b2565b6000610e8d83836040518060400160405280601881526020017f536166654d6174683a206d6f64756c6f206279207a65726f0000000000000000815250613cfd565b6000600a821015613c5c57506030810160f81b61125d565b5060570160f81b90565b60008183613ca1576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016101279190614d5a565b506000838581613cad57fe5b0495945050505050565b60008184841115613cf5576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016101279190614d5a565b505050900390565b60008183613d38576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016101279190614d5a565b50828481613d4257fe5b06949350505050565b60405180610100016040528060006001600160a01b03168152602001600015158152602001600081526020016000815260200160008152602001613d8d613edb565b8152602001613d9a613edb565b8152602001613da7613edb565b905290565b60405180610120016040528060006001600160a01b03168152602001613dd0613edb565b8152602001613ddd613edb565b81526020016000815260200160008152602001606081526020016060815260200160006001600160a01b03168152602001600063ffffffff1681525090565b6040518061014001604052806000815260200160008152602001613e3e613edb565b8152602001613e4b613edb565b8152602001613e58613edb565b8152602001613e65613edb565b81526020016000151581526020016000815260200160008152602001600081525090565b604080516101008101825260008082526020820152908101613ea9613f2c565b8152602001600081526020016000815260200160006001600160a01b0316815260200160008152602001606081525090565b604080518082019091526000808252602082015290565b604051806060016040528060008152602001613f0c613f19565b8152602001613da7613f55565b6040518060200160405280600081525090565b604080516080810190915260008082526020820190815260200160008152602001600081525090565b604080516060810182526000808252602082018190529181019190915290565b80356120bf816150c2565b80516120bf816150c2565b600082601f830112613f9c57600080fd5b8151613faf613faa82614f3c565b614f15565b81815260209384019390925082018360005b83811015613fed5781518601613fd78882614267565b8452506020928301929190910190600101613fc1565b5050505092915050565b600082601f83011261400857600080fd5b8135614016613faa82614f3c565b9150818183526020840193506020810190508385604084028201111561403b57600080fd5b60005b83811015613fed57816140518882614440565b8452506020909201916040919091019060010161403e565b600082601f83011261407a57600080fd5b8135614088613faa82614f3c565b81815260209384019390925082018360005b83811015613fed57813586016140b088826144c2565b845250602092830192919091019060010161409a565b600082601f8301126140d757600080fd5b81356140e5613faa82614f3c565b9150818183526020840193506020810190508385602084028201111561410a57600080fd5b60005b83811015613fed57816141208882614548565b845250602092830192919091019060010161410d565b600082601f83011261414757600080fd5b8151614155613faa82614f3c565b9150818183526020840193506020810190508385602084028201111561417a57600080fd5b60005b83811015613fed57816141908882614553565b845250602092830192919091019060010161417d565b80516120bf816150d6565b600082601f8301126141c257600080fd5b81356141d0613faa82614f5d565b915080825260208301602083018583830111156141ec57600080fd5b6141f783828461506c565b50505092915050565b600082601f83011261421157600080fd5b815161421f613faa82614f5d565b9150808252602083016020830185838301111561423b57600080fd5b6141f7838284615078565b80516120bf816150df565b80516120bf816150ec565b80356120bf816150f9565b6000610160828403121561427a57600080fd5b614285610100614f15565b905060006142938484614246565b82525060206142a484848301614553565b60208301525060406142b884828501614343565b60408301525060c06142cc84828501614553565b60608301525060e06142e084828501614553565b6080830152506101006142f584828501613f80565b60a08301525061012061430a84828501614553565b60c08301525061014082015167ffffffffffffffff81111561432b57600080fd5b61433784828501614200565b60e08301525092915050565b60006080828403121561435557600080fd5b61435f6080614f15565b9050600061436d84846141a6565b825250602061437e84848301614251565b602083015250604061439284828501614251565b60408301525060606143a684828501614553565b60608301525092915050565b6000602082840312156143c457600080fd5b6143ce6020614f15565b905060006143dc8484614553565b82525092915050565b6000606082840312156143f757600080fd5b6144016060614f15565b9050600061440f8484614569565b825250602061442084848301614569565b60208301525060406144348482850161455e565b60408301525092915050565b60006040828403121561445257600080fd5b61445c6040614f15565b9050600061446a8484613f75565b825250602061447b84848301614548565b60208301525092915050565b60006040828403121561449957600080fd5b6144a36040614f15565b905060006144b184846141a6565b825250602061447b8484830161453d565b6000608082840312156144d457600080fd5b6144de6080614f15565b905060006144ec848461425c565b82525060206144fd84848301614548565b602083015250604061451184828501613f75565b604083015250606082013567ffffffffffffffff81111561453157600080fd5b6143a6848285016141b1565b80516120bf81615106565b80356120bf8161510f565b80516120bf8161510f565b80516120bf81615118565b80516120bf81615121565b60006020828403121561458657600080fd5b60006116c78484613f80565b6000602082840312156145a457600080fd5b815167ffffffffffffffff8111156145bb57600080fd5b6116c784828501613f8b565b6000602082840312156145d957600080fd5b815167ffffffffffffffff8111156145f057600080fd5b6116c784828501614136565b60006020828403121561460e57600080fd5b60006116c784846141a6565b60006020828403121561462c57600080fd5b60006116c784846143b2565b60006060828403121561464a57600080fd5b60006116c784846143e5565b6000806000806000806000610120888a03121561467257600080fd5b600061467e8a8a614440565b975050604061468f8a828b01614440565b965050608088013567ffffffffffffffff8111156146ac57600080fd5b6146b88a828b016140c6565b95505060a088013567ffffffffffffffff8111156146d557600080fd5b6146e18a828b016140c6565b94505060c088013567ffffffffffffffff8111156146fe57600080fd5b61470a8a828b01614069565b93505060e088013567ffffffffffffffff81111561472757600080fd5b6147338a828b01613ff7565b9250506101006147458a828b01614548565b91505092959891949750929550565b60006040828403121561476657600080fd5b60006116c78484614487565b6000806040838503121561478557600080fd5b600061479185856143b2565b92505060206147a2858286016143b2565b9150509250929050565b6000602082840312156147be57600080fd5b60006116c78484614553565b6000602082840312156147dc57600080fd5b60006116c7848461455e565b6000610e8d8383614acb565b60006148008383614bb7565b505060400190565b61481181615044565b82525050565b61481181614f98565b600061482b82614f8b565b6148358185614f8f565b93508360208202850161484785614f85565b8060005b85811015614881578484038952815161486485826147e8565b945061486f83614f85565b60209a909a019992505060010161484b565b5091979650505050505050565b600061489982614f8b565b6148a38185614f8f565b93506148ae83614f85565b8060005b838110156148dc5781516148c688826147f4565b97506148d183614f85565b9250506001016148b2565b509495945050505050565b61481181614fa3565b6148116148fc82614fa8565b614ff2565b6148116148fc82614fcd565b6148116148fc82614ff2565b600061492482614f8b565b61492e8185614f8f565b935061493e818560208601615078565b614947816150a4565b9093019392505050565b600061495c82614f8b565b614966818561125d565b9350614976818560208601615078565b9290920192915050565b6148118161504b565b61481181615056565b61481181615061565b60006149a8601b83614f8f565b7f536166654d6174683a206164646974696f6e206f766572666c6f770000000000815260200192915050565b60006149e1602583614f8f565b7f4c697175696461746f7250726f7879426173653a204d61726b6574206e6f742081527f666f756e64000000000000000000000000000000000000000000000000000000602082015260400192915050565b6000614a40602183614f8f565b7f536166654d6174683a206d756c7469706c69636174696f6e206f766572666c6f81527f7700000000000000000000000000000000000000000000000000000000000000602082015260400192915050565b6000614a9f601f83614f8f565b7f5265656e7472616e637947756172643a207265656e7472616e742063616c6c00815260200192915050565b8051600090610160840190614ae08582614989565b506020830151614af36020860182614bd7565b506040830151614b066040860182614b6d565b506060830151614b1960c0860182614bd7565b506080830151614b2c60e0860182614bd7565b5060a0830151614b40610100860182614817565b5060c0830151614b54610120860182614bd7565b5060e08301518482036101408601526139a98282614919565b80516080830190614b7e84826148e7565b506020820151614b916020850182614992565b506040820151614ba46040850182614992565b5060608201516118926060850182614bd7565b80516040830190614bc88482614817565b50602082015161189260208501825b61481181614ff2565b6148118161502a565b6000614bf5828461490d565b50602001919050565b6000610e8d8284614951565b6000614c168286614951565b9150614c228285614901565b6002820191506139a98284614951565b6000614c3e8289614951565b9150614c4a8288614901565b600282019150614c5a8287614951565b9150614c668286614901565b600282019150614c768285614951565b9150614c8282846148f0565b506001019695505050505050565b6000614c9c828b614951565b9150614ca8828a614901565b600282019150614cb88289614951565b9150614cc48288614901565b600282019150614cd48287614951565b9150614ce08286614901565b600282019150614cf08285614951565b9150614cfc82846148f0565b5060010198975050505050505050565b602081016120bf8284614817565b60408101614d288285614817565b610e8d6020830184614808565b60408082528101614d46818561488e565b905081810360208301526116c78184614820565b60208082528101610e8d8184614919565b602081016120bf8284614980565b602080825281016120bf8161499b565b602080825281016120bf816149d4565b602080825281016120bf81614a33565b602080825281016120bf81614a92565b604081016120bf8284614bb7565b60608101614dd58285614bb7565b610e8d6040830184614bd7565b602081016120bf8284614bd7565b60408101614dfe8285614bd7565b610e8d6020830184614817565b60408101614e198285614bd7565b81810360208301526116c78184614919565b60408101614e398285614bd7565b610e8d6020830184614bd7565b6101208101614e55828c614bd7565b614e62602083018b614bd7565b614e6f604083018a614817565b614e7c6060830189614817565b614e896080830188614bd7565b614e9660a0830187614bd7565b614ea360c0830186614bd7565b614eb060e0830185614bd7565b818103610100830152614ec38184614919565b9b9a5050505050505050505050565b60608101614ee08286614bd7565b614eed6020830185614bd7565b6116c76040830184614be0565b60408101614f088285614bd7565b610e8d6020830184614be0565b60405181810167ffffffffffffffff81118282101715614f3457600080fd5b604052919050565b600067ffffffffffffffff821115614f5357600080fd5b5060209081020190565b600067ffffffffffffffff821115614f7457600080fd5b506020601f91909101601f19160190565b60200190565b5190565b90815260200190565b60006120bf8261501e565b151590565b7fff000000000000000000000000000000000000000000000000000000000000001690565b7fffff0000000000000000000000000000000000000000000000000000000000001690565b90565b8061125d816150ae565b8061125d816150b8565b6fffffffffffffffffffffffffffffffff1690565b6001600160a01b031690565b63ffffffff1690565b6bffffffffffffffffffffffff1690565b60006120bf825b60006120bf82614f98565b60006120bf82614ff5565b60006120bf82614fff565b82818337506000910152565b60005b8381101561509357818101518382015260200161507b565b838111156118925750506000910152565b601f01601f191690565b600981106106b857fe5b600281106106b857fe5b6150cb81614f98565b81146106b857600080fd5b6150cb81614fa3565b600981106106b857600080fd5b600281106106b857600080fd5b600481106106b857600080fd5b6150cb81615009565b6150cb81614ff2565b6150cb8161502a565b6150cb8161503356fea365627a7a72315820c4a4bc4b6deb268035b631577fbfb75627ae00fb5e3c3e97d16c3434fa3abda96c6578706572696d656e74616cf564736f6c63430005100040

Constructor Arguments (ABI-Encoded and is the last bytes of the Contract Creation Code above)

000000000000000000000000dec1ae3b570ac3c57871bbd7bfeacc807f973bea0000000000000000000000006bd780e7fdf01d77e4d475c821f1e7ae0540907200000000000000000000000010d98759762efac656bd4be7f2f5599208f44fac

-----Decoded View---------------
Arg [0] : _expiryProxy (address): 0xDEc1ae3b570ac3c57871BBD7bFeacC807f973Bea
Arg [1] : _dolomiteMargin (address): 0x6Bd780E7fDf01D77e4d475c821f1e7AE05409072
Arg [2] : _liquidatorAssetRegistry (address): 0x10d98759762EFaC656BD4bE7F2f5599208F44FAc

-----Encoded View---------------
3 Constructor Arguments found :
Arg [0] : 000000000000000000000000dec1ae3b570ac3c57871bbd7bfeacc807f973bea
Arg [1] : 0000000000000000000000006bd780e7fdf01d77e4d475c821f1e7ae05409072
Arg [2] : 00000000000000000000000010d98759762efac656bd4be7f2f5599208f44fac


Block Transaction Gas Used Reward
Age Block Fee Address BC Fee Address Voting Power Jailed Incoming
Block Uncle Number Difficulty Gas Used Reward
Loading
Make sure to use the "Vote Down" button for any spammy posts, and the "Vote Up" for interesting conversations.