def compile(self): ezo = self.app.ezo log = self.app.log for filename in self.app.pargs.extra_args: log.info(cyan("compiling contracts in {}".format(filename))) filename = get_contract_path(self.app.config["ezo"], filename) contracts_source, err = Contract.load(filename) if err: log.error(red("error loading contracts file: {}".format(err))) return err contracts, err = Contract.compile(contracts_source, ezo) if err: log.error( red("error compiling contracts source: {}".format(err))) return err # persist the compiled contract for contract in contracts: contract.source = contracts_source iid, err = contract.save(overwrite=self.app.pargs.overwrite) if err: log.error( red("error while persisting Contract to datastore: {}". format(err))) return err else: log.info(cyan("contract saved: {}".format(iid))) print("pytest>>CONTRACT_SAVED") return
def handlers(self): log = self.app.log ezo = self.app.ezo args = self.app.pargs # set the Source template directory from config Source.templates_dir = ezo.config["templates-dir"] # go through each contract hash and create handlers for h in args.extra_args: log.info( cyan("generating any missing event handlers for contract: {}". format(h))) c, err = Contract.get(h, ezo) if err: log.error(red("error getting contract: {}".format(err))) continue if not c: log.error( red("no contract was found for {} - was it compiled?". format(h))) continue _, err = c.generate_event_handlers(overwrite=args.overwrite) if err: log.error( red("error generating handlers for {}: {}".format(h, err))) continue log.info(cyan("handlers for contract {} generated".format(h))) return
def call(self): params = self.app.pargs.c_args ezo = self.app.ezo args = self.app.pargs log = self.app.log if not args.target: log.error("target must be set with the -t option before deploying") return # ezo.target = args.target _, err = ezo.dial(args.target) if err: log.error(red("error with node: {}".format(err))) return if len(params) != 3: self.app.log.error( red("missing parameters for send tx - 3 required")) return name = params[0] method = params[1] data = params[2] resp, err = Contract.call(ezo, name, method, data, args.target) if err: self.app.log.error(red("call error: {}".format(err))) return self.app.log.info(blue("call response: {}".format(yellow(resp)))) return
def start(self): log = self.app.log ezo = self.app.ezo args = self.app.pargs # inject password into the environment if args.password: os.environ["EZO_PASSWORD"] = args.password log.debug("starting ezo") if not args.target: log.error("target must be set with the -t option before deploying") return # ezo.target = args.target _, err = ezo.dial(args.target) if err: log.error(red("error with node: {}".format(err))) return err if not args.extra_args: log.error(red("error: missing contract name")) return res, err = ezo.start(args.extra_args, target=args.target) if err: log.error(red("error: {}".format(err))) else: log.info(cyan("result: {}".format(res)))
def deploys(self): ezo = self.app.ezo log = self.app.log res, err = get_deploys(self.app.pargs.term, ezo) if err: log.error(red("error viewing deployments")) log.error(red(err)) return err v = view_deploys(res) print() print(bright(blue("+-------+"))) for vs in v: print(yellow(vs)) print(blue("+--------------+")) print(yellow("ezo deployments: {}".format(bright(len(res))))) print(reset("")) return len(res)
def start(self, contract_names, target): ''' loads the contracts -- starts their event listeners :param contract_names: :return: ''' if isinstance(contract_names, str): contract_names = [contract_names] if not isinstance(contract_names, list): return None, "error: expecting a string, or a list of contract names" contract_listeners = [] for name in contract_names: c, err = Contract.get(name, self) if err: EZO.log.error(red("error loading contract {}".format(name))) EZO.log.error(red(err)) continue if not c: EZO.log.warn(blue("contract {} not found".format(name))) continue address, err = Contract.get_address(name, c.hash, self.db, target=target) if err: EZO.log.error(red("error obtaining address for contract {}").format(name)) EZO.log.error(red(err)) continue if not address: EZO.log.error(red("no address for contract {}".format(name))) continue contract_listeners.append(c.listen(address, target)) if contract_listeners: loop = asyncio.get_event_loop() loop.run_until_complete( asyncio.gather(*contract_listeners) ) else: return None, "unable to start contract listeners"
def generate_event_handlers(self, overwrite=False): # get the contract name, events from the abi contract_name = inflection.underscore(self.name.replace('<stdin>:', '')) errors = list() events = [x for x in self.abi if x["type"] == "event"] for event in events: # get the topic sha3 topic = Web3.sha3(text=get_topic_sha3(event)) # build full path to new event handler hp = get_handler_path(self._ezo.config, contract_name) if not os.path.isdir(hp): os.mkdir(hp) event_name = inflection.underscore(event['name']) eh = "{}/{}_{}".format(hp, event_name, "handler.py") # check to see if it exists # if not, or if overwrite option is on if not os.path.exists(eh) or overwrite: # create event handler scaffold in python code, err = gen_event_handler_code(event_name) if err: print(red("gen error: {}".format(err))) try: with open(eh, "w+") as f: f.write(code) except Exception as e: print(red("gen error: {}".format(e))) errors.append(e) continue # map the topic to the handler self.te_map[topic] = eh _, err = self.save(overwrite=True) if err: return None, err return None, errors
def deploy(self): ezo = self.app.ezo log = self.app.log args = self.app.pargs # inject password into the environment if args.password: os.environ["EZO_PASSWORD"] = args.password log.debug("deploying...") if not args.target: log.error( red("target must be set with the -t option before deploying")) return _, err = ezo.dial(args.target) if err: log.error(red("unable to dial node")) log.error(red("error: {}".format(err))) return for h in args.extra_args: log.info(cyan("deploying contract {} to {}".format(h, args.target))) # get the compiled contract by it's Contract Name c, err = Contract.get(h, ezo) if err: log.error( red("error loading contract from storage: {}".format(err))) return if not c: log.error( red("contract '{}' not found -- has it been compiled?"). format((h))) return # deploy the contract addr, err = c.deploy(target=args.target, overwrite=self.app.pargs.overwrite) if err: log.error( red("error deploying contract {} to {}".format( c.hash, args.target))) log.error(red("message: {}".format(err))) return log.info( cyan( "successfully deployed contract {} named {} to stage '{}' at address {}" .format(c.hash, c.name, args.target, addr))) return
def tx(self): params = self.app.pargs.c_args ezo = self.app.ezo args = self.app.pargs log = self.app.log # inject password into the environment if args.password: os.environ["EZO_PASSWORD"] = args.password if not args.target: log.error("target must be set with the -t option before deploying") return if len(params) != 3: self.app.log.error( red("missing parameters for send tx - 3 required")) return _, err = ezo.dial(args.target) if err: log.error(red("error with node: {}".format(err))) return err name = params[0] method = params[1] data = params[2] resp, err = Contract.send(ezo, name, method, data, target=args.target) if err: self.app.log.error(red("tx error: {}".format(err))) return err self.app.log.info(bright(blue("=============="))) for k, v in resp.items(): self.app.log.info("{}: {}".format(yellow(k), cyan(v))) self.app.log.info(blue("==============")) self.app.log.info(reset(""))
def create_ethereum_account(): ac = Account.create() wf = xp.locate_wordfile() words = xp.generate_wordlist(wordfile=wf, min_length=5, max_length=8) password_str = xp.generate_xkcdpassword(words) print( cyan( "password string to decrypt private key -- please store in safe location:" )) print() print(yellow(password_str)) print() print(cyan("address:")) print(yellow(ac.address)) ks = json.dumps(Account.encrypt(ac.privateKey, password_str), indent=2) print(red(ks))