Add redistribution code

This commit is contained in:
nolash 2021-02-02 21:14:41 +01:00
parent 42d7212a08
commit b5d30e12ef
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746

View File

@ -23,6 +23,8 @@ contract RedistributedDemurrageToken {
event Approval(address indexed _owner, address indexed _spender, uint256 _value); event Approval(address indexed _owner, address indexed _spender, uint256 _value);
event Mint(address indexed _minter, address indexed _beneficiary, uint256 _amount); event Mint(address indexed _minter, address indexed _beneficiary, uint256 _amount);
event Debug(uint256 _foo); event Debug(uint256 _foo);
event Taxed(uint256 indexed _period);
event Redistribution(address indexed _account, uint256 indexed _period, uint256 _amount);
constructor(string memory _name, string memory _symbol, uint32 _taxLevel, uint256 _period) { constructor(string memory _name, string memory _symbol, uint32 _taxLevel, uint256 _period) {
owner = msg.sender; owner = msg.sender;
@ -56,17 +58,25 @@ contract RedistributedDemurrageToken {
} }
function increaseBalance(address _account, uint256 _delta) private returns (bool) { function increaseBalance(address _account, uint256 _delta) private returns (bool) {
uint256 oldBalance = getBaseBalance(_account); uint256 oldBalance;
account[_account] &= bytes20(0x00); uint256 newBalance;
account[_account] |= bytes32((oldBalance + _delta) & 0x00ffffffffffffffffffffffffffffffffffffffff);
oldBalance = getBaseBalance(_account);
newBalance = oldBalance + _delta;
account[_account] &= bytes32(0xffffffffffffffffffffffff0000000000000000000000000000000000000000);
account[_account] |= bytes32(newBalance & 0x00ffffffffffffffffffffffffffffffffffffffff);
return true; return true;
} }
function decreaseBalance(address _account, uint256 _delta) private returns (bool) { function decreaseBalance(address _account, uint256 _delta) private returns (bool) {
uint256 oldBalance = getBaseBalance(_account); uint256 oldBalance;
uint256 newBalance;
oldBalance = getBaseBalance(_account);
require(oldBalance >= _delta); require(oldBalance >= _delta);
account[_account] &= bytes20(0x00); newBalance = oldBalance - _delta;
account[_account] |= bytes32((oldBalance - _delta) & 0x00ffffffffffffffffffffffffffffffffffffffff); account[_account] &= bytes32(0xffffffffffffffffffffffff0000000000000000000000000000000000000000);
account[_account] |= bytes32(newBalance & 0x00ffffffffffffffffffffffffffffffffffffffff);
return true; return true;
} }
@ -95,6 +105,14 @@ contract RedistributedDemurrageToken {
return uint256(redistribution & 0x00000000000000000000000000000000000000000000000000ffffffffffffff); return uint256(redistribution & 0x00000000000000000000000000000000000000000000000000ffffffffffffff);
} }
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;
}
function redistributionCount() public view returns (uint256) { function redistributionCount() public view returns (uint256) {
return redistributions.length; return redistributions.length;
} }
@ -126,16 +144,28 @@ contract RedistributedDemurrageToken {
return (block.number - periodStart) / periodDuration + 1; return (block.number - periodStart) / periodDuration + 1;
} }
//function checkPeriod() private view returns (bytes32) {
function checkPeriod() private view returns (bytes32) { function checkPeriod() private view returns (bytes32) {
bytes32 lastRedistribution = redistributions[redistributions.length-1]; bytes32 lastRedistribution;
uint256 currentPeriod = this.actualPeriod(); uint256 currentPeriod;
lastRedistribution = redistributions[redistributions.length-1];
currentPeriod = this.actualPeriod();
if (currentPeriod <= toRedistributionPeriod(lastRedistribution)) { if (currentPeriod <= toRedistributionPeriod(lastRedistribution)) {
return bytes32(0x00); return bytes32(0x00);
} }
return lastRedistribution; return lastRedistribution;
} }
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();
}
function applyTax() public returns (uint256) { function applyTax() public returns (uint256) {
bytes32 pendingRedistribution; bytes32 pendingRedistribution;
bytes32 nextRedistribution; bytes32 nextRedistribution;
@ -149,28 +179,53 @@ contract RedistributedDemurrageToken {
currentPeriod = toRedistributionPeriod(pendingRedistribution); currentPeriod = toRedistributionPeriod(pendingRedistribution);
nextRedistribution = toRedistribution(0, totalSupply, currentPeriod + 1); nextRedistribution = toRedistribution(0, totalSupply, currentPeriod + 1);
redistributions.push(nextRedistribution); redistributions.push(nextRedistribution);
emit Taxed(currentPeriod);
return demurrageModifier; return demurrageModifier;
} }
function accountPeriod(address _account) public returns (uint256) { function toReward(address _amount, uint256 _period) public view returns (uint256) {
return (uint256(account[_account]) & 0xffffffffffffffffffffffff0000000000000000000000000000000000000000) >> 160; return 1000000 * (((1000000-taxLevel)/1000000) ** _period);
} }
function registerAccountPeriod(address _account, uint256 _period) private returns (bool) { function applyRedistributionOnAccount(address _account) public returns (bool) {
account[_account] &= 0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff; bytes32 periodRedistribution;
account[_account] |= bytes32(_period << 160); uint256 supply;
incrementRedistributionParticipants(); 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;
} }
function transfer(address _to, uint256 _value) public returns (bool) { function transfer(address _to, uint256 _value) public returns (bool) {
// TODO: Prefer to truncate the result, instead it seems to round to nearest :/
uint256 baseValue; uint256 baseValue;
bool result; bool result;
applyTax(); applyTax();
// TODO: Prefer to truncate the result, instead it seems to round to nearest :/
baseValue = (_value * 1000000) / demurrageModifier; baseValue = (_value * 1000000) / demurrageModifier;
result = transferBase(msg.sender, _to, baseValue); result = transferBase(msg.sender, _to, baseValue);
emit Transfer(msg.sender, _to, _value);
applyRedistributionOnAccount(msg.sender);
return result; return result;
} }