This repository has been archived on 2023-03-01. You can view files and clone it, but cannot push or open issues or pull requests.
erc20-demurrage-token-archive/solidity/RedistributedDemurrageToken.sol

248 lines
8.3 KiB
Solidity
Raw Normal View History

2021-02-02 11:26:44 +01:00
pragma solidity > 0.6.11;
// SPDX-License-Identifier: GPL-3.0-or-later
contract RedistributedDemurrageToken {
address public owner;
uint256 public decimals;
string public name;
string public symbol;
uint256 public totalSupply;
uint256 public periodStart;
uint256 public periodDuration;
2021-02-02 16:10:21 +01:00
uint256 public taxLevel; // PPM
uint256 public demurrageModifier; // PPM
2021-02-02 11:26:44 +01:00
bytes32[] public redistributions; // uint40(participants) uint160(value) uint56(period)
2021-02-02 16:10:21 +01:00
mapping (address => bytes32) account;
mapping (address => bool) minter;
2021-02-02 11:26:44 +01:00
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
2021-02-02 16:10:21 +01:00
event Mint(address indexed _minter, address indexed _beneficiary, uint256 _amount);
2021-02-02 19:09:13 +01:00
event Debug(uint256 _foo);
2021-02-02 21:14:41 +01:00
event Taxed(uint256 indexed _period);
event Redistribution(address indexed _account, uint256 indexed _period, uint256 _amount);
2021-02-02 11:26:44 +01:00
constructor(string memory _name, string memory _symbol, uint32 _taxLevel, uint256 _period) {
owner = msg.sender;
2021-02-02 16:10:21 +01:00
minter[owner] = true;
2021-02-02 11:26:44 +01:00
periodStart = block.number;
periodDuration = _period;
taxLevel = _taxLevel;
name = _name;
symbol = _symbol;
decimals = 6;
2021-02-02 16:10:21 +01:00
demurrageModifier = 1000000;
bytes32 initialRedistribution = toRedistribution(0, 0, 1);
2021-02-02 11:26:44 +01:00
redistributions.push(initialRedistribution);
}
2021-02-02 16:10:21 +01:00
function addMinter(address _minter) public returns (bool) {
require(msg.sender == owner);
minter[_minter] = true;
return true;
}
function balanceOf(address _account) public view returns (uint256) {
uint256 baseBalance = getBaseBalance(_account);
uint256 inverseModifier = 1000000 - demurrageModifier;
uint256 balanceModifier = (inverseModifier * baseBalance) / 1000000;
return baseBalance - balanceModifier;
}
function getBaseBalance(address _account) private view returns (uint256) {
return uint256(account[_account]) & 0x00ffffffffffffffffffffffffffffffffffffffff;
}
function increaseBalance(address _account, uint256 _delta) private returns (bool) {
2021-02-02 21:14:41 +01:00
uint256 oldBalance;
uint256 newBalance;
oldBalance = getBaseBalance(_account);
newBalance = oldBalance + _delta;
account[_account] &= bytes32(0xffffffffffffffffffffffff0000000000000000000000000000000000000000);
account[_account] |= bytes32(newBalance & 0x00ffffffffffffffffffffffffffffffffffffffff);
2021-02-02 16:10:21 +01:00
return true;
}
function decreaseBalance(address _account, uint256 _delta) private returns (bool) {
2021-02-02 21:14:41 +01:00
uint256 oldBalance;
uint256 newBalance;
oldBalance = getBaseBalance(_account);
2021-02-02 19:09:13 +01:00
require(oldBalance >= _delta);
2021-02-02 21:14:41 +01:00
newBalance = oldBalance - _delta;
account[_account] &= bytes32(0xffffffffffffffffffffffff0000000000000000000000000000000000000000);
account[_account] |= bytes32(newBalance & 0x00ffffffffffffffffffffffffffffffffffffffff);
2021-02-02 16:10:21 +01:00
return true;
}
function mintTo(address _beneficiary, uint256 _amount) external returns (bool) {
require(minter[msg.sender]);
2021-02-02 19:09:13 +01:00
// TODO: get base amount for minting
applyTax();
totalSupply += _amount;
2021-02-02 16:10:21 +01:00
increaseBalance(_beneficiary, _amount);
emit Mint(msg.sender, _beneficiary, _amount);
saveRedistributionSupply();
2021-02-02 16:10:21 +01:00
return true;
}
2021-02-02 11:26:44 +01:00
function toRedistribution(uint256 _participants, uint256 _value, uint256 _period) private pure returns(bytes32) {
bytes32 redistribution;
redistribution |= bytes32((_participants & 0xffffffffff) << 216);
redistribution |= bytes32((_value & 0xffffffffffffffffffffffff) << 56);
redistribution |= bytes32(_period & 0xffffffffffffff);
2021-02-02 11:26:44 +01:00
return redistribution;
}
function toRedistributionPeriod(bytes32 redistribution) public pure returns (uint256) {
2021-02-02 19:09:13 +01:00
return uint256(redistribution & 0x00000000000000000000000000000000000000000000000000ffffffffffffff);
2021-02-02 11:26:44 +01:00
}
2021-02-02 21:14:41 +01:00
function toRedistributionSupply(bytes32 redistribution) public pure returns (uint256) {
return uint256(redistribution & 0x0000000000ffffffffffffffffffffffffffffffffffffffff00000000000000) >> 56;
}
function toRedistributionParticipants(bytes32 redistribution) public pure returns (uint256) {
return uint256(redistribution & 0xffffffffff000000000000000000000000000000000000000000000000000000) >> 216;
}
2021-02-02 11:26:44 +01:00
function redistributionCount() public view returns (uint256) {
return redistributions.length;
}
function incrementRedistributionParticipants() private returns (bool) {
uint256 currentRedistribution;
uint256 participants;
currentRedistribution = uint256(redistributions[redistributions.length-1]);
participants = ((currentRedistribution & 0xffffffffff000000000000000000000000000000000000000000000000000000) >> 216) + 1;
currentRedistribution &= 0x0000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffff;
currentRedistribution |= participants << 216;
2021-02-02 19:09:13 +01:00
emit Debug(participants);
redistributions[redistributions.length-1] = bytes32(currentRedistribution);
}
function saveRedistributionSupply() private returns (bool) {
uint256 currentRedistribution;
currentRedistribution = uint256(redistributions[redistributions.length-1]);
currentRedistribution &= 0xffffffffff0000000000000000000000000000000000000000ffffffffffffff;
2021-02-02 19:09:13 +01:00
currentRedistribution |= totalSupply << 56;
redistributions[redistributions.length-1] = bytes32(currentRedistribution);
}
2021-02-02 11:26:44 +01:00
function actualPeriod() public view returns (uint256) {
2021-02-02 19:09:13 +01:00
return (block.number - periodStart) / periodDuration + 1;
2021-02-02 11:26:44 +01:00
}
function checkPeriod() private view returns (bytes32) {
2021-02-02 21:14:41 +01:00
bytes32 lastRedistribution;
uint256 currentPeriod;
lastRedistribution = redistributions[redistributions.length-1];
currentPeriod = this.actualPeriod();
2021-02-02 19:09:13 +01:00
if (currentPeriod <= toRedistributionPeriod(lastRedistribution)) {
2021-02-02 11:26:44 +01:00
return bytes32(0x00);
}
return lastRedistribution;
}
2021-02-02 21:14:41 +01:00
function accountPeriod(address _account) public returns (uint256) {
return (uint256(account[_account]) & 0xffffffffffffffffffffffff0000000000000000000000000000000000000000) >> 160;
}
function registerAccountPeriod(address _account, uint256 _period) private returns (bool) {
account[_account] &= 0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff;
account[_account] |= bytes32(_period << 160);
incrementRedistributionParticipants();
}
2021-02-02 11:26:44 +01:00
function applyTax() public returns (uint256) {
bytes32 pendingRedistribution;
bytes32 nextRedistribution;
2021-02-02 16:10:21 +01:00
uint256 currentPeriod;
2021-02-02 11:26:44 +01:00
pendingRedistribution = checkPeriod();
if (pendingRedistribution == bytes32(0x00)) {
2021-02-02 19:09:13 +01:00
return demurrageModifier;
2021-02-02 11:26:44 +01:00
}
2021-02-02 16:10:21 +01:00
demurrageModifier -= (demurrageModifier * taxLevel) / 1000000;
currentPeriod = toRedistributionPeriod(pendingRedistribution);
2021-02-02 19:09:13 +01:00
nextRedistribution = toRedistribution(0, totalSupply, currentPeriod + 1);
2021-02-02 11:26:44 +01:00
redistributions.push(nextRedistribution);
2021-02-02 21:14:41 +01:00
emit Taxed(currentPeriod);
2021-02-02 11:26:44 +01:00
return demurrageModifier;
}
2021-02-02 21:14:41 +01:00
function toReward(address _amount, uint256 _period) public view returns (uint256) {
return 1000000 * (((1000000-taxLevel)/1000000) ** _period);
2021-02-02 16:46:27 +01:00
}
2021-02-02 21:14:41 +01:00
function applyRedistributionOnAccount(address _account) public returns (bool) {
bytes32 periodRedistribution;
uint256 supply;
uint256 participants;
uint256 value;
uint256 period;
period = accountPeriod(_account);
if (period >= actualPeriod()) {
return false;
}
participants = toRedistributionParticipants(periodRedistribution);
if (participants == 0) {
// TODO: In this case we need to give back to everyone, so we need a total accounts counter
revert('0 participants');
return true;
}
supply = toRedistributionSupply(periodRedistribution);
value = supply / participants;
account[_account] &= bytes32(0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff);
increaseBalance(_account, value);
emit Redistribution(_account, period, value);
return true;
2021-02-02 16:46:27 +01:00
}
function transfer(address _to, uint256 _value) public returns (bool) {
uint256 baseValue;
bool result;
2021-02-02 19:09:13 +01:00
applyTax();
2021-02-02 21:14:41 +01:00
// TODO: Prefer to truncate the result, instead it seems to round to nearest :/
2021-02-02 16:46:27 +01:00
baseValue = (_value * 1000000) / demurrageModifier;
result = transferBase(msg.sender, _to, baseValue);
2021-02-02 21:14:41 +01:00
applyRedistributionOnAccount(msg.sender);
2021-02-02 16:10:21 +01:00
return result;
}
2021-02-02 16:46:27 +01:00
function transferBase(address _from, address _to, uint256 _value) private returns (bool) {
uint256 period;
if (!decreaseBalance(msg.sender, _value)) {
revert('ERR_TX_DECREASEBALANCE');
}
if (!increaseBalance(_to, _value)) {
revert('ERR_TX_INCREASEBALANCE');
}
period = actualPeriod();
if (accountPeriod(_from) != period) {
registerAccountPeriod(_from, period);
2021-02-02 16:46:27 +01:00
}
2021-02-02 16:10:21 +01:00
return true;
2021-02-02 11:26:44 +01:00
}
}