diff --git a/parse.py b/parse.py index 63d7e0a..720b4a1 100644 --- a/parse.py +++ b/parse.py @@ -68,15 +68,16 @@ lexer = lex.lex() load_dotenv() # Chain Params -chainId = os.getenv("CHAIN_ID") -rpc = os.getenv("RPC") -bearer_token = os.getenv("BEARER_TOKEN") -gas_cap = os.getenv("GAS_FEE_CAP") +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") -gas_topup = os.getenv("GAS_TOPUP") +token_index = os.getenv("TOKEN_INDEX","0xD774bc082003eaF8DF74eEcD43AD44F03D488418") +gas_topup = os.getenv("GAS_TOPUP","0.01ether") + +bearer_token = os.getenv("BEARER_TOKEN") w3 = Web3(Web3.HTTPProvider(rpc)) @@ -100,15 +101,26 @@ w3 = Web3(Web3.HTTPProvider(rpc)) # print(tok) -class TokenTransfer: +class VoucherTransfer: def __init__( - self, to_address=None, from_address=None, amount=None, token_address=None + self, to_address=None, from_address=None, amount=None, token_address=None,decimals = None ): self.to_address = to_address self.from_address = from_address + self.decimals = decimals 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 + class CmdId(enum.IntEnum): KEY_CREATE = 0x1 @@ -495,12 +507,13 @@ def do_custodial_token_transfer(transfer): return None -def store_voucher(voucher_creator, voucher_symbol, voucher_address): +def store_voucher(voucher_detail): vouchers = [] voucher = { - "voucher_address": voucher_address, - "owner": voucher_creator, - "symbol": voucher_symbol, + "voucher_address": voucher_detail.address, + "owner": voucher_detail.owner, + "symbol": voucher_detail.symbol, + "decimals": voucher_detail.decimals } vouchers.append(voucher) try: @@ -548,9 +561,16 @@ def key_create_handler(cmd): def voucher_create_handler(cmd): - name = cmd.n symbol = cmd.s + 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 + random_ascii = "".join(random.choices(string.ascii_letters, k=2)).upper() try: with open("vouchers.json", "r") as f: @@ -563,16 +583,20 @@ def voucher_create_handler(cmd): 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 + voucher_detail.owner = private_key + # Command to create a voucher publish_token = ( - f"ge-publish --private-key {private_key} --json " + f"ge-publish --private-key {voucher_detail.owner} --json " f"--rpc {rpc} --gas-fee-cap {gas_cap} --chainid {chainId} " - f'p erc20 --name "{name}" --symbol "{symbol}"' + f'p erc20 --name "{voucher_detail.name}" --symbol "{voucher_detail.symbol}"' ) result = subprocess.run(publish_token, shell=True, capture_output=True, text=True) @@ -587,10 +611,11 @@ def voucher_create_handler(cmd): try: data = json.loads(deployment_result) contract_address = data.get("contract_address", None) + voucher_detail.address = contract_address except json.JSONDecodeError as e: print("Error parsing JSON:", e) - store_voucher(private_key, symbol, contract_address) + store_voucher(voucher_detail) # sleep for 5 second to allow chain to sync time.sleep(5) @@ -619,20 +644,20 @@ def voucher_create_handler(cmd): stderr=result2.stderr, ) - message = f"Voucher {name} created with address {contract_address} and symbol {symbol} and added to token index: {token_index}" + message = f"Voucher {voucher_detail.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): - token_transfer = TokenTransfer() + voucher_transfer = VoucherTransfer() # Amount to transfer value = cmd.v # Amount to transfer is_custodial_address = False - token_transfer.amount = value + voucher_transfer.amount = value # Token symbol to transfer if str(cmd.a).startswith("NameAgent"): @@ -642,12 +667,15 @@ def voucher_transfer_handler(cmd): 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: index = voucher_symbols.index(voucher_name) - token_transfer.token_address = voucher_address[index] + voucher_transfer.token_address = voucher_address[index] + voucher_transfer.decimals = voucher_decimal[index] elif str(cmd.a).startswith("AddressAgent"): - token_transfer.token_address = "0x" + str(cmd.a).split(":")[1] + voucher_transfer.token_address = "0x" + str(cmd.a).split(":")[1] else: raise ValueError( f"Invalid command: {cmd.t}. Expected 'NameAgent' or 'AddressAgent'." @@ -659,17 +687,17 @@ def voucher_transfer_handler(cmd): custodial_address = find_custodial_address(key_name) if custodial_address is not None: to = custodial_address - token_transfer.to_address = to + voucher_transfer.to_address = to 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 - token_transfer.to_address = to + voucher_transfer.to_address = to elif str(cmd.t).startswith("AddressAgent"): to = "0x" + str(cmd.t).split(":")[1] - token_transfer.to_address = to + voucher_transfer.to_address = to else: raise ValueError( f"Invalid command: {cmd.t}. Expected 'NameAgent' or 'AddressAgent'." @@ -680,7 +708,7 @@ def voucher_transfer_handler(cmd): custodial_address = find_custodial_address(key_name) if custodial_address is not None: is_custodial_address = True - token_transfer.from_address = custodial_address + voucher_transfer.from_address = custodial_address else: store_path = os.path.join("user_store", f"{key_name}.json") with open(store_path, "r") as file: @@ -694,10 +722,10 @@ def voucher_transfer_handler(cmd): f"Invalid command: {cmd.t}. Expected 'NameAgent' or 'AddressAgent'." ) - amount_transfered = value / 10**6 + amount_transfered = value / pow(10, voucher_transfer.decimals) if is_custodial_address: - tracking_id = do_custodial_token_transfer(token_transfer) + tracking_id = do_custodial_token_transfer(voucher_transfer) if tracking_id is not None: message = f"Transfered {amount_transfered} {voucher_name} to {to} " printMessage(message) @@ -706,7 +734,7 @@ def voucher_transfer_handler(cmd): command = ( f"cast send --private-key {from_private_key} " f"--rpc-url {rpc} " - f"{token_transfer.token_address} " + f"{voucher_transfer.token_address} " f'"transfer(address,uint256)" {to} {value}' f" --json " )