mirror of
git://holbrook.no/erc20-demurrage-token
synced 2024-11-29 02:36:46 +01:00
Add preliminary code comments
This commit is contained in:
parent
ea6990a84c
commit
3cecc77cd1
@ -15,8 +15,8 @@ contract RedistributedDemurrageToken {
|
|||||||
uint256 public taxLevel; // PPM
|
uint256 public taxLevel; // PPM
|
||||||
uint256 public demurrageModifier; // PPM
|
uint256 public demurrageModifier; // PPM
|
||||||
|
|
||||||
bytes32[] public redistributions; // uint40(participants) uint160(value) uint56(period)
|
bytes32[] public redistributions; // uint40(participants) | uint160(value) | uint56(period)
|
||||||
mapping (address => bytes32) account;
|
mapping (address => bytes32) account; // uint12(period) | uint160(value)
|
||||||
mapping (address => bool) minter;
|
mapping (address => bool) minter;
|
||||||
|
|
||||||
event Transfer(address indexed _from, address indexed _to, uint256 _value);
|
event Transfer(address indexed _from, address indexed _to, uint256 _value);
|
||||||
@ -40,12 +40,14 @@ contract RedistributedDemurrageToken {
|
|||||||
redistributions.push(initialRedistribution);
|
redistributions.push(initialRedistribution);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// ERC20
|
||||||
function balanceOf(address _account) public view returns (uint256) {
|
function balanceOf(address _account) public view returns (uint256) {
|
||||||
uint256 baseBalance = getBaseBalance(_account);
|
uint256 baseBalance = getBaseBalance(_account);
|
||||||
uint256 inverseModifier = 1000000 - demurrageModifier;
|
uint256 inverseModifier = 1000000 - demurrageModifier;
|
||||||
@ -53,10 +55,12 @@ contract RedistributedDemurrageToken {
|
|||||||
return baseBalance - balanceModifier;
|
return baseBalance - balanceModifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Balance unmodified by demurrage
|
||||||
function getBaseBalance(address _account) private view returns (uint256) {
|
function getBaseBalance(address _account) private view returns (uint256) {
|
||||||
return uint256(account[_account]) & 0x00ffffffffffffffffffffffffffffffffffffffff;
|
return uint256(account[_account]) & 0x00ffffffffffffffffffffffffffffffffffffffff;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Increases base balance for a single account
|
||||||
function increaseBalance(address _account, uint256 _delta) private returns (bool) {
|
function increaseBalance(address _account, uint256 _delta) private returns (bool) {
|
||||||
uint256 oldBalance;
|
uint256 oldBalance;
|
||||||
uint256 newBalance;
|
uint256 newBalance;
|
||||||
@ -68,6 +72,7 @@ contract RedistributedDemurrageToken {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Decreases base balance for a single account
|
||||||
function decreaseBalance(address _account, uint256 _delta) private returns (bool) {
|
function decreaseBalance(address _account, uint256 _delta) private returns (bool) {
|
||||||
uint256 oldBalance;
|
uint256 oldBalance;
|
||||||
uint256 newBalance;
|
uint256 newBalance;
|
||||||
@ -80,6 +85,8 @@ contract RedistributedDemurrageToken {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Creates new tokens out of thin air, and allocates them to the given address
|
||||||
|
// Triggers tax
|
||||||
function mintTo(address _beneficiary, uint256 _amount) external returns (bool) {
|
function mintTo(address _beneficiary, uint256 _amount) external returns (bool) {
|
||||||
require(minter[msg.sender]);
|
require(minter[msg.sender]);
|
||||||
|
|
||||||
@ -92,6 +99,7 @@ contract RedistributedDemurrageToken {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deserializes the redistribution word
|
||||||
function toRedistribution(uint256 _participants, uint256 _value, uint256 _period) private pure returns(bytes32) {
|
function toRedistribution(uint256 _participants, uint256 _value, uint256 _period) private pure returns(bytes32) {
|
||||||
bytes32 redistribution;
|
bytes32 redistribution;
|
||||||
|
|
||||||
@ -101,22 +109,27 @@ contract RedistributedDemurrageToken {
|
|||||||
return redistribution;
|
return redistribution;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Serializes the demurrage period part of the redistribution word
|
||||||
function toRedistributionPeriod(bytes32 redistribution) public pure returns (uint256) {
|
function toRedistributionPeriod(bytes32 redistribution) public pure returns (uint256) {
|
||||||
return uint256(redistribution & 0x00000000000000000000000000000000000000000000000000ffffffffffffff);
|
return uint256(redistribution & 0x00000000000000000000000000000000000000000000000000ffffffffffffff);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Serializes the supply part of the redistribution word
|
||||||
function toRedistributionSupply(bytes32 redistribution) public pure returns (uint256) {
|
function toRedistributionSupply(bytes32 redistribution) public pure returns (uint256) {
|
||||||
return uint256(redistribution & 0x0000000000ffffffffffffffffffffffffffffffffffffffff00000000000000) >> 56;
|
return uint256(redistribution & 0x0000000000ffffffffffffffffffffffffffffffffffffffff00000000000000) >> 56;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Serializes the number of participants part of the redistribution word
|
||||||
function toRedistributionParticipants(bytes32 redistribution) public pure returns (uint256) {
|
function toRedistributionParticipants(bytes32 redistribution) public pure returns (uint256) {
|
||||||
return uint256(redistribution & 0xffffffffff000000000000000000000000000000000000000000000000000000) >> 216;
|
return uint256(redistribution & 0xffffffffff000000000000000000000000000000000000000000000000000000) >> 216;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add number of participants for the current redistribution period by one
|
||||||
function incrementRedistributionParticipants() private returns (bool) {
|
function incrementRedistributionParticipants() private returns (bool) {
|
||||||
uint256 currentRedistribution;
|
uint256 currentRedistribution;
|
||||||
uint256 participants;
|
uint256 participants;
|
||||||
@ -130,6 +143,7 @@ contract RedistributedDemurrageToken {
|
|||||||
redistributions[redistributions.length-1] = bytes32(currentRedistribution);
|
redistributions[redistributions.length-1] = bytes32(currentRedistribution);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Save the current total supply amount to the current redistribution period
|
||||||
function saveRedistributionSupply() private returns (bool) {
|
function saveRedistributionSupply() private returns (bool) {
|
||||||
uint256 currentRedistribution;
|
uint256 currentRedistribution;
|
||||||
|
|
||||||
@ -140,10 +154,12 @@ contract RedistributedDemurrageToken {
|
|||||||
redistributions[redistributions.length-1] = bytes32(currentRedistribution);
|
redistributions[redistributions.length-1] = bytes32(currentRedistribution);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get the demurrage period of the current block number
|
||||||
function actualPeriod() public view returns (uint256) {
|
function actualPeriod() public view returns (uint256) {
|
||||||
return (block.number - periodStart) / periodDuration + 1;
|
return (block.number - periodStart) / periodDuration + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add an entered demurrage period to the redistribution array
|
||||||
function checkPeriod() private view returns (bytes32) {
|
function checkPeriod() private view returns (bytes32) {
|
||||||
bytes32 lastRedistribution;
|
bytes32 lastRedistribution;
|
||||||
uint256 currentPeriod;
|
uint256 currentPeriod;
|
||||||
@ -156,16 +172,20 @@ contract RedistributedDemurrageToken {
|
|||||||
return lastRedistribution;
|
return lastRedistribution;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deserialize the pemurrage period for the given account is participating in
|
||||||
function accountPeriod(address _account) public returns (uint256) {
|
function accountPeriod(address _account) public returns (uint256) {
|
||||||
return (uint256(account[_account]) & 0xffffffffffffffffffffffff0000000000000000000000000000000000000000) >> 160;
|
return (uint256(account[_account]) & 0xffffffffffffffffffffffff0000000000000000000000000000000000000000) >> 160;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Save the given demurrage period as the currently participation period for the given address
|
||||||
function registerAccountPeriod(address _account, uint256 _period) private returns (bool) {
|
function registerAccountPeriod(address _account, uint256 _period) private returns (bool) {
|
||||||
account[_account] &= 0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff;
|
account[_account] &= 0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff;
|
||||||
account[_account] |= bytes32(_period << 160);
|
account[_account] |= bytes32(_period << 160);
|
||||||
incrementRedistributionParticipants();
|
incrementRedistributionParticipants();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Recalculate the demurrage modifier for the new period
|
||||||
|
// After this, all REPORTED balances will have been reduced by the corresponding ratio (but the totalsupply stays the same)
|
||||||
function applyTax() public returns (uint256) {
|
function applyTax() public returns (uint256) {
|
||||||
bytes32 pendingRedistribution;
|
bytes32 pendingRedistribution;
|
||||||
bytes32 nextRedistribution;
|
bytes32 nextRedistribution;
|
||||||
@ -183,6 +203,7 @@ contract RedistributedDemurrageToken {
|
|||||||
return demurrageModifier;
|
return demurrageModifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Calculate a value reduced by demurrage by the given period
|
||||||
function toTaxPeriodAmount(uint256 _value, uint256 _period) public view returns (uint256) {
|
function toTaxPeriodAmount(uint256 _value, uint256 _period) public view returns (uint256) {
|
||||||
uint256 valueFactor;
|
uint256 valueFactor;
|
||||||
|
|
||||||
@ -196,6 +217,8 @@ contract RedistributedDemurrageToken {
|
|||||||
return (valueFactor * _value) / 1000000;
|
return (valueFactor * _value) / 1000000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the given account is participating in a period and that period has been crossed
|
||||||
|
// THEN increase the base value of the account with its share of the value reduction of the period
|
||||||
function applyRedistributionOnAccount(address _account) public returns (bool) {
|
function applyRedistributionOnAccount(address _account) public returns (bool) {
|
||||||
bytes32 periodRedistribution;
|
bytes32 periodRedistribution;
|
||||||
uint256 supply;
|
uint256 supply;
|
||||||
@ -226,6 +249,7 @@ contract RedistributedDemurrageToken {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ERC20, triggers tax and/or redistribution
|
||||||
function transfer(address _to, uint256 _value) public returns (bool) {
|
function transfer(address _to, uint256 _value) public returns (bool) {
|
||||||
uint256 baseValue;
|
uint256 baseValue;
|
||||||
bool result;
|
bool result;
|
||||||
@ -240,6 +264,7 @@ contract RedistributedDemurrageToken {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ERC20 transfer backend for transfer, transferFrom
|
||||||
function transferBase(address _from, address _to, uint256 _value) private returns (bool) {
|
function transferBase(address _from, address _to, uint256 _value) private returns (bool) {
|
||||||
uint256 period;
|
uint256 period;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user