2020-11-17 19:23:57 +01:00
""" Deploys giftable token, and optionally gifts a set amount to all accounts in wallet
. . moduleauthor : : Louis Holbrook < dev @holbrook.no >
. . pgp : : 0826 EDA1702D1E87C6E2875121D2E7BB88C2A746
"""
# SPDX-License-Identifier: GPL-3.0-or-later
# standard imports
2020-12-01 23:54:17 +01:00
import os
2020-11-17 19:23:57 +01:00
import json
import argparse
import logging
# third-party imports
import web3
logging . basicConfig ( level = logging . WARNING )
logg = logging . getLogger ( )
logging . getLogger ( ' web3 ' ) . setLevel ( logging . WARNING )
logging . getLogger ( ' urllib3 ' ) . setLevel ( logging . WARNING )
2020-12-11 16:45:01 +01:00
script_dir = os . path . dirname ( __file__ )
data_dir = os . path . join ( script_dir , ' .. ' , ' data ' )
2020-11-17 19:23:57 +01:00
argparser = argparse . ArgumentParser ( )
argparser . add_argument ( ' -p ' , ' --provider ' , dest = ' p ' , default = ' http://localhost:8545 ' , type = str , help = ' Web3 provider url (http only) ' )
argparser . add_argument ( ' -n ' , ' --name ' , dest = ' n ' , default = ' Giftable Token ' , type = str , help = ' Token name ' )
argparser . add_argument ( ' -s ' , ' --symbol ' , dest = ' s ' , default = ' GFT ' , type = str , help = ' Token symbol ' )
argparser . add_argument ( ' -d ' , ' --decimals ' , dest = ' d ' , default = 18 , type = int , help = ' Token decimals ' )
2020-12-08 19:37:20 +01:00
argparser . add_argument ( ' -o ' , ' --owner ' , dest = ' o ' , type = str , help = ' Reserve owner account ' )
2020-11-30 11:37:58 +01:00
argparser . add_argument ( ' -a ' , ' --account ' , dest = ' a ' , action = ' append ' , type = str , help = ' Account to fund ' )
2020-12-06 14:12:49 +01:00
argparser . add_argument ( ' -m ' , ' --minter ' , dest = ' m ' , action = ' append ' , type = str , help = ' Minter to add ' )
2020-12-11 16:45:01 +01:00
argparser . add_argument ( ' --abi-dir ' , dest = ' abi_dir ' , type = str , default = data_dir , help = ' Directory containing bytecode and abi (default: {} ) ' . format ( data_dir ) )
2020-11-17 19:23:57 +01:00
argparser . add_argument ( ' -v ' , action = ' store_true ' , help = ' Be verbose ' )
argparser . add_argument ( ' amount ' , type = int , help = ' Initial token supply (will be owned by contract creator) ' )
args = argparser . parse_args ( )
if args . v :
logg . setLevel ( logging . DEBUG )
2020-12-01 23:54:17 +01:00
def main ( ) :
2020-11-17 19:23:57 +01:00
w3 = web3 . Web3 ( web3 . Web3 . HTTPProvider ( args . p ) )
2020-12-11 16:42:47 +01:00
f = open ( os . path . join ( args . abi_dir , ' GiftableToken.abi.json ' ) , ' r ' )
2020-11-17 19:23:57 +01:00
abi = json . load ( f )
f . close ( )
2020-12-11 16:42:47 +01:00
f = open ( os . path . join ( args . abi_dir , ' GiftableToken.bin ' ) , ' r ' )
2020-11-17 19:23:57 +01:00
bytecode = f . read ( )
f . close ( )
2020-11-30 11:37:58 +01:00
w3 . eth . defaultAccount = w3 . eth . accounts [ 0 ]
2020-12-08 19:37:20 +01:00
if args . o != None :
2020-12-08 20:58:46 +01:00
w3 . eth . defaultAccount = args . o
2020-11-17 19:23:57 +01:00
c = w3 . eth . contract ( abi = abi , bytecode = bytecode )
2020-12-06 14:12:49 +01:00
tx_hash = c . constructor ( args . n , args . s , args . d ) . transact ( )
2020-11-17 19:23:57 +01:00
rcpt = w3 . eth . getTransactionReceipt ( tx_hash )
address = rcpt . contractAddress
c = w3 . eth . contract ( abi = abi , address = address )
logg . debug ( ' construct tx {} address {} ' . format ( tx_hash . hex ( ) , address ) )
2020-12-06 14:12:49 +01:00
2020-11-17 19:23:57 +01:00
balance = c . functions . balanceOf ( w3 . eth . defaultAccount ) . call ( )
2020-12-06 14:12:49 +01:00
logg . info ( ' balance {} : {} {} ' . format ( w3 . eth . defaultAccount , balance , tx_hash ) )
2020-11-17 19:23:57 +01:00
2020-11-30 11:37:58 +01:00
if args . a != None :
for a in args . a :
2020-11-17 19:23:57 +01:00
if a == w3 . eth . defaultAccount :
continue
2020-12-06 14:12:49 +01:00
tx_hash_mint = c . functions . mint ( args . amount ) . transact ( )
tx_hash_transfer = c . functions . transfer ( a , args . amount ) . transact ( )
2020-11-17 19:23:57 +01:00
rcpt = w3 . eth . getTransactionReceipt ( tx_hash )
balance = c . functions . balanceOf ( a ) . call ( )
logg . info ( ' balance {} : {} ' . format ( a , balance ) )
2020-12-06 14:12:49 +01:00
if args . m != None :
for a in args . m :
if a == w3 . eth . defaultAccount :
continue
tx_hash_minter = c . functions . addMinter ( a ) . transact ( )
logg . info ( ' minter {} ' . format ( a ) )
2020-11-17 19:23:57 +01:00
print ( address )
2020-12-01 23:54:17 +01:00
if __name__ == ' __main__ ' :
main ( )