erc20-demurrage-token/solidity/DemurrageTokenSingleNocap.sol

776 lines
22 KiB
Solidity
Raw Normal View History

2023-02-04 15:41:40 +01:00
pragma solidity >= 0.8.0;
import "aux/ABDKMath64x64.sol";
2023-03-26 08:26:02 +02:00
// SPDX-License-Identifier: AGPL-3.0-or-later
2023-02-10 18:02:29 +01:00
contract DemurrageTokenSingleNocap {
2023-02-08 09:44:37 +01:00
struct redistributionItem {
uint32 period;
uint72 value;
2023-02-10 06:02:24 +01:00
uint64 demurrage;
2023-02-08 09:44:37 +01:00
}
2023-03-26 08:26:02 +02:00
redistributionItem[] public redistributions;
// Account balances
mapping (address => uint256) account;
// Cached demurrage amount, ppm with 38 digit resolution
//uint128 public demurrageAmount;
int128 public demurrageAmount;
2021-06-08 16:38:45 +02:00
// Cached demurrage timestamp; the timestamp for which demurrageAmount was last calculated
uint256 public demurrageTimestamp;
2023-03-26 08:26:02 +02:00
// Implements EIP173
address public owner;
address newOwner;
// Implements ERC20
string public name;
// Implements ERC20
string public symbol;
// Implements ERC20
2023-02-10 18:02:29 +01:00
uint256 public immutable decimals;
2023-02-04 15:41:40 +01:00
uint256 supply;
2022-05-30 09:53:22 +02:00
// Last executed period
uint256 public lastPeriod;
// Last sink redistribution amount
uint256 public totalSink;
2023-02-04 15:41:40 +01:00
// Value of burnt tokens (burnt tokens do not decay)
2023-03-26 08:26:02 +02:00
uint256 burned;
2023-02-04 15:41:40 +01:00
// 128 bit resolution of the demurrage divisor
// (this constant x 1000000 is contained within 128 bits)
//uint256 constant nanoDivider = 100000000000000000000000000; // now nanodivider, 6 zeros less
2021-06-08 16:38:45 +02:00
// remaining decimal positions of nanoDivider to reach 38, equals precision in growth and decay
//uint256 constant growthResolutionFactor = 1000000000000;
2021-06-08 16:38:45 +02:00
// demurrage decimal width; 38 places
//uint256 public immutable resolutionFactor = nanoDivider * growthResolutionFactor;
// Timestamp of start of periods (time which contract constructor was called)
uint256 public immutable periodStart;
// Duration of a single redistribution period in seconds
uint256 public immutable periodDuration;
// Demurrage in ppm per minute
2023-02-19 11:41:54 +01:00
//uint256 public immutable decayLevel;
// 64x64
2023-02-19 11:41:54 +01:00
int128 public immutable decayLevel;
2021-06-08 16:38:45 +02:00
// Addresses allowed to mint new tokens
mapping (address => bool) minter;
// Storage for ERC20 approve/transferFrom methods
mapping (address => mapping (address => uint256 ) ) allowance; // holder -> spender -> amount (amount is subject to demurrage)
// Address to send unallocated redistribution tokens
2022-05-30 09:53:22 +02:00
address public sinkAddress;
// timestamp when token contract expires
uint256 public expires;
bool expired;
2023-02-10 16:58:31 +01:00
// supply xap
uint256 public maxSupply;
// Implements ERC20
event Transfer(address indexed _from, address indexed _to, uint256 _value);
// Implements ERC20
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
2023-03-26 08:26:02 +02:00
// Implements Minter
event Mint(address indexed _minter, address indexed _beneficiary, uint256 _value);
// New demurrage cache milestone calculated
2023-02-09 12:44:20 +01:00
event Decayed(uint256 indexed _period, uint256 indexed _periodCount, int128 indexed _oldAmount, int128 _newAmount);
// When a new period threshold has been crossed
event Period(uint256 _period);
// Redistribution applied on a single eligible account
event Redistribution(address indexed _account, uint256 indexed _period, uint256 _value);
// Temporary event used in development, will be removed on prod
2023-02-10 06:02:24 +01:00
//event Debug(bytes32 _foo);
event Debug(int128 indexed _foo, uint256 indexed _bar);
2023-03-26 08:26:02 +02:00
// Implements Burn
2023-02-04 15:41:40 +01:00
event Burn(address indexed _burner, uint256 _value);
// EIP173
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); // EIP173
2023-03-26 08:26:02 +02:00
// Implements Expire
event Expired(uint256 _timestamp);
2023-03-26 09:03:44 +02:00
// Implements Expire
event ExpiryChange(uint256 indexed _oldTimestamp, uint256 _newTimestamp);
2023-02-10 16:58:31 +01:00
event Cap(uint256 indexed _oldCap, uint256 _newCap);
2023-03-26 08:26:02 +02:00
// Implements Seal
uint256 public sealState;
uint8 constant WRITER_STATE = 1;
uint8 constant SINK_STATE = 2;
uint8 constant EXPIRY_STATE = 4;
uint8 constant CAP_STATE = 8;
2023-03-26 08:26:02 +02:00
// Implements Seal
uint256 constant public maxSealState = 15;
2023-03-26 08:26:02 +02:00
// Implements Seal
2023-02-19 09:07:25 +01:00
event SealStateChange(bool indexed _final, uint256 _sealState);
2023-02-19 11:41:54 +01:00
constructor(string memory _name, string memory _symbol, uint8 _decimals, int128 _decayLevel, uint256 _periodMinutes, address _defaultSinkAddress) {
require(_decayLevel < (1 << 64));
2023-02-08 09:44:37 +01:00
redistributionItem memory initialRedistribution;
2023-02-19 11:41:54 +01:00
//require(ABDKMath64x64.toUInt(_decayLevel) == 0);
// ACL setup
owner = msg.sender;
// ERC20 setup
name = _name;
symbol = _symbol;
decimals = _decimals;
// Demurrage setup
2021-06-08 16:38:45 +02:00
demurrageTimestamp = block.timestamp;
periodStart = demurrageTimestamp;
periodDuration = _periodMinutes * 60;
demurrageAmount = ABDKMath64x64.fromUInt(1);
2023-02-19 11:41:54 +01:00
decayLevel = ABDKMath64x64.ln(_decayLevel);
2023-02-10 06:02:24 +01:00
initialRedistribution = toRedistribution(0, demurrageAmount, 0, 1);
redistributions.push(initialRedistribution);
// Misc settings
sinkAddress = _defaultSinkAddress;
}
function seal(uint256 _state) public returns(uint256) {
2023-02-10 16:21:37 +01:00
require(_state < 16, 'ERR_INVALID_STATE');
require(_state & sealState == 0, 'ERR_ALREADY_LOCKED');
sealState |= _state;
2023-02-19 09:07:25 +01:00
emit SealStateChange(sealState == maxSealState, sealState);
return uint256(sealState);
}
2023-02-19 09:07:25 +01:00
function isSealed(uint256 _state) public view returns(bool) {
require(_state < maxSealState);
if (_state == 0) {
return sealState == maxSealState;
}
return _state & sealState == _state;
}
2023-03-26 08:26:02 +02:00
// Set when token expires.
// Value is set it terms of redistribution periods.
// Cannot be set to a time in the past.
2023-02-10 15:30:25 +01:00
function setExpirePeriod(uint256 _expirePeriod) public {
2023-02-10 16:58:31 +01:00
uint256 r;
2023-03-26 09:03:44 +02:00
uint256 oldTimestamp;
2023-02-10 16:58:31 +01:00
require(!isSealed(EXPIRY_STATE));
require(!expired);
require(msg.sender == owner);
2023-02-10 16:58:31 +01:00
r = periodStart + (_expirePeriod * periodDuration);
require(r > expires);
2023-03-26 09:03:44 +02:00
oldTimestamp = expires;
2023-02-10 16:58:31 +01:00
expires = r;
2023-03-26 09:03:44 +02:00
emit ExpiryChange(oldTimestamp, expires);
2023-02-10 16:58:31 +01:00
}
2023-03-26 08:26:02 +02:00
// Change max token supply.
// Can only increase supply cap, not decrease.
2023-02-10 16:58:31 +01:00
function setMaxSupply(uint256 _cap) public {
require(!isSealed(CAP_STATE));
require(msg.sender == owner);
require(_cap > totalSupply());
2023-02-10 16:58:31 +01:00
emit Cap(maxSupply, _cap);
maxSupply = _cap;
}
// Change sink address for redistribution
function setSinkAddress(address _sinkAddress) public {
require(!isSealed(SINK_STATE));
require(msg.sender == owner);
sinkAddress = _sinkAddress;
}
2023-02-10 15:30:25 +01:00
// Expire the contract if expire is set and we have gone over the threshold.
// Finalizes demurrage up to the timestamp of the expiry.
// The first approve, transfer or transferFrom call that hits the ex == 2 will get the tx mined. but without the actual effect. Otherwise we would have to wait until an external egent called applyExpiry to get the correct final balance.
2023-03-26 08:34:27 +02:00
// Implements Expire
2023-02-10 14:44:35 +01:00
function applyExpiry() public returns(uint8) {
if (expired) {
2023-02-10 14:44:35 +01:00
return 1;
}
if (expires == 0) {
2023-02-10 14:44:35 +01:00
return 0;
}
if (block.timestamp >= expires) {
2023-02-10 14:44:35 +01:00
applyDemurrageLimited(expires - demurrageTimestamp / 60);
expired = true;
emit Expired(block.timestamp);
2023-02-10 14:44:35 +01:00
changePeriod();
return 2;
}
2023-02-10 14:44:35 +01:00
return 0;
}
// Given address will be allowed to call the mintTo() function
2023-03-26 08:26:02 +02:00
// Implements Writer
2023-02-12 09:31:08 +01:00
function addWriter(address _minter) public returns (bool) {
require(!isSealed(WRITER_STATE));
require(msg.sender == owner);
minter[_minter] = true;
return true;
}
// Given address will no longer be allowed to call the mintTo() function
2023-03-26 08:26:02 +02:00
// Implements Writer
2023-02-12 09:31:08 +01:00
function deleteWriter(address _minter) public returns (bool) {
require(!isSealed(WRITER_STATE));
require(msg.sender == owner || _minter == msg.sender);
minter[_minter] = false;
return true;
}
2023-03-26 08:26:02 +02:00
// Implements Writer
function isWriter(address _minter) public view returns(bool) {
return minter[_minter];
}
/// Implements ERC20
function balanceOf(address _account) public view returns (uint256) {
int128 baseBalance;
int128 currentDemurragedAmount;
uint256 periodCount;
baseBalance = ABDKMath64x64.fromUInt(baseBalanceOf(_account));
2021-06-08 16:38:45 +02:00
periodCount = getMinutesDelta(demurrageTimestamp);
currentDemurragedAmount = ABDKMath64x64.mul(baseBalance, demurrageAmount);
return decayBy(ABDKMath64x64.toUInt(currentDemurragedAmount), periodCount);
}
// Balance unmodified by demurrage
function baseBalanceOf(address _account) public view returns (uint256) {
return account[_account];
}
/// Increases base balance for a single account
function increaseBaseBalance(address _account, uint256 _delta) private returns (bool) {
uint256 oldBalance;
uint256 workAccount;
workAccount = uint256(account[_account]);
if (_delta == 0) {
return false;
}
oldBalance = baseBalanceOf(_account);
account[_account] = oldBalance + _delta;
return true;
}
/// Decreases base balance for a single account
function decreaseBaseBalance(address _account, uint256 _delta) private returns (bool) {
uint256 oldBalance;
uint256 workAccount;
workAccount = uint256(account[_account]);
if (_delta == 0) {
return false;
}
oldBalance = baseBalanceOf(_account);
require(oldBalance >= _delta, 'ERR_OVERSPEND'); // overspend guard
account[_account] = oldBalance - _delta;
return true;
}
2023-02-19 10:44:25 +01:00
// Send full balance of one account to another
function sweep(address _account) public returns (uint256) {
uint256 v;
v = account[msg.sender];
account[msg.sender] = 0;
account[_account] += v;
return v;
}
// Creates new tokens out of thin air, and allocates them to the given address
// Triggers tax
2023-03-26 08:26:02 +02:00
// Implements Minter
function mintTo(address _beneficiary, uint256 _amount) public returns (bool) {
uint256 baseAmount;
2023-02-10 14:44:35 +01:00
require(applyExpiry() == 0);
2023-02-19 09:07:25 +01:00
require(minter[msg.sender] || msg.sender == owner, 'ERR_ACCESS');
changePeriod();
2023-02-10 16:58:31 +01:00
if (maxSupply > 0) {
require(supply + _amount <= maxSupply);
}
2023-02-04 15:41:40 +01:00
supply += _amount;
baseAmount = toBaseAmount(_amount);
increaseBaseBalance(_beneficiary, baseAmount);
emit Mint(msg.sender, _beneficiary, _amount);
saveRedistributionSupply();
return true;
}
2023-03-26 08:26:02 +02:00
// Implements Minter
function mint(address _beneficiary, uint256 _amount, bytes calldata _data) public {
_data;
mintTo(_beneficiary, _amount);
}
// Implements Minter
function safeMint(address _beneficiary, uint256 _amount, bytes calldata _data) public {
_data;
mintTo(_beneficiary, _amount);
}
// Deserializes the redistribution word
2023-02-10 06:02:24 +01:00
function toRedistribution(uint256 _participants, int128 _demurrageModifier, uint256 _value, uint256 _period) public pure returns(redistributionItem memory) {
2023-02-08 09:44:37 +01:00
redistributionItem memory redistribution;
2023-02-08 09:44:37 +01:00
redistribution.period = uint32(_period);
redistribution.value = uint72(_value);
2023-02-10 06:02:24 +01:00
redistribution.demurrage = uint64(uint128(_demurrageModifier) & 0xffffffffffffffff);
_participants;
return redistribution;
2023-02-08 09:44:37 +01:00
}
// Serializes the demurrage period part of the redistribution word
function toRedistributionPeriod(redistributionItem memory _redistribution) public pure returns (uint256) {
return uint256(_redistribution.period);
}
// Serializes the supply part of the redistribution word
function toRedistributionSupply(redistributionItem memory _redistribution) public pure returns (uint256) {
return uint256(_redistribution.value);
}
// Serializes the number of participants part of the redistribution word
2023-02-10 06:02:24 +01:00
function toRedistributionDemurrageModifier(redistributionItem memory _redistribution) public pure returns (int128) {
int128 r;
r = int128(int64(_redistribution.demurrage) & int128(0x0000000000000000ffffffffffffffff));
if (r == 0) {
r = ABDKMath64x64.fromUInt(1);
}
return r;
}
// Client accessor to the redistributions array length
function redistributionCount() public view returns (uint256) {
return redistributions.length;
}
// Save the current total supply amount to the current redistribution period
function saveRedistributionSupply() private returns (bool) {
redistributionItem memory currentRedistribution;
2021-06-08 16:38:45 +02:00
uint256 grownSupply;
2023-02-04 15:41:40 +01:00
grownSupply = totalSupply();
currentRedistribution = redistributions[redistributions.length-1];
currentRedistribution.value = uint72(grownSupply);
redistributions[redistributions.length-1] = currentRedistribution;
return true;
}
// Get the demurrage period of the current block number
function actualPeriod() public view returns (uint128) {
return uint128((block.timestamp - periodStart) / periodDuration + 1);
}
2023-02-04 15:41:40 +01:00
// Retrieve next redistribution if the period threshold has been crossed
2023-02-10 06:02:24 +01:00
function checkPeriod() private view returns (redistributionItem memory) {
redistributionItem memory lastRedistribution;
redistributionItem memory emptyRedistribution;
uint256 currentPeriod;
2022-05-30 09:53:22 +02:00
lastRedistribution = redistributions[lastPeriod];
currentPeriod = this.actualPeriod();
if (currentPeriod <= toRedistributionPeriod(lastRedistribution)) {
2023-02-10 06:02:24 +01:00
return emptyRedistribution;
}
return lastRedistribution;
}
2023-03-19 10:23:51 +01:00
function getDistribution(uint256 _supply, int128 _demurrageAmount) public pure returns (uint256) {
2023-02-10 06:02:24 +01:00
int128 difference;
2021-06-08 16:38:45 +02:00
2023-02-10 07:21:32 +01:00
difference = ABDKMath64x64.mul(ABDKMath64x64.fromUInt(_supply), ABDKMath64x64.sub(ABDKMath64x64.fromUInt(1), _demurrageAmount));
return _supply - ABDKMath64x64.toUInt(difference);
2023-02-10 06:02:24 +01:00
2021-06-08 16:38:45 +02:00
}
2023-03-19 10:23:51 +01:00
function getDistributionFromRedistribution(redistributionItem memory _redistribution) public pure returns (uint256) {
uint256 redistributionSupply;
2023-02-10 06:02:24 +01:00
int128 redistributionDemurrage;
redistributionSupply = toRedistributionSupply(_redistribution);
2021-06-08 16:38:45 +02:00
redistributionDemurrage = toRedistributionDemurrageModifier(_redistribution);
return getDistribution(redistributionSupply, redistributionDemurrage);
}
2021-06-08 16:38:45 +02:00
// Returns the amount sent to the sink address
2023-02-10 06:02:24 +01:00
function applyDefaultRedistribution(redistributionItem memory _redistribution) private returns (uint256) {
2021-06-08 16:38:45 +02:00
uint256 unit;
2022-05-30 09:53:22 +02:00
uint256 baseUnit;
2021-06-08 16:38:45 +02:00
2023-02-10 10:06:04 +01:00
unit = totalSupply() - getDistributionFromRedistribution(_redistribution);
2022-05-30 09:53:22 +02:00
baseUnit = toBaseAmount(unit) - totalSink;
increaseBaseBalance(sinkAddress, baseUnit);
2023-02-10 06:02:24 +01:00
emit Redistribution(sinkAddress, _redistribution.period, unit);
2022-05-30 09:53:22 +02:00
lastPeriod += 1;
totalSink += baseUnit;
return unit;
}
2023-02-10 06:02:24 +01:00
// 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.
function changePeriod() public returns (bool) {
redistributionItem memory currentRedistribution;
redistributionItem memory nextRedistribution;
redistributionItem memory lastRedistribution;
uint256 currentPeriod;
int128 lastDemurrageAmount;
int128 nextRedistributionDemurrage;
uint256 demurrageCounts;
uint256 nextPeriod;
applyDemurrage();
currentRedistribution = checkPeriod();
if (isEmptyRedistribution(currentRedistribution)) {
return false;
}
// calculate the decay from previous redistributino
lastRedistribution = redistributions[lastPeriod];
currentPeriod = toRedistributionPeriod(currentRedistribution);
nextPeriod = currentPeriod + 1;
lastDemurrageAmount = toRedistributionDemurrageModifier(lastRedistribution);
2023-02-10 10:06:04 +01:00
demurrageCounts = (periodDuration * currentPeriod) / 60;
// TODO refactor decayby to take int128 then DRY with it
2023-02-19 11:41:54 +01:00
nextRedistributionDemurrage = ABDKMath64x64.exp(ABDKMath64x64.mul(decayLevel, ABDKMath64x64.fromUInt(demurrageCounts)));
2023-02-10 06:02:24 +01:00
nextRedistribution = toRedistribution(0, nextRedistributionDemurrage, totalSupply(), nextPeriod);
redistributions.push(nextRedistribution);
applyDefaultRedistribution(nextRedistribution);
emit Period(nextPeriod);
return true;
}
2021-06-08 16:38:45 +02:00
// Calculate the time delta in whole minutes passed between given timestamp and current timestamp
function getMinutesDelta(uint256 _lastTimestamp) public view returns (uint256) {
return (block.timestamp - _lastTimestamp) / 60;
}
// Calculate and cache the demurrage value corresponding to the (period of the) time of the method call
2023-02-10 14:44:35 +01:00
function applyDemurrage() public returns (uint256) {
return applyDemurrageLimited(0);
}
// returns true if expired
2023-02-10 14:44:35 +01:00
function applyDemurrageLimited(uint256 _rounds) public returns (uint256) {
2023-02-09 12:44:20 +01:00
int128 v;
2021-06-08 16:38:45 +02:00
uint256 periodCount;
2023-02-09 12:44:20 +01:00
int128 periodPoint;
int128 lastDemurrageAmount;
2023-02-10 14:44:35 +01:00
if (expired) {
return 0;
}
2021-06-08 16:38:45 +02:00
periodCount = getMinutesDelta(demurrageTimestamp);
if (periodCount == 0) {
2023-02-10 14:44:35 +01:00
return 0;
}
lastDemurrageAmount = demurrageAmount;
// safety limit for exponential calculation to ensure that we can always
// execute this code no matter how much time passes.
if (_rounds > 0 && _rounds < periodCount) {
periodCount = _rounds;
}
2023-02-09 12:44:20 +01:00
periodPoint = ABDKMath64x64.fromUInt(periodCount);
2023-02-19 11:41:54 +01:00
v = ABDKMath64x64.mul(decayLevel, periodPoint);
2023-02-09 12:44:20 +01:00
v = ABDKMath64x64.exp(v);
demurrageAmount = ABDKMath64x64.mul(demurrageAmount, v);
2023-02-10 10:06:04 +01:00
2021-06-08 16:38:45 +02:00
demurrageTimestamp = demurrageTimestamp + (periodCount * 60);
emit Decayed(demurrageTimestamp, periodCount, lastDemurrageAmount, demurrageAmount);
2023-02-10 14:44:35 +01:00
return periodCount;
}
// Return timestamp of start of period threshold
function getPeriodTimeDelta(uint256 _periodCount) public view returns (uint256) {
return periodStart + (_periodCount * periodDuration);
}
// Amount of demurrage cycles inbetween the current timestamp and the given target time
function demurrageCycles(uint256 _target) public view returns (uint256) {
return (block.timestamp - _target) / 60;
}
2023-03-26 08:26:02 +02:00
// Equality check for empty redistribution data
function isEmptyRedistribution(redistributionItem memory _redistribution) public pure returns(bool) {
if (_redistribution.period > 0) {
return false;
}
if (_redistribution.value > 0) {
return false;
}
if (_redistribution.demurrage > 0) {
return false;
}
return true;
}
// Calculate a value reduced by demurrage by the given period
function decayBy(uint256 _value, uint256 _period) public view returns (uint256) {
int128 valuePoint;
int128 periodPoint;
int128 v;
valuePoint = ABDKMath64x64.fromUInt(_value);
periodPoint = ABDKMath64x64.fromUInt(_period);
2023-02-08 09:44:37 +01:00
2023-02-19 11:41:54 +01:00
v = ABDKMath64x64.mul(decayLevel, periodPoint);
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) {
int128 r;
2023-02-09 13:08:28 +01:00
r = ABDKMath64x64.div(ABDKMath64x64.fromUInt(_value), demurrageAmount);
return ABDKMath64x64.toUInt(r);
}
2023-03-26 08:26:02 +02:00
// Triggers tax and/or redistribution
// Implements ERC20
function approve(address _spender, uint256 _value) public returns (bool) {
uint256 baseValue;
2023-02-10 14:44:35 +01:00
uint8 ex;
2023-02-10 14:44:35 +01:00
ex = applyExpiry();
if (ex == 2) {
return false;
} else if (ex > 0) {
revert('EXPIRED');
}
2023-02-04 15:41:40 +01:00
if (allowance[msg.sender][_spender] > 0) {
require(_value == 0, 'ZERO_FIRST');
}
changePeriod();
baseValue = toBaseAmount(_value);
2023-02-04 15:41:40 +01:00
allowance[msg.sender][_spender] = baseValue;
emit Approval(msg.sender, _spender, _value);
return true;
}
2023-02-04 15:41:40 +01:00
// Reduce allowance by amount
function decreaseAllowance(address _spender, uint256 _value) public returns (bool) {
uint256 baseValue;
baseValue = toBaseAmount(_value);
require(allowance[msg.sender][_spender] >= baseValue);
changePeriod();
allowance[msg.sender][_spender] -= baseValue;
emit Approval(msg.sender, _spender, allowance[msg.sender][_spender]);
return true;
}
// Increase allowance by amount
function increaseAllowance(address _spender, uint256 _value) public returns (bool) {
uint256 baseValue;
changePeriod();
baseValue = toBaseAmount(_value);
allowance[msg.sender][_spender] += baseValue;
emit Approval(msg.sender, _spender, allowance[msg.sender][_spender]);
return true;
}
2023-03-26 08:26:02 +02:00
// Triggers tax and/or redistribution
// Implements ERC20
function transfer(address _to, uint256 _value) public returns (bool) {
uint256 baseValue;
bool result;
2023-02-10 14:44:35 +01:00
uint8 ex;
2023-02-10 14:44:35 +01:00
ex = applyExpiry();
if (ex == 2) {
return false;
} else if (ex > 0) {
revert('EXPIRED');
}
changePeriod();
baseValue = toBaseAmount(_value);
result = transferBase(msg.sender, _to, baseValue);
emit Transfer(msg.sender, _to, _value);
return result;
}
2023-03-26 08:26:02 +02:00
// Triggers tax and/or redistribution
// Implements ERC20
function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {
uint256 baseValue;
bool result;
2023-02-10 14:44:35 +01:00
uint8 ex;
2023-02-10 14:44:35 +01:00
ex = applyExpiry();
if (ex == 2) {
return false;
} else if (ex > 0) {
revert('EXPIRED');
}
changePeriod();
baseValue = toBaseAmount(_value);
require(allowance[_from][msg.sender] >= baseValue);
2023-02-04 15:41:40 +01:00
allowance[_from][msg.sender] -= baseValue;
result = transferBase(_from, _to, baseValue);
2023-02-04 15:41:40 +01:00
emit Transfer(_from, _to, _value);
return result;
}
// ERC20 transfer backend for transfer, transferFrom
function transferBase(address _from, address _to, uint256 _value) private returns (bool) {
decreaseBaseBalance(_from, _value);
increaseBaseBalance(_to, _value);
return true;
}
// Implements EIP173
function transferOwnership(address _newOwner) public returns (bool) {
address oldOwner;
2023-03-26 08:26:02 +02:00
require(msg.sender == owner);
oldOwner = owner;
owner = _newOwner;
emit OwnershipTransferred(oldOwner, owner);
2023-03-19 10:23:51 +01:00
return true;
}
2023-02-04 15:41:40 +01:00
// Explicitly and irretrievably burn tokens
// Only token minters can burn tokens
2023-03-26 08:26:02 +02:00
// Implements Burner
function burn(uint256 _value) public returns(bool) {
2023-02-10 14:44:35 +01:00
require(applyExpiry() == 0);
2023-03-22 11:17:40 +01:00
require(minter[msg.sender] || msg.sender == owner, 'ERR_ACCESS');
2023-02-04 15:41:40 +01:00
require(_value <= account[msg.sender]);
uint256 _delta = toBaseAmount(_value);
//applyDemurrage();
2023-02-04 15:41:40 +01:00
decreaseBaseBalance(msg.sender, _delta);
burned += _value;
emit Burn(msg.sender, _value);
2023-03-22 08:58:25 +01:00
return true;
2023-02-04 15:41:40 +01:00
}
2023-03-26 08:26:02 +02:00
// Implements Burner
function burn(address _from, uint256 _value, bytes calldata _data) public {
require(_from == msg.sender, 'ERR_ONLY_SELF_BURN');
_data;
burn(_value);
}
// Implements Burner
function burn() public returns(bool) {
return burn(account[msg.sender]);
}
2023-02-04 15:41:40 +01:00
// Implements ERC20
function totalSupply() public view returns (uint256) {
return supply - burned;
}
// Return total number of burned tokens
2023-03-26 08:26:02 +02:00
// Implements Burner
2023-02-04 15:41:40 +01:00
function totalBurned() public view returns (uint256) {
return burned;
}
2023-03-22 08:58:25 +01:00
// Return total number of tokens ever minted
2023-03-26 08:26:02 +02:00
// Implements Burner
2023-03-22 08:58:25 +01:00
function totalMinted() public view returns (uint256) {
return supply;
}
// Implements EIP165
function supportsInterface(bytes4 _sum) public pure returns (bool) {
2023-03-26 08:26:02 +02:00
if (_sum == 0xb61bc941) { // ERC20
return true;
}
if (_sum == 0x5878bcf4) { // Minter
return true;
}
if (_sum == 0xbc4babdd) { // Burner
return true;
}
if (_sum == 0x0d7491f8) { // Seal
return true;
}
if (_sum == 0xabe1f1f5) { // Writer
return true;
}
2023-03-26 08:34:27 +02:00
if (_sum == 0x841a0e94) { // Expire
return true;
}
2023-03-26 08:26:02 +02:00
if (_sum == 0x01ffc9a7) { // ERC165
return true;
}
2023-03-26 08:26:02 +02:00
if (_sum == 0x9493f8b2) { // ERC173
return true;
}
2023-03-26 08:26:02 +02:00
if (_sum == 0xd0017968) { // ERC5678Ext20
return true;
}
return false;
}
}