文件 1 的 1:rAAVE.sol
pragma solidity >=0.6.0 <0.8.0;
abstract contract Context {
function _msgSender() internal view virtual returns (address payable) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes memory) {
this;
return msg.data;
}
}
pragma solidity >=0.6.0 <0.8.0;
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor () internal {
address msgSender = _msgSender();
_owner = msgSender;
emit OwnershipTransferred(address(0), msgSender);
}
function owner() public view returns (address) {
return _owner;
}
modifier onlyOwner() {
require(_owner == _msgSender(), "Ownable: caller is not the owner");
_;
}
function renounceOwnership() public virtual onlyOwner {
emit OwnershipTransferred(_owner, address(0));
_owner = address(0);
}
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
}
pragma solidity >=0.6.0 <0.8.0;
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 sub(uint256 a, uint256 b) internal pure returns (uint256) {
return sub(a, b, "SafeMath: subtraction overflow");
}
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 div(uint256 a, uint256 b) internal pure returns (uint256) {
return div(a, b, "SafeMath: division by zero");
}
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
uint256 c = a / b;
return c;
}
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return mod(a, b, "SafeMath: modulo by zero");
}
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b != 0, errorMessage);
return a % b;
}
}
pragma solidity >=0.6.0 <0.8.0;
abstract contract ReentrancyGuard {
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor () internal {
_status = _NOT_ENTERED;
}
modifier nonReentrant() {
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
_status = _ENTERED;
_;
_status = _NOT_ENTERED;
}
}
pragma solidity >=0.6.0 <0.8.0;
interface IERC20 {
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);
}
pragma solidity ^0.6.0;
contract ElasticERC20 is Context, IERC20
{
using SafeMath for uint256;
uint8 constant UNSCALED_DECIMALS = 24;
uint256 constant UNSCALED_FACTOR = 10 ** uint256(UNSCALED_DECIMALS);
mapping (address => mapping (address => uint256)) private allowances_;
mapping (address => uint256) private unscaledBalances_;
uint256 private unscaledTotalSupply_;
string private name_;
string private symbol_;
uint8 private decimals_;
uint256 private scalingFactor_;
constructor (string memory _name, string memory _symbol) public
{
name_ = _name;
symbol_ = _symbol;
_setupDecimals(18);
}
function name() public view returns (string memory _name)
{
return name_;
}
function symbol() public view returns (string memory _symbol)
{
return symbol_;
}
function decimals() public view returns (uint8 _decimals)
{
return decimals_;
}
function totalSupply() public view override returns (uint256 _supply)
{
return _scale(unscaledTotalSupply_, scalingFactor_);
}
function balanceOf(address _account) public view override returns (uint256 _balance)
{
return _scale(unscaledBalances_[_account], scalingFactor_);
}
function allowance(address _owner, address _spender) public view virtual override returns (uint256 _allowance)
{
return allowances_[_owner][_spender];
}
function approve(address _spender, uint256 _amount) public virtual override returns (bool _success)
{
_approve(_msgSender(), _spender, _amount);
return true;
}
function increaseAllowance(address _spender, uint256 _addedValue) public virtual returns (bool _success)
{
_approve(_msgSender(), _spender, allowances_[_msgSender()][_spender].add(_addedValue));
return true;
}
function decreaseAllowance(address _spender, uint256 _subtractedValue) public virtual returns (bool _success)
{
_approve(_msgSender(), _spender, allowances_[_msgSender()][_spender].sub(_subtractedValue, "ERC20: decreased allowance below zero"));
return true;
}
function transfer(address _recipient, uint256 _amount) public virtual override returns (bool _success)
{
_transfer(_msgSender(), _recipient, _amount);
return true;
}
function transferFrom(address _sender, address _recipient, uint256 _amount) public virtual override returns (bool _success)
{
_transfer(_sender, _recipient, _amount);
_approve(_sender, _msgSender(), allowances_[_sender][_msgSender()].sub(_amount, "ERC20: transfer amount exceeds allowance"));
return true;
}
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 _transfer(address _sender, address _recipient, uint256 _amount) internal virtual
{
uint256 _unscaledAmount = _unscale(_amount, scalingFactor_);
require(_sender != address(0), "ERC20: transfer from the zero address");
require(_recipient != address(0), "ERC20: transfer to the zero address");
_beforeTokenTransfer(_sender, _recipient, _amount);
unscaledBalances_[_sender] = unscaledBalances_[_sender].sub(_unscaledAmount, "ERC20: transfer amount exceeds balance");
unscaledBalances_[_recipient] = unscaledBalances_[_recipient].add(_unscaledAmount);
emit Transfer(_sender, _recipient, _amount);
}
function _mint(address _account, uint256 _amount) internal virtual
{
uint256 _unscaledAmount = _unscale(_amount, scalingFactor_);
require(_account != address(0), "ERC20: mint to the zero address");
_beforeTokenTransfer(address(0), _account, _amount);
unscaledTotalSupply_ = unscaledTotalSupply_.add(_unscaledAmount);
uint256 _maxScalingFactor = _calcMaxScalingFactor(unscaledTotalSupply_);
require(scalingFactor_ <= _maxScalingFactor, "unsupported scaling factor");
unscaledBalances_[_account] = unscaledBalances_[_account].add(_unscaledAmount);
emit Transfer(address(0), _account, _amount);
}
function _burn(address _account, uint256 _amount) internal virtual
{
uint256 _unscaledAmount = _unscale(_amount, scalingFactor_);
require(_account != address(0), "ERC20: burn from the zero address");
_beforeTokenTransfer(_account, address(0), _amount);
unscaledBalances_[_account] = unscaledBalances_[_account].sub(_unscaledAmount, "ERC20: burn amount exceeds balance");
unscaledTotalSupply_ = unscaledTotalSupply_.sub(_unscaledAmount);
emit Transfer(_account, address(0), _amount);
}
function _setupDecimals(uint8 _decimals) internal
{
decimals_ = _decimals;
scalingFactor_ = 10 ** uint256(_decimals);
}
function _beforeTokenTransfer(address _from, address _to, uint256 _amount) internal virtual { }
function unscaledTotalSupply() public view returns (uint256 _supply)
{
return unscaledTotalSupply_;
}
function unscaledBalanceOf(address _account) public view returns (uint256 _balance)
{
return unscaledBalances_[_account];
}
function scalingFactor() public view returns (uint256 _scalingFactor)
{
return scalingFactor_;
}
function maxScalingFactor() public view returns (uint256 _maxScalingFactor)
{
return _calcMaxScalingFactor(unscaledTotalSupply_);
}
function _calcMaxScalingFactor(uint256 _unscaledTotalSupply) internal pure returns (uint256 _maxScalingFactor)
{
return uint256(-1).div(_unscaledTotalSupply);
}
function _scale(uint256 _unscaledAmount, uint256 _scalingFactor) internal pure returns (uint256 _amount)
{
return _unscaledAmount.mul(_scalingFactor).div(UNSCALED_FACTOR);
}
function _unscale(uint256 _amount, uint256 _scalingFactor) internal pure returns (uint256 _unscaledAmount)
{
return _amount.mul(UNSCALED_FACTOR).div(_scalingFactor);
}
function _setScalingFactor(uint256 _scalingFactor) internal
{
uint256 _maxScalingFactor = _calcMaxScalingFactor(unscaledTotalSupply_);
require(0 < _scalingFactor && _scalingFactor <= _maxScalingFactor, "unsupported scaling factor");
scalingFactor_ = _scalingFactor;
}
}
pragma solidity ^0.6.0;
library Executor
{
struct Target {
address to;
bytes data;
}
function addTarget(Target[] storage _targets, address _to, bytes memory _data) internal
{
_targets.push(Target({ to: _to, data: _data }));
}
function removeTarget(Target[] storage _targets, uint256 _index) internal
{
require(_index < _targets.length, "invalid index");
_targets[_index] = _targets[_targets.length - 1];
_targets.pop();
}
function executeAll(Target[] storage _targets) internal
{
for (uint256 _i = 0; _i < _targets.length; _i++) {
Target storage _target = _targets[_i];
bool _success = _externalCall(_target.to, _target.data);
require(_success, "call failed");
}
}
function _externalCall(address _to, bytes memory _data) private returns (bool _success)
{
assembly {
_success := call(gas(), _to, 0, add(_data, 0x20), mload(_data), 0, 0)
}
}
}
pragma solidity ^0.6.0;
interface GElastic
{
function referenceToken() external view returns (address _referenceToken);
function treasury() external view returns (address _treasury);
function rebaseMinimumDeviation() external view returns (uint256 _rebaseMinimumDeviation);
function rebaseDampeningFactor() external view returns (uint256 _rebaseDampeningFactor);
function rebaseTreasuryMintPercent() external view returns (uint256 _rebaseTreasuryMintPercent);
function rebaseTimingParameters() external view returns (uint256 _rebaseMinimumInterval, uint256 _rebaseWindowOffset, uint256 _rebaseWindowLength);
function rebaseActive() external view returns (bool _rebaseActive);
function rebaseAvailable() external view returns (bool _available);
function lastRebaseTime() external view returns (uint256 _lastRebaseTime);
function epoch() external view returns (uint256 _epoch);
function lastExchangeRate() external view returns (uint256 _exchangeRate);
function currentExchangeRate() external view returns (uint256 _exchangeRate);
function pair() external view returns (address _pair);
function rebase() external;
function activateOracle(address _pair) external;
function activateRebase() external;
function setTreasury(address _newTreasury) external;
function setRebaseMinimumDeviation(uint256 _newRebaseMinimumDeviation) external;
function setRebaseDampeningFactor(uint256 _newRebaseDampeningFactor) external;
function setRebaseTreasuryMintPercent(uint256 _newRebaseTreasuryMintPercent) external;
function setRebaseTimingParameters(uint256 _newRebaseMinimumInterval, uint256 _newRebaseWindowOffset, uint256 _newRebaseWindowLength) external;
function addPostRebaseTarget(address _to, bytes memory _data) external;
function removePostRebaseTarget(uint256 _index) external;
event Rebase(uint256 indexed _epoch, uint256 _oldScalingFactor, uint256 _newScalingFactor);
event ChangeTreasury(address _oldTreasury, address _newTreasury);
event ChangeRebaseMinimumDeviation(uint256 _oldRebaseMinimumDeviation, uint256 _newRebaseMinimumDeviation);
event ChangeRebaseDampeningFactor(uint256 _oldRebaseDampeningFactor, uint256 _newRebaseDampeningFactor);
event ChangeRebaseTreasuryMintPercent(uint256 _oldRebaseTreasuryMintPercent, uint256 _newRebaseTreasuryMintPercent);
event ChangeRebaseTimingParameters(uint256 _oldRebaseMinimumInterval, uint256 _oldRebaseWindowOffset, uint256 _oldRebaseWindowLength, uint256 _newRebaseMinimumInterval, uint256 _newRebaseWindowOffset, uint256 _newRebaseWindowLength);
event AddPostRebaseTarget(address indexed _to, bytes _data);
event RemovePostRebaseTarget(address indexed _to, bytes _data);
}
pragma solidity ^0.6.0;
library GElasticTokenManager
{
using SafeMath for uint256;
using GElasticTokenManager for GElasticTokenManager.Self;
uint256 constant MAXIMUM_REBASE_TREASURY_MINT_PERCENT = 25e16;
uint256 constant DEFAULT_REBASE_MINIMUM_INTERVAL = 24 hours;
uint256 constant DEFAULT_REBASE_WINDOW_OFFSET = 17 hours;
uint256 constant DEFAULT_REBASE_WINDOW_LENGTH = 1 hours;
uint256 constant DEFAULT_REBASE_MINIMUM_DEVIATION = 5e16;
uint256 constant DEFAULT_REBASE_DAMPENING_FACTOR = 10;
uint256 constant DEFAULT_REBASE_TREASURY_MINT_PERCENT = 10e16;
struct Self {
address treasury;
uint256 rebaseMinimumDeviation;
uint256 rebaseDampeningFactor;
uint256 rebaseTreasuryMintPercent;
uint256 rebaseMinimumInterval;
uint256 rebaseWindowOffset;
uint256 rebaseWindowLength;
bool rebaseActive;
uint256 lastRebaseTime;
uint256 epoch;
}
function init(Self storage _self, address _treasury) public
{
_self.treasury = _treasury;
_self.rebaseMinimumDeviation = DEFAULT_REBASE_MINIMUM_DEVIATION;
_self.rebaseDampeningFactor = DEFAULT_REBASE_DAMPENING_FACTOR;
_self.rebaseTreasuryMintPercent = DEFAULT_REBASE_TREASURY_MINT_PERCENT;
_self.rebaseMinimumInterval = DEFAULT_REBASE_MINIMUM_INTERVAL;
_self.rebaseWindowOffset = DEFAULT_REBASE_WINDOW_OFFSET;
_self.rebaseWindowLength = DEFAULT_REBASE_WINDOW_LENGTH;
_self.rebaseActive = false;
_self.lastRebaseTime = 0;
_self.epoch = 0;
}
function activateRebase(Self storage _self) public
{
require(!_self.rebaseActive, "already active");
_self.rebaseActive = true;
_self.lastRebaseTime = now.sub(now.mod(_self.rebaseMinimumInterval)).add(_self.rebaseWindowOffset);
}
function setTreasury(Self storage _self, address _treasury) public
{
require(_treasury != address(0), "invalid treasury");
_self.treasury = _treasury;
}
function setRebaseMinimumDeviation(Self storage _self, uint256 _rebaseMinimumDeviation) public
{
require(_rebaseMinimumDeviation > 0, "invalid minimum deviation");
_self.rebaseMinimumDeviation = _rebaseMinimumDeviation;
}
function setRebaseDampeningFactor(Self storage _self, uint256 _rebaseDampeningFactor) public
{
require(_rebaseDampeningFactor > 0, "invalid dampening factor");
_self.rebaseDampeningFactor = _rebaseDampeningFactor;
}
function setRebaseTreasuryMintPercent(Self storage _self, uint256 _rebaseTreasuryMintPercent) public
{
require(_rebaseTreasuryMintPercent <= MAXIMUM_REBASE_TREASURY_MINT_PERCENT, "invalid percent");
_self.rebaseTreasuryMintPercent = _rebaseTreasuryMintPercent;
}
function setRebaseTimingParameters(Self storage _self, uint256 _rebaseMinimumInterval, uint256 _rebaseWindowOffset, uint256 _rebaseWindowLength) public
{
require(_rebaseMinimumInterval > 0, "invalid interval");
require(_rebaseWindowOffset.add(_rebaseWindowLength) <= _rebaseMinimumInterval, "invalid window");
_self.rebaseMinimumInterval = _rebaseMinimumInterval;
_self.rebaseWindowOffset = _rebaseWindowOffset;
_self.rebaseWindowLength = _rebaseWindowLength;
}
function rebaseAvailable(Self storage _self) public view returns (bool _available)
{
return _self._rebaseAvailable();
}
function rebase(Self storage _self, uint256 _exchangeRate, uint256 _totalSupply) public returns (uint256 _delta, bool _positive, uint256 _mintAmount)
{
require(_self._rebaseAvailable(), "not available");
_self.lastRebaseTime = now.sub(now.mod(_self.rebaseMinimumInterval)).add(_self.rebaseWindowOffset);
_self.epoch = _self.epoch.add(1);
_positive = _exchangeRate > 1e18;
uint256 _deviation = _positive ? _exchangeRate.sub(1e18) : uint256(1e18).sub(_exchangeRate);
if (_deviation < _self.rebaseMinimumDeviation) {
_deviation = 0;
_positive = false;
}
_delta = _deviation.div(_self.rebaseDampeningFactor);
_mintAmount = 0;
if (_positive) {
uint256 _mintPercent = _delta.mul(_self.rebaseTreasuryMintPercent).div(1e18);
_delta = _delta.sub(_mintPercent);
_mintAmount = _totalSupply.mul(_mintPercent).div(1e18);
}
return (_delta, _positive, _mintAmount);
}
function _rebaseAvailable(Self storage _self) internal view returns (bool _available)
{
if (!_self.rebaseActive) return false;
if (now < _self.lastRebaseTime.add(_self.rebaseMinimumInterval)) return false;
uint256 _offset = now.mod(_self.rebaseMinimumInterval);
return _self.rebaseWindowOffset <= _offset && _offset < _self.rebaseWindowOffset.add(_self.rebaseWindowLength);
}
}
pragma solidity >=0.4.0;
library FullMath {
function fullMul(uint256 x, uint256 y) internal 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;
if (h == 0) return l / d;
require(h < d, 'FullMath: FULLDIV_OVERFLOW');
return fullDiv(l, h, d);
}
}
pragma solidity >=0.4.0;
library Babylonian {
function sqrt(uint256 x) internal pure returns (uint256) {
if (x == 0) return 0;
uint256 xx = x;
uint256 r = 1;
if (xx >= 0x100000000000000000000000000000000) {
xx >>= 128;
r <<= 64;
}
if (xx >= 0x10000000000000000) {
xx >>= 64;
r <<= 32;
}
if (xx >= 0x100000000) {
xx >>= 32;
r <<= 16;
}
if (xx >= 0x10000) {
xx >>= 16;
r <<= 8;
}
if (xx >= 0x100) {
xx >>= 8;
r <<= 4;
}
if (xx >= 0x10) {
xx >>= 4;
r <<= 2;
}
if (xx >= 0x8) {
r <<= 1;
}
r = (r + x / r) >> 1;
r = (r + x / r) >> 1;
r = (r + x / r) >> 1;
r = (r + x / r) >> 1;
r = (r + x / r) >> 1;
r = (r + x / r) >> 1;
r = (r + x / r) >> 1;
uint256 r1 = x / r;
return (r < r1 ? r : r1);
}
}
pragma solidity >=0.5.0;
library BitMath {
function mostSignificantBit(uint256 x) internal pure returns (uint8 r) {
require(x > 0, 'BitMath::mostSignificantBit: zero');
if (x >= 0x100000000000000000000000000000000) {
x >>= 128;
r += 128;
}
if (x >= 0x10000000000000000) {
x >>= 64;
r += 64;
}
if (x >= 0x100000000) {
x >>= 32;
r += 32;
}
if (x >= 0x10000) {
x >>= 16;
r += 16;
}
if (x >= 0x100) {
x >>= 8;
r += 8;
}
if (x >= 0x10) {
x >>= 4;
r += 4;
}
if (x >= 0x4) {
x >>= 2;
r += 2;
}
if (x >= 0x2) r += 1;
}
function leastSignificantBit(uint256 x) internal pure returns (uint8 r) {
require(x > 0, 'BitMath::leastSignificantBit: zero');
r = 255;
if (x & uint128(-1) > 0) {
r -= 128;
} else {
x >>= 128;
}
if (x & uint64(-1) > 0) {
r -= 64;
} else {
x >>= 64;
}
if (x & uint32(-1) > 0) {
r -= 32;
} else {
x >>= 32;
}
if (x & uint16(-1) > 0) {
r -= 16;
} else {
x >>= 16;
}
if (x & uint8(-1) > 0) {
r -= 8;
} else {
x >>= 8;
}
if (x & 0xf > 0) {
r -= 4;
} else {
x >>= 4;
}
if (x & 0x3 > 0) {
r -= 2;
} else {
x >>= 2;
}
if (x & 0x1 > 0) r -= 1;
}
}
pragma solidity >=0.4.0;
library FixedPoint {
struct uq112x112 {
uint224 _x;
}
struct uq144x112 {
uint256 _x;
}
uint8 public constant RESOLUTION = 112;
uint256 public constant Q112 = 0x10000000000000000000000000000;
uint256 private constant Q224 = 0x100000000000000000000000000000000000000000000000000000000;
uint256 private constant LOWER_MASK = 0xffffffffffffffffffffffffffff;
function encode(uint112 x) internal pure returns (uq112x112 memory) {
return uq112x112(uint224(x) << RESOLUTION);
}
function encode144(uint144 x) internal pure returns (uq144x112 memory) {
return uq144x112(uint256(x) << RESOLUTION);
}
function decode(uq112x112 memory self) internal pure returns (uint112) {
return uint112(self._x >> RESOLUTION);
}
function decode144(uq144x112 memory self) internal pure returns (uint144) {
return uint144(self._x >> RESOLUTION);
}
function mul(uq112x112 memory self, uint256 y) internal pure returns (uq144x112 memory) {
uint256 z = 0;
require(y == 0 || (z = self._x * y) / y == self._x, 'FixedPoint::mul: overflow');
return uq144x112(z);
}
function muli(uq112x112 memory self, int256 y) internal pure returns (int256) {
uint256 z = FullMath.mulDiv(self._x, uint256(y < 0 ? -y : y), Q112);
require(z < 2**255, 'FixedPoint::muli: overflow');
return y < 0 ? -int256(z) : int256(z);
}
function muluq(uq112x112 memory self, uq112x112 memory other) internal pure returns (uq112x112 memory) {
if (self._x == 0 || other._x == 0) {
return uq112x112(0);
}
uint112 upper_self = uint112(self._x >> RESOLUTION);
uint112 lower_self = uint112(self._x & LOWER_MASK);
uint112 upper_other = uint112(other._x >> RESOLUTION);
uint112 lower_other = uint112(other._x & LOWER_MASK);
uint224 upper = uint224(upper_self) * upper_other;
uint224 lower = uint224(lower_self) * lower_other;
uint224 uppers_lowero = uint224(upper_self) * lower_other;
uint224 uppero_lowers = uint224(upper_other) * lower_self;
require(upper <= uint112(-1), 'FixedPoint::muluq: upper overflow');
uint256 sum = uint256(upper << RESOLUTION) + uppers_lowero + uppero_lowers + (lower >> RESOLUTION);
require(sum <= uint224(-1), 'FixedPoint::muluq: sum overflow');
return uq112x112(uint224(sum));
}
function divuq(uq112x112 memory self, uq112x112 memory other) internal pure returns (uq112x112 memory) {
require(other._x > 0, 'FixedPoint::divuq: division by zero');
if (self._x == other._x) {
return uq112x112(uint224(Q112));
}
if (self._x <= uint144(-1)) {
uint256 value = (uint256(self._x) << RESOLUTION) / other._x;
require(value <= uint224(-1), 'FixedPoint::divuq: overflow');
return uq112x112(uint224(value));
}
uint256 result = FullMath.mulDiv(Q112, self._x, other._x);
require(result <= uint224(-1), 'FixedPoint::divuq: overflow');
return uq112x112(uint224(result));
}
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));
}
}
function reciprocal(uq112x112 memory self) internal pure returns (uq112x112 memory) {
require(self._x != 0, 'FixedPoint::reciprocal: reciprocal of zero');
require(self._x != 1, 'FixedPoint::reciprocal: overflow');
return uq112x112(uint224(Q224 / self._x));
}
function sqrt(uq112x112 memory self) internal pure returns (uq112x112 memory) {
if (self._x <= uint144(-1)) {
return uq112x112(uint224(Babylonian.sqrt(uint256(self._x) << 112)));
}
uint8 safeShiftBits = 255 - BitMath.mostSignificantBit(self._x);
safeShiftBits -= safeShiftBits % 2;
return uq112x112(uint224(Babylonian.sqrt(uint256(self._x) << safeShiftBits) << ((112 - safeShiftBits) / 2)));
}
}
pragma solidity >=0.5.0;
interface IUniswapV2Pair {
event Approval(address indexed owner, address indexed spender, uint value);
event Transfer(address indexed from, address indexed to, uint value);
function name() external pure returns (string memory);
function symbol() external pure returns (string memory);
function decimals() external pure returns (uint8);
function totalSupply() external view returns (uint);
function balanceOf(address owner) external view returns (uint);
function allowance(address owner, address spender) external view returns (uint);
function approve(address spender, 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 DOMAIN_SEPARATOR() external view returns (bytes32);
function PERMIT_TYPEHASH() external pure returns (bytes32);
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;
event Mint(address indexed sender, uint amount0, uint amount1);
event Burn(address indexed sender, uint amount0, uint amount1, address indexed to);
event Swap(
address indexed sender,
uint amount0In,
uint amount1In,
uint amount0Out,
uint amount1Out,
address indexed to
);
event Sync(uint112 reserve0, uint112 reserve1);
function MINIMUM_LIQUIDITY() external pure returns (uint);
function factory() external view returns (address);
function token0() external view returns (address);
function token1() external view returns (address);
function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
function price0CumulativeLast() external view returns (uint);
function price1CumulativeLast() external view returns (uint);
function kLast() external view returns (uint);
function mint(address to) external returns (uint liquidity);
function burn(address to) external returns (uint amount0, uint amount1);
function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
function skim(address to) external;
function sync() external;
function initialize(address, address) external;
}
pragma solidity >=0.5.0;
library UniswapV2OracleLibrary {
using FixedPoint for *;
function currentBlockTimestamp() internal view returns (uint32) {
return uint32(block.timestamp % 2 ** 32);
}
function currentCumulativePrices(
address pair
) internal view returns (uint price0Cumulative, uint price1Cumulative, uint32 blockTimestamp) {
blockTimestamp = currentBlockTimestamp();
price0Cumulative = IUniswapV2Pair(pair).price0CumulativeLast();
price1Cumulative = IUniswapV2Pair(pair).price1CumulativeLast();
(uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast) = IUniswapV2Pair(pair).getReserves();
if (blockTimestampLast != blockTimestamp) {
uint32 timeElapsed = blockTimestamp - blockTimestampLast;
price0Cumulative += uint(FixedPoint.fraction(reserve1, reserve0)._x) * timeElapsed;
price1Cumulative += uint(FixedPoint.fraction(reserve0, reserve1)._x) * timeElapsed;
}
}
}
pragma solidity ^0.6.0;
interface Factory
{
function getPair(address _tokenA, address _tokenB) external view returns (address _pair);
function createPair(address _tokenA, address _tokenB) external returns (address _pair);
}
interface PoolToken is IERC20
{
}
interface Pair is PoolToken
{
function token0() external view returns (address _token0);
function token1() external view returns (address _token1);
function price0CumulativeLast() external view returns (uint256 _price0CumulativeLast);
function price1CumulativeLast() external view returns (uint256 _price1CumulativeLast);
function getReserves() external view returns (uint112 _reserve0, uint112 _reserve1, uint32 _blockTimestampLast);
function mint(address _to) external returns (uint256 _liquidity);
function sync() external;
}
interface Router01
{
function WETH() external pure returns (address _token);
function addLiquidity(address _tokenA, address _tokenB, uint256 _amountADesired, uint256 _amountBDesired, uint256 _amountAMin, uint256 _amountBMin, address _to, uint256 _deadline) external returns (uint256 _amountA, uint256 _amountB, uint256 _liquidity);
function removeLiquidity(address _tokenA, address _tokenB, uint256 _liquidity, uint256 _amountAMin, uint256 _amountBMin, address _to, uint256 _deadline) external returns (uint256 _amountA, uint256 _amountB);
function swapExactTokensForTokens(uint256 _amountIn, uint256 _amountOutMin, address[] calldata _path, address _to, uint256 _deadline) external returns (uint256[] memory _amounts);
function swapETHForExactTokens(uint256 _amountOut, address[] calldata _path, address _to, uint256 _deadline) external payable returns (uint256[] memory _amounts);
function getAmountOut(uint256 _amountIn, uint256 _reserveIn, uint256 _reserveOut) external pure returns (uint256 _amountOut);
}
interface Router02 is Router01
{
}
pragma solidity ^0.6.0;
library GPriceOracle
{
using FixedPoint for FixedPoint.uq112x112;
using FixedPoint for FixedPoint.uq144x112;
using GPriceOracle for GPriceOracle.Self;
uint256 constant DEFAULT_MINIMUM_INTERVAL = 23 hours;
struct Self {
address pair;
bool use0;
uint256 minimumInterval;
uint256 priceCumulativeLast;
uint32 blockTimestampLast;
FixedPoint.uq112x112 priceAverage;
}
function init(Self storage _self) public
{
_self.pair = address(0);
_self.minimumInterval = DEFAULT_MINIMUM_INTERVAL;
}
function active(Self storage _self) public view returns (bool _isActive)
{
return _self._active();
}
function activate(Self storage _self, address _pair, bool _use0) public
{
require(!_self._active(), "already active");
require(_pair != address(0), "invalid pair");
_self.pair = _pair;
_self.use0 = _use0;
_self.priceCumulativeLast = _use0 ? Pair(_pair).price0CumulativeLast() : Pair(_pair).price1CumulativeLast();
uint112 reserve0;
uint112 reserve1;
(reserve0, reserve1, _self.blockTimestampLast) = Pair(_pair).getReserves();
require(reserve0 > 0 && reserve1 > 0, "no reserves");
}
function changeMinimumInterval(Self storage _self, uint256 _minimumInterval) public
{
require(_minimumInterval > 0, "invalid interval");
_self.minimumInterval = _minimumInterval;
}
function consultLastPrice(Self storage _self, uint256 _amountIn) public view returns (uint256 _amountOut)
{
require(_self._active(), "not active");
return _self.priceAverage.mul(_amountIn).decode144();
}
function consultCurrentPrice(Self storage _self, uint256 _amountIn) public view returns (uint256 _amountOut)
{
require(_self._active(), "not active");
(,, FixedPoint.uq112x112 memory _priceAverage) = _self._estimatePrice(false);
return _priceAverage.mul(_amountIn).decode144();
}
function updatePrice(Self storage _self) public
{
require(_self._active(), "not active");
(_self.priceCumulativeLast, _self.blockTimestampLast, _self.priceAverage) = _self._estimatePrice(true);
}
function _active(Self storage _self) internal view returns (bool _isActive)
{
return _self.pair != address(0);
}
function _estimatePrice(Self storage _self, bool _enforceTimeElapsed) internal view returns (uint256 _priceCumulative, uint32 _blockTimestamp, FixedPoint.uq112x112 memory _priceAverage)
{
uint256 _price0Cumulative;
uint256 _price1Cumulative;
(_price0Cumulative, _price1Cumulative, _blockTimestamp) = UniswapV2OracleLibrary.currentCumulativePrices(_self.pair);
_priceCumulative = _self.use0 ? _price0Cumulative : _price1Cumulative;
uint32 _timeElapsed = _blockTimestamp - _self.blockTimestampLast;
if (_enforceTimeElapsed) {
require(_timeElapsed >= _self.minimumInterval, "minimum interval not elapsed");
}
_priceAverage = FixedPoint.uq112x112(uint224((_priceCumulative - _self.priceCumulativeLast) / _timeElapsed));
}
}
pragma solidity ^0.6.0;
library Math
{
function _min(uint256 _amount1, uint256 _amount2) internal pure returns (uint256 _minAmount)
{
return _amount1 < _amount2 ? _amount1 : _amount2;
}
function _max(uint256 _amount1, uint256 _amount2) internal pure returns (uint256 _maxAmount)
{
return _amount1 > _amount2 ? _amount1 : _amount2;
}
}
pragma solidity ^0.6.0;
contract GElasticToken is ElasticERC20, Ownable, ReentrancyGuard, GElastic
{
using SafeMath for uint256;
using GElasticTokenManager for GElasticTokenManager.Self;
using GPriceOracle for GPriceOracle.Self;
using Executor for Executor.Target[];
address public immutable override referenceToken;
GElasticTokenManager.Self etm;
GPriceOracle.Self oracle;
Executor.Target[] public targets;
modifier onlyEOA()
{
require(tx.origin == _msgSender(), "not an externally owned account");
_;
}
constructor (string memory _name, string memory _symbol, uint8 _decimals, address _referenceToken, uint256 _initialSupply)
ElasticERC20(_name, _symbol) public
{
address _treasury = msg.sender;
_setupDecimals(_decimals);
assert(_referenceToken != address(0));
referenceToken = _referenceToken;
etm.init(_treasury);
oracle.init();
_mint(_treasury, _initialSupply);
}
function treasury() external view override returns (address _treasury)
{
return etm.treasury;
}
function rebaseMinimumDeviation() external view override returns (uint256 _rebaseMinimumDeviation)
{
return etm.rebaseMinimumDeviation;
}
function rebaseDampeningFactor() external view override returns (uint256 _rebaseDampeningFactor)
{
return etm.rebaseDampeningFactor;
}
function rebaseTreasuryMintPercent() external view override returns (uint256 _rebaseTreasuryMintPercent)
{
return etm.rebaseTreasuryMintPercent;
}
function rebaseTimingParameters() external view override returns (uint256 _rebaseMinimumInterval, uint256 _rebaseWindowOffset, uint256 _rebaseWindowLength)
{
return (etm.rebaseMinimumInterval, etm.rebaseWindowOffset, etm.rebaseWindowLength);
}
function rebaseAvailable() external view override returns (bool _rebaseAvailable)
{
return etm.rebaseAvailable();
}
function rebaseActive() external view override returns (bool _rebaseActive)
{
return etm.rebaseActive;
}
function lastRebaseTime() external view override returns (uint256 _lastRebaseTime)
{
return etm.lastRebaseTime;
}
function epoch() external view override returns (uint256 _epoch)
{
return etm.epoch;
}
function lastExchangeRate() external view override returns (uint256 _exchangeRate)
{
return oracle.consultLastPrice(10 ** uint256(decimals()));
}
function currentExchangeRate() external view override returns (uint256 _exchangeRate)
{
return oracle.consultCurrentPrice(10 ** uint256(decimals()));
}
function pair() external view override returns (address _pair)
{
return oracle.pair;
}
function rebase() external override onlyEOA nonReentrant
{
oracle.updatePrice();
uint256 _exchangeRate = oracle.consultLastPrice(10 ** uint256(decimals()));
uint256 _totalSupply = totalSupply();
(uint256 _delta, bool _positive, uint256 _mintAmount) = etm.rebase(_exchangeRate, _totalSupply);
_rebase(etm.epoch, _delta, _positive);
if (_mintAmount > 0) {
_mint(etm.treasury, _mintAmount);
}
Pair(oracle.pair).sync();
targets.executeAll();
}
function activateOracle(address _pair) external override onlyOwner nonReentrant
{
address _token0 = Pair(_pair).token0();
address _token1 = Pair(_pair).token1();
require(_token0 == address(this) && _token1 == referenceToken || _token1 == address(this) && _token0 == referenceToken, "invalid pair");
oracle.activate(_pair, _token0 == address(this));
}
function activateRebase() external override onlyOwner nonReentrant
{
require(oracle.active(), "not available");
etm.activateRebase();
}
function setTreasury(address _newTreasury) external override onlyOwner nonReentrant
{
address _oldTreasury = etm.treasury;
etm.setTreasury(_newTreasury);
emit ChangeTreasury(_oldTreasury, _newTreasury);
}
function setRebaseMinimumDeviation(uint256 _newRebaseMinimumDeviation) external override onlyOwner nonReentrant
{
uint256 _oldRebaseMinimumDeviation = etm.rebaseMinimumDeviation;
etm.setRebaseMinimumDeviation(_newRebaseMinimumDeviation);
emit ChangeRebaseMinimumDeviation(_oldRebaseMinimumDeviation, _newRebaseMinimumDeviation);
}
function setRebaseDampeningFactor(uint256 _newRebaseDampeningFactor) external override onlyOwner nonReentrant
{
uint256 _oldRebaseDampeningFactor = etm.rebaseDampeningFactor;
etm.setRebaseDampeningFactor(_newRebaseDampeningFactor);
emit ChangeRebaseDampeningFactor(_oldRebaseDampeningFactor, _newRebaseDampeningFactor);
}
function setRebaseTreasuryMintPercent(uint256 _newRebaseTreasuryMintPercent) external override onlyOwner nonReentrant
{
uint256 _oldRebaseTreasuryMintPercent = etm.rebaseTreasuryMintPercent;
etm.setRebaseTreasuryMintPercent(_newRebaseTreasuryMintPercent);
emit ChangeRebaseTreasuryMintPercent(_oldRebaseTreasuryMintPercent, _newRebaseTreasuryMintPercent);
}
function setRebaseTimingParameters(uint256 _newRebaseMinimumInterval, uint256 _newRebaseWindowOffset, uint256 _newRebaseWindowLength) external override onlyOwner nonReentrant
{
uint256 _oldRebaseMinimumInterval = etm.rebaseMinimumInterval;
uint256 _oldRebaseWindowOffset = etm.rebaseWindowOffset;
uint256 _oldRebaseWindowLength = etm.rebaseWindowLength;
etm.setRebaseTimingParameters(_newRebaseMinimumInterval, _newRebaseWindowOffset, _newRebaseWindowLength);
oracle.changeMinimumInterval(_newRebaseMinimumInterval.sub(_newRebaseWindowLength));
emit ChangeRebaseTimingParameters(_oldRebaseMinimumInterval, _oldRebaseWindowOffset, _oldRebaseWindowLength, _newRebaseMinimumInterval, _newRebaseWindowOffset, _newRebaseWindowLength);
}
function addPostRebaseTarget(address _to, bytes memory _data) external override onlyOwner nonReentrant
{
_addPostRebaseTarget(_to, _data);
}
function removePostRebaseTarget(uint256 _index) external override onlyOwner nonReentrant
{
_removePostRebaseTarget(_index);
}
function addBalancerPostRebaseTarget(address _pool) external onlyOwner nonReentrant
{
_addPostRebaseTarget(_pool, abi.encodeWithSignature("gulp(address)", address(this)));
}
function addUniswapV2PostRebaseTarget(address _pair) external onlyOwner nonReentrant
{
_addPostRebaseTarget(_pair, abi.encodeWithSignature("sync()"));
}
function _addPostRebaseTarget(address _to, bytes memory _data) internal
{
targets.addTarget(_to, _data);
emit AddPostRebaseTarget(_to, _data);
}
function _removePostRebaseTarget(uint256 _index) internal
{
Executor.Target storage _target = targets[_index];
address _to = _target.to;
bytes memory _data = _target.data;
targets.removeTarget(_index);
emit RemovePostRebaseTarget(_to, _data);
}
function _rebase(uint256 _epoch, uint256 _delta, bool _positive) internal virtual
{
uint256 _oldScalingFactor = scalingFactor();
uint256 _newScalingFactor;
if (_delta == 0) {
_newScalingFactor = _oldScalingFactor;
} else {
if (_positive) {
_newScalingFactor = _oldScalingFactor.mul(uint256(1e18).add(_delta)).div(1e18);
} else {
_newScalingFactor = _oldScalingFactor.mul(uint256(1e18).sub(_delta)).div(1e18);
}
}
if (_newScalingFactor > _oldScalingFactor) {
_newScalingFactor = Math._min(_newScalingFactor, maxScalingFactor());
}
_setScalingFactor(_newScalingFactor);
emit Rebase(_epoch, _oldScalingFactor, _newScalingFactor);
}
}
pragma solidity >=0.6.0 <0.8.0;
contract ERC20 is Context, IERC20 {
using SafeMath for uint256;
mapping (address => uint256) private _balances;
mapping (address => mapping (address => uint256)) private _allowances;
uint256 private _totalSupply;
string private _name;
string private _symbol;
uint8 private _decimals;
constructor (string memory name_, string memory symbol_) public {
_name = name_;
_symbol = symbol_;
_decimals = 18;
}
function name() public view returns (string memory) {
return _name;
}
function symbol() public view returns (string memory) {
return _symbol;
}
function decimals() public view returns (uint8) {
return _decimals;
}
function totalSupply() public view override returns (uint256) {
return _totalSupply;
}
function balanceOf(address account) public view override returns (uint256) {
return _balances[account];
}
function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
_transfer(_msgSender(), 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(_msgSender(), spender, amount);
return true;
}
function transferFrom(address sender, address recipient, uint256 amount) public virtual override returns (bool) {
_transfer(sender, recipient, amount);
_approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "ERC20: transfer amount exceeds allowance"));
return true;
}
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
return true;
}
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][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 amount) internal virtual {
require(account != address(0), "ERC20: mint to the zero address");
_beforeTokenTransfer(address(0), account, amount);
_totalSupply = _totalSupply.add(amount);
_balances[account] = _balances[account].add(amount);
emit Transfer(address(0), account, amount);
}
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 _setupDecimals(uint8 decimals_) internal {
_decimals = decimals_;
}
function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual { }
}
pragma solidity ^0.6.0;
interface GLPMining
{
function reserveToken() external view returns (address _reserveToken);
function rewardsToken() external view returns (address _rewardsToken);
function treasury() external view returns (address _treasury);
function performanceFee() external view returns (uint256 _performanceFee);
function rewardRatePerWeek() external view returns (uint256 _rewardRatePerWeek);
function calcSharesFromCost(uint256 _cost) external view returns (uint256 _shares);
function calcCostFromShares(uint256 _shares) external view returns (uint256 _cost);
function calcSharesFromTokenAmount(address _token, uint256 _amount) external view returns (uint256 _shares);
function calcTokenAmountFromShares(address _token, uint256 _shares) external view returns (uint256 _amount);
function totalReserve() external view returns (uint256 _totalReserve);
function rewardInfo() external view returns (uint256 _lockedReward, uint256 _unlockedReward, uint256 _rewardPerBlock);
function pendingFees() external view returns (uint256 _feeShares);
function deposit(uint256 _cost) external;
function withdraw(uint256 _shares) external;
function depositToken(address _token, uint256 _amount, uint256 _minShares) external;
function withdrawToken(address _token, uint256 _shares, uint256 _minAmount) external;
function gulpRewards(uint256 _minCost) external;
function gulpFees() external;
function setTreasury(address _treasury) external;
function setPerformanceFee(uint256 _performanceFee) external;
function setRewardRatePerWeek(uint256 _rewardRatePerWeek) external;
event ChangeTreasury(address _oldTreasury, address _newTreasury);
event ChangePerformanceFee(uint256 _oldPerformanceFee, uint256 _newPerformanceFee);
event ChangeRewardRatePerWeek(uint256 _oldRewardRatePerWeek, uint256 _newRewardRatePerWeek);
}
pragma solidity >=0.6.2 <0.8.0;
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 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 _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);
}
}
}
}
pragma solidity >=0.6.0 <0.8.0;
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");
}
}
}
pragma solidity ^0.6.0;
library Transfers
{
using SafeERC20 for IERC20;
function _getBalance(address _token) internal view returns (uint256 _balance)
{
return IERC20(_token).balanceOf(address(this));
}
function _approveFunds(address _token, address _to, uint256 _amount) internal
{
uint256 _allowance = IERC20(_token).allowance(address(this), _to);
if (_allowance > _amount) {
IERC20(_token).safeDecreaseAllowance(_to, _allowance - _amount);
}
else
if (_allowance < _amount) {
IERC20(_token).safeIncreaseAllowance(_to, _amount - _allowance);
}
}
function _pullFunds(address _token, address _from, uint256 _amount) internal
{
if (_amount == 0) return;
IERC20(_token).safeTransferFrom(_from, address(this), _amount);
}
function _pushFunds(address _token, address _to, uint256 _amount) internal
{
if (_amount == 0) return;
IERC20(_token).safeTransfer(_to, _amount);
}
}
pragma solidity ^0.6.0;
library $
{
address constant AAVE = 0x7Fc66500c84A76Ad7e9c93437bFc5Ac33E2DDaE9;
address constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
address constant UniswapV2_FACTORY = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;
address constant UniswapV2_ROUTER02 = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
}
pragma solidity ^0.6.0;
library UniswapV2LiquidityPoolAbstraction
{
using SafeMath for uint256;
function _estimateJoinPool(address _pair, address _token, uint256 _amount) internal view returns (uint256 _shares)
{
if (_amount == 0) return 0;
address _router = $.UniswapV2_ROUTER02;
address _token0 = Pair(_pair).token0();
(uint256 _reserve0, uint256 _reserve1,) = Pair(_pair).getReserves();
uint256 _balance = _token == _token0 ? _reserve0 : _reserve1;
uint256 _otherBalance = _token == _token0 ? _reserve1 : _reserve0;
uint256 _totalSupply = Pair(_pair).totalSupply();
uint256 _swapAmount = _calcSwapOutputFromInput(_balance, _amount);
if (_swapAmount == 0) _swapAmount = _amount / 2;
uint256 _leftAmount = _amount.sub(_swapAmount);
uint256 _otherAmount = Router02(_router).getAmountOut(_swapAmount, _balance, _otherBalance);
_shares = Math._min(_totalSupply.mul(_leftAmount) / _balance.add(_swapAmount), _totalSupply.mul(_otherAmount) / _otherBalance.sub(_otherAmount));
return _shares;
}
function _estimateExitPool(address _pair, address _token, uint256 _shares) internal view returns (uint256 _amount)
{
if (_shares == 0) return 0;
address _router = $.UniswapV2_ROUTER02;
address _token0 = Pair(_pair).token0();
(uint256 _reserve0, uint256 _reserve1,) = Pair(_pair).getReserves();
uint256 _balance = _token == _token0 ? _reserve0 : _reserve1;
uint256 _otherBalance = _token == _token0 ? _reserve1 : _reserve0;
uint256 _totalSupply = Pair(_pair).totalSupply();
uint256 _baseAmount = _balance.mul(_shares) / _totalSupply;
uint256 _swapAmount = _otherBalance.mul(_shares) / _totalSupply;
uint256 _additionalAmount = Router02(_router).getAmountOut(_swapAmount, _otherBalance.sub(_swapAmount), _balance.sub(_baseAmount));
_amount = _baseAmount.add(_additionalAmount);
return _amount;
}
function _joinPool(address _pair, address _token, uint256 _amount, uint256 _minShares) internal returns (uint256 _shares)
{
if (_amount == 0) return 0;
address _router = $.UniswapV2_ROUTER02;
address _token0 = Pair(_pair).token0();
address _token1 = Pair(_pair).token1();
address _otherToken = _token == _token0 ? _token1 : _token0;
(uint256 _reserve0, uint256 _reserve1,) = Pair(_pair).getReserves();
uint256 _swapAmount = _calcSwapOutputFromInput(_token == _token0 ? _reserve0 : _reserve1, _amount);
if (_swapAmount == 0) _swapAmount = _amount / 2;
uint256 _leftAmount = _amount.sub(_swapAmount);
Transfers._approveFunds(_token, _router, _amount);
address[] memory _path = new address[](2);
_path[0] = _token;
_path[1] = _otherToken;
uint256 _otherAmount = Router02(_router).swapExactTokensForTokens(_swapAmount, 1, _path, address(this), uint256(-1))[1];
Transfers._approveFunds(_otherToken, _router, _otherAmount);
(,,_shares) = Router02(_router).addLiquidity(_token, _otherToken, _leftAmount, _otherAmount, 1, 1, address(this), uint256(-1));
require(_shares >= _minShares, "high slippage");
return _shares;
}
function _exitPool(address _pair, address _token, uint256 _shares, uint256 _minAmount) internal returns (uint256 _amount)
{
if (_shares == 0) return 0;
address _router = $.UniswapV2_ROUTER02;
address _token0 = Pair(_pair).token0();
address _token1 = Pair(_pair).token1();
address _otherToken = _token == _token0 ? _token1 : _token0;
Transfers._approveFunds(_pair, _router, _shares);
(uint256 _baseAmount, uint256 _swapAmount) = Router02(_router).removeLiquidity(_token, _otherToken, _shares, 1, 1, address(this), uint256(-1));
Transfers._approveFunds(_otherToken, _router, _swapAmount);
address[] memory _path = new address[](2);
_path[0] = _otherToken;
_path[1] = _token;
uint256 _additionalAmount = Router02(_router).swapExactTokensForTokens(_swapAmount, 1, _path, address(this), uint256(-1))[1];
_amount = _baseAmount.add(_additionalAmount);
require(_amount >= _minAmount, "high slippage");
return _amount;
}
function _calcSwapOutputFromInput(uint256 _reserveAmount, uint256 _inputAmount) private pure returns (uint256)
{
return Babylonian.sqrt(_reserveAmount.mul(_inputAmount.mul(3988000).add(_reserveAmount.mul(3988009)))).sub(_reserveAmount.mul(1997)) / 1994;
}
}
pragma solidity ^0.6.0;
contract GLPMiningToken is ERC20, Ownable, ReentrancyGuard, GLPMining
{
uint256 constant MAXIMUM_PERFORMANCE_FEE = 50e16;
uint256 constant BLOCKS_PER_WEEK = 7 days / uint256(13 seconds);
uint256 constant DEFAULT_PERFORMANCE_FEE = 10e16;
uint256 constant DEFAULT_REWARD_RATE_PER_WEEK = 10e16;
address public immutable override reserveToken;
address public immutable override rewardsToken;
address public override treasury;
uint256 public override performanceFee = DEFAULT_PERFORMANCE_FEE;
uint256 public override rewardRatePerWeek = DEFAULT_REWARD_RATE_PER_WEEK;
uint256 lastContractBlock = block.number;
uint256 lastRewardPerBlock = 0;
uint256 lastUnlockedReward = 0;
uint256 lastLockedReward = 0;
uint256 lastTotalSupply = 1;
uint256 lastTotalReserve = 1;
constructor (string memory _name, string memory _symbol, uint8 _decimals, address _reserveToken, address _rewardsToken)
ERC20(_name, _symbol) public
{
address _treasury = msg.sender;
_setupDecimals(_decimals);
assert(_reserveToken != address(0));
assert(_rewardsToken != address(0));
assert(_reserveToken != _rewardsToken);
reserveToken = _reserveToken;
rewardsToken = _rewardsToken;
treasury = _treasury;
_mint(address(this), 1);
}
function calcSharesFromCost(uint256 _cost) public view override returns (uint256 _shares)
{
return _cost.mul(totalSupply()).div(totalReserve());
}
function calcCostFromShares(uint256 _shares) public view override returns (uint256 _cost)
{
return _shares.mul(totalReserve()).div(totalSupply());
}
function calcSharesFromTokenAmount(address _token, uint256 _amount) external view override returns (uint256 _shares)
{
uint256 _cost = UniswapV2LiquidityPoolAbstraction._estimateJoinPool(reserveToken, _token, _amount);
return calcSharesFromCost(_cost);
}
function calcTokenAmountFromShares(address _token, uint256 _shares) external view override returns (uint256 _amount)
{
uint256 _cost = calcCostFromShares(_shares);
return UniswapV2LiquidityPoolAbstraction._estimateExitPool(reserveToken, _token, _cost);
}
function totalReserve() public view override returns (uint256 _totalReserve)
{
return Transfers._getBalance(reserveToken);
}
function rewardInfo() external view override returns (uint256 _lockedReward, uint256 _unlockedReward, uint256 _rewardPerBlock)
{
(, _rewardPerBlock, _unlockedReward, _lockedReward) = _calcCurrentRewards();
return (_lockedReward, _unlockedReward, _rewardPerBlock);
}
function pendingFees() external view override returns (uint256 _feeShares)
{
return _calcFees();
}
function deposit(uint256 _cost) external override nonReentrant
{
address _from = msg.sender;
uint256 _shares = calcSharesFromCost(_cost);
Transfers._pullFunds(reserveToken, _from, _cost);
_mint(_from, _shares);
}
function withdraw(uint256 _shares) external override nonReentrant
{
address _from = msg.sender;
uint256 _cost = calcCostFromShares(_shares);
Transfers._pushFunds(reserveToken, _from, _cost);
_burn(_from, _shares);
}
function depositToken(address _token, uint256 _amount, uint256 _minShares) external override nonReentrant
{
address _from = msg.sender;
uint256 _minCost = calcCostFromShares(_minShares);
Transfers._pullFunds(_token, _from, _amount);
uint256 _cost = UniswapV2LiquidityPoolAbstraction._joinPool(reserveToken, _token, _amount, _minCost);
uint256 _shares = _cost.mul(totalSupply()).div(totalReserve().sub(_cost));
_mint(_from, _shares);
}
function withdrawToken(address _token, uint256 _shares, uint256 _minAmount) external override nonReentrant
{
address _from = msg.sender;
uint256 _cost = calcCostFromShares(_shares);
uint256 _amount = UniswapV2LiquidityPoolAbstraction._exitPool(reserveToken, _token, _cost, _minAmount);
Transfers._pushFunds(_token, _from, _amount);
_burn(_from, _shares);
}
function gulpRewards(uint256 _minCost) external override nonReentrant
{
_updateRewards();
UniswapV2LiquidityPoolAbstraction._joinPool(reserveToken, rewardsToken, lastUnlockedReward, _minCost);
lastUnlockedReward = 0;
}
function gulpFees() external override nonReentrant
{
uint256 _feeShares = _calcFees();
if (_feeShares > 0) {
lastTotalSupply = totalSupply();
lastTotalReserve = totalReserve();
_mint(treasury, _feeShares);
}
}
function setTreasury(address _newTreasury) external override onlyOwner nonReentrant
{
require(_newTreasury != address(0), "invalid address");
address _oldTreasury = treasury;
treasury = _newTreasury;
emit ChangeTreasury(_oldTreasury, _newTreasury);
}
function setPerformanceFee(uint256 _newPerformanceFee) external override onlyOwner nonReentrant
{
require(_newPerformanceFee <= MAXIMUM_PERFORMANCE_FEE, "invalid rate");
uint256 _oldPerformanceFee = performanceFee;
performanceFee = _newPerformanceFee;
emit ChangePerformanceFee(_oldPerformanceFee, _newPerformanceFee);
}
function setRewardRatePerWeek(uint256 _newRewardRatePerWeek) external override onlyOwner nonReentrant
{
require(_newRewardRatePerWeek <= 1e18, "invalid rate");
uint256 _oldRewardRatePerWeek = rewardRatePerWeek;
rewardRatePerWeek = _newRewardRatePerWeek;
emit ChangeRewardRatePerWeek(_oldRewardRatePerWeek, _newRewardRatePerWeek);
}
function _updateRewards() internal
{
(lastContractBlock, lastRewardPerBlock, lastUnlockedReward, lastLockedReward) = _calcCurrentRewards();
uint256 _balanceReward = Transfers._getBalance(rewardsToken);
uint256 _totalReward = lastLockedReward.add(lastUnlockedReward);
if (_balanceReward > _totalReward) {
uint256 _newLockedReward = _balanceReward.sub(_totalReward);
uint256 _newRewardPerBlock = _calcRewardPerBlock(_newLockedReward);
lastRewardPerBlock = lastRewardPerBlock.add(_newRewardPerBlock);
lastLockedReward = lastLockedReward.add(_newLockedReward);
}
else
if (_balanceReward < _totalReward) {
uint256 _removedLockedReward = _totalReward.sub(_balanceReward);
if (_removedLockedReward >= lastLockedReward) {
_removedLockedReward = lastLockedReward;
}
uint256 _removedRewardPerBlock = _calcRewardPerBlock(_removedLockedReward);
if (_removedLockedReward >= lastLockedReward) {
_removedRewardPerBlock = lastRewardPerBlock;
}
lastRewardPerBlock = lastRewardPerBlock.sub(_removedRewardPerBlock);
lastLockedReward = lastLockedReward.sub(_removedLockedReward);
lastUnlockedReward = _balanceReward.sub(lastLockedReward);
}
}
function _calcFees() internal view returns (uint256 _feeShares)
{
uint256 _oldTotalSupply = lastTotalSupply;
uint256 _oldTotalReserve = lastTotalReserve;
uint256 _newTotalSupply = totalSupply();
uint256 _newTotalReserve = totalReserve();
uint256 _positive = _oldTotalSupply.mul(_newTotalReserve);
uint256 _negative = _newTotalSupply.mul(_oldTotalReserve);
if (_positive > _negative) {
uint256 _profitCost = _positive.sub(_negative).div(_oldTotalSupply);
uint256 _feeCost = _profitCost.mul(performanceFee).div(1e18);
return calcSharesFromCost(_feeCost);
}
return 0;
}
function _calcCurrentRewards() internal view returns (uint256 _currentContractBlock, uint256 _currentRewardPerBlock, uint256 _currentUnlockedReward, uint256 _currentLockedReward)
{
uint256 _contractBlock = lastContractBlock;
uint256 _rewardPerBlock = lastRewardPerBlock;
uint256 _unlockedReward = lastUnlockedReward;
uint256 _lockedReward = lastLockedReward;
if (_contractBlock < block.number) {
uint256 _week = _contractBlock.div(BLOCKS_PER_WEEK);
uint256 _offset = _contractBlock.mod(BLOCKS_PER_WEEK);
_contractBlock = block.number;
uint256 _currentWeek = _contractBlock.div(BLOCKS_PER_WEEK);
uint256 _currentOffset = _contractBlock.mod(BLOCKS_PER_WEEK);
while (_week < _currentWeek) {
uint256 _blocks = BLOCKS_PER_WEEK.sub(_offset);
uint256 _reward = _blocks.mul(_rewardPerBlock);
_unlockedReward = _unlockedReward.add(_reward);
_lockedReward = _lockedReward.sub(_reward);
_rewardPerBlock = _calcRewardPerBlock(_lockedReward);
_week++;
_offset = 0;
}
uint256 _blocks = _currentOffset.sub(_offset);
uint256 _reward = _blocks.mul(_rewardPerBlock);
_unlockedReward = _unlockedReward.add(_reward);
_lockedReward = _lockedReward.sub(_reward);
}
return (_contractBlock, _rewardPerBlock, _unlockedReward, _lockedReward);
}
function _calcRewardPerBlock(uint256 _lockedReward) internal view returns (uint256 _rewardPerBlock)
{
return _lockedReward.mul(rewardRatePerWeek).div(1e18).div(BLOCKS_PER_WEEK);
}
}
pragma solidity ^0.6.0;
interface WETH is IERC20
{
function deposit() external payable;
function withdraw(uint256 _amount) external;
}
pragma solidity ^0.6.0;
library Wrapping
{
function _wrap(uint256 _amount) internal
{
WETH($.WETH).deposit{value: _amount}();
}
function _unwrap(uint256 _amount) internal
{
WETH($.WETH).withdraw(_amount);
}
}
pragma solidity ^0.6.0;
contract GEtherBridge
{
function deposit(address _stakeToken, uint256 _minShares) external payable
{
address _from = msg.sender;
uint256 _amount = msg.value;
address _token = $.WETH;
Wrapping._wrap(_amount);
Transfers._approveFunds(_token, _stakeToken, _amount);
GLPMining(_stakeToken).depositToken(_token, _amount, _minShares);
uint256 _shares = Transfers._getBalance(_stakeToken);
Transfers._pushFunds(_stakeToken, _from, _shares);
}
function withdraw(address _stakeToken, uint256 _shares, uint256 _minAmount) external
{
address payable _from = msg.sender;
address _token = $.WETH;
Transfers._pullFunds(_stakeToken, _from, _shares);
GLPMining(_stakeToken).withdrawToken(_token, _shares, _minAmount);
uint256 _amount = Transfers._getBalance(_token);
Wrapping._unwrap(_amount);
_from.transfer(_amount);
}
receive() external payable {}
}
pragma solidity ^0.6.0;
contract rAAVE is GElasticToken
{
constructor (uint256 _initialSupply)
GElasticToken("rebase AAVE", "rAAVE", 18, $.AAVE, _initialSupply) public
{
}
}
contract stkAAVE_rAAVE is GLPMiningToken
{
constructor (address _AAVE_rAAVE, address _rAAVE)
GLPMiningToken("staked AAVE/rAAVE", "stkAAVE/rAAVE", 18, _AAVE_rAAVE, _rAAVE) public
{
}
}
contract stkGRO_rAAVE is GLPMiningToken
{
constructor (address _GRO_rAAVE, address _rAAVE)
GLPMiningToken("staked GRO/rAAVE", "stkGRO/rAAVE", 18, _GRO_rAAVE, _rAAVE) public
{
}
}
contract stkETH_rAAVE is GLPMiningToken
{
constructor (address _ETH_rAAVE, address _rAAVE)
GLPMiningToken("staked ETH/rAAVE", "stkETH/rAAVE", 18, _ETH_rAAVE, _rAAVE) public
{
}
}
pragma solidity ^0.6.0;
contract GTokenRegistry is Ownable
{
function registerNewToken(address _growthToken, address _oldGrowthToken) public onlyOwner
{
emit NewToken(_growthToken, _oldGrowthToken);
}
event NewToken(address indexed _growthToken, address indexed _oldGrowthToken);
}