add explit nonce value to cast commands

This commit is contained in:
Carlosokumu 2024-12-12 14:02:27 +03:00
parent 7b9bff6541
commit 01d7de89c2
Signed by: carlos
GPG Key ID: 7BD6BC8160A5C953

View File

@ -382,7 +382,7 @@ def find_custodial_address(key_name):
filename = f"{key_name}.json" filename = f"{key_name}.json"
file_path = os.path.join(directory, filename) file_path = os.path.join(directory, filename)
# Check if the file exists # Check if the file exists with the keyname
if os.path.isfile(file_path): if os.path.isfile(file_path):
with open(file_path, "r") as f: with open(file_path, "r") as f:
custodial_account = json.load(f) custodial_account = json.load(f)
@ -422,10 +422,11 @@ def store_key_in_keystore(keystore_dir, private_key, key_name, address):
return store_path return store_path
def load_gas(address): def load_gas(address,nonce):
command = ( command = (
f"cast send {address} " f"cast send {address} "
f"--value {gas_topup} " f"--value {gas_topup} "
f"--nonce {nonce} "
f"--private-key {master_private_key} " f"--private-key {master_private_key} "
f"--rpc-url {rpc} " f"--rpc-url {rpc} "
f" --json " f" --json "
@ -465,6 +466,7 @@ def create_custodialaccount():
def do_custodial_token_transfer(transfer): def do_custodial_token_transfer(transfer):
#token transfer custodial endpoint
url = "http://localhost:5003/api/v2/token/transfer" url = "http://localhost:5003/api/v2/token/transfer"
headers = { headers = {
@ -520,6 +522,8 @@ def key_create_handler(cmd):
store_name = cmd.t store_name = cmd.t
keystore_dir = "user_store" keystore_dir = "user_store"
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: if cmd.k is None:
address, private_key = generate_private_key() address, private_key = generate_private_key()
@ -536,8 +540,9 @@ def key_create_handler(cmd):
raise ValueError("account address cannot be None") raise ValueError("account address cannot be None")
private_key = None private_key = None
keystore_dir = "custodialstore" keystore_dir = "custodialstore"
load_gas(address) load_gas(address,nonce)
store_key_in_keystore(keystore_dir, private_key, key_name, address) store_key_in_keystore(keystore_dir, private_key, key_name, address)
return address return address
@ -588,18 +593,22 @@ def voucher_create_handler(cmd):
store_voucher(private_key, symbol, contract_address) store_voucher(private_key, symbol, contract_address)
# sleep for 5 second to allow chain to sync
time.sleep(5)
master_address = w3.eth.account.from_key(master_private_key)
nonce = w3.eth.get_transaction_count(master_address.address,'pending')
# Command to add the token to the token index # Command to add the token to the token index
add_token_to_index = ( add_token_to_index = (
f"cast send --private-key {master_private_key} " f"cast send --private-key {master_private_key} "
f"--nonce {nonce} "
f"--rpc-url {rpc} " f"--rpc-url {rpc} "
f"{token_index} " f"{token_index} "
f'"add(address)" {contract_address} ' f'"add(address)" {contract_address} '
f" --json " f" --json "
) )
# sleep for 5 second to allow chain to sync
time.sleep(5)
result2 = subprocess.run( result2 = subprocess.run(
add_token_to_index, shell=True, capture_output=True, text=True add_token_to_index, shell=True, capture_output=True, text=True
) )
@ -653,7 +662,7 @@ def voucher_transfer_handler(cmd):
to = custodial_address to = custodial_address
token_transfer.to_address = to token_transfer.to_address = to
else: else:
store_path = os.path.join("custodial_store", f"{key_name}.json") store_path = os.path.join("user_store", f"{key_name}.json")
with open(store_path, "r") as file: with open(store_path, "r") as file:
data = json.load(file) data = json.load(file)
acct = w3.eth.account.from_key(data["private_key"]) acct = w3.eth.account.from_key(data["private_key"])
@ -674,7 +683,7 @@ def voucher_transfer_handler(cmd):
is_custodial_address = True is_custodial_address = True
token_transfer.from_address = custodial_address token_transfer.from_address = custodial_address
else: else:
store_path = os.path.join("custodial_store", f"{key_name}.json") store_path = os.path.join("user_store", f"{key_name}.json")
with open(store_path, "r") as file: with open(store_path, "r") as file:
data = json.load(file) data = json.load(file)
from_private_key = data["private_key"] from_private_key = data["private_key"]
@ -719,11 +728,15 @@ def voucher_mint_handler(cmd):
value = cmd.v value = cmd.v
if str(cmd.t).startswith("NameAgent"): if str(cmd.t).startswith("NameAgent"):
key_name = str(cmd.t).split(":")[1] key_name = str(cmd.t).split(":")[1]
store_path = os.path.join("custodial_store", f"{key_name}.json") custodial_address = find_custodial_address(key_name)
with open(store_path, "r") as file: if custodial_address is not None:
data = json.load(file) to = custodial_address
acct = w3.eth.account.from_key(data["private_key"]) else:
to = acct.address store_path = os.path.join("user_store", f"{key_name}.json")
with open(store_path, "r") as file:
data = json.load(file)
acct = w3.eth.account.from_key(data["private_key"])
to = acct.address
elif str(cmd.t).startswith("AddressAgent"): elif str(cmd.t).startswith("AddressAgent"):
to = "0x" + str(cmd.t).split(":")[1] to = "0x" + str(cmd.t).split(":")[1]
else: else: