Contract Address Details

0x02f0826ef6aD107Cfc861152B32B52fD11BaB9ED

Token
Wrapped Songbird (WSGB)
Creator
0x493044–a7586e at 0x1cd5be–b5a631
Balance
6,723,030,396.867672401790992024 SGB
Tokens
Fetching tokens...
Transactions
2,284,114 Transactions
Transfers
42 Transfers
Gas Used
765,062,763,685
Last Balance Update
19116238
Contract name:
WNat




Optimization enabled
true
Compiler version
v0.7.6+commit.7338295f




Optimization runs
200
EVM Version
istanbul




Verified at
2021-09-17 08:22:07.461761Z

Constructor Arguments

000000000000000000000000493044fbbaa7f9f78379864fa88accaff6a7586e000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000105772617070656420536f6e67626972640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000045753474200000000000000000000000000000000000000000000000000000000

Arg [0] (address) : 0x493044fbbaa7f9f78379864fa88accaff6a7586e
Arg [1] (string) : Wrapped Songbird
Arg [2] (string) : WSGB

              

Contract source code

// Sources flattened with hardhat v2.3.0 https://hardhat.org
// File @openzeppelin/contracts/math/[email protected]
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.8.0;
/**
* @dev Standard math utilities missing in the Solidity language.
*/
library Math {
/**
* @dev Returns the largest of two numbers.
*/
function max(uint256 a, uint256 b) internal pure returns (uint256) {
return a >= b ? a : b;
}
/**
* @dev Returns the smallest of two numbers.
*/
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
/**
* @dev Returns the average of two numbers. The result is rounded towards
* zero.
*/
function average(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b) / 2 can overflow, so we distribute
return (a / 2) + (b / 2) + ((a % 2 + b % 2) / 2);
}
}
// File @openzeppelin/contracts/math/[email protected]
//
pragma solidity >=0.6.0 <0.8.0;
/**
* @dev Wrappers over Solidity's arithmetic operations with added overflow
* checks.
*
* Arithmetic operations in Solidity wrap on overflow. This can easily result
* in bugs, because programmers usually assume that an overflow raises an
* error, which is the standard behavior in high level programming languages.
* `SafeMath` restores this intuition by reverting the transaction when an
* operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*/
library SafeMath {
/**
* @dev Returns the addition of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
uint256 c = a + b;
if (c < a) return (false, 0);
return (true, c);
}
/**
* @dev Returns the substraction of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b > a) return (false, 0);
return (true, a - b);
}
/**
* @dev Returns the multiplication of two unsigned integers, with an overflow flag.
*
* _Available since v3.4._
*/
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) return (true, 0);
uint256 c = a * b;
if (c / a != b) return (false, 0);
return (true, c);
}
/**
* @dev Returns the division of two unsigned integers, with a division by zero flag.
*
* _Available since v3.4._
*/
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b == 0) return (false, 0);
return (true, a / b);
}
/**
* @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
*
* _Available since v3.4._
*/
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b == 0) return (false, 0);
return (true, a % b);
}
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
*
* - Addition cannot overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a, "SafeMath: subtraction overflow");
return a - b;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
*
* - Multiplication cannot overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) return 0;
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
/**
* @dev Returns the integer division of two unsigned integers, reverting on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0, "SafeMath: division by zero");
return a / b;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0, "SafeMath: modulo by zero");
return a % b;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {trySub}.
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
*
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
return a - b;
}
/**
* @dev Returns the integer division of two unsigned integers, reverting with custom message on
* division by zero. The result is rounded towards zero.
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {tryDiv}.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
return a / b;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* reverting with custom message when dividing by zero.
*
* CAUTION: This function is deprecated because it requires allocating memory for the error
* message unnecessarily. For custom revert reasons use {tryMod}.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
*
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
return a % b;
}
}
// File contracts/token/lib/CheckPointHistory.sol
//
pragma solidity 0.7.6;
/**
* @title Check Point History library
* @notice A contract to manage checkpoints as of a given block.
* @dev Store value history by block number with detachable state.
**/
library CheckPointHistory {
using SafeMath for uint256;
/**
* @dev `CheckPoint` is the structure that attaches a block number to a
* given value; the block number attached is the one that last changed the
* value
**/
struct CheckPoint {
// `fromBlock` is the block number that the value was generated from
uint256 fromBlock;
// `value` is the amount of tokens at a specific block number
uint256 value;
}
struct CheckPointHistoryState {
// `checkpoints` is an array that tracks values at non-contiguous block numbers
CheckPoint[] checkpoints;
// `checkpoints` before `startIndex` have been deleted
// INVARIANT: checkpoints.length == 0 || startIndex < checkpoints.length (strict!)
uint256 startIndex;
}
/**
* @notice Binary search of _checkpoints array.
* @param _checkpoints An array of CheckPoint to search.
* @param _startIndex Smallest possible index to be returned.
* @param _blockNumber The block number to search for.
*/
function _indexOfGreatestBlockLessThan(
CheckPoint[] storage _checkpoints,
uint256 _startIndex,
uint256 _blockNumber
)
private view
returns (uint256 index)
{
// Binary search of the value by given block number in the array
uint256 min = _startIndex;
uint256 max = _checkpoints.length.sub(1);
while (max > min) {
uint256 mid = (max.add(min).add(1)).div(2);
if (_checkpoints[mid].fromBlock <= _blockNumber) {
min = mid;
} else {
max = mid.sub(1);
}
}
return min;
}
/**
* @notice Queries the value at a specific `_blockNumber`
* @param _self A CheckPointHistoryState instance to manage.
* @param _blockNumber The block number of the value active at that time
* @return _value The value at `_blockNumber`
**/
function valueAt(
CheckPointHistoryState storage _self,
uint256 _blockNumber
)
internal view
returns (uint256 _value)
{
uint256 historyCount = _self.checkpoints.length;
// No _checkpoints, return 0
if (historyCount == 0) return 0;
// Shortcut for the actual value (extra optimized for current block, to save one storage read)
// historyCount - 1 is safe, since historyCount != 0
if (_blockNumber >= block.number || _blockNumber >= _self.checkpoints[historyCount - 1].fromBlock) {
return _self.checkpoints[historyCount - 1].value;
}
// guard values at start
uint256 startIndex = _self.startIndex;
if (_blockNumber < _self.checkpoints[startIndex].fromBlock) {
// reading data before `startIndex` is only safe before first cleanup
require(startIndex == 0, "CheckPointHistory: reading from cleaned-up block");
return 0;
}
// Find the block with number less than or equal to block given
uint256 index = _indexOfGreatestBlockLessThan(_self.checkpoints, startIndex, _blockNumber);
return _self.checkpoints[index].value;
}
/**
* @notice Queries the value at `block.number`
* @param _self A CheckPointHistoryState instance to manage.
* @return _value The value at `block.number`
**/
function valueAtNow(CheckPointHistoryState storage _self) internal view returns (uint256 _value) {
return valueAt(_self, block.number);
}
/**
* @notice Writes the value at the current block.
* @param _self A CheckPointHistoryState instance to manage.
* @param _value Value to write.
**/
function writeValue(
CheckPointHistoryState storage _self,
uint256 _value
)
internal
{
uint256 historyCount = _self.checkpoints.length;
if (historyCount == 0) {
// checkpoints array empty, push new CheckPoint
_self.checkpoints.push(CheckPoint({fromBlock: block.number, value: _value}));
} else {
// historyCount - 1 is safe, since historyCount != 0
CheckPoint storage lastCheckpoint = _self.checkpoints[historyCount - 1];
uint256 lastBlock = lastCheckpoint.fromBlock;
// slither-disable-next-line incorrect-equality
if (block.number == lastBlock) {
// If last check point is the current block, just update
lastCheckpoint.value = _value;
} else {
// we should never have future blocks in history
assert (block.number > lastBlock);
// push new CheckPoint
_self.checkpoints.push(CheckPoint({fromBlock: block.number, value: _value}));
}
}
}
/**
* Delete at most `_count` of the oldest checkpoints.
* At least one checkpoint at or before `_cleanupBlockNumber` will remain
* (unless the history was empty to start with).
*/
function cleanupOldCheckpoints(
CheckPointHistoryState storage _self,
uint256 _count,
uint256 _cleanupBlockNumber
)
internal
returns (uint256)
{
if (_cleanupBlockNumber == 0) return 0; // optimization for when cleaning is not enabled
uint256 length = _self.checkpoints.length;
if (length == 0) return 0;
uint256 startIndex = _self.startIndex;
// length - 1 is safe, since length != 0 (check above)
uint256 endIndex = Math.min(startIndex.add(_count), length - 1); // last element can never be deleted
uint256 index = startIndex;
// we can delete `checkpoint[index]` while the next checkpoint is at `_cleanupBlockNumber` or before
while (index < endIndex && _self.checkpoints[index + 1].fromBlock <= _cleanupBlockNumber) {
delete _self.checkpoints[index];
index++;
}
if (index > startIndex) { // index is the first not deleted index
_self.startIndex = index;
}
return index - startIndex; // safe: index >= startIndex at start and then increases
}
}
// File contracts/token/lib/CheckPointsByAddress.sol
//
pragma solidity 0.7.6;
/**
* @title Check Points By Address library
* @notice A contract to manage checkpoint history for a collection of addresses.
* @dev Store value history by address, and then by block number.
**/
library CheckPointsByAddress {
using SafeMath for uint256;
using CheckPointHistory for CheckPointHistory.CheckPointHistoryState;
struct CheckPointsByAddressState {
// `historyByAddress` is the map that stores the check point history of each address
mapping(address => CheckPointHistory.CheckPointHistoryState) historyByAddress;
}
/**
/**
* @notice Send `amount` value to `to` address from `from` address.
* @param _self A CheckPointsByAddressState instance to manage.
* @param _from Address of the history of from values
* @param _to Address of the history of to values
* @param _amount The amount of value to be transferred
**/
function transmit(
CheckPointsByAddressState storage _self,
address _from,
address _to,
uint256 _amount
)
internal
{
// Shortcut
if (_amount == 0) return;
// Both from and to can never be zero
assert(!(_from == address(0) && _to == address(0)));
// Update transferer value
if (_from != address(0)) {
// Compute the new from balance
uint256 newValueFrom = valueOfAtNow(_self, _from).sub(_amount);
writeValue(_self, _from, newValueFrom);
}
// Update transferee value
if (_to != address(0)) {
// Compute the new to balance
uint256 newValueTo = valueOfAtNow(_self, _to).add(_amount);
writeValue(_self, _to, newValueTo);
}
}
/**
* @notice Queries the value of `_owner` at a specific `_blockNumber`.
* @param _self A CheckPointsByAddressState instance to manage.
* @param _owner The address from which the value will be retrieved.
* @param _blockNumber The block number to query for the then current value.
* @return The value at `_blockNumber` for `_owner`.
**/
function valueOfAt(
CheckPointsByAddressState storage _self,
address _owner,
uint256 _blockNumber
)
internal view
returns (uint256)
{
// Get history for _owner
CheckPointHistory.CheckPointHistoryState storage history = _self.historyByAddress[_owner];
// Return value at given block
return history.valueAt(_blockNumber);
}
/**
* @notice Get the value of the `_owner` at the current `block.number`.
* @param _self A CheckPointsByAddressState instance to manage.
* @param _owner The address of the value is being requested.
* @return The value of `_owner` at the current block.
**/
function valueOfAtNow(CheckPointsByAddressState storage _self, address _owner) internal view returns (uint256) {
return valueOfAt(_self, _owner, block.number);
}
/**
* @notice Writes the `value` at the current block number for `_owner`.
* @param _self A CheckPointsByAddressState instance to manage.
* @param _owner The address of `_owner` to write.
* @param _value The value to write.
* @dev Sender must be the owner of the contract.
**/
function writeValue(
CheckPointsByAddressState storage _self,
address _owner,
uint256 _value
)
internal
{
// Get history for _owner
CheckPointHistory.CheckPointHistoryState storage history = _self.historyByAddress[_owner];
// Write the value
history.writeValue(_value);
}
/**
* Delete at most `_count` of the oldest checkpoints.
* At least one checkpoint at or before `_cleanupBlockNumber` will remain
* (unless the history was empty to start with).
*/
function cleanupOldCheckpoints(
CheckPointsByAddressState storage _self,
address _owner,
uint256 _count,
uint256 _cleanupBlockNumber
)
internal
returns (uint256)
{
if (_owner != address(0)) {
return _self.historyByAddress[_owner].cleanupOldCheckpoints(_count, _cleanupBlockNumber);
}
return 0;
}
}
// File contracts/token/lib/CheckPointHistoryCache.sol
//
pragma solidity 0.7.6;
library CheckPointHistoryCache {
using SafeMath for uint256;
using CheckPointHistory for CheckPointHistory.CheckPointHistoryState;
struct CacheState {
// mapping blockNumber => (value + 1)
mapping(uint256 => uint256) cache;
}
function valueAt(
CacheState storage _self,
CheckPointHistory.CheckPointHistoryState storage _checkPointHistory,
uint256 _blockNumber
)
internal returns (uint256 _value, bool _cacheCreated)
{
// is it in cache?
uint256 cachedValue = _self.cache[_blockNumber];
if (cachedValue != 0) {
return (cachedValue - 1, false); // safe, cachedValue != 0
}
// read from _checkPointHistory
uint256 historyValue = _checkPointHistory.valueAt(_blockNumber);
_self.cache[_blockNumber] = historyValue.add(1); // store to cache (add 1 to differentiate from empty)
return (historyValue, true);
}
function deleteAt(
CacheState storage _self,
uint256 _blockNumber
)
internal returns (uint256 _deleted)
{
if (_self.cache[_blockNumber] != 0) {
_self.cache[_blockNumber] = 0;
return 1;
}
return 0;
}
}
// File contracts/token/implementation/CheckPointable.sol
//
pragma solidity 0.7.6;
/**
* @title Check Pointable ERC20 Behavior
* @notice ERC20 behavior which adds balance check point features.
**/
abstract contract CheckPointable {
using CheckPointHistory for CheckPointHistory.CheckPointHistoryState;
using CheckPointsByAddress for CheckPointsByAddress.CheckPointsByAddressState;
using CheckPointHistoryCache for CheckPointHistoryCache.CacheState;
using SafeMath for uint256;
// The number of history cleanup steps executed for every write operation.
// It is more than 1 to make as certain as possible that all history gets cleaned eventually.
uint256 private constant CLEANUP_COUNT = 2;
// Private member variables
CheckPointsByAddress.CheckPointsByAddressState private balanceHistory;
CheckPointHistory.CheckPointHistoryState private totalSupply;
CheckPointHistoryCache.CacheState private totalSupplyCache;
// Historic data for the blocks before `cleanupBlockNumber` can be erased,
// history before that block should never be used since it can be inconsistent.
uint256 private cleanupBlockNumber;
// Address of the contract that is allowed to call methods for history cleaning.
address private cleanerContract;
/**
* Emitted when a total supply cache entry is created.
* Allows history cleaners to track total supply cache cleanup opportunities off-chain.
*/
event CreatedTotalSupplyCache(uint256 _blockNumber);
// Most cleanup opportunities can be deduced from standard event
// Transfer(from, to, amount):
// - balance history for `from` (if nonzero) and `to` (if nonzero)
// - total supply history when either `from` or `to` is zero
modifier notBeforeCleanupBlock(uint256 _blockNumber) {
require(_blockNumber >= cleanupBlockNumber, "CheckPointable: reading from cleaned-up block");
_;
}
modifier onlyCleaner {
require(msg.sender == cleanerContract, "Only cleaner contract");
_;
}
/**
* @dev Queries the token balance of `_owner` at a specific `_blockNumber`.
* @param _owner The address from which the balance will be retrieved.
* @param _blockNumber The block number when the balance is queried.
* @return _balance The balance at `_blockNumber`.
**/
function balanceOfAt(address _owner, uint256 _blockNumber)
public virtual view
notBeforeCleanupBlock(_blockNumber)
returns (uint256 _balance)
{
return balanceHistory.valueOfAt(_owner, _blockNumber);
}
/**
* @notice Burn current token `amount` for `owner` of checkpoints at current block.
* @param _owner The address of the owner to burn tokens.
* @param _amount The amount to burn.
*/
function _burnForAtNow(address _owner, uint256 _amount) internal virtual {
uint256 newBalance = balanceOfAt(_owner, block.number).sub(_amount, "Burn too big for owner");
balanceHistory.writeValue(_owner, newBalance);
balanceHistory.cleanupOldCheckpoints(_owner, CLEANUP_COUNT, cleanupBlockNumber);
totalSupply.writeValue(totalSupplyAt(block.number).sub(_amount, "Burn too big for total supply"));
totalSupply.cleanupOldCheckpoints(CLEANUP_COUNT, cleanupBlockNumber);
}
/**
* @notice Mint current token `amount` for `owner` of checkpoints at current block.
* @param _owner The address of the owner to burn tokens.
* @param _amount The amount to burn.
*/
function _mintForAtNow(address _owner, uint256 _amount) internal virtual {
uint256 newBalance = balanceOfAt(_owner, block.number).add(_amount);
balanceHistory.writeValue(_owner, newBalance);
balanceHistory.cleanupOldCheckpoints(_owner, CLEANUP_COUNT, cleanupBlockNumber);
totalSupply.writeValue(totalSupplyAt(block.number).add(_amount));
totalSupply.cleanupOldCheckpoints(CLEANUP_COUNT, cleanupBlockNumber);
}
/**
* @notice Total amount of tokens at a specific `_blockNumber`.
* @param _blockNumber The block number when the _totalSupply is queried
* @return _totalSupply The total amount of tokens at `_blockNumber`
**/
function totalSupplyAt(uint256 _blockNumber)
public virtual view
notBeforeCleanupBlock(_blockNumber)
returns(uint256 _totalSupply)
{
return totalSupply.valueAt(_blockNumber);
}
/**
* @notice Total amount of tokens at a specific `_blockNumber`.
* @param _blockNumber The block number when the _totalSupply is queried
* @return _totalSupply The total amount of tokens at `_blockNumber`
**/
function _totalSupplyAtCached(uint256 _blockNumber) internal
notBeforeCleanupBlock(_blockNumber)
returns(uint256 _totalSupply)
{
// use cache only for the past (the value will never change)
require(_blockNumber < block.number, "Can only be used for past blocks");
(uint256 value, bool cacheCreated) = totalSupplyCache.valueAt(totalSupply, _blockNumber);
if (cacheCreated) emit CreatedTotalSupplyCache(_blockNumber);
return value;
}
/**
* @notice Transmit token `_amount` `_from` address `_to` address of checkpoints at current block.
* @param _from The address of the sender.
* @param _to The address of the receiver.
* @param _amount The amount to transmit.
*/
function _transmitAtNow(address _from, address _to, uint256 _amount) internal virtual {
balanceHistory.transmit(_from, _to, _amount);
balanceHistory.cleanupOldCheckpoints(_from, CLEANUP_COUNT, cleanupBlockNumber);
balanceHistory.cleanupOldCheckpoints(_to, CLEANUP_COUNT, cleanupBlockNumber);
}
/**
* Set the cleanup block number.
*/
function _setCleanupBlockNumber(uint256 _blockNumber) internal {
require(_blockNumber >= cleanupBlockNumber, "Cleanup block number must never decrease");
require(_blockNumber < block.number, "Cleanup block must be in the past");
cleanupBlockNumber = _blockNumber;
}
/**
* Get the cleanup block number.
*/
function _cleanupBlockNumber() internal view returns (uint256) {
return cleanupBlockNumber;
}
/**
* @notice Update history at token transfer, the CheckPointable part of `_beforeTokenTransfer` hook.
* @param _from The address of the sender.
* @param _to The address of the receiver.
* @param _amount The amount to transmit.
*/
function _updateBalanceHistoryAtTransfer(address _from, address _to, uint256 _amount) internal virtual {
if (_from == address(0)) {
// mint checkpoint balance data for transferee
_mintForAtNow(_to, _amount);
} else if (_to == address(0)) {
// burn checkpoint data for transferer
_burnForAtNow(_from, _amount);
} else {
// transfer checkpoint balance data
_transmitAtNow(_from, _to, _amount);
}
}
// history cleanup methods
/**
* Set the contract that is allowed to call history cleaning methods.
*/
function _setCleanerContract(address _cleanerContract) internal {
cleanerContract = _cleanerContract;
}
/**
* Delete balance checkpoints that expired (i.e. are before `cleanupBlockNumber`).
* Method can only be called from the `cleanerContract` (which may be a proxy to external cleaners).
* @param _owner balance owner account address
* @param _count maximum number of checkpoints to delete
* @return the number of checkpoints deleted
*/
function balanceHistoryCleanup(address _owner, uint256 _count) external onlyCleaner returns (uint256) {
return balanceHistory.cleanupOldCheckpoints(_owner, _count, cleanupBlockNumber);
}
/**
* Delete total supply checkpoints that expired (i.e. are before `cleanupBlockNumber`).
* Method can only be called from the `cleanerContract` (which may be a proxy to external cleaners).
* @param _count maximum number of checkpoints to delete
* @return the number of checkpoints deleted
*/
function totalSupplyHistoryCleanup(uint256 _count) external onlyCleaner returns (uint256) {
return totalSupply.cleanupOldCheckpoints(_count, cleanupBlockNumber);
}
/**
* Delete total supply cache entry that expired (i.e. is before `cleanupBlockNumber`).
* Method can only be called from the `cleanerContract` (which may be a proxy to external cleaners).
* @param _blockNumber the block number for which total supply value was cached
* @return the number of cache entries deleted (always 0 or 1)
*/
function totalSupplyCacheCleanup(uint256 _blockNumber) external onlyCleaner returns (uint256) {
require(_blockNumber < cleanupBlockNumber, "No cleanup after cleanup block");
return totalSupplyCache.deleteAt(_blockNumber);
}
}
// File @openzeppelin/contracts/utils/[email protected]
//
pragma solidity >=0.6.0 <0.8.0;
/*
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with GSN meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address payable) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes memory) {
this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
return msg.data;
}
}
// File @openzeppelin/contracts/token/ERC20/[email protected]
//
pragma solidity >=0.6.0 <0.8.0;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `recipient`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address recipient, uint256 amount) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* @dev Moves `amount` tokens from `sender` to `recipient` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
}
// File @openzeppelin/contracts/token/ERC20/[email protected]
//
pragma solidity >=0.6.0 <0.8.0;
/**
* @dev Implementation of the {IERC20} interface.
*
* This implementation is agnostic to the way tokens are created. This means
* that a supply mechanism has to be added in a derived contract using {_mint}.
* For a generic mechanism see {ERC20PresetMinterPauser}.
*
* TIP: For a detailed writeup see our guide
* https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226[How
* to implement supply mechanisms].
*
* We have followed general OpenZeppelin guidelines: functions revert instead
* of returning `false` on failure. This behavior is nonetheless conventional
* and does not conflict with the expectations of ERC20 applications.
*
* Additionally, an {Approval} event is emitted on calls to {transferFrom}.
* This allows applications to reconstruct the allowance for all accounts just
* by listening to said events. Other implementations of the EIP may not emit
* these events, as it isn't required by the specification.
*
* Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
* functions have been added to mitigate the well-known issues around setting
* allowances. See {IERC20-approve}.
*/
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;
/**
* @dev Sets the values for {name} and {symbol}, initializes {decimals} with
* a default value of 18.
*
* To select a different value for {decimals}, use {_setupDecimals}.
*
* All three of these values are immutable: they can only be set once during
* construction.
*/
constructor (string memory name_, string memory symbol_) public {
_name = name_;
_symbol = symbol_;
_decimals = 18;
}
/**
* @dev Returns the name of the token.
*/
function name() public view virtual returns (string memory) {
return _name;
}
/**
* @dev Returns the symbol of the token, usually a shorter version of the
* name.
*/
function symbol() public view virtual returns (string memory) {
return _symbol;
}
/**
* @dev Returns the number of decimals used to get its user representation.
* For example, if `decimals` equals `2`, a balance of `505` tokens should
* be displayed to a user as `5,05` (`505 / 10 ** 2`).
*
* Tokens usually opt for a value of 18, imitating the relationship between
* Ether and Wei. This is the value {ERC20} uses, unless {_setupDecimals} is
* called.
*
* NOTE: This information is only used for _display_ purposes: it in
* no way affects any of the arithmetic of the contract, including
* {IERC20-balanceOf} and {IERC20-transfer}.
*/
function decimals() public view virtual returns (uint8) {
return _decimals;
}
/**
* @dev See {IERC20-totalSupply}.
*/
function totalSupply() public view virtual override returns (uint256) {
return _totalSupply;
}
/**
* @dev See {IERC20-balanceOf}.
*/
function balanceOf(address account) public view virtual override returns (uint256) {
return _balances[account];
}
/**
* @dev See {IERC20-transfer}.
*
* Requirements:
*
* - `recipient` cannot be the zero address.
* - the caller must have a balance of at least `amount`.
*/
function transfer(address recipient, uint256 amount) public virtual override returns (bool) {
_transfer(_msgSender(), recipient, amount);
return true;
}
/**
* @dev See {IERC20-allowance}.
*/
function allowance(address owner, address spender) public view virtual override returns (uint256) {
return _allowances[owner][spender];
}
/**
* @dev See {IERC20-approve}.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function approve(address spender, uint256 amount) public virtual override returns (bool) {
_approve(_msgSender(), spender, amount);
return true;
}
/**
* @dev See {IERC20-transferFrom}.
*
* Emits an {Approval} event indicating the updated allowance. This is not
* required by the EIP. See the note at the beginning of {ERC20}.
*
* Requirements:
*
* - `sender` and `recipient` cannot be the zero address.
* - `sender` must have a balance of at least `amount`.
* - the caller must have allowance for ``sender``'s tokens of at least
* `amount`.
*/
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;
}
/**
* @dev Atomically increases the allowance granted to `spender` by the caller.
*
* This is an alternative to {approve} that can be used as a mitigation for
* problems described in {IERC20-approve}.
*
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
_approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
return true;
}
/**
* @dev Atomically decreases the allowance granted to `spender` by the caller.
*
* This is an alternative to {approve} that can be used as a mitigation for
* problems described in {IERC20-approve}.
*
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
* - `spender` must have allowance for the caller of at least
* `subtractedValue`.
*/
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;
}
/**
* @dev Moves tokens `amount` from `sender` to `recipient`.
*
* This is internal function is equivalent to {transfer}, and can be used to
* e.g. implement automatic token fees, slashing mechanisms, etc.
*
* Emits a {Transfer} event.
*
* Requirements:
*
* - `sender` cannot be the zero address.
* - `recipient` cannot be the zero address.
* - `sender` must have a balance of at least `amount`.
*/
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);
}
/** @dev Creates `amount` tokens and assigns them to `account`, increasing
* the total supply.
*
* Emits a {Transfer} event with `from` set to the zero address.
*
* Requirements:
*
* - `to` cannot be the zero address.
*/
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);
}
/**
* @dev Destroys `amount` tokens from `account`, reducing the
* total supply.
*
* Emits a {Transfer} event with `to` set to the zero address.
*
* Requirements:
*
* - `account` cannot be the zero address.
* - `account` must have at least `amount` tokens.
*/
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);
}
/**
* @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
*
* This internal function is equivalent to `approve`, and can be used to
* e.g. set automatic allowances for certain subsystems, etc.
*
* Emits an {Approval} event.
*
* Requirements:
*
* - `owner` cannot be the zero address.
* - `spender` cannot be the zero address.
*/
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);
}
/**
* @dev Sets {decimals} to a value other than the default one of 18.
*
* WARNING: This function should only be called from the constructor. Most
* applications that interact with token contracts will not expect
* {decimals} to ever change, and may work incorrectly if it does.
*/
function _setupDecimals(uint8 decimals_) internal virtual {
_decimals = decimals_;
}
/**
* @dev Hook that is called before any transfer of tokens. This includes
* minting and burning.
*
* Calling conditions:
*
* - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
* will be to transferred to `to`.
* - when `from` is zero, `amount` tokens will be minted for `to`.
* - when `to` is zero, `amount` of ``from``'s tokens will be burned.
* - `from` and `to` are never both zero.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/
function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual { }
}
// File contracts/utils/implementation/SafePct.sol
//
pragma solidity 0.7.6;
/**
* @dev Compute percentages safely without phantom overflows.
*
* Intermediate operations can overflow even when the result will always
* fit into computed type. Developers usually
* assume that overflows raise errors. `SafePct` restores this intuition by
* reverting the transaction when such an operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*
* Can be combined with {SafeMath} and {SignedSafeMath} to extend it to smaller types, by performing
* all math on `uint256` and `int256` and then downcasting.
*/
library SafePct {
using SafeMath for uint256;
/**
* Requirements:
*
* - intermediate operations must revert on overflow
*/
function mulDiv(uint256 x, uint256 y, uint256 z) internal pure returns (uint256) {
require(z > 0, "Division by zero");
if (x == 0) return 0;
uint256 xy = x * y;
if (xy / x == y) { // no overflow happened - same as in SafeMath mul
return xy / z;
}
//slither-disable-next-line divide-before-multiply
uint256 a = x / z;
uint256 b = x % z; // x = a * z + b
//slither-disable-next-line divide-before-multiply
uint256 c = y / z;
uint256 d = y % z; // y = c * z + d
return (a.mul(c).mul(z)).add(a.mul(d)).add(b.mul(c)).add(b.mul(d).div(z));
}
}
// File contracts/userInterfaces/IGovernanceVotePower.sol
//
pragma solidity 0.7.6;
interface IGovernanceVotePower {
/**
* @notice Delegate all governance vote power of `msg.sender` to `_to`.
* @param _to The address of the recipient
**/
function delegate(address _to) external;
/**
* @notice Undelegate all governance vote power that `msg.sender` has delegated.
**/
function undelegate() external;
/**
* @notice Get the governance vote power of `_who` at block `_blockNumber`
* @param _who The address to get voting power.
* @param _blockNumber The block number at which to fetch.
* @return Governance vote power of `_who` at `_blockNumber`.
*/
function votePowerOfAt(address _who, uint256 _blockNumber) external view returns(uint256);
}
// File contracts/userInterfaces/IVPContractEvents.sol
//
pragma solidity 0.7.6;
interface IVPContractEvents {
/**
* Event triggered when an account delegates or undelegates another account.
* Definition: `votePowerFromTo(from, to)` is `changed` from `priorVotePower` to `newVotePower`.
* For undelegation, `newVotePower` is 0.
*
* Note: the event is always emitted from VPToken's `writeVotePowerContract`.
*/
event Delegate(address indexed from, address indexed to, uint256 priorVotePower, uint256 newVotePower);
/**
* Event triggered only when account `delegator` revokes delegation to `delegatee`
* for a single block in the past (typically the current vote block).
*
* Note: the event is always emitted from VPToken's `writeVotePowerContract` and/or `readVotePowerContract`.
*/
event Revoke(address indexed delegator, address indexed delegatee, uint256 votePower, uint256 blockNumber);
}
// File contracts/userInterfaces/IVPToken.sol
//
pragma solidity 0.7.6;
interface IVPToken is IERC20 {
/**
* @notice Delegate by percentage `_bips` of voting power to `_to` from `msg.sender`.
* @param _to The address of the recipient
* @param _bips The percentage of voting power to be delegated expressed in basis points (1/100 of one percent).
* Not cummulative - every call resets the delegation value (and value of 0 undelegates `to`).
**/
function delegate(address _to, uint256 _bips) external;
/**
* @notice Explicitly delegate `_amount` of voting power to `_to` from `msg.sender`.
* @param _to The address of the recipient
* @param _amount An explicit vote power amount to be delegated.
* Not cummulative - every call resets the delegation value (and value of 0 undelegates `to`).
**/
function delegateExplicit(address _to, uint _amount) external;
/**
* @notice Revoke all delegation from sender to `_who` at given block.
* Only affects the reads via `votePowerOfAtCached()` in the block `_blockNumber`.
* Block `_blockNumber` must be in the past.
* This method should be used only to prevent rogue delegate voting in the current voting block.
* To stop delegating use delegate/delegateExplicit with value of 0 or undelegateAll/undelegateAllExplicit.
* @param _who Address of the delegatee
* @param _blockNumber The block number at which to revoke delegation.
*/
function revokeDelegationAt(address _who, uint _blockNumber) external;
/**
* @notice Undelegate all voting power for delegates of `msg.sender`
* Can only be used with percentage delegation.
* Does not reset delegation mode back to NOTSET.
**/
function undelegateAll() external;
/**
* @notice Undelegate all explicit vote power by amount delegates for `msg.sender`.
* Can only be used with explicit delegation.
* Does not reset delegation mode back to NOTSET.
* @param _delegateAddresses Explicit delegation does not store delegatees' addresses,
* so the caller must supply them.
* @return The amount still delegated (in case the list of delegates was incomplete).
*/
function undelegateAllExplicit(address[] memory _delegateAddresses) external returns (uint256);
/**
* @dev Should be compatible with ERC20 method
*/
function name() external view returns (string memory);
/**
* @dev Should be compatible with ERC20 method
*/
function symbol() external view returns (string memory);
/**
* @dev Should be compatible with ERC20 method
*/
function decimals() external view returns (uint8);
/**
* @notice Total amount of tokens at a specific `_blockNumber`.
* @param _blockNumber The block number when the totalSupply is queried
* @return The total amount of tokens at `_blockNumber`
**/
function totalSupplyAt(uint _blockNumber) external view returns(uint256);
/**
* @dev Queries the token balance of `_owner` at a specific `_blockNumber`.
* @param _owner The address from which the balance will be retrieved.
* @param _blockNumber The block number when the balance is queried.
* @return The balance at `_blockNumber`.
**/
function balanceOfAt(address _owner, uint _blockNumber) external view returns (uint256);
/**
* @notice Get the current total vote power.
* @return The current total vote power (sum of all accounts' vote powers).
*/
function totalVotePower() external view returns(uint256);
/**
* @notice Get the total vote power at block `_blockNumber`
* @param _blockNumber The block number at which to fetch.
* @return The total vote power at the block (sum of all accounts' vote powers).
*/
function totalVotePowerAt(uint _blockNumber) external view returns(uint256);
/**
* @notice Get the current vote power of `_owner`.
* @param _owner The address to get voting power.
* @return Current vote power of `_owner`.
*/
function votePowerOf(address _owner) external view returns(uint256);
/**
* @notice Get the vote power of `_owner` at block `_blockNumber`
* @param _owner The address to get voting power.
* @param _blockNumber The block number at which to fetch.
* @return Vote power of `_owner` at `_blockNumber`.
*/
function votePowerOfAt(address _owner, uint256 _blockNumber) external view returns(uint256);
/**
* @notice Get the delegation mode for '_who'. This mode determines whether vote power is
* allocated by percentage or by explicit value. Once the delegation mode is set,
* it never changes, even if all delegations are removed.
* @param _who The address to get delegation mode.
* @return delegation mode: 0 = NOTSET, 1 = PERCENTAGE, 2 = AMOUNT (i.e. explicit)
*/
function delegationModeOf(address _who) external view returns(uint256);
/**
* @notice Get current delegated vote power `_from` delegator delegated `_to` delegatee.
* @param _from Address of delegator
* @param _to Address of delegatee
* @return The delegated vote power.
*/
function votePowerFromTo(address _from, address _to) external view returns(uint256);
/**
* @notice Get delegated the vote power `_from` delegator delegated `_to` delegatee at `_blockNumber`.
* @param _from Address of delegator
* @param _to Address of delegatee
* @param _blockNumber The block number at which to fetch.
* @return The delegated vote power.
*/
function votePowerFromToAt(address _from, address _to, uint _blockNumber) external view returns(uint256);
/**
* @notice Compute the current undelegated vote power of `_owner`
* @param _owner The address to get undelegated voting power.
* @return The unallocated vote power of `_owner`
*/
function undelegatedVotePowerOf(address _owner) external view returns(uint256);
/**
* @notice Get the undelegated vote power of `_owner` at given block.
* @param _owner The address to get undelegated voting power.
* @param _blockNumber The block number at which to fetch.
* @return The undelegated vote power of `_owner` (= owner's own balance minus all delegations from owner)
*/
function undelegatedVotePowerOfAt(address _owner, uint256 _blockNumber) external view returns(uint256);
/**
* @notice Get the vote power delegation `delegationAddresses`
* and `_bips` of `_who`. Returned in two separate positional arrays.
* @param _who The address to get delegations.
* @return _delegateAddresses Positional array of delegation addresses.
* @return _bips Positional array of delegation percents specified in basis points (1/100 or 1 percent)
* @return _count The number of delegates.
* @return _delegationMode The mode of the delegation (NOTSET=0, PERCENTAGE=1, AMOUNT=2).
*/
function delegatesOf(address _who)
external view
returns (
address[] memory _delegateAddresses,
uint256[] memory _bips,
uint256 _count,
uint256 _delegationMode
);
/**
* @notice Get the vote power delegation `delegationAddresses`
* and `pcts` of `_who`. Returned in two separate positional arrays.
* @param _who The address to get delegations.
* @param _blockNumber The block for which we want to know the delegations.
* @return _delegateAddresses Positional array of delegation addresses.
* @return _bips Positional array of delegation percents specified in basis points (1/100 or 1 percent)
* @return _count The number of delegates.
* @return _delegationMode The mode of the delegation (NOTSET=0, PERCENTAGE=1, AMOUNT=2).
*/
function delegatesOfAt(address _who, uint256 _blockNumber)
external view
returns (
address[] memory _delegateAddresses,
uint256[] memory _bips,
uint256 _count,
uint256 _delegationMode
);
/**
* Returns VPContract used for readonly operations (view methods).
* The only non-view method that might be called on it is `revokeDelegationAt`.
*
* @notice `readVotePowerContract` is almost always equal to `writeVotePowerContract`
* except during upgrade from one VPContract to a new version (which should happen
* rarely or never and will be anounced before).
*
* @notice You shouldn't call any methods on VPContract directly, all are exposed
* via VPToken (and state changing methods are forbidden from direct calls).
* This is the reason why this method returns `IVPContractEvents` - it should only be used
* for listening to events (`Revoke` only).
*/
function readVotePowerContract() external view returns (IVPContractEvents);
/**
* Returns VPContract used for state changing operations (non-view methods).
* The only non-view method that might be called on it is `revokeDelegationAt`.
*
* @notice `writeVotePowerContract` is almost always equal to `readVotePowerContract`
* except during upgrade from one VPContract to a new version (which should happen
* rarely or never and will be anounced before). In the case of upgrade,
* `writeVotePowerContract` will be replaced first to establish delegations, and
* after some perio (e.g. after a reward epoch ends) `readVotePowerContract` will be set equal to it.
*
* @notice You shouldn't call any methods on VPContract directly, all are exposed
* via VPToken (and state changing methods are forbidden from direct calls).
* This is the reason why this method returns `IVPContractEvents` - it should only be used
* for listening to events (`Delegate` and `Revoke` only).
*/
function writeVotePowerContract() external view returns (IVPContractEvents);
/**
* When set, allows token owners to participate in governance voting
* and delegate governance vote power.
*/
function governanceVotePower() external view returns (IGovernanceVotePower);
}
// File contracts/token/interface/IICleanable.sol
//
pragma solidity 0.7.6;
interface IICleanable {
/**
* Set the contract that is allowed to call history cleaning methods.
*/
function setCleanerContract(address _cleanerContract) external;
/**
* Set the cleanup block number.
* Historic data for the blocks before `cleanupBlockNumber` can be erased,
* history before that block should never be used since it can be inconsistent.
* In particular, cleanup block number must be before current vote power block.
* @param _blockNumber The new cleanup block number.
*/
function setCleanupBlockNumber(uint256 _blockNumber) external;
/**
* Set the contract that is allowed to set cleanupBlockNumber.
* Usually this will be an instance of CleanupBlockNumberManager.
*/
function setCleanupBlockNumberManager(address _cleanupBlockNumberManager) external;
/**
* Get the current cleanup block number.
*/
function cleanupBlockNumber() external view returns (uint256);
}
// File contracts/token/interface/IIVPContract.sol
//
pragma solidity 0.7.6;
interface IIVPContract is IICleanable, IVPContractEvents {
/**
* Update vote powers when tokens are transfered.
* Also update delegated vote powers for percentage delegation
* and check for enough funds for explicit delegations.
**/
function updateAtTokenTransfer(
address _from,
address _to,
uint256 _fromBalance,
uint256 _toBalance,
uint256 _amount
) external;
/**
* @notice Delegate `_bips` percentage of voting power to `_to` from `_from`
* @param _from The address of the delegator
* @param _to The address of the recipient
* @param _balance The delegator's current balance
* @param _bips The percentage of voting power to be delegated expressed in basis points (1/100 of one percent).
* Not cummulative - every call resets the delegation value (and value of 0 revokes delegation).
**/
function delegate(
address _from,
address _to,
uint256 _balance,
uint256 _bips
) external;
/**
* @notice Explicitly delegate `_amount` of voting power to `_to` from `msg.sender`.
* @param _from The address of the delegator
* @param _to The address of the recipient
* @param _balance The delegator's current balance
* @param _amount An explicit vote power amount to be delegated.
* Not cummulative - every call resets the delegation value (and value of 0 undelegates `to`).
**/
function delegateExplicit(
address _from,
address _to,
uint256 _balance,
uint _amount
) external;
/**
* @notice Revoke all delegation from sender to `_who` at given block.
* Only affects the reads via `votePowerOfAtCached()` in the block `_blockNumber`.
* Block `_blockNumber` must be in the past.
* This method should be used only to prevent rogue delegate voting in the current voting block.
* To stop delegating use delegate/delegateExplicit with value of 0 or undelegateAll/undelegateAllExplicit.
* @param _from The address of the delegator
* @param _who Address of the delegatee
* @param _balance The delegator's current balance
* @param _blockNumber The block number at which to revoke delegation.
**/
function revokeDelegationAt(
address _from,
address _who,
uint256 _balance,
uint _blockNumber
) external;
/**
* @notice Undelegate all voting power for delegates of `msg.sender`
* Can only be used with percentage delegation.
* Does not reset delegation mode back to NOTSET.
* @param _from The address of the delegator
**/
function undelegateAll(
address _from,
uint256 _balance
) external;
/**
* @notice Undelegate all explicit vote power by amount delegates for `msg.sender`.
* Can only be used with explicit delegation.
* Does not reset delegation mode back to NOTSET.
* @param _from The address of the delegator
* @param _delegateAddresses Explicit delegation does not store delegatees' addresses,
* so the caller must supply them.
* @return The amount still delegated (in case the list of delegates was incomplete).
*/
function undelegateAllExplicit(
address _from,
address[] memory _delegateAddresses
) external returns (uint256);
/**
* @notice Get the vote power of `_who` at block `_blockNumber`
* Reads/updates cache and upholds revocations.
* @param _who The address to get voting power.
* @param _blockNumber The block number at which to fetch.
* @return Vote power of `_who` at `_blockNumber`.
*/
function votePowerOfAtCached(address _who, uint256 _blockNumber) external returns(uint256);
/**
* @notice Get the current vote power of `_who`.
* @param _who The address to get voting power.
* @return Current vote power of `_who`.
*/
function votePowerOf(address _who) external view returns(uint256);
/**
* @notice Get the vote power of `_who` at block `_blockNumber`
* @param _who The address to get voting power.
* @param _blockNumber The block number at which to fetch.
* @return Vote power of `_who` at `_blockNumber`.
*/
function votePowerOfAt(address _who, uint256 _blockNumber) external view returns(uint256);
/**
* Return vote powers for several addresses in a batch.
* @param _owners The list of addresses to fetch vote power of.
* @param _blockNumber The block number at which to fetch.
* @return A list of vote powers.
*/
function batchVotePowerOfAt(
address[] memory _owners,
uint256 _blockNumber
)
external view returns(uint256[] memory);
/**
* @notice Get current delegated vote power `_from` delegator delegated `_to` delegatee.
* @param _from Address of delegator
* @param _to Address of delegatee
* @param _balance The delegator's current balance
* @return The delegated vote power.
*/
function votePowerFromTo(
address _from,
address _to,
uint256 _balance
) external view returns(uint256);
/**
* @notice Get delegated the vote power `_from` delegator delegated `_to` delegatee at `_blockNumber`.
* @param _from Address of delegator
* @param _to Address of delegatee
* @param _balance The delegator's current balance
* @param _blockNumber The block number at which to fetch.
* @return The delegated vote power.
*/
function votePowerFromToAt(
address _from,
address _to,
uint256 _balance,
uint _blockNumber
) external view returns(uint256);
/**
* @notice Compute the current undelegated vote power of `_owner`
* @param _owner The address to get undelegated voting power.
* @param _balance Owner's current balance
* @return The unallocated vote power of `_owner`
*/
function undelegatedVotePowerOf(
address _owner,
uint256 _balance
) external view returns(uint256);
/**
* @notice Get the undelegated vote power of `_owner` at given block.
* @param _owner The address to get undelegated voting power.
* @param _blockNumber The block number at which to fetch.
* @return The undelegated vote power of `_owner` (= owner's own balance minus all delegations from owner)
*/
function undelegatedVotePowerOfAt(
address _owner,
uint256 _balance,
uint256 _blockNumber
) external view returns(uint256);
/**
* @notice Get the delegation mode for '_who'. This mode determines whether vote power is
* allocated by percentage or by explicit value.
* @param _who The address to get delegation mode.
* @return Delegation mode (NOTSET=0, PERCENTAGE=1, AMOUNT=2))
*/
function delegationModeOf(address _who) external view returns (uint256);
/**
* @notice Get the vote power delegation `_delegateAddresses`
* and `pcts` of an `_owner`. Returned in two separate positional arrays.
* @param _owner The address to get delegations.
* @return _delegateAddresses Positional array of delegation addresses.
* @return _bips Positional array of delegation percents specified in basis points (1/100 or 1 percent)
* @return _count The number of delegates.
* @return _delegationMode The mode of the delegation (NOTSET=0, PERCENTAGE=1, AMOUNT=2).
*/
function delegatesOf(
address _owner
)
external view
returns (
address[] memory _delegateAddresses,
uint256[] memory _bips,
uint256 _count,
uint256 _delegationMode
);
/**
* @notice Get the vote power delegation `delegationAddresses`
* and `pcts` of an `_owner`. Returned in two separate positional arrays.
* @param _owner The address to get delegations.
* @param _blockNumber The block for which we want to know the delegations.
* @return _delegateAddresses Positional array of delegation addresses.
* @return _bips Positional array of delegation percents specified in basis points (1/100 or 1 percent)
* @return _count The number of delegates.
* @return _delegationMode The mode of the delegation (NOTSET=0, PERCENTAGE=1, AMOUNT=2).
*/
function delegatesOfAt(
address _owner,
uint256 _blockNumber
)
external view
returns (
address[] memory _delegateAddresses,
uint256[] memory _bips,
uint256 _count,
uint256 _delegationMode
);
/**
* The VPToken (or some other contract) that owns this VPContract.
* All state changing methods may be called only from this address.
* This is because original msg.sender is sent in `_from` parameter
* and we must be sure that it cannot be faked by directly calling VPContract.
* Owner token is also used in case of replacement to recover vote powers from balances.
*/
function ownerToken() external view returns (IVPToken);
/**
* Return true if this IIVPContract is configured to be used as a replacement for other contract.
* It means that vote powers are not necessarily correct at the initialization, therefore
* every method that reads vote power must check whether it is initialized for that address and block.
*/
function isReplacement() external view returns (bool);
}
// File contracts/token/interface/IIGovernanceVotePower.sol
//
pragma solidity 0.7.6;
interface IIGovernanceVotePower is IGovernanceVotePower {
/**
* Update vote powers when tokens are transfered.
**/
function updateAtTokenTransfer(
address _from,
address _to,
uint256 _fromBalance,
uint256 _toBalance,
uint256 _amount
) external;
/**
* Set the cleanup block number.
* Historic data for the blocks before `cleanupBlockNumber` can be erased,
* history before that block should never be used since it can be inconsistent.
* In particular, cleanup block number must be before current vote power block.
* @param _blockNumber The new cleanup block number.
*/
function setCleanupBlockNumber(uint256 _blockNumber) external;
/**
* Set the contract that is allowed to call history cleaning methods.
*/
function setCleanerContract(address _cleanerContract) external;
/**
* @notice Get the token that this governance vote power contract belongs to.
*/
function ownerToken() external view returns(IVPToken);
}
// File contracts/token/interface/IIVPToken.sol
//
pragma solidity 0.7.6;
interface IIVPToken is IVPToken, IICleanable {
/**
* Sets new governance vote power contract that allows token owners to participate in governance voting
* and delegate governance vote power.
*/
function setGovernanceVotePower(IIGovernanceVotePower _governanceVotePower) external;
/**
* @notice Get the total vote power at block `_blockNumber` using cache.
* It tries to read the cached value and if not found, reads the actual value and stores it in cache.
* Can only be used if `_blockNumber` is in the past, otherwise reverts.
* @param _blockNumber The block number at which to fetch.
* @return The total vote power at the block (sum of all accounts' vote powers).
*/
function totalVotePowerAtCached(uint256 _blockNumber) external returns(uint256);
/**
* @notice Get the vote power of `_owner` at block `_blockNumber` using cache.
* It tries to read the cached value and if not found, reads the actual value and stores it in cache.
* Can only be used if _blockNumber is in the past, otherwise reverts.
* @param _owner The address to get voting power.
* @param _blockNumber The block number at which to fetch.
* @return Vote power of `_owner` at `_blockNumber`.
*/
function votePowerOfAtCached(address _owner, uint256 _blockNumber) external returns(uint256);
/**
* Return vote powers for several addresses in a batch.
* @param _owners The list of addresses to fetch vote power of.
* @param _blockNumber The block number at which to fetch.
* @return A list of vote powers.
*/
function batchVotePowerOfAt(
address[] memory _owners,
uint256 _blockNumber
) external view returns(uint256[] memory);
}
// File contracts/governance/implementation/GovernedBase.sol
//
pragma solidity 0.7.6;
/**
* @title Governed Base
* @notice This abstract base class defines behaviors for a governed contract.
* @dev This class is abstract so that specific behaviors can be defined for the constructor.
* Contracts should not be left ungoverned, but not all contract will have a constructor
* (for example those pre-defined in genesis).
**/
abstract contract GovernedBase {
address public governance;
address public proposedGovernance;
bool private initialised;
event GovernanceProposed(address proposedGovernance);
event GovernanceUpdated (address oldGovernance, address newGoveranance);
modifier onlyGovernance () {
require (msg.sender == governance, "only governance");
_;
}
constructor(address _governance) {
if (_governance != address(0)) {
initialise(_governance);
}
}
/**
* @notice First of a two step process for turning over governance to another address.
* @param _governance The address to propose to receive governance role.
* @dev Must hold governance to propose another address.
*/
function proposeGovernance(address _governance) external onlyGovernance {
proposedGovernance = _governance;
emit GovernanceProposed(_governance);
}
/**
* @notice Once proposed, claimant can claim the governance role as the second of a two-step process.
*/
function claimGovernance() external {
require(msg.sender == proposedGovernance, "not claimaint");
emit GovernanceUpdated(governance, proposedGovernance);
governance = proposedGovernance;
proposedGovernance = address(0);
}
/**
* @notice In a one-step process, turn over governance to another address.
* @dev Must hold governance to transfer.
*/
function transferGovernance(address _governance) external onlyGovernance {
emit GovernanceUpdated(governance, _governance);
governance = _governance;
proposedGovernance = address(0);
}
/**
* @notice Initialize the governance address if not first initialized.
*/
function initialise(address _governance) public virtual {
require(initialised == false, "initialised != false");
initialised = true;
emit GovernanceUpdated(governance, _governance);
governance = _governance;
proposedGovernance = address(0);
}
}
// File contracts/governance/implementation/Governed.sol
//
pragma solidity 0.7.6;
/**
* @title Governed
* @dev For deployed, governed contracts, enforce a non-zero address at create time.
**/
contract Governed is GovernedBase {
constructor(address _governance) GovernedBase(_governance) {
require(_governance != address(0), "_governance zero");
}
}
// File contracts/token/implementation/VPToken.sol
//
pragma solidity 0.7.6;
/**
* @title Vote Power Token
* @dev An ERC20 token to enable the holder to delegate voting power
* equal 1-1 to their balance, with history tracking by block.
**/
contract VPToken is IIVPToken, ERC20, CheckPointable, Governed {
using SafeMath for uint256;
using SafePct for uint256;
// the VPContract to use for reading vote powers and delegations
IIVPContract private readVpContract;
// the VPContract to use for writing vote powers and delegations
// normally same as `readVpContract` except during switch
// when reading happens from the old and writing goes to the new VPContract
IIVPContract private writeVpContract;
// the contract to use for governance vote power and delegation
// here only to properly update governance vp during transfers -
// all actual operations go directly to governance vp contract
IIGovernanceVotePower private governanceVP;
// the contract that is allowed to set cleanupBlockNumber
// usually this will be an instance of CleanupBlockNumberManager
address private cleanupBlockNumberManager;
/**
* When true, the argument to `setWriteVpContract` must be a vpContract
* with `isReplacement` set to `true`. To be used for creating the correct VPContract.
*/
bool public needsReplacementVPContract = false;
/**
* Event used to track history of VPToken -> VPContract / GovernanceVotePower
* associations (e.g. by external cleaners).
* @param _contractType 0 = read VPContract, 1 = write VPContract, 2 = governance vote power
* @param _oldContractAddress vote power contract address before change
* @param _newContractAddress vote power contract address after change
*/
event VotePowerContractChanged(uint256 _contractType, address _oldContractAddress, address _newContractAddress);
constructor(
address _governance,
//slither-disable-next-line shadowing-local
string memory _name,
//slither-disable-next-line shadowing-local
string memory _symbol
)
Governed(_governance) ERC20(_name, _symbol)
{
/* empty block */
}
/**
* @dev Should be compatible with ERC20 method
*/
function name() public view override(ERC20, IVPToken) returns (string memory) {
return ERC20.name();
}
/**
* @dev Should be compatible with ERC20 method
*/
function symbol() public view override(ERC20, IVPToken) returns (string memory) {
return ERC20.symbol();
}
/**
* @dev Should be compatible with ERC20 method
*/
function decimals() public view override(ERC20, IVPToken) returns (uint8) {
return ERC20.decimals();
}
/**
* @notice Total amount of tokens at a specific `_blockNumber`.
* @param _blockNumber The block number when the totalSupply is queried
* @return The total amount of tokens at `_blockNumber`
**/
function totalSupplyAt(uint256 _blockNumber) public view override(CheckPointable, IVPToken) returns(uint256) {
return CheckPointable.totalSupplyAt(_blockNumber);
}
/**
* @dev Queries the token balance of `_owner` at a specific `_blockNumber`.
* @param _owner The address from which the balance will be retrieved.
* @param _blockNumber The block number when the balance is queried.
* @return The balance at `_blockNumber`.
**/
function balanceOfAt(
address _owner,
uint256 _blockNumber
)
public view
override(CheckPointable, IVPToken)
returns (uint256)
{
return CheckPointable.balanceOfAt(_owner, _blockNumber);
}
/**
* @notice Delegate `_bips` of voting power to `_to` from `msg.sender`
* @param _to The address of the recipient
* @param _bips The percentage of voting power to be delegated expressed in basis points (1/100 of one percent).
* Not cummulative - every call resets the delegation value (and value of 0 revokes delegation).
**/
function delegate(address _to, uint256 _bips) external override {
// Get the current balance of sender and delegate by percentage _to recipient
_checkWriteVpContract().delegate(msg.sender, _to, balanceOf(msg.sender), _bips);
}
/**
* @notice Delegate `_amount` of voting power to `_to` from `msg.sender`
* @param _to The address of the recipient
* @param _amount An explicit vote power amount to be delegated.
* Not cummulative - every call resets the delegation value (and value of 0 revokes delegation).
**/
function delegateExplicit(address _to, uint256 _amount) external override {
_checkWriteVpContract().delegateExplicit(msg.sender, _to, balanceOf(msg.sender), _amount);
}
/**
* @notice Compute the current undelegated vote power of `_owner`
* @param _owner The address to get undelegated voting power.
* @return The unallocated vote power of `_owner`
*/
function undelegatedVotePowerOf(address _owner) external view override returns(uint256) {
return _checkReadVpContract().undelegatedVotePowerOf(_owner, balanceOf(_owner));
}
/**
* @notice Get the undelegated vote power of `_owner` at given block.
* @param _owner The address to get undelegated voting power.
* @param _blockNumber The block number at which to fetch.
* @return The unallocated vote power of `_owner`
*/
function undelegatedVotePowerOfAt(address _owner, uint256 _blockNumber) external view override returns (uint256) {
return _checkReadVpContract()
.undelegatedVotePowerOfAt(_owner, balanceOfAt(_owner, _blockNumber), _blockNumber);
}
/**
* @notice Undelegate all voting power for delegates of `msg.sender`
**/
function undelegateAll() external override {
_checkWriteVpContract().undelegateAll(msg.sender, balanceOf(msg.sender));
}
/**
* @notice Undelegate all explicit vote power by amount delegates for `msg.sender`.
* @param _delegateAddresses Explicit delegation does not store delegatees' addresses,
* so the caller must supply them.
* @return _remainingDelegation The amount still delegated (in case the list of delegates was incomplete).
*/
function undelegateAllExplicit(
address[] memory _delegateAddresses
)
external override
returns (uint256 _remainingDelegation)
{
return _checkWriteVpContract().undelegateAllExplicit(msg.sender, _delegateAddresses);
}
/**
* @notice Revoke all delegation from sender to `_who` at given block.
* Only affects the reads via `votePowerOfAtCached()` in the block `_blockNumber`.
* Block `_blockNumber` must be in the past.
* This method should be used only to prevent rogue delegate voting in the current voting block.
* To stop delegating use delegate/delegateExplicit with value of 0 or undelegateAll/undelegateAllExplicit.
*/
function revokeDelegationAt(address _who, uint256 _blockNumber) public override {
IIVPContract writeVPC = writeVpContract;
IIVPContract readVPC = readVpContract;
if (address(writeVPC) != address(0)) {
writeVPC.revokeDelegationAt(msg.sender, _who, balanceOfAt(msg.sender, _blockNumber), _blockNumber);
}
if (address(readVPC) != address(writeVPC) && address(readVPC) != address(0)) {
try readVPC.revokeDelegationAt(msg.sender, _who, balanceOfAt(msg.sender, _blockNumber), _blockNumber) {
} catch {
// do nothing
}
}
}
/**
* @notice Get current delegated vote power `_from` delegator delegated `_to` delegatee.
* @param _from Address of delegator
* @param _to Address of delegatee
* @return votePower The delegated vote power.
*/
function votePowerFromTo(
address _from,
address _to
)
external view override
returns(uint256)
{
return _checkReadVpContract().votePowerFromTo(_from, _to, balanceOf(_from));
}
/**
* @notice Get delegated the vote power `_from` delegator delegated `_to` delegatee at `_blockNumber`.
* @param _from Address of delegator
* @param _to Address of delegatee
* @param _blockNumber The block number at which to fetch.
* @return The delegated vote power.
*/
function votePowerFromToAt(
address _from,
address _to,
uint256 _blockNumber
)
external view override
returns(uint256)
{
return _checkReadVpContract().votePowerFromToAt(_from, _to, balanceOfAt(_from, _blockNumber), _blockNumber);
}
/**
* @notice Get the current total vote power.
* @return The current total vote power (sum of all accounts' vote powers).
*/
function totalVotePower() external view override returns(uint256) {
return totalSupply();
}
/**
* @notice Get the total vote power at block `_blockNumber`
* @param _blockNumber The block number at which to fetch.
* @return The total vote power at the block (sum of all accounts' vote powers).
*/
function totalVotePowerAt(uint256 _blockNumber) external view override returns(uint256) {
return totalSupplyAt(_blockNumber);
}
/**
* @notice Get the total vote power at block `_blockNumber` using cache.
* It tries to read the cached value and if not found, reads the actual value and stores it in cache.
* Can only be used if `_blockNumber` is in the past, otherwise reverts.
* @param _blockNumber The block number at which to fetch.
* @return The total vote power at the block (sum of all accounts' vote powers).
*/
function totalVotePowerAtCached(uint256 _blockNumber) public override returns(uint256) {
return _totalSupplyAtCached(_blockNumber);
}
/**
* @notice Get the delegation mode for '_who'. This mode determines whether vote power is
* allocated by percentage or by explicit value. Once the delegation mode is set,
* it never changes, even if all delegations are removed.
* @param _who The address to get delegation mode.
* @return delegation mode: 0 = NOTSET, 1 = PERCENTAGE, 2 = AMOUNT (i.e. explicit)
*/
function delegationModeOf(address _who) external view override returns (uint256) {
return _checkReadVpContract().delegationModeOf(_who);
}
/**
* @notice Get the current vote power of `_owner`.
* @param _owner The address to get voting power.
* @return Current vote power of `_owner`.
*/
function votePowerOf(address _owner) external view override returns(uint256) {
return _checkReadVpContract().votePowerOf(_owner);
}
/**
* @notice Get the vote power of `_owner` at block `_blockNumber`
* @param _owner The address to get voting power.
* @param _blockNumber The block number at which to fetch.
* @return Vote power of `_owner` at `_blockNumber`.
*/
function votePowerOfAt(address _owner, uint256 _blockNumber) external view override returns(uint256) {
return _checkReadVpContract().votePowerOfAt(_owner, _blockNumber);
}
/**
* Return vote powers for several addresses in a batch.
* @param _owners The list of addresses to fetch vote power of.
* @param _blockNumber The block number at which to fetch.
* @return A list of vote powers.
*/
function batchVotePowerOfAt(
address[] memory _owners,
uint256 _blockNumber
)
external view override
returns(uint256[] memory)
{
return _checkReadVpContract().batchVotePowerOfAt(_owners, _blockNumber);
}
/**
* @notice Get the vote power of `_owner` at block `_blockNumber` using cache.
* It tries to read the cached value and if not found, reads the actual value and stores it in cache.
* Can only be used if _blockNumber is in the past, otherwise reverts.
* @param _owner The address to get voting power.
* @param _blockNumber The block number at which to fetch.
* @return Vote power of `_owner` at `_blockNumber`.
*/
function votePowerOfAtCached(address _owner, uint256 _blockNumber) public override returns(uint256) {
return _checkReadVpContract().votePowerOfAtCached(_owner, _blockNumber);
}
/**
* @notice Get the vote power delegation `delegationAddresses`
* and `_bips` of `_who`. Returned in two separate positional arrays.
* @param _owner The address to get delegations.
* @return _delegateAddresses Positional array of delegation addresses.
* @return _bips Positional array of delegation percents specified in basis points (1/100 or 1 percent)
* @return _count The number of delegates.
* @return _delegationMode The mode of the delegation (NOTSET=0, PERCENTAGE=1, AMOUNT=2).
*/
function delegatesOf(
address _owner
)
external view override
returns (
address[] memory _delegateAddresses,
uint256[] memory _bips,
uint256 _count,
uint256 _delegationMode
)
{
return _checkReadVpContract().delegatesOf(_owner);
}
/**
* @notice Get the vote power delegation `delegationAddresses`
* and `pcts` of `_who`. Returned in two separate positional arrays.
* @param _owner The address to get delegations.
* @param _blockNumber The block for which we want to know the delegations.
* @return _delegateAddresses Positional array of delegation addresses.
* @return _bips Positional array of delegation percents specified in basis points (1/100 or 1 percent)
* @return _count The number of delegates.
* @return _delegationMode The mode of the delegation (NOTSET=0, PERCENTAGE=1, AMOUNT=2).
*/
function delegatesOfAt(
address _owner,
uint256 _blockNumber
)
external view override
returns (
address[] memory _delegateAddresses,
uint256[] memory _bips,
uint256 _count,
uint256 _delegationMode
)
{
return _checkReadVpContract().delegatesOfAt(_owner, _blockNumber);
}
// Update vote power and balance checkpoints before balances are modified. This is implemented
// in the _beforeTokenTransfer hook, which is executed for _mint, _burn, and _transfer operations.
function _beforeTokenTransfer(
address _from,
address _to,
uint256 _amount
)
internal virtual
override(ERC20)
{
require(_from != _to, "Cannot transfer to self");
uint256 fromBalance = balanceOf(_from);
uint256 toBalance = balanceOf(_to);
// update vote powers
IIVPContract vpc = writeVpContract;
if (address(vpc) != address(0)) {
vpc.updateAtTokenTransfer(_from, _to, fromBalance, toBalance, _amount);
} else if (!needsReplacementVPContract) {
// transfers without vpcontract are allowed, but after they are made
// any added vpcontract must have isReplacement set
needsReplacementVPContract = true;
}
// update governance vote powers
IIGovernanceVotePower gvp = governanceVP;
if (address(gvp) != address(0)) {
gvp.updateAtTokenTransfer(_from, _to, fromBalance, toBalance, _amount);
}
// update balance history
_updateBalanceHistoryAtTransfer(_from, _to, _amount);
}
/**
* Call from governance to set read VpContract on token, e.g.
* `vpToken.setReadVpContract(new VPContract(vpToken))`
* Read VPContract must be set before any of the VPToken delegation or vote power reading methods are called,
* otherwise they will revert.
* NOTE: If readVpContract differs from writeVpContract all reads will be "frozen" and will not reflect
* changes (not even revokes; they may or may not reflect balance transfers).
* @param _vpContract Read vote power contract to be used by this token.
*/
function setReadVpContract(IIVPContract _vpContract) external onlyGovernance {
if (address(_vpContract) != address(0)) {
require(address(_vpContract.ownerToken()) == address(this),
"VPContract not owned by this token");
// set contract's cleanup block
_vpContract.setCleanupBlockNumber(_cleanupBlockNumber());
}
emit VotePowerContractChanged(0, address(readVpContract), address(_vpContract));
readVpContract = _vpContract;
}
/**
* Call from governance to set write VpContract on token, e.g.
* `vpToken.setWriteVpContract(new VPContract(vpToken))`
* Write VPContract must be set before any of the VPToken delegation modifying methods are called,
* otherwise they will revert.
* @param _vpContract Write vote power contract to be used by this token.
*/
function setWriteVpContract(IIVPContract _vpContract) external onlyGovernance {
if (address(_vpContract) != address(0)) {
require(address(_vpContract.ownerToken()) == address(this),
"VPContract not owned by this token");
require(!needsReplacementVPContract || _vpContract.isReplacement(),
"VPContract not configured for replacement");
// set contract's cleanup block
_vpContract.setCleanupBlockNumber(_cleanupBlockNumber());
// once a non-null vpcontract is set, every other has to have isReplacement flag set
needsReplacementVPContract = true;
}
emit VotePowerContractChanged(1, address(writeVpContract), address(_vpContract));
writeVpContract = _vpContract;
}
/**
* Return read vpContract, ensuring that it is not zero.
*/
function _checkReadVpContract() internal view returns (IIVPContract) {
IIVPContract vpc = readVpContract;
require(address(vpc) != address(0), "Token missing read VPContract");
return vpc;
}
/**
* Return write vpContract, ensuring that it is not zero.
*/
function _checkWriteVpContract() internal view returns (IIVPContract) {
IIVPContract vpc = writeVpContract;
require(address(vpc) != address(0), "Token missing write VPContract");
return vpc;
}
/**
* Return vpContract use for reading, may be zero.
*/
function _getReadVpContract() internal view returns (IIVPContract) {
return readVpContract;
}
/**
* Return vpContract use for writing, may be zero.
*/
function _getWriteVpContract() internal view returns (IIVPContract) {
return writeVpContract;
}
/**
* Returns VPContract event interface used for readonly operations (view methods).
*/
function readVotePowerContract() external view override returns (IVPContractEvents) {
return readVpContract;
}
/**
* Returns VPContract event interface used for state changing operations (non-view methods).
*/
function writeVotePowerContract() external view override returns (IVPContractEvents) {
return writeVpContract;
}
/**
* Set the cleanup block number.
* Historic data for the blocks before `cleanupBlockNumber` can be erased,
* history before that block should never be used since it can be inconsistent.
* In particular, cleanup block number must be before current vote power block.
* @param _blockNumber The new cleanup block number.
*/
function setCleanupBlockNumber(uint256 _blockNumber) external override {
require(msg.sender == address(governance) || msg.sender == cleanupBlockNumberManager,
"only governance or manager");
_setCleanupBlockNumber(_blockNumber);
if (address(readVpContract) != address(0)) {
readVpContract.setCleanupBlockNumber(_blockNumber);
}
if (address(writeVpContract) != address(0) && address(writeVpContract) != address(readVpContract)) {
writeVpContract.setCleanupBlockNumber(_blockNumber);
}
if (address(governanceVP) != address(0)) {
governanceVP.setCleanupBlockNumber(_blockNumber);
}
}
/**
* Get the current cleanup block number.
*/
function cleanupBlockNumber() external view override returns (uint256) {
return _cleanupBlockNumber();
}
/**
* Set the contract that is allowed to set cleanupBlockNumber.
* Usually this will be an instance of CleanupBlockNumberManager.
*/
function setCleanupBlockNumberManager(address _cleanupBlockNumberManager) external override onlyGovernance {
cleanupBlockNumberManager = _cleanupBlockNumberManager;
}
/**
* Set the contract that is allowed to call history cleaning methods.
*/
function setCleanerContract(address _cleanerContract) external override onlyGovernance {
_setCleanerContract(_cleanerContract);
if (address(readVpContract) != address(0)) {
readVpContract.setCleanerContract(_cleanerContract);
}
if (address(writeVpContract) != address(0) && address(writeVpContract) != address(readVpContract)) {
writeVpContract.setCleanerContract(_cleanerContract);
}
if (address(governanceVP) != address(0)) {
governanceVP.setCleanerContract(_cleanerContract);
}
}
/**
* Sets new governance vote power contract that allows token owners to participate in governance voting
* and delegate governance vote power.
*/
function setGovernanceVotePower(IIGovernanceVotePower _governanceVotePower) external override onlyGovernance {
require(address(_governanceVotePower.ownerToken()) == address(this),
"Governance vote power contract does not belong to this token.");
emit VotePowerContractChanged(2, address(governanceVP), address(_governanceVotePower));
governanceVP = _governanceVotePower;
}
/**
* When set, allows token owners to participate in governance voting
* and delegate governance vote power.
*/
function governanceVotePower() external view override returns (IGovernanceVotePower) {
return governanceVP;
}
}
// File contracts/token/lib/DelegationHistory.sol
//
pragma solidity 0.7.6;
/**
* @title DelegationHistory library
* @notice A contract to manage checkpoints as of a given block.
* @dev Store value history by block number with detachable state.
**/
library DelegationHistory {
using SafeMath for uint256;
using SafePct for uint256;
uint256 public constant MAX_DELEGATES_BY_PERCENT = 2;
string private constant MAX_DELEGATES_MSG = "Max delegates exceeded";
/**
* @dev `CheckPoint` is the structure that attaches a block number to a
* given value; the block number attached is the one that last changed the
* value
**/
struct CheckPoint {
// `fromBlock` is the block number that the value was generated from
uint256 fromBlock;
// the list of active delegates at this time
address[] delegates;
// the values delegated to the corresponding delegate at this time
uint256[] values;
}
struct CheckPointHistoryState {
// `checkpoints` is an array that tracks delegations at non-contiguous block numbers
CheckPoint[] checkpoints;
// `checkpoints` before `startIndex` have been deleted
// INVARIANT: checkpoints.length == 0 || startIndex < checkpoints.length (strict!)
uint256 startIndex;
}
/**
* @notice Queries the value at a specific `_blockNumber`
* @param _self A CheckPointHistoryState instance to manage.
* @param _delegate The delegate for which we need value.
* @param _blockNumber The block number of the value active at that time
* @return _value The value of the `_delegate` at `_blockNumber`
**/
function valueOfAt(
CheckPointHistoryState storage _self,
address _delegate,
uint256 _blockNumber
)
internal view
returns (uint256 _value)
{
(bool found, uint256 index) = _findGreatestBlockLessThan(_self.checkpoints, _self.startIndex, _blockNumber);
if (!found) return 0;
// find the delegate and return the corresponding value
CheckPoint storage cp = _self.checkpoints[index];
uint256 length = cp.delegates.length;
for (uint256 i = 0; i < length; i++) {
if (cp.delegates[i] == _delegate) {
return cp.values[i];
}
}
return 0; // _delegate not found
}
/**
* @notice Queries the value at `block.number`
* @param _self A CheckPointHistoryState instance to manage.
* @param _delegate The delegate for which we need value.
* @return _value The value at `block.number`
**/
function valueOfAtNow(
CheckPointHistoryState storage _self,
address _delegate
)
internal view
returns (uint256 _value)
{
return valueOfAt(_self, _delegate, block.number);
}
/**
* @notice Writes the value at the current block.
* @param _self A CheckPointHistoryState instance to manage.
* @param _delegate The delegate tu update.
* @param _value The new value to set for this delegate (value `0` deletes `_delegate` from the list).
**/
function writeValue(
CheckPointHistoryState storage _self,
address _delegate,
uint256 _value
)
internal
{
uint256 historyCount = _self.checkpoints.length;
if (historyCount == 0) {
// checkpoints array empty, push new CheckPoint
if (_value != 0) {
CheckPoint storage cp = _self.checkpoints.push();
cp.fromBlock = block.number;
cp.delegates.push(_delegate);
cp.values.push(_value);
}
} else {
// historyCount - 1 is safe, since historyCount != 0
CheckPoint storage lastCheckpoint = _self.checkpoints[historyCount - 1];
uint256 lastBlock = lastCheckpoint.fromBlock;
// slither-disable-next-line incorrect-equality
if (block.number == lastBlock) {
// If last check point is the current block, just update
_updateDelegates(lastCheckpoint, _delegate, _value);
} else {
// we should never have future blocks in history
assert(block.number > lastBlock);
// last check point block is before
CheckPoint storage cp = _self.checkpoints.push();
cp.fromBlock = block.number;
_copyAndUpdateDelegates(cp, lastCheckpoint, _delegate, _value);
}
}
}
/**
* Get all percentage delegations active at a time.
* @param _self A CheckPointHistoryState instance to manage.
* @param _blockNumber The block number to query.
* @return _delegates The active percentage delegates at the time.
* @return _values The delegates' values at the time.
**/
function delegationsAt(
CheckPointHistoryState storage _self,
uint256 _blockNumber
)
internal view
returns (
address[] memory _delegates,
uint256[] memory _values
)
{
(bool found, uint256 index) = _findGreatestBlockLessThan(_self.checkpoints, _self.startIndex, _blockNumber);
if (!found) {
return (new address[](0), new uint256[](0));
}
// copy delegates and values to memory arrays
// (to prevent caller updating the stored value)
CheckPoint storage cp = _self.checkpoints[index];
uint256 length = cp.delegates.length;
_delegates = new address[](length);
_values = new uint256[](length);
for (uint256 i = 0; i < length; i++) {
_delegates[i] = cp.delegates[i];
_values[i] = cp.values[i];
}
}
/**
* Get all percentage delegations active now.
* @param _self A CheckPointHistoryState instance to manage.
* @return _delegates The active percentage delegates.
* @return _values The delegates' values.
**/
function delegationsAtNow(
CheckPointHistoryState storage _self
)
internal view
returns (address[] memory _delegates, uint256[] memory _values)
{
return delegationsAt(_self, block.number);
}
/**
* Get the number of delegations.
* @param _self A CheckPointHistoryState instance to query.
* @param _blockNumber The block number to query.
* @return _count Count of delegations at the time.
**/
function countAt(
CheckPointHistoryState storage _self,
uint256 _blockNumber
)
internal view
returns (uint256 _count)
{
(bool found, uint256 index) = _findGreatestBlockLessThan(_self.checkpoints, _self.startIndex, _blockNumber);
if (!found) return 0;
return _self.checkpoints[index].delegates.length;
}
/**
* Get the sum of all delegation values.
* @param _self A CheckPointHistoryState instance to query.
* @param _blockNumber The block number to query.
* @return _total Total delegation value at the time.
**/
function totalValueAt(
CheckPointHistoryState storage _self,
uint256 _blockNumber
)
internal view
returns (uint256 _total)
{
(bool found, uint256 index) = _findGreatestBlockLessThan(_self.checkpoints, _self.startIndex, _blockNumber);
if (!found) return 0;
CheckPoint storage cp = _self.checkpoints[index];
uint256 length = cp.values.length;
_total = 0;
for (uint256 i = 0; i < length; i++) {
_total = _total.add(cp.values[i]);
}
}
/**
* Get the sum of all delegation values.
* @param _self A CheckPointHistoryState instance to query.
* @return _total Total delegation value at the time.
**/
function totalValueAtNow(
CheckPointHistoryState storage _self
)
internal view
returns (uint256 _total)
{
return totalValueAt(_self, block.number);
}
/**
* Get the sum of all delegation values, every one scaled by `_mul/_div`.
* @param _self A CheckPointHistoryState instance to query.
* @param _mul The multiplier.
* @param _div The divisor.
* @param _blockNumber The block number to query.
* @return _total Total scaled delegation value at the time.
**/
function scaledTotalValueAt(
CheckPointHistoryState storage _self,
uint256 _mul,
uint256 _div,
uint256 _blockNumber
)
internal view
returns (uint256 _total)
{
(bool found, uint256 index) = _findGreatestBlockLessThan(_self.checkpoints, _self.startIndex, _blockNumber);
if (!found) return 0;
CheckPoint storage cp = _self.checkpoints[index];
uint256 length = cp.values.length;
_total = 0;
for (uint256 i = 0; i < length; i++) {
_total = _total.add(cp.values[i].mulDiv(_mul, _div));
}
}
/**
* Clear all delegations at this moment.
* @param _self A CheckPointHistoryState instance to manage.
*/
function clear(CheckPointHistoryState storage _self) internal {
if (_self.checkpoints.length > 0) {
// add an empty checkpoint
CheckPoint storage cp = _self.checkpoints.push();
cp.fromBlock = block.number;
}
}
/**
* Delete at most `_count` of the oldest checkpoints.
* At least one checkpoint at or before `_cleanupBlockNumber` will remain
* (unless the history was empty to start with).
*/
function cleanupOldCheckpoints(
CheckPointHistoryState storage _self,
uint256 _count,
uint256 _cleanupBlockNumber
)
internal
returns (uint256)
{
if (_cleanupBlockNumber == 0) return 0; // optimization for when cleaning is not enabled
uint256 length = _self.checkpoints.length;
if (length == 0) return 0;
uint256 startIndex = _self.startIndex;
// length - 1 is safe, since length != 0 (check above)
uint256 endIndex = Math.min(startIndex.add(_count), length - 1); // last element can never be deleted
uint256 index = startIndex;
// we can delete `checkpoint[index]` while the next checkpoint is at `_cleanupBlockNumber` or before
while (index < endIndex && _self.checkpoints[index + 1].fromBlock <= _cleanupBlockNumber) {
delete _self.checkpoints[index];
index++;
}
if (index > startIndex) { // index is the first not deleted index
_self.startIndex = index;
}
return index - startIndex; // safe: index = startIndex at start and increases in loop
}
/////////////////////////////////////////////////////////////////////////////////
// helper functions for writeValueAt
function _copyAndUpdateDelegates(
CheckPoint storage _cp,
CheckPoint storage _orig,
address _delegate,
uint256 _value
)
private
{
uint256 length = _orig.delegates.length;
bool updated = false;
for (uint256 i = 0; i < length; i++) {
address origDelegate = _orig.delegates[i];
if (origDelegate == _delegate) {
// copy delegate, but with new value
_appendDelegate(_cp, origDelegate, _value, i);
updated = true;
} else {
// just copy the delegate with original value
_appendDelegate(_cp, origDelegate, _orig.values[i], i);
}
}
if (!updated) {
// delegate is not in the original list, so add it
_appendDelegate(_cp, _delegate, _value, length);
}
}
function _updateDelegates(CheckPoint storage _cp, address _delegate, uint256 _value) private {
uint256 length = _cp.delegates.length;
uint256 i = 0;
while (i < length && _cp.delegates[i] != _delegate) ++i;
if (i < length) {
if (_value != 0) {
_cp.values[i] = _value;
} else {
_deleteDelegate(_cp, i, length - 1); // length - 1 is safe: 0 <= i < length
}
} else {
_appendDelegate(_cp, _delegate, _value, length);
}
}
function _appendDelegate(CheckPoint storage _cp, address _delegate, uint256 _value, uint256 _length) private {
if (_value != 0) {
require(_length < MAX_DELEGATES_BY_PERCENT, MAX_DELEGATES_MSG);
_cp.delegates.push(_delegate);
_cp.values.push(_value);
}
}
function _deleteDelegate(CheckPoint storage _cp, uint256 _index, uint256 _last) private {
if (_index < _last) {
_cp.delegates[_index] = _cp.delegates[_last];
_cp.values[_index] = _cp.values[_last];
}
_cp.delegates.pop();
_cp.values.pop();
}
/////////////////////////////////////////////////////////////////////////////////
// helper functions for querying
/**
* @notice Binary search of _checkpoints array.
* @param _checkpoints An array of CheckPoint to search.
* @param _startIndex Smallest possible index to be returned.
* @param _blockNumber The block number to search for.
*/
function _binarySearchGreatestBlockLessThan(
CheckPoint[] storage _checkpoints,
uint256 _startIndex,
uint256 _blockNumber
)
private view
returns (uint256 _index)
{
// Binary search of the value by given block number in the array
uint256 min = _startIndex;
uint256 max = _checkpoints.length.sub(1);
while (max > min) {
uint256 mid = (max.add(min).add(1)).div(2);
if (_checkpoints[mid].fromBlock <= _blockNumber) {
min = mid;
} else {
max = mid.sub(1);
}
}
return min;
}
/**
* @notice Binary search of _checkpoints array. Extra optimized for the common case when we are
* searching for the last block.
* @param _checkpoints An array of CheckPoint to search.
* @param _startIndex Smallest possible index to be returned.
* @param _blockNumber The block number to search for.
* @return _found true if value was found (only `false` if `_blockNumber` is before first
* checkpoint or the checkpoint array is empty)
* @return _index index of the newest block with number less than or equal `_blockNumber`
*/
function _findGreatestBlockLessThan(
CheckPoint[] storage _checkpoints,
uint256 _startIndex,
uint256 _blockNumber
)
private view
returns (
bool _found,
uint256 _index
)
{
uint256 historyCount = _checkpoints.length;
if (historyCount == 0) {
_found = false;
} else if (_blockNumber >= block.number || _blockNumber >= _checkpoints[historyCount - 1].fromBlock) {
// _blockNumber >= block.number saves one storage read for reads at current block
_found = true;
_index = historyCount - 1; // safe, historyCount != 0 in this branch
} else if (_blockNumber < _checkpoints[_startIndex].fromBlock) {
// reading data before `_startIndex` is only safe before first cleanup
require(_startIndex == 0, "DelegationHistory: reading from cleaned-up block");
_found = false;
} else {
_found = true;
_index = _binarySearchGreatestBlockLessThan(_checkpoints, _startIndex, _blockNumber);
}
}
}
// File contracts/token/lib/PercentageDelegation.sol
//
pragma solidity 0.7.6;
/**
* @title PercentageDelegation library
* @notice Only handles percentage delegation
* @notice A library to manage a group of _delegates for allocating voting power by a delegator.
**/
library PercentageDelegation {
using CheckPointHistory for CheckPointHistory.CheckPointHistoryState;
using DelegationHistory for DelegationHistory.CheckPointHistoryState;
using SafeMath for uint256;
using SafePct for uint256;
uint256 public constant MAX_BIPS = 10000;
string private constant MAX_BIPS_MSG = "Max delegation bips exceeded";
/**
* @dev `DelegationState` is the state structure used by this library to contain/manage
* a grouing of _delegates (a PercentageDelegation) for a delegator.
*/
struct DelegationState {
// percentages by _delegates
DelegationHistory.CheckPointHistoryState delegation;
}
/**
* @notice Add or replace an existing _delegate with allocated vote power in basis points.
* @param _self A DelegationState instance to manage.
* @param _delegate The address of the _delegate to add/replace
* @param _bips Allocation of the delegation specified in basis points (1/100 of 1 percent)
* @dev If you send a `_bips` of zero, `_delegate` will be deleted if one
* exists in the delegation; if zero and `_delegate` does not exist, it will not be added.
*/
function addReplaceDelegate(
DelegationState storage _self,
address _delegate,
uint256 _bips
)
internal
{
// Check for max delegation basis points
assert(_bips <= MAX_BIPS);
// Change the delegate's percentage
_self.delegation.writeValue(_delegate, _bips);
// check the total
require(_self.delegation.totalValueAtNow() <= MAX_BIPS, MAX_BIPS_MSG);
}
/**
* @notice Get the total of the explicit vote power delegation bips of all delegates at given block.
* @param _self A DelegationState instance to manage.
* @param _blockNumber The block to query.
* @return _totalBips The total vote power bips delegated.
*/
function getDelegatedTotalAt(
DelegationState storage _self,
uint256 _blockNumber
)
internal view
returns (uint256 _totalBips)
{
return _self.delegation.totalValueAt(_blockNumber);
}
/**
* @notice Get the total of the bips vote power delegation bips of all _delegates.
* @param _self A DelegationState instance to manage.
* @return _totalBips The total vote power bips delegated.
*/
function getDelegatedTotal(
DelegationState storage _self
)
internal view
returns (uint256 _totalBips)
{
return _self.delegation.totalValueAtNow();
}
/**
* @notice Given a _delegate address, return the bips of the vote power delegation.
* @param _self A DelegationState instance to manage.
* @param _delegate The delegate address to find.
* @param _blockNumber The block to query.
* @return _bips The percent of vote power allocated to the delegate address.
*/
function getDelegatedValueAt(
DelegationState storage _self,
address _delegate,
uint256 _blockNumber
)
internal view
returns (uint256 _bips)
{
return _self.delegation.valueOfAt(_delegate, _blockNumber);
}
/**
* @notice Given a delegate address, return the bips of the vote power delegation.
* @param _self A DelegationState instance to manage.
* @param _delegate The delegate address to find.
* @return _bips The percent of vote power allocated to the delegate address.
*/
function getDelegatedValue(
DelegationState storage _self,
address _delegate
)
internal view
returns (uint256 _bips)
{
return _self.delegation.valueOfAtNow(_delegate);
}
/**
* @notice Returns lists of delegate addresses and corresponding values at given block.
* @param _self A DelegationState instance to manage.
* @param _blockNumber The block to query.
* @return _delegates Positional array of delegation addresses.
* @return _values Positional array of delegation percents specified in basis points (1/100 or 1 percent)
*/
function getDelegationsAt(
DelegationState storage _self,
uint256 _blockNumber
)
internal view
returns (
address[] memory _delegates,
uint256[] memory _values
)
{
return _self.delegation.delegationsAt(_blockNumber);
}
/**
* @notice Returns lists of delegate addresses and corresponding values.
* @param _self A DelegationState instance to manage.
* @return _delegates Positional array of delegation addresses.
* @return _values Positional array of delegation percents specified in basis points (1/100 or 1 percent)
*/
function getDelegations(
DelegationState storage _self
)
internal view
returns (
address[] memory _delegates,
uint256[] memory _values
)
{
return _self.delegation.delegationsAtNow();
}
/**
* Get the number of delegations.
* @param _self A DelegationState instance to manage.
* @param _blockNumber The block number to query.
* @return _count Count of delegations at the time.
**/
function getCountAt(
DelegationState storage _self,
uint256 _blockNumber
)
internal view
returns (uint256 _count)
{
return _self.delegation.countAt(_blockNumber);
}
/**
* Get the number of delegations.
* @param _self A DelegationState instance to manage.
* @return _count Count of delegations at the time.
**/
function getCount(
DelegationState storage _self
)
internal view
returns (uint256 _count)
{
return _self.delegation.countAt(block.number);
}
/**
* @notice Get the total amount (absolute) of the vote power delegation of all delegates.
* @param _self A DelegationState instance to manage.
* @param _balance Owner's balance.
* @return _totalAmount The total vote power amount delegated.
*/
function getDelegatedTotalAmountAt(
DelegationState storage _self,
uint256 _balance,
uint256 _blockNumber
)
internal view
returns (uint256 _totalAmount)
{
return _self.delegation.scaledTotalValueAt(_balance, MAX_BIPS, _blockNumber);
}
/**
* @notice Clears all delegates.
* @param _self A DelegationState instance to manage.
* @dev Delegation mode remains PERCENTAGE, even though the delgation is now empty.
*/
function clear(DelegationState storage _self) internal {
_self.delegation.clear();
}
/**
* Delete at most `_count` of the oldest checkpoints.
* At least one checkpoint at or before `_cleanupBlockNumber` will remain
* (unless the history was empty to start with).
*/
function cleanupOldCheckpoints(
DelegationState storage _self,
uint256 _count,
uint256 _cleanupBlockNumber
)
internal
returns (uint256)
{
return _self.delegation.cleanupOldCheckpoints(_count, _cleanupBlockNumber);
}
}
// File contracts/token/lib/ExplicitDelegation.sol
//
pragma solidity 0.7.6;
/**
* @title ExplicitDelegation library
* @notice A library to manage a group of delegates for allocating voting power by a delegator.
**/
library ExplicitDelegation {
using CheckPointHistory for CheckPointHistory.CheckPointHistoryState;
using CheckPointsByAddress for CheckPointsByAddress.CheckPointsByAddressState;
using SafeMath for uint256;
using SafePct for uint256;
/**
* @dev `DelegationState` is the state structure used by this library to contain/manage
* a grouing of delegates (a ExplicitDelegation) for a delegator.
*/
struct DelegationState {
CheckPointHistory.CheckPointHistoryState delegatedTotal;
// `delegatedVotePower` is a map of delegators pointing to a map of delegates
// containing a checkpoint history of delegated vote power balances.
CheckPointsByAddress.CheckPointsByAddressState delegatedVotePower;
}
/**
* @notice Add or replace an existing _delegate with new vote power (explicit).
* @param _self A DelegationState instance to manage.
* @param _delegate The address of the _delegate to add/replace
* @param _amount Allocation of the delegation as explicit amount
*/
function addReplaceDelegate(
DelegationState storage _self,
address _delegate,
uint256 _amount
)
internal
{
uint256 prevAmount = _self.delegatedVotePower.valueOfAtNow(_delegate);
uint256 newTotal = _self.delegatedTotal.valueAtNow().sub(prevAmount, "Total < 0").add(_amount);
_self.delegatedVotePower.writeValue(_delegate, _amount);
_self.delegatedTotal.writeValue(newTotal);
}
/**
* Delete at most `_count` of the oldest checkpoints.
* At least one checkpoint at or before `_cleanupBlockNumber` will remain
* (unless the history was empty to start with).
*/
function cleanupOldCheckpoints(
DelegationState storage _self,
address _owner,
uint256 _count,
uint256 _cleanupBlockNumber
)
internal
returns(uint256 _deleted)
{
_deleted = _self.delegatedTotal.cleanupOldCheckpoints(_count, _cleanupBlockNumber);
// safe: cleanupOldCheckpoints always returns the number of deleted elements which is small, so no owerflow
_deleted += _self.delegatedVotePower.cleanupOldCheckpoints(_owner, _count, _cleanupBlockNumber);
}
/**
* @notice Get the _total of the explicit vote power delegation amount.
* @param _self A DelegationState instance to manage.
* @param _blockNumber The block to query.
* @return _total The _total vote power amount delegated.
*/
function getDelegatedTotalAt(
DelegationState storage _self, uint256 _blockNumber
)
internal view
returns (uint256 _total)
{
return _self.delegatedTotal.valueAt(_blockNumber);
}
/**
* @notice Get the _total of the explicit vote power delegation amount.
* @param _self A DelegationState instance to manage.
* @return _total The total vote power amount delegated.
*/
function getDelegatedTotal(
DelegationState storage _self
)
internal view
returns (uint256 _total)
{
return _self.delegatedTotal.valueAtNow();
}
/**
* @notice Given a delegate address, return the explicit amount of the vote power delegation.
* @param _self A DelegationState instance to manage.
* @param _delegate The _delegate address to find.
* @param _blockNumber The block to query.
* @return _value The percent of vote power allocated to the _delegate address.
*/
function getDelegatedValueAt(
DelegationState storage _self,
address _delegate,
uint256 _blockNumber
)
internal view
returns (uint256 _value)
{
return _self.delegatedVotePower.valueOfAt(_delegate, _blockNumber);
}
/**
* @notice Given a delegate address, return the explicit amount of the vote power delegation.
* @param _self A DelegationState instance to manage.
* @param _delegate The _delegate address to find.
* @return _value The percent of vote power allocated to the _delegate address.
*/
function getDelegatedValue(
DelegationState storage _self,
address _delegate
)
internal view
returns (uint256 _value)
{
return _self.delegatedVotePower.valueOfAtNow(_delegate);
}
}
// File contracts/token/lib/VotePower.sol
//
pragma solidity 0.7.6;
/**
* @title Vote power library
* @notice A library to record delegate vote power balances by delegator
* and delegatee.
**/
library VotePower {
using CheckPointHistory for CheckPointHistory.CheckPointHistoryState;
using CheckPointsByAddress for CheckPointsByAddress.CheckPointsByAddressState;
using SafeMath for uint256;
/**
* @dev `VotePowerState` is state structure used by this library to manage vote
* power amounts by delegator and it's delegates.
*/
struct VotePowerState {
// `votePowerByAddress` is the map that tracks the voting power balance
// of each address, by block.
CheckPointsByAddress.CheckPointsByAddressState votePowerByAddress;
}
/**
* @notice This modifier checks that both addresses are non-zero.
* @param _delegator A delegator address.
* @param _delegatee A delegatee address.
*/
modifier addressesNotZero(address _delegator, address _delegatee) {
// Both addresses cannot be zero
assert(!(_delegator == address(0) && _delegatee == address(0)));
_;
}
/**
* @notice Burn vote power.
* @param _self A VotePowerState instance to manage.
* @param _owner The address of the vote power to be burned.
* @param _amount The amount of vote power to burn.
*/
function _burn(
VotePowerState storage _self,
address _owner,
uint256 _amount
)
internal
{
// Shortcut
if (_amount == 0) {
return;
}
// Cannot burn the zero address
assert(_owner != address(0));
// Burn vote power for address
_self.votePowerByAddress.transmit(_owner, address(0), _amount);
}
/**
* @notice Delegate vote power `_amount` to `_delegatee` address from `_delegator` address.
* @param _delegator Delegator address
* @param _delegatee Delegatee address
* @param _amount The _amount of vote power to send from _delegator to _delegatee
* @dev Amount recorded at the current block.
**/
function delegate(
VotePowerState storage _self,
address _delegator,
address _delegatee,
uint256 _amount
)
internal
addressesNotZero(_delegator, _delegatee)
{
// Shortcut
if (_amount == 0) {
return;
}
// Transmit vote power
_self.votePowerByAddress.transmit(_delegator, _delegatee, _amount);
}
/**
* @notice Mint vote power.
* @param _self A VotePowerState instance to manage.
* @param _owner The address owning the new vote power.
* @param _amount The amount of vote power to mint.
*/
function _mint(
VotePowerState storage _self,
address _owner,
uint256 _amount
)
internal
{
// Shortcut
if (_amount == 0) {
return;
}
// Cannot mint the zero address
assert(_owner != address(0));
// Mint vote power for address
_self.votePowerByAddress.transmit(address(0), _owner, _amount);
}
/**
* @notice Transmit current vote power `_amount` from `_delegator` to `_delegatee`.
* @param _delegator Address of delegator.
* @param _delegatee Address of delegatee.
* @param _amount Amount of vote power to transmit.
*/
function transmit(
VotePowerState storage _self,
address _delegator,
address _delegatee,
uint256 _amount
)
internal
addressesNotZero(_delegator, _delegatee)
{
_self.votePowerByAddress.transmit(_delegator, _delegatee, _amount);
}
/**
* @notice Undelegate vote power `_amount` from `_delegatee` address
* to `_delegator` address
* @param _delegator Delegator address
* @param _delegatee Delegatee address
* @param _amount The amount of vote power recovered by delegator from delegatee
**/
function undelegate(
VotePowerState storage _self,
address _delegator,
address _delegatee,
uint256 _amount
)
internal
addressesNotZero(_delegator, _delegatee)
{
// Shortcut
if (_amount == 0) {
return;
}
// Recover vote power
_self.votePowerByAddress.transmit(_delegatee, _delegator, _amount);
}
/**
* Delete at most `_count` of the oldest checkpoints.
* At least one checkpoint at or before `_cleanupBlockNumber` will remain
* (unless the history was empty to start with).
*/
function cleanupOldCheckpoints(
VotePowerState storage _self,
address _owner,
uint256 _count,
uint256 _cleanupBlockNumber
)
internal
returns (uint256)
{
return _self.votePowerByAddress.cleanupOldCheckpoints(_owner, _count, _cleanupBlockNumber);
}
/**
* @notice Get the vote power of `_who` at `_blockNumber`.
* @param _self A VotePowerState instance to manage.
* @param _who Address to get vote power.
* @param _blockNumber Block number of the block to fetch vote power.
* @return _votePower The fetched vote power.
*/
function votePowerOfAt(
VotePowerState storage _self,
address _who,
uint256 _blockNumber
)
internal view
returns(uint256 _votePower)
{
return _self.votePowerByAddress.valueOfAt(_who, _blockNumber);
}
/**
* @notice Get the current vote power of `_who`.
* @param _self A VotePowerState instance to manage.
* @param _who Address to get vote power.
* @return _votePower The fetched vote power.
*/
function votePowerOfAtNow(
VotePowerState storage _self,
address _who
)
internal view
returns(uint256 _votePower)
{
return votePowerOfAt(_self, _who, block.number);
}
}
// File contracts/token/lib/VotePowerCache.sol
//
pragma solidity 0.7.6;
/**
* @title Vote power library
* @notice A library to record delegate vote power balances by delegator
* and delegatee.
**/
library VotePowerCache {
using SafeMath for uint256;
using VotePower for VotePower.VotePowerState;
struct RevocationCacheRecord {
// revoking delegation only affects cached value therefore we have to track
// the revocation in order not to revoke twice
// mapping delegatee => revokedValue
mapping(address => uint256) revocations;
}
/**
* @dev `CacheState` is state structure used by this library to manage vote
* power amounts by delegator and it's delegates.
*/
struct CacheState {
// map keccak256([address, _blockNumber]) -> (value + 1)
mapping(bytes32 => uint256) valueCache;
// map keccak256([address, _blockNumber]) -> RevocationCacheRecord
mapping(bytes32 => RevocationCacheRecord) revocationCache;
}
/**
* @notice Get the cached value at given block. If there is no cached value, original
* value is returned and stored to cache. Cache never gets stale, because original
* value can never change in a past block.
* @param _self A VotePowerCache instance to manage.
* @param _votePower A VotePower instance to read from if cache is empty.
* @param _who Address to get vote power.
* @param _blockNumber Block number of the block to fetch vote power.
* precondition: _blockNumber < block.number
*/
function valueOfAt(
CacheState storage _self,
VotePower.VotePowerState storage _votePower,
address _who,
uint256 _blockNumber
)
internal
returns (uint256 _value, bool _createdCache)
{
bytes32 key = keccak256(abi.encode(_who, _blockNumber));
// is it in cache?
uint256 cachedValue = _self.valueCache[key];
if (cachedValue != 0) {
return (cachedValue - 1, false); // safe, cachedValue != 0
}
// read from _votePower
uint256 votePowerValue = _votePower.votePowerOfAt(_who, _blockNumber);
_writeCacheValue(_self, key, votePowerValue);
return (votePowerValue, true);
}
/**
* @notice Get the cached value at given block. If there is no cached value, original
* value is returned. Cache is never modified.
* @param _self A VotePowerCache instance to manage.
* @param _votePower A VotePower instance to read from if cache is empty.
* @param _who Address to get vote power.
* @param _blockNumber Block number of the block to fetch vote power.
* precondition: _blockNumber < block.number
*/
function valueOfAtReadonly(
CacheState storage _self,
VotePower.VotePowerState storage _votePower,
address _who,
uint256 _blockNumber
)
internal view
returns (uint256 _value)
{
bytes32 key = keccak256(abi.encode(_who, _blockNumber));
// is it in cache?
uint256 cachedValue = _self.valueCache[key];
if (cachedValue != 0) {
return cachedValue - 1; // safe, cachedValue != 0
}
// read from _votePower
return _votePower.votePowerOfAt(_who, _blockNumber);
}
/**
* @notice Delete cached value for `_who` at given block.
* Only used for history cleanup.
* @param _self A VotePowerCache instance to manage.
* @param _who Address to get vote power.
* @param _blockNumber Block number of the block to fetch vote power.
* @return _deleted The number of cache items deleted (always 0 or 1).
* precondition: _blockNumber < cleanupBlockNumber
*/
function deleteValueAt(
CacheState storage _self,
address _who,
uint256 _blockNumber
)
internal
returns (uint256 _deleted)
{
bytes32 key = keccak256(abi.encode(_who, _blockNumber));
if (_self.valueCache[key] != 0) {
delete _self.valueCache[key];
return 1;
}
return 0;
}
/**
* @notice Revoke vote power delegation from `from` to `to` at given block.
* Updates cached values for the block, so they are the only vote power values respecting revocation.
* @dev Only delegatees cached value is changed, delegator doesn't get the vote power back; so
* the revoked vote power is forfeit for as long as this vote power block is in use. This is needed to
* prevent double voting.
* @param _self A VotePowerCache instance to manage.
* @param _votePower A VotePower instance to read from if cache is empty.
* @param _from The delegator.
* @param _to The delegatee.
* @param _revokedValue Value of delegation is not stored here, so it must be supplied by caller.
* @param _blockNumber Block number of the block to modify.
* precondition: _blockNumber < block.number
*/
function revokeAt(
CacheState storage _self,
VotePower.VotePowerState storage _votePower,
address _from,
address _to,
uint256 _revokedValue,
uint256 _blockNumber
)
internal
{
if (_revokedValue == 0) return;
bytes32 keyFrom = keccak256(abi.encode(_from, _blockNumber));
if (_self.revocationCache[keyFrom].revocations[_to] != 0) {
revert("Already revoked");
}
// read values and prime cacheOf
(uint256 valueTo,) = valueOfAt(_self, _votePower, _to, _blockNumber);
// write new values
bytes32 keyTo = keccak256(abi.encode(_to, _blockNumber));
_writeCacheValue(_self, keyTo, valueTo.sub(_revokedValue, "Revoked value too large"));
// mark as revoked
_self.revocationCache[keyFrom].revocations[_to] = _revokedValue;
}
/**
* @notice Delete revocation from `_from` to `_to` at block `_blockNumber`.
* Only used for history cleanup.
* @param _self A VotePowerCache instance to manage.
* @param _from The delegator.
* @param _to The delegatee.
* @param _blockNumber Block number of the block to modify.
* precondition: _blockNumber < cleanupBlockNumber
*/
function deleteRevocationAt(
CacheState storage _self,
address _from,
address _to,
uint256 _blockNumber
)
internal
returns (uint256 _deleted)
{
bytes32 keyFrom = keccak256(abi.encode(_from, _blockNumber));
RevocationCacheRecord storage revocationRec = _self.revocationCache[keyFrom];
uint256 value = revocationRec.revocations[_to];
if (value != 0) {
delete revocationRec.revocations[_to];
return 1;
}
return 0;
}
/**
* @notice Returns true if `from` has revoked vote pover delgation of `to` in block `_blockNumber`.
* @param _self A VotePowerCache instance to manage.
* @param _from The delegator.
* @param _to The delegatee.
* @param _blockNumber Block number of the block to fetch result.
* precondition: _blockNumber < block.number
*/
function revokedFromToAt(
CacheState storage _self,
address _from,
address _to,
uint256 _blockNumber
)
internal view
returns (bool revoked)
{
bytes32 keyFrom = keccak256(abi.encode(_from, _blockNumber));
return _self.revocationCache[keyFrom].revocations[_to] != 0;
}
function _writeCacheValue(CacheState storage _self, bytes32 _key, uint256 _value) private {
// store to cacheOf (add 1 to differentiate from empty)
_self.valueCache[_key] = _value.add(1);
}
}
// File contracts/token/implementation/Delegatable.sol
//
pragma solidity 0.7.6;
/**
* @title Delegateable ERC20 behavior
* @notice An ERC20 Delegateable behavior to delegate voting power
* of a token to delegates. This contract orchestrates interaction between
* managing a delegation and the vote power allocations that result.
**/
contract Delegatable is IVPContractEvents {
using PercentageDelegation for PercentageDelegation.DelegationState;
using ExplicitDelegation for ExplicitDelegation.DelegationState;
using SafeMath for uint256;
using SafePct for uint256;
using VotePower for VotePower.VotePowerState;
using VotePowerCache for VotePowerCache.CacheState;
enum DelegationMode {
NOTSET,
PERCENTAGE,
AMOUNT
}
// The number of history cleanup steps executed for every write operation.
// It is more than 1 to make as certain as possible that all history gets cleaned eventually.
uint256 private constant CLEANUP_COUNT = 2;
string constant private UNDELEGATED_VP_TOO_SMALL_MSG =
"Undelegated vote power too small";
// Map that tracks delegation mode of each address.
mapping(address => DelegationMode) private delegationModes;
// `percentageDelegations` is the map that tracks the percentage voting power delegation of each address.
// Explicit delegations are tracked directly through votePower.
mapping(address => PercentageDelegation.DelegationState) private percentageDelegations;
mapping(address => ExplicitDelegation.DelegationState) private explicitDelegations;
// `votePower` tracks all voting power balances
VotePower.VotePowerState private votePower;
// `votePower` tracks all voting power balances
VotePowerCache.CacheState private votePowerCache;
// Historic data for the blocks before `cleanupBlockNumber` can be erased,
// history before that block should never be used since it can be inconsistent.
uint256 private cleanupBlockNumber;
// Address of the contract that is allowed to call methods for history cleaning.
address private cleanerContract;
/**
* Emitted when a vote power cache entry is created.
* Allows history cleaners to track vote power cache cleanup opportunities off-chain.
*/
event CreatedVotePowerCache(address _owner, uint256 _blockNumber);
// Most history cleanup opportunities can be deduced from standard events:
// Transfer(from, to, amount):
// - vote power checkpoints for `from` (if nonzero) and `to` (if nonzero)
// - vote power checkpoints for percentage delegatees of `from` and `to` are also created,
// but they don't have to be checked since Delegate events are also emitted in case of
// percentage delegation vote power change due to delegators balance change
// - Note: Transfer event is emitted from VPToken but vote power checkpoint delegationModes
// must be called on its writeVotePowerContract
// Delegate(from, to, priorVP, newVP):
// - vote power checkpoints for `from` and `to`
// - percentage delegation checkpoint for `from` (if `from` uses percentage delegation mode)
// - explicit delegation checkpoint from `from` to `to` (if `from` uses explicit delegation mode)
// Revoke(from, to, vp, block):
// - vote power cache for `from` and `to` at `block`
// - revocation cache block from `from` to `to` at `block`
/**
* Reading from history is not allowed before `cleanupBlockNumber`, since data before that
* might have been deleted and is thus unreliable.
*/
modifier notBeforeCleanupBlock(uint256 _blockNumber) {
require(_blockNumber >= cleanupBlockNumber, "Delegatable: reading from cleaned-up block");
_;
}
/**
* History cleaning methods can be called only from the cleaner address.
*/
modifier onlyCleaner {
require(msg.sender == cleanerContract, "Only cleaner contract");
_;
}
/**
* @notice (Un)Allocate `_owner` vote power of `_amount` across owner delegate
* vote power percentages.
* @param _owner The address of the vote power owner.
* @param _priorBalance The owner's balance before change.
* @param _newBalance The owner's balance after change.
*/
function _allocateVotePower(address _owner, uint256 _priorBalance, uint256 _newBalance) private {
// Only proceed if we have a delegation by percentage
if (delegationModes[_owner] == DelegationMode.PERCENTAGE) {
// Get the voting delegation for the _owner
PercentageDelegation.DelegationState storage delegation = percentageDelegations[_owner];
// Iterate over the delegates
(address[] memory delegates, uint256[] memory bipses) = delegation.getDelegations();
for (uint256 i = 0; i < delegates.length; i++) {
address delegatee = delegates[i];
// Compute the delegated vote power for the delegatee
uint256 priorValue = _priorBalance.mulDiv(bipses[i], PercentageDelegation.MAX_BIPS);
uint256 newValue = _newBalance.mulDiv(bipses[i], PercentageDelegation.MAX_BIPS);
// Compute new voting power
if (newValue > priorValue) {
// increase (subtraction is safe as newValue > priorValue)
votePower.delegate(_owner, delegatee, newValue - priorValue);
} else {
// decrease (subtraction is safe as newValue < priorValue)
votePower.undelegate(_owner, delegatee, priorValue - newValue);
}
votePower.cleanupOldCheckpoints(_owner, CLEANUP_COUNT, cleanupBlockNumber);
votePower.cleanupOldCheckpoints(delegatee, CLEANUP_COUNT, cleanupBlockNumber);
emit Delegate(_owner, delegatee, priorValue, newValue);
}
}
}
/**
* @notice Burn `_amount` of vote power for `_owner`.
* @param _owner The address of the _owner vote power to burn.
* @param _ownerCurrentBalance The current token balance of the owner (which is their allocatable vote power).
* @param _amount The amount of vote power to burn.
*/
function _burnVotePower(address _owner, uint256 _ownerCurrentBalance, uint256 _amount) internal {
// for PERCENTAGE delegation: reduce owner vote power allocations
// revert with the same error as ERC20 in case transfer exceeds balance
uint256 newOwnerBalance = _ownerCurrentBalance.sub(_amount, "ERC20: transfer amount exceeds balance");
_allocateVotePower(_owner, _ownerCurrentBalance, newOwnerBalance);
// for AMOUNT delegation: is there enough unallocated VP _to burn if explicitly delegated?
require(_isTransmittable(_owner, _ownerCurrentBalance, _amount), UNDELEGATED_VP_TOO_SMALL_MSG);
// burn vote power
votePower._burn(_owner, _amount);
votePower.cleanupOldCheckpoints(_owner, CLEANUP_COUNT, cleanupBlockNumber);
}
/**
* @notice Get whether `_owner` current delegation can be delegated by percentage.
* @param _owner Address of delegation to check.
* @return True if delegation can be delegated by percentage.
*/
function _canDelegateByPct(address _owner) internal view returns(bool) {
// Get the delegation mode.
DelegationMode delegationMode = delegationModes[_owner];
// Return true if delegation is safe _to store percents, which can also
// apply if there is not delegation mode set.
return delegationMode == DelegationMode.NOTSET || delegationMode == DelegationMode.PERCENTAGE;
}
/**
* @notice Get whether `_owner` current delegation can be delegated by amount.
* @param _owner Address of delegation to check.
* @return True if delegation can be delegated by amount.
*/
function _canDelegateByAmount(address _owner) internal view returns(bool) {
// Get the delegation mode.
DelegationMode delegationMode = delegationModes[_owner];
// Return true if delegation is safe to store explicit amounts, which can also
// apply if there is not delegation mode set.
return delegationMode == DelegationMode.NOTSET || delegationMode == DelegationMode.AMOUNT;
}
/**
* @notice Delegate `_amount` of voting power to `_to` from `_from`
* @param _from The address of the delegator
* @param _to The address of the recipient
* @param _senderCurrentBalance The senders current balance (not their voting power)
* @param _amount The amount of voting power to be delegated
**/
function _delegateByAmount(
address _from,
address _to,
uint256 _senderCurrentBalance,
uint256 _amount
)
internal virtual
{
require (_to != address(0), "Cannot delegate to zero");
require (_to != _from, "Cannot delegate to self");
require (_canDelegateByAmount(_from), "Cannot delegate by amount");
// Get the vote power delegation for the sender
ExplicitDelegation.DelegationState storage delegation = explicitDelegations[_from];
// the prior value
uint256 priorAmount = delegation.getDelegatedValue(_to);
// Delegate new power
if (_amount < priorAmount) {
// Prior amount is greater, just reduce the delegated amount.
// subtraction is safe since _amount < priorAmount
votePower.undelegate(_from, _to, priorAmount - _amount);
} else {
// Is there enough undelegated vote power?
uint256 availableAmount = _undelegatedVotePowerOf(_from, _senderCurrentBalance).add(priorAmount);
require(availableAmount >= _amount, UNDELEGATED_VP_TOO_SMALL_MSG);
// Increase the delegated amount of vote power.
// subtraction is safe since _amount >= priorAmount
votePower.delegate(_from, _to, _amount - priorAmount);
}
votePower.cleanupOldCheckpoints(_from, CLEANUP_COUNT, cleanupBlockNumber);
votePower.cleanupOldCheckpoints(_to, CLEANUP_COUNT, cleanupBlockNumber);
// Add/replace delegate
delegation.addReplaceDelegate(_to, _amount);
delegation.cleanupOldCheckpoints(_to, CLEANUP_COUNT, cleanupBlockNumber);
// update mode if needed
if (delegationModes[_from] != DelegationMode.AMOUNT) {
delegationModes[_from] = DelegationMode.AMOUNT;
}
// emit event for delegation change
emit Delegate(_from, _to, priorAmount, _amount);
}
/**
* @notice Delegate `_bips` of voting power to `_to` from `_from`
* @param _from The address of the delegator
* @param _to The address of the recipient
* @param _senderCurrentBalance The senders current balance (not their voting power)
* @param _bips The percentage of voting power in basis points (1/100 of 1 percent) to be delegated
**/
function _delegateByPercentage(
address _from,
address _to,
uint256 _senderCurrentBalance,
uint256 _bips
)
internal virtual
{
require (_to != address(0), "Cannot delegate to zero");
require (_to != _from, "Cannot delegate to self");
require (_canDelegateByPct(_from), "Cannot delegate by percentage");
// Get the vote power delegation for the sender
PercentageDelegation.DelegationState storage delegation = percentageDelegations[_from];
// Get prior percent for delegate if exists
uint256 priorBips = delegation.getDelegatedValue(_to);
uint256 reverseVotePower = 0;
uint256 newVotePower = 0;
// Add/replace delegate
delegation.addReplaceDelegate(_to, _bips);
delegation.cleanupOldCheckpoints(CLEANUP_COUNT, cleanupBlockNumber);
// First, back out old voting power percentage, if not zero
if (priorBips != 0) {
reverseVotePower = _senderCurrentBalance.mulDiv(priorBips, PercentageDelegation.MAX_BIPS);
}
// Calculate the new vote power
if (_bips != 0) {
newVotePower = _senderCurrentBalance.mulDiv(_bips, PercentageDelegation.MAX_BIPS);
}
// Delegate new power
if (newVotePower < reverseVotePower) {
// subtraction is safe since newVotePower < reverseVotePower
votePower.undelegate(_from, _to, reverseVotePower - newVotePower);
} else {
// subtraction is safe since newVotePower >= reverseVotePower
votePower.delegate(_from, _to, newVotePower - reverseVotePower);
}
votePower.cleanupOldCheckpoints(_from, CLEANUP_COUNT, cleanupBlockNumber);
votePower.cleanupOldCheckpoints(_to, CLEANUP_COUNT, cleanupBlockNumber);
// update mode if needed
if (delegationModes[_from] != DelegationMode.PERCENTAGE) {
delegationModes[_from] = DelegationMode.PERCENTAGE;
}
// emit event for delegation change
emit Delegate(_from, _to, reverseVotePower, newVotePower);
}
/**
* @notice Get the delegation mode for '_who'. This mode determines whether vote power is
* allocated by percentage or by explicit value.
* @param _who The address to get delegation mode.
* @return Delegation mode
*/
function _delegationModeOf(address _who) internal view returns (DelegationMode) {
return delegationModes[_who];
}
/**
* @notice Get the vote power delegation `delegationAddresses`
* and `_bips` of an `_owner`. Returned in two separate positional arrays.
* @param _owner The address to get delegations.
* @param _blockNumber The block for which we want to know the delegations.
* @return _delegateAddresses Positional array of delegation addresses.
* @return _bips Positional array of delegation percents specified in basis points (1/100 or 1 percent)
*/
function _percentageDelegatesOfAt(
address _owner,
uint256 _blockNumber
)
internal view
notBeforeCleanupBlock(_blockNumber)
returns (
address[] memory _delegateAddresses,
uint256[] memory _bips
)
{
PercentageDelegation.DelegationState storage delegation = percentageDelegations[_owner];
address[] memory allDelegateAddresses;
uint256[] memory allBips;
(allDelegateAddresses, allBips) = delegation.getDelegationsAt(_blockNumber);
// delete revoked addresses
for (uint256 i = 0; i < allDelegateAddresses.length; i++) {
if (votePowerCache.revokedFromToAt(_owner, allDelegateAddresses[i], _blockNumber)) {
allBips[i] = 0;
}
}
uint256 length = 0;
for (uint256 i = 0; i < allDelegateAddresses.length; i++) {
if (allBips[i] != 0) length++;
}
_delegateAddresses = new address[](length);
_bips = new uint256[](length);
uint256 destIndex = 0;
for (uint256 i = 0; i < allDelegateAddresses.length; i++) {
if (allBips[i] != 0) {
_delegateAddresses[destIndex] = allDelegateAddresses[i];
_bips[destIndex] = allBips[i];
destIndex++;
}
}
}
/**
* @notice Checks if enough undelegated vote power exists to allow a token
* transfer to occur if vote power is explicitly delegated.
* @param _owner The address of transmittable vote power to check.
* @param _ownerCurrentBalance The current balance of `_owner`.
* @param _amount The amount to check.
* @return True is `_amount` is transmittable.
*/
function _isTransmittable(
address _owner,
uint256 _ownerCurrentBalance,
uint256 _amount
)
private view returns(bool)
{
// Only proceed if we have a delegation by _amount
if (delegationModes[_owner] == DelegationMode.AMOUNT) {
// Return true if there is enough vote power _to cover the transfer
return _undelegatedVotePowerOf(_owner, _ownerCurrentBalance) >= _amount;
} else {
// Not delegated by _amount, so transfer always allowed
return true;
}
}
/**
* @notice Mint `_amount` of vote power for `_owner`.
* @param _owner The address of the owner to receive new vote power.
* @param _amount The amount of vote power to mint.
*/
function _mintVotePower(address _owner, uint256 _ownerCurrentBalance, uint256 _amount) internal {
votePower._mint(_owner, _amount);
votePower.cleanupOldCheckpoints(_owner, CLEANUP_COUNT, cleanupBlockNumber);
// Allocate newly minted vote power over delegates
_allocateVotePower(_owner, _ownerCurrentBalance, _ownerCurrentBalance.add(_amount));
}
/**
* @notice Revoke the vote power of `_to` at block `_blockNumber`
* @param _from The address of the delegator
* @param _to The delegatee address of vote power to revoke.
* @param _senderBalanceAt The sender's balance at the block to be revoked.
* @param _blockNumber The block number at which to revoke.
*/
function _revokeDelegationAt(
address _from,
address _to,
uint256 _senderBalanceAt,
uint256 _blockNumber
)
internal
notBeforeCleanupBlock(_blockNumber)
{
require(_blockNumber < block.number, "Revoke is only for the past, use undelegate for the present");
// Get amount revoked
uint256 votePowerRevoked = _votePowerFromToAtNoRevokeCheck(_from, _to, _senderBalanceAt, _blockNumber);
// Revoke vote power
votePowerCache.revokeAt(votePower, _from, _to, votePowerRevoked, _blockNumber);
// Emit revoke event
emit Revoke(_from, _to, votePowerRevoked, _blockNumber);
}
/**
* @notice Transmit `_amount` of vote power `_from` address `_to` address.
* @param _from The address of the sender.
* @param _to The address of the receiver.
* @param _fromCurrentBalance The current token balance of the transmitter.
* @param _toCurrentBalance The current token balance of the receiver.
* @param _amount The amount of vote power to transmit.
*/
function _transmitVotePower(
address _from,
address _to,
uint256 _fromCurrentBalance,
uint256 _toCurrentBalance,
uint256 _amount
)
internal
{
// for PERCENTAGE delegation: reduce sender vote power allocations
// revert with the same error as ERC20 in case transfer exceeds balance
uint256 newFromBalance = _fromCurrentBalance.sub(_amount, "ERC20: transfer amount exceeds balance");
_allocateVotePower(_from, _fromCurrentBalance, newFromBalance);
// for AMOUNT delegation: transmit vote power _to receiver
require(_isTransmittable(_from, _fromCurrentBalance, _amount), UNDELEGATED_VP_TOO_SMALL_MSG);
votePower.transmit(_from, _to, _amount);
votePower.cleanupOldCheckpoints(_from, CLEANUP_COUNT, cleanupBlockNumber);
votePower.cleanupOldCheckpoints(_to, CLEANUP_COUNT, cleanupBlockNumber);
// Allocate receivers new vote power according _to their delegates
_allocateVotePower(_to, _toCurrentBalance, _toCurrentBalance.add(_amount));
}
/**
* @notice Undelegate all vote power by percentage for `delegation` of `_who`.
* @param _from The address of the delegator
* @param _senderCurrentBalance The current balance of message sender.
* precondition: delegationModes[_who] == DelegationMode.PERCENTAGE
*/
function _undelegateAllByPercentage(address _from, uint256 _senderCurrentBalance) internal {
DelegationMode delegationMode = delegationModes[_from];
if (delegationMode == DelegationMode.NOTSET) return;
require(delegationMode == DelegationMode.PERCENTAGE,
"undelegateAll can only be used in percentage delegation mode");
PercentageDelegation.DelegationState storage delegation = percentageDelegations[_from];
// Iterate over the delegates
(address[] memory delegates, uint256[] memory _bips) = delegation.getDelegations();
for (uint256 i = 0; i < delegates.length; i++) {
address to = delegates[i];
// Compute vote power to be reversed for the delegate
uint256 reverseVotePower = _senderCurrentBalance.mulDiv(_bips[i], PercentageDelegation.MAX_BIPS);
// Transmit vote power back to _owner
votePower.undelegate(_from, to, reverseVotePower);
votePower.cleanupOldCheckpoints(_from, CLEANUP_COUNT, cleanupBlockNumber);
votePower.cleanupOldCheckpoints(to, CLEANUP_COUNT, cleanupBlockNumber);
// Emit vote power reversal event
emit Delegate(_from, to, reverseVotePower, 0);
}
// Clear delegates
delegation.clear();
delegation.cleanupOldCheckpoints(CLEANUP_COUNT, cleanupBlockNumber);
}
/**
* @notice Undelegate all vote power by amount delegates for `_from`.
* @param _from The address of the delegator
* @param _delegateAddresses Explicit delegation does not store delegatees' addresses,
* so the caller must supply them.
*/
function _undelegateAllByAmount(
address _from,
address[] memory _delegateAddresses
)
internal
returns (uint256 _remainingDelegation)
{
DelegationMode delegationMode = delegationModes[_from];
if (delegationMode == DelegationMode.NOTSET) return 0;
require(delegationMode == DelegationMode.AMOUNT,
"undelegateAllExplicit can only be used in explicit delegation mode");
ExplicitDelegation.DelegationState storage delegation = explicitDelegations[_from];
// Iterate over the delegates
for (uint256 i = 0; i < _delegateAddresses.length; i++) {
address to = _delegateAddresses[i];
// Compute vote power _to be reversed for the delegate
uint256 reverseVotePower = delegation.getDelegatedValue(to);
if (reverseVotePower == 0) continue;
// Transmit vote power back _to _owner
votePower.undelegate(_from, to, reverseVotePower);
votePower.cleanupOldCheckpoints(_from, CLEANUP_COUNT, cleanupBlockNumber);
votePower.cleanupOldCheckpoints(to, CLEANUP_COUNT, cleanupBlockNumber);
// change delagation
delegation.addReplaceDelegate(to, 0);
delegation.cleanupOldCheckpoints(to, CLEANUP_COUNT, cleanupBlockNumber);
// Emit vote power reversal event
emit Delegate(_from, to, reverseVotePower, 0);
}
return delegation.getDelegatedTotal();
}
/**
* @notice Check if the `_owner` has made any delegations.
* @param _owner The address of owner to get delegated vote power.
* @return The total delegated vote power at block.
*/
function _hasAnyDelegations(address _owner) internal view returns(bool) {
DelegationMode delegationMode = delegationModes[_owner];
if (delegationMode == DelegationMode.NOTSET) {
return false;
} else if (delegationMode == DelegationMode.AMOUNT) {
return explicitDelegations[_owner].getDelegatedTotal() > 0;
} else { // delegationMode == DelegationMode.PERCENTAGE
return percentageDelegations[_owner].getCount() > 0;
}
}
/**
* @notice Get the total delegated vote power of `_owner` at some block.
* @param _owner The address of owner to get delegated vote power.
* @param _ownerBalanceAt The balance of the owner at that block (not their vote power).
* @param _blockNumber The block number at which to fetch.
* @return _votePower The total delegated vote power at block.
*/
function _delegatedVotePowerOfAt(
address _owner,
uint256 _ownerBalanceAt,
uint256 _blockNumber
)
internal view
notBeforeCleanupBlock(_blockNumber)
returns(uint256 _votePower)
{
// Get the vote power delegation for the _owner
DelegationMode delegationMode = delegationModes[_owner];
if (delegationMode == DelegationMode.NOTSET) {
return 0;
} else if (delegationMode == DelegationMode.AMOUNT) {
return explicitDelegations[_owner].getDelegatedTotalAt(_blockNumber);
} else { // delegationMode == DelegationMode.PERCENTAGE
return percentageDelegations[_owner].getDelegatedTotalAmountAt(_ownerBalanceAt, _blockNumber);
}
}
/**
* @notice Get the undelegated vote power of `_owner` at some block.
* @param _owner The address of owner to get undelegated vote power.
* @param _ownerBalanceAt The balance of the owner at that block (not their vote power).
* @param _blockNumber The block number at which to fetch.
* @return _votePower The undelegated vote power at block.
*/
function _undelegatedVotePowerOfAt(
address _owner,
uint256 _ownerBalanceAt,
uint256 _blockNumber
)
internal view
notBeforeCleanupBlock(_blockNumber)
returns(uint256 _votePower)
{
// Return the current balance less delegations or zero if negative
uint256 delegated = _delegatedVotePowerOfAt(_owner, _ownerBalanceAt, _blockNumber);
bool overflow;
uint256 result;
(overflow, result) = _ownerBalanceAt.trySub(delegated);
return result;
}
/**
* @notice Get the undelegated vote power of `_owner`.
* @param _owner The address of owner to get undelegated vote power.
* @param _ownerCurrentBalance The current balance of the owner (not their vote power).
* @return _votePower The undelegated vote power.
*/
function _undelegatedVotePowerOf(
address _owner,
uint256 _ownerCurrentBalance
)
internal view
returns(uint256 _votePower)
{
return _undelegatedVotePowerOfAt(_owner, _ownerCurrentBalance, block.number);
}
/**
* @notice Get current delegated vote power `_from` delegator delegated `_to` delegatee.
* @param _from Address of delegator
* @param _to Address of delegatee
* @return _votePower The delegated vote power.
*/
function _votePowerFromTo(
address _from,
address _to,
uint256 _currentFromBalance
)
internal view
returns(uint256 _votePower)
{
// no need for revocation check at current block
return _votePowerFromToAtNoRevokeCheck(_from, _to, _currentFromBalance, block.number);
}
/**
* @notice Get delegated the vote power `_from` delegator delegated `_to` delegatee at `_blockNumber`.
* @param _from Address of delegator
* @param _to Address of delegatee
* @param _fromBalanceAt From's balance at the block `_blockNumber`.
* @param _blockNumber The block number at which to fetch.
* @return _votePower The delegated vote power.
*/
function _votePowerFromToAt(
address _from,
address _to,
uint256 _fromBalanceAt,
uint256 _blockNumber
)
internal view
notBeforeCleanupBlock(_blockNumber)
returns(uint256 _votePower)
{
// if revoked, return 0
if (votePowerCache.revokedFromToAt(_from, _to, _blockNumber)) return 0;
return _votePowerFromToAtNoRevokeCheck(_from, _to, _fromBalanceAt, _blockNumber);
}
/**
* @notice Get delegated the vote power `_from` delegator delegated `_to` delegatee at `_blockNumber`.
* Private use only - ignores revocations.
* @param _from Address of delegator
* @param _to Address of delegatee
* @param _fromBalanceAt From's balance at the block `_blockNumber`.
* @param _blockNumber The block number at which to fetch.
* @return _votePower The delegated vote power.
*/
function _votePowerFromToAtNoRevokeCheck(
address _from,
address _to,
uint256 _fromBalanceAt,
uint256 _blockNumber
)
private view
returns(uint256 _votePower)
{
// assumed: notBeforeCleanupBlock(_blockNumber)
DelegationMode delegationMode = delegationModes[_from];
if (delegationMode == DelegationMode.NOTSET) {
return 0;
} else if (delegationMode == DelegationMode.PERCENTAGE) {
uint256 _bips = percentageDelegations[_from].getDelegatedValueAt(_to, _blockNumber);
return _fromBalanceAt.mulDiv(_bips, PercentageDelegation.MAX_BIPS);
} else { // delegationMode == DelegationMode.AMOUNT
return explicitDelegations[_from].getDelegatedValueAt(_to, _blockNumber);
}
}
/**
* @notice Get the current vote power of `_who`.
* @param _who The address to get voting power.
* @return Current vote power of `_who`.
*/
function _votePowerOf(address _who) internal view returns(uint256) {
return votePower.votePowerOfAtNow(_who);
}
/**
* @notice Get the vote power of `_who` at block `_blockNumber`
* @param _who The address to get voting power.
* @param _blockNumber The block number at which to fetch.
* @return Vote power of `_who` at `_blockNumber`.
*/
function _votePowerOfAt(
address _who,
uint256 _blockNumber
)
internal view
notBeforeCleanupBlock(_blockNumber)
returns(uint256)
{
// read cached value for past blocks to respect revocations (and possibly get a cache speedup)
if (_blockNumber < block.number) {
return votePowerCache.valueOfAtReadonly(votePower, _who, _blockNumber);
} else {
return votePower.votePowerOfAtNow(_who);
}
}
/**
* Return vote powers for several addresses in a batch.
* Only works for past blocks.
* @param _owners The list of addresses to fetch vote power of.
* @param _blockNumber The block number at which to fetch.
* @return _votePowers A list of vote powers corresponding to _owners.
*/
function _batchVotePowerOfAt(
address[] memory _owners,
uint256 _blockNumber
)
internal view
notBeforeCleanupBlock(_blockNumber)
returns(uint256[] memory _votePowers)
{
require(_blockNumber < block.number, "Can only be used for past blocks");
_votePowers = new uint256[](_owners.length);
for (uint256 i = 0; i < _owners.length; i++) {
// read through cache, much faster if it has been set
_votePowers[i] = votePowerCache.valueOfAtReadonly(votePower, _owners[i], _blockNumber);
}
}
/**
* @notice Get the vote power of `_who` at block `_blockNumber`
* Reads/updates cache and upholds revocations.
* @param _who The address to get voting power.
* @param _blockNumber The block number at which to fetch.
* @return Vote power of `_who` at `_blockNumber`.
*/
function _votePowerOfAtCached(
address _who,
uint256 _blockNumber
)
internal
notBeforeCleanupBlock(_blockNumber)
returns(uint256)
{
require(_blockNumber < block.number, "Can only be used for past blocks");
(uint256 vp, bool createdCache) = votePowerCache.valueOfAt(votePower, _who, _blockNumber);
if (createdCache) emit CreatedVotePowerCache(_who, _blockNumber);
return vp;
}
/**
* Set the cleanup block number.
*/
function _setCleanupBlockNumber(uint256 _blockNumber) internal {
require(_blockNumber >= cleanupBlockNumber, "Cleanup block number must never decrease");
require(_blockNumber < block.number, "Cleanup block must be in the past");
cleanupBlockNumber = _blockNumber;
}
/**
* Get the cleanup block number.
*/
function _cleanupBlockNumber() internal view returns (uint256) {
return cleanupBlockNumber;
}
/**
* Set the contract that is allowed to call history cleaning methods.
*/
function _setCleanerContract(address _cleanerContract) internal {
cleanerContract = _cleanerContract;
}
// history cleanup methods
/**
* Delete vote power checkpoints that expired (i.e. are before `cleanupBlockNumber`).
* Method can only be called from the `cleanerContract` (which may be a proxy to external cleaners).
* @param _owner vote power owner account address
* @param _count maximum number of checkpoints to delete
* @return the number of checkpoints deleted
*/
function votePowerHistoryCleanup(address _owner, uint256 _count) external onlyCleaner returns (uint256) {
return votePower.cleanupOldCheckpoints(_owner, _count, cleanupBlockNumber);
}
/**
* Delete vote power cache entry that expired (i.e. is before `cleanupBlockNumber`).
* Method can only be called from the `cleanerContract` (which may be a proxy to external cleaners).
* @param _owner vote power owner account address
* @param _blockNumber the block number for which total supply value was cached
* @return the number of cache entries deleted (always 0 or 1)
*/
function votePowerCacheCleanup(address _owner, uint256 _blockNumber) external onlyCleaner returns (uint256) {
require(_blockNumber < cleanupBlockNumber, "No cleanup after cleanup block");
return votePowerCache.deleteValueAt(_owner, _blockNumber);
}
/**
* Delete revocation entry that expired (i.e. is before `cleanupBlockNumber`).
* Method can only be called from the `cleanerContract` (which may be a proxy to external cleaners).
* @param _from the delegator address
* @param _to the delegatee address
* @param _blockNumber the block number for which total supply value was cached
* @return the number of revocation entries deleted (always 0 or 1)
*/
function revocationCleanup(
address _from,
address _to,
uint256 _blockNumber
)
external onlyCleaner
returns (uint256)
{
require(_blockNumber < cleanupBlockNumber, "No cleanup after cleanup block");
return votePowerCache.deleteRevocationAt(_from, _to, _blockNumber);
}
/**
* Delete percentage delegation checkpoints that expired (i.e. are before `cleanupBlockNumber`).
* Method can only be called from the `cleanerContract` (which may be a proxy to external cleaners).
* @param _owner balance owner account address
* @param _count maximum number of checkpoints to delete
* @return the number of checkpoints deleted
*/
function percentageDelegationHistoryCleanup(address _owner, uint256 _count)
external onlyCleaner
returns (uint256)
{
return percentageDelegations[_owner].cleanupOldCheckpoints(_count, cleanupBlockNumber);
}
/**
* Delete explicit delegation checkpoints that expired (i.e. are before `cleanupBlockNumber`).
* Method can only be called from the `cleanerContract` (which may be a proxy to external cleaners).
* @param _from the delegator address
* @param _to the delegatee address
* @param _count maximum number of checkpoints to delete
* @return the number of checkpoints deleted
*/
function explicitDelegationHistoryCleanup(address _from, address _to, uint256 _count)
external
onlyCleaner
returns (uint256)
{
return explicitDelegations[_from].cleanupOldCheckpoints(_to, _count, cleanupBlockNumber);
}
}
// File contracts/token/implementation/VPContract.sol
//
pragma solidity 0.7.6;
contract VPContract is IIVPContract, Delegatable {
using SafeMath for uint256;
/**
* The VPToken (or some other contract) that owns this VPContract.
* All state changing methods may be called only from this address.
* This is because original msg.sender is sent in `_from` parameter
* and we must be sure that it cannot be faked by directly calling VPContract.
* Owner token is also used in case of replacement to recover vote powers from balances.
*/
IVPToken public immutable override ownerToken;
/**
* Return true if this IIVPContract is configured to be used as a replacement for other contract.
* It means that vote powers are not necessarily correct at the initialization, therefore
* every method that reads vote power must check whether it is initialized for that address and block.
*/
bool public immutable override isReplacement;
// the contract that is allowed to set cleanupBlockNumber
// usually this will be an instance of CleanupBlockNumberManager
// only set when detached from vptoken and directly registered to CleanupBlockNumberManager
address private cleanupBlockNumberManager;
// The block number when vote power for an address was first set.
// Reading vote power before this block would return incorrect result and must revert.
mapping (address => uint256) private votePowerInitializationBlock;
// Vote power cache for past blocks when vote power was not initialized.
// Reading vote power at that block would return incorrect result, so cache must be set by some other means.
// No need for revocation info, since there can be no delegations at such block.
mapping (bytes32 => uint256) private uninitializedVotePowerCache;
string constant private ALREADY_EXPLICIT_MSG = "Already delegated explicitly";
string constant private ALREADY_PERCENT_MSG = "Already delegated by percentage";
string constant internal VOTE_POWER_NOT_INITIALIZED = "Vote power not initialized";
/**
* All external methods in VPContract can only be executed by the owner token.
*/
modifier onlyOwnerToken {
require(msg.sender == address(ownerToken), "only owner token");
_;
}
/**
* Setting cleaner contract is allowed from
* owner token or owner token's governance (when VPContract is detached,
* methods can no longer be called via the owner token, but the VPContract
* still remembers the old owner token and can see its governance).
*/
modifier onlyOwnerOrGovernance {
require(msg.sender == address(ownerToken) ||
msg.sender == GovernedBase(address(ownerToken)).governance(),
"only owner or governance");
_;
}
modifier onlyPercent(address sender) {
// If a delegate cannot be added by percentage, revert.
require(_canDelegateByPct(sender), ALREADY_EXPLICIT_MSG);
_;
}
modifier onlyExplicit(address sender) {
// If a delegate cannot be added by explicit amount, revert.
require(_canDelegateByAmount(sender), ALREADY_PERCENT_MSG);
_;
}
/**
* Construct VPContract for given VPToken.
*/
constructor(IVPToken _ownerToken, bool _isReplacement) {
require(address(_ownerToken) != address(0), "VPContract must belong to a VPToken");
ownerToken = _ownerToken;
isReplacement = _isReplacement;
}
/**
* Set the cleanup block number.
* Historic data for the blocks before `cleanupBlockNumber` can be erased,
* history before that block should never be used since it can be inconsistent.
* In particular, cleanup block number must be before current vote power block.
* The method can be called by the owner token, its governance or cleanupBlockNumberManager.
* @param _blockNumber The new cleanup block number.
*/
function setCleanupBlockNumber(uint256 _blockNumber) external override {
require(msg.sender == address(ownerToken) ||
msg.sender == cleanupBlockNumberManager ||
msg.sender == GovernedBase(address(ownerToken)).governance(),
"only owner, governance or cleanup block manager");
_setCleanupBlockNumber(_blockNumber);
}
/**
* Set the contract that is allowed to set cleanupBlockNumber.
* Usually this will be an instance of CleanupBlockNumberManager.
* Only to be set when detached from the owner token and directly attached to cleanup block number manager.
*/
function setCleanupBlockNumberManager(address _cbnManager) external override onlyOwnerOrGovernance {
cleanupBlockNumberManager = _cbnManager;
}
/**
* Set the contract that is allowed to call history cleaning methods.
* The method can be called by the owner token or its governance.
*/
function setCleanerContract(address _cleanerContract) external override onlyOwnerOrGovernance {
_setCleanerContract(_cleanerContract);
}
/**
* Update vote powers when tokens are transfered.
* Also update delegated vote powers for percentage delegation
* and check for enough funds for explicit delegations.
**/
function updateAtTokenTransfer(
address _from,
address _to,
uint256 _fromBalance,
uint256 _toBalance,
uint256 _amount
)
external override
onlyOwnerToken
{
if (_from == address(0)) {
// mint new vote power
_initializeVotePower(_to, _toBalance);
_mintVotePower(_to, _toBalance, _amount);
} else if (_to == address(0)) {
// burn vote power
_initializeVotePower(_from, _fromBalance);
_burnVotePower(_from, _fromBalance, _amount);
} else {
// transmit vote power _to receiver
_initializeVotePower(_from, _fromBalance);
_initializeVotePower(_to, _toBalance);
_transmitVotePower(_from, _to, _fromBalance, _toBalance, _amount);
}
}
/**
* @notice Delegate `_bips` percentage of voting power to `_to` from `_from`
* @param _from The address of the delegator
* @param _to The address of the recipient
* @param _balance The delegator's current balance
* @param _bips The percentage of voting power to be delegated expressed in basis points (1/100 of one percent).
* Not cummulative - every call resets the delegation value (and value of 0 revokes delegation).
**/
function delegate(
address _from,
address _to,
uint256 _balance,
uint256 _bips
)
external override
onlyOwnerToken
onlyPercent(_from)
{
_initializeVotePower(_from, _balance);
if (!_votePowerInitialized(_to)) {
_initializeVotePower(_to, ownerToken.balanceOf(_to));
}
_delegateByPercentage(_from, _to, _balance, _bips);
}
/**
* @notice Explicitly delegate `_amount` of voting power to `_to` from `_from`.
* @param _from The address of the delegator
* @param _to The address of the recipient
* @param _balance The delegator's current balance
* @param _amount An explicit vote power amount to be delegated.
* Not cummulative - every call resets the delegation value (and value of 0 undelegates `to`).
**/
function delegateExplicit(
address _from,
address _to,
uint256 _balance,
uint _amount
)
external override
onlyOwnerToken
onlyExplicit(_from)
{
_initializeVotePower(_from, _balance);
if (!_votePowerInitialized(_to)) {
_initializeVotePower(_to, ownerToken.balanceOf(_to));
}
_delegateByAmount(_from, _to, _balance, _amount);
}
/**
* @notice Revoke all delegation from `_from` to `_to` at given block.
* Only affects the reads via `votePowerOfAtCached()` in the block `_blockNumber`.
* Block `_blockNumber` must be in the past.
* This method should be used only to prevent rogue delegate voting in the current voting block.
* To stop delegating use delegate/delegateExplicit with value of 0 or undelegateAll/undelegateAllExplicit.
* @param _from The address of the delegator
* @param _to Address of the delegatee
* @param _balance The delegator's current balance
* @param _blockNumber The block number at which to revoke delegation.
**/
function revokeDelegationAt(
address _from,
address _to,
uint256 _balance,
uint _blockNumber
)
external override
onlyOwnerToken
{
// ASSERT: if there was a delegation, _from and _to must be initialized
if (!isReplacement ||
(_votePowerInitializedAt(_from, _blockNumber) && _votePowerInitializedAt(_to, _blockNumber))) {
_revokeDelegationAt(_from, _to, _balance, _blockNumber);
}
}
/**
* @notice Undelegate all voting power for delegates of `_from`
* Can only be used with percentage delegation.
* Does not reset delegation mode back to NOTSET.
* @param _from The address of the delegator
**/
function undelegateAll(
address _from,
uint256 _balance
)
external override
onlyOwnerToken
onlyPercent(_from)
{
if (_hasAnyDelegations(_from)) {
// ASSERT: since there were delegations, _from and its targets must be initialized
_undelegateAllByPercentage(_from, _balance);
}
}
/**
* @notice Undelegate all explicit vote power by amount delegates for `_from`.
* Can only be used with explicit delegation.
* Does not reset delegation mode back to NOTSET.
* @param _from The address of the delegator
* @param _delegateAddresses Explicit delegation does not store delegatees' addresses,
* so the caller must supply them.
* @return The amount still delegated (in case the list of delegates was incomplete).
*/
function undelegateAllExplicit(
address _from,
address[] memory _delegateAddresses
)
external override
onlyOwnerToken
onlyExplicit(_from)
returns (uint256)
{
if (_hasAnyDelegations(_from)) {
// ASSERT: since there were delegations, _from and its targets must be initialized
return _undelegateAllByAmount(_from, _delegateAddresses);
}
return 0;
}
/**
* @notice Get the vote power of `_who` at block `_blockNumber`
* Reads/updates cache and upholds revocations.
* @param _who The address to get voting power.
* @param _blockNumber The block number at which to fetch.
* @return Vote power of `_who` at `_blockNumber`.
*/
function votePowerOfAtCached(address _who, uint256 _blockNumber) external override returns(uint256) {
if (!isReplacement || _votePowerInitializedAt(_who, _blockNumber)) {
// use standard method
return _votePowerOfAtCached(_who, _blockNumber);
} else {
// use uninitialized vote power cache
bytes32 key = keccak256(abi.encode(_who, _blockNumber));
uint256 cached = uninitializedVotePowerCache[key];
if (cached != 0) {
return cached - 1; // safe, cached != 0
}
uint256 balance = ownerToken.balanceOfAt(_who, _blockNumber);
uninitializedVotePowerCache[key] = balance.add(1);
return balance;
}
}
/**
* Get the current cleanup block number.
*/
function cleanupBlockNumber() external view override returns (uint256) {
return _cleanupBlockNumber();
}
/**
* @notice Get the current vote power of `_who`.
* @param _who The address to get voting power.
* @return Current vote power of `_who`.
*/
function votePowerOf(address _who) external view override returns(uint256) {
if (_votePowerInitialized(_who)) {
return _votePowerOf(_who);
} else {
return ownerToken.balanceOf(_who);
}
}
/**
* @notice Get the vote power of `_who` at block `_blockNumber`
* @param _who The address to get voting power.
* @param _blockNumber The block number at which to fetch.
* @return Vote power of `_who` at `_blockNumber`.
*/
function votePowerOfAt(address _who, uint256 _blockNumber) public view override returns(uint256) {
if (!isReplacement || _votePowerInitializedAt(_who, _blockNumber)) {
return _votePowerOfAt(_who, _blockNumber);
} else {
return ownerToken.balanceOfAt(_who, _blockNumber);
}
}
/**
* Return vote powers for several addresses in a batch.
* @param _owners The list of addresses to fetch vote power of.
* @param _blockNumber The block number at which to fetch.
* @return _votePowers A list of vote powers corresponding to _owners.
*/
function batchVotePowerOfAt(
address[] memory _owners,
uint256 _blockNumber
)
external view override
returns(uint256[] memory _votePowers)
{
_votePowers = _batchVotePowerOfAt(_owners, _blockNumber);
// zero results might not have been initialized
if (isReplacement) {
for (uint256 i = 0; i < _votePowers.length; i++) {
if (_votePowers[i] == 0 && !_votePowerInitializedAt(_owners[i], _blockNumber)) {
_votePowers[i] = ownerToken.balanceOfAt(_owners[i], _blockNumber);
}
}
}
}
/**
* @notice Get current delegated vote power `_from` delegator delegated `_to` delegatee.
* @param _from Address of delegator
* @param _to Address of delegatee
* @param _balance The delegator's current balance
* @return The delegated vote power.
*/
function votePowerFromTo(
address _from,
address _to,
uint256 _balance
)
external view override
returns (uint256)
{
// ASSERT: if the result is nonzero, _from and _to are initialized
return _votePowerFromTo(_from, _to, _balance);
}
/**
* @notice Get delegated the vote power `_from` delegator delegated `_to` delegatee at `_blockNumber`.
* @param _from Address of delegator
* @param _to Address of delegatee
* @param _balance The delegator's current balance
* @param _blockNumber The block number at which to fetch.
* @return The delegated vote power.
*/
function votePowerFromToAt(
address _from,
address _to,
uint256 _balance,
uint _blockNumber
)
external view override
returns (uint256)
{
// ASSERT: if the result is nonzero, _from and _to were initialized at _blockNumber
return _votePowerFromToAt(_from, _to, _balance, _blockNumber);
}
/**
* @notice Get the delegation mode for '_who'. This mode determines whether vote power is
* allocated by percentage or by explicit value.
* @param _who The address to get delegation mode.
* @return Delegation mode (NOTSET=0, PERCENTAGE=1, AMOUNT=2))
*/
function delegationModeOf(address _who) external view override returns (uint256) {
return uint256(_delegationModeOf(_who));
}
/**
* @notice Compute the current undelegated vote power of `_owner`
* @param _owner The address to get undelegated voting power.
* @param _balance Owner's current balance
* @return The unallocated vote power of `_owner`
*/
function undelegatedVotePowerOf(
address _owner,
uint256 _balance
)
external view override
returns (uint256)
{
if (_votePowerInitialized(_owner)) {
return _undelegatedVotePowerOf(_owner, _balance);
} else {
// ASSERT: there are no delegations
return _balance;
}
}
/**
* @notice Get the undelegated vote power of `_owner` at given block.
* @param _owner The address to get undelegated voting power.
* @param _blockNumber The block number at which to fetch.
* @return The undelegated vote power of `_owner` (= owner's own balance minus all delegations from owner)
*/
function undelegatedVotePowerOfAt(
address _owner,
uint256 _balance,
uint256 _blockNumber
)
external view override
returns (uint256)
{
if (_votePowerInitialized(_owner)) {
return _undelegatedVotePowerOfAt(_owner, _balance, _blockNumber);
} else {
// ASSERT: there were no delegations at _blockNumber
return _balance;
}
}
/**
* @notice Get the vote power delegation `_delegateAddresses`
* and `pcts` of an `_owner`. Returned in two separate positional arrays.
* Also returns the count of delegates and delegation mode.
* @param _owner The address to get delegations.
* @return _delegateAddresses Positional array of delegation addresses.
* @return _bips Positional array of delegation percents specified in basis points (1/100 or 1 percent)
* @return _count The number of delegates.
* @return _delegationMode The mode of the delegation (NOTSET=0, PERCENTAGE=1, AMOUNT=2).
*/
function delegatesOf(address _owner)
external view override
returns (
address[] memory _delegateAddresses,
uint256[] memory _bips,
uint256 _count,
uint256 _delegationMode
)
{
// ASSERT: either _owner is initialized or there are no delegations
return delegatesOfAt(_owner, block.number);
}
/**
* @notice Get the vote power delegation `delegationAddresses`
* and `_bips` of an `_owner`. Returned in two separate positional arrays.
* Also returns the count of delegates and delegation mode.
* @param _owner The address to get delegations.
* @param _blockNumber The block for which we want to know the delegations.
* @return _delegateAddresses Positional array of delegation addresses.
* @return _bips Positional array of delegation percents specified in basis points (1/100 or 1 percent)
* @return _count The number of delegates.
* @return _delegationMode The mode of the delegation (NOTSET=0, PERCENTAGE=1, AMOUNT=2).
*/
function delegatesOfAt(
address _owner,
uint256 _blockNumber
)
public view override
returns (
address[] memory _delegateAddresses,
uint256[] memory _bips,
uint256 _count,
uint256 _delegationMode
)
{
// ASSERT: either _owner was initialized or there were no delegations
DelegationMode mode = _delegationModeOf(_owner);
if (mode == DelegationMode.PERCENTAGE) {
// Get the vote power delegation for the _owner
(_delegateAddresses, _bips) = _percentageDelegatesOfAt(_owner, _blockNumber);
} else if (mode == DelegationMode.NOTSET) {
_delegateAddresses = new address[](0);
_bips = new uint256[](0);
} else {
revert ("delegatesOf does not work in AMOUNT delegation mode");
}
_count = _delegateAddresses.length;
_delegationMode = uint256(mode);
}
/**
* Initialize vote power to current balance if not initialized already.
* @param _owner The address to initialize voting power.
* @param _balance The owner's current balance.
*/
function _initializeVotePower(address _owner, uint256 _balance) internal {
if (!isReplacement) return;
if (votePowerInitializationBlock[_owner] == 0) {
// consistency check - no delegations should be made from or to owner before vote power is initialized
// (that would be dangerous, because vote power would have been delegated incorrectly)
assert(_votePowerOf(_owner) == 0 && !_hasAnyDelegations(_owner));
_mintVotePower(_owner, 0, _balance);
votePowerInitializationBlock[_owner] = block.number.add(1);
}
}
/**
* Has the vote power of `_owner` been initialized?
* @param _owner The address to check.
* @return true if vote power of _owner is initialized
*/
function _votePowerInitialized(address _owner) internal view returns (bool) {
if (!isReplacement) return true;
return votePowerInitializationBlock[_owner] != 0;
}
/**
* Was vote power of `_owner` initialized at some block?
* @param _owner The address to check.
* @param _blockNumber The block for which we want to check.
* @return true if vote power of _owner was initialized at _blockNumber
*/
function _votePowerInitializedAt(address _owner, uint256 _blockNumber) internal view returns (bool) {
if (!isReplacement) return true;
uint256 initblock = votePowerInitializationBlock[_owner];
return initblock != 0 && initblock - 1 <= _blockNumber;
}
}
// File contracts/userInterfaces/IWNat.sol
//
pragma solidity 0.7.6;
interface IWNat {
/**
* @notice Deposit native token and mint WNAT ERC20.
*/
function deposit() external payable;
/**
* @notice Withdraw native token and burn WNAT ERC20.
* @param _amount The amount to withdraw.
*/
function withdraw(uint256 _amount) external;
/**
* @notice Deposit native token from msg.sender and mint WNAT ERC20.
* @param _recipient An address to receive minted WNAT.
*/
function depositTo(address _recipient) external payable;
/**
* @notice Withdraw WNAT from an owner and send NAT to msg.sender given an allowance.
* @param _owner An address spending the native tokens.
* @param _amount The amount to spend.
*
* Requirements:
*
* - `_owner` must have a balance of at least `_amount`.
* - the caller must have allowance for `_owners`'s tokens of at least
* `_amount`.
*/
function withdrawFrom(address _owner, uint256 _amount) external;
}
// File contracts/token/implementation/WNat.sol
//
pragma solidity 0.7.6;
/**
* @title Wrapped Native token
* @notice Accept native token deposits and mint ERC20 WNAT (wrapped native) tokens 1-1.
* @dev Attribution: https://rinkeby.etherscan.io/address/0xc778417e063141139fce010982780140aa0cd5ab#code
*/
contract WNat is VPToken, IWNat {
using SafeMath for uint256;
event Deposit(address indexed dst, uint amount);
event Withdrawal(address indexed src, uint amount);
/**
* Construct an ERC20 token.
*/
constructor(address _governance, string memory _name, string memory _symbol)
VPToken(_governance, _name, _symbol)
{
}
receive() external payable {
deposit();
}
/**
* @notice Withdraw WNAT from an owner and send native tokens to msg.sender given an allowance.
* @param owner An address spending the Native tokens.
* @param amount The amount to spend.
*
* Requirements:
*
* - `owner` must have a balance of at least `amount`.
* - the caller must have allowance for `owners`'s tokens of at least
* `amount`.
*/
function withdrawFrom(address owner, uint256 amount) external override {
// Reduce senders allowance
_approve(owner, msg.sender, allowance(owner, msg.sender).sub(amount, "allowance below zero"));
// Burn the owners balance
_burn(owner, amount);
// Emit withdraw event
emit Withdrawal(owner, amount);
// Move value to sender (last statement, to prevent reentrancy)
msg.sender.transfer(amount);
}
/**
* @notice Deposit Native from msg.sender and mints WNAT ERC20 to recipient address.
* @param recipient An address to receive minted WNAT.
*/
function depositTo(address recipient) external payable override {
require(recipient != address(0), "Cannot deposit to zero address");
// Mint WNAT
_mint(recipient, msg.value);
// Emit deposit event
emit Deposit(recipient, msg.value);
}
/**
* @notice Deposit Native and mint wNat ERC20.
*/
function deposit() public payable override {
// Mint WNAT
_mint(msg.sender, msg.value);
// Emit deposit event
emit Deposit(msg.sender, msg.value);
}
/**
* @notice Withdraw Native and burn WNAT ERC20.
* @param amount The amount to withdraw.
*/
function withdraw(uint256 amount) external override {
// Burn WNAT tokens
_burn(msg.sender, amount);
// Emit withdrawal event
emit Withdrawal(msg.sender, amount);
// Send Native to sender (last statement, to prevent reentrancy)
msg.sender.transfer(amount);
}
}

Contract ABI

[{"type":"constructor","stateMutability":"nonpayable","inputs":[{"type":"address","name":"_governance","internalType":"address"},{"type":"string","name":"_name","internalType":"string"},{"type":"string","name":"_symbol","internalType":"string"}]},{"type":"event","name":"Approval","inputs":[{"type":"address","name":"owner","internalType":"address","indexed":true},{"type":"address","name":"spender","internalType":"address","indexed":true},{"type":"uint256","name":"value","internalType":"uint256","indexed":false}],"anonymous":false},{"type":"event","name":"CreatedTotalSupplyCache","inputs":[{"type":"uint256","name":"_blockNumber","internalType":"uint256","indexed":false}],"anonymous":false},{"type":"event","name":"Deposit","inputs":[{"type":"address","name":"dst","internalType":"address","indexed":true},{"type":"uint256","name":"amount","internalType":"uint256","indexed":false}],"anonymous":false},{"type":"event","name":"GovernanceProposed","inputs":[{"type":"address","name":"proposedGovernance","internalType":"address","indexed":false}],"anonymous":false},{"type":"event","name":"GovernanceUpdated","inputs":[{"type":"address","name":"oldGovernance","internalType":"address","indexed":false},{"type":"address","name":"newGoveranance","internalType":"address","indexed":false}],"anonymous":false},{"type":"event","name":"Transfer","inputs":[{"type":"address","name":"from","internalType":"address","indexed":true},{"type":"address","name":"to","internalType":"address","indexed":true},{"type":"uint256","name":"value","internalType":"uint256","indexed":false}],"anonymous":false},{"type":"event","name":"VotePowerContractChanged","inputs":[{"type":"uint256","name":"_contractType","internalType":"uint256","indexed":false},{"type":"address","name":"_oldContractAddress","internalType":"address","indexed":false},{"type":"address","name":"_newContractAddress","internalType":"address","indexed":false}],"anonymous":false},{"type":"event","name":"Withdrawal","inputs":[{"type":"address","name":"src","internalType":"address","indexed":true},{"type":"uint256","name":"amount","internalType":"uint256","indexed":false}],"anonymous":false},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"allowance","inputs":[{"type":"address","name":"owner","internalType":"address"},{"type":"address","name":"spender","internalType":"address"}]},{"type":"function","stateMutability":"nonpayable","outputs":[{"type":"bool","name":"","internalType":"bool"}],"name":"approve","inputs":[{"type":"address","name":"spender","internalType":"address"},{"type":"uint256","name":"amount","internalType":"uint256"}]},{"type":"function","stateMutability":"nonpayable","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"balanceHistoryCleanup","inputs":[{"type":"address","name":"_owner","internalType":"address"},{"type":"uint256","name":"_count","internalType":"uint256"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"balanceOf","inputs":[{"type":"address","name":"account","internalType":"address"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"balanceOfAt","inputs":[{"type":"address","name":"_owner","internalType":"address"},{"type":"uint256","name":"_blockNumber","internalType":"uint256"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256[]","name":"","internalType":"uint256[]"}],"name":"batchVotePowerOfAt","inputs":[{"type":"address[]","name":"_owners","internalType":"address[]"},{"type":"uint256","name":"_blockNumber","internalType":"uint256"}]},{"type":"function","stateMutability":"nonpayable","outputs":[],"name":"claimGovernance","inputs":[]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"cleanupBlockNumber","inputs":[]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint8","name":"","internalType":"uint8"}],"name":"decimals","inputs":[]},{"type":"function","stateMutability":"nonpayable","outputs":[{"type":"bool","name":"","internalType":"bool"}],"name":"decreaseAllowance","inputs":[{"type":"address","name":"spender","internalType":"address"},{"type":"uint256","name":"subtractedValue","internalType":"uint256"}]},{"type":"function","stateMutability":"nonpayable","outputs":[],"name":"delegate","inputs":[{"type":"address","name":"_to","internalType":"address"},{"type":"uint256","name":"_bips","internalType":"uint256"}]},{"type":"function","stateMutability":"nonpayable","outputs":[],"name":"delegateExplicit","inputs":[{"type":"address","name":"_to","internalType":"address"},{"type":"uint256","name":"_amount","internalType":"uint256"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"address[]","name":"_delegateAddresses","internalType":"address[]"},{"type":"uint256[]","name":"_bips","internalType":"uint256[]"},{"type":"uint256","name":"_count","internalType":"uint256"},{"type":"uint256","name":"_delegationMode","internalType":"uint256"}],"name":"delegatesOf","inputs":[{"type":"address","name":"_owner","internalType":"address"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"address[]","name":"_delegateAddresses","internalType":"address[]"},{"type":"uint256[]","name":"_bips","internalType":"uint256[]"},{"type":"uint256","name":"_count","internalType":"uint256"},{"type":"uint256","name":"_delegationMode","internalType":"uint256"}],"name":"delegatesOfAt","inputs":[{"type":"address","name":"_owner","internalType":"address"},{"type":"uint256","name":"_blockNumber","internalType":"uint256"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"delegationModeOf","inputs":[{"type":"address","name":"_who","internalType":"address"}]},{"type":"function","stateMutability":"payable","outputs":[],"name":"deposit","inputs":[]},{"type":"function","stateMutability":"payable","outputs":[],"name":"depositTo","inputs":[{"type":"address","name":"recipient","internalType":"address"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"address","name":"","internalType":"address"}],"name":"governance","inputs":[]},{"type":"function","stateMutability":"view","outputs":[{"type":"address","name":"","internalType":"contract IGovernanceVotePower"}],"name":"governanceVotePower","inputs":[]},{"type":"function","stateMutability":"nonpayable","outputs":[{"type":"bool","name":"","internalType":"bool"}],"name":"increaseAllowance","inputs":[{"type":"address","name":"spender","internalType":"address"},{"type":"uint256","name":"addedValue","internalType":"uint256"}]},{"type":"function","stateMutability":"nonpayable","outputs":[],"name":"initialise","inputs":[{"type":"address","name":"_governance","internalType":"address"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"string","name":"","internalType":"string"}],"name":"name","inputs":[]},{"type":"function","stateMutability":"view","outputs":[{"type":"bool","name":"","internalType":"bool"}],"name":"needsReplacementVPContract","inputs":[]},{"type":"function","stateMutability":"nonpayable","outputs":[],"name":"proposeGovernance","inputs":[{"type":"address","name":"_governance","internalType":"address"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"address","name":"","internalType":"address"}],"name":"proposedGovernance","inputs":[]},{"type":"function","stateMutability":"view","outputs":[{"type":"address","name":"","internalType":"contract IVPContractEvents"}],"name":"readVotePowerContract","inputs":[]},{"type":"function","stateMutability":"nonpayable","outputs":[],"name":"revokeDelegationAt","inputs":[{"type":"address","name":"_who","internalType":"address"},{"type":"uint256","name":"_blockNumber","internalType":"uint256"}]},{"type":"function","stateMutability":"nonpayable","outputs":[],"name":"setCleanerContract","inputs":[{"type":"address","name":"_cleanerContract","internalType":"address"}]},{"type":"function","stateMutability":"nonpayable","outputs":[],"name":"setCleanupBlockNumber","inputs":[{"type":"uint256","name":"_blockNumber","internalType":"uint256"}]},{"type":"function","stateMutability":"nonpayable","outputs":[],"name":"setCleanupBlockNumberManager","inputs":[{"type":"address","name":"_cleanupBlockNumberManager","internalType":"address"}]},{"type":"function","stateMutability":"nonpayable","outputs":[],"name":"setGovernanceVotePower","inputs":[{"type":"address","name":"_governanceVotePower","internalType":"contract IIGovernanceVotePower"}]},{"type":"function","stateMutability":"nonpayable","outputs":[],"name":"setReadVpContract","inputs":[{"type":"address","name":"_vpContract","internalType":"contract IIVPContract"}]},{"type":"function","stateMutability":"nonpayable","outputs":[],"name":"setWriteVpContract","inputs":[{"type":"address","name":"_vpContract","internalType":"contract IIVPContract"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"string","name":"","internalType":"string"}],"name":"symbol","inputs":[]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"totalSupply","inputs":[]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"totalSupplyAt","inputs":[{"type":"uint256","name":"_blockNumber","internalType":"uint256"}]},{"type":"function","stateMutability":"nonpayable","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"totalSupplyCacheCleanup","inputs":[{"type":"uint256","name":"_blockNumber","internalType":"uint256"}]},{"type":"function","stateMutability":"nonpayable","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"totalSupplyHistoryCleanup","inputs":[{"type":"uint256","name":"_count","internalType":"uint256"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"totalVotePower","inputs":[]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"totalVotePowerAt","inputs":[{"type":"uint256","name":"_blockNumber","internalType":"uint256"}]},{"type":"function","stateMutability":"nonpayable","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"totalVotePowerAtCached","inputs":[{"type":"uint256","name":"_blockNumber","internalType":"uint256"}]},{"type":"function","stateMutability":"nonpayable","outputs":[{"type":"bool","name":"","internalType":"bool"}],"name":"transfer","inputs":[{"type":"address","name":"recipient","internalType":"address"},{"type":"uint256","name":"amount","internalType":"uint256"}]},{"type":"function","stateMutability":"nonpayable","outputs":[{"type":"bool","name":"","internalType":"bool"}],"name":"transferFrom","inputs":[{"type":"address","name":"sender","internalType":"address"},{"type":"address","name":"recipient","internalType":"address"},{"type":"uint256","name":"amount","internalType":"uint256"}]},{"type":"function","stateMutability":"nonpayable","outputs":[],"name":"transferGovernance","inputs":[{"type":"address","name":"_governance","internalType":"address"}]},{"type":"function","stateMutability":"nonpayable","outputs":[],"name":"undelegateAll","inputs":[]},{"type":"function","stateMutability":"nonpayable","outputs":[{"type":"uint256","name":"_remainingDelegation","internalType":"uint256"}],"name":"undelegateAllExplicit","inputs":[{"type":"address[]","name":"_delegateAddresses","internalType":"address[]"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"undelegatedVotePowerOf","inputs":[{"type":"address","name":"_owner","internalType":"address"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"undelegatedVotePowerOfAt","inputs":[{"type":"address","name":"_owner","internalType":"address"},{"type":"uint256","name":"_blockNumber","internalType":"uint256"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"votePowerFromTo","inputs":[{"type":"address","name":"_from","internalType":"address"},{"type":"address","name":"_to","internalType":"address"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"votePowerFromToAt","inputs":[{"type":"address","name":"_from","internalType":"address"},{"type":"address","name":"_to","internalType":"address"},{"type":"uint256","name":"_blockNumber","internalType":"uint256"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"votePowerOf","inputs":[{"type":"address","name":"_owner","internalType":"address"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"votePowerOfAt","inputs":[{"type":"address","name":"_owner","internalType":"address"},{"type":"uint256","name":"_blockNumber","internalType":"uint256"}]},{"type":"function","stateMutability":"nonpayable","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"votePowerOfAtCached","inputs":[{"type":"address","name":"_owner","internalType":"address"},{"type":"uint256","name":"_blockNumber","internalType":"uint256"}]},{"type":"function","stateMutability":"nonpayable","outputs":[],"name":"withdraw","inputs":[{"type":"uint256","name":"amount","internalType":"uint256"}]},{"type":"function","stateMutability":"nonpayable","outputs":[],"name":"withdrawFrom","inputs":[{"type":"address","name":"owner","internalType":"address"},{"type":"uint256","name":"amount","internalType":"uint256"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"address","name":"","internalType":"contract IVPContractEvents"}],"name":"writeVotePowerContract","inputs":[]},{"type":"receive","stateMutability":"payable"}]
            

Deployed ByteCode

0x60806040526004361061036f5760003560e01c806395d89b41116101c6578063d0e30db0116100f7578063e64767aa11610095578063f5f3d4f71161006f578063f5f3d4f714610f2b578063f62f8f3a14610f40578063f683776714610f6a578063f6a494af14610f9d5761037e565b8063e64767aa14610e76578063ed475a7914610eb9578063f0e292c914610ef25761037e565b8063d6aa0b77116100d1578063d6aa0b7714610dba578063dd62ed3e14610ded578063deea13e714610e28578063e587497e14610e3d5761037e565b8063d0e30db014610d6a578063d38bfff414610d72578063d582cef414610da55761037e565b8063b302f39311610164578063be0ca7471161013e578063be0ca74714610c99578063c373a08e14610cd4578063caeb942b14610d07578063d06dc3ad14610d315761037e565b8063b302f39314610c25578063b760faf914610c3a578063bbd6fbf814610c605761037e565b80639ca2231a116101a05780639ca2231a14610b4d5780639d6a890f14610b80578063a457c2d714610bb3578063a9059cbb14610bec5761037e565b806395d89b4114610af9578063981b24d014610b0e5780639b3baa0e14610b385761037e565b806349e3c7e5116102a0578063755d10a41161023e57806383035a821161021857806383035a8214610a395780638c2b8ae114610a7257806392bfe6d814610a875780639470b0bd14610ac05761037e565b8063755d10a4146108f95780637de5b8ed1461092c5780637f4fcaa914610a065761037e565b80635d36b1901161027a5780635d36b190146107ee5780635d6d11eb1461080357806360f7ac97146108b157806370a08231146108c65761037e565b806349e3c7e5146106a05780634ee2cd7e146107a05780635aa6e675146107d95761037e565b806323b872dd1161030d57806331d12a16116102e757806331d12a16146105e057806339509351146106135780633e5aa26a1461064c57806343ea370b146106765761037e565b806323b872dd146105485780632e1a7d4d1461058b578063313ce567146105b55761037e565b806313de97f51161034957806313de97f514610493578063142d1018146104bd57806318160ddd146105025780631fec092a146105175761037e565b8063026e402b1461038357806306fdde03146103bc578063095ea7b3146104465761037e565b3661037e5761037c610fd0565b005b600080fd5b34801561038f57600080fd5b5061037c600480360360408110156103a657600080fd5b506001600160a01b038135169060200135611012565b3480156103c857600080fd5b506103d16110ad565b6040805160208082528351818301528351919283929083019185019080838360005b8381101561040b5781810151838201526020016103f3565b50505050905090810190601f1680156104385780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561045257600080fd5b5061047f6004803603604081101561046957600080fd5b506001600160a01b0381351690602001356110bc565b604080519115158252519081900360200190f35b34801561049f57600080fd5b5061037c600480360360208110156104b657600080fd5b50356110da565b3480156104c957600080fd5b506104f0600480360360208110156104e057600080fd5b50356001600160a01b03166112de565b60408051918252519081900360200190f35b34801561050e57600080fd5b506104f0611366565b34801561052357600080fd5b5061052c61136c565b604080516001600160a01b039092168252519081900360200190f35b34801561055457600080fd5b5061047f6004803603606081101561056b57600080fd5b506001600160a01b0381358116916020810135909116906040013561137b565b34801561059757600080fd5b5061037c600480360360208110156105ae57600080fd5b5035611403565b3480156105c157600080fd5b506105ca611474565b6040805160ff9092168252519081900360200190f35b3480156105ec57600080fd5b5061037c6004803603602081101561060357600080fd5b50356001600160a01b031661147e565b34801561061f57600080fd5b5061047f6004803603604081101561063657600080fd5b506001600160a01b038135169060200135611667565b34801561065857600080fd5b506104f06004803603602081101561066f57600080fd5b50356116b5565b34801561068257600080fd5b506104f06004803603602081101561069957600080fd5b50356116c0565b3480156106ac57600080fd5b50610750600480360360408110156106c357600080fd5b810190602081018135600160201b8111156106dd57600080fd5b8201836020820111156106ef57600080fd5b803590602001918460208302840111600160201b8311171561071057600080fd5b919080806020026020016040519081016040528093929190818152602001838360200280828437600092019190915250929550509135925061177b915050565b60408051602080825283518183015283519192839290830191858101910280838360005b8381101561078c578181015183820152602001610774565b505050509050019250505060405180910390f35b3480156107ac57600080fd5b506104f0600480360360408110156107c357600080fd5b506001600160a01b0381351690602001356118dd565b3480156107e557600080fd5b5061052c6118e9565b3480156107fa57600080fd5b5061037c6118f8565b34801561080f57600080fd5b506104f06004803603602081101561082657600080fd5b810190602081018135600160201b81111561084057600080fd5b82018360208201111561085257600080fd5b803590602001918460208302840111600160201b8311171561087357600080fd5b9190808060200260200160405190810160405280939291908181526020018383602002808284376000920191909152509295506119ba945050505050565b3480156108bd57600080fd5b5061052c611a6c565b3480156108d257600080fd5b506104f0600480360360208110156108e957600080fd5b50356001600160a01b0316611a7b565b34801561090557600080fd5b5061037c6004803603602081101561091c57600080fd5b50356001600160a01b0316611a96565b34801561093857600080fd5b5061095f6004803603602081101561094f57600080fd5b50356001600160a01b0316611d47565b604051808060200180602001858152602001848152602001838103835287818151815260200191508051906020019060200280838360005b838110156109af578181015183820152602001610997565b50505050905001838103825286818151815260200191508051906020019060200280838360005b838110156109ee5781810151838201526020016109d6565b50505050905001965050505050505060405180910390f35b348015610a1257600080fd5b5061037c60048036036020811015610a2957600080fd5b50356001600160a01b0316611f0a565b348015610a4557600080fd5b506104f060048036036040811015610a5c57600080fd5b506001600160a01b038135169060200135611f7d565b348015610a7e57600080fd5b5061052c61201f565b348015610a9357600080fd5b506104f060048036036040811015610aaa57600080fd5b506001600160a01b03813516906020013561202e565b348015610acc57600080fd5b5061037c60048036036040811015610ae357600080fd5b506001600160a01b03813516906020013561208c565b348015610b0557600080fd5b506103d161214e565b348015610b1a57600080fd5b506104f060048036036020811015610b3157600080fd5b5035612158565b348015610b4457600080fd5b5061052c612163565b348015610b5957600080fd5b5061037c60048036036020811015610b7057600080fd5b50356001600160a01b0316612172565b348015610b8c57600080fd5b5061037c60048036036020811015610ba357600080fd5b50356001600160a01b03166122e6565b348015610bbf57600080fd5b5061047f60048036036040811015610bd657600080fd5b506001600160a01b0381351690602001356123c0565b348015610bf857600080fd5b5061047f60048036036040811015610c0f57600080fd5b506001600160a01b038135169060200135612428565b348015610c3157600080fd5b5061037c61243c565b61037c60048036036020811015610c5057600080fd5b50356001600160a01b03166124bc565b348015610c6c57600080fd5b5061037c60048036036040811015610c8357600080fd5b506001600160a01b038135169060200135612563565b348015610ca557600080fd5b506104f060048036036040811015610cbc57600080fd5b506001600160a01b03813581169160200135166126d0565b348015610ce057600080fd5b5061037c60048036036020811015610cf757600080fd5b50356001600160a01b0316612747565b348015610d1357600080fd5b506104f060048036036020811015610d2a57600080fd5b50356127ec565b348015610d3d57600080fd5b5061037c60048036036040811015610d5457600080fd5b506001600160a01b0381351690602001356127f7565b61037c610fd0565b348015610d7e57600080fd5b5061037c60048036036020811015610d9557600080fd5b50356001600160a01b0316612818565b348015610db157600080fd5b5061047f6128da565b348015610dc657600080fd5b506104f060048036036020811015610ddd57600080fd5b50356001600160a01b03166128ea565b348015610df957600080fd5b506104f060048036036040811015610e1057600080fd5b506001600160a01b0381358116916020013516612950565b348015610e3457600080fd5b506104f061297b565b348015610e4957600080fd5b506104f060048036036040811015610e6057600080fd5b506001600160a01b038135169060200135612985565b348015610e8257600080fd5b506104f060048036036060811015610e9957600080fd5b506001600160a01b038135811691602081013590911690604001356129f9565b348015610ec557600080fd5b5061095f60048036036040811015610edc57600080fd5b506001600160a01b038135169060200135612aad565b348015610efe57600080fd5b506104f060048036036040811015610f1557600080fd5b506001600160a01b038135169060200135612c79565b348015610f3757600080fd5b506104f0612ce6565b348015610f4c57600080fd5b506104f060048036036020811015610f6357600080fd5b5035612cf0565b348015610f7657600080fd5b506104f060048036036020811015610f8d57600080fd5b50356001600160a01b0316612d5b565b348015610fa957600080fd5b5061037c60048036036020811015610fc057600080fd5b50356001600160a01b0316612db1565b610fda3334612f79565b60408051348152905133917fe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c919081900360200190a2565b61101a613069565b6001600160a01b0316636230001a338461103333611a7b565b856040518563ffffffff1660e01b815260040180856001600160a01b03168152602001846001600160a01b03168152602001838152602001828152602001945050505050600060405180830381600087803b15801561109157600080fd5b505af11580156110a5573d6000803e3d6000fd5b505050505050565b60606110b76130ca565b905090565b60006110d06110c9613160565b8484613164565b5060015b92915050565b600c546001600160a01b03163314806110fd57506011546001600160a01b031633145b61114e576040805162461bcd60e51b815260206004820152601a60248201527f6f6e6c7920676f7665726e616e6365206f72206d616e61676572000000000000604482015290519081900360640190fd5b61115781613250565b600e546001600160a01b0316156111ce57600e54604080516313de97f560e01b81526004810184905290516001600160a01b03909216916313de97f59160248082019260009290919082900301818387803b1580156111b557600080fd5b505af11580156111c9573d6000803e3d6000fd5b505050505b600f546001600160a01b0316158015906111f95750600e54600f546001600160a01b03908116911614155b1561126457600f54604080516313de97f560e01b81526004810184905290516001600160a01b03909216916313de97f59160248082019260009290919082900301818387803b15801561124b57600080fd5b505af115801561125f573d6000803e3d6000fd5b505050505b6010546001600160a01b0316156112db57601054604080516313de97f560e01b81526004810184905290516001600160a01b03909216916313de97f59160248082019260009290919082900301818387803b1580156112c257600080fd5b505af11580156112d6573d6000803e3d6000fd5b505050505b50565b60006112e86132d4565b6001600160a01b031663142d1018836040518263ffffffff1660e01b815260040180826001600160a01b0316815260200191505060206040518083038186803b15801561133457600080fd5b505afa158015611348573d6000803e3d6000fd5b505050506040513d602081101561135e57600080fd5b505192915050565b60025490565b600f546001600160a01b031690565b6000611388848484613335565b6113f884611394613160565b6113f385604051806060016040528060288152602001614309602891396001600160a01b038a166000908152600160205260408120906113d2613160565b6001600160a01b031681526020810191909152604001600020549190613490565b613164565b5060015b9392505050565b61140d3382613527565b60408051828152905133917f7fcf532c15f0a6db0bd6d0e038bea71d30d808c7d98cb3bf7268a95bf5081b65919081900360200190a2604051339082156108fc029083906000818181858888f19350505050158015611470573d6000803e3d6000fd5b5050565b60006110b7613623565b600c546001600160a01b031633146114cf576040805162461bcd60e51b815260206004820152600f60248201526e6f6e6c7920676f7665726e616e636560881b604482015290519081900360640190fd5b6001600160a01b038116156115f857306001600160a01b0316816001600160a01b031663653718836040518163ffffffff1660e01b815260040160206040518083038186803b15801561152157600080fd5b505afa158015611535573d6000803e3d6000fd5b505050506040513d602081101561154b57600080fd5b50516001600160a01b0316146115925760405162461bcd60e51b815260040180806020018281038252602281526020018061442d6022913960400191505060405180910390fd5b806001600160a01b03166313de97f56115a961362c565b6040518263ffffffff1660e01b815260040180828152602001915050600060405180830381600087803b1580156115df57600080fd5b505af11580156115f3573d6000803e3d6000fd5b505050505b600e5460408051600081526001600160a01b03928316602082015291831682820152517fbec98a6c0f609cda6b9f23b95824ba6ac733cb57edd17d344f2f2796844007399181900360600190a1600e80546001600160a01b0319166001600160a01b0392909216919091179055565b60006110d0611674613160565b846113f38560016000611685613160565b6001600160a01b03908116825260208083019390935260409182016000908120918c168152925290205490613632565b60006110d482612158565b600b546000906001600160a01b0316331461171a576040805162461bcd60e51b815260206004820152601560248201527413db9b1e4818db19585b995c8818dbdb9d1c9858dd605a1b604482015290519081900360640190fd5b600a548210611770576040805162461bcd60e51b815260206004820152601e60248201527f4e6f20636c65616e757020616674657220636c65616e757020626c6f636b0000604482015290519081900360640190fd5b6110d460098361368c565b60606117856132d4565b6001600160a01b03166349e3c7e584846040518363ffffffff1660e01b81526004018080602001838152602001828103825284818151815260200191508051906020019060200280838360005b838110156117ea5781810151838201526020016117d2565b50505050905001935050505060006040518083038186803b15801561180e57600080fd5b505afa158015611822573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f19168201604052602081101561184b57600080fd5b8101908080516040519392919084600160201b82111561186a57600080fd5b90830190602082018581111561187f57600080fd5b82518660208202830111600160201b8211171561189b57600080fd5b82525081516020918201928201910280838360005b838110156118c85781810151838201526020016118b0565b50505050905001604052505050905092915050565b60006113fc83836136c0565b600c546001600160a01b031681565b600d546001600160a01b03163314611947576040805162461bcd60e51b815260206004820152600d60248201526c1b9bdd0818db185a5b585a5b9d609a1b604482015290519081900360640190fd5b600c54600d54604080516001600160a01b03938416815292909116602083015280517f434a2db650703b36c824e745330d6397cdaa9ee2cc891a4938ae853e1c50b68d9281900390910190a1600d8054600c80546001600160a01b03199081166001600160a01b03841617909155169055565b60006119c4613069565b6001600160a01b0316630f8b8af733846040518363ffffffff1660e01b815260040180836001600160a01b0316815260200180602001828103825283818151815260200191508051906020019060200280838360005b83811015611a32578181015183820152602001611a1a565b505050509050019350505050602060405180830381600087803b158015611a5857600080fd5b505af1158015611348573d6000803e3d6000fd5b600d546001600160a01b031681565b6001600160a01b031660009081526020819052604090205490565b600c546001600160a01b03163314611ae7576040805162461bcd60e51b815260206004820152600f60248201526e6f6e6c7920676f7665726e616e636560881b604482015290519081900360640190fd5b6001600160a01b03811615611cd857306001600160a01b0316816001600160a01b031663653718836040518163ffffffff1660e01b815260040160206040518083038186803b158015611b3957600080fd5b505afa158015611b4d573d6000803e3d6000fd5b505050506040513d6020811015611b6357600080fd5b50516001600160a01b031614611baa5760405162461bcd60e51b815260040180806020018281038252602281526020018061442d6022913960400191505060405180910390fd5b601154600160a01b900460ff161580611c245750806001600160a01b031663aa94d3f26040518163ffffffff1660e01b815260040160206040518083038186803b158015611bf757600080fd5b505afa158015611c0b573d6000803e3d6000fd5b505050506040513d6020811015611c2157600080fd5b50515b611c5f5760405162461bcd60e51b81526004018080602001828103825260298152602001806142e06029913960400191505060405180910390fd5b806001600160a01b03166313de97f5611c7661362c565b6040518263ffffffff1660e01b815260040180828152602001915050600060405180830381600087803b158015611cac57600080fd5b505af1158015611cc0573d6000803e3d6000fd5b50506011805460ff60a01b1916600160a01b17905550505b600f5460408051600181526001600160a01b03928316602082015291831682820152517fbec98a6c0f609cda6b9f23b95824ba6ac733cb57edd17d344f2f2796844007399181900360600190a1600f80546001600160a01b0319166001600160a01b0392909216919091179055565b606080600080611d556132d4565b6001600160a01b0316637de5b8ed866040518263ffffffff1660e01b815260040180826001600160a01b0316815260200191505060006040518083038186803b158015611da157600080fd5b505afa158015611db5573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526080811015611dde57600080fd5b8101908080516040519392919084600160201b821115611dfd57600080fd5b908301906020820185811115611e1257600080fd5b82518660208202830111600160201b82111715611e2e57600080fd5b82525081516020918201928201910280838360005b83811015611e5b578181015183820152602001611e43565b5050505090500160405260200180516040519392919084600160201b821115611e8357600080fd5b908301906020820185811115611e9857600080fd5b82518660208202830111600160201b82111715611eb457600080fd5b82525081516020918201928201910280838360005b83811015611ee1578181015183820152602001611ec9565b505050509190910160409081526020830151920151959b949a5090985093965091945050505050565b600c546001600160a01b03163314611f5b576040805162461bcd60e51b815260206004820152600f60248201526e6f6e6c7920676f7665726e616e636560881b604482015290519081900360640190fd5b601180546001600160a01b0319166001600160a01b0392909216919091179055565b6000611f876132d4565b6001600160a01b0316633150392784611fa086866118dd565b856040518463ffffffff1660e01b815260040180846001600160a01b03168152602001838152602001828152602001935050505060206040518083038186803b158015611fec57600080fd5b505afa158015612000573d6000803e3d6000fd5b505050506040513d602081101561201657600080fd5b50519392505050565b6010546001600160a01b031690565b60006120386132d4565b6001600160a01b03166392bfe6d884846040518363ffffffff1660e01b815260040180836001600160a01b031681526020018281526020019250505060206040518083038186803b158015611fec57600080fd5b6120d382336113f38460405180604001604052806014815260200173616c6c6f77616e63652062656c6f77207a65726f60601b8152506120cc8833612950565b9190613490565b6120dd8282613527565b6040805182815290516001600160a01b038416917f7fcf532c15f0a6db0bd6d0e038bea71d30d808c7d98cb3bf7268a95bf5081b65919081900360200190a2604051339082156108fc029083906000818181858888f19350505050158015612149573d6000803e3d6000fd5b505050565b60606110b7613718565b60006110d482613779565b600e546001600160a01b031690565b600c546001600160a01b031633146121c3576040805162461bcd60e51b815260206004820152600f60248201526e6f6e6c7920676f7665726e616e636560881b604482015290519081900360640190fd5b306001600160a01b0316816001600160a01b031663653718836040518163ffffffff1660e01b815260040160206040518083038186803b15801561220657600080fd5b505afa15801561221a573d6000803e3d6000fd5b505050506040513d602081101561223057600080fd5b50516001600160a01b0316146122775760405162461bcd60e51b815260040180806020018281038252603d815260200180614331603d913960400191505060405180910390fd5b60105460408051600281526001600160a01b03928316602082015291831682820152517fbec98a6c0f609cda6b9f23b95824ba6ac733cb57edd17d344f2f2796844007399181900360600190a1601080546001600160a01b0319166001600160a01b0392909216919091179055565b600d54600160a01b900460ff161561233c576040805162461bcd60e51b8152602060048201526014602482015273696e697469616c6973656420213d2066616c736560601b604482015290519081900360640190fd5b600d805460ff60a01b1916600160a01b179055600c54604080516001600160a01b039283168152918316602083015280517f434a2db650703b36c824e745330d6397cdaa9ee2cc891a4938ae853e1c50b68d9281900390910190a1600c80546001600160a01b039092166001600160a01b0319928316179055600d80549091169055565b60006110d06123cd613160565b846113f38560405180606001604052806025815260200161440860259139600160006123f7613160565b6001600160a01b03908116825260208083019390935260409182016000908120918d16815292529020549190613490565b60006110d0612435613160565b8484613335565b612444613069565b6001600160a01b03166305109ecf3361245c33611a7b565b6040518363ffffffff1660e01b815260040180836001600160a01b0316815260200182815260200192505050600060405180830381600087803b1580156124a257600080fd5b505af11580156124b6573d6000803e3d6000fd5b50505050565b6001600160a01b038116612517576040805162461bcd60e51b815260206004820152601e60248201527f43616e6e6f74206465706f73697420746f207a65726f20616464726573730000604482015290519081900360640190fd5b6125218134612f79565b6040805134815290516001600160a01b038316917fe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c919081900360200190a250565b600f54600e546001600160a01b039182169116811561260e57816001600160a01b031663c7c62fab338661259733886118dd565b876040518563ffffffff1660e01b815260040180856001600160a01b03168152602001846001600160a01b03168152602001838152602001828152602001945050505050600060405180830381600087803b1580156125f557600080fd5b505af1158015612609573d6000803e3d6000fd5b505050505b816001600160a01b0316816001600160a01b03161415801561263857506001600160a01b03811615155b156124b657806001600160a01b031663c7c62fab338661265833886118dd565b876040518563ffffffff1660e01b815260040180856001600160a01b03168152602001846001600160a01b03168152602001838152602001828152602001945050505050600060405180830381600087803b1580156126b657600080fd5b505af19250505080156126c7575060015b6124b6576124b6565b60006126da6132d4565b6001600160a01b0316639dc6b9f284846126f387611a7b565b6040518463ffffffff1660e01b815260040180846001600160a01b03168152602001836001600160a01b03168152602001828152602001935050505060206040518083038186803b158015611fec57600080fd5b600c546001600160a01b03163314612798576040805162461bcd60e51b815260206004820152600f60248201526e6f6e6c7920676f7665726e616e636560881b604482015290519081900360640190fd5b600d80546001600160a01b0383166001600160a01b0319909116811790915560408051918252517f1f95fb40be3a947982072902a887b521248d1d8931a39eb38f84f4d6fd758b699181900360200190a150565b60006110d4826137c8565b6127ff613069565b6001600160a01b031663404d9e82338461103333611a7b565b600c546001600160a01b03163314612869576040805162461bcd60e51b815260206004820152600f60248201526e6f6e6c7920676f7665726e616e636560881b604482015290519081900360640190fd5b600c54604080516001600160a01b039283168152918316602083015280517f434a2db650703b36c824e745330d6397cdaa9ee2cc891a4938ae853e1c50b68d9281900390910190a1600c80546001600160a01b039092166001600160a01b0319928316179055600d80549091169055565b601154600160a01b900460ff1681565b60006128f46132d4565b6001600160a01b0316634a03d5568361290c85611a7b565b6040518363ffffffff1660e01b815260040180836001600160a01b031681526020018281526020019250505060206040518083038186803b15801561133457600080fd5b6001600160a01b03918216600090815260016020908152604080832093909416825291909152205490565b60006110b761362c565b600061298f6132d4565b6001600160a01b031663e587497e84846040518363ffffffff1660e01b815260040180836001600160a01b0316815260200182815260200192505050602060405180830381600087803b1580156129e557600080fd5b505af1158015612000573d6000803e3d6000fd5b6000612a036132d4565b6001600160a01b031663833aca928585612a1d88876118dd565b866040518563ffffffff1660e01b815260040180856001600160a01b03168152602001846001600160a01b0316815260200183815260200182815260200194505050505060206040518083038186803b158015612a7957600080fd5b505afa158015612a8d573d6000803e3d6000fd5b505050506040513d6020811015612aa357600080fd5b5051949350505050565b606080600080612abb6132d4565b6001600160a01b031663ed475a7987876040518363ffffffff1660e01b815260040180836001600160a01b031681526020018281526020019250505060006040518083038186803b158015612b0f57600080fd5b505afa158015612b23573d6000803e3d6000fd5b505050506040513d6000823e601f3d908101601f191682016040526080811015612b4c57600080fd5b8101908080516040519392919084600160201b821115612b6b57600080fd5b908301906020820185811115612b8057600080fd5b82518660208202830111600160201b82111715612b9c57600080fd5b82525081516020918201928201910280838360005b83811015612bc9578181015183820152602001612bb1565b5050505090500160405260200180516040519392919084600160201b821115612bf157600080fd5b908301906020820185811115612c0657600080fd5b82518660208202830111600160201b82111715612c2257600080fd5b82525081516020918201928201910280838360005b83811015612c4f578181015183820152602001612c37565b505050509190910160409081526020830151920151959c949b509099509397509195505050505050565b600b546000906001600160a01b03163314612cd3576040805162461bcd60e51b815260206004820152601560248201527413db9b1e4818db19585b995c8818dbdb9d1c9858dd605a1b604482015290519081900360640190fd5b600a546113fc90600690859085906138b6565b60006110b7611366565b600b546000906001600160a01b03163314612d4a576040805162461bcd60e51b815260206004820152601560248201527413db9b1e4818db19585b995c8818dbdb9d1c9858dd605a1b604482015290519081900360640190fd5b600a546110d49060079084906138fc565b6000612d656132d4565b6001600160a01b031663f6837767836040518263ffffffff1660e01b815260040180826001600160a01b0316815260200191505060206040518083038186803b15801561133457600080fd5b600c546001600160a01b03163314612e02576040805162461bcd60e51b815260206004820152600f60248201526e6f6e6c7920676f7665726e616e636560881b604482015290519081900360640190fd5b612e0b816139c2565b600e546001600160a01b031615612e8357600e546040805163f6a494af60e01b81526001600160a01b0384811660048301529151919092169163f6a494af91602480830192600092919082900301818387803b158015612e6a57600080fd5b505af1158015612e7e573d6000803e3d6000fd5b505050505b600f546001600160a01b031615801590612eae5750600e54600f546001600160a01b03908116911614155b15612f1a57600f546040805163f6a494af60e01b81526001600160a01b0384811660048301529151919092169163f6a494af91602480830192600092919082900301818387803b158015612f0157600080fd5b505af1158015612f15573d6000803e3d6000fd5b505050505b6010546001600160a01b0316156112db576010546040805163f6a494af60e01b81526001600160a01b0384811660048301529151919092169163f6a494af91602480830192600092919082900301818387803b1580156112c257600080fd5b6001600160a01b038216612fd4576040805162461bcd60e51b815260206004820152601f60248201527f45524332303a206d696e7420746f20746865207a65726f206164647265737300604482015290519081900360640190fd5b612fe0600083836139e4565b600254612fed9082613632565b6002556001600160a01b0382166000908152602081905260409020546130139082613632565b6001600160a01b0383166000818152602081815260408083209490945583518581529351929391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9281900390910190a35050565b600f546000906001600160a01b0316806110b7576040805162461bcd60e51b815260206004820152601e60248201527f546f6b656e206d697373696e67207772697465205650436f6e74726163740000604482015290519081900360640190fd5b60038054604080516020601f60026000196101006001881615020190951694909404938401819004810282018101909252828152606093909290918301828280156131565780601f1061312b57610100808354040283529160200191613156565b820191906000526020600020905b81548152906001019060200180831161313957829003601f168201915b5050505050905090565b3390565b6001600160a01b0383166131a95760405162461bcd60e51b81526004018080602001828103825260248152602001806143b46024913960400191505060405180910390fd5b6001600160a01b0382166131ee5760405162461bcd60e51b81526004018080602001828103825260228152602001806142436022913960400191505060405180910390fd5b6001600160a01b03808416600081815260016020908152604080832094871680845294825291829020859055815185815291517f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259281900390910190a3505050565b600a548110156132915760405162461bcd60e51b815260040180806020018281038252602881526020018061428b6028913960400191505060405180910390fd5b4381106132cf5760405162461bcd60e51b81526004018080602001828103825260218152602001806142226021913960400191505060405180910390fd5b600a55565b600e546000906001600160a01b0316806110b7576040805162461bcd60e51b815260206004820152601d60248201527f546f6b656e206d697373696e672072656164205650436f6e7472616374000000604482015290519081900360640190fd5b6001600160a01b03831661337a5760405162461bcd60e51b815260040180806020018281038252602581526020018061438f6025913960400191505060405180910390fd5b6001600160a01b0382166133bf5760405162461bcd60e51b81526004018080602001828103825260238152602001806141dd6023913960400191505060405180910390fd5b6133ca8383836139e4565b61340781604051806060016040528060268152602001614265602691396001600160a01b0386166000908152602081905260409020549190613490565b6001600160a01b0380851660009081526020819052604080822093909355908416815220546134369082613632565b6001600160a01b038084166000818152602081815260409182902094909455805185815290519193928716927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92918290030190a3505050565b6000818484111561351f5760405162461bcd60e51b81526004018080602001828103825283818151815260200191508051906020019080838360005b838110156134e45781810151838201526020016134cc565b50505050905090810190601f1680156135115780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b505050900390565b6001600160a01b03821661356c5760405162461bcd60e51b815260040180806020018281038252602181526020018061436e6021913960400191505060405180910390fd5b613578826000836139e4565b6135b581604051806060016040528060228152602001614200602291396001600160a01b0385166000908152602081905260409020549190613490565b6001600160a01b0383166000908152602081905260409020556002546135db9082613bc9565b6002556040805182815290516000916001600160a01b038516917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9181900360200190a35050565b60055460ff1690565b600a5490565b6000828201838110156113fc576040805162461bcd60e51b815260206004820152601b60248201527f536166654d6174683a206164646974696f6e206f766572666c6f770000000000604482015290519081900360640190fd5b600081815260208390526040812054156136b7575060008181526020839052604081205560016110d4565b50600092915050565b600081600a548110156137045760405162461bcd60e51b815260040180806020018281038252602d8152602001806142b3602d913960400191505060405180910390fd5b61371060068585613c26565b949350505050565b60048054604080516020601f60026000196101006001881615020190951694909404938401819004810282018101909252828152606093909290918301828280156131565780601f1061312b57610100808354040283529160200191613156565b600081600a548110156137bd5760405162461bcd60e51b815260040180806020018281038252602d8152602001806142b3602d913960400191505060405180910390fd5b6113fc600784613c51565b600081600a5481101561380c5760405162461bcd60e51b815260040180806020018281038252602d8152602001806142b3602d913960400191505060405180910390fd5b438310613860576040805162461bcd60e51b815260206004820181905260248201527f43616e206f6e6c79206265207573656420666f72207061737420626c6f636b73604482015290519081900360640190fd5b6000806138706009600787613d77565b9150915080156138ae576040805186815290517ffec477a10b4fcdfdf1114eb32b3caf6118b2d76d20e1fcb70007274bb4b616be9181900360200190a15b509392505050565b60006001600160a01b038416156138f1576001600160a01b03841660009081526020869052604090206138ea9084846138fc565b9050613710565b506000949350505050565b60008161390b575060006113fc565b83548061391c5760009150506113fc565b600185015460006139396139308388613632565b60018503613dd5565b9050815b818110801561396f57508588600001826001018154811061395a57fe5b90600052602060002090600202016000015411155b156139a45787600001818154811061398357fe5b6000918252602082206002909102018181556001908101919091550161393d565b828111156139b457600188018190555b919091039695505050505050565b600b80546001600160a01b0319166001600160a01b0392909216919091179055565b816001600160a01b0316836001600160a01b03161415613a4b576040805162461bcd60e51b815260206004820152601760248201527f43616e6e6f74207472616e7366657220746f2073656c66000000000000000000604482015290519081900360640190fd5b6000613a5684611a7b565b90506000613a6384611a7b565b600f549091506001600160a01b03168015613afd576040805163756da1b160e11b81526001600160a01b038881166004830152878116602483015260448201869052606482018590526084820187905291519183169163eadb43629160a48082019260009290919082900301818387803b158015613ae057600080fd5b505af1158015613af4573d6000803e3d6000fd5b50505050613b22565b601154600160a01b900460ff16613b22576011805460ff60a01b1916600160a01b1790555b6010546001600160a01b03168015613bb5576040805163756da1b160e11b81526001600160a01b038981166004830152888116602483015260448201879052606482018690526084820188905291519183169163eadb43629160a48082019260009290919082900301818387803b158015613b9c57600080fd5b505af1158015613bb0573d6000803e3d6000fd5b505050505b613bc0878787613deb565b50505050505050565b600082821115613c20576040805162461bcd60e51b815260206004820152601e60248201527f536166654d6174683a207375627472616374696f6e206f766572666c6f770000604482015290519081900360640190fd5b50900390565b6001600160a01b0382166000908152602084905260408120613c488184613c51565b95945050505050565b815460009080613c655760009150506110d4565b4383101580613c975750836000016001820381548110613c8157fe5b9060005260206000209060020201600001548310155b15613cc857836000016001820381548110613cae57fe5b9060005260206000209060020201600101549150506110d4565b60018401548454859082908110613cdb57fe5b906000526020600020906002020160000154841015613d3c578015613d315760405162461bcd60e51b81526004018080602001828103825260308152602001806143d86030913960400191505060405180910390fd5b6000925050506110d4565b6000613d49868387613e2b565b9050856000018181548110613d5a57fe5b906000526020600020906002020160010154935050505092915050565b60008181526020849052604081205481908015613d9d5760001901915060009050613dcd565b6000613da98686613c51565b9050613db6816001613632565b600086815260208990526040902055925060019150505b935093915050565b6000818310613de457816113fc565b5090919050565b6001600160a01b038316613e0857613e038282613eb7565b612149565b6001600160a01b038216613e2057613e038382613f14565b612149838383613fbc565b825460009083908290613e3f906001613bc9565b90505b81811115613eae576000613e6c6002613e666001613e608688613632565b90613632565b90613ff2565b905084878281548110613e7b57fe5b90600052602060002090600202016000015411613e9a57809250613ea8565b613ea5816001613bc9565b91505b50613e42565b50949350505050565b6000613ec782613e6085436118dd565b9050613ed560068483614059565b600a54613ee99060069085906002906138b6565b50613f02613efa83613e6043612158565b600790614077565b600a546124b6906007906002906138fc565b6000613f538260405180604001604052806016815260200175213ab937103a37b7903134b3903337b91037bbb732b960511b8152506120cc86436118dd565b9050613f6160068483614059565b600a54613f759060069085906002906138b6565b50613f02613efa836040518060400160405280601d81526020017f4275726e20746f6f2062696720666f7220746f74616c20737570706c790000008152506120cc43612158565b613fc96006848484614140565b600a54613fdd9060069085906002906138b6565b50600a546124b69060069084906002906138b6565b6000808211614048576040805162461bcd60e51b815260206004820152601a60248201527f536166654d6174683a206469766973696f6e206279207a65726f000000000000604482015290519081900360640190fd5b81838161405157fe5b049392505050565b6001600160a01b03821660009081526020849052604090206124b681835b8154806140bb576040805180820190915243815260208082018481528554600181810188556000888152939093209351600290910290930192835551910155612149565b60008360000160018303815481106140cf57fe5b600091825260209091206002909102018054909150438114156140f857600182018490556112d6565b80431161410157fe5b604080518082019091524381526020808201868152875460018181018a5560008a81529390932093516002909102909301928355519101555050505050565b8061414a576124b6565b6001600160a01b03831615801561416857506001600160a01b038216155b1561416f57fe5b6001600160a01b038316156141a35760006141948261418e87876141cf565b90613bc9565b90506141a1858583614059565b505b6001600160a01b038216156124b65760006141c282613e6087866141cf565b90506112d6858483614059565b60006113fc838343613c2656fe45524332303a207472616e7366657220746f20746865207a65726f206164647265737345524332303a206275726e20616d6f756e7420657863656564732062616c616e6365436c65616e757020626c6f636b206d75737420626520696e20746865207061737445524332303a20617070726f766520746f20746865207a65726f206164647265737345524332303a207472616e7366657220616d6f756e7420657863656564732062616c616e6365436c65616e757020626c6f636b206e756d626572206d757374206e65766572206465637265617365436865636b506f696e7461626c653a2072656164696e672066726f6d20636c65616e65642d757020626c6f636b5650436f6e7472616374206e6f7420636f6e6669677572656420666f72207265706c6163656d656e7445524332303a207472616e7366657220616d6f756e74206578636565647320616c6c6f77616e6365476f7665726e616e636520766f746520706f77657220636f6e747261637420646f6573206e6f742062656c6f6e6720746f207468697320746f6b656e2e45524332303a206275726e2066726f6d20746865207a65726f206164647265737345524332303a207472616e736665722066726f6d20746865207a65726f206164647265737345524332303a20617070726f76652066726f6d20746865207a65726f2061646472657373436865636b506f696e74486973746f72793a2072656164696e672066726f6d20636c65616e65642d757020626c6f636b45524332303a2064656372656173656420616c6c6f77616e63652062656c6f77207a65726f5650436f6e7472616374206e6f74206f776e6564206279207468697320746f6b656ea26469706673582212202256264c5df72247242c0f71b36b1504ea6b44e5e7f4a7fd7f9b683cba27939164736f6c63430007060033