def view_deploys(results): l = list() for res in results: for key, value in res.items(): key = key.replace(EZO.DEPLOYED + ":", "").split(':') l.append( "contract: {:35s} target: {:20s} hash: {:27s} address: {:35s} timestamp: {:25s}" .format(cyan(key[0]), magenta(key[1]), blue(key[2]), blue(value["address"]), cyan(value["timestamp"]))) return l
def event_output(contract, event_name, data): ts = datetime.utcnow() EZO.log.info( ("event: {:25s} contract: {:35s} address {:40s} timestamp: {:25s}" ).format(yellow(event_name), magenta(contract.name.replace("<stdin>:", "")), blue(data.address), magenta(ts)))
async def listen(self, address, target): ''' starts event listener for the contract :return: ''' if not address: return None, "listening address not provided" EZO.log.info(bright("hello ezo::listening to address: {}".format(blue(address)))) interval = self._ezo.config["poll-interval"] event_filter = self._ezo.w3.eth.filter({"address": address, "toBlock": "latest"}) loop = asyncio.new_event_loop() try: while True: for event in event_filter.get_new_entries(): if EZO.log: EZO.log.debug(bright("event received: {}".format(event))) ContractEvent.handler(event, self, target) await asyncio.sleep(interval) except Exception as e: return None, e finally: loop.close()
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 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 view_contracts(results): l = list() for res in results: for key, value in res.items(): key = key.replace(EZO.CONTRACT + ":", "") l.append( bright( "contract: {:35s} hash: {:25s} timestamp: {:25s}".format( cyan(key), format(blue(value["hash"])), cyan(value["timestamp"])))) return l
def project(self): ezo = self.app.ezo # set the Source template directory from config Source.templates_dir = ezo.config["templates-dir"] res, err = EZO.create_project(self.app.pargs.term) if err: print(err) return err print( bright( blue("new ezo project '{}' created".format( self.app.pargs.term)))) print(reset("")) return
def handler(rce, contract, target): ce = ContractEvent(rce, target) # find the mappped method for the topic if ce.event_topic in contract.te_map: handler_path = contract.te_map[ce.event_topic] s = importlib.util.spec_from_file_location("handlers", handler_path) handler_module = importlib.util.module_from_spec(s) s.loader.exec_module(handler_module) handler_module.handler(ce, contract) else: EZO.log.warn(blue("topic {} not in map".format(ce.event_topic)))
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 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"