diff --git a/python/erc20_demurrage_token/token.py b/python/erc20_demurrage_token/token.py index 94ddd4e..532bd19 100644 --- a/python/erc20_demurrage_token/token.py +++ b/python/erc20_demurrage_token/token.py @@ -152,10 +152,44 @@ class DemurrageToken(ERC20): return o + def redistributions(self, contract_address, idx, sender_address=ZERO_ADDRESS): + o = jsonrpc_template() + o['method'] = 'eth_call' + enc = ABIContractEncoder() + enc.method('redistributions') + enc.typ(ABIContractType.UINT256) + enc.uint256(idx) + 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 + + + def to_redistribution_period(self, contract_address, redistribution, sender_address=ZERO_ADDRESS): + o = jsonrpc_template() + o['method'] = 'eth_call' + enc = ABIContractEncoder() + enc.method('toRedistributionPeriod') + enc.typ(ABIContractType.BYTES32) + enc.bytes32(redistribution) + 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 + + def apply_demurrage(self, contract_address, sender_address): return self.transact_noarg('applyDemurrage', contract_address, sender_address) + def change_period(self, contract_address, sender_address): + return self.transact_noarg('changePeriod', contract_address, sender_address) + + def actual_period(self, contract_address, sender_address=ZERO_ADDRESS): return self.call_noarg('actualPeriod', contract_address, sender_address=sender_address) @@ -179,4 +213,12 @@ class DemurrageToken(ERC20): def parse_to_base_amount(self, v): return abi_decode_single(ABIContractType.UINT256, v) + + def parse_redistributions(self, v): + return abi_decode_single(ABIContractType.BYTES32, v) + + + def parse_to_redistribution_period(self, v): + return abi_decode_single(ABIContractType.UINT256, v) + diff --git a/python/tests/test_basic.py b/python/tests/test_basic.py index 4117e45..544dcfd 100644 --- a/python/tests/test_basic.py +++ b/python/tests/test_basic.py @@ -9,7 +9,6 @@ import datetime from chainlib.eth.constant import ZERO_ADDRESS from chainlib.eth.nonce import RPCNonceOracle from chainlib.eth.tx import receipt -import eth_tester # local imports from erc20_demurrage_token import DemurrageToken diff --git a/python/tests/test_period.py b/python/tests/test_period.py index 5f7a60a..63ef6e8 100644 --- a/python/tests/test_period.py +++ b/python/tests/test_period.py @@ -4,76 +4,64 @@ import unittest import json import logging -# third-party imports -import web3 -import eth_tester -import eth_abi +# external imports +from chainlib.eth.constant import ZERO_ADDRESS +from chainlib.eth.nonce import RPCNonceOracle +from chainlib.eth.tx import receipt + +# local imports +from erc20_demurrage_token import DemurrageToken + +# test imports +from tests.base import TestDemurrageDefault 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 = 1 - - -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 * (10 ** 32), 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 - b = self.w3.eth.getBlock(self.start_block) - self.start_time = b['timestamp'] - - - def tearDown(self): - pass - +class TestPeriod(TestDemurrageDefault): 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) + nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 1024) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) - self.eth_tester.time_travel(self.start_time + 61) - tx_hash = self.contract.functions.changePeriod().transact() - r = self.w3.eth.getTransactionReceipt(tx_hash) - self.assertEqual(r.status, 1) + self.backend.time_travel(self.start_time + 61) - redistribution = self.contract.functions.redistributions(1).call() - self.assertEqual(2, self.contract.functions.toRedistributionPeriod(redistribution).call()) - self.assertEqual(2, self.contract.functions.actualPeriod().call()) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + (tx_hash, o) = c.change_period(self.address, self.accounts[0]) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + o = c.redistributions(self.address, 1, sender_address=self.accounts[0]) + r = self.rpc.do(o) + redistribution = c.parse_redistributions(r) + + o = c.to_redistribution_period(self.address, redistribution, sender_address=self.accounts[0]) + r = self.rpc.do(o) + period = c.parse_to_redistribution_period(r) + self.assertEqual(2, period) + + o = c.redistributions(self.address, 1, sender_address=self.accounts[0]) + r = self.rpc.do(o) + redistribution = c.parse_redistributions(r) + + o = c.to_redistribution_period(self.address, redistribution, sender_address=self.accounts[0]) + r = self.rpc.do(o) + period = c.parse_to_redistribution_period(r) + self.assertEqual(2, period) + + o = c.actual_period(self.address, sender_address=self.accounts[0]) + r = self.rpc.do(o) + period = c.parse_actual_period(r) + self.assertEqual(2, period) if __name__ == '__main__':