update handlers: add thread sleep to allow token registration

This commit is contained in:
Carlosokumu 2024-12-06 14:08:43 +03:00
parent 9aaa125948
commit d1a794fe37
Signed by: carlos
GPG Key ID: 7BD6BC8160A5C953

View File

@ -2,6 +2,7 @@ import json
import random
import re
import string
import time
import ply.lex as lex
import ply.yacc as yacc
import enum
@ -64,11 +65,12 @@ lexer = lex.lex()
load_dotenv()
#Chain params
privatekey = os.getenv("PRIVATE_KEY")
#Chain Params
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")
@ -368,12 +370,11 @@ def store_key_in_keystore(private_key, key_name, address):
return store_path
def load_gas(address,nonce):
global call_count
command = (
f'cast send {address} '
f'--value {gas_topup} '
f'--nonce {nonce} '
f'--private-key {privatekey} '
f'--private-key {master_private_key} '
f'--rpc-url {rpc} '
f' --json '
)
@ -409,16 +410,21 @@ def store_voucher(voucher_creator,voucher_symbol,voucher_address):
def key_create_handler(cmd):
key_name = str(cmd.f).split(":")[1]
master_address = w3.eth.account.from_key(privatekey)
master_address = w3.eth.account.from_key(master_private_key)
nonce = w3.eth.get_transaction_count(master_address.address,'pending')
if cmd.k is None:
address,private_key = generate_private_key()
else:
if private_key.startswith("0x"):
private_key = private_key[2:]
address = w3.eth.account.from_key(privatekey)
if cmd.k.startswith("0x"):
private_key = cmd.k[2:]
else:
private_key = cmd.k
address = w3.eth.account.from_key(private_key).address
load_gas(address,nonce)
store_key_in_keystore(private_key, key_name, address)
return address
@ -426,7 +432,7 @@ def voucher_create_handler(cmd):
name = cmd.n
symbol = cmd.s
random_ascii = ''.join(random.choices(string.ascii_letters, k=2))
random_ascii = ''.join(random.choices(string.ascii_letters, k=2)).upper()
try:
with open("vouchers.json", 'r') as f:
existing_vouchers = json.load(f)
@ -437,17 +443,20 @@ def voucher_create_handler(cmd):
if symbol in voucher_symbols:
symbol = symbol + random_ascii
if privatekey.startswith("0x"):
private_key = privatekey[2:]
if master_private_key.startswith("0x"):
private_key = master_private_key[2:]
else:
private_key = master_private_key
#Command to create a voucher
command = f'ge-publish --private-key {private_key} --json ' \
publish_token = f'ge-publish --private-key {private_key} --json ' \
f'--rpc {rpc} --gas-fee-cap {gasCap} --chainid {chainId} ' \
f'p erc20 --name "{name}" --symbol "{symbol}"'
result = subprocess.run(command, shell=True, capture_output=True, text=True)
result = subprocess.run(publish_token, shell=True, capture_output=True, text=True)
if result.returncode != 0:
raise subprocess.CalledProcessError(result.returncode, command, output=result.stdout, stderr=result.stderr)
raise subprocess.CalledProcessError(result.returncode, publish_token, output=result.stdout, stderr=result.stderr)
output_lines = result.stderr.strip().split("\n")
deployment_result = output_lines[1]
try:
@ -455,25 +464,35 @@ def voucher_create_handler(cmd):
contract_address = data.get('contract_address', None)
except json.JSONDecodeError as e:
print("Error parsing JSON:", e)
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 = (
add_token_to_index = (
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)
#sleep for 5 second to allow chain to sync
time.sleep(5)
result2 = subprocess.run(add_token_to_index, shell=True, capture_output=True, text=True)
if result2.returncode != 0:
raise subprocess.CalledProcessError(result.returncode, command, output=result.stdout, stderr=result.stderr)
raise subprocess.CalledProcessError(result2.returncode, add_token_to_index, output=result2.stdout, stderr=result2.stderr)
message = f"Voucher {name} created with address {contract_address} and symbol {symbol} and added to token index: {token_index}"
printMessage(message)
return contract_address
def voucher_transfer_handler(cmd):
#Amount to transfer
value = cmd.v # Amount to transfer
#Token symbol to transfer
if str(cmd.a).startswith("NameAgent"):
voucher_name = str(cmd.a).split(":")[1]
with open("vouchers.json", "r") as file:
@ -501,14 +520,26 @@ def voucher_transfer_handler(cmd):
to = "0x" + str(cmd.t).split(":")[1]
else:
raise ValueError(f"Invalid command: {cmd.t}. Expected 'NameAgent' or 'AddressAgent'.")
if str(cmd.f).startswith("NameAgent"):
key_name = str(cmd.f).split(":")[1]
store_path = os.path.join("custodial_store", f"{key_name}.json")
with open(store_path, "r") as file:
data = json.load(file)
from_private_key = data["private_key"]
elif str(cmd.f).startswith("AddressAgent"):
from_private_key = "0x" + str(cmd.f).split(":")[1]
else:
raise ValueError(f"Invalid command: {cmd.t}. Expected 'NameAgent' or 'AddressAgent'.")
command = (
f'cast send --private-key {privatekey} '
f'cast send --private-key {from_private_key} '
f'--rpc-url {rpc} '
f'{s} '
f'"transfer(address,uint256)" {to} {value}'
f' --json '
)
result = subprocess.run(command, shell=True, capture_output=True, text=True)
if result.returncode != 0:
raise subprocess.CalledProcessError(result.returncode, command, output=result.stdout, stderr=result.stderr)
@ -540,13 +571,17 @@ def voucher_mint_handler(cmd):
voucher_symbol = str(cmd.a).split(":")[1]
with open("vouchers.json", "r") as file:
data = json.load(file)
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]
else:
raise ValueError(f"Voucher with symbol {voucher_symbol} was not found")
elif str(cmd.a).startswith("AddressAgent"):
s = "0x" + str(cmd.a).split(":")[1]
@ -566,7 +601,7 @@ 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}"
message = f"Added supply of {value} {voucher_symbol} to :{to}"
printMessage(message)
return data["transactionHash"]