craft-nft

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

mint.py (4604B)


      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 chainlib.eth.address import to_checksum_address
     34 from hexathon import strip_0x
     35 
     36 # local imports
     37 from craft_nft import CraftNFT
     38 
     39 logg = logging.getLogger()
     40 
     41 
     42 def process_config_local(config, arg, args, flags):
     43     token_id = strip_0x(args.token_id)
     44     bytes.fromhex(token_id)
     45     config.add(token_id, '_TOKEN_ID', False)
     46 
     47     config.add(args.batch, '_TOKEN_BATCH', False)
     48     config.add(args.index, '_TOKEN_INDEX', False)
     49 
     50     if args.fee_limit == None:
     51         config.add(200000, '_FEE_LIMIT', True)
     52 
     53     return config
     54 
     55 
     56 def process_settings_local(settings, config):
     57     settings.set('TOKEN_ID', config.get('_TOKEN_ID'))
     58 
     59     if config.get('_POSARG') != None:
     60         recipient = to_checksum_address(config.get('_POSARG'))
     61         settings.set('RECIPIENT', recipient)
     62 
     63     settings.set('TOKEN_INDEX', config.get('_TOKEN_INDEX'))
     64 
     65     if (config.get('_TOKEN_BATCH') != None):
     66         settings.set('TOKEN_BATCH', config.get('_TOKEN_BATCH'))
     67         return settings
     68 
     69     conn = settings.get('CONN')
     70     c = CraftNFT(settings.get('CHAIN_SPEC'))
     71     i = 0
     72     while True:
     73         o = c.get_token_spec(
     74                 settings.get('EXEC'),
     75                 settings.get('TOKEN_ID'),
     76                 i,
     77                 )
     78         r = conn.do(o)
     79         spec = c.parse_token_spec(r)
     80         if spec.count == 0:
     81             if spec.cursor == 0:
     82                 settings.set('TOKEN_BATCH', 0)
     83                 return settings
     84         if spec.cursor < spec.count:
     85             settings.set('TOKEN_BATCH', i)
     86             return settings
     87 
     88         i += 1
     89 
     90 
     91 arg_flags = ArgFlag()
     92 arg = Arg(arg_flags)
     93 flags = arg_flags.STD_WRITE | arg_flags.WALLET | arg_flags.VALUE | arg_flags.TAB | arg_flags.EXEC
     94 
     95 argparser = chainlib.eth.cli.ArgumentParser()
     96 argparser = process_args(argparser, arg, flags)
     97 argparser.add_argument('--token-id', type=str, required=True, help='Token id to mint from')
     98 argparser.add_argument('--check', action='store_true', help='Only check whether a token can be minted')
     99 argparser.add_argument('--batch', type=int, default=0, help='Mint from the given batch. If not specified, the first mintable batch will be used')
    100 argparser.add_argument('--index', type=int, help='Index of token in batch to mint. If not specified, will mint next available index.')
    101 argparser.add_argument('token_recipient', type=str, nargs='*', help='Recipient address')
    102 args = argparser.parse_args(sys.argv[1:])
    103 
    104 logg = process_log(args, logg)
    105 
    106 config = Config()
    107 config = process_config(config, arg, args, flags, positional_name='token_recipient')
    108 config = process_config_local(config, arg, args, flags)
    109 logg.debug('config loaded:\n{}'.format(config))
    110 
    111 settings = ChainSettings()
    112 settings = process_settings(settings, config)
    113 settings = process_settings_local(settings, config)
    114 logg.debug('settings loaded:\n{}'.format(settings))
    115 
    116 
    117 def main():
    118     conn = settings.get('CONN')
    119 
    120     c = CraftNFT(
    121             settings.get('CHAIN_SPEC'),
    122             signer=settings.get('SIGNER'),
    123             gas_oracle=settings.get('FEE_ORACLE'),
    124             nonce_oracle=settings.get('NONCE_ORACLE')
    125             )
    126 
    127     (tx_hash_hex, o) = c.mint_to(
    128             settings.get('EXEC'),
    129             settings.get('SENDER_ADDRESS'),
    130             settings.get('RECIPIENT'),
    131             settings.get('TOKEN_ID'),
    132             settings.get('TOKEN_BATCH'),
    133             index=settings.get('TOKEN_INDEX'),
    134             )
    135 
    136     if config.get('_RPC_SEND'):
    137         conn.do(o)
    138         if config.true('_WAIT'):
    139             r = conn.wait(tx_hash_hex)
    140             if r['status'] == 0:
    141                 sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you')
    142                 sys.exit(1)
    143         print(tx_hash_hex)
    144     else:
    145         print(o)
    146 
    147 if __name__ == '__main__':
    148     main()