2020-12-07 11:41:06 +01:00
""" Mints and gifts tokens to a given address
2021-01-09 01:24:03 +01:00
2020-12-07 11:41:06 +01:00
. . moduleauthor : : Louis Holbrook < dev @holbrook.no >
. . pgp : : 0826 EDA1702D1E87C6E2875121D2E7BB88C2A746
"""
# SPDX-License-Identifier: GPL-3.0-or-later
# standard imports
2021-01-09 01:24:03 +01:00
import sys
2020-12-07 11:41:06 +01:00
import os
import json
import argparse
import logging
2021-01-09 01:24:03 +01:00
import time
2020-12-07 11:41:06 +01:00
# third-party imports
2021-01-09 23:12:08 +01:00
from crypto_dev_signer . eth . signer import ReferenceSigner as EIP155Signer
2021-03-22 14:56:27 +01:00
from crypto_dev_signer . keystore . dict import DictKeystore
from chainlib . eth . tx import receipt
2021-03-19 20:15:50 +01:00
from chainlib . chain import ChainSpec
2021-03-22 14:56:27 +01:00
from chainlib . eth . nonce import RPCNonceOracle
from chainlib . eth . gas import RPCGasOracle
from chainlib . eth . connection import EthHTTPConnection
# local imports
from giftable_erc20_token import GiftableToken
2020-12-07 11:41:06 +01:00
logging . basicConfig ( level = logging . WARNING )
logg = logging . getLogger ( )
2020-12-11 16:45:01 +01:00
script_dir = os . path . dirname ( __file__ )
data_dir = os . path . join ( script_dir , ' .. ' , ' data ' )
2020-12-07 11:41:06 +01:00
argparser = argparse . ArgumentParser ( )
argparser . add_argument ( ' -p ' , ' --provider ' , dest = ' p ' , default = ' http://localhost:8545 ' , type = str , help = ' Web3 provider url (http only) ' )
2021-01-09 01:24:03 +01:00
argparser . add_argument ( ' -e ' , action = ' store_true ' , help = ' Treat all transactions as essential ' )
argparser . add_argument ( ' -w ' , action = ' store_true ' , help = ' Wait for the last transaction to be confirmed ' )
argparser . add_argument ( ' -ww ' , action = ' store_true ' , help = ' Wait for every transaction to be confirmed ' )
2021-04-04 14:40:22 +02:00
argparser . add_argument ( ' -i ' , ' --chain-spec ' , dest = ' i ' , type = str , default = ' evm:ethereum:1 ' , help = ' Chain specification string ' )
2021-01-09 01:24:03 +01:00
argparser . add_argument ( ' -a ' , ' --token-address ' , required = ' True ' , dest = ' a ' , type = str , help = ' Giftable token address ' )
argparser . add_argument ( ' -y ' , ' --key-file ' , dest = ' y ' , type = str , help = ' Ethereum keystore file to use for signing ' )
2020-12-07 11:41:06 +01:00
argparser . add_argument ( ' -v ' , action = ' store_true ' , help = ' Be verbose ' )
2021-03-22 14:56:27 +01:00
argparser . add_argument ( ' -vv ' , action = ' store_true ' , help = ' Be more verbose ' )
argparser . add_argument ( ' --env-prefix ' , default = os . environ . get ( ' CONFINI_ENV_PREFIX ' ) , dest = ' env_prefix ' , type = str , help = ' environment prefix for variables to overwrite configuration ' )
2021-01-09 01:24:03 +01:00
argparser . add_argument ( ' --recipient ' , type = str , help = ' Recipient account address. If not set, tokens will be gifted to the keystore account ' )
2021-03-22 14:56:27 +01:00
argparser . add_argument ( ' value ' , type = int , help = ' Value of tokens to mint and gift ' )
2020-12-07 11:41:06 +01:00
args = argparser . parse_args ( )
2021-03-22 14:56:27 +01:00
if args . vv :
2020-12-07 11:41:06 +01:00
logg . setLevel ( logging . DEBUG )
2021-03-22 14:56:27 +01:00
elif args . v :
logg . setLevel ( logging . INFO )
2020-12-07 11:41:06 +01:00
2021-01-09 23:12:08 +01:00
block_all = args . ww
2021-02-15 18:18:18 +01:00
block_last = args . w or block_all
2021-01-09 01:24:03 +01:00
2021-03-22 14:56:27 +01:00
passphrase_env = ' ETH_PASSPHRASE '
if args . env_prefix != None :
passphrase_env = args . env_prefix + ' _ ' + passphrase_env
passphrase = os . environ . get ( passphrase_env )
if passphrase == None :
logg . warning ( ' no passphrase given ' )
passphrase = ' '
2021-01-09 01:24:03 +01:00
signer_address = None
2021-01-09 23:12:08 +01:00
keystore = DictKeystore ( )
2021-01-09 01:24:03 +01:00
if args . y != None :
logg . debug ( ' loading keystore file {} ' . format ( args . y ) )
2021-03-22 14:56:27 +01:00
signer_address = keystore . import_keystore_file ( args . y , password = passphrase )
2021-01-09 01:24:03 +01:00
logg . debug ( ' now have key for signer address {} ' . format ( signer_address ) )
2021-01-09 23:12:08 +01:00
signer = EIP155Signer ( keystore )
2021-02-19 13:55:55 +01:00
chain_spec = ChainSpec . from_chain_str ( args . i )
2021-01-09 23:12:08 +01:00
2021-03-22 14:56:27 +01:00
rpc = EthHTTPConnection ( args . p )
nonce_oracle = RPCNonceOracle ( signer_address , rpc )
gas_oracle = RPCGasOracle ( rpc , code_callback = GiftableToken . gas )
2021-01-09 01:24:03 +01:00
2021-03-22 14:56:27 +01:00
token_address = args . a
recipient_address = args . recipient
if recipient_address == None :
recipient_address = signer_address
token_value = args . value
2021-01-09 01:24:03 +01:00
2021-03-22 14:56:27 +01:00
def main ( ) :
2021-04-04 14:40:22 +02:00
c = GiftableToken ( chain_spec , signer = signer , gas_oracle = gas_oracle , nonce_oracle = nonce_oracle )
2021-03-22 14:56:27 +01:00
( tx_hash_hex , o ) = c . mint_to ( token_address , signer_address , recipient_address , token_value )
rpc . do ( o )
2021-03-22 18:13:59 +01:00
if block_last :
r = rpc . wait ( tx_hash_hex )
if r [ ' status ' ] == 0 :
sys . stderr . write ( ' EVM revert. Wish I had more to tell you ' )
sys . exit ( 1 )
2021-01-09 23:12:08 +01:00
2021-03-22 14:56:27 +01:00
logg . info ( ' mint to {} tx {} ' . format ( recipient_address , tx_hash_hex ) )
2021-01-09 23:12:08 +01:00
2021-03-22 14:56:27 +01:00
print ( tx_hash_hex )
2021-01-09 23:12:08 +01:00
2021-01-09 01:24:03 +01:00
sys . exit ( 0 )
2020-12-07 11:41:06 +01:00
2021-01-09 01:24:03 +01:00
if __name__ == ' __main__ ' :
main ( )