craft-nft

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

publish.py (3817B)


      1 """Publish a new NFT smart contract to the network
      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     config.add(args.name, '_TOKEN_NAME', False)
     43     config.add(args.symbol, '_TOKEN_SYMBOL', False)
     44 
     45 #    declaration_hash = ZERO_CONTENT
     46 #    if args.declaration_file != None:
     47 #        f = open(args.declaration_file, 'r')
     48 #        declaration = f.read()
     49 #        f.close()
     50 #        h = hashlib.sha256()
     51 #        h.update(declaration.encode('utf-8'))
     52 #        z = h.digest()
     53 #        declaration_hash = z.hex()
     54 #    declaration_hash = strip_0x(declaration_hash)
     55 #    config.add(declaration_hash, '_TOKEN_DECLARATIONÍ„', False)
     56 #    logg.debug('declaration hash is {}'.format(declaration_hash))
     57     if args.fee_limit == None:
     58         config.add(CraftNFT.gas(), '_FEE_LIMIT', True)
     59 
     60     return config
     61 
     62 arg_flags = ArgFlag()
     63 arg = Arg(arg_flags)
     64 flags = arg_flags.STD_WRITE | arg_flags.WALLET | arg_flags.VALUE | arg_flags.TAB
     65 
     66 argparser = chainlib.eth.cli.ArgumentParser()
     67 argparser.add_argument('--name', type=str, required=True, help='Token name')
     68 argparser.add_argument('--symbol', type=str, required=True, help='Token symbol')
     69 #argparser.add_argument('--declaration-file', dest='declaration_file', type=str, help='File describing the purpose and terms of the token')
     70 argparser = process_args(argparser, arg, flags)
     71 args = argparser.parse_args(sys.argv[1:])
     72 
     73 logg = process_log(args, logg)
     74 
     75 config = Config()
     76 config = process_config(config, arg, args, flags)
     77 config = process_config_local(config, arg, args, flags)
     78 logg.debug('config loaded:\n{}'.format(config))
     79 
     80 settings = ChainSettings()
     81 settings = process_settings(settings, config)
     82 logg.debug('settings loaded:\n{}'.format(settings))
     83 
     84 
     85 def main():
     86     token_name = config.get('_TOKEN_NAME')
     87     token_symbol = config.get('_TOKEN_SYMBOL')
     88     #token_declaration = config.get('_TOKEN_DECLARATIONÍ„')
     89     conn = settings.get('CONN')
     90 
     91     c = CraftNFT(
     92             settings.get('CHAIN_SPEC'),
     93             signer=settings.get('SIGNER'),
     94             gas_oracle=settings.get('FEE_ORACLE'),
     95             nonce_oracle=settings.get('NONCE_ORACLE')
     96             )
     97 
     98     (tx_hash_hex, o) = c.constructor(
     99             settings.get('SENDER_ADDRESS'),
    100             token_name,
    101             token_symbol,
    102             #token_declaration,
    103             #enumeration=True,
    104             )
    105     if config.get('_RPC_SEND'):
    106         conn.do(o)
    107         if config.true('_WAIT'):
    108             r = conn.wait(tx_hash_hex)
    109             if r['status'] == 0:
    110                 sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you')
    111                 sys.exit(1)
    112             # TODO: pass through translator for keys (evm tester uses underscore instead of camelcase)
    113             address = r['contractAddress']
    114 
    115             print(address)
    116         else:
    117             print(tx_hash_hex)
    118     else:
    119         print(o)
    120 
    121 if __name__ == '__main__':
    122     main()