EIP-8

Created Diff never expires
/**
/**
*Submitted for verification at Etherscan.io on 2020-11-10
*Submitted for verification at Etherscan.io on 2020-11-10
*/
*/


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




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


/*
/*
Copyright 2019 dYdX Trading Inc.
Copyright 2019 dYdX Trading Inc.
Copyright 2020 Empty Set Squad <emptysetsquad@protonmail.com>
Copyright 2020 Empty Set Squad <emptysetsquad@protonmail.com>


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


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


Unless required by applicable law or agreed to in writing, software
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
*/
*/
/**
/**
* @title Decimal
* @title Decimal
* @author dYdX
* @author dYdX
*
*
* Library that defines a fixed-point number with 18 decimal places.
* Library that defines a fixed-point number with 18 decimal places.
*/
*/
library Decimal {
library Decimal {
using SafeMath for uint256;
using SafeMath for uint256;


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


uint256 constant BASE = 10**18;
uint256 constant BASE = 10**18;


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




struct D256 {
struct D256 {
uint256 value;
uint256 value;
}
}


// ============ Static Functions ============
// ============ Static Functions ============


function zero()
function zero()
internal
internal
pure
pure
returns (D256 memory)
returns (D256 memory)
{
{
return D256({ value: 0 });
return D256({ value: 0 });
}
}


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


function from(
function from(
uint256 a
uint256 a
)
)
internal
internal
pure
pure
returns (D256 memory)
returns (D256 memory)
{
{
return D256({ value: a.mul(BASE) });
return D256({ value: a.mul(BASE) });
}
}


function ratio(
function ratio(
uint256 a,
uint256 a,
uint256 b
uint256 b
)
)
internal
internal
pure
pure
returns (D256 memory)
returns (D256 memory)
{
{
return D256({ value: getPartial(a, BASE, b) });
return D256({ value: getPartial(a, BASE, b) });
}
}


// ============ Self Functions ============
// ============ Self Functions ============


function add(
function add(
D256 memory self,
D256 memory self,
uint256 b
uint256 b
)
)
internal
internal
pure
pure
returns (D256 memory)
returns (D256 memory)
{
{
return D256({ value: self.value.add(b.mul(BASE)) });
return D256({ value: self.value.add(b.mul(BASE)) });
}
}


function sub(
function sub(
D256 memory self,
D256 memory self,
uint256 b
uint256 b
)
)
internal
internal
pure
pure
returns (D256 memory)
returns (D256 memory)
{
{
return D256({ value: self.value.sub(b.mul(BASE)) });
return D256({ value: self.value.sub(b.mul(BASE)) });
}
}


function sub(
function sub(
D256 memory self,
D256 memory self,
uint256 b,
uint256 b,
string memory reason
string memory reason
)
)
internal
internal
pure
pure
returns (D256 memory)
returns (D256 memory)
{
{
return D256({ value: self.value.sub(b.mul(BASE), reason) });
return D256({ value: self.value.sub(b.mul(BASE), reason) });
}
}


function mul(
function mul(
D256 memory self,
D256 memory self,
uint256 b
uint256 b
)
)
internal
internal
pure
pure
returns (D256 memory)
returns (D256 memory)
{
{
return D256({ value: self.value.mul(b) });
return D256({ value: self.value.mul(b) });
}
}


function div(
function div(
D256 memory self,
D256 memory self,
uint256 b
uint256 b
)
)
internal
internal
pure
pure
returns (D256 memory)
returns (D256 memory)
{
{
return D256({ value: self.value.div(b) });
return D256({ value: self.value.div(b) });
}
}


function pow(
function pow(
D256 memory self,
D256 memory self,
uint256 b
uint256 b
)
)
internal
internal
pure
pure
returns (D256 memory)
returns (D256 memory)
{
{
if (b == 0) {
if (b == 0) {
return from(1);
return from(1);
}
}


D256 memory temp = D256({ value: self.value });
D256 memory temp = D256({ value: self.value });
for (uint256 i = 1; i < b; i++) {
for (uint256 i = 1; i < b; i++) {
temp = mul(temp, self);
temp = mul(temp, self);
}
}


return temp;
return temp;
}
}


function add(
function add(
D256 memory self,
D256 memory self,
D256 memory b
D256 memory b
)
)
internal
internal
pure
pure
returns (D256 memory)
returns (D256 memory)
{
{
return D256({ value: self.value.add(b.value) });
return D256({ value: self.value.add(b.value) });
}
}


function sub(
function sub(
D256 memory self,
D256 memory self,
D256 memory b
D256 memory b
)
)
internal
internal
pure
pure
returns (D256 memory)
returns (D256 memory)
{
{
return D256({ value: self.value.sub(b.value) });
return D256({ value: self.value.sub(b.value) });
}
}


function sub(
function sub(
D256 memory self,
D256 memory self,
D256 memory b,
D256 memory b,
string memory reason
string memory reason
)
)
internal
internal
pure
pure
returns (D256 memory)
returns (D256 memory)
{
{
return D256({ value: self.value.sub(b.value, reason) });
return D256({ value: self.value.sub(b.value, reason) });
}
}


function mul(
function mul(
D256 memory self,
D256 memory self,
D256 memory b
D256 memory b
)
)
internal
internal
pure
pure
returns (D256 memory)
returns (D256 memory)
{
{
return D256({ value: getPartial(self.value, b.value, BASE) });
return D256({ value: getPartial(self.value, b.value, BASE) });
}
}


function div(
function div(
D256 memory self,
D256 memory self,
D256 memory b
D256 memory b
)
)
internal
internal
pure
pure
returns (D256 memory)
returns (D256 memory)
{
{
return D256({ value: getPartial(self.value, BASE, b.value) });
return D256({ value: getPartial(self.value, BASE, b.value) });
}
}


function equals(D256 memory self, D256 memory b) internal pure returns (bool) {
function equals(D256 memory self, D256 memory b) internal pure returns (bool) {
return self.value == b.value;
return self.value == b.value;
}
}


function greaterThan(D256 memory self, D256 memory b) internal pure returns (bool) {
function greaterThan(D256 memory self, D256 memory b) internal pure returns (bool) {
return compareTo(self, b) == 2;
return compareTo(self, b) == 2;
}
}


function lessThan(D256 memory self, D256 memory b) internal pure returns (bool) {
function lessThan(D256 memory self, D256 memory b) internal pure returns (bool) {
return compareTo(self, b) == 0;
return compareTo(self, b) == 0;
}
}


function greaterThanOrEqualTo(D256 memory self, D256 memory b) internal pure returns (bool) {
function greaterThanOrEqualTo(D256 memory self, D256 memory b) internal pure returns (bool) {
return compareTo(self, b) > 0;
return compareTo(self, b) > 0;
}
}


function lessThanOrEqualTo(D256 memory self, D256 memory b) internal pure returns (bool) {
function lessThanOrEqualTo(D256 memory self, D256 memory b) internal pure returns (bool) {
return compareTo(self, b) < 2;
return compareTo(self, b) < 2;
}
}


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


function asUint256(D256 memory self) internal pure returns (uint256) {
function asUint256(D256 memory self) internal pure returns (uint256) {
return self.value.div(BASE);
return self.value.div(BASE);
}
}


// ============ Core Methods ============
// ============ Core Methods ============


function getPartial(
function getPartial(
uint256 target,
uint256 target,
uint256 numerator,
uint256 numerator,
uint256 denominator
uint256 denominator
)
)
private
private
pure
pure
returns (uint256)
returns (uint256)
{
{
return target.mul(numerator).div(denominator);
return target.mul(numerator).div(denominator);
}
}


function compareTo(
function compareTo(
D256 memory a,
D256 memory a,
D256 memory b
D256 memory b
)
)
private
private
pure
pure
returns (uint256)
returns (uint256)
{
{
if (a.value == b.value) {
if (a.value == b.value) {
return 1;
return 1;
}
}
return a.value > b.value ? 2 : 0;
return a.value > b.value ? 2 : 0;
}
}
}
}


/*
/*
Copyright 2020 Empty Set Squad <emptysetsquad@protonmail.com>
Copyright 2020 Empty Set Squad <emptysetsquad@protonmail.com>


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


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


Unless required by applicable law or agreed to in writing, software
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
*/
*/
library Constants {
library Constants {
/* Chain */
/* Chain */
uint256 private constant CHAIN_ID = 1; // Mainnet
uint256 private constant CHAIN_ID = 1; // Mainnet


/* Bootstrapping */
/* Bootstrapping */
uint256 private constant BOOTSTRAPPING_PERIOD = 90;
uint256 private constant BOOTSTRAPPING_PERIOD = 90;
uint256 private constant BOOTSTRAPPING_PRICE = 11e17; // 1.10 USDC
uint256 private constant BOOTSTRAPPING_PRICE = 11e17; // 1.10 USDC
uint256 private constant BOOTSTRAPPING_SPEEDUP_FACTOR = 3; // 30 days @ 8 hours
uint256 private constant BOOTSTRAPPING_SPEEDUP_FACTOR = 3; // 30 days @ 8 hours


/* Oracle */
/* Oracle */
address private constant USDC = address(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48);
address private constant USDC = address(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48);
uint256 private constant ORACLE_RESERVE_MINIMUM = 1e10; // 10,000 USDC
uint256 private constant ORACLE_RESERVE_MINIMUM = 1e10; // 10,000 USDC


/* Bonding */
/* Bonding */
uint256 private constant INITIAL_STAKE_MULTIPLE = 1e6; // 100 ESD -> 100M ESDS
uint256 private constant INITIAL_STAKE_MULTIPLE = 1e6; // 100 ESD -> 100M ESDS


/* Epoch */
/* Epoch */
struct EpochStrategy {
struct EpochStrategy {
uint256 offset;
uint256 offset;
uint256 start;
uint256 start;
uint256 period;
uint256 period;
}
}


uint256 private constant PREVIOUS_EPOCH_OFFSET = 91;
uint256 private constant PREVIOUS_EPOCH_OFFSET = 91;
uint256 private constant PREVIOUS_EPOCH_START = 1600905600;
uint256 private constant PREVIOUS_EPOCH_START = 1600905600;
uint256 private constant PREVIOUS_EPOCH_PERIOD = 86400;
uint256 private constant PREVIOUS_EPOCH_PERIOD = 86400;


uint256 private constant CURRENT_EPOCH_OFFSET = 106;
uint256 private constant CURRENT_EPOCH_OFFSET = 106;
uint256 private constant CURRENT_EPOCH_START = 1602201600;
uint256 private constant CURRENT_EPOCH_START = 1602201600;
uint256 private constant CURRENT_EPOCH_PERIOD = 28800;
uint256 private constant CURRENT_EPOCH_PERIOD = 28800;


/* Governance */
/* Governance */
uint256 private constant GOVERNANCE_PERIOD = 9;
uint256 private constant GOVERNANCE_PERIOD = 9;
uint256 private constant GOVERNANCE_QUORUM = 33e16; // 33%
uint256 private constant GOVERNANCE_QUORUM = 20e16; // 20%
uint256 private constant GOVERNANCE_SUPER_MAJORITY = 66e16; // 66%
uint256 private constant GOVERNANCE_SUPER_MAJORITY = 66e16; // 66%
uint256 private constant GOVERNANCE_EMERGENCY_DELAY = 6; // 6 epochs
uint256 private constant GOVERNANCE_EMERGENCY_DELAY = 6; // 6 epochs


/* DAO */
/* DAO */
uint256 private constant ADVANCE_INCENTIVE = 1e20; // 100 ESD
uint256 private constant ADVANCE_INCENTIVE = 1e20; // 100 ESD
uint256 private constant DAO_EXIT_LOCKUP_EPOCHS = 15; // 15 epochs fluid
uint256 private constant DAO_EXIT_LOCKUP_EPOCHS = 15; // 15 epochs fluid


/* Pool */
/* Pool */
uint256 private constant POOL_EXIT_LOCKUP_EPOCHS = 5; // 5 epochs fluid
uint256 private constant POOL_EXIT_LOCKUP_EPOCHS = 5; // 5 epochs fluid


/* Market */
/* Market */
uint256 private constant COUPON_EXPIRATION = 90;
uint256 private constant COUPON_EXPIRATION = 90;
uint256 private constant DEBT_RATIO_CAP = 35e16; // 35%
uint256 private constant DEBT_RATIO_CAP = 35e16; // 35%


/* Regulator */
/* Regulator */
uint256 private constant SUPPLY_CHANGE_LIMIT = 3e16; // 3%
uint256 private constant SUPPLY_CHANGE_LIMIT = 3e16; // 3%
uint256 private constant COUPON_SUPPLY_CHANGE_LIMIT = 6e16; // 6%
uint256 private constant COUPON_SUPPLY_CHANGE_LIMIT = 6e16; // 6%
uint256 private constant ORACLE_POOL_RATIO = 20; // 20%
uint256 private constant ORACLE_POOL_RATIO = 20; // 20%


/* Deployed */
/* Deployed */
address private constant DAO_ADDRESS = address(0x443D2f2755DB5942601fa062Cc248aAA153313D3);
address private constant DAO_ADDRESS = address(0x443D2f2755DB5942601fa062Cc248aAA153313D3);
address private constant DOLLAR_ADDRESS = address(0x36F3FD68E7325a35EB768F1AedaAe9EA0689d723);
address private constant DOLLAR_ADDRESS = address(0x36F3FD68E7325a35EB768F1AedaAe9EA0689d723);
address private constant PAIR_ADDRESS = address(0x88ff79eB2Bc5850F27315415da8685282C7610F9);
address private constant PAIR_ADDRESS = address(0x88ff79eB2Bc5850F27315415da8685282C7610F9);


/* Pool Migration */
/* Pool Migration */
address private constant LEGACY_POOL_ADDRESS = address(0xdF0Ae5504A48ab9f913F8490fBef1b9333A68e68);
address private constant LEGACY_POOL_ADDRESS = address(0xdF0Ae5504A48ab9f913F8490fBef1b9333A68e68);
uint256 private constant LEGACY_POOL_REWARD = 1e18; // 1 ESD
uint256 private constant LEGACY_POOL_REWARD = 1e18; // 1 ESD


/**
/**
* Getters
* Getters
*/
*/


function getUsdcAddress() internal pure returns (address) {
function getUsdcAddress() internal pure returns (address) {
return USDC;
return USDC;
}
}


function getOracleReserveMinimum() internal pure returns (uint256) {
function getOracleReserveMinimum() internal pure returns (uint256) {
return ORACLE_RESERVE_MINIMUM;
return ORACLE_RESERVE_MINIMUM;
}
}


function getPreviousEpochStrategy() internal pure returns (EpochStrategy memory) {
function getPreviousEpochStrategy() internal pure returns (EpochStrategy memory) {
return EpochStrategy({
return EpochStrategy({
offset: PREVIOUS_EPOCH_OFFSET,
offset: PREVIOUS_EPOCH_OFFSET,
start: PREVIOUS_EPOCH_START,
start: PREVIOUS_EPOCH_START,
period: PREVIOUS_EPOCH_PERIOD
period: PREVIOUS_EPOCH_PERIOD
});
});
}
}


function getCurrentEpochStrategy() internal pure returns (EpochStrategy memory) {
function getCurrentEpochStrategy() internal pure returns (EpochStrategy memory) {
return EpochStrategy({
return EpochStrategy({
offset: CURRENT_EPOCH_OFFSET,
offset: CURRENT_EPOCH_OFFSET,
start: CURRENT_EPOCH_START,
start: CURRENT_EPOCH_START,
period: CURRENT_EPOCH_PERIOD
period: CURRENT_EPOCH_PERIOD
});
});
}
}


function getInitialStakeMultiple() internal pure returns (uint256) {
function getInitialStakeMultiple() internal pure returns (uint256) {
return INITIAL_STAKE_MULTIPLE;
return INITIAL_STAKE_MULTIPLE;
}
}


function getBootstrappingPeriod() internal pure returns (uint256) {
function getBootstrappingPeriod() internal pure returns (uint256) {
return BOOTSTRAPPING_PERIOD;
return BOOTSTRAPPING_PERIOD;
}
}


function getBootstrappingPrice() internal pure returns (Decimal.D256 memory) {
function getBootstrappingPrice() internal pure returns (Decimal.D256 memory) {
return Decimal.D256({value: BOOTSTRAPPING_PRICE});
return Decimal.D256({value: BOOTSTRAPPING_PRICE});
}
}


function getBootstrappingSpeedupFactor() internal pure returns (uint256) {
function getBootstrappingSpeedupFactor() internal pure returns (uint256) {
return BOOTSTRAPPING_SPEEDUP_FACTOR;
return BOOTSTRAPPING_SPEEDUP_FACTOR;
}
}


function getGovernancePeriod() internal pure returns (uint256) {
function getGovernancePeriod() internal pure returns (uint256) {
return GOVERNANCE_PERIOD;
return GOVERNANCE_PERIOD;
}
}


function getGovernanceQuorum() internal pure returns (Decimal.D256 memory) {
function getGovernanceQuorum() internal pure returns (Decimal.D256 memory) {
return Decimal.D256({value: GOVERNANCE_QUORUM});
return Decimal.D256({value: GOVERNANCE_QUORUM});
}
}


function getGovernanceSuperMajority() internal pure returns (Decimal.D256 memory) {
function getGovernanceSuperMajority() internal pure returns (Decimal.D256 memory) {
return Decimal.D256({value: GOVERNANCE_SUPER_MAJORITY});
return Decimal.D256({value: GOVERNANCE_SUPER_MAJORITY});
}
}


function getGovernanceEmergencyDelay() internal pure returns (uint256) {
function getGovernanceEmergencyDelay() internal pure returns (uint256) {
return GOVERNANCE_EMERGENCY_DELAY;
return GOVERNANCE_EMERGENCY_DELAY;
}
}


function getAdvanceIncentive() internal pure returns (uint256) {
function getAdvanceIncentive() internal pure returns (uint256) {
return ADVANCE_INCENTIVE;
return ADVANCE_INCENTIVE;
}
}


function getDAOExitLockupEpochs() internal pure returns (uint256) {
function getDAOExitLockupEpochs() internal pure returns (uint256) {
return DAO_EXIT_LOCKUP_EPOCHS;
return DAO_EXIT_LOCKUP_EPOCHS;
}
}


function getPoolExitLockupEpochs() internal pure returns (uint256) {
function getPoolExitLockupEpochs() internal pure returns (uint256) {
return POOL_EXIT_LOCKUP_EPOCHS;
return POOL_EXIT_LOCKUP_EPOCHS;
}
}


function getCouponExpiration() internal pure returns (uint256) {
function getCouponExpiration() internal pure returns (uint256) {
return COUPON_EXPIRATION;
return COUPON_EXPIRATION;
}
}


function getDebtRatioCap() internal pure returns (Decimal.D256 memory) {
function getDebtRatioCap() internal pure returns (Decimal.D256 memory) {
return Decimal.D256({value: DEBT_RATIO_CAP});
return Decimal.D256({value: DEBT_RATIO_CAP});
}
}


function getSupplyChangeLimit() internal pure returns (Decimal.D256 memory) {
function getSupplyChangeLimit() internal pure returns (Decimal.D256 memory) {
return Decimal.D256({value: SUPPLY_CHANGE_LIMIT});
return Decimal.D256({value: SUPPLY_CHANGE_LIMIT});
}
}


function getCouponSupplyChangeLimit() internal pure returns (Decimal.D256 memory) {
function getCouponSupplyChangeLimit() internal pure returns (Decimal.D256 memory) {
return Decimal.D256({value: COUPON_SUPPLY_CHANGE_LIMIT});
return Decimal.D256({value: COUPON_SUPPLY_CHANGE_LIMIT});
}
}


function getOraclePoolRatio() internal pure returns (uint256) {
function getOraclePoolRatio() internal pure returns (uint256) {
return ORACLE_POOL_RATIO;
return ORACLE_POOL_RATIO;
}
}


function getChainId() internal pure returns (uint256) {
function getChainId() internal pure returns (uint256) {
return CHAIN_ID;
return CHAIN_ID;
}
}


function getDaoAddress() internal pure returns (address) {
function getDaoAddress() internal pure returns (address) {
return DAO_ADDRESS;
return DAO_ADDRESS;
}
}


function getDollarAddress() internal pure returns (address) {
function getDollarAddress() internal pure returns (address) {
return DOLLAR_ADDRESS;
return DOLLAR_ADDRESS;
}
}


function getPairAddress() internal pure returns (address) {
function getPairAddress() internal pure returns (address) {
return PAIR_ADDRESS;
return PAIR_ADDRESS;
}
}


function getLegacyPoolAddress() internal pure returns (address) {
function getLegacyPoolAddress() internal pure returns (address) {
return LEGACY_POOL_ADDRESS;
return LEGACY_POOL_ADDRESS;
}
}


function getLegacyPoolReward() internal pure returns (uint256) {
function getLegacyPoolReward() internal pure returns (uint256) {
return LEGACY_POOL_REWARD;
return LEGACY_POOL_REWARD;
}
}
}
}


/*
/*
Copyright 2020 Empty Set Squad <emptysetsquad@protonmail.com>
Copyright 2020 Empty Set Squad <emptysetsquad@protonmail.com>


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


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


Unless required by applicable law or agreed to in writing, software
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
*/
*/
contract Curve {
contract Curve {
using SafeMath for uint256;
using SafeMath for uint256;
using Decimal for Decimal.D256;
using Decimal for Decimal.D256;


function calculateCouponPremium(
function calculateCouponPremium(
uint256 totalSupply,
uint256 totalSupply,
uint256 totalDebt,
uint256 totalDebt,
uint256 amount
uint256 amount
) internal pure returns (uint256) {
) internal pure returns (uint256) {
return effectivePremium(totalSupply, totalDebt, amount).mul(amount).asUint256();
return effectivePremium(totalSupply, totalDebt, amount).mul(amount).asUint256();
}
}


function effectivePremium(
function effectivePremium(
uint256 totalSupply,
uint256 totalSupply,
uint256 totalDebt,
uint256 totalDebt,
uint256 amount
uint256 amount
) private pure returns (Decimal.D256 memory) {
) private pure returns (Decimal.D256 memory) {
Decimal.D256 memory debtRatio = Decimal.ratio(totalDebt, totalSupply);
Decimal.D256 memory debtRatio = Decimal.ratio(totalDebt, totalSupply);
Decimal.D256 memory debtRatioUpperBound = Constants.getDebtRatioCap();
Decimal.D256 memory debtRatioUpperBound = Constants.getDebtRatioCap();


uint256 totalSupplyEnd = totalSupply.sub(amount);
uint256 totalSupplyEnd = totalSupply.sub(amount);
uint256 totalDebtEnd = totalDebt.sub(amount);
uint256 totalDebtEnd = totalDebt.sub(amount);
Decimal.D256 memory debtRatioEnd = Decimal.ratio(totalDebtEnd, totalSupplyEnd);
Decimal.D256 memory debtRatioEnd = Decimal.ratio(totalDebtEnd, totalSupplyEnd);


if (debtRatio.greaterThan(debtRatioUpperBound)) {
if (debtRatio.greaterThan(debtRatioUpperBound)) {
if (debtRatioEnd.greaterThan(debtRatioUpperBound)) {
if (debtRatioEnd.greaterThan(debtRatioUpperBound)) {
return curve(debtRatioUpperBound);
return curve(debtRatioUpperBound);
}
}


Decimal.D256 memory premiumCurve = curveMean(debtRatioEnd, debtRatioUpperBound);
Decimal.D256 memory premiumCurve = curveMean(debtRatioEnd, debtRatioUpperBound);
Decimal.D256 memory premiumCurveDelta = debtRatioUpperBound.sub(debtRatioEnd);
Decimal.D256 memory premiumCurveDelta = debtRatioUpperBound.sub(debtRatioEnd);
Decimal.D256 memory premiumFlat = curve(debtRatioUpperBound);
Decimal.D256 memory premiumFlat = curve(debtRatioUpperBound);
Decimal.D256 memory premiumFlatDelta = debtRatio.sub(debtRatioUpperBound);
Decimal.D256 memory premiumFlatDelta = debtRatio.sub(debtRatioUpperBound);
return (premiumCurve.mul(premiumCurveDelta)).add(premiumFlat.mul(premiumFlatDelta))
return (premiumCurve.mul(premiumCurveDelta)).add(premiumFlat.mul(premiumFlatDelta))
.div(premiumCurveDelta.add(premiumFlatDelta));
.div(premiumCurveDelta.add(premiumFlatDelta));
}
}


return curveMean(debtRatioEnd, debtRatio);
return curveMean(debtRatioEnd, debtRatio);
}
}


// 1/(3(1-R)^2)-1/3
// 1/(3(1-R)^2)-1/3
function curve(Decimal.D256 memory debtRatio) private pure returns (Decimal.D256 memory) {
function curve(Decimal.D256 memory debtRatio) private pure returns (Decimal.D256 memory) {
return Decimal.one().div(
return Decimal.one().div(
Decimal.from(3).mul((Decimal.one().sub(debtRatio)).pow(2))
Decimal.from(3).mul((Decimal.one().sub(debtRatio)).pow(2))
).sub(Decimal.ratio(1, 3));
).sub(Decimal.ratio(1, 3));
}
}


// 1/(3(1-R)(1-R'))-1/3
// 1/(3(1-R)(1-R'))-1/3
function curveMean(
function curveMean(
Decimal.D256 memory lower,
Decimal.D256 memory lower,
Decimal.D256 memory upper
Decimal.D256 memory upper
) private pure returns (Decimal.D256 memory) {
) private pure returns (Decimal.D256 memory) {
if (lower.equals(upper)) {
if (lower.equals(upper)) {
return curve(lower);
return curve(lower);
}
}


return Decimal.one().div(
return Decimal.one().div(
Decimal.from(3).mul(Decimal.one().sub(upper)).mul(Decimal.one().sub(lower))
Decimal.from(3).mul(Decimal.one().sub(upper)).mul(Decimal.one().sub(lower))
).sub(Decimal.ratio(1, 3));
).sub(Decimal.ratio(1, 3));
}
}
}
}


interface IUniswapV2Pair {
interface IUniswapV2Pair {
event Approval(address indexed owner, address indexed spender, uint value);
event Approval(address indexed owner, address indexed spender, uint value);
event Transfer(address indexed from, address indexed to, uint value);
event Transfer(address indexed from, address indexed to, uint value);


function name() external pure returns (string memory);
function name() external pure returns (string memory);
function symbol() external pure returns (string memory);
function symbol() external pure returns (string memory);
function decimals() external pure returns (uint8);
function decimals() external pure returns (uint8);
function totalSupply() external view returns (uint);
function totalSupply() external view returns (uint);
function balanceOf(address owner) external view returns (uint);
function balanceOf(address owner) external view returns (uint);
function allowance(address owner, address spender) external view returns (uint);
function allowance(address owner, address spender) external view returns (uint);


function approve(address spender, uint value) external returns (bool);
function approve(address spender, uint value) external returns (bool);
function transfer(address to, uint value) external returns (bool);
function transfer(address to, uint value) external returns (bool);
function transferFrom(address from, address to, uint value) external returns (bool);
function transferFrom(address from, address to, uint value) external returns (bool);


function DOMAIN_SEPARATOR() external view returns (bytes32);
function DOMAIN_SEPARATOR() external view returns (bytes32);
function PERMIT_TYPEHASH() external pure returns (bytes32);
function PERMIT_TYPEHASH() external pure returns (bytes32);
function nonces(address owner) external view returns (uint);
function nonces(address owner) external view returns (uint);


function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external;
function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external;


event Mint(address indexed sender, uint amount0, uint amount1);
event Mint(address indexed sender, uint amount0, uint amount1);
event Burn(address indexed sender, uint amount0, uint amount1, address indexed to);
event Burn(address indexed sender, uint amount0, uint amount1, address indexed to);
event Swap(
event Swap(
address indexed sender,
address indexed sender,
uint amount0In,
uint amount0In,
uint amount1In,
uint amount1In,
uint amount0Out,
uint amount0Out,
uint amount1Out,
uint amount1Out,
address indexed to
address indexed to
);
);
event Sync(uint112 reserve0, uint112 reserve1);
event Sync(uint112 reserve0, uint112 reserve1);


function MINIMUM_LIQUIDITY() external pure returns (uint);
function MINIMUM_LIQUIDITY() external pure returns (uint);
function factory() external view returns (address);
function factory() external view returns (address);
function token0() external view returns (address);
function token0() external view returns (address);
function token1() external view returns (address);
function token1() external view returns (address);
function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
function price0CumulativeLast() external view returns (uint);
function price0CumulativeLast() external view returns (uint);
function price1CumulativeLast() external view returns (uint);
function price1CumulativeLast() external view returns (uint);
function kLast() external view returns (uint);
function kLast() external view returns (uint);


function mint(address to) external returns (uint liquidity);
function mint(address to) external returns (uint liquidity);
function burn(address to) external returns (uint amount0, uint amount1);
function burn(address to) external returns (uint amount0, uint amount1);
function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
function skim(address to) external;
function skim(address to) external;
function sync() external;
function sync() external;


function initialize(address, address) external;
function initialize(address, address) external;
}
}


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


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


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


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


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


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


/**
/**
* @dev Emitted when `valu
* @dev Emitted when `valu