data-seeding-ng/parse.py

900 lines
23 KiB
Python
Raw Normal View History

2024-11-25 09:39:12 +01:00
import json
import random
2024-11-26 08:05:11 +01:00
import re
import string
import time
2024-10-27 03:55:06 +01:00
import ply.lex as lex
import ply.yacc as yacc
import enum
import uuid
2024-10-28 00:07:20 +01:00
import os
import logging
2024-10-28 01:45:07 +01:00
import sys
import select
2024-10-28 00:07:20 +01:00
from multiprocessing import Process
from multiprocessing import Pipe
2024-11-19 17:03:58 +01:00
import subprocess
2024-12-11 15:46:38 +01:00
import requests
import requests.adapters
2024-11-25 09:39:12 +01:00
from web3 import Web3
2024-11-23 11:23:16 +01:00
from dotenv import load_dotenv
2024-10-28 00:07:20 +01:00
logg = logging.getLogger()
logg.setLevel(logging.DEBUG)
2024-10-27 03:55:06 +01:00
2024-11-25 09:39:12 +01:00
2024-10-27 03:55:06 +01:00
tokens = (
2024-12-11 15:46:38 +01:00
"ID",
"NOID",
"HEX",
"LABEL",
"VALUE",
"END",
2024-10-27 03:55:06 +01:00
)
2024-12-11 15:46:38 +01:00
t_ID = r"[a-zA-Z0-9]+(-[a-zA-Z0-9]+)+"
t_NOID = r"\-"
t_LABEL = r"[a-zA-Z_]+"
t_VALUE = r"\d+"
t_END = r"\n"
t_HEX = r"0x([a-fA-F0-9])+"
2024-10-27 03:55:06 +01:00
2024-12-11 15:46:38 +01:00
# def t_VALUE(t):
2024-10-27 03:55:06 +01:00
# r'\d+'
# t.value = int(t.value)
2024-12-11 15:46:38 +01:00
# def t_HEX(t):
2024-10-27 03:55:06 +01:00
# r'0x([a-fA-F0-9])+'
2024-12-11 15:46:38 +01:00
# print(t)
# try:
# t.value = bytes.fromhex(t.value)
# except:
# return False
# return t.value
# t.value = t.value
2024-10-27 03:55:06 +01:00
# pass
2024-12-11 15:46:38 +01:00
t_ignore = " \t"
2024-10-27 03:55:06 +01:00
def t_error(t):
print("problem: ", t.value[0])
t.lexer.skip(1)
2024-11-23 11:23:16 +01:00
2024-12-11 15:46:38 +01:00
lexer = lex.lex()
2024-11-23 11:23:16 +01:00
load_dotenv()
2024-12-11 15:46:38 +01:00
# Chain Params
chainId = os.getenv("CHAIN_ID","44787")
rpc = os.getenv("RPC","https://alfajores-forno.celo-testnet.org/")
gas_cap = os.getenv("GAS_FEE_CAP","35000000000")
master_private_key = os.getenv("MASTER_PRIVATE_KEY")
token_index = os.getenv("TOKEN_INDEX","0xD774bc082003eaF8DF74eEcD43AD44F03D488418")
gas_topup = os.getenv("GAS_TOPUP","0.01ether")
bearer_token = os.getenv("BEARER_TOKEN")
2024-11-23 11:23:16 +01:00
2024-11-25 09:39:12 +01:00
w3 = Web3(Web3.HTTPProvider(rpc))
2024-11-23 11:23:16 +01:00
2024-10-27 03:55:06 +01:00
#
2024-12-11 15:46:38 +01:00
# data = '''
# FOOBAR uf-2etg 0xa3bdefa momo 123
2024-10-27 03:55:06 +01:00
#
2024-12-11 15:46:38 +01:00
# BARBAR
# BAZ
2024-10-27 03:55:06 +01:00
#
2024-12-11 15:46:38 +01:00
# BASFB foo-bar-baz
2024-10-27 03:55:06 +01:00
#'''
#
2024-12-11 15:46:38 +01:00
# lexer.input(data)
2024-10-27 03:55:06 +01:00
#
2024-12-11 15:46:38 +01:00
# while True:
2024-10-27 03:55:06 +01:00
# tok = lexer.token()
# if not tok:
# break
# print(tok)
class VoucherTransfer:
2024-12-11 15:46:38 +01:00
def __init__(
self, to_address=None, from_address=None, amount=None, token_address=None,decimals = None
2024-12-11 15:46:38 +01:00
):
self.to_address = to_address
self.from_address = from_address
self.decimals = decimals
2024-12-11 15:46:38 +01:00
self.amount = amount
self.token_address = token_address
class VoucherDetail:
def __init__(
self, name =None, symbol =None, decimals =None,owner = None,address = None
):
self.name = name
self.owner = owner
self.address = address
self.symbol = symbol
self.decimals = decimals
2024-12-11 15:46:38 +01:00
2024-10-27 03:55:06 +01:00
class CmdId(enum.IntEnum):
KEY_CREATE = 0x1
VOUCHER_CREATE = 0x10
VOUCHER_MINT = 0x11
VOUCHER_TRANSFER = 0x12
WAIT = 0x20
NEED = 0x21
PEEK = 0x80
class Agent:
def __str__(self):
2024-12-11 15:46:38 +01:00
return (
self.__class__.__name__ + ":"
) # {}'.format(self.__class__.__name__, self.v)
2024-10-27 03:55:06 +01:00
class AddressAgent(Agent):
def __init__(self, v):
self.v = bytes.fromhex(v[2:])
def __str__(self):
return Agent.__str__(self) + self.v.hex()
class NameAgent(Agent):
def __init__(self, v):
self.v = v
def __str__(self):
return Agent.__str__(self) + self.v
2024-12-11 15:46:38 +01:00
class Cmd:
2024-10-27 03:55:06 +01:00
def __init__(self, cmd):
self.c = CmdId[cmd]
self.i = None
self.a = None
self.f = None
self.t = None
self.v = 0
self.k = None
self.d = 0
self.p = 0
self.s = None
self.n = None
def __str__(self):
2024-12-11 15:46:38 +01:00
return "[{:02d}]{} i={} v={} a={} f={} t={} k={} d={} p={} s={} n={}".format(
self.c,
self.c.name,
self.i,
self.v,
self.a,
self.f,
self.t,
self.k,
self.d,
self.p,
self.s,
self.n,
)
2024-10-27 03:55:06 +01:00
def to_store(v):
return str(v)
def to_agent(v):
r = None
try:
r = AddressAgent(v)
except ValueError:
r = NameAgent(v)
return r
def p_cmd(p):
2024-12-11 15:46:38 +01:00
"""cmd : key_create
| voucher_mint
| voucher_transfer
| voucher_create
| pair
"""
2024-10-27 03:55:06 +01:00
p[0] = p[1]
def p_key_create(p):
2024-12-11 15:46:38 +01:00
"""key_create : change_label LABEL LABEL
| change_label LABEL LABEL HEX
"""
2024-10-27 03:55:06 +01:00
o = p[1]
o.f = NameAgent(p[2])
o.t = to_store(p[3])
if len(p) > 4:
o.k = p[4]
p[0] = o
def p_voucher_mint(p):
2024-12-11 15:46:38 +01:00
"""voucher_mint : change_label hv
| change_label nv
"""
2024-10-27 03:55:06 +01:00
o = p[1]
if o.c.value & 0x10 == 0:
raise ValueError("not a voucher command")
o.v = int(p[2][1])
o.a = to_agent(p[2][0])
2024-12-11 15:46:38 +01:00
p[0] = o
2024-10-27 03:55:06 +01:00
def p_voucher_create(p):
2024-12-11 15:46:38 +01:00
"""voucher_create : change_label LABEL LABEL VALUE
| change_label LABEL LABEL VALUE VALUE VALUE
"""
2024-10-27 03:55:06 +01:00
o = p[1]
if o.c.value & 0x10 == 0:
raise ValueError("not a voucher command")
o.s = p[2]
o.n = p[3]
o.v = int(p[4])
if len(p) > 5:
o.d = p[5]
o.p = p[6]
2024-12-11 15:46:38 +01:00
p[0] = o
2024-10-27 03:55:06 +01:00
def p_voucher_mint_recipient(p):
2024-12-11 15:46:38 +01:00
"""voucher_mint : change_label hv HEX
| change_label nv HEX
| change_label hv LABEL
| change_label nv LABEL
"""
2024-10-27 03:55:06 +01:00
o = p[1]
if o.c.value & 0x10 == 0:
raise ValueError("not a voucher command")
o.v = int(p[2][1])
o.a = to_agent(p[2][0])
o.t = to_agent(p[3])
2024-12-11 15:46:38 +01:00
p[0] = o
2024-10-27 03:55:06 +01:00
def p_voucher_transfer(p):
2024-12-11 15:46:38 +01:00
"""voucher_transfer : change_label hv HEX HEX
| change_label nv HEX HEX
| change_label hv LABEL HEX
| change_label nv LABEL HEX
| change_label hv LABEL LABEL
| change_label nv LABEL LABEL
| change_label hv HEX LABEL
| change_label nv HEX LABEL
"""
2024-10-27 03:55:06 +01:00
o = p[1]
if o.c.value & 0x10 == 0:
raise ValueError("not a voucher command")
o.v = int(p[2][1])
o.a = to_agent(p[2][0])
o.t = to_agent(p[3])
o.f = to_agent(p[4])
2024-12-11 15:46:38 +01:00
p[0] = o
2024-10-27 03:55:06 +01:00
def p_nv(p):
2024-12-11 15:46:38 +01:00
"nv : LABEL VALUE"
p[0] = (
p[1],
p[2],
)
2024-10-27 03:55:06 +01:00
def p_hv(p):
2024-12-11 15:46:38 +01:00
"hv : HEX VALUE"
p[0] = (
p[1],
p[2],
)
2024-10-27 03:55:06 +01:00
def p_change_label(p):
2024-12-11 15:46:38 +01:00
"""change_label : pair
| pairnoid
"""
2024-10-27 03:55:06 +01:00
p[0] = p[1]
def p_pair(p):
2024-12-11 15:46:38 +01:00
"pair : LABEL ID"
2024-10-27 03:55:06 +01:00
o = Cmd(p[1])
o.i = p[2]
p[0] = o
def p_pairnoid(p):
2024-12-11 15:46:38 +01:00
"pairnoid : LABEL NOID"
2024-10-27 03:55:06 +01:00
o = Cmd(p[1])
o.i = str(uuid.uuid4())
p[0] = o
2024-10-28 00:07:20 +01:00
class Router:
def __init__(self):
self.__routes = {}
self.__r = {}
def register(self, cmd_id, fn):
self.__routes[cmd_id] = fn
def sync(self, uid):
o = self.__r[uid]
if o[1] == None:
return None
r = o[1][0].recv()
2024-12-11 15:46:38 +01:00
o = (
o[0],
None,
)
2024-10-28 00:07:20 +01:00
o[0].join()
return r
def __wrap(self, uid, fn, cmd):
r = fn(cmd)
self.__r[uid][1][1].send(r)
def exec(self, cmd):
2024-10-28 01:45:07 +01:00
logg.debug("router exec {}".format(cmd))
2024-12-11 15:46:38 +01:00
if cmd.c & 0xA0 > 0:
2024-10-28 00:07:20 +01:00
return self.sync(cmd.i)
fn = self.__routes[cmd.c]
pi = Pipe(False)
2024-12-11 15:46:38 +01:00
po = Process(
target=self.__wrap,
args=(
cmd.i,
fn,
cmd,
),
)
self.__r[cmd.i] = (
po,
pi,
)
2024-10-28 00:07:20 +01:00
po.start()
po.join()
2024-10-28 00:07:20 +01:00
2024-10-28 01:45:07 +01:00
def finish(self):
print("syncing")
2024-10-28 00:07:20 +01:00
for k, v in self.__r.items():
2024-10-28 01:45:07 +01:00
print("syncing key " + k)
2024-10-28 00:07:20 +01:00
r = self.sync(k)
2024-10-28 01:45:07 +01:00
logg.debug("synced " + k + ": " + r)
print("synced " + k + ": " + r)
def __del__(self):
self.finish()
2024-10-28 00:07:20 +01:00
def noop_handler(cmd):
return str(cmd)
2024-11-26 08:05:11 +01:00
def printMessage(message):
2024-12-11 15:46:38 +01:00
box_width = len(message) + 4
print("+" + "-" * (box_width - 2) + "+")
print("| " + message + " |")
print("+" + "-" * (box_width - 2) + "+")
def find_custodial_address(key_name):
directory = "custodialstore"
filename = f"{key_name}.json"
file_path = os.path.join(directory, filename)
# Check if the file exists with the keyname
2024-12-11 15:46:38 +01:00
if os.path.isfile(file_path):
with open(file_path, "r") as f:
custodial_account = json.load(f)
return custodial_account["address"]
else:
return None
2024-11-26 08:05:11 +01:00
def remove_ansi_escape_codes(text):
2024-12-11 15:46:38 +01:00
return re.sub(r"\u001b\[.*?m", "", text)
2024-11-26 08:05:11 +01:00
2024-11-25 09:39:12 +01:00
def generate_private_key():
"""Generate a new private key."""
web3 = Web3()
2024-12-11 15:46:38 +01:00
account = web3.eth.account.create()
return account.address, w3.to_hex(account.key)
2024-11-25 09:39:12 +01:00
2024-12-11 15:46:38 +01:00
def store_key_in_keystore(keystore_dir, private_key, key_name, address):
2024-11-27 13:30:47 +01:00
# Create the directory if it doesn't exist
if not os.path.exists(keystore_dir):
2024-12-11 15:46:38 +01:00
os.makedirs(keystore_dir)
2024-11-27 13:30:47 +01:00
2024-11-25 09:39:12 +01:00
keystore = {
2024-12-11 15:46:38 +01:00
"key_name": key_name,
"private_key": private_key,
"address": address,
2024-11-25 09:39:12 +01:00
}
2024-11-27 13:30:47 +01:00
store_path = os.path.join(keystore_dir, f"{key_name}.json")
2024-12-11 15:46:38 +01:00
2024-11-25 09:39:12 +01:00
# Save to JSON file (simulated keystore)
2024-12-11 15:46:38 +01:00
with open(store_path, "w") as f:
2024-11-25 09:39:12 +01:00
json.dump(keystore, f)
2024-11-19 17:03:58 +01:00
2024-12-11 15:46:38 +01:00
return store_path
def load_gas(address,nonce):
command = (
2024-12-11 15:46:38 +01:00
f"cast send {address} "
f"--value {gas_topup} "
f"--nonce {nonce} "
2024-12-11 15:46:38 +01:00
f"--private-key {master_private_key} "
f"--rpc-url {rpc} "
f" --json "
)
result = subprocess.run(command, shell=True, capture_output=True, text=True)
if result.returncode != 0:
2024-12-11 15:46:38 +01:00
raise subprocess.CalledProcessError(
result.returncode, command, output=result.stdout, stderr=result.stderr
)
2024-12-10 13:55:12 +01:00
message = f"Added {gas_topup} to {address}"
printMessage(message)
2024-12-11 15:46:38 +01:00
def create_custodialaccount():
# create custodial account endpoint
url = "http://localhost:5003/api/v2/account/create"
headers = {
"Authorization": f"Bearer {bearer_token}",
"Content-Type": "application/json",
}
try:
response = requests.post(url, headers=headers)
# Check if the request was successful (status code 200)
if response.status_code == 200:
account = response.json()
public_key = account["result"]["publicKey"]
return public_key
else:
return None
except requests.exceptions.RequestException as e:
print("Error:", e)
return None
def do_custodial_token_transfer(transfer):
#token transfer custodial endpoint
2024-12-11 15:46:38 +01:00
url = "http://localhost:5003/api/v2/token/transfer"
headers = {
"Authorization": f"Bearer {bearer_token}",
"Content-Type": "application/json",
}
data = {
"from": transfer.from_address,
"to": transfer.to_address,
"amount": str(transfer.amount),
"tokenAddress": transfer.token_address,
}
json_data = json.dumps(data)
try:
response = requests.post(url=url, headers=headers, data=json_data)
# Check if the request was successful (status code 200)
if response.status_code == 200:
transfer = response.json()
return transfer["result"]["trackingId"]
else:
2024-12-15 10:37:33 +01:00
return None
2024-12-11 15:46:38 +01:00
except requests.exceptions.RequestException as e:
print("Error:", e)
return None
def store_voucher(voucher_detail):
vouchers = []
2024-11-26 08:05:11 +01:00
voucher = {
"voucher_address": voucher_detail.address,
"owner": voucher_detail.owner,
"symbol": voucher_detail.symbol,
"decimals": voucher_detail.decimals
2024-11-26 08:05:11 +01:00
}
vouchers.append(voucher)
try:
2024-12-11 15:46:38 +01:00
with open("vouchers.json", "r") as f:
existing_vouchers = json.load(f)
2024-12-11 15:46:38 +01:00
except FileNotFoundError:
existing_vouchers = []
for entry in existing_vouchers:
vouchers.append(entry)
2024-12-11 15:46:38 +01:00
with open("vouchers.json", "w") as f:
json.dump(vouchers, f)
2024-11-26 08:05:11 +01:00
2024-11-25 09:39:12 +01:00
def key_create_handler(cmd):
2024-12-11 15:46:38 +01:00
key_name = str(cmd.f).split(":")[1]
store_name = cmd.t
keystore_dir = "user_store"
master_address = w3.eth.account.from_key(master_private_key)
nonce = w3.eth.get_transaction_count(master_address.address,'pending')
2024-11-25 10:37:52 +01:00
if cmd.k is None:
2024-12-11 15:46:38 +01:00
address, private_key = generate_private_key()
2024-11-25 10:37:52 +01:00
else:
if cmd.k.startswith("0x"):
private_key = cmd.k[2:]
else:
private_key = cmd.k
2024-12-11 15:46:38 +01:00
address = w3.eth.account.from_key(private_key).address
2024-12-11 15:46:38 +01:00
if store_name == "custodialstore":
address = create_custodialaccount()
if address is None:
raise ValueError("account address cannot be None")
private_key = None
keystore_dir = "custodialstore"
2024-12-11 15:46:38 +01:00
load_gas(address,nonce)
2024-12-11 15:46:38 +01:00
store_key_in_keystore(keystore_dir, private_key, key_name, address)
2024-11-25 09:39:12 +01:00
return address
2024-11-19 17:03:58 +01:00
2024-12-11 15:46:38 +01:00
def voucher_create_handler(cmd):
2024-11-19 17:03:58 +01:00
symbol = cmd.s
2024-11-23 11:23:16 +01:00
if cmd.n is None or cmd.s is None or cmd.v is None:
raise ValueError("cmd.n, cmd.s, and cmd.v must not be None")
voucher_detail = VoucherDetail()
voucher_detail.name = cmd.n
voucher_detail.decimals = cmd.v
2024-12-11 15:46:38 +01:00
random_ascii = "".join(random.choices(string.ascii_letters, k=2)).upper()
try:
2024-12-11 15:46:38 +01:00
with open("vouchers.json", "r") as f:
existing_vouchers = json.load(f)
2024-12-11 15:46:38 +01:00
except FileNotFoundError:
existing_vouchers = []
2024-12-11 15:46:38 +01:00
voucher_symbols = list(map(lambda symbol: symbol["symbol"], existing_vouchers))
if symbol in voucher_symbols:
symbol = symbol + random_ascii
voucher_detail.symbol = symbol
if master_private_key.startswith("0x"):
private_key = master_private_key[2:]
else:
private_key = master_private_key
2024-11-27 13:30:47 +01:00
voucher_detail.owner = private_key
2024-12-11 15:46:38 +01:00
# Command to create a voucher
publish_token = (
f"ge-publish --private-key {voucher_detail.owner} --json "
2024-12-11 15:46:38 +01:00
f"--rpc {rpc} --gas-fee-cap {gas_cap} --chainid {chainId} "
f'p erc20 --name "{voucher_detail.name}" --symbol "{voucher_detail.symbol}"'
2024-12-11 15:46:38 +01:00
)
result = subprocess.run(publish_token, shell=True, capture_output=True, text=True)
2024-11-23 11:23:16 +01:00
if result.returncode != 0:
2024-12-11 15:46:38 +01:00
raise subprocess.CalledProcessError(
result.returncode, publish_token, output=result.stdout, stderr=result.stderr
)
2024-11-30 09:46:55 +01:00
output_lines = result.stderr.strip().split("\n")
deployment_result = output_lines[1]
2024-11-30 09:46:55 +01:00
try:
2024-12-11 15:46:38 +01:00
data = json.loads(deployment_result)
contract_address = data.get("contract_address", None)
voucher_detail.address = contract_address
2024-11-30 09:46:55 +01:00
except json.JSONDecodeError as e:
2024-12-11 15:46:38 +01:00
print("Error parsing JSON:", e)
store_voucher(voucher_detail)
2024-12-11 15:46:38 +01:00
# 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')
2024-12-11 15:46:38 +01:00
# Command to add the token to the token index
add_token_to_index = (
2024-12-11 15:46:38 +01:00
f"cast send --private-key {master_private_key} "
f"--nonce {nonce} "
2024-12-11 15:46:38 +01:00
f"--rpc-url {rpc} "
f"{token_index} "
f'"add(address)" {contract_address} '
f" --json "
)
result2 = subprocess.run(
add_token_to_index, shell=True, capture_output=True, text=True
)
if result2.returncode != 0:
2024-12-11 15:46:38 +01:00
raise subprocess.CalledProcessError(
result2.returncode,
add_token_to_index,
output=result2.stdout,
stderr=result2.stderr,
)
message = f"Voucher {voucher_detail.name} created with address {contract_address} and symbol {symbol} and added to token index: {token_index}"
printMessage(message)
2024-11-19 17:03:58 +01:00
return contract_address
2024-12-11 15:46:38 +01:00
2024-11-19 17:03:58 +01:00
def voucher_transfer_handler(cmd):
voucher_transfer = VoucherTransfer()
2024-12-11 15:46:38 +01:00
# Amount to transfer
value = cmd.v # Amount to transfer
is_custodial_address = False
voucher_transfer.amount = value
2024-12-11 15:46:38 +01:00
# Token symbol to transfer
if str(cmd.a).startswith("NameAgent"):
voucher_name = str(cmd.a).split(":")[1]
2024-11-26 08:05:11 +01:00
with open("vouchers.json", "r") as file:
2024-12-11 15:46:38 +01:00
data = json.load(file)
2024-12-11 15:46:38 +01:00
voucher_symbols = list(map(lambda symbol: symbol["symbol"], data))
voucher_address = list(map(lambda address: address["voucher_address"], data))
voucher_decimal = list(map(lambda decimal: decimal["decimals"], data))
if voucher_name in voucher_symbols:
2024-12-11 15:46:38 +01:00
index = voucher_symbols.index(voucher_name)
voucher_transfer.token_address = voucher_address[index]
voucher_transfer.decimals = voucher_decimal[index]
2024-12-11 15:46:38 +01:00
elif str(cmd.a).startswith("AddressAgent"):
voucher_transfer.token_address = "0x" + str(cmd.a).split(":")[1]
2024-12-11 15:46:38 +01:00
else:
raise ValueError(
f"Invalid command: {cmd.t}. Expected 'NameAgent' or 'AddressAgent'."
)
if str(cmd.t).startswith("NameAgent"):
key_name = str(cmd.t).split(":")[1]
custodial_address = find_custodial_address(key_name)
if custodial_address is not None:
to = custodial_address
voucher_transfer.to_address = to
2024-12-11 15:46:38 +01:00
else:
store_path = os.path.join("user_store", f"{key_name}.json")
2024-12-11 15:46:38 +01:00
with open(store_path, "r") as file:
data = json.load(file)
acct = w3.eth.account.from_key(data["private_key"])
to = acct.address
voucher_transfer.to_address = to
2024-12-11 15:46:38 +01:00
elif str(cmd.t).startswith("AddressAgent"):
2024-11-26 12:42:33 +01:00
to = "0x" + str(cmd.t).split(":")[1]
voucher_transfer.to_address = to
2024-12-11 15:46:38 +01:00
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]
custodial_address = find_custodial_address(key_name)
if custodial_address is not None:
is_custodial_address = True
voucher_transfer.from_address = custodial_address
2024-12-11 15:46:38 +01:00
else:
store_path = os.path.join("user_store", f"{key_name}.json")
2024-12-11 15:46:38 +01:00
with open(store_path, "r") as file:
data = json.load(file)
from_private_key = data["private_key"]
2024-12-10 13:55:12 +01:00
2024-12-11 15:46:38 +01:00
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'."
)
amount_transfered = value / pow(10, voucher_transfer.decimals)
2024-12-11 15:46:38 +01:00
if is_custodial_address:
tracking_id = do_custodial_token_transfer(voucher_transfer)
2024-12-11 15:46:38 +01:00
if tracking_id is not None:
message = f"Transfered {amount_transfered} {voucher_name} to {to} "
printMessage(message)
return tracking_id
else:
command = (
f"cast send --private-key {from_private_key} "
f"--rpc-url {rpc} "
f"{voucher_transfer.token_address} "
2024-12-11 15:46:38 +01:00
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
)
if result.stderr:
raise ValueError(f"Command failed with error: {result.stderr}")
data = json.loads(result.stdout)
message = f"Transfered {amount_transfered} {voucher_name} to {to} "
printMessage(message)
return data["transactionHash"]
2024-11-19 17:03:58 +01:00
def voucher_mint_handler(cmd):
value = cmd.v
2024-12-15 10:37:33 +01:00
master_address = w3.eth.account.from_key(master_private_key)
nonce = w3.eth.get_transaction_count(master_address.address,'pending')
2024-11-26 08:05:11 +01:00
if str(cmd.t).startswith("NameAgent"):
2024-12-11 15:46:38 +01:00
key_name = str(cmd.t).split(":")[1]
custodial_address = find_custodial_address(key_name)
if custodial_address is not None:
to = custodial_address
else:
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
2024-11-26 08:05:11 +01:00
elif str(cmd.t).startswith("AddressAgent"):
2024-12-11 15:46:38 +01:00
to = "0x" + str(cmd.t).split(":")[1]
2024-11-26 12:42:33 +01:00
else:
2024-12-11 15:46:38 +01:00
raise ValueError(
f"Invalid command: {cmd.t}. Expected 'NameAgent' or 'AddressAgent'."
)
2024-11-26 08:05:11 +01:00
if str(cmd.a).startswith("NameAgent"):
2024-12-11 15:46:38 +01:00
voucher_symbol = str(cmd.a).split(":")[1]
2024-11-26 08:05:11 +01:00
with open("vouchers.json", "r") as file:
2024-12-11 15:46:38 +01:00
data = json.load(file)
2024-12-11 15:46:38 +01:00
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:
2024-12-11 15:46:38 +01:00
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")
2024-11-26 08:05:11 +01:00
elif str(cmd.a).startswith("AddressAgent"):
2024-12-11 15:46:38 +01:00
s = "0x" + str(cmd.a).split(":")[1]
2024-11-26 12:42:33 +01:00
else:
2024-12-11 15:46:38 +01:00
raise ValueError(
f"Invalid command: {cmd.t}. Expected 'NameAgent' or 'AddressAgent'."
)
2024-11-19 17:03:58 +01:00
command = (
2024-12-11 15:46:38 +01:00
f"cast send --private-key {privatekey} "
f"--rpc-url {rpc} "
2024-12-15 10:37:33 +01:00
f"--nonce {nonce} "
2024-12-11 15:46:38 +01:00
f" {s} "
2024-11-23 11:23:16 +01:00
f'"mintTo(address,uint256)" {to} {value}'
2024-12-11 15:46:38 +01:00
f" --json "
2024-11-19 17:03:58 +01:00
)
result = subprocess.run(command, shell=True, capture_output=True, text=True)
if result.returncode != 0:
2024-12-11 15:46:38 +01:00
raise subprocess.CalledProcessError(
result.returncode, command, output=result.stdout, stderr=result.stderr
)
2024-11-19 17:03:58 +01:00
if result.stderr:
raise ValueError(f"Command failed with error: {result.stderr}")
2024-11-29 12:31:47 +01:00
data = json.loads(result.stdout)
2024-12-10 13:55:12 +01:00
mint_amount = value / 10**6
message = f"Minted {mint_amount} {voucher_symbol} to :{to}"
printMessage(message)
2024-11-29 12:31:47 +01:00
return data["transactionHash"]
2024-11-19 17:03:58 +01:00
2024-10-27 03:55:06 +01:00
parser = yacc.yacc()
2024-10-28 01:45:07 +01:00
running = True
class FileGet:
def __init__(self, o, fp):
self.__running = True
self.__o = o
2024-12-11 15:46:38 +01:00
self.__f = open(fp, "r")
2024-10-28 01:45:07 +01:00
def run(self):
while self.__running:
v = ifc.get()
if v == None:
break
2024-12-11 15:46:38 +01:00
v = v.strip("\n")
2024-10-28 01:45:07 +01:00
if len(v) == 0:
break
r = parser.parse(v)
self.__o.exec(r)
def get(self):
return self.__f.readline()
2024-12-11 15:46:38 +01:00
2024-10-28 01:45:07 +01:00
class WaitGet:
def __init__(self, o, *r):
self.__running = True
self.__o = o
self.__f = r
# TODO: router copy results in missing keys to sync when closing down
def __process(self, f):
while self.__running:
r = select.select([f], [], [])
v = r[0][0].recv()
if v == None:
break
2024-12-11 15:46:38 +01:00
v = v.strip("\n")
2024-10-28 01:45:07 +01:00
if len(v) == 0:
break
r = parser.parse(v)
self.__o.exec(r)
def run(self):
2024-12-11 15:46:38 +01:00
(
fo,
fi,
) = Pipe()
2024-10-28 01:45:07 +01:00
p = Process(target=self.__process, args=(fo,))
p.start()
while self.__running:
v = input("> ")
if v == "":
fi.send(None)
break
fi.send(v)
p.join()
2024-12-11 15:46:38 +01:00
logg.debug("waitget run end")
2024-10-28 01:45:07 +01:00
2024-12-11 15:46:38 +01:00
if __name__ == "__main__":
2024-10-28 01:45:07 +01:00
ifc = None
2024-10-28 00:07:20 +01:00
o = Router()
2024-12-11 15:46:38 +01:00
o.register(CmdId.KEY_CREATE, key_create_handler)
o.register(CmdId.VOUCHER_CREATE, voucher_create_handler)
o.register(CmdId.VOUCHER_MINT, voucher_mint_handler)
2024-12-11 15:46:38 +01:00
o.register(CmdId.VOUCHER_TRANSFER, voucher_transfer_handler)
2024-10-28 01:45:07 +01:00
if len(sys.argv) > 1:
ifc = FileGet(o, sys.argv[1])
else:
ifc = WaitGet(o, sys.stdin)
ifc.run()
o.finish()