Compare commits

...
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.

6 Commits

5 changed files with 418 additions and 21 deletions

View File

@ -129,6 +129,8 @@ class Test(unittest.TestCase):
def test_apply_tax(self):
self.contract.functions.mintTo(self.w3.eth.accounts[1], 1024).transact()
self.eth_tester.mine_blocks(PERIOD)
tx_hash = self.contract.functions.applyTax().transact()
r = self.w3.eth.getTransactionReceipt(tx_hash)

View File

@ -0,0 +1,78 @@
# standard imports
import os
import unittest
import json
import logging
# third-party imports
import web3
import eth_tester
import eth_abi
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
logging.getLogger('web3').setLevel(logging.WARNING)
logging.getLogger('eth.vm').setLevel(logging.WARNING)
testdir = os.path.dirname(__file__)
#BLOCKTIME = 5 # seconds
TAX_LEVEL = 10000 * 2 # 2%
#PERIOD = int(60/BLOCKTIME) * 60 * 24 * 30 # month
PERIOD = 2
class Test(unittest.TestCase):
contract = None
def setUp(self):
eth_params = eth_tester.backends.pyevm.main.get_default_genesis_params({
'gas_limit': 9000000,
})
f = open(os.path.join(testdir, '../../solidity/RedistributedDemurrageToken.bin'), 'r')
self.bytecode = f.read()
f.close()
f = open(os.path.join(testdir, '../../solidity/RedistributedDemurrageToken.json'), 'r')
self.abi = json.load(f)
f.close()
backend = eth_tester.PyEVMBackend(eth_params)
self.eth_tester = eth_tester.EthereumTester(backend)
provider = web3.Web3.EthereumTesterProvider(self.eth_tester)
self.w3 = web3.Web3(provider)
self.sink_address = self.w3.eth.accounts[9]
c = self.w3.eth.contract(abi=self.abi, bytecode=self.bytecode)
tx_hash = c.constructor('Foo Token', 'FOO', 6, TAX_LEVEL, PERIOD, self.sink_address).transact({'from': self.w3.eth.accounts[0]})
r = self.w3.eth.getTransactionReceipt(tx_hash)
self.contract = self.w3.eth.contract(abi=self.abi, address=r.contractAddress)
self.start_block = self.w3.eth.blockNumber
def tearDown(self):
pass
def test_period(self):
tx_hash = self.contract.functions.mintTo(self.w3.eth.accounts[1], 1024).transact()
r = self.w3.eth.getTransactionReceipt(tx_hash)
self.assertEqual(r.status, 1)
self.eth_tester.mine_blocks(PERIOD * 10)
tx_hash = self.contract.functions.transfer(self.w3.eth.accounts[2], 500).transact({'from': self.w3.eth.accounts[1]})
r = self.w3.eth.getTransactionReceipt(tx_hash)
logg.debug('r {}'.format(r));
self.assertEqual(r.status, 1)
period = self.contract.functions.accountPeriod(self.w3.eth.accounts[1]).call()
self.assertEqual(period, 12)
if __name__ == '__main__':
unittest.main()

102
python/tests/test_pure.py Normal file
View File

@ -0,0 +1,102 @@
# standard imports
import os
import unittest
import json
import logging
import math
# third-party imports
import web3
import eth_tester
import eth_abi
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
logging.getLogger('web3').setLevel(logging.WARNING)
logging.getLogger('eth.vm').setLevel(logging.WARNING)
testdir = os.path.dirname(__file__)
#BLOCKTIME = 5 # seconds
TAX_LEVEL = 10000 * 2 # 2%
#PERIOD = int(60/BLOCKTIME) * 60 * 24 * 30 # month
PERIOD = 10
class Test(unittest.TestCase):
contract = None
def setUp(self):
eth_params = eth_tester.backends.pyevm.main.get_default_genesis_params({
'gas_limit': 9000000,
})
f = open(os.path.join(testdir, '../../solidity/RedistributedDemurrageToken.bin'), 'r')
self.bytecode = f.read()
f.close()
f = open(os.path.join(testdir, '../../solidity/RedistributedDemurrageToken.json'), 'r')
self.abi = json.load(f)
f.close()
backend = eth_tester.PyEVMBackend(eth_params)
self.eth_tester = eth_tester.EthereumTester(backend)
provider = web3.Web3.EthereumTesterProvider(self.eth_tester)
self.w3 = web3.Web3(provider)
self.sink_address = self.w3.eth.accounts[9]
c = self.w3.eth.contract(abi=self.abi, bytecode=self.bytecode)
tx_hash = c.constructor('Foo Token', 'FOO', 6, TAX_LEVEL, PERIOD, self.sink_address).transact({'from': self.w3.eth.accounts[0]})
r = self.w3.eth.getTransactionReceipt(tx_hash)
self.contract = self.w3.eth.contract(abi=self.abi, address=r.contractAddress)
self.start_block = self.w3.eth.blockNumber
def tearDown(self):
pass
def test_tax_period(self):
a = self.contract.functions.toTaxPeriodAmount(1000000, 0).call()
self.assertEqual(1000000, a)
a = self.contract.functions.toTaxPeriodAmount(1000000, 1).call()
self.assertEqual(980000, a)
a = self.contract.functions.toTaxPeriodAmount(1000000, 2).call()
self.assertEqual(960400, a)
def test_fractional_state(self):
with self.assertRaises(eth_tester.exceptions.TransactionFailed):
self.contract.functions.remainder(2, 1).call();
with self.assertRaises(eth_tester.exceptions.TransactionFailed):
remainder = self.contract.functions.remainder(0, 100001).call();
remainder = self.contract.functions.remainder(1, 2).call();
self.assertEqual(remainder, 0);
whole = 5000001
parts = 20000
expect = whole - (math.floor(whole/parts) * parts)
remainder = self.contract.functions.remainder(parts, whole).call();
self.assertEqual(remainder, expect)
parts = 30000
expect = whole - (math.floor(whole/parts) * parts)
remainder = self.contract.functions.remainder(parts, whole).call();
self.assertEqual(remainder, expect)
parts = 40001
expect = whole - (math.floor(whole/parts) * parts)
remainder = self.contract.functions.remainder(parts, whole).call();
self.assertEqual(remainder, expect)
if __name__ == '__main__':
unittest.main()

View File

@ -0,0 +1,190 @@
# standard imports
import os
import unittest
import json
import logging
# third-party imports
import web3
import eth_tester
import eth_abi
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
logging.getLogger('web3').setLevel(logging.WARNING)
logging.getLogger('eth.vm').setLevel(logging.WARNING)
testdir = os.path.dirname(__file__)
#BLOCKTIME = 5 # seconds
TAX_LEVEL = 10000 * 2 # 2%
#PERIOD = int(60/BLOCKTIME) * 60 * 24 * 30 # month
PERIOD = 20
class Test(unittest.TestCase):
contract = None
def setUp(self):
eth_params = eth_tester.backends.pyevm.main.get_default_genesis_params({
'gas_limit': 9000000,
})
f = open(os.path.join(testdir, '../../solidity/RedistributedDemurrageToken.bin'), 'r')
self.bytecode = f.read()
f.close()
f = open(os.path.join(testdir, '../../solidity/RedistributedDemurrageToken.json'), 'r')
self.abi = json.load(f)
f.close()
backend = eth_tester.PyEVMBackend(eth_params)
self.eth_tester = eth_tester.EthereumTester(backend)
provider = web3.Web3.EthereumTesterProvider(self.eth_tester)
self.w3 = web3.Web3(provider)
self.sink_address = self.w3.eth.accounts[9]
c = self.w3.eth.contract(abi=self.abi, bytecode=self.bytecode)
tx_hash = c.constructor('Foo Token', 'FOO', 6, TAX_LEVEL, PERIOD, self.sink_address).transact({'from': self.w3.eth.accounts[0]})
r = self.w3.eth.getTransactionReceipt(tx_hash)
self.contract = self.w3.eth.contract(abi=self.abi, address=r.contractAddress)
self.start_block = self.w3.eth.blockNumber
logg.debug('starting at block number {}'.format(self.start_block))
def tearDown(self):
pass
def debug_periods(self):
pactual = self.contract.functions.actualPeriod().call()
pstart = self.contract.functions.periodStart().call()
pduration = self.contract.functions.periodDuration().call()
blocknumber = self.w3.eth.blockNumber;
logg.debug('actual {} start {} duration {} blocknumber {}'.format(pactual, pstart, pduration, blocknumber))
# TODO: check receipt log outputs
@unittest.skip('foo')
def test_redistribution_storage(self):
self.contract.functions.mintTo(self.w3.eth.accounts[1], 2000).transact()
tx_hash = self.contract.functions.transfer(self.w3.eth.accounts[2], 500).transact({'from': self.w3.eth.accounts[1]})
r = self.w3.eth.getTransactionReceipt(tx_hash)
logg.debug('tx before {}'.format(r))
self.assertEqual(r.status, 1)
self.eth_tester.mine_blocks(PERIOD)
redistribution = self.contract.functions.redistributions(0).call();
self.assertEqual(redistribution.hex(), '000000000100000000000000000000000000000000000007d000000000000001')
tx_hash = self.contract.functions.mintTo(self.w3.eth.accounts[0], 1000000).transact()
r = self.w3.eth.getTransactionReceipt(tx_hash)
self.assertEqual(r.status, 1)
redistribution = self.contract.functions.redistributions(1).call()
self.assertEqual(redistribution.hex(), '000000000000000000000000000000000000000000000f4a1000000000000002')
def test_redistribution_balance_on_zero_participants(self):
supply = 1000000000000
tx_hash = self.contract.functions.mintTo(self.w3.eth.accounts[1], supply).transact()
r = self.w3.eth.getTransactionReceipt(tx_hash)
self.eth_tester.mine_blocks(PERIOD)
tx_hash = self.contract.functions.applyTax().transact()
r = self.w3.eth.getTransactionReceipt(tx_hash)
logg.debug('r {}'.format(r))
self.assertEqual(r.status, 1)
redistribution = self.contract.functions.redistributions(0).call();
supply = self.contract.functions.totalSupply().call()
sink_increment = int(supply * (TAX_LEVEL / 1000000))
for l in r['logs']:
if l.topics[0].hex() == '0x337db9c77a0769d770641c73e3282be23b15e2bddd830c219461dec832313389': # event Taxed(uint256,uint256)
period = int.from_bytes(l.topics[1], 'big')
self.assertEqual(period, 1)
b = bytes.fromhex(l.data[2:])
remainder = int.from_bytes(b, 'big')
self.assertEqual(remainder, sink_increment)
logg.debug('period {} remainder {}'.format(period, remainder))
sink_balance = self.contract.functions.balanceOf(self.sink_address).call()
logg.debug('{} {}'.format(sink_increment, sink_balance))
self.assertEqual(sink_balance, int(sink_increment * 0.98))
self.assertEqual(sink_balance, int(sink_increment * (1000000 - TAX_LEVEL) / 1000000))
balance = self.contract.functions.balanceOf(self.w3.eth.accounts[1]).call()
self.assertEqual(balance, supply - sink_increment)
def test_redistribution_two_of_ten(self):
mint_amount = 100000000
z = 0
for i in range(10):
self.contract.functions.mintTo(self.w3.eth.accounts[i], mint_amount).transact()
z += mint_amount
initial_balance = self.contract.functions.balanceOf(self.w3.eth.accounts[1]).call()
spend_amount = 1000000
external_address = web3.Web3.toChecksumAddress('0x' + os.urandom(20).hex())
self.contract.functions.transfer(external_address, spend_amount).transact({'from': self.w3.eth.accounts[1]})
tx_hash = self.contract.functions.transfer(external_address, spend_amount).transact({'from': self.w3.eth.accounts[2]})
r = self.w3.eth.getTransactionReceipt(tx_hash)
# No cheating!
self.contract.functions.transfer(self.w3.eth.accounts[3], spend_amount).transact({'from': self.w3.eth.accounts[3]})
# Too low
self.contract.functions.transfer(external_address, spend_amount-1).transact({'from': self.w3.eth.accounts[4]})
self.assertEqual(r.status, 1)
self.eth_tester.mine_blocks(PERIOD)
self.contract.functions.applyTax().transact()
bummer_balance = self.contract.functions.balanceOf(self.w3.eth.accounts[3]).call()
self.assertEqual(bummer_balance, mint_amount - (mint_amount * (TAX_LEVEL / 1000000)))
logg.debug('bal {} '.format(bummer_balance))
bummer_balance = self.contract.functions.balanceOf(self.w3.eth.accounts[1]).call()
spender_balance = mint_amount - spend_amount
spender_decayed_balance = int(spender_balance - (spender_balance * (TAX_LEVEL / 1000000)))
self.assertEqual(bummer_balance, spender_decayed_balance)
logg.debug('bal {} '.format(bummer_balance))
tx_hash = self.contract.functions.applyRedistributionOnAccount(self.w3.eth.accounts[1]).transact()
r = self.w3.eth.getTransactionReceipt(tx_hash)
logg.debug('log {}'.format(r.logs))
self.contract.functions.applyRedistributionOnAccount(self.w3.eth.accounts[2]).transact()
redistribution_data = self.contract.functions.redistributions(0).call()
logg.debug('redist data {}'.format(redistribution_data.hex()))
account_period_data = self.contract.functions.accountPeriod(self.w3.eth.accounts[1]).call()
logg.debug('account period {}'.format(account_period_data))
actual_period = self.contract.functions.actualPeriod().call()
logg.debug('period {}'.format(actual_period))
redistribution = int((z / 2) * (TAX_LEVEL / 1000000))
spender_new_base_balance = ((mint_amount - spend_amount) + redistribution)
spender_new_decayed_balance = int(spender_new_base_balance - (spender_new_base_balance * (TAX_LEVEL / 1000000)))
spender_actual_balance = self.contract.functions.balanceOf(self.w3.eth.accounts[1]).call()
logg.debug('rrr {} {}'.format(redistribution, spender_new_decayed_balance))
self.assertEqual(spender_actual_balance, spender_new_decayed_balance)
if __name__ == '__main__':
unittest.main()

View File

@ -6,10 +6,11 @@ pragma solidity > 0.6.11;
contract RedistributedDemurrageToken {
address public owner;
uint256 public decimals;
string public name;
string public symbol;
uint256 public decimals;
uint256 public totalSupply;
uint256 public minimumParticipantSpend;
uint256 public periodStart;
uint256 public periodDuration;
@ -43,6 +44,7 @@ contract RedistributedDemurrageToken {
sinkAddress = _defaultSinkAddress;
bytes32 initialRedistribution = toRedistribution(0, 0, 1);
redistributions.push(initialRedistribution);
minimumParticipantSpend = 10 ** uint256(_decimals);
}
// Given address will be allowed to call the mintTo() function
@ -198,7 +200,7 @@ contract RedistributedDemurrageToken {
uint256 truncatedResult;
if (_numParts == 0) { // no division by zero please
return _sumWhole;
revert('ERR_NUMPARTS_ZERO');
}
require(_numParts < _sumWhole); // At least you are never LESS than the sum of your parts. Think about that.
@ -207,18 +209,42 @@ contract RedistributedDemurrageToken {
return _sumWhole - truncatedResult;
}
// Called in the edge case where participant number is 0. It will override the participant count to 1.
// Returns the remainder sent to the sink address
function applyDefaultRedistribution(bytes32 _redistribution) private returns (uint256) {
uint256 redistributionSupply;
uint256 redistributionPeriod;
uint256 unit;
uint256 truncatedResult;
redistributionSupply = toRedistributionSupply(_redistribution);
unit = (redistributionSupply * taxLevel) / 1000000;
truncatedResult = (unit * 1000000) / taxLevel;
if (truncatedResult < redistributionSupply) {
redistributionPeriod = toRedistributionPeriod(_redistribution); // since we reuse period here, can possibly be optimized by passing period instead
redistributions[redistributionPeriod-1] &= 0x0000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffff; // just to be safe, zero out all participant count data, in this case there will be only one
redistributions[redistributionPeriod-1] |= 0x8000000001000000000000000000000000000000000000000000000000000000;
}
increaseBaseBalance(sinkAddress, unit); //truncatedResult);
return unit;
}
// sets the remainder bit for the given period and books the remainder to the sink address balance
// returns false if no change was made
function applyRemainderOnPeriod(uint256 _remainder, uint256 _period) private returns (bool) {
bytes32 redistribution;
redistribution = redistributions[_period-1];
uint256 periodSupply;
if (_remainder == 0) {
return false;
}
redistribution |= 0x8000000000000000000000000000000000000000000000000000000000000000;
increaseBaseBalance(sinkAddress, _remainder);
redistributions[_period-1] |= 0x8000000000000000000000000000000000000000000000000000000000000000;
periodSupply = toRedistributionSupply(redistributions[_period-1]);
increaseBaseBalance(sinkAddress, periodSupply - _remainder);
return true;
}
@ -241,8 +267,12 @@ contract RedistributedDemurrageToken {
redistributions.push(nextRedistribution);
currentParticipants = toRedistributionParticipants(currentRedistribution);
currentRemainder = remainder(currentParticipants, totalSupply); // we can use totalSupply directly because it will always be the same as the recorded supply on the current redistribution
applyRemainderOnPeriod(currentRemainder, currentPeriod);
if (currentParticipants == 0) {
currentRemainder = applyDefaultRedistribution(currentRedistribution);
} else {
currentRemainder = remainder(currentParticipants, totalSupply); // we can use totalSupply directly because it will always be the same as the recorded supply on the current redistribution
applyRemainderOnPeriod(currentRemainder, currentPeriod);
}
emit Taxed(currentPeriod, currentRemainder);
return demurrageModifier;
}
@ -255,9 +285,8 @@ contract RedistributedDemurrageToken {
// valueFactor = 1000000 * (((1000000-taxLevel)/1000000) ** _period);
valueFactor = 1000000;
for (uint256 i = 0; i < _period; i++) {
valueFactor = (valueFactor * taxLevel) / 1000000;
valueFactor = valueFactor - ((valueFactor * taxLevel) / 1000000);
}
return (valueFactor * _value) / 1000000;
}
@ -282,9 +311,8 @@ contract RedistributedDemurrageToken {
}
supply = toRedistributionSupply(periodRedistribution);
// TODO: Make sure value for balance increases round down, and that we can do a single allocation to a sink account with the difference. We can use the highest bit in "participants" for that.
baseValue = supply / participants;
value = toTaxPeriodAmount(baseValue, period-1);
baseValue = ((supply / participants) * (taxLevel) / 1000000);
value = toTaxPeriodAmount(baseValue, period - 1);
account[_account] &= bytes32(0x000000000000000000000000ffffffffffffffffffffffffffffffffffffffff);
increaseBaseBalance(_account, value);
@ -317,14 +345,11 @@ contract RedistributedDemurrageToken {
function transferBase(address _from, address _to, uint256 _value) private returns (bool) {
uint256 period;
if (!decreaseBaseBalance(_from, _value)) {
revert('ERR_TX_DECREASEBALANCE');
}
if (!increaseBaseBalance(_to, _value)) {
revert('ERR_TX_INCREASEBALANCE');
}
decreaseBaseBalance(_from, _value);
increaseBaseBalance(_to, _value);
period = actualPeriod();
if (_value > 0 && accountPeriod(_from) != period) {
if (_value >= minimumParticipantSpend && accountPeriod(_from) != period && _from != _to) {
registerAccountPeriod(_from, period);
}
return true;