mirror of
git://holbrook.no/erc20-demurrage-token
synced 2024-11-05 10:06:45 +01:00
WIP Improve redistribution precision
This commit is contained in:
parent
e8781a9aa0
commit
0dba167af2
File diff suppressed because one or more lines are too long
@ -357,6 +357,23 @@ class DemurrageToken(ERC20):
|
||||
return o
|
||||
|
||||
|
||||
def get_distribution(self, contract_address, supply, demurrage_amount, sender_address=ZERO_ADDRESS):
|
||||
o = jsonrpc_template()
|
||||
o['method'] = 'eth_call'
|
||||
enc = ABIContractEncoder()
|
||||
enc.method('getDistribution')
|
||||
enc.typ(ABIContractType.UINT256)
|
||||
enc.typ(ABIContractType.UINT256)
|
||||
enc.uint256(supply)
|
||||
enc.uint256(demurrage_amount)
|
||||
data = add_0x(enc.get())
|
||||
tx = self.template(sender_address, contract_address)
|
||||
tx = self.set_code(tx, data)
|
||||
o['params'].append(self.normalize(tx))
|
||||
o['params'].append('latest')
|
||||
return o
|
||||
|
||||
|
||||
@classmethod
|
||||
def parse_actual_period(self, v):
|
||||
return abi_decode_single(ABIContractType.UINT256, v)
|
||||
@ -419,3 +436,8 @@ class DemurrageToken(ERC20):
|
||||
@classmethod
|
||||
def parse_decay_by(self, v):
|
||||
return abi_decode_single(ABIContractType.UINT256, v)
|
||||
|
||||
|
||||
@classmethod
|
||||
def parse_get_distribution(self, v):
|
||||
return abi_decode_single(ABIContractType.UINT256, v)
|
||||
|
@ -8,7 +8,7 @@ from erc20_demurrage_token.sim import DemurrageTokenSimulation
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logg = logging.getLogger()
|
||||
|
||||
decay_per_minute = 0.00050105908373373 # equals approx 2% per month
|
||||
decay_per_minute = 0.00000050105908373373 # equals approx 2% per month
|
||||
|
||||
# parameters for simulation object
|
||||
settings = DemurrageTokenSettings()
|
||||
|
@ -60,7 +60,7 @@ class TestDemurrage(EthTesterCase):
|
||||
except TypeError:
|
||||
self.start_time = int(r['timestamp'])
|
||||
|
||||
self.default_supply = 1000000000000
|
||||
self.default_supply = 10 ** 12
|
||||
self.default_supply_cap = int(self.default_supply * 10)
|
||||
|
||||
|
||||
|
@ -55,7 +55,7 @@ class TestBasic(TestDemurrageDefault):
|
||||
|
||||
|
||||
def test_apply_demurrage(self):
|
||||
modifier = 10 * (10 ** 37)
|
||||
modifier = (10 ** 38)
|
||||
|
||||
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
||||
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||
|
@ -31,124 +31,139 @@ testdir = os.path.dirname(__file__)
|
||||
|
||||
class TestRedistribution(TestDemurrageUnit):
|
||||
|
||||
def test_single_step(self):
|
||||
# TODO: move to "pure" test file when getdistribution is implemented in all contracts
|
||||
def test_distribution(self):
|
||||
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
||||
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||
|
||||
mint_amount = 100000000
|
||||
demurrage = (self.tax_level / 1000000) * (10^38)
|
||||
supply = self.default_supply
|
||||
|
||||
(tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], mint_amount)
|
||||
self.rpc.do(o)
|
||||
|
||||
self.backend.time_travel(self.start_time + self.period_seconds)
|
||||
|
||||
(tx_hash, o) = c.change_period(self.address, self.accounts[0])
|
||||
self.rpc.do(o)
|
||||
|
||||
expected_balance = int(mint_amount - ((self.tax_level / 1000000) * mint_amount))
|
||||
|
||||
o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0])
|
||||
o = c.get_distribution(self.address, supply, demurrage, sender_address=self.accounts[0])
|
||||
r = self.rpc.do(o)
|
||||
balance = c.parse_balance_of(r)
|
||||
|
||||
self.assertEqual(balance, expected_balance)
|
||||
distribution = c.parse_get_distribution(r)
|
||||
expected_distribution = self.default_supply * (self.tax_level / 1000000)
|
||||
self.assertEqual(distribution, self.default_supply + expected_distribution)
|
||||
|
||||
|
||||
def test_single_step_multi(self):
|
||||
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
||||
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||
|
||||
mint_amount = 100000000
|
||||
|
||||
for i in range(3):
|
||||
(tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[i+1], mint_amount)
|
||||
self.rpc.do(o)
|
||||
|
||||
self.backend.time_travel(self.start_time + self.period_seconds)
|
||||
|
||||
(tx_hash, o) = c.change_period(self.address, self.accounts[0])
|
||||
self.rpc.do(o)
|
||||
|
||||
expected_balance = int(mint_amount - ((self.tax_level / 1000000) * mint_amount))
|
||||
|
||||
for i in range(3):
|
||||
o = c.balance_of(self.address, self.accounts[i+1], sender_address=self.accounts[0])
|
||||
r = self.rpc.do(o)
|
||||
balance = c.parse_balance_of(r)
|
||||
self.assertEqual(balance, expected_balance)
|
||||
|
||||
|
||||
def test_single_step_transfer(self):
|
||||
nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
||||
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||
|
||||
mint_amount = 100000000
|
||||
half_mint_amount = int(mint_amount / 2)
|
||||
|
||||
(tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], mint_amount)
|
||||
self.rpc.do(o)
|
||||
|
||||
(tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[2], mint_amount)
|
||||
self.rpc.do(o)
|
||||
|
||||
nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc)
|
||||
c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||
(tx_hash, o) = c.transfer(self.address, self.accounts[1], self.accounts[3], half_mint_amount)
|
||||
self.rpc.do(o)
|
||||
|
||||
self.backend.time_travel(self.start_time + self.period_seconds)
|
||||
|
||||
(tx_hash, o) = c.change_period(self.address, self.accounts[1])
|
||||
self.rpc.do(o)
|
||||
o = receipt(tx_hash)
|
||||
r = self.rpc.do(o)
|
||||
self.assertEqual(r['status'], 1)
|
||||
|
||||
demurrage_amount = int((self.tax_level / 1000000) * mint_amount)
|
||||
|
||||
expected_balance = mint_amount - demurrage_amount
|
||||
o = c.balance_of(self.address, self.accounts[2], sender_address=self.accounts[0])
|
||||
r = self.rpc.do(o)
|
||||
balance = c.parse_balance_of(r)
|
||||
self.assertEqual(balance, expected_balance)
|
||||
|
||||
half_demurrage_amount = int((self.tax_level / 1000000) * half_mint_amount)
|
||||
|
||||
expected_balance = half_mint_amount - half_demurrage_amount
|
||||
o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0])
|
||||
r = self.rpc.do(o)
|
||||
balance = c.parse_balance_of(r)
|
||||
self.assertEqual(balance, expected_balance)
|
||||
|
||||
o = c.balance_of(self.address, self.accounts[3], sender_address=self.accounts[0])
|
||||
r = self.rpc.do(o)
|
||||
balance = c.parse_balance_of(r)
|
||||
self.assertEqual(balance, expected_balance)
|
||||
|
||||
o = c.total_supply(self.address, sender_address=self.accounts[0])
|
||||
r = self.rpc.do(o)
|
||||
supply = c.parse_total_supply(r)
|
||||
|
||||
|
||||
o = c.redistributions(self.address, 0, sender_address=self.accounts[0])
|
||||
redistribution = self.rpc.do(o)
|
||||
o = c.to_redistribution_supply(self.address, redistribution, sender_address=self.accounts[0])
|
||||
r = self.rpc.do(o)
|
||||
supply = c.parse_to_redistribution_item(r)
|
||||
o = c.demurrage_amount(self.address, sender_address=self.accounts[0])
|
||||
r = self.rpc.do(o)
|
||||
demurrage = c.parse_demurrage_amount(r)
|
||||
logg.debug('\nrediistribution {}\ndemurrage {}\nsupply {}'.format(redistribution, demurrage, supply))
|
||||
|
||||
expected_balance = int(supply * (self.tax_level / 1000000))
|
||||
expected_balance_tolerance = 1
|
||||
|
||||
o = c.balance_of(self.address, self.sink_address, sender_address=self.accounts[0])
|
||||
r = self.rpc.do(o)
|
||||
balance = c.parse_balance_of(r)
|
||||
self.assertGreaterEqual(balance, expected_balance - expected_balance_tolerance)
|
||||
self.assertLessEqual(balance, expected_balance)
|
||||
|
||||
# def test_single_step(self):
|
||||
# nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
||||
# c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||
#
|
||||
# mint_amount = 100000000
|
||||
#
|
||||
# (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], mint_amount)
|
||||
# self.rpc.do(o)
|
||||
#
|
||||
# self.backend.time_travel(self.start_time + self.period_seconds)
|
||||
#
|
||||
# (tx_hash, o) = c.change_period(self.address, self.accounts[0])
|
||||
# self.rpc.do(o)
|
||||
#
|
||||
# expected_balance = int(mint_amount - ((self.tax_level / 1000000) * mint_amount))
|
||||
#
|
||||
# o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0])
|
||||
# r = self.rpc.do(o)
|
||||
# balance = c.parse_balance_of(r)
|
||||
#
|
||||
# self.assertEqual(balance, expected_balance)
|
||||
#
|
||||
#
|
||||
# def test_single_step_multi(self):
|
||||
# nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
||||
# c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||
#
|
||||
# mint_amount = 100000000
|
||||
#
|
||||
# for i in range(3):
|
||||
# (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[i+1], mint_amount)
|
||||
# self.rpc.do(o)
|
||||
#
|
||||
# self.backend.time_travel(self.start_time + self.period_seconds)
|
||||
#
|
||||
# (tx_hash, o) = c.change_period(self.address, self.accounts[0])
|
||||
# self.rpc.do(o)
|
||||
#
|
||||
# expected_balance = int(mint_amount - ((self.tax_level / 1000000) * mint_amount))
|
||||
#
|
||||
# for i in range(3):
|
||||
# o = c.balance_of(self.address, self.accounts[i+1], sender_address=self.accounts[0])
|
||||
# r = self.rpc.do(o)
|
||||
# balance = c.parse_balance_of(r)
|
||||
# self.assertEqual(balance, expected_balance)
|
||||
#
|
||||
#
|
||||
# def test_single_step_transfer(self):
|
||||
# nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc)
|
||||
# c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||
#
|
||||
# mint_amount = 100000000
|
||||
# half_mint_amount = int(mint_amount / 2)
|
||||
#
|
||||
# (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], mint_amount)
|
||||
# self.rpc.do(o)
|
||||
#
|
||||
# (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[2], mint_amount)
|
||||
# self.rpc.do(o)
|
||||
#
|
||||
# nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc)
|
||||
# c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle)
|
||||
# (tx_hash, o) = c.transfer(self.address, self.accounts[1], self.accounts[3], half_mint_amount)
|
||||
# self.rpc.do(o)
|
||||
#
|
||||
# self.backend.time_travel(self.start_time + self.period_seconds)
|
||||
#
|
||||
# (tx_hash, o) = c.change_period(self.address, self.accounts[1])
|
||||
# self.rpc.do(o)
|
||||
# o = receipt(tx_hash)
|
||||
# r = self.rpc.do(o)
|
||||
# self.assertEqual(r['status'], 1)
|
||||
#
|
||||
# demurrage_amount = int((self.tax_level / 1000000) * mint_amount)
|
||||
#
|
||||
# expected_balance = mint_amount - demurrage_amount
|
||||
# o = c.balance_of(self.address, self.accounts[2], sender_address=self.accounts[0])
|
||||
# r = self.rpc.do(o)
|
||||
# balance = c.parse_balance_of(r)
|
||||
# self.assertEqual(balance, expected_balance)
|
||||
#
|
||||
# half_demurrage_amount = int((self.tax_level / 1000000) * half_mint_amount)
|
||||
#
|
||||
# expected_balance = half_mint_amount - half_demurrage_amount
|
||||
# o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0])
|
||||
# r = self.rpc.do(o)
|
||||
# balance = c.parse_balance_of(r)
|
||||
# self.assertEqual(balance, expected_balance)
|
||||
#
|
||||
# o = c.balance_of(self.address, self.accounts[3], sender_address=self.accounts[0])
|
||||
# r = self.rpc.do(o)
|
||||
# balance = c.parse_balance_of(r)
|
||||
# self.assertEqual(balance, expected_balance)
|
||||
#
|
||||
# o = c.total_supply(self.address, sender_address=self.accounts[0])
|
||||
# r = self.rpc.do(o)
|
||||
# supply = c.parse_total_supply(r)
|
||||
#
|
||||
#
|
||||
# o = c.redistributions(self.address, 0, sender_address=self.accounts[0])
|
||||
# redistribution = self.rpc.do(o)
|
||||
# o = c.to_redistribution_supply(self.address, redistribution, sender_address=self.accounts[0])
|
||||
# r = self.rpc.do(o)
|
||||
# supply = c.parse_to_redistribution_item(r)
|
||||
# o = c.demurrage_amount(self.address, sender_address=self.accounts[0])
|
||||
# r = self.rpc.do(o)
|
||||
# demurrage = c.parse_demurrage_amount(r)
|
||||
# logg.debug('\nrediistribution {}\ndemurrage {}\nsupply {}'.format(redistribution, demurrage, supply))
|
||||
#
|
||||
# expected_balance = int(supply * (self.tax_level / 1000000))
|
||||
# expected_balance_tolerance = 1
|
||||
#
|
||||
# o = c.balance_of(self.address, self.sink_address, sender_address=self.accounts[0])
|
||||
# r = self.rpc.do(o)
|
||||
# balance = c.parse_balance_of(r)
|
||||
# self.assertGreaterEqual(balance, expected_balance - expected_balance_tolerance)
|
||||
# self.assertLessEqual(balance, expected_balance)
|
||||
#
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
@ -6,13 +6,13 @@ contract DemurrageTokenSingleCap {
|
||||
|
||||
// Redistribution bit field, with associated shifts and masks
|
||||
// (Uses sub-byte boundaries)
|
||||
bytes32[] public redistributions; // uint95(unused) | uint20(demurrageModifier) | uint36(participants) | uint72(value) | uint32(period)
|
||||
bytes32[] public redistributions; // uint51(unused) | uint64(demurrageModifier) | uint36(participants) | uint72(value) | uint32(period)
|
||||
uint8 constant shiftRedistributionPeriod = 0;
|
||||
uint256 constant maskRedistributionPeriod = 0x00000000000000000000000000000000000000000000000000000000ffffffff; // (1 << 32) - 1
|
||||
uint8 constant shiftRedistributionValue = 32;
|
||||
uint256 constant maskRedistributionValue = 0x00000000000000000000000000000000000000ffffffffffffffffff00000000; // ((1 << 72) - 1) << 32
|
||||
uint8 constant shiftRedistributionDemurrage = 140;
|
||||
uint256 constant maskRedistributionDemurrage = 0x000000000000000000000ffffffff00000000000000000000000000000000000; // ((1 << 20) - 1) << 140
|
||||
uint8 constant shiftRedistributionDemurrage = 104;
|
||||
uint256 constant maskRedistributionDemurrage = 0x000000ffffffffffffffffffffffffffffffff00000000000000000000000000; // ((1 << 20) - 1) << 140
|
||||
|
||||
// Account balances
|
||||
mapping (address => uint256) account;
|
||||
@ -55,6 +55,9 @@ contract DemurrageTokenSingleCap {
|
||||
// remaining decimal positions of nanoDivider to reach 38, equals precision in growth and decay
|
||||
uint256 constant growthResolutionFactor = 1000000000000;
|
||||
|
||||
// demurrage decimal width; 38 places
|
||||
uint256 immutable resolutionFactor = nanoDivider * growthResolutionFactor;
|
||||
|
||||
// Timestamp of start of periods (time which contract constructor was called)
|
||||
uint256 public immutable periodStart;
|
||||
|
||||
@ -111,10 +114,10 @@ contract DemurrageTokenSingleCap {
|
||||
demurrageTimestamp = block.timestamp;
|
||||
periodStart = demurrageTimestamp;
|
||||
periodDuration = _periodMinutes * 60;
|
||||
demurrageAmount = uint128(nanoDivider * 1000000000000); // Represents 38 decimal places
|
||||
demurrageAmount = 100000000000000000000000000000000000000; // Represents 38 decimal places, same as resolutionFactor
|
||||
//demurragePeriod = 1;
|
||||
taxLevel = _taxLevelMinute; // Represents 38 decimal places
|
||||
bytes32 initialRedistribution = toRedistribution(0, 10000000000000000000, 0, 1);
|
||||
bytes32 initialRedistribution = toRedistribution(0, nanoDivider, 0, 1);
|
||||
redistributions.push(initialRedistribution);
|
||||
|
||||
// Misc settings
|
||||
@ -246,7 +249,8 @@ contract DemurrageTokenSingleCap {
|
||||
uint256 currentRedistribution;
|
||||
uint256 grownSupply;
|
||||
|
||||
grownSupply = growBy(totalSupply, 1);
|
||||
//grownSupply = growBy(totalSupply, 1);
|
||||
grownSupply = totalSupply;
|
||||
currentRedistribution = uint256(redistributions[redistributions.length-1]);
|
||||
currentRedistribution &= (~maskRedistributionValue);
|
||||
currentRedistribution |= (grownSupply << shiftRedistributionValue);
|
||||
@ -280,10 +284,15 @@ contract DemurrageTokenSingleCap {
|
||||
// Returns the amount sent to the sink address
|
||||
function applyDefaultRedistribution(bytes32 _redistribution) private returns (uint256) {
|
||||
uint256 redistributionSupply;
|
||||
uint256 redistributionDemurrage;
|
||||
uint256 unit;
|
||||
|
||||
redistributionSupply = toRedistributionSupply(_redistribution);
|
||||
unit = getDistribution(redistributionSupply, demurrageAmount);
|
||||
redistributionDemurrage = resolutionFactor - toRedistributionDemurrageModifier(_redistribution);
|
||||
if (redistributionDemurrage == 0) {
|
||||
return 0;
|
||||
}
|
||||
unit = getDistribution(redistributionSupply, redistributionDemurrage);
|
||||
increaseBaseBalance(sinkAddress, toBaseAmount(unit / nanoDivider));
|
||||
return unit;
|
||||
}
|
||||
@ -349,9 +358,9 @@ contract DemurrageTokenSingleCap {
|
||||
|
||||
demurrageCounts = demurrageCycles(periodTimestamp);
|
||||
if (demurrageCounts > 0) {
|
||||
nextRedistributionDemurrage = growBy(currentDemurrageAmount, demurrageCounts) / nanoDivider;
|
||||
nextRedistributionDemurrage = growBy(currentDemurrageAmount, demurrageCounts);
|
||||
} else {
|
||||
nextRedistributionDemurrage = currentDemurrageAmount / nanoDivider;
|
||||
nextRedistributionDemurrage = currentDemurrageAmount;
|
||||
}
|
||||
|
||||
nextRedistribution = toRedistribution(0, nextRedistributionDemurrage, totalSupply, nextPeriod);
|
||||
|
Loading…
Reference in New Issue
Block a user