erc20-demurrage-token

ERC20 token with redistributed continual demurrage
Log | Files | Refs | README

bench.old (10774B)


      1 # standard imports
      2 import os
      3 import unittest
      4 import json
      5 import logging
      6 
      7 # third-party imports
      8 import web3
      9 import eth_tester
     10 import eth_abi
     11 
     12 logging.basicConfig(level=logging.DEBUG)
     13 logg = logging.getLogger()
     14 
     15 logging.getLogger('web3').setLevel(logging.WARNING)
     16 logging.getLogger('eth.vm').setLevel(logging.WARNING)
     17 
     18 testdir = os.path.dirname(__file__)
     19 
     20 TAX_LEVEL = 10000 * 2 # 2%
     21 
     22 
     23 class Test(unittest.TestCase):
     24 
     25     contract = None
     26 
     27     def setUp(self):
     28         eth_params = eth_tester.backends.pyevm.main.get_default_genesis_params({
     29             'gas_limit': 9000000,
     30             })
     31 
     32         f = open(os.path.join(testdir, '../../solidity/RedistributedDemurrageToken.bin'), 'r')
     33         self.bytecode = f.read()
     34         f.close()
     35 
     36         f = open(os.path.join(testdir, '../../solidity/RedistributedDemurrageToken.json'), 'r')
     37         self.abi = json.load(f)
     38         f.close()
     39 
     40         backend = eth_tester.PyEVMBackend(eth_params)
     41         self.eth_tester =  eth_tester.EthereumTester(backend)
     42         provider = web3.Web3.EthereumTesterProvider(self.eth_tester)
     43         self.w3 = web3.Web3(provider)
     44         self.sink_address = self.w3.eth.accounts[9]
     45 
     46     def tearDown(self):
     47         pass
     48 
     49 
     50     def test_construct(self):
     51             period = 10
     52             c = self.w3.eth.contract(abi=self.abi, bytecode=self.bytecode)
     53             tx_hash = c.constructor('Foo Token', 'FOO', 6, TAX_LEVEL * (10 ** 32), period, self.sink_address).transact({'from': self.w3.eth.accounts[0]})
     54             r = self.w3.eth.getTransactionReceipt(tx_hash)
     55             print('construct: {}'.format(r['gasUsed']))
     56 
     57 
     58     def test_gas_changeperiod(self):
     59         period = 43200
     60         for i in range(5):
     61             c = self.w3.eth.contract(abi=self.abi, bytecode=self.bytecode)
     62             tx_hash = c.constructor('Foo Token', 'FOO', 6, TAX_LEVEL * (10 ** 32), period, self.sink_address).transact({'from': self.w3.eth.accounts[0]})
     63             r = self.w3.eth.getTransactionReceipt(tx_hash)
     64             contract = self.w3.eth.contract(abi=self.abi, address=r.contractAddress)
     65 
     66             start_block = self.w3.eth.blockNumber
     67             b = self.w3.eth.getBlock(start_block)
     68             start_time = b['timestamp']
     69 
     70             period_seconds = period * 60
     71             self.eth_tester.time_travel(start_time + period_seconds + (60 * (10 ** i)))
     72             tx_hash = contract.functions.changePeriod().transact()
     73             r = self.w3.eth.getTransactionReceipt(tx_hash)
     74 
     75             print('changePeriod {} ({}): {}'.format(i, 60 * (10 ** i), r['gasUsed']))
     76 
     77 
     78     def test_mint(self):
     79             period = 10
     80             c = self.w3.eth.contract(abi=self.abi, bytecode=self.bytecode)
     81             tx_hash = c.constructor('Foo Token', 'FOO', 6, TAX_LEVEL * (10 ** 32), period, self.sink_address).transact({'from': self.w3.eth.accounts[0]})
     82             r = self.w3.eth.getTransactionReceipt(tx_hash)
     83             contract = self.w3.eth.contract(abi=self.abi, address=r.contractAddress)
     84 
     85             start_block = self.w3.eth.blockNumber
     86             b = self.w3.eth.getBlock(start_block)
     87             start_time = b['timestamp']
     88 
     89             tx_hash = contract.functions.mintTo(self.w3.eth.accounts[1], 1000000).transact({'from': self.w3.eth.accounts[0]})
     90             r = self.w3.eth.getTransactionReceipt(tx_hash)
     91             print ('mintTo: {}'.format(r['gasUsed']))
     92 
     93 
     94     def test_transfer(self):
     95             period = 10
     96             c = self.w3.eth.contract(abi=self.abi, bytecode=self.bytecode)
     97             tx_hash = c.constructor('Foo Token', 'FOO', 6, TAX_LEVEL * (10 ** 32), period, self.sink_address).transact({'from': self.w3.eth.accounts[0]})
     98             r = self.w3.eth.getTransactionReceipt(tx_hash)
     99             contract = self.w3.eth.contract(abi=self.abi, address=r.contractAddress)
    100 
    101             start_block = self.w3.eth.blockNumber
    102             b = self.w3.eth.getBlock(start_block)
    103             start_time = b['timestamp']
    104 
    105             contract.functions.mintTo(self.w3.eth.accounts[1], 1000000).transact({'from': self.w3.eth.accounts[0]})
    106 
    107             tx_hash = contract.functions.transfer(self.w3.eth.accounts[2], 1000000).transact({'from': self.w3.eth.accounts[1]})
    108             r = self.w3.eth.getTransactionReceipt(tx_hash)
    109             print ('transfer: {}'.format(r['gasUsed']))
    110 
    111 
    112     def test_approve(self):
    113             period = 10
    114             c = self.w3.eth.contract(abi=self.abi, bytecode=self.bytecode)
    115             tx_hash = c.constructor('Foo Token', 'FOO', 6, TAX_LEVEL * (10 ** 32), period, self.sink_address).transact({'from': self.w3.eth.accounts[0]})
    116             r = self.w3.eth.getTransactionReceipt(tx_hash)
    117             contract = self.w3.eth.contract(abi=self.abi, address=r.contractAddress)
    118 
    119             start_block = self.w3.eth.blockNumber
    120             b = self.w3.eth.getBlock(start_block)
    121             start_time = b['timestamp']
    122 
    123             contract.functions.mintTo(self.w3.eth.accounts[1], 1000000).transact({'from': self.w3.eth.accounts[0]})
    124 
    125             tx_hash = contract.functions.approve(self.w3.eth.accounts[2], 1000000).transact({'from': self.w3.eth.accounts[1]})
    126             r = self.w3.eth.getTransactionReceipt(tx_hash)
    127             print ('approve: {}'.format(r['gasUsed']))
    128 
    129 
    130     def test_transferfrom(self):
    131             period = 10
    132             c = self.w3.eth.contract(abi=self.abi, bytecode=self.bytecode)
    133             tx_hash = c.constructor('Foo Token', 'FOO', 6, TAX_LEVEL * (10 ** 32), period, self.sink_address).transact({'from': self.w3.eth.accounts[0]})
    134             r = self.w3.eth.getTransactionReceipt(tx_hash)
    135             contract = self.w3.eth.contract(abi=self.abi, address=r.contractAddress)
    136 
    137             start_block = self.w3.eth.blockNumber
    138             b = self.w3.eth.getBlock(start_block)
    139             start_time = b['timestamp']
    140 
    141             contract.functions.mintTo(self.w3.eth.accounts[1], 1000000).transact({'from': self.w3.eth.accounts[0]})
    142 
    143             contract.functions.approve(self.w3.eth.accounts[2], 1000000).transact({'from': self.w3.eth.accounts[1]})
    144 
    145             tx_hash = contract.functions.transferFrom(self.w3.eth.accounts[1], self.w3.eth.accounts[3], 1000000).transact({'from': self.w3.eth.accounts[2]})
    146             r = self.w3.eth.getTransactionReceipt(tx_hash)
    147             print ('transferFrom: {}'.format(r['gasUsed']))
    148 
    149 
    150     def test_redistribute_default(self):
    151             period = 10
    152             c = self.w3.eth.contract(abi=self.abi, bytecode=self.bytecode)
    153             tx_hash = c.constructor('Foo Token', 'FOO', 6, TAX_LEVEL * (10 ** 32), period, self.sink_address).transact({'from': self.w3.eth.accounts[0]})
    154             r = self.w3.eth.getTransactionReceipt(tx_hash)
    155             contract = self.w3.eth.contract(abi=self.abi, address=r.contractAddress)
    156 
    157             start_block = self.w3.eth.blockNumber
    158             b = self.w3.eth.getBlock(start_block)
    159             start_time = b['timestamp']
    160 
    161             for i in range(100):
    162                 addr = web3.Web3.toChecksumAddress('0x' + os.urandom(20).hex())
    163                 contract.functions.mintTo(addr, 1000000 * (i+1)).transact({'from': self.w3.eth.accounts[0]})
    164 
    165             self.eth_tester.time_travel(start_time + period * 60 + 1)
    166             redistribution = contract.functions.redistributions(0).call()
    167             tx_hash = contract.functions.changePeriod().transact({'from': self.w3.eth.accounts[2]})
    168             r = self.w3.eth.getTransactionReceipt(tx_hash)
    169             print ('chainPeriod -> defaultRedistribution: {}'.format(r['gasUsed']))
    170 
    171 
    172     def test_redistribution_account(self):
    173             period = 10
    174             c = self.w3.eth.contract(abi=self.abi, bytecode=self.bytecode)
    175             tx_hash = c.constructor('Foo Token', 'FOO', 6, TAX_LEVEL * (10 ** 32), period, self.sink_address).transact({'from': self.w3.eth.accounts[0]})
    176             r = self.w3.eth.getTransactionReceipt(tx_hash)
    177             contract = self.w3.eth.contract(abi=self.abi, address=r.contractAddress)
    178 
    179             start_block = self.w3.eth.blockNumber
    180             b = self.w3.eth.getBlock(start_block)
    181             start_time = b['timestamp']
    182 
    183             contract.functions.mintTo(self.w3.eth.accounts[1], 1000000).transact({'from': self.w3.eth.accounts[0]})
    184             contract.functions.transfer(self.w3.eth.accounts[2], 1000000).transact({'from': self.w3.eth.accounts[1]})
    185 
    186             for i in range(100):
    187                 addr = web3.Web3.toChecksumAddress('0x' + os.urandom(20).hex())
    188                 contract.functions.mintTo(addr, 1000000 * (i+1)).transact({'from': self.w3.eth.accounts[0]})
    189 
    190             self.eth_tester.time_travel(start_time + period * 60 + 1)
    191             redistribution = contract.functions.redistributions(0).call()
    192             tx_hash = contract.functions.applyRedistributionOnAccount(self.w3.eth.accounts[1]).transact({'from': self.w3.eth.accounts[2]})
    193             r = self.w3.eth.getTransactionReceipt(tx_hash)
    194             self.assertEqual(r.logs[0].topics[0].hex(), '0x9a2a887706623ad3ff7fc85652deeceabe9fe1e00466c597972079ee91ea40d3')
    195             print ('redistribute account: {}'.format(r['gasUsed']))
    196 
    197 
    198     def test_redistribution_account_transfer(self):
    199             period = 10
    200             c = self.w3.eth.contract(abi=self.abi, bytecode=self.bytecode)
    201             tx_hash = c.constructor('Foo Token', 'FOO', 6, TAX_LEVEL * (10 ** 32), period, self.sink_address).transact({'from': self.w3.eth.accounts[0]})
    202             r = self.w3.eth.getTransactionReceipt(tx_hash)
    203             contract = self.w3.eth.contract(abi=self.abi, address=r.contractAddress)
    204 
    205             start_block = self.w3.eth.blockNumber
    206             b = self.w3.eth.getBlock(start_block)
    207             start_time = b['timestamp']
    208 
    209             contract.functions.mintTo(self.w3.eth.accounts[1], 2000000).transact({'from': self.w3.eth.accounts[0]})
    210             contract.functions.transfer(self.w3.eth.accounts[2], 1000000).transact({'from': self.w3.eth.accounts[1]})
    211 
    212             for i in range(10):
    213                 addr = web3.Web3.toChecksumAddress('0x' + os.urandom(20).hex())
    214                 contract.functions.mintTo(addr, 1000000 * (i+1)).transact({'from': self.w3.eth.accounts[0]})
    215 
    216             self.eth_tester.time_travel(start_time + period * 60 + 1)
    217             redistribution = contract.functions.redistributions(0).call()
    218             contract.functions.changePeriod().transact({'from': self.w3.eth.accounts[0]})
    219             tx_hash = contract.functions.transfer(self.w3.eth.accounts[3], 100000).transact({'from': self.w3.eth.accounts[1]})
    220             r = self.w3.eth.getTransactionReceipt(tx_hash)
    221             self.assertEqual(r.logs[0].topics[0].hex(), '0x9a2a887706623ad3ff7fc85652deeceabe9fe1e00466c597972079ee91ea40d3')
    222             print ('redistribute account: {}'.format(r['gasUsed']))
    223 
    224 
    225 if __name__ == '__main__':
    226     unittest.main()
    227 
    228