From 9aaa1259486cd0cf1c41d533d7e8dd2ba22f8c8c Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Fri, 6 Dec 2024 10:23:19 +0300 Subject: [PATCH] add command to add token to token index --- parse.py | 66 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/parse.py b/parse.py index 71df782..3005cd2 100644 --- a/parse.py +++ b/parse.py @@ -69,6 +69,9 @@ privatekey = os.getenv("PRIVATE_KEY") chainId = os.getenv("CHAIN_ID") rpc = os.getenv("RPC") gasCap = os.getenv("GAS_FEE_CAP") +master_private_key = os.getenv("MASTER_PRIVATE_KEY") +token_index = os.getenv("TOKEN_INDEX") +gas_topup = os.getenv("GAS_TOPUP") w3 = Web3(Web3.HTTPProvider(rpc)) @@ -368,7 +371,7 @@ def load_gas(address,nonce): global call_count command = ( f'cast send {address} ' - f'--value 0.01ether ' + f'--value {gas_topup} ' f'--nonce {nonce} ' f'--private-key {privatekey} ' f'--rpc-url {rpc} ' @@ -385,8 +388,9 @@ def load_gas(address,nonce): def store_voucher(voucher_creator,voucher_symbol,voucher_address): vouchers = [] voucher = { - voucher_symbol: voucher_address, - 'owner': voucher_creator + 'voucher_address': voucher_address, + 'owner': voucher_creator, + 'symbol': voucher_symbol } vouchers.append(voucher) try: @@ -422,25 +426,21 @@ def voucher_create_handler(cmd): name = cmd.n symbol = cmd.s - random_ascii = ''.join(random.choices(string.ascii_letters, k=3)) - + random_ascii = ''.join(random.choices(string.ascii_letters, k=2)) try: with open("vouchers.json", 'r') as f: existing_vouchers = json.load(f) except (FileNotFoundError): existing_vouchers = [] + voucher_symbols = list(map(lambda symbol: symbol['symbol'], existing_vouchers)) + + if symbol in voucher_symbols: + symbol = symbol + random_ascii - for voucher in existing_vouchers: - try: - value = voucher[cmd.s] - symbol = symbol + random_ascii - print(f"Value for {symbol}: {value}") - except KeyError: - #Skip appending random ascii letters - print("Symbol does not exist") if privatekey.startswith("0x"): private_key = privatekey[2:] + #Command to create a voucher command = f'ge-publish --private-key {private_key} --json ' \ f'--rpc {rpc} --gas-fee-cap {gasCap} --chainid {chainId} ' \ f'p erc20 --name "{name}" --symbol "{symbol}"' @@ -458,15 +458,33 @@ def voucher_create_handler(cmd): message = f"Voucher {name} created with address {contract_address} and symbol {symbol}" store_voucher(private_key,symbol,contract_address) printMessage(message) + + #Command to add the token to the token index + add_token = ( + f'cast send --private-key {master_private_key} ' + f'--rpc-url {rpc} ' + f'{token_index} ' + f'"add(address)" {contract_address} ' + f' --json ' + ) + result2 = subprocess.run(add_token, shell=True, capture_output=True, text=True) + if result2.returncode != 0: + raise subprocess.CalledProcessError(result.returncode, command, output=result.stdout, stderr=result.stderr) return contract_address - + def voucher_transfer_handler(cmd): value = cmd.v # Amount to transfer if str(cmd.a).startswith("NameAgent"): voucher_name = str(cmd.a).split(":")[1] with open("vouchers.json", "r") as file: data = json.load(file) - s = data[voucher_name] + + voucher_symbols = list(map(lambda symbol: symbol['symbol'], data)) + voucher_address = list(map(lambda address: address['voucher_address'], data)) + if voucher_name in voucher_symbols: + index = voucher_symbols.index(voucher_name) + s = voucher_address[index] + elif str(cmd.a).startswith("AddressAgent"): s = "0x" + str(cmd.a).split(":")[1] else: @@ -497,6 +515,8 @@ def voucher_transfer_handler(cmd): if result.stderr: raise ValueError(f"Command failed with error: {result.stderr}") data = json.loads(result.stdout) + message = f"Transfered {value} {voucher_name} to {to} " + printMessage(message) return data["transactionHash"] @@ -517,16 +537,22 @@ def voucher_mint_handler(cmd): raise ValueError(f"Invalid command: {cmd.t}. Expected 'NameAgent' or 'AddressAgent'.") if str(cmd.a).startswith("NameAgent"): - voucher_name = str(cmd.a).split(":")[1] + voucher_symbol = str(cmd.a).split(":")[1] with open("vouchers.json", "r") as file: data = json.load(file) - s = data[voucher_name] - privatekey = data["owner"] + voucher_symbols = list(map(lambda symbol: symbol['symbol'], data)) + voucher_address = list(map(lambda address: address['voucher_address'], data)) + voucher_owners = list(map(lambda address: address['owner'], data)) + if voucher_symbol in voucher_symbols: + index = voucher_symbols.index(voucher_symbol) + s = voucher_address[index] + privatekey = voucher_owners[index] + elif str(cmd.a).startswith("AddressAgent"): s = "0x" + str(cmd.a).split(":")[1] else: raise ValueError(f"Invalid command: {cmd.t}. Expected 'NameAgent' or 'AddressAgent'.") - + command = ( f'cast send --private-key {privatekey} ' f'--rpc-url {rpc} ' @@ -540,6 +566,8 @@ def voucher_mint_handler(cmd): if result.stderr: raise ValueError(f"Command failed with error: {result.stderr}") data = json.loads(result.stdout) + message = f"Added supply to {voucher_symbol} with value {value} and address {voucher_address}" + printMessage(message) return data["transactionHash"]