bZx

Created Diff never expires
/**
/**
*Submitted for verification at Etherscan.io on 2020-09-04
*Submitted for verification at Etherscan.io on 2020-09-13
*/
*/


/**
/**
* Copyright 2017-2020, bZeroX, LLC <https://bzx.network/>. All Rights Reserved.
* Copyright 2017-2020, bZeroX, LLC <https://bzx.network/>. All Rights Reserved.
* Licensed under the Apache License, Version 2.0.
* Licensed under the Apache License, Version 2.0.
*/
*/


pragma solidity 0.5.17;
pragma solidity 0.5.17;
pragma experimental ABIEncoderV2;
pragma experimental ABIEncoderV2;




interface IWeth {
interface IWeth {
function deposit() external payable;
function deposit() external payable;
function withdraw(uint256 wad) external;
function withdraw(uint256 wad) external;
}
}


contract IERC20 {
contract IERC20 {
string public name;
string public name;
uint8 public decimals;
uint8 public decimals;
string public symbol;
string public symbol;
function totalSupply() public view returns (uint256);
function totalSupply() public view returns (uint256);
function balanceOf(address _who) public view returns (uint256);
function balanceOf(address _who) public view returns (uint256);
function allowance(address _owner, address _spender) public view returns (uint256);
function allowance(address _owner, address _spender) public view returns (uint256);
function approve(address _spender, uint256 _value) public returns (bool);
function approve(address _spender, uint256 _value) public returns (bool);
function transfer(address _to, uint256 _value) public returns (bool);
function transfer(address _to, uint256 _value) public returns (bool);
function transferFrom(address _from, address _to, uint256 _value) public returns (bool);
function transferFrom(address _from, address _to, uint256 _value) public returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
}


contract IWethERC20 is IWeth, IERC20 {}
contract IWethERC20 is IWeth, IERC20 {}


/**
/**
* @dev Wrappers over Solidity's arithmetic operations with added overflow
* @dev Wrappers over Solidity's arithmetic operations with added overflow
* checks.
* checks.
*
*
* Arithmetic operations in Solidity wrap on overflow. This can easily result
* Arithmetic operations in Solidity wrap on overflow. This can easily result
* in bugs, because programmers usually assume that an overflow raises an
* in bugs, because programmers usually assume that an overflow raises an
* error, which is the standard behavior in high level programming languages.
* error, which is the standard behavior in high level programming languages.
* `SafeMath` restores this intuition by reverting the transaction when an
* `SafeMath` restores this intuition by reverting the transaction when an
* operation overflows.
* operation overflows.
*
*
* Using this library instead of the unchecked operations eliminates an entire
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
* class of bugs, so it's recommended to use it always.
*/
*/
library SafeMath {
library SafeMath {
/**
/**
* @dev Returns the addition of two unsigned integers, reverting on
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
* overflow.
*
*
* Counterpart to Solidity's `+` operator.
* Counterpart to Solidity's `+` operator.
*
*
* Requirements:
* Requirements:
* - Addition cannot overflow.
* - Addition cannot overflow.
*/
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
require(c >= a, "SafeMath: addition overflow");


return c;
return c;
}
}


/**
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
* overflow (when the result is negative).
*
*
* Counterpart to Solidity's `-` operator.
* Counterpart to Solidity's `-` operator.
*
*
* Requirements:
* Requirements:
* - Subtraction cannot overflow.
* - Subtraction cannot overflow.
*/
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return sub(a, b, "SafeMath: subtraction overflow");
return sub(a, b, "SafeMath: subtraction overflow");
}
}


/**
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
* overflow (when the result is negative).
*
*
* Counterpart to Solidity's `-` operator.
* Counterpart to Solidity's `-` operator.
*
*
* Requirements:
* Requirements:
* - Subtraction cannot overflow.
* - Subtraction cannot overflow.
*
*
* _Available since v2.4.0._
* _Available since v2.4.0._
*/
*/
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
require(b <= a, errorMessage);
uint256 c = a - b;
uint256 c = a - b;


return c;
return c;
}
}


/**
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
* overflow.
*
*
* Counterpart to Solidity's `*` operator.
* Counterpart to Solidity's `*` operator.
*
*
* Requirements:
* Requirements:
* - Multiplication cannot overflow.
* - Multiplication cannot overflow.
*/
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) {
if (a == 0) {
return 0;
return 0;
}
}


uint256 c = a * b;
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
require(c / a == b, "SafeMath: multiplication overflow");


return c;
return c;
}
}


/**
/**
* @dev Returns the integer division of two unsigned integers. Reverts on
* @dev Returns the integer division of two unsigned integers. Reverts on
* division by zero. The result is rounded towards zero.
* division by zero. The result is rounded towards zero.
*
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
* uses an invalid opcode to revert (consuming all remaining gas).
*
*
* Requirements:
* Requirements:
* - The divisor cannot be zero.
* - The divisor cannot be zero.
*/
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return div(a, b, "SafeMath: division by zero");
return div(a, b, "SafeMath: division by zero");
}
}


/**
/**
* @dev Returns the integer division of two unsigned integers. Reverts with custom message on
* @dev Returns the integer division of two unsigned integers. Reverts with custom message on
* division by zero. The result is rounded towards zero.
* division by zero. The result is rounded towards zero.
*
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
* uses an invalid opcode to revert (consuming all remaining gas).
*
*
* Requirements:
* Requirements:
* - The divisor cannot be zero.
* - The divisor cannot be zero.
*
*
* _Available since v2.4.0._
* _Available since v2.4.0._
*/
*/
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
// Solidity only automatically asserts when dividing by 0
// Solidity only automatically asserts when dividing by 0
require(b != 0, errorMessage);
require(b != 0, errorMessage);
uint256 c = a / b;
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
// assert(a == b * c + a % b); // There is no case in which this doesn't hold


return c;
return c;
}
}


/**
/**
* @dev Integer division of two numbers, rounding up and truncating the quotient
* @dev Integer division of two numbers, rounding up and truncating the quotient
*/
*/
function divCeil(uint256 a, uint256 b) internal pure returns (uint256) {
function divCeil(uint256 a, uint256 b) internal pure returns (uint256) {
return divCeil(a, b, "SafeMath: division by zero");
return divCeil(a, b, "SafeMath: division by zero");
}
}


/**
/**
* @dev Integer division of two numbers, rounding up and truncating the quotient
* @dev Integer division of two numbers, rounding up and truncating the quotient
*/
*/
function divCeil(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
function divCeil(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
// Solidity only automatically asserts when dividing by 0
// Solidity only automatically asserts when dividing by 0
require(b != 0, errorMessage);
require(b != 0, errorMessage);


if (a == 0) {
if (a == 0) {
return 0;
return 0;
}
}
uint256 c = ((a - 1) / b) + 1;
uint256 c = ((a - 1) / b) + 1;


return c;
return c;
}
}


/**
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts when dividing by zero.
* Reverts when dividing by zero.
*
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
* invalid opcode to revert (consuming all remaining gas).
*
*
* Requirements:
* Requirements:
* - The divisor cannot be zero.
* - The divisor cannot be zero.
*/
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return mod(a, b, "SafeMath: modulo by zero");
return mod(a, b, "SafeMath: modulo by zero");
}
}


/**
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts with custom message when dividing by zero.
* Reverts with custom message when dividing by zero.
*
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
* invalid opcode to revert (consuming all remaining gas).
*
*
* Requirements:
* Requirements:
* - The divisor cannot be zero.
* - The divisor cannot be zero.
*
*
* _Available since v2.4.0._
* _Available since v2.4.0._
*/
*/
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b != 0, errorMessage);
require(b != 0, errorMessage);
return a % b;
return a % b;
}
}


function min256(uint256 _a, uint256 _b) internal pure returns (uint256) {
function min256(uint256 _a, uint256 _b) internal pure returns (uint256) {
return _a < _b ? _a : _b;
return _a < _b ? _a : _b;
}
}
}
}


/**
/**
* @title SignedSafeMath
* @title SignedSafeMath
* @dev Signed math operations with safety checks that revert on error.
* @dev Signed math operations with safety checks that revert on error.
*/
*/
library SignedSafeMath {
library SignedSafeMath {
int256 constant private _INT256_MIN = -2**255;
int256 constant private _INT256_MIN = -2**255;


/**
/**
* @dev Returns the multiplication of two signed integers, reverting on
* @dev Returns the multiplication of two signed integers, reverting on
* overflow.
* overflow.
*
*
* Counterpart to Solidity's `*` operator.
* Counterpart to Solidity's `*` operator.
*
*
* Requirements:
* Requirements:
*
*
* - Multiplication cannot overflow.
* - Multiplication cannot overflow.
*/
*/
function mul(int256 a, int256 b) internal pure returns (int256) {
function mul(int256 a, int256 b) internal pure returns (int256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) {
if (a == 0) {
return 0;
return 0;
}
}


require(!(a == -1 && b == _INT256_MIN), "SignedSafeMath: multiplication overflow");
require(!(a == -1 && b == _INT256_MIN), "SignedSafeMath: multiplication overflow");


int256 c = a * b;
int256 c = a * b;
require(c / a == b, "SignedSafeMath: multiplication overflow");
require(c / a == b, "SignedSafeMath: multiplication overflow");


return c;
return c;
}
}


/**
/**
* @dev Returns the integer division of two signed integers. Reverts on
* @dev Returns the integer division of two signed integers. Reverts on
* division by zero. The result is rounded towards zero.
* division by zero. The result is rounded towards zero.
*
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
* uses an invalid opcode to revert (consuming all remaining gas).
*
*
* Requirements:
* Requirements:
*
*
* - The divisor cannot be zero.
* - The divisor cannot be zero.
*/
*/
function div(int256 a, int256 b) internal pure returns (int256) {
function div(int256 a, int256 b) internal pure returns (int256) {
require(b != 0, "SignedSafeMath: division by zero");
require(b != 0, "SignedSafeMath: division by zero");
require(!(b == -1 && a == _INT256_MIN), "SignedSafeMath: division overflow");
require(!(b == -1 && a == _INT256_MIN), "SignedSafeMath: division overflow");


int256 c = a / b;
int256 c = a / b;


return c;
return c;
}
}


/**
/**
* @dev Returns the subtraction of two signed integers, reverting on
* @dev Returns the subtraction of two signed integers, reverting on
* overflow.
* overflow.
*
*
* Counterpart to Solidity's `-` operator.
* Counterpart to Solidity's `-` operator.
*
*
* Requirements:
* Requirements:
*
*
* - Subtraction cannot overflow.
* - Subtraction cannot overflow.
*/
*/
function sub(int256 a, int256 b) internal pure returns (int256) {
function sub(int256 a, int256 b) internal pure returns (int256) {
int256 c = a - b;
int256 c = a - b;
require((b >= 0 && c <= a) || (b < 0 && c > a), "SignedSafeMath: subtraction overflow");
require((b >= 0 && c <= a) || (b < 0 && c > a), "SignedSafeMath: subtraction overflow");


return c;
return c;
}
}


/**
/**
* @dev Returns the addition of two signed integers, reverting on
* @dev Returns the addition of two signed integers, reverting on
* overflow.
* overflow.
*
*
* Counterpart to Solidity's `+` operator.
* Counterpart to Solidity's `+` operator.
*
*
* Requirements:
* Requirements:
*
*
* - Addition cannot overflow.
* - Addition cannot overflow.
*/
*/
function add(int256 a, int256 b) internal pure returns (int256) {
function add(int256 a, int256 b) internal pure returns (int256) {
int256 c = a + b;
int256 c = a + b;
require((b >= 0 && c >= a) || (b < 0 && c < a), "SignedSafeMath: addition overflow");
require((b >= 0 && c >= a) || (b < 0 && c < a), "SignedSafeMath: addition overflow");


return c;
return c;
}
}
}
}


/**
/**
* @title Helps contracts guard against reentrancy attacks.
* @title Helps contracts guard against reentrancy attacks.
* @author Remco Bloemen <remco@2π.com>, Eenae <alexey@mixbytes.io>
* @author Remco Bloemen <remco@2π.com>, Eenae <alexey@mixbytes.io>
* @dev If you mark a function `nonReentrant`, you should also
* @dev If you mark a function `nonReentrant`, you should also
* mark it `external`.
* mark it `external`.
*/
*/
contract ReentrancyGuard {
contract ReentrancyGuard {


/// @dev Constant for unlocked guard state - non-zero to prevent extra gas costs.
/// @dev Constant for unlocked guard state - non-zero to prevent extra gas costs.
/// See: https://github.com/OpenZeppelin/openzeppelin-solidity/issues/1056
/// See: https://github.com/OpenZeppelin/openzeppelin-solidity/issues/1056
uint256 internal constant REENTRANCY_GUARD_FREE = 1;
uint256 internal constant REENTRANCY_GUARD_FREE = 1;


/// @dev Constant for locked guard state
/// @dev Constant for locked guard state
uint256 internal constant REENTRANCY_GUARD_LOCKED = 2;
uint256 internal constant REENTRANCY_GUARD_LOCKED = 2;


/**
/**
* @dev We use a single lock for the whole contract.
* @dev We use a single lock for the whole contract.
*/
*/
uint256 internal reentrancyLock = REENTRANCY_GUARD_FREE;
uint256 internal reentrancyLock = REENTRANCY_GUARD_FREE;


/**
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* @dev Prevents a contract from calling itself, directly or indirectly.
* If you mark a function `nonReentrant`, you should also
* If you mark a function `nonReentrant`, you should also
* mark it `external`. Calling one `nonReentrant` function from
* mark it `external`. Calling one `nonReentrant` function from
* another is not supported. Instead, you can implement a
* another is not supported. Instead, you can implement a
* `private` function doing the actual work, and an `external`
* `private` function doing the actual work, and an `external`
* wrapper marked as `nonReentrant`.
* wrapper marked as `nonReentrant`.
*/
*/
modifier nonReentrant() {
modifier nonReentrant() {
require(reentrancyLock == REENTRANCY_GUARD_FREE, "nonReentrant");
require(reentrancyLock == REENTRANCY_GUARD_FREE, "nonReentrant");
reentrancyLock = REENTRANCY_GUARD_LOCKED;
reentrancyLock = REENTRANCY_GUARD_LOCKED;
_;
_;
reentrancyLock = REENTRANCY_GUARD_FREE;
reentrancyLock = REENTRANCY_GUARD_FREE;
}
}
}
}


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


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


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


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


/*
/*
* @dev Provides information about the current execution context, including the
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with GSN meta-transactions the account sending and
* manner, since when dealing with GSN meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* paying for execution may not be the actual sender (as far as an application
* is concerned).
* is concerned).
*
*
* This contract is only required for intermediate, library-like contracts.
* This contract is only required for intermediate, library-like contracts.
*/
*/
contract Context {
contract Context {
// Empty internal constructor, to prevent people from mistakenly deploying
// Empty internal constructor, to prevent people from mistakenly deploying
// an instance of this contract, which should be used via inheritance.
// an instance of this contract, which should be used via inheritance.
constructor () internal { }
constructor () internal { }
// solhint-disable-previous-line no-empty-blocks
// solhint-disable-previous-line no-empty-blocks


function _msgSender() internal view returns (address payable) {
function _msgSender() internal view returns (address payable) {
return msg.sender;
return msg.sender;
}
}


function _msgData() internal view returns (bytes memory) {
function _msgData() internal view returns (bytes memory) {
this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
return msg.data;
return msg.data;
}
}
}
}


/**
/**
* @dev Contract module which provides a basic access control mechanism, where
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
* specific functions.
*
*
* This module is used through inheritance. It will make available the modifier
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
* the owner.
*/
*/
contract Ownable is Context {
contract Ownable is Context {
address private _owner;
address private _owner;


event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);


/**
/**
* @dev Initializes the contract setting the deployer as the initial owner.
* @dev Initializes the contract setting the deployer as the initial owner.
*/
*/
constructor () internal {
constructor () internal {
address msgSender = _msgSender();
address msgSender = _msgSender();
_owner = msgSender;
_owner = msgSender;
emit OwnershipTransferred(address(0), msgSender);
emit OwnershipTransferred(address(0), msgSender);
}
}


/**
/**
* @dev Returns the address of the current owner.
* @dev Returns the address of the current owner.
*/
*/
function owner() public view returns (address) {
function owner() public view returns (address) {
return _owner;
return _owner;
}
}


/**
/**
* @dev Throws if called by any account other than the owner.
* @dev Throws if called by any account other than the owner.
*/
*/
modifier onlyOwner() {
modifier onlyOwner() {
require(isOwner(), "unauthorized");
require(isOwner(), "unauthorized");
_;
_;
}
}


/**
/**
* @dev Returns true if the caller is the current owner.
* @dev Returns true if the caller is the current owner.
*/
*/
function isOwner() public view returns (bool) {
function isOwner() public view returns (bool) {
return _msgSender() == _owner;
return _msgSender() == _owner;
}
}


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


/**
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* @dev Transfers ownership of the contract to a new account (`newOwner`).
*/
*/
function _transferOwnership(address newOwner) internal {
function _transferOwnership(address newOwner) internal {
require(newOwner != address(0), "Ownable: new owner is the zero address");
require(newOwner != address(0), "Ownable: new owner is the zero address");
emit OwnershipTransferred(_owner, newOwner);
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
_owner = newOwner;
}
}
}
}


interface ProtocolLike {
interface ProtocolLike {
function borrowOrTradeFromPool(
function borrowOrTradeFromPool(
bytes32 loanParamsId,
bytes32 loanParamsId,
bytes32 loanId, // if 0, start a new loan
bytes32 loanId, // if 0, start a new loan
bool isTorqueLoan,
bool isTorqueLoan,
uint256 initialMargin,
uint256 initialMargin,
address[4] calldata sentAddresses,
address[4] calldata sentAddresses,
// lender: must match loan if loanId provided
// lender: must match loan if loanId provided
// borrower: must match loan if loanId provided
// borrower: must match loan if loanId provided
// receiver: receiver of funds (address(0) assumes borrower address)
// receiver: receiver of funds (address(0) assumes borrower address)
// manager: delegated manager of loan unless address(0)
// manager: delegated manager of loan unless address(0)
uint256[5] calldata sentValues,
uint256[5] calldata sentValues,
// newRate: new loan interest rate
// newRate: new loan interest rate
// newPrincipal: new loan size (borrowAmount + any borrowed interest)
// newPrincipal: new loan size (borrowAmount + any borrowed interest)
// torqueInterest: new amount of interest to escrow for Torque loan (determines initial loan length)
// torqueInterest: new amount of interest to escrow for Torque loan (determines initial loan length)
// loanTokenReceived: total loanToken deposit (amount not sent to borrower in the case of Torque loans)
// loanTokenReceived: total loanToken deposit (amount not sent to borrower in the case of Torque loans)
// collateralTokenReceived: total collateralToken deposit
// collateralTokenReceived: total collateralToken deposit
bytes calldata loanDataBytes)
bytes calldata loanDataBytes)
external
external
payable
payable
returns (uint256 newPrincipal, uint256 newCollateral);
returns (uint256 newPrincipal, uint256 newCollateral);


function getTotalPrincipal(
function getTotalPrincipal(
address lender,
address lender,
address loanToken)
address loanToken)
external
external
view
view
returns (uint256);
returns (uint256);


function withdrawAccruedInterest(
function withdrawAccruedInterest(
address loanToken)
address loanToken)
external;
external;


function getLenderInterestData(
function getLenderInterestData(
address lender,
address lender,
address loanToken)
address loanToken)
external
external
view
view
returns (
returns (
uint256 interestPaid,
uint256 interestPaid,
uint256 interestPaidDate,
uint256 interestPaidDate,
uint256 interestOwedPerDay,
uint256 interestOwedPerDay,
uint256 interestUnPaid,
uint256 interestUnPaid,
uint256 interestFeePercent,
uint256 interestFeePercent,
uint256 principalTotal);
uint256 principalTotal);


function priceFeeds()
function priceFeeds()
external
external
view
view
returns (address);
returns (address);


function getEstimatedMarginExposure(
function getEstimatedMarginExposure(
address loanToken,
address loanToken,
address collateralToken,
address collateralToken,
uint256 loanTokenSent,
uint256 loanTokenSent,
uint256 collateralTokenSent,
uint256 collateralTokenSent,
uint256 interestRate,
uint256 interestRate,
uint256 newPrincipal)
uint256 newPrincipal)
external
external
view
view
returns (uint256);
returns (uint256);


function getRequiredCollateralByParams(
function getRequiredCollateralByParams(
bytes32 loanParamsId,
bytes32 loanParamsId,
address loanToken,
address loanToken,
address collateralToken,
address collateralToken,
uint256 newPrincipal,
uint256 newPrincipal,
bool isTorqueLoan)
bool isTorqueLoan)
external
external
view
view
returns (uint256 collateralAmountRequired);
returns (uint256 collateralAmountRequired);


function getBorrowAmountByParams(
function getBorrowAmountByParams(
bytes32 loanParamsId,
bytes32 loanParamsId,
address loanToken,
address loanToken,
address collateralToken,
address collateralToken,
uint256 collateralTokenAmount,
uint256 collateralTokenAmount,
bool isTorqueLoan)
bool isTorqueLoan)
external
external
view
view
returns (uint256 borrowAmount);
returns (uint256 borrowAmount);


function isLoanPool(
function isLoanPool(
address loanPool)
address loanPool)
external
external
view
view
returns (bool);
returns (bool);


function lendingFeePercent()
function lendingFeePercent()
external
external
view
view
returns (uint256);
returns (uint256);
}
}


interface FeedsLike {
interface FeedsLike {
function queryRate(
function queryRate(
address sourceTokenAddress,
address sourceTokenAddress,
address destTokenAddress)
address destTokenAddress)
external
external
view
view
returns (uint256 rate, uint256 precision);
returns (uint256 rate, uint256 precision);
}
}


contract ITokenHolderLike {
contract ITokenHolderLike {
function balanceOf(address _who) public view returns (uint256);
function balanceOf(address _who) public view returns (uint256);
function freeUpTo(uint256 value) public returns (uint256);
function freeUpTo(uint256 value) public returns (uint256);
function freeFromUpTo(address from, uint256 value) public returns (uint256);
function freeFromUpTo(address from, uint256 value) public returns (uint256);
}
}


contract GasTokenUser {
contract GasTokenUser {


ITokenHolderLike constant public gasToken = ITokenHolderLike(0x0000000000004946c0e9F43F4Dee607b0eF1fA1c);
ITokenHolderLike constant public gasToken = ITokenHolderLike(0x0000000000004946c0e9F43F4Dee607b0eF1fA1c);
ITokenHolderLike constant public tokenHolder = ITokenHolderLike(0x55Eb3DD3f738cfdda986B8Eff3fa784477552C61);
ITokenHolderLike constant public tokenHolder = ITokenHolderLike(0x55Eb3DD3f738cfdda986B8Eff3fa784477552C61);


modifier usesGasToken(address holder) {
modifier usesGasToken(address holder) {
if (holder == address(0)) {
if (holder == address(0)) {
holder = address(tokenHolder);
holder = address(tokenHolder);
}
}


if (gasToken.balanceOf(holder) != 0) {
if (gasToken.balanceOf(holder) != 0) {
uint256 gasCalcValue = gasleft();
uint256 gasCalcValue = gasleft();


_;
_;


gasCalcValue = (_gasUsed(gasCalcValue) + 14154) / 41947;
gasCalcValue = (_gasUsed(gasCalcValue) + 14154) / 41947;


if (holder == address(tokenHolder)) {
if (holder == address(tokenHolder)) {
tokenHolder.freeUpTo(
tokenHolder.freeUpTo(
gasCalcValue
gasCalcValue
);
);
} else {
} else {
tokenHolder.freeFromUpTo(
tokenHolder.freeFromUpTo(
holder,
holder,
gasCalcValue
gasCalcValue
);
);
}
}


} else {
} else {
_;
_;
}
}
}
}


function _gasUsed(
function _gasUsed(
uint256 startingGas)
uint256 startingGas)
internal
internal
view
view
returns (uint256)
returns (uint256)
{
{
return 21000 +
return 21000 +
startingGas -
startingGas -
gasleft() +
gasleft() +
16 *
16 *
msg.data.length;
msg.data.length;


}
}
}
}


contract Pausable {
contract Pausable {


// keccak256("Pausable_FunctionPause")
// keccak256("Pausable_FunctionPause")
bytes32 internal constant Pausable_FunctionPause = 0xa7143c84d793a15503da6f19bf9119a2dac94448ca45d77c8bf08f57b2e91047;
bytes32 internal constant Pausable_FunctionPause = 0xa7143c84d793a15503da6f19bf9119a2dac94448ca45d77c8bf08f57b2e91047;


modifier pausable(bytes4 sig) {
modifier pausable(bytes4 sig) {
require(!_isPaused(sig), "unauthorized");
require(!_isPaused(sig), "unauthorized");
_;
_;
}
}


function _isPaused(
function _isPaused(
bytes4 sig)
bytes4 sig)
internal
internal
view
view
returns (bool isPaused)
returns (bool isPaused)
{
{
bytes32 slot = keccak256(abi.encodePacked(sig, Pausable_FunctionPause));
bytes32 slot = keccak256(abi.encodePacked(sig, Pausable_FunctionPause));
assembly {
assembly {
isPaused := sload(slot)
isPaused := sload(slot)
}
}
}
}
}
}


contract LoanTokenBase is ReentrancyGuard, Ownable, Pausable {
contract LoanTokenBase is ReentrancyGuard, Ownable, Pausable {


uint256 internal constant WEI_PRECISION = 10**18;
uint256 internal constant WEI_PRECISION = 10**18;
uint256 internal constant WEI_PERCENT_PRECISION = 10**20;
uint256 internal constant WEI_PERCENT_PRECISION = 10**20;


int256 internal constant sWEI_PRECISION = 10**18;
int256 internal constant sWEI_PRECISION = 10**18;


string public name;
string public name;
string public symbol;
string public symbol;
uint8 public decimals;
uint8 public decimals;


// uint88 for tight packing -> 8 + 88 + 160 = 256
// uint88 for tight packing -> 8 + 88 + 160 = 256
uint88 internal lastSettleTime_;
uint88 internal lastSettleTime_;


address public loanTokenAddress;
address public loanTokenAddress;


uint256 public baseRate;
uint256 public baseRate;
uint256 public rateMultiplier;
uint256 public rateMultiplier;
uint256 public lowUtilBaseRate;
uint256 public lowUtilBaseRate;
uint256 public lowUtilRateMultiplier;
uint256 public lowUtilRateMultiplier;


uint256 public targetLevel;
uint256 public targetLevel;
uint256 public kinkLevel;
uint256 public kinkLevel;
uint256 public maxScaleRate;
uint256 public maxScaleRate;


uint256 internal _flTotalAssetSupply;
uint256 internal _flTotalAssetSupply;
uint256 public checkpointSupply;
uint256 public checkpointSupply;
uint256 public initialPrice;
uint256 public initialPrice;


mapping (uint256 => bytes32) public loanParamsIds; // mapping of keccak256(collateralToken, isTorqueLoan) to loanParamsId
mapping (uint256 => bytes32) public loanParamsIds; // mapping of keccak256(collateralToken, isTorqueLoan) to loanParamsId
mapping (address => uint256) internal checkpointPrices_; // price of token at last user checkpoint
mapping (address => uint256) internal checkpointPrices_; // price of token at last user checkpoint
}
}


contract AdvancedTokenStorage is LoanTokenBase {
contract AdvancedTokenStorage is LoanTokenBase {
using SafeMath for uint256;
using SafeMath for uint256;


event Transfer(
event Transfer(
address indexed from,
address indexed from,
address indexed to,
address indexed to,
uint256 value
uint256 value
);
);


event Approval(
event Approval(
address indexed owner,
address indexed owner,
address indexed spender,
address indexed spender,
uint256 value
uint256 value
);
);


event Mint(
event Mint(
address indexed minter,
address indexed minter,
uint256 tokenAmount,
uint256 tokenAmount,
uint256 assetAmount,
uint256 assetAmount,
uint256 price
uint256 price
);
);


event Burn(
event Burn(
address indexed burner,
address indexed burner,
uint256 tokenAmount,
uint256 tokenAmount,
uint256 assetAmount,
uint256 assetAmount,
uint256 price
uint256 price
);
);


mapping(address => uint256) internal balances;
mapping(address => uint256) internal balances;
mapping (address => mapping (address => uint256)) internal allowed;
mapping (address => mapping (address => uint256)) internal allowed;
uint256 internal totalSupply_;
uint256 internal totalSupply_;


function totalSupply()
function totalSupply()
public
public
view
view
returns (uint256)
returns (uint256)
{
{
return totalSupply_;
return totalSupply_;
}
}


function balanceOf(
function balanceOf(
address _owner)
address _owner)
public
public
view
view
returns (uint256)
returns (uint256)
{
{
return balances[_owner];
return balances[_owner];
}
}


function allowance(
function allowance(
address _owner,
address _owner,
address _spender)
address _spender)
public
public
view
view
returns (uint256)
returns (uint256)
{
{
return allowed[_owner][_spender];
return allowed[_owner][_spender];
}
}
}
}


contract AdvancedToken is AdvancedTokenStorage {
contract AdvancedToken is AdvancedTokenStorage {
using SafeMath for uint256;
using SafeMath for uint256;


function approve(
function approve(
address _spender,
address _spender,
uint256 _value)
uint256 _value)
public
public
returns (bool)
returns (bool)
{
{
allowed[msg.sender][_spender] = _value;
allowed[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
emit Approval(msg.sender, _spender, _value);
return true;
return true;
}
}


function increaseApproval(
function increaseApproval(
address _spender,
address _spender,
uint256 _addedValue)
uint256 _addedValue)
public
public
returns (bool)
returns (bool)
{
{
uint256 _allowed = allowed[msg.sender][_spender]
uint256 _allowed = allowed[msg.sender][_spender]
.add(_addedValue);
.add(_addedValue);
allowed[msg.sender][_spender] = _allowed;
allowed[msg.sender][_spender] = _allowed;


emit Approval(msg.sender, _spender, _allowed);
emit Approval(msg.sender, _spender, _allowed);
return true;
return true;
}
}


function decreaseApproval(
function decreaseApproval(
address _spender,
address _spender,
uint256 _subtractedValue)
uint256 _subtractedValue)
public
public
returns (bool)
returns (bool)
{
{
uint2
uint2