|
|
|
|
|
|
|
pragma solidity 0.7.5;
|
|
|
|
interface IOwnable {
|
|
function policy() external view returns (address);
|
|
|
|
function renounceManagement() external;
|
|
|
|
function pushManagement( address newOwner_ ) external;
|
|
|
|
function pullManagement() external;
|
|
}
|
|
|
|
contract Ownable is IOwnable {
|
|
|
|
address internal _owner;
|
|
address internal _newOwner;
|
|
|
|
event OwnershipPushed(address indexed previousOwner, address indexed newOwner);
|
|
event OwnershipPulled(address indexed previousOwner, address indexed newOwner);
|
|
|
|
constructor () {
|
|
_owner = msg.sender;
|
|
emit OwnershipPushed( address(0), _owner );
|
|
}
|
|
|
|
function policy() public view override returns (address) {
|
|
return _owner;
|
|
}
|
|
|
|
modifier onlyPolicy() {
|
|
require( _owner == msg.sender, "Ownable: caller is not the owner" );
|
|
_;
|
|
}
|
|
|
|
function renounceManagement() public virtual override onlyPolicy() {
|
|
emit OwnershipPushed( _owner, address(0) );
|
|
_owner = address(0);
|
|
}
|
|
|
|
function pushManagement( address newOwner_ ) public virtual override onlyPolicy() {
|
|
require( newOwner_ != address(0), "Ownable: new owner is the zero address");
|
|
emit OwnershipPushed( _owner, newOwner_ );
|
|
_newOwner = newOwner_;
|
|
}
|
|
|
|
function pullManagement() public virtual override {
|
|
require( msg.sender == _newOwner, "Ownable: must be new owner to pull");
|
|
emit OwnershipPulled( _owner, _newOwner );
|
|
_owner = _newOwner;
|
|
}
|
|
}
|
|
|
|
library SafeMath {
|
|
|
|
function add(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
uint256 c = a + b;
|
|
require(c >= a, "SafeMath: addition overflow");
|
|
|
|
return c;
|
|
}
|
|
|
|
function add32(uint32 a, uint32 b) internal pure returns (uint32) {
|
|
uint32 c = a + b;
|
|
require(c >= a, "SafeMath-32: addition overflow");
|
|
|
|
return c;
|
|
}
|
|
|
|
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
return sub(a, b, "SafeMath: subtraction overflow");
|
|
}
|
|
|
|
function sub32(uint32 a, uint32 b) internal pure returns (uint32) {
|
|
require(b <= a, "SafeMath-32: subtraction overflow");
|
|
uint32 c = a - b;
|
|
|
|
return c;
|
|
}
|
|
|
|
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
|
|
require(b <= a, errorMessage);
|
|
uint256 c = a - b;
|
|
|
|
return c;
|
|
}
|
|
|
|
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
if (a == 0) {
|
|
return 0;
|
|
}
|
|
|
|
uint256 c = a * b;
|
|
require(c / a == b, "SafeMath: multiplication overflow");
|
|
|
|
return c;
|
|
}
|
|
|
|
function mul32(uint32 a, uint32 b) internal pure returns (uint32) {
|
|
if (a == 0) {
|
|
return 0;
|
|
}
|
|
|
|
uint32 c = a * b;
|
|
require(c / a == b, "SafeMath: multiplication overflow");
|
|
|
|
return c;
|
|
}
|
|
}
|
|
|
|
library Address {
|
|
|
|
function isContract(address account) internal view returns (bool) {
|
|
|
|
uint256 size;
|
|
|
|
assembly { size := extcodesize(account) }
|
|
return size > 0;
|
|
}
|
|
|
|
function sendValue(address payable recipient, uint256 amount) internal {
|
|
require(address(this).balance >= amount, "Address: insufficient balance");
|
|
|
|
|
|
(bool success, ) = recipient.call{ value: amount }("");
|
|
require(success, "Address: unable to send value, recipient may have reverted");
|
|
}
|
|
|
|
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
|
|
return functionCall(target, data, "Address: low-level call failed");
|
|
}
|
|
|
|
function functionCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
|
|
return _functionCallWithValue(target, data, 0, errorMessage);
|
|
}
|
|
|
|
function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
|
|
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
|
|
}
|
|
|
|
function functionCallWithValue(address target, bytes memory data, uint256 value, string memory errorMessage) internal returns (bytes memory) {
|
|
require(address(this).balance >= value, "Address: insufficient balance for call");
|
|
require(isContract(target), "Address: call to non-contract");
|
|
|
|
|
|
(bool success, bytes memory returndata) = target.call{ value: value }(data);
|
|
return _verifyCallResult(success, returndata, errorMessage);
|
|
}
|
|
|
|
function _functionCallWithValue(address target, bytes memory data, uint256 weiValue, string memory errorMessage) private returns (bytes memory) {
|
|
require(isContract(target), "Address: call to non-contract");
|
|
|
|
|
|
(bool success, bytes memory returndata) = target.call{ value: weiValue }(data);
|
|
if (success) {
|
|
return returndata;
|
|
} else {
|
|
|
|
if (returndata.length > 0) {
|
|
|
|
|
|
|
|
assembly {
|
|
let returndata_size := mload(returndata)
|
|
revert(add(32, returndata), returndata_size)
|
|
}
|
|
} else {
|
|
revert(errorMessage);
|
|
}
|
|
}
|
|
}
|
|
|
|
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
|
|
return functionStaticCall(target, data, "Address: low-level static call failed");
|
|
}
|
|
|
|
function functionStaticCall(address target, bytes memory data, string memory errorMessage) internal view returns (bytes memory) {
|
|
require(isContract(target), "Address: static call to non-contract");
|
|
|
|
|
|
(bool success, bytes memory returndata) = target.staticcall(data);
|
|
return _verifyCallResult(success, returndata, errorMessage);
|
|
}
|
|
|
|
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
|
|
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
|
|
}
|
|
|
|
function functionDelegateCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
|
|
require(isContract(target), "Address: delegate call to non-contract");
|
|
|
|
|
|
(bool success, bytes memory returndata) = target.delegatecall(data);
|
|
return _verifyCallResult(success, returndata, errorMessage);
|
|
}
|
|
|
|
function _verifyCallResult(bool success, bytes memory returndata, string memory errorMessage) private pure returns(bytes memory) {
|
|
if (success) {
|
|
return returndata;
|
|
} else {
|
|
if (returndata.length > 0) {
|
|
|
|
assembly {
|
|
let returndata_size := mload(returndata)
|
|
revert(add(32, returndata), returndata_size)
|
|
}
|
|
} else {
|
|
revert(errorMessage);
|
|
}
|
|
}
|
|
}
|
|
|
|
function addressToString(address _address) internal pure returns(string memory) {
|
|
bytes32 _bytes = bytes32(uint256(_address));
|
|
bytes memory HEX = "0123456789abcdef";
|
|
bytes memory _addr = new bytes(42);
|
|
|
|
_addr[0] = '0';
|
|
_addr[1] = 'x';
|
|
|
|
for(uint256 i = 0; i < 20; i++) {
|
|
_addr[2+i*2] = HEX[uint8(_bytes[i + 12] >> 4)];
|
|
_addr[3+i*2] = HEX[uint8(_bytes[i + 12] & 0x0f)];
|
|
}
|
|
|
|
return string(_addr);
|
|
|
|
}
|
|
}
|
|
|
|
interface IERC20 {
|
|
function decimals() external view returns (uint8);
|
|
|
|
function totalSupply() external view returns (uint256);
|
|
|
|
function balanceOf(address account) external view returns (uint256);
|
|
|
|
function transfer(address recipient, uint256 amount) external returns (bool);
|
|
|
|
function allowance(address owner, address spender) external view returns (uint256);
|
|
|
|
function approve(address spender, uint256 amount) external returns (bool);
|
|
|
|
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
|
|
|
|
event Transfer(address indexed from, address indexed to, uint256 value);
|
|
|
|
event Approval(address indexed owner, address indexed spender, uint256 value);
|
|
}
|
|
|
|
abstract contract ERC20 is IERC20 {
|
|
|
|
using SafeMath for uint256;
|
|
|
|
|
|
bytes32 constant private ERC20TOKEN_ERC1820_INTERFACE_ID = keccak256( "ERC20Token" );
|
|
|
|
mapping (address => uint256) internal _balances;
|
|
|
|
mapping (address => mapping (address => uint256)) internal _allowances;
|
|
|
|
uint256 internal _totalSupply;
|
|
|
|
string internal _name;
|
|
|
|
string internal _symbol;
|
|
|
|
uint8 internal _decimals;
|
|
|
|
constructor (string memory name_, string memory symbol_, uint8 decimals_) {
|
|
_name = name_;
|
|
_symbol = symbol_;
|
|
_decimals = decimals_;
|
|
}
|
|
|
|
function name() public view returns (string memory) {
|
|
return _name;
|
|
}
|
|
|
|
function symbol() public view returns (string memory) {
|
|
return _symbol;
|
|
}
|
|
|
|
function decimals() public view override returns (uint8) {
|
|
return _decimals;
|
|
}
|
|
|
|
function totalSupply() public view override returns (uint256) {
|
|
return _totalSupply;
|
|
}
|
|
|
|
function balanceOf(address account) public view virtual override returns (uint256) {
|
|
return _balances[account];
|
|
}
|
|
|
|
function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
|
|
_transfer(msg.sender, recipient, amount);
|
|
return true;
|
|
}
|
|
|
|
function allowance(address owner, address spender) public view virtual override returns (uint256) {
|
|
return _allowances[owner][spender];
|
|
}
|
|
|
|
function approve(address spender, uint256 amount) public virtual override returns (bool) {
|
|
_approve(msg.sender, spender, amount);
|
|
return true;
|
|
}
|
|
|
|
function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
|
|
_transfer(sender, recipient, amount);
|
|
_approve(sender, msg.sender, _allowances[sender][msg.sender].sub(amount, "ERC20: transfer amount exceeds allowance"));
|
|
return true;
|
|
}
|
|
|
|
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
|
|
_approve(msg.sender, spender, _allowances[msg.sender][spender].add(addedValue));
|
|
return true;
|
|
}
|
|
|
|
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
|
|
_approve(msg.sender, spender, _allowances[msg.sender][spender].sub(subtractedValue, "ERC20: decreased allowance below zero"));
|
|
return true;
|
|
}
|
|
|
|
function _transfer(address sender, address recipient, uint256 amount) internal virtual {
|
|
require(sender != address(0), "ERC20: transfer from the zero address");
|
|
require(recipient != address(0), "ERC20: transfer to the zero address");
|
|
|
|
_beforeTokenTransfer(sender, recipient, amount);
|
|
|
|
_balances[sender] = _balances[sender].sub(amount, "ERC20: transfer amount exceeds balance");
|
|
_balances[recipient] = _balances[recipient].add(amount);
|
|
emit Transfer(sender, recipient, amount);
|
|
}
|
|
|
|
function _mint(address account_, uint256 ammount_) internal virtual {
|
|
require(account_ != address(0), "ERC20: mint to the zero address");
|
|
_beforeTokenTransfer(address( this ), account_, ammount_);
|
|
_totalSupply = _totalSupply.add(ammount_);
|
|
_balances[account_] = _balances[account_].add(ammount_);
|
|
emit Transfer(address( this ), account_, ammount_);
|
|
}
|
|
|
|
function _burn(address account, uint256 amount) internal virtual {
|
|
require(account != address(0), "ERC20: burn from the zero address");
|
|
|
|
_beforeTokenTransfer(account, address(0), amount);
|
|
|
|
_balances[account] = _balances[account].sub(amount, "ERC20: burn amount exceeds balance");
|
|
_totalSupply = _totalSupply.sub(amount);
|
|
emit Transfer(account, address(0), amount);
|
|
}
|
|
|
|
function _approve(address owner, address spender, uint256 amount) internal virtual {
|
|
require(owner != address(0), "ERC20: approve from the zero address");
|
|
require(spender != address(0), "ERC20: approve to the zero address");
|
|
|
|
_allowances[owner][spender] = amount;
|
|
emit Approval(owner, spender, amount);
|
|
}
|
|
|
|
function _beforeTokenTransfer( address from_, address to_, uint256 amount_ ) internal virtual { }
|
|
}
|
|
|
|
interface IERC2612Permit {
|
|
|
|
function permit(
|
|
address owner,
|
|
address spender,
|
|
uint256 amount,
|
|
uint256 deadline,
|
|
uint8 v,
|
|
bytes32 r,
|
|
bytes32 s
|
|
) external;
|
|
|
|
function nonces(address owner) external view returns (uint256);
|
|
}
|
|
|
|
library Counters {
|
|
using SafeMath for uint256;
|
|
|
|
struct Counter {
|
|
|
|
uint256 _value;
|
|
}
|
|
|
|
function current(Counter storage counter) internal view returns (uint256) {
|
|
return counter._value;
|
|
}
|
|
|
|
function increment(Counter storage counter) internal {
|
|
counter._value += 1;
|
|
}
|
|
|
|
function decrement(Counter storage counter) internal {
|
|
counter._value = counter._value.sub(1);
|
|
}
|
|
}
|
|
|
|
abstract contract ERC20Permit is ERC20, IERC2612Permit {
|
|
using Counters for Counters.Counter;
|
|
|
|
mapping(address => Counters.Counter) private _nonces;
|
|
|
|
|
|
bytes32 public constant PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9;
|
|
|
|
bytes32 public DOMAIN_SEPARATOR;
|
|
|
|
constructor() {
|
|
uint256 chainID;
|
|
assembly {
|
|
chainID := chainid()
|
|
}
|
|
|
|
DOMAIN_SEPARATOR = keccak256(
|
|
abi.encode(
|
|
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
|
|
keccak256(bytes(name())),
|
|
keccak256(bytes("1")),
|
|
chainID,
|
|
address(this)
|
|
)
|
|
);
|
|
}
|
|
|
|
function permit(
|
|
address owner,
|
|
address spender,
|
|
uint256 amount,
|
|
uint256 deadline,
|
|
uint8 v,
|
|
bytes32 r,
|
|
bytes32 s
|
|
) public virtual override {
|
|
require(block.timestamp <= deadline, "Permit: expired deadline");
|
|
|
|
bytes32 hashStruct =
|
|
keccak256(abi.encode(PERMIT_TYPEHASH, owner, spender, amount, _nonces[owner].current(), deadline));
|
|
|
|
bytes32 _hash = keccak256(abi.encodePacked(uint16(0x1901), DOMAIN_SEPARATOR, hashStruct));
|
|
|
|
address signer = ecrecover(_hash, v, r, s);
|
|
require(signer != address(0) && signer == owner, "ZeroSwapPermit: Invalid signature");
|
|
|
|
_nonces[owner].increment();
|
|
_approve(owner, spender, amount);
|
|
}
|
|
|
|
function nonces(address owner) public view override returns (uint256) {
|
|
return _nonces[owner].current();
|
|
}
|
|
}
|
|
|
|
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 {
|
|
|
|
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));
|
|
}
|
|
|
|
function _callOptionalReturn(IERC20 token, bytes memory data) private {
|
|
|
|
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
|
|
if (returndata.length > 0) {
|
|
|
|
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
|
|
}
|
|
}
|
|
}
|
|
|
|
library FullMath {
|
|
function fullMul(uint256 x, uint256 y) private pure returns (uint256 l, uint256 h) {
|
|
uint256 mm = mulmod(x, y, uint256(-1));
|
|
l = x * y;
|
|
h = mm - l;
|
|
if (mm < l) h -= 1;
|
|
}
|
|
|
|
function fullDiv(
|
|
uint256 l,
|
|
uint256 h,
|
|
uint256 d
|
|
) private pure returns (uint256) {
|
|
uint256 pow2 = d & -d;
|
|
d /= pow2;
|
|
l /= pow2;
|
|
l += h * ((-pow2) / pow2 + 1);
|
|
uint256 r = 1;
|
|
r *= 2 - d * r;
|
|
r *= 2 - d * r;
|
|
r *= 2 - d * r;
|
|
r *= 2 - d * r;
|
|
r *= 2 - d * r;
|
|
r *= 2 - d * r;
|
|
r *= 2 - d * r;
|
|
r *= 2 - d * r;
|
|
return l * r;
|
|
}
|
|
|
|
function mulDiv(
|
|
uint256 x,
|
|
uint256 y,
|
|
uint256 d
|
|
) internal pure returns (uint256) {
|
|
(uint256 l, uint256 h) = fullMul(x, y);
|
|
uint256 mm = mulmod(x, y, d);
|
|
if (mm > l) h -= 1;
|
|
l -= mm;
|
|
require(h < d, 'FullMath::mulDiv: overflow');
|
|
return fullDiv(l, h, d);
|
|
}
|
|
}
|
|
|
|
library FixedPoint {
|
|
|
|
struct uq112x112 {
|
|
uint224 _x;
|
|
}
|
|
|
|
struct uq144x112 {
|
|
uint256 _x;
|
|
}
|
|
|
|
uint8 private constant RESOLUTION = 112;
|
|
uint256 private constant Q112 = 0x10000000000000000000000000000;
|
|
uint256 private constant Q224 = 0x100000000000000000000000000000000000000000000000000000000;
|
|
uint256 private constant LOWER_MASK = 0xffffffffffffffffffffffffffff;
|
|
|
|
function decode(uq112x112 memory self) internal pure returns (uint112) {
|
|
return uint112(self._x >> RESOLUTION);
|
|
}
|
|
|
|
function decode112with18(uq112x112 memory self) internal pure returns (uint) {
|
|
return uint(self._x) / 5192296858534827;
|
|
}
|
|
|
|
function fraction(uint256 numerator, uint256 denominator) internal pure returns (uq112x112 memory) {
|
|
require(denominator > 0, 'FixedPoint::fraction: division by zero');
|
|
if (numerator == 0) return FixedPoint.uq112x112(0);
|
|
|
|
if (numerator <= uint144(-1)) {
|
|
uint256 result = (numerator << RESOLUTION) / denominator;
|
|
require(result <= uint224(-1), 'FixedPoint::fraction: overflow');
|
|
return uq112x112(uint224(result));
|
|
} else {
|
|
uint256 result = FullMath.mulDiv(numerator, Q112, denominator);
|
|
require(result <= uint224(-1), 'FixedPoint::fraction: overflow');
|
|
return uq112x112(uint224(result));
|
|
}
|
|
}
|
|
}
|
|
|
|
interface AggregatorV3Interface {
|
|
|
|
function decimals() external view returns (uint8);
|
|
function description() external view returns (string memory);
|
|
function version() external view returns (uint256);
|
|
|
|
|
|
|
|
|
|
function getRoundData(uint80 _roundId)
|
|
external
|
|
view
|
|
returns (
|
|
uint80 roundId,
|
|
int256 answer,
|
|
uint256 startedAt,
|
|
uint256 updatedAt,
|
|
uint80 answeredInRound
|
|
);
|
|
function latestRoundData()
|
|
external
|
|
view
|
|
returns (
|
|
uint80 roundId,
|
|
int256 answer,
|
|
uint256 startedAt,
|
|
uint256 updatedAt,
|
|
uint80 answeredInRound
|
|
);
|
|
}
|
|
|
|
interface ITreasury {
|
|
function deposit( uint _amount, address _token, uint _profit ) external returns ( bool );
|
|
function valueOf( address _token, uint _amount ) external view returns ( uint value_ );
|
|
function mintRewards( address _recipient, uint _amount ) external;
|
|
}
|
|
|
|
interface IStaking {
|
|
function stake( uint _amount, address _recipient ) external returns ( bool );
|
|
}
|
|
|
|
interface IStakingHelper {
|
|
function stake( uint _amount, address _recipient ) external;
|
|
}
|
|
|
|
contract BondDepositoryLPv2 is Ownable {
|
|
|
|
using FixedPoint for *;
|
|
using SafeERC20 for IERC20;
|
|
using SafeMath for uint;
|
|
using SafeMath for uint32;
|
|
|
|
|
|
|
|
|
|
|
|
event BondCreated( uint deposit, uint indexed payout, uint indexed expires, uint indexed priceInUSD );
|
|
event BondRedeemed( address indexed recipient, uint payout, uint remaining );
|
|
event BondPriceChanged( uint indexed priceInUSD, uint indexed internalPrice, uint indexed debtRatio );
|
|
event ControlVariableAdjustment( uint initialBCV, uint newBCV, uint adjustment, bool addition );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
IERC20 public immutable BIG;
|
|
address public immutable principle;
|
|
address public immutable treasury;
|
|
address public immutable DAO;
|
|
|
|
address public staking;
|
|
address public stakingHelper;
|
|
bool public useHelper;
|
|
|
|
Terms public terms;
|
|
Adjust public adjustment;
|
|
|
|
mapping( address => Bond ) public bondInfo;
|
|
|
|
uint public totalDebt;
|
|
uint32 public lastDecay;
|
|
uint public minPayout = 1000;
|
|
uint256 public scaling = 9;
|
|
|
|
|
|
|
|
|
|
struct Terms {
|
|
uint controlVariable;
|
|
uint minimumPrice;
|
|
uint maxPayout;
|
|
uint maxDebt;
|
|
uint32 vestingTerm;
|
|
}
|
|
|
|
|
|
struct Bond {
|
|
uint payout;
|
|
uint32 vesting;
|
|
uint32 lastTime;
|
|
uint pricePaid;
|
|
}
|
|
|
|
|
|
struct Adjust {
|
|
bool add;
|
|
uint rate;
|
|
uint target;
|
|
uint32 buffer;
|
|
uint32 lastTime;
|
|
}
|
|
|
|
|
|
|
|
constructor (
|
|
address _BIG,
|
|
address _principle,
|
|
address _treasury,
|
|
address _DAO
|
|
) {
|
|
require( _BIG != address(0) );
|
|
BIG = IERC20( _BIG );
|
|
require( _principle != address(0) );
|
|
principle = _principle;
|
|
require( _treasury != address(0) );
|
|
treasury = _treasury;
|
|
require( _DAO != address(0) );
|
|
DAO = _DAO;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function initializeBondTerms(
|
|
uint _controlVariable,
|
|
uint32 _vestingTerm,
|
|
uint _minimumPrice,
|
|
uint _maxPayout,
|
|
uint _maxDebt
|
|
) external onlyPolicy() {
|
|
require( terms.controlVariable == 0, "Bonds must be initialized from 0" );
|
|
terms = Terms ({
|
|
controlVariable: _controlVariable,
|
|
vestingTerm: _vestingTerm,
|
|
minimumPrice: _minimumPrice,
|
|
maxPayout: _maxPayout,
|
|
maxDebt: _maxDebt
|
|
});
|
|
lastDecay = uint32(block.timestamp);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
enum PARAMETER { VESTING, PAYOUT, DEBT, MINPRICE }
|
|
|
|
|
|
|
|
|
|
|
|
function setBondTerms ( PARAMETER _parameter, uint _input ) external onlyPolicy() {
|
|
if ( _parameter == PARAMETER.VESTING ) {
|
|
require( _input >= 300, "Vesting must be longer than 5 minutes" );
|
|
terms.vestingTerm = uint32(_input);
|
|
} else if ( _parameter == PARAMETER.PAYOUT ) {
|
|
require( _input <= 1000, "Payout cannot be above 1 percent" );
|
|
terms.maxPayout = _input;
|
|
} else if ( _parameter == PARAMETER.DEBT ) {
|
|
terms.maxDebt = _input;
|
|
} else if ( _parameter == PARAMETER.MINPRICE ) {
|
|
|
|
terms.minimumPrice = _input;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function setAdjustment (
|
|
bool _addition,
|
|
uint _increment,
|
|
uint _target,
|
|
uint32 _buffer
|
|
) external onlyPolicy() {
|
|
require( _increment <= terms.controlVariable.mul( 25 ) / 1000, "Increment too large" );
|
|
|
|
adjustment = Adjust({
|
|
add: _addition,
|
|
rate: _increment,
|
|
target: _target,
|
|
buffer: _buffer,
|
|
lastTime: uint32(block.timestamp)
|
|
});
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function setStaking( address _staking, bool _helper ) external onlyPolicy() {
|
|
require( _staking != address(0) );
|
|
if ( _helper ) {
|
|
useHelper = true;
|
|
stakingHelper = _staking;
|
|
} else {
|
|
useHelper = false;
|
|
staking = _staking;
|
|
}
|
|
}
|
|
|
|
function setMinPayout(uint256 _new) public onlyPolicy() {
|
|
minPayout = _new;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function deposit(
|
|
uint _amount,
|
|
uint _maxPrice,
|
|
address _depositor
|
|
) external returns ( uint ) {
|
|
require( _depositor != address(0), "Invalid address" );
|
|
|
|
decayDebt();
|
|
require( totalDebt <= terms.maxDebt, "Max capacity reached" );
|
|
|
|
uint priceInUSD = bondPriceInUSD();
|
|
uint nativePrice = _bondPrice();
|
|
|
|
require( _maxPrice >= nativePrice, "Slippage limit: more than max price" );
|
|
|
|
uint value = ITreasury( treasury ).valueOf( principle, _amount );
|
|
uint payout = payoutFor( value );
|
|
|
|
require( payout >= minPayout, "Bond too small" );
|
|
require( payout <= maxPayout(), "Bond too large");
|
|
|
|
|
|
|
|
|
|
|
|
IERC20( principle ).safeTransferFrom( msg.sender, treasury, _amount );
|
|
ITreasury( treasury ).mintRewards( address(this), payout );
|
|
|
|
|
|
totalDebt = totalDebt.add( value );
|
|
|
|
|
|
bondInfo[ _depositor ] = Bond({
|
|
payout: bondInfo[ _depositor ].payout.add( payout ),
|
|
vesting: terms.vestingTerm,
|
|
lastTime: uint32(block.timestamp),
|
|
pricePaid: priceInUSD
|
|
});
|
|
|
|
|
|
emit BondCreated( _amount, payout, block.timestamp.add( terms.vestingTerm ), priceInUSD );
|
|
emit BondPriceChanged( bondPriceInUSD(), _bondPrice(), debtRatio() );
|
|
|
|
adjust();
|
|
return payout;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function redeem( address _recipient, bool _stake ) external returns ( uint ) {
|
|
require(msg.sender == _recipient, "NA");
|
|
Bond memory info = bondInfo[ _recipient ];
|
|
|
|
uint percentVested = percentVestedFor( _recipient );
|
|
|
|
if ( percentVested >= 10000 ) {
|
|
delete bondInfo[ _recipient ];
|
|
emit BondRedeemed( _recipient, info.payout, 0 );
|
|
return stakeOrSend( _recipient, _stake, info.payout );
|
|
|
|
} else {
|
|
|
|
uint payout = info.payout.mul( percentVested ) / 10000 ;
|
|
|
|
bondInfo[ _recipient ] = Bond({
|
|
payout: info.payout.sub( payout ),
|
|
vesting: info.vesting.sub32( uint32( block.timestamp ).sub32( info.lastTime ) ),
|
|
lastTime: uint32(block.timestamp),
|
|
pricePaid: info.pricePaid
|
|
});
|
|
|
|
emit BondRedeemed( _recipient, payout, bondInfo[ _recipient ].payout );
|
|
return stakeOrSend( _recipient, _stake, payout );
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function stakeOrSend( address _recipient, bool _stake, uint _amount ) internal returns ( uint ) {
|
|
if ( !_stake ) {
|
|
BIG.transfer( _recipient, _amount );
|
|
} else {
|
|
if ( useHelper ) {
|
|
BIG.approve( stakingHelper, _amount );
|
|
IStakingHelper( stakingHelper ).stake( _amount, _recipient );
|
|
} else {
|
|
BIG.approve( staking, _amount );
|
|
IStaking( staking ).stake( _amount, _recipient );
|
|
}
|
|
}
|
|
return _amount;
|
|
}
|
|
|
|
|
|
|
|
|
|
function adjust() internal {
|
|
uint timeCanAdjust = adjustment.lastTime.add32( adjustment.buffer );
|
|
if( adjustment.rate != 0 && block.timestamp >= timeCanAdjust ) {
|
|
uint initial = terms.controlVariable;
|
|
uint bcv = initial;
|
|
if ( adjustment.add ) {
|
|
|
|
bcv = bcv.add(adjustment.rate);
|
|
if ( bcv >= adjustment.target ) {
|
|
adjustment.rate = 0;
|
|
bcv = adjustment.target;
|
|
}
|
|
} else {
|
|
bcv = bcv.sub(adjustment.rate);
|
|
if ( bcv <= adjustment.target ) {
|
|
adjustment.rate = 0;
|
|
bcv = adjustment.target;
|
|
}
|
|
}
|
|
terms.controlVariable = bcv;
|
|
adjustment.lastTime = uint32(block.timestamp);
|
|
emit ControlVariableAdjustment( initial, bcv, adjustment.rate, adjustment.add );
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
function decayDebt() internal {
|
|
totalDebt = totalDebt.sub( debtDecay() );
|
|
lastDecay = uint32(block.timestamp);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function maxPayout() public view returns ( uint ) {
|
|
return BIG.totalSupply().mul( terms.maxPayout ) / 100000 ;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function payoutFor( uint _value ) public view returns ( uint ) {
|
|
return FixedPoint.fraction( _value, bondPrice() ).decode112with18() / 10 ** (18 - scaling);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function bondPrice() public view returns ( uint price_ ) {
|
|
price_ = terms.controlVariable.mul( debtRatio() ) / 10 ** (9 - scaling);
|
|
if ( price_ < terms.minimumPrice ) {
|
|
price_ = terms.minimumPrice;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function _bondPrice() internal returns ( uint price_ ) {
|
|
price_ = terms.controlVariable.mul( debtRatio() ) / 10 ** (9 - scaling);
|
|
if ( price_ < terms.minimumPrice ) {
|
|
price_ = terms.minimumPrice;
|
|
} else if ( terms.minimumPrice != 0 ) {
|
|
terms.minimumPrice = 0;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function bondPriceInUSD() public view returns ( uint price_ ) {
|
|
price_ = bondPrice().mul( 10 ** (18 - scaling) );
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function debtRatio() public view returns ( uint debtRatio_ ) {
|
|
uint supply = BIG.totalSupply();
|
|
debtRatio_ = FixedPoint.fraction(
|
|
currentDebt().mul( 1e9 ),
|
|
supply
|
|
).decode112with18() / 1e18;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function standardizedDebtRatio() external view returns ( uint ) {
|
|
return debtRatio();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function currentDebt() public view returns ( uint ) {
|
|
return totalDebt.sub( debtDecay() );
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function debtDecay() public view returns ( uint decay_ ) {
|
|
uint32 timeSinceLast = uint32(block.timestamp).sub32( lastDecay );
|
|
decay_ = totalDebt.mul( timeSinceLast ) / terms.vestingTerm;
|
|
if ( decay_ > totalDebt ) {
|
|
decay_ = totalDebt;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function percentVestedFor( address _depositor ) public view returns ( uint percentVested_ ) {
|
|
Bond memory bond = bondInfo[ _depositor ];
|
|
uint secondsSinceLast = uint32(block.timestamp).sub32( bond.lastTime );
|
|
uint vesting = bond.vesting;
|
|
|
|
if ( vesting > 0 ) {
|
|
percentVested_ = secondsSinceLast.mul( 10000 ) / vesting;
|
|
} else {
|
|
percentVested_ = 0;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function pendingPayoutFor( address _depositor ) external view returns ( uint pendingPayout_ ) {
|
|
uint percentVested = percentVestedFor( _depositor );
|
|
uint payout = bondInfo[ _depositor ].payout;
|
|
|
|
if ( percentVested >= 10000 ) {
|
|
pendingPayout_ = payout;
|
|
} else {
|
|
pendingPayout_ = payout.mul( percentVested ) / 10000;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function recoverLostToken( address _token ) external returns ( bool ) {
|
|
require( _token != address(BIG) );
|
|
require( _token != principle );
|
|
IERC20( _token ).safeTransfer( DAO, IERC20( _token ).balanceOf( address(this) ) );
|
|
return true;
|
|
}
|
|
} |