update handlers: append new vouchers to json store
This commit is contained in:
parent
dc87181e0a
commit
78fc736ed7
68
parse.py
68
parse.py
@ -1,5 +1,7 @@
|
||||
import json
|
||||
import random
|
||||
import re
|
||||
import string
|
||||
import ply.lex as lex
|
||||
import ply.yacc as yacc
|
||||
import enum
|
||||
@ -58,6 +60,8 @@ def t_error(t):
|
||||
|
||||
lexer = lex.lex()
|
||||
|
||||
|
||||
|
||||
load_dotenv()
|
||||
|
||||
#Chain params
|
||||
@ -322,6 +326,14 @@ def noop_handler(cmd):
|
||||
return str(cmd)
|
||||
|
||||
|
||||
def printMessage(message):
|
||||
box_width = len(message) + 4
|
||||
print("+" + "-" * (box_width - 2) + "+")
|
||||
print("| " + message + " |")
|
||||
print("+" + "-" * (box_width - 2) + "+")
|
||||
|
||||
|
||||
|
||||
def remove_ansi_escape_codes(text):
|
||||
return re.sub(r'\u001b\[.*?m', '', text)
|
||||
|
||||
@ -352,24 +364,56 @@ 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 0.01ether '
|
||||
f'--nonce {nonce} '
|
||||
f'--private-key {privatekey} '
|
||||
f'--rpc-url {rpc} '
|
||||
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)
|
||||
|
||||
message = f"Added some gas to {address}"
|
||||
printMessage(message)
|
||||
|
||||
|
||||
def store_voucher(voucher_creator,voucher_symbol,voucher_address):
|
||||
vouchers = []
|
||||
voucher = {
|
||||
voucher_symbol: voucher_address,
|
||||
'owner': voucher_creator
|
||||
}
|
||||
vouchers.append(voucher)
|
||||
try:
|
||||
with open("vouchers.json", 'r') as f:
|
||||
existing_vouchers = json.load(f)
|
||||
except (FileNotFoundError):
|
||||
existing_vouchers = []
|
||||
|
||||
for entry in existing_vouchers:
|
||||
vouchers.append(entry)
|
||||
|
||||
with open("vouchers.json", 'w') as f:
|
||||
json.dump(voucher, f)
|
||||
json.dump(vouchers, f)
|
||||
|
||||
|
||||
|
||||
def key_create_handler(cmd):
|
||||
key_name = str(cmd.f).split(":")[1]
|
||||
|
||||
master_address = w3.eth.account.from_key(privatekey)
|
||||
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)
|
||||
load_gas(address,nonce)
|
||||
store_key_in_keystore(private_key, key_name, address)
|
||||
return address
|
||||
|
||||
@ -378,6 +422,22 @@ def voucher_create_handler(cmd):
|
||||
name = cmd.n
|
||||
symbol = cmd.s
|
||||
|
||||
random_ascii = ''.join(random.choices(string.ascii_letters, k=3))
|
||||
|
||||
try:
|
||||
with open("vouchers.json", 'r') as f:
|
||||
existing_vouchers = json.load(f)
|
||||
except (FileNotFoundError):
|
||||
existing_vouchers = []
|
||||
|
||||
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:]
|
||||
|
||||
@ -395,7 +455,9 @@ def voucher_create_handler(cmd):
|
||||
contract_address = data.get('contract_address', None)
|
||||
except json.JSONDecodeError as e:
|
||||
print("Error parsing JSON:", e)
|
||||
print("Voucher created with contract address:",contract_address)
|
||||
message = f"Voucher {name} created with address {contract_address} and symbol {symbol}"
|
||||
store_voucher(private_key,symbol,contract_address)
|
||||
printMessage(message)
|
||||
return contract_address
|
||||
|
||||
def voucher_transfer_handler(cmd):
|
||||
|
Loading…
Reference in New Issue
Block a user