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.
erc20-demurrage-token-archive/python/erc20_demurrage_token/runnable/apply.py

153 lines
4.2 KiB
Python
Raw Normal View History

2022-03-14 13:58:45 +01:00
"""Deploy sarafu token
.. moduleauthor:: Louis Holbrook <dev@holbrook.no>
.. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
"""
# standard imports
import sys
import os
import json
import argparse
import logging
2022-05-30 09:53:22 +02:00
import datetime
import math
2022-03-14 13:58:45 +01:00
# external imports
import confini
2023-01-11 14:06:09 +01:00
import chainlib.eth.cli
2022-05-30 09:53:22 +02:00
from chainlib.eth.block import (
block_latest,
block_by_number,
Block,
)
2022-03-14 13:58:45 +01:00
from chainlib.eth.connection import EthHTTPConnection
from chainlib.eth.tx import receipt
from chainlib.eth.constant import ZERO_ADDRESS
2022-05-30 09:53:22 +02:00
from hexathon import to_int as hex_to_int
2023-01-11 14:35:24 +01:00
import chainlib.eth.cli
from chainlib.eth.settings import process_settings
from chainlib.settings import ChainSettings
2023-01-11 14:06:09 +01:00
from chainlib.eth.cli.arg import (
Arg,
ArgFlag,
process_args,
)
from chainlib.eth.cli.config import (
Config,
process_config,
)
2023-01-11 14:35:24 +01:00
from chainlib.eth.cli.log import process_log
2022-03-14 13:58:45 +01:00
# local imports
import erc20_demurrage_token
from erc20_demurrage_token import (
DemurrageToken,
DemurrageTokenSettings,
)
2023-01-11 14:35:24 +01:00
logg = logging.getLogger()
2022-03-14 13:58:45 +01:00
2023-01-11 14:06:09 +01:00
def process_config_local(config, arg, args, flags):
config.add(args.steps, '_STEPS', False)
2023-01-11 14:35:24 +01:00
return config
2023-01-11 14:06:09 +01:00
2022-03-14 13:58:45 +01:00
2023-01-11 14:06:09 +01:00
arg_flags = ArgFlag()
arg = Arg(arg_flags)
flags = arg_flags.STD_WRITE | arg_flags.EXEC | arg_flags.WALLET
2022-03-14 13:58:45 +01:00
2023-01-11 14:06:09 +01:00
argparser = chainlib.eth.cli.ArgumentParser()
argparser = process_args(argparser, arg, flags)
2022-05-30 09:53:22 +02:00
argparser.add_argument('--steps', type=int, default=0, help='Max demurrage steps to apply per round')
2022-03-14 13:58:45 +01:00
args = argparser.parse_args()
2023-01-11 14:06:09 +01:00
logg = process_log(args, logg)
2022-03-14 13:58:45 +01:00
2023-01-11 14:06:09 +01:00
config = Config()
config = process_config(config, arg, args, flags)
config = process_config_local(config, arg, args, flags)
logg.debug('config loaded:\n{}'.format(config))
2022-03-14 13:58:45 +01:00
2023-01-11 14:06:09 +01:00
settings = ChainSettings()
settings = process_settings(settings, config)
logg.debug('settings loaded:\n{}'.format(settings))
2022-03-14 13:58:45 +01:00
def main():
2023-01-11 14:06:09 +01:00
chain_spec = settings.get('CHAIN_SPEC')
conn = settings.get('CONN')
2022-05-30 09:53:22 +02:00
o = block_latest()
r = conn.do(o)
block_start_number = None
try:
block_start_number = hex_to_int(r)
except TypeError:
block_start_number = int(r)
o = block_by_number(block_start_number)
r = conn.do(o)
block_start = Block(r)
block_start_timestamp = block_start.timestamp
block_start_datetime = datetime.datetime.fromtimestamp(block_start_timestamp)
2022-03-14 13:58:45 +01:00
2023-01-11 14:06:09 +01:00
gas_oracle = settings.get('FEE_ORACLE')
2022-05-30 09:53:22 +02:00
c = DemurrageToken(chain_spec, gas_oracle=gas_oracle)
2023-01-11 14:06:09 +01:00
o = c.demurrage_timestamp(settings.get('EXEC'))
2022-05-30 09:53:22 +02:00
r = conn.do(o)
demurrage_timestamp = None
try:
demurrage_timestamp = hex_to_int(r)
except TypeError:
demurrage_timestamp = int(r)
demurrage_datetime = datetime.datetime.fromtimestamp(demurrage_timestamp)
total_seconds = block_start_timestamp - demurrage_timestamp
total_steps = total_seconds / 60
if total_steps < 1.0:
logg.error('only {} seconds since last demurrage application, skipping'.format(total_seconds))
return
logg.debug('block start is at {} demurrage is at {} -> {} minutes'.format(
block_start_datetime,
demurrage_datetime,
total_steps,
))
rounds = 1
if config.get('_STEPS') > 0:
rounds = math.ceil(total_steps / config.get('_STEPS'))
logg.info('will perform {} rounds of {} steps'.format(rounds, config.get('_STEPS')))
last_tx_hash = None
for i in range(rounds):
2023-01-11 14:06:09 +01:00
signer = settings.get('SIGNER')
signer_address = settings.get('SENDER_ADDRESS')
2022-05-30 09:53:22 +02:00
2023-01-11 14:06:09 +01:00
nonce_oracle = settings.get('NONCE_ORACLE')
2022-05-30 09:53:22 +02:00
c = DemurrageToken(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle)
(tx_hash_hex, o) = c.apply_demurrage(config.get('_EXEC_ADDRESS'), signer_address, limit=config.get('_STEPS'))
2023-01-11 14:06:09 +01:00
if settings.get('RPC_SEND'):
2022-03-14 13:58:45 +01:00
print(tx_hash_hex)
2022-05-30 09:53:22 +02:00
conn.do(o)
2023-01-11 14:06:09 +01:00
if config.true('_WAIT_ALL') or (i == rounds - 1 and config.true('_WAIT')):
2022-05-30 09:53:22 +02:00
r = conn.wait(tx_hash_hex)
if r['status'] == 0:
sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you')
sys.exit(1)
else:
print(o)
2022-03-14 13:58:45 +01:00
if __name__ == '__main__':
main()