erc20-demurrage-token

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

sim.py (2884B)


      1 # standard imports
      2 import logging
      3 
      4 # local imports
      5 from erc20_demurrage_token import DemurrageTokenSettings
      6 from erc20_demurrage_token.sim import DemurrageTokenSimulation
      7 
      8 logging.basicConfig(level=logging.WARNING)
      9 logg = logging.getLogger()
     10 
     11 decay_per_minute = 0.000050105908373373 # equals approx 2% per month
     12 
     13 # parameters for simulation object
     14 settings = DemurrageTokenSettings()
     15 settings.name = 'Simulated Demurrage Token'
     16 settings.symbol = 'SIM'
     17 settings.decimals = 6
     18 settings.demurrage_level = int(decay_per_minute*(10**40))
     19 settings.period_minutes = 10800 # 1 week in minutes
     20 chain = 'evm:foochain:42'
     21 cap = (10 ** 6) * (10 ** 12)
     22 
     23 # instantiate simulation
     24 sim = DemurrageTokenSimulation(chain, settings, redistribute=True, cap=cap, actors=10)
     25 
     26 # name the usual suspects
     27 alice = sim.actors[0]
     28 bob = sim.actors[1]
     29 carol = sim.actors[2]
     30 
     31 # mint and transfer (every single action advances one block, and one second in time)
     32 sim.mint(alice, sim.from_units(100)) # 10000000 tokens
     33 sim.mint(bob, sim.from_units(100))
     34 sim.transfer(alice, carol, sim.from_units(50))
     35 
     36 # check that balances have been updated
     37 assert sim.balance(alice) == sim.from_units(50)
     38 assert sim.balance(bob) == sim.from_units(100)
     39 assert sim.balance(carol) == sim.from_units(50)
     40 
     41 # advance to next redistribution period
     42 sim.next()
     43 
     44 # inspect balances
     45 print('alice balance: demurraged {:>9d} base {:>9d}'.format(sim.balance(alice), sim.balance(alice, base=True)))
     46 print('bob balance:   demurraged {:>9d} base {:>9d}'.format(sim.balance(bob), sim.balance(bob, base=True)))
     47 print('carol balance: demurraged {:>9d} base {:>9d}'.format(sim.balance(carol), sim.balance(carol, base=True)))
     48 print('sink balance:  demurraged {:>9d} base {:>9d}'.format(sim.balance(sim.sink_address), sim.balance(sim.sink_address, base=True)))
     49 
     50 # get times
     51 minutes = sim.get_minutes()
     52 start = sim.get_now()
     53 timestamp = sim.get_start()
     54 period = sim.get_period()
     55 print('start {} now {} period {} minutes passed {}'.format(start, timestamp, period, minutes))
     56 
     57 
     58 contract_demurrage = 1 - sim.get_demurrage_modifier()    # demurrage in percent (float)
     59 frontend_demurrage = ((1 - decay_per_minute) ** minutes / 100)   # corresponding demurrage modifier (float)
     60 demurrage_delta = contract_demurrage - frontend_demurrage      # difference between demurrage in contract and demurrage calculated in frontend
     61 
     62 alice_checksum = 50000000 - (50000000 * frontend_demurrage) + (200000000 * frontend_demurrage) # alice's balance calculated with frontend demurrage
     63 print("""alice frontend balance {}
     64 alice contract balance {}
     65 frontend demurrage {}
     66 contract demurrage {}
     67 demurrage delta {}""".format(
     68     alice_checksum,
     69     sim.balance(alice),
     70     frontend_demurrage,
     71     contract_demurrage,
     72     demurrage_delta),
     73 )
     74 
     75 balance_sum = sim.balance(alice) + sim.balance(bob) + sim.balance(carol)
     76 print('sum of contract demurraged balances {}'.format(balance_sum))