craft-nft

A standalone NFT implementation for real-world arts and crafts assets
Info | Log | Files | Refs | README

allocate.py (3306B)


      1 """Allocates a new token or token batch
      2 
      3 .. moduleauthor:: Louis Holbrook <dev@holbrook.no>
      4 .. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746 
      5 
      6 """
      7 
      8 # SPDX-License-Identifier: GPL-3.0-or-later
      9 
     10 # standard imports
     11 import sys
     12 import os
     13 import json
     14 import argparse
     15 import logging
     16 import time
     17 import hashlib
     18 from enum import Enum
     19 
     20 # external imports
     21 import chainlib.eth.cli
     22 from chainlib.chain import ChainSpec
     23 from chainlib.eth.constant import ZERO_ADDRESS
     24 from chainlib.settings import ChainSettings
     25 from chainlib.eth.settings import process_settings
     26 from chainlib.eth.cli.arg import Arg
     27 from chainlib.eth.cli.arg import ArgFlag
     28 from chainlib.eth.cli.arg import process_args
     29 from chainlib.eth.cli.log import process_log
     30 from chainlib.eth.cli.config import Config
     31 from chainlib.eth.cli.config import process_config
     32 from chainlib.eth.constant import ZERO_CONTENT
     33 from hexathon import strip_0x
     34 
     35 # local imports
     36 from craft_nft import CraftNFT
     37 
     38 logg = logging.getLogger()
     39 
     40 
     41 def process_config_local(config, arg, args, flags):
     42     if config.get('_EXEC_ADDRESS') == None:
     43         raise ValueError('exec address required')
     44     token_id = strip_0x(config.get('_POSARG'))
     45     bytes.fromhex(token_id)
     46     config.add(token_id, '_TOKEN_ID', False)
     47 
     48     if args.nolimit:
     49         config.add(-1, '_TOKEN_COUNT', False)
     50     else: 
     51         assert args.count < 2**48
     52         config.add(args.count, '_TOKEN_COUNT', False)
     53 
     54     if args.fee_limit == None:
     55         config.add(200000, '_FEE_LIMIT', True)
     56 
     57     return config
     58 
     59 arg_flags = ArgFlag()
     60 arg = Arg(arg_flags)
     61 flags = arg_flags.STD_WRITE | arg_flags.VALUE | arg_flags.TAB | arg_flags.EXEC
     62 
     63 argparser = chainlib.eth.cli.ArgumentParser()
     64 argparser = process_args(argparser, arg, flags)
     65 argparser.add_argument('--nolimit', action='store_true', help='Unbounded token batch')
     66 argparser.add_argument('--count', default=0, type=int, help='Amount of tokens in batch')
     67 argparser.add_argument('token_id', type=str, nargs='*', help='token id: sha256 sum of token data, in hex')
     68 args = argparser.parse_args(sys.argv[1:])
     69 
     70 logg = process_log(args, logg)
     71 
     72 config = Config()
     73 config = process_config(config, arg, args, flags, positional_name='token_id')
     74 config = process_config_local(config, arg, args, flags)
     75 logg.debug('config loaded:\n{}'.format(config))
     76 
     77 settings = ChainSettings()
     78 settings = process_settings(settings, config)
     79 logg.debug('settings loaded:\n{}'.format(settings))
     80 
     81 
     82 def main():
     83     token_id = config.get('_TOKEN_ID')
     84     token_count = config.get('_TOKEN_COUNT')
     85     conn = settings.get('CONN')
     86 
     87     c = CraftNFT(
     88             settings.get('CHAIN_SPEC'),
     89             signer=settings.get('SIGNER'),
     90             gas_oracle=settings.get('FEE_ORACLE'),
     91             nonce_oracle=settings.get('NONCE_ORACLE')
     92             )
     93 
     94     (tx_hash_hex, o) = c.allocate(
     95             settings.get('EXEC'),
     96             settings.get('SENDER_ADDRESS'),
     97             token_id,
     98             token_count,
     99             )
    100 
    101     if config.get('_RPC_SEND'):
    102         conn.do(o)
    103         if config.true('_WAIT'):
    104             r = conn.wait(tx_hash_hex)
    105             if r['status'] == 0:
    106                 sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you')
    107                 sys.exit(1)
    108 
    109         print(tx_hash_hex)
    110     else:
    111         print(o)
    112 
    113 if __name__ == '__main__':
    114     main()