eth-erc721

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

transfer.py (3358B)


      1 """Transfers an ERC721 NFT between accounts
      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 eth_erc721 import ERC721
     38 
     39 logg = logging.getLogger()
     40 
     41 
     42 def process_config_local(config, arg, args, flags):
     43     token_id_in = config.get('_POSARG')
     44     is_hex = False
     45     try:
     46         token_id = strip_0x(token_id_in)
     47         if token_id != token_id_in:
     48             is_hex = True
     49     except ValueError:
     50         pass
     51 
     52     token_id = None
     53     if is_hex:
     54         token_id = int(token_id_in, 16)
     55     else:
     56         token_id = int(token_id_in, 10)
     57     config.add(token_id, '_TOKEN_ID', False)
     58 
     59     return config
     60 
     61 
     62 def process_settings_local(settings, config):
     63     settings.set('VALUE', config.get('_TOKEN_ID'))
     64     return settings
     65 
     66 
     67 arg_flags = ArgFlag()
     68 arg = Arg(arg_flags)
     69 flags = arg_flags.STD_WRITE | arg_flags.WALLET | arg_flags.VALUE | arg_flags.TAB | arg_flags.EXEC
     70 
     71 argparser = chainlib.eth.cli.ArgumentParser()
     72 argparser = process_args(argparser, arg, flags)
     73 argparser.add_argument('token_id', type=str, nargs='*', help='Token ID to transfer. Prefix with 0x to specify as hex.')
     74 args = argparser.parse_args(sys.argv[1:])
     75 
     76 logg = process_log(args, logg)
     77 
     78 config = Config()
     79 config = process_config(config, arg, args, flags, positional_name='token_id')
     80 config = process_config_local(config, arg, args, flags)
     81 logg.debug('config loaded:\n{}'.format(config))
     82 
     83 settings = ChainSettings()
     84 settings = process_settings(settings, config)
     85 settings = process_settings_local(settings, config)
     86 logg.debug('settings loaded:\n{}'.format(settings))
     87 
     88 
     89 def main():
     90     token_address = settings.get('EXEC')
     91     signer_address = settings.get('SENDER_ADDRESS')
     92     recipient = settings.get('RECIPIENT')
     93     value = settings.get('VALUE')
     94     conn = settings.get('CONN')
     95     g = ERC721(
     96             settings.get('CHAIN_SPEC'),
     97             signer=settings.get('SIGNER'),
     98             gas_oracle=settings.get('GAS_ORACLE'),
     99             nonce_oracle=settings.get('NONCE_ORACLE'),
    100             )
    101     
    102     (tx_hash_hex, o) = g.transfer_from(token_address, signer_address, signer_address, recipient, value, id_generator=settings.get('RPC_ID_GENERATOR'))
    103 
    104     if settings.get('RPC_SEND'):
    105         conn.do(o)
    106         if settings.get('WAIT'):
    107             r = conn.wait(tx_hash_hex)
    108             if r['status'] == 0:
    109                 logg.critical('VM revert. Wish I could tell you more')
    110                 sys.exit(1)
    111         print(tx_hash_hex)
    112 
    113     else:
    114         print(o['params'][0])
    115 
    116 
    117 if __name__ == '__main__':
    118     main()
    119