WIP Implement abdk math contract for constructor, decay
This commit is contained in:
parent
bcc957f861
commit
ffc041c1a3
@ -1,13 +1,15 @@
|
|||||||
pragma solidity >= 0.8.0;
|
pragma solidity >= 0.8.0;
|
||||||
|
|
||||||
|
|
||||||
|
import "aux/ABDKMath64x64.sol";
|
||||||
|
|
||||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
contract DemurrageTokenSingleCap {
|
contract DemurrageTokenSingleCap {
|
||||||
|
|
||||||
struct redistributionItem {
|
struct redistributionItem {
|
||||||
uint32 period;
|
uint32 period;
|
||||||
uint72 value;
|
uint72 value;
|
||||||
uint104 demurrage;
|
uint40 demurrage;
|
||||||
}
|
}
|
||||||
redistributionItem[] public redistributions; // uint51(unused) | uint64(demurrageModifier) | uint36(participants) | uint72(value) | uint32(period)
|
redistributionItem[] public redistributions; // uint51(unused) | uint64(demurrageModifier) | uint36(participants) | uint72(value) | uint32(period)
|
||||||
|
|
||||||
@ -15,7 +17,8 @@ contract DemurrageTokenSingleCap {
|
|||||||
mapping (address => uint256) account;
|
mapping (address => uint256) account;
|
||||||
|
|
||||||
// Cached demurrage amount, ppm with 38 digit resolution
|
// Cached demurrage amount, ppm with 38 digit resolution
|
||||||
uint128 public demurrageAmount;
|
//uint128 public demurrageAmount;
|
||||||
|
int128 public demurrageAmount;
|
||||||
|
|
||||||
// Cached demurrage timestamp; the timestamp for which demurrageAmount was last calculated
|
// Cached demurrage timestamp; the timestamp for which demurrageAmount was last calculated
|
||||||
uint256 public demurrageTimestamp;
|
uint256 public demurrageTimestamp;
|
||||||
@ -49,13 +52,13 @@ contract DemurrageTokenSingleCap {
|
|||||||
|
|
||||||
// 128 bit resolution of the demurrage divisor
|
// 128 bit resolution of the demurrage divisor
|
||||||
// (this constant x 1000000 is contained within 128 bits)
|
// (this constant x 1000000 is contained within 128 bits)
|
||||||
uint256 constant nanoDivider = 100000000000000000000000000; // now nanodivider, 6 zeros less
|
//uint256 constant nanoDivider = 100000000000000000000000000; // now nanodivider, 6 zeros less
|
||||||
|
|
||||||
// remaining decimal positions of nanoDivider to reach 38, equals precision in growth and decay
|
// remaining decimal positions of nanoDivider to reach 38, equals precision in growth and decay
|
||||||
uint256 constant growthResolutionFactor = 1000000000000;
|
//uint256 constant growthResolutionFactor = 1000000000000;
|
||||||
|
|
||||||
// demurrage decimal width; 38 places
|
// demurrage decimal width; 38 places
|
||||||
uint256 public immutable resolutionFactor = nanoDivider * growthResolutionFactor;
|
//uint256 public immutable resolutionFactor = nanoDivider * growthResolutionFactor;
|
||||||
|
|
||||||
// Timestamp of start of periods (time which contract constructor was called)
|
// Timestamp of start of periods (time which contract constructor was called)
|
||||||
uint256 public immutable periodStart;
|
uint256 public immutable periodStart;
|
||||||
@ -64,7 +67,9 @@ contract DemurrageTokenSingleCap {
|
|||||||
uint256 public immutable periodDuration;
|
uint256 public immutable periodDuration;
|
||||||
|
|
||||||
// Demurrage in ppm per minute
|
// Demurrage in ppm per minute
|
||||||
uint256 public immutable taxLevel;
|
//uint256 public immutable taxLevel;
|
||||||
|
// 64x64
|
||||||
|
int128 public taxLevel;
|
||||||
|
|
||||||
// Addresses allowed to mint new tokens
|
// Addresses allowed to mint new tokens
|
||||||
mapping (address => bool) minter;
|
mapping (address => bool) minter;
|
||||||
@ -102,9 +107,12 @@ contract DemurrageTokenSingleCap {
|
|||||||
// EIP173
|
// EIP173
|
||||||
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); // EIP173
|
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); // EIP173
|
||||||
|
|
||||||
constructor(string memory _name, string memory _symbol, uint8 _decimals, uint128 _taxLevelMinute, uint256 _periodMinutes, address _defaultSinkAddress) public {
|
constructor(string memory _name, string memory _symbol, uint8 _decimals, int128 _taxLevel, uint256 _periodMinutes, address _defaultSinkAddress) {
|
||||||
|
require(_taxLevel < (1 << 64));
|
||||||
redistributionItem memory initialRedistribution;
|
redistributionItem memory initialRedistribution;
|
||||||
|
|
||||||
|
//require(ABDKMath64x64.toUInt(_taxLevel) == 0);
|
||||||
|
|
||||||
// ACL setup
|
// ACL setup
|
||||||
owner = msg.sender;
|
owner = msg.sender;
|
||||||
minter[owner] = true;
|
minter[owner] = true;
|
||||||
@ -118,9 +126,11 @@ contract DemurrageTokenSingleCap {
|
|||||||
demurrageTimestamp = block.timestamp;
|
demurrageTimestamp = block.timestamp;
|
||||||
periodStart = demurrageTimestamp;
|
periodStart = demurrageTimestamp;
|
||||||
periodDuration = _periodMinutes * 60;
|
periodDuration = _periodMinutes * 60;
|
||||||
demurrageAmount = uint128(nanoDivider) * 100;
|
demurrageAmount = ABDKMath64x64.fromUInt(1);
|
||||||
taxLevel = _taxLevelMinute; // Represents 38 decimal places
|
|
||||||
initialRedistribution = toRedistribution(0, demurrageAmount, 0, 1);
|
//taxLevel = ABDKMath64x64.mul(ABDKMath64x64.ln(ABDKMath64x64.sub(demurrageAmount, , ABDKMath64x64.fromUInt(_periodMinutes));
|
||||||
|
taxLevel = ABDKMath64x64.ln(_taxLevel);
|
||||||
|
initialRedistribution = toRedistribution(0, uint40(uint128(demurrageAmount)), 0, 1);
|
||||||
redistributions.push(initialRedistribution);
|
redistributions.push(initialRedistribution);
|
||||||
|
|
||||||
// Misc settings
|
// Misc settings
|
||||||
@ -128,96 +138,96 @@ contract DemurrageTokenSingleCap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Change sink address for redistribution
|
// // Change sink address for redistribution
|
||||||
function setSinkAddress(address _sinkAddress) public {
|
// function setSinkAddress(address _sinkAddress) public {
|
||||||
require(msg.sender == owner);
|
// require(msg.sender == owner);
|
||||||
sinkAddress = _sinkAddress;
|
// sinkAddress = _sinkAddress;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// Given address will be allowed to call the mintTo() function
|
// // Given address will be allowed to call the mintTo() function
|
||||||
function addMinter(address _minter) public returns (bool) {
|
// function addMinter(address _minter) public returns (bool) {
|
||||||
require(msg.sender == owner);
|
// require(msg.sender == owner);
|
||||||
minter[_minter] = true;
|
// minter[_minter] = true;
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// Given address will no longer be allowed to call the mintTo() function
|
// // Given address will no longer be allowed to call the mintTo() function
|
||||||
function removeMinter(address _minter) public returns (bool) {
|
// function removeMinter(address _minter) public returns (bool) {
|
||||||
require(msg.sender == owner || _minter == msg.sender);
|
// require(msg.sender == owner || _minter == msg.sender);
|
||||||
minter[_minter] = false;
|
// minter[_minter] = false;
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
/// Implements ERC20
|
// /// Implements ERC20
|
||||||
function balanceOf(address _account) public view returns (uint256) {
|
// function balanceOf(address _account) public view returns (uint256) {
|
||||||
uint256 baseBalance;
|
// uint256 baseBalance;
|
||||||
uint256 currentDemurragedAmount;
|
// uint256 currentDemurragedAmount;
|
||||||
uint256 periodCount;
|
// uint256 periodCount;
|
||||||
|
//
|
||||||
baseBalance = baseBalanceOf(_account);
|
// baseBalance = baseBalanceOf(_account);
|
||||||
|
//
|
||||||
periodCount = getMinutesDelta(demurrageTimestamp);
|
// periodCount = getMinutesDelta(demurrageTimestamp);
|
||||||
|
//
|
||||||
currentDemurragedAmount = uint128(decayBy(demurrageAmount * 10000000000, periodCount));
|
// currentDemurragedAmount = uint128(decayBy(demurrageAmount * 10000000000, periodCount));
|
||||||
|
//
|
||||||
return (baseBalance * currentDemurragedAmount) / (nanoDivider * 1000000000000);
|
// return (baseBalance * currentDemurragedAmount) / (nanoDivider * 1000000000000);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
/// Balance unmodified by demurrage
|
// /// Balance unmodified by demurrage
|
||||||
function baseBalanceOf(address _account) public view returns (uint256) {
|
// function baseBalanceOf(address _account) public view returns (uint256) {
|
||||||
return account[_account];
|
// return account[_account];
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
/// Increases base balance for a single account
|
// /// Increases base balance for a single account
|
||||||
function increaseBaseBalance(address _account, uint256 _delta) private returns (bool) {
|
// function increaseBaseBalance(address _account, uint256 _delta) private returns (bool) {
|
||||||
uint256 oldBalance;
|
// uint256 oldBalance;
|
||||||
uint256 newBalance;
|
// uint256 newBalance;
|
||||||
uint256 workAccount;
|
// uint256 workAccount;
|
||||||
|
//
|
||||||
workAccount = uint256(account[_account]);
|
// workAccount = uint256(account[_account]);
|
||||||
|
//
|
||||||
if (_delta == 0) {
|
// if (_delta == 0) {
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
oldBalance = baseBalanceOf(_account);
|
// oldBalance = baseBalanceOf(_account);
|
||||||
account[_account] = oldBalance + _delta;
|
// account[_account] = oldBalance + _delta;
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
/// Decreases base balance for a single account
|
// /// Decreases base balance for a single account
|
||||||
function decreaseBaseBalance(address _account, uint256 _delta) private returns (bool) {
|
// function decreaseBaseBalance(address _account, uint256 _delta) private returns (bool) {
|
||||||
uint256 oldBalance;
|
// uint256 oldBalance;
|
||||||
uint256 newBalance;
|
// uint256 newBalance;
|
||||||
uint256 workAccount;
|
// uint256 workAccount;
|
||||||
|
//
|
||||||
workAccount = uint256(account[_account]);
|
// workAccount = uint256(account[_account]);
|
||||||
|
//
|
||||||
if (_delta == 0) {
|
// if (_delta == 0) {
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
oldBalance = baseBalanceOf(_account);
|
// oldBalance = baseBalanceOf(_account);
|
||||||
require(oldBalance >= _delta, 'ERR_OVERSPEND'); // overspend guard
|
// require(oldBalance >= _delta, 'ERR_OVERSPEND'); // overspend guard
|
||||||
account[_account] = oldBalance - _delta;
|
// account[_account] = oldBalance - _delta;
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// Creates new tokens out of thin air, and allocates them to the given address
|
// // Creates new tokens out of thin air, and allocates them to the given address
|
||||||
// Triggers tax
|
// // Triggers tax
|
||||||
function mintTo(address _beneficiary, uint256 _amount) external returns (bool) {
|
// function mintTo(address _beneficiary, uint256 _amount) external returns (bool) {
|
||||||
uint256 baseAmount;
|
// uint256 baseAmount;
|
||||||
|
//
|
||||||
require(minter[msg.sender], 'ERR_ACCESS');
|
// require(minter[msg.sender], 'ERR_ACCESS');
|
||||||
|
//
|
||||||
changePeriod();
|
// changePeriod();
|
||||||
baseAmount = toBaseAmount(_amount);
|
// baseAmount = toBaseAmount(_amount);
|
||||||
supply += _amount;
|
// supply += _amount;
|
||||||
increaseBaseBalance(_beneficiary, baseAmount);
|
// increaseBaseBalance(_beneficiary, baseAmount);
|
||||||
emit Mint(msg.sender, _beneficiary, _amount);
|
// emit Mint(msg.sender, _beneficiary, _amount);
|
||||||
saveRedistributionSupply();
|
// saveRedistributionSupply();
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Deserializes the redistribution word
|
// Deserializes the redistribution word
|
||||||
function toRedistribution(uint256 _participants, uint256 _demurrageModifierPpm, uint256 _value, uint256 _period) public pure returns(redistributionItem memory) {
|
function toRedistribution(uint256 _participants, uint256 _demurrageModifierPpm, uint256 _value, uint256 _period) public pure returns(redistributionItem memory) {
|
||||||
@ -225,185 +235,199 @@ contract DemurrageTokenSingleCap {
|
|||||||
|
|
||||||
redistribution.period = uint32(_period);
|
redistribution.period = uint32(_period);
|
||||||
redistribution.value = uint72(_value);
|
redistribution.value = uint72(_value);
|
||||||
redistribution.demurrage = uint104(_demurrageModifierPpm);
|
redistribution.demurrage = uint40(_demurrageModifierPpm);
|
||||||
return redistribution;
|
return redistribution;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
//
|
||||||
// Serializes the demurrage period part of the redistribution word
|
// // Serializes the demurrage period part of the redistribution word
|
||||||
function toRedistributionPeriod(redistributionItem memory _redistribution) public pure returns (uint256) {
|
// function toRedistributionPeriod(redistributionItem memory _redistribution) public pure returns (uint256) {
|
||||||
return uint256(_redistribution.period);
|
// return uint256(_redistribution.period);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// Serializes the supply part of the redistribution word
|
// // Serializes the supply part of the redistribution word
|
||||||
function toRedistributionSupply(redistributionItem memory _redistribution) public pure returns (uint256) {
|
// function toRedistributionSupply(redistributionItem memory _redistribution) public pure returns (uint256) {
|
||||||
return uint256(_redistribution.value);
|
// return uint256(_redistribution.value);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// Serializes the number of participants part of the redistribution word
|
// // Serializes the number of participants part of the redistribution word
|
||||||
function toRedistributionDemurrageModifier(redistributionItem memory _redistribution) public pure returns (uint256) {
|
// function toRedistributionDemurrageModifier(redistributionItem memory _redistribution) public pure returns (uint256) {
|
||||||
return uint256(_redistribution.demurrage);
|
// return uint256(_redistribution.demurrage);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
// Client accessor to the redistributions array length
|
// // Client accessor to the redistributions array length
|
||||||
function redistributionCount() public view returns (uint256) {
|
// function redistributionCount() public view returns (uint256) {
|
||||||
return redistributions.length;
|
// return redistributions.length;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// Save the current total supply amount to the current redistribution period
|
// // Save the current total supply amount to the current redistribution period
|
||||||
function saveRedistributionSupply() private returns (bool) {
|
// function saveRedistributionSupply() private returns (bool) {
|
||||||
redistributionItem memory currentRedistribution;
|
// redistributionItem memory currentRedistribution;
|
||||||
uint256 grownSupply;
|
// uint256 grownSupply;
|
||||||
|
//
|
||||||
grownSupply = totalSupply();
|
// grownSupply = totalSupply();
|
||||||
currentRedistribution = redistributions[redistributions.length-1];
|
// currentRedistribution = redistributions[redistributions.length-1];
|
||||||
currentRedistribution.value = uint72(grownSupply);
|
// currentRedistribution.value = uint72(grownSupply);
|
||||||
|
//
|
||||||
redistributions[redistributions.length-1] = currentRedistribution;
|
// redistributions[redistributions.length-1] = currentRedistribution;
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// Get the demurrage period of the current block number
|
// // Get the demurrage period of the current block number
|
||||||
function actualPeriod() public view returns (uint128) {
|
// function actualPeriod() public view returns (uint128) {
|
||||||
return uint128((block.timestamp - periodStart) / periodDuration + 1);
|
// return uint128((block.timestamp - periodStart) / periodDuration + 1);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// Retrieve next redistribution if the period threshold has been crossed
|
// // Retrieve next redistribution if the period threshold has been crossed
|
||||||
function checkPeriod() private view returns (redistributionItem memory) {
|
// function checkPeriod() private view returns (redistributionItem memory) {
|
||||||
redistributionItem memory lastRedistribution;
|
// redistributionItem memory lastRedistribution;
|
||||||
redistributionItem memory emptyRedistribution;
|
// redistributionItem memory emptyRedistribution;
|
||||||
uint256 currentPeriod;
|
// uint256 currentPeriod;
|
||||||
|
//
|
||||||
lastRedistribution = redistributions[lastPeriod];
|
// lastRedistribution = redistributions[lastPeriod];
|
||||||
currentPeriod = this.actualPeriod();
|
// currentPeriod = this.actualPeriod();
|
||||||
if (currentPeriod <= toRedistributionPeriod(lastRedistribution)) {
|
// if (currentPeriod <= toRedistributionPeriod(lastRedistribution)) {
|
||||||
return emptyRedistribution;
|
// return emptyRedistribution;
|
||||||
}
|
// }
|
||||||
return lastRedistribution;
|
// return lastRedistribution;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
function getDistribution(uint256 _supply, uint256 _demurrageAmount) public view returns (uint256) {
|
// function getDistribution(uint256 _supply, uint256 _demurrageAmount) public view returns (uint256) {
|
||||||
uint256 difference;
|
// uint256 difference;
|
||||||
|
//
|
||||||
difference = _supply * (resolutionFactor - (_demurrageAmount * 10000000000));
|
// difference = _supply * (resolutionFactor - (_demurrageAmount * 10000000000));
|
||||||
return difference / resolutionFactor;
|
// return difference / resolutionFactor;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
function getDistributionFromRedistribution(redistributionItem memory _redistribution) public returns (uint256) {
|
// function getDistributionFromRedistribution(redistributionItem memory _redistribution) public returns (uint256) {
|
||||||
uint256 redistributionSupply;
|
// uint256 redistributionSupply;
|
||||||
uint256 redistributionDemurrage;
|
// uint256 redistributionDemurrage;
|
||||||
|
//
|
||||||
redistributionSupply = toRedistributionSupply(_redistribution);
|
// redistributionSupply = toRedistributionSupply(_redistribution);
|
||||||
redistributionDemurrage = toRedistributionDemurrageModifier(_redistribution);
|
// redistributionDemurrage = toRedistributionDemurrageModifier(_redistribution);
|
||||||
return getDistribution(redistributionSupply, redistributionDemurrage);
|
// return getDistribution(redistributionSupply, redistributionDemurrage);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// Returns the amount sent to the sink address
|
// // Returns the amount sent to the sink address
|
||||||
function applyDefaultRedistribution(redistributionItem memory _redistribution) private returns (uint256) {
|
// function applyDefaultRedistribution(redistributionItem memory _redistribution) private returns (uint256) {
|
||||||
uint256 unit;
|
// uint256 unit;
|
||||||
uint256 baseUnit;
|
// uint256 baseUnit;
|
||||||
|
//
|
||||||
unit = getDistributionFromRedistribution(_redistribution);
|
// unit = getDistributionFromRedistribution(_redistribution);
|
||||||
baseUnit = toBaseAmount(unit) - totalSink;
|
// baseUnit = toBaseAmount(unit) - totalSink;
|
||||||
increaseBaseBalance(sinkAddress, baseUnit);
|
// increaseBaseBalance(sinkAddress, baseUnit);
|
||||||
lastPeriod += 1;
|
// lastPeriod += 1;
|
||||||
totalSink += baseUnit;
|
// totalSink += baseUnit;
|
||||||
return unit;
|
// return unit;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// Calculate the time delta in whole minutes passed between given timestamp and current timestamp
|
// // Calculate the time delta in whole minutes passed between given timestamp and current timestamp
|
||||||
function getMinutesDelta(uint256 _lastTimestamp) public view returns (uint256) {
|
// function getMinutesDelta(uint256 _lastTimestamp) public view returns (uint256) {
|
||||||
return (block.timestamp - _lastTimestamp) / 60;
|
// return (block.timestamp - _lastTimestamp) / 60;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// Calculate and cache the demurrage value corresponding to the (period of the) time of the method call
|
// // Calculate and cache the demurrage value corresponding to the (period of the) time of the method call
|
||||||
function applyDemurrage() public returns (bool) {
|
// function applyDemurrage() public returns (bool) {
|
||||||
return applyDemurrageLimited(0);
|
// return applyDemurrageLimited(0);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
function applyDemurrageLimited(uint256 _rounds) public returns (bool) {
|
// function applyDemurrageLimited(uint256 _rounds) public returns (bool) {
|
||||||
uint256 periodCount;
|
// uint256 periodCount;
|
||||||
uint256 lastDemurrageAmount;
|
// uint256 lastDemurrageAmount;
|
||||||
|
//
|
||||||
periodCount = getMinutesDelta(demurrageTimestamp);
|
// periodCount = getMinutesDelta(demurrageTimestamp);
|
||||||
if (periodCount == 0) {
|
// if (periodCount == 0) {
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
lastDemurrageAmount = demurrageAmount;
|
// lastDemurrageAmount = demurrageAmount;
|
||||||
|
//
|
||||||
// safety limit for exponential calculation to ensure that we can always
|
// // safety limit for exponential calculation to ensure that we can always
|
||||||
// execute this code no matter how much time passes.
|
// // execute this code no matter how much time passes.
|
||||||
if (_rounds > 0 && _rounds < periodCount) {
|
// if (_rounds > 0 && _rounds < periodCount) {
|
||||||
periodCount = _rounds;
|
// periodCount = _rounds;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
demurrageAmount = uint128(decayBy(lastDemurrageAmount, periodCount));
|
// demurrageAmount = uint128(decayBy(lastDemurrageAmount, periodCount));
|
||||||
//demurragePeriod = epochPeriodCount;
|
// //demurragePeriod = epochPeriodCount;
|
||||||
demurrageTimestamp = demurrageTimestamp + (periodCount * 60);
|
// demurrageTimestamp = demurrageTimestamp + (periodCount * 60);
|
||||||
emit Decayed(demurrageTimestamp, periodCount, lastDemurrageAmount, demurrageAmount);
|
// emit Decayed(demurrageTimestamp, periodCount, lastDemurrageAmount, demurrageAmount);
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// Return timestamp of start of period threshold
|
// // Return timestamp of start of period threshold
|
||||||
function getPeriodTimeDelta(uint256 _periodCount) public view returns (uint256) {
|
// function getPeriodTimeDelta(uint256 _periodCount) public view returns (uint256) {
|
||||||
return periodStart + (_periodCount * periodDuration);
|
// return periodStart + (_periodCount * periodDuration);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// Amount of demurrage cycles inbetween the current timestamp and the given target time
|
// // Amount of demurrage cycles inbetween the current timestamp and the given target time
|
||||||
function demurrageCycles(uint256 _target) public view returns (uint256) {
|
// function demurrageCycles(uint256 _target) public view returns (uint256) {
|
||||||
return (block.timestamp - _target) / 60;
|
// return (block.timestamp - _target) / 60;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
function isEmptyRedistribution(redistributionItem memory _redistribution) public pure returns(bool) {
|
// function isEmptyRedistribution(redistributionItem memory _redistribution) public pure returns(bool) {
|
||||||
if (_redistribution.period > 0) {
|
// if (_redistribution.period > 0) {
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
if (_redistribution.value > 0) {
|
// if (_redistribution.value > 0) {
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
if (_redistribution.demurrage > 0) {
|
// if (_redistribution.demurrage > 0) {
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// Recalculate the demurrage modifier for the new period
|
// // Recalculate the demurrage modifier for the new period
|
||||||
// Note that the supply for the consecutive period will be taken at the time of code execution, and thus not necessarily at the time when the redistribution period threshold was crossed.
|
// // Note that the supply for the consecutive period will be taken at the time of code execution, and thus not necessarily at the time when the redistribution period threshold was crossed.
|
||||||
function changePeriod() public returns (bool) {
|
// function changePeriod() public returns (bool) {
|
||||||
redistributionItem memory currentRedistribution;
|
// redistributionItem memory currentRedistribution;
|
||||||
redistributionItem memory nextRedistribution;
|
// redistributionItem memory nextRedistribution;
|
||||||
redistributionItem memory lastRedistribution;
|
// redistributionItem memory lastRedistribution;
|
||||||
uint256 currentPeriod;
|
// uint256 currentPeriod;
|
||||||
uint256 lastDemurrageAmount;
|
// uint256 lastDemurrageAmount;
|
||||||
uint256 nextRedistributionDemurrage;
|
// uint256 nextRedistributionDemurrage;
|
||||||
uint256 demurrageCounts;
|
// uint256 demurrageCounts;
|
||||||
uint256 nextPeriod;
|
// uint256 nextPeriod;
|
||||||
|
//
|
||||||
applyDemurrage();
|
// applyDemurrage();
|
||||||
currentRedistribution = checkPeriod();
|
// currentRedistribution = checkPeriod();
|
||||||
if (isEmptyRedistribution(currentRedistribution)) {
|
// if (isEmptyRedistribution(currentRedistribution)) {
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// calculate the decay from previous redistributino
|
// // calculate the decay from previous redistributino
|
||||||
lastRedistribution = redistributions[lastPeriod];
|
// lastRedistribution = redistributions[lastPeriod];
|
||||||
currentPeriod = toRedistributionPeriod(currentRedistribution);
|
// currentPeriod = toRedistributionPeriod(currentRedistribution);
|
||||||
nextPeriod = currentPeriod + 1;
|
// nextPeriod = currentPeriod + 1;
|
||||||
lastDemurrageAmount = toRedistributionDemurrageModifier(lastRedistribution);
|
// lastDemurrageAmount = toRedistributionDemurrageModifier(lastRedistribution);
|
||||||
demurrageCounts = periodDuration / 60;
|
// demurrageCounts = periodDuration / 60;
|
||||||
nextRedistributionDemurrage = decayBy(lastDemurrageAmount, demurrageCounts);
|
// nextRedistributionDemurrage = decayBy(lastDemurrageAmount, demurrageCounts);
|
||||||
|
//
|
||||||
nextRedistribution = toRedistribution(0, nextRedistributionDemurrage, totalSupply(), nextPeriod);
|
// nextRedistribution = toRedistribution(0, nextRedistributionDemurrage, totalSupply(), nextPeriod);
|
||||||
redistributions.push(nextRedistribution);
|
// redistributions.push(nextRedistribution);
|
||||||
|
//
|
||||||
applyDefaultRedistribution(nextRedistribution);
|
// applyDefaultRedistribution(nextRedistribution);
|
||||||
emit Period(nextPeriod);
|
// emit Period(nextPeriod);
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
// Reverse a value reduced by demurrage by the given period to its original value
|
// // Reverse a value reduced by demurrage by the given period to its original value
|
||||||
// function growBy(uint256 _value, uint256 _period) public view returns (uint256) {
|
//// function growBy(uint256 _value, uint256 _period) public view returns (uint256) {
|
||||||
|
//// uint256 valueFactor;
|
||||||
|
//// uint256 truncatedTaxLevel;
|
||||||
|
////
|
||||||
|
//// valueFactor = growthResolutionFactor;
|
||||||
|
//// truncatedTaxLevel = taxLevel / nanoDivider;
|
||||||
|
////
|
||||||
|
//// for (uint256 i = 0; i < _period; i++) {
|
||||||
|
//// valueFactor = valueFactor + ((valueFactor * truncatedTaxLevel) / growthResolutionFactor);
|
||||||
|
//// }
|
||||||
|
//// return (valueFactor * _value) / growthResolutionFactor;
|
||||||
|
//// }
|
||||||
|
//
|
||||||
|
// // Calculate a value reduced by demurrage by the given period
|
||||||
|
// function decayBy(uint256 _value, uint256 _period) public view returns (uint256) {
|
||||||
// uint256 valueFactor;
|
// uint256 valueFactor;
|
||||||
// uint256 truncatedTaxLevel;
|
// uint256 truncatedTaxLevel;
|
||||||
//
|
//
|
||||||
@ -411,152 +435,155 @@ contract DemurrageTokenSingleCap {
|
|||||||
// truncatedTaxLevel = taxLevel / nanoDivider;
|
// truncatedTaxLevel = taxLevel / nanoDivider;
|
||||||
//
|
//
|
||||||
// for (uint256 i = 0; i < _period; i++) {
|
// for (uint256 i = 0; i < _period; i++) {
|
||||||
// valueFactor = valueFactor + ((valueFactor * truncatedTaxLevel) / growthResolutionFactor);
|
// valueFactor = valueFactor - ((valueFactor * truncatedTaxLevel) / growthResolutionFactor);
|
||||||
// }
|
// }
|
||||||
// return (valueFactor * _value) / growthResolutionFactor;
|
// return (valueFactor * _value) / growthResolutionFactor;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// Calculate a value reduced by demurrage by the given period
|
|
||||||
function decayBy(uint256 _value, uint256 _period) public view returns (uint256) {
|
function decayBy(uint256 _value, uint256 _period) public view returns (uint256) {
|
||||||
uint256 valueFactor;
|
int128 valuePoint;
|
||||||
uint256 truncatedTaxLevel;
|
int128 periodPoint;
|
||||||
|
int128 v;
|
||||||
|
|
||||||
valueFactor = growthResolutionFactor;
|
valuePoint = ABDKMath64x64.fromUInt(_value);
|
||||||
truncatedTaxLevel = taxLevel / nanoDivider;
|
periodPoint = ABDKMath64x64.fromUInt(_period);
|
||||||
|
|
||||||
for (uint256 i = 0; i < _period; i++) {
|
//valuePoint -= ABDKMath64x64.mul(ABDKMath64x64.exp(ABDKMath64x64.mul(taxLevel, periodPoint)), valuePoint);
|
||||||
valueFactor = valueFactor - ((valueFactor * truncatedTaxLevel) / growthResolutionFactor);
|
//valuePoint -= ABDKMath64x64.exp(ABDKMath64x64.mul(taxLevel, periodPoint));
|
||||||
}
|
v = ABDKMath64x64.mul(taxLevel, periodPoint);
|
||||||
return (valueFactor * _value) / growthResolutionFactor;
|
v = ABDKMath64x64.exp(v);
|
||||||
|
v = ABDKMath64x64.mul(valuePoint, v);
|
||||||
|
return ABDKMath64x64.toUInt(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inflates the given amount according to the current demurrage modifier
|
//
|
||||||
function toBaseAmount(uint256 _value) public view returns (uint256) {
|
// // Inflates the given amount according to the current demurrage modifier
|
||||||
return (_value * resolutionFactor) / (demurrageAmount * 10000000000);
|
// function toBaseAmount(uint256 _value) public view returns (uint256) {
|
||||||
}
|
// return (_value * resolutionFactor) / (demurrageAmount * 10000000000);
|
||||||
|
// }
|
||||||
// Implements ERC20, triggers tax and/or redistribution
|
//
|
||||||
function approve(address _spender, uint256 _value) public returns (bool) {
|
// // Implements ERC20, triggers tax and/or redistribution
|
||||||
uint256 baseValue;
|
// function approve(address _spender, uint256 _value) public returns (bool) {
|
||||||
|
// uint256 baseValue;
|
||||||
if (allowance[msg.sender][_spender] > 0) {
|
//
|
||||||
require(_value == 0, 'ZERO_FIRST');
|
// if (allowance[msg.sender][_spender] > 0) {
|
||||||
}
|
// require(_value == 0, 'ZERO_FIRST');
|
||||||
|
// }
|
||||||
changePeriod();
|
//
|
||||||
|
// changePeriod();
|
||||||
baseValue = toBaseAmount(_value);
|
//
|
||||||
allowance[msg.sender][_spender] = baseValue;
|
// baseValue = toBaseAmount(_value);
|
||||||
emit Approval(msg.sender, _spender, _value);
|
// allowance[msg.sender][_spender] = baseValue;
|
||||||
return true;
|
// emit Approval(msg.sender, _spender, _value);
|
||||||
}
|
// return true;
|
||||||
|
// }
|
||||||
// Reduce allowance by amount
|
//
|
||||||
function decreaseAllowance(address _spender, uint256 _value) public returns (bool) {
|
// // Reduce allowance by amount
|
||||||
uint256 baseValue;
|
// function decreaseAllowance(address _spender, uint256 _value) public returns (bool) {
|
||||||
|
// uint256 baseValue;
|
||||||
baseValue = toBaseAmount(_value);
|
//
|
||||||
require(allowance[msg.sender][_spender] >= baseValue);
|
// baseValue = toBaseAmount(_value);
|
||||||
|
// require(allowance[msg.sender][_spender] >= baseValue);
|
||||||
changePeriod();
|
//
|
||||||
|
// changePeriod();
|
||||||
allowance[msg.sender][_spender] -= baseValue;
|
//
|
||||||
emit Approval(msg.sender, _spender, allowance[msg.sender][_spender]);
|
// allowance[msg.sender][_spender] -= baseValue;
|
||||||
return true;
|
// emit Approval(msg.sender, _spender, allowance[msg.sender][_spender]);
|
||||||
}
|
// return true;
|
||||||
|
// }
|
||||||
// Increase allowance by amount
|
//
|
||||||
function increaseAllowance(address _spender, uint256 _value) public returns (bool) {
|
// // Increase allowance by amount
|
||||||
uint256 baseValue;
|
// function increaseAllowance(address _spender, uint256 _value) public returns (bool) {
|
||||||
|
// uint256 baseValue;
|
||||||
changePeriod();
|
//
|
||||||
|
// changePeriod();
|
||||||
baseValue = toBaseAmount(_value);
|
//
|
||||||
|
// baseValue = toBaseAmount(_value);
|
||||||
allowance[msg.sender][_spender] += baseValue;
|
//
|
||||||
emit Approval(msg.sender, _spender, allowance[msg.sender][_spender]);
|
// allowance[msg.sender][_spender] += baseValue;
|
||||||
return true;
|
// emit Approval(msg.sender, _spender, allowance[msg.sender][_spender]);
|
||||||
}
|
// return true;
|
||||||
|
// }
|
||||||
// Implements ERC20, triggers tax and/or redistribution
|
//
|
||||||
function transfer(address _to, uint256 _value) public returns (bool) {
|
// // Implements ERC20, triggers tax and/or redistribution
|
||||||
uint256 baseValue;
|
// function transfer(address _to, uint256 _value) public returns (bool) {
|
||||||
bool result;
|
// uint256 baseValue;
|
||||||
|
// bool result;
|
||||||
changePeriod();
|
//
|
||||||
|
// changePeriod();
|
||||||
baseValue = toBaseAmount(_value);
|
//
|
||||||
result = transferBase(msg.sender, _to, baseValue);
|
// baseValue = toBaseAmount(_value);
|
||||||
emit Transfer(msg.sender, _to, _value);
|
// result = transferBase(msg.sender, _to, baseValue);
|
||||||
return result;
|
// emit Transfer(msg.sender, _to, _value);
|
||||||
}
|
// return result;
|
||||||
|
// }
|
||||||
// Implements ERC20, triggers tax and/or redistribution
|
//
|
||||||
function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
|
// // Implements ERC20, triggers tax and/or redistribution
|
||||||
uint256 baseValue;
|
// function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
|
||||||
bool result;
|
// uint256 baseValue;
|
||||||
|
// bool result;
|
||||||
changePeriod();
|
//
|
||||||
|
// changePeriod();
|
||||||
baseValue = toBaseAmount(_value);
|
//
|
||||||
require(allowance[_from][msg.sender] >= baseValue);
|
// baseValue = toBaseAmount(_value);
|
||||||
|
// require(allowance[_from][msg.sender] >= baseValue);
|
||||||
allowance[_from][msg.sender] -= baseValue;
|
//
|
||||||
result = transferBase(_from, _to, baseValue);
|
// allowance[_from][msg.sender] -= baseValue;
|
||||||
|
// result = transferBase(_from, _to, baseValue);
|
||||||
emit Transfer(_from, _to, _value);
|
//
|
||||||
return result;
|
// emit Transfer(_from, _to, _value);
|
||||||
}
|
// return result;
|
||||||
|
// }
|
||||||
// ERC20 transfer backend for transfer, transferFrom
|
//
|
||||||
function transferBase(address _from, address _to, uint256 _value) private returns (bool) {
|
// // ERC20 transfer backend for transfer, transferFrom
|
||||||
uint256 period;
|
// function transferBase(address _from, address _to, uint256 _value) private returns (bool) {
|
||||||
|
// uint256 period;
|
||||||
decreaseBaseBalance(_from, _value);
|
//
|
||||||
increaseBaseBalance(_to, _value);
|
// decreaseBaseBalance(_from, _value);
|
||||||
|
// increaseBaseBalance(_to, _value);
|
||||||
return true;
|
//
|
||||||
}
|
// return true;
|
||||||
|
// }
|
||||||
// Implements EIP173
|
//
|
||||||
function transferOwnership(address _newOwner) public returns (bool) {
|
// // Implements EIP173
|
||||||
require(msg.sender == owner);
|
// function transferOwnership(address _newOwner) public returns (bool) {
|
||||||
newOwner = _newOwner;
|
// require(msg.sender == owner);
|
||||||
}
|
// newOwner = _newOwner;
|
||||||
|
// }
|
||||||
// Implements OwnedAccepter
|
//
|
||||||
function acceptOwnership() public returns (bool) {
|
// // Implements OwnedAccepter
|
||||||
address oldOwner;
|
// function acceptOwnership() public returns (bool) {
|
||||||
|
// address oldOwner;
|
||||||
require(msg.sender == newOwner);
|
//
|
||||||
oldOwner = owner;
|
// require(msg.sender == newOwner);
|
||||||
owner = newOwner;
|
// oldOwner = owner;
|
||||||
newOwner = address(0);
|
// owner = newOwner;
|
||||||
emit OwnershipTransferred(oldOwner, owner);
|
// newOwner = address(0);
|
||||||
}
|
// emit OwnershipTransferred(oldOwner, owner);
|
||||||
|
// }
|
||||||
// Explicitly and irretrievably burn tokens
|
//
|
||||||
// Only token minters can burn tokens
|
// // Explicitly and irretrievably burn tokens
|
||||||
function burn(uint256 _value) public {
|
// // Only token minters can burn tokens
|
||||||
require(minter[msg.sender]);
|
// function burn(uint256 _value) public {
|
||||||
require(_value <= account[msg.sender]);
|
// require(minter[msg.sender]);
|
||||||
uint256 _delta = toBaseAmount(_value);
|
// require(_value <= account[msg.sender]);
|
||||||
|
// uint256 _delta = toBaseAmount(_value);
|
||||||
applyDemurrage();
|
//
|
||||||
decreaseBaseBalance(msg.sender, _delta);
|
// applyDemurrage();
|
||||||
burned += _value;
|
// decreaseBaseBalance(msg.sender, _delta);
|
||||||
emit Burn(msg.sender, _value);
|
// burned += _value;
|
||||||
}
|
// emit Burn(msg.sender, _value);
|
||||||
|
// }
|
||||||
// Implements ERC20
|
//
|
||||||
function totalSupply() public view returns (uint256) {
|
// // Implements ERC20
|
||||||
return supply - burned;
|
// function totalSupply() public view returns (uint256) {
|
||||||
}
|
// return supply - burned;
|
||||||
|
// }
|
||||||
// Return total number of burned tokens
|
//
|
||||||
function totalBurned() public view returns (uint256) {
|
// // Return total number of burned tokens
|
||||||
return burned;
|
// function totalBurned() public view returns (uint256) {
|
||||||
}
|
// return burned;
|
||||||
|
// }
|
||||||
|
|
||||||
// Implements EIP165
|
// Implements EIP165
|
||||||
function supportsInterface(bytes4 _sum) public pure returns (bool) {
|
function supportsInterface(bytes4 _sum) public pure returns (bool) {
|
||||||
|
@ -15,8 +15,8 @@ multi_cap:
|
|||||||
multi: multi_nocap multi_cap
|
multi: multi_nocap multi_cap
|
||||||
|
|
||||||
single_nocap:
|
single_nocap:
|
||||||
$(SOLC) DemurrageTokenSingleNocap.sol --abi --evm-version byzantium | awk 'NR>3' > DemurrageTokenSingleNocap.json
|
$(SOLC) DemurrageTokenSingleNocap.sol --abi --evm-version byzantium | awk 'NR==4' > DemurrageTokenSingleNocap.json
|
||||||
$(SOLC) DemurrageTokenSingleNocap.sol --bin --evm-version byzantium | awk 'NR>3' > DemurrageTokenSingleNocap.bin
|
$(SOLC) DemurrageTokenSingleNocap.sol --bin --evm-version byzantium | awk 'NR==4' > DemurrageTokenSingleNocap.bin
|
||||||
truncate -s -1 DemurrageTokenSingleNocap.bin
|
truncate -s -1 DemurrageTokenSingleNocap.bin
|
||||||
|
|
||||||
single_cap:
|
single_cap:
|
||||||
|
Reference in New Issue
Block a user