eth-erc721

ERC-721 'NFT' token interface with example developer badge token contract
Info | Log | Files | Refs | LICENSE

mint.py (3393B)


      1 """Mints and gifts NFTs to a given address
      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 
     18 # external imports
     19 import chainlib.eth.cli
     20 from chainlib.eth.tx import receipt
     21 from chainlib.chain import ChainSpec
     22 from chainlib.eth.connection import EthHTTPConnection
     23 from chainlib.eth.address import to_checksum_address
     24 from hexathon import (
     25         strip_0x,
     26         add_0x,
     27         )
     28 
     29 # local imports
     30 from eth_badgetoken import BadgeToken
     31 
     32 logging.basicConfig(level=logging.WARNING)
     33 logg = logging.getLogger()
     34 
     35 script_dir = os.path.dirname(__file__)
     36 data_dir = os.path.join(script_dir, '..', 'data')
     37 
     38 arg_flags = chainlib.eth.cli.argflag_std_write | chainlib.eth.cli.Flag.EXEC | chainlib.eth.cli.Flag.WALLET
     39 argparser = chainlib.eth.cli.ArgumentParser(arg_flags)
     40 argparser.add_positional('token_id', type=str, help='32 bytes digest to use as token id')
     41 args = argparser.parse_args()
     42 extra_args = {
     43     'token_id': None,
     44     }
     45 config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, default_fee_limit=BadgeToken.gas())
     46 
     47 wallet = chainlib.eth.cli.Wallet()
     48 wallet.from_config(config)
     49 
     50 rpc = chainlib.eth.cli.Rpc(wallet=wallet)
     51 conn = rpc.connect_by_config(config)
     52 
     53 chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC'))
     54 
     55 token_id_bytes = bytes.fromhex(strip_0x(config.get('_TOKEN_ID')))
     56 if len(token_id_bytes) != 32:
     57     token_id_int = None
     58     try:
     59         token_id_int = int(config.get('_TOKEN_ID'))
     60     except:
     61         token_id_int = strip_0x(config.get('_TOKEN_ID'))
     62         token_id_int = int(token_id_int, 16)
     63     token_id_bytes = token_id_int.to_bytes(32, byteorder='big')
     64     logg.info('numeric token id value {} parsed to {}'.format(config.get('_TOKEN_ID'), token_id_bytes.hex()))
     65 token_id = int.from_bytes(token_id_bytes, byteorder='big')
     66 
     67 
     68 def main():
     69     signer = rpc.get_signer()
     70     signer_address = rpc.get_sender_address()
     71 
     72     gas_oracle = rpc.get_gas_oracle()
     73     nonce_oracle = rpc.get_nonce_oracle()
     74 
     75     c = BadgeToken(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle)
     76 
     77     recipient_address_input = config.get('_RECIPIENT')
     78     if recipient_address_input == None:
     79         recipient_address_input = signer_address
     80 
     81     recipient_address = add_0x(to_checksum_address(recipient_address_input))
     82     if not config.true('_UNSAFE') and recipient_address != add_0x(recipient_address_input):
     83         raise ValueError('invalid checksum address for recipient')
     84 
     85     token_address = add_0x(to_checksum_address(config.get('_EXEC_ADDRESS')))
     86     if not config.true('_UNSAFE') and token_address != add_0x(config.get('_EXEC_ADDRESS')):
     87         raise ValueError('invalid checksum address for contract')
     88 
     89     (tx_hash_hex, o) = c.mint_to(token_address, signer_address, recipient_address, token_id)
     90     if config.get('_RPC_SEND'):
     91         conn.do(o)
     92         if config.get('_WAIT'):
     93             r = conn.wait(tx_hash_hex)
     94             if r['status'] == 0:
     95                 sys.stderr.write('EVM revert. Wish I had more to tell you')
     96                 sys.exit(1)
     97 
     98         logg.info('mint to {} tx {}'.format(recipient_address, tx_hash_hex))
     99         print(tx_hash_hex)
    100     else:
    101         print(o)
    102 
    103 
    104 
    105 if __name__ == '__main__':
    106     main()