def _call_check_price(self, service_metadata): pricing = service_metadata["pricing"] if (pricing["price_model"] == "fixed_price" and pricing["price_in_cogs"] != self.args.price): raise Exception( "Service price is %s, but you set price %s" % (cogs2stragi( pricing["price_in_cogs"]), cogs2stragi(self.args.price)))
def print_unclaimed(self): grpc_channel = open_grpc_channel(self.args.endpoint) payments = self._call_GetListUnclaimed(grpc_channel) self._printout("# channel_id channel_nonce signed_amount (AGI)") total = 0 for p in payments: self._printout("%i %i %s"%(p["channel_id"], p["nonce"], cogs2stragi(p["amount"]))) total += p["amount"] self._printout("# total_unclaimed_in_AGI = %s"%cogs2stragi(total))
def print_unclaimed(self): grpc_channel = open_grpc_channel(self.args.endpoint) payments = self._call_GetListUnclaimed(grpc_channel) self._printout("# channel_id channel_nonce signed_amount (AGI)") total = 0 for p in payments: self._printout("%i %i %s" % ( p["channel_id"], p["nonce"], cogs2stragi(p["amount"]))) total += p["amount"] self._printout("# total_unclaimed_in_AGI = %s" % cogs2stragi(total))
def print_agi_and_mpe_balances(self): """ Print balance of ETH, AGI, and MPE wallet """ if (self.args.account): account = self.args.account else: account = self.ident.address eth_wei = self.w3.eth.getBalance(account) agi_cogs = self.call_contract_command("SingularityNetToken", "balanceOf", [account]) mpe_cogs = self.call_contract_command("MultiPartyEscrow", "balances", [account]) # we cannot use _pprint here because it doesn't conserve order yet self._printout(" account: %s"%account) self._printout(" ETH: %s"%self.w3.fromWei(eth_wei, 'ether')) self._printout(" AGI: %s"%cogs2stragi(agi_cogs)) self._printout(" MPE: %s"%cogs2stragi(mpe_cogs))
def call_server_statelessly_with_params(self, params, group_name): # if service is not initilized we will initialize it (unless we want skip registry check for update) if (not self.args.skip_update_check): self._init_or_update_registered_org_if_needed() self._init_or_update_registered_service_if_needed() org_metadata = self._read_metadata_for_org(self.args.org_id) service_metadata = self._get_service_metadata() endpoint = self._get_endpoint_from_metadata_or_args(service_metadata) grpc_channel = open_grpc_channel(endpoint) # if channel was not initilized we will try to initailize it (it will work only in simple case of signer == sender) channel = self._smart_get_initialized_channel_for_org( org_metadata, filter_by="signer") channel_id = channel["channelId"] price = self._get_price_from_metadata(service_metadata, group_name) server_state = self._get_channel_state_from_server( grpc_channel, channel_id) proceed = self.args.yes or input( "Price for this call will be %s AGI (use -y to remove this warning). Proceed? (y/n): " % (cogs2stragi(price))) == "y" if (not proceed): self._error("Cancelled") return self._call_server_via_grpc_channel( grpc_channel, channel_id, server_state["current_nonce"], server_state["current_signed_amount"] + price, params, service_metadata)
def _print_channels_dict_from_blockchain(self, channels_dict): # print only caption if (self.args.only_id): self._printout("#organization_id service_id channelId") else: self._printout( "#organization_id service_id group_name channel_id nonce value(AGI) expiration(blocks)" ) for org_id, service_id in channels_dict: channels = self._filter_channels_sender_or_signer( channels_dict[org_id, service_id]) metadata = self._read_metadata_for_service(org_id, service_id) for channel in channels: channel_id = channel["channelId"] group = metadata.get_group_by_group_id(channel["groupId"]) if (group is None): group_name = "UNDIFINED" else: group_name = group["group_name"] if (self.args.only_id): self._printout( "%s %s %s %i" % (org_id, service_id, group_name, channel_id)) else: channel_blockchain = self._get_channel_state_from_blockchain( channel_id) value_agi = cogs2stragi(channel_blockchain["value"]) self._printout("%s %s %s %i %i %s %i" % (org_id, service_id, group_name, channel_id, channel_blockchain["nonce"], value_agi, channel_blockchain["expiration"]))
def _open_channel_for_service(self, metadata): mpe_cogs = self.call_contract_command("MultiPartyEscrow", "balances", [self.ident.address]) if (mpe_cogs < self.args.amount): raise Exception("insufficient funds. You MPE balance is %s AGI " % cogs2stragi(mpe_cogs)) group_id = metadata.get_group_id(self.args.group_name) recipient = metadata.get_payment_address(self.args.group_name) if (self.args.signer): signer = self.args.signer else: signer = self.ident.address channel_info = { "sender": self.ident.address, "signer": signer, "recipient": recipient, "groupId": group_id } expiration = self._get_expiration_from_args() params = [ channel_info["signer"], channel_info["recipient"], channel_info["groupId"], self.args.amount, expiration ] rez = self.transact_contract_command("MultiPartyEscrow", "openChannel", params) if (len(rez[1]) != 1 or rez[1][0]["event"] != "ChannelOpen"): raise Exception( "We've expected only one ChannelOpen event after openChannel. Make sure that you use correct MultiPartyEscrow address" ) return rez[1][0]["args"]["channelId"], channel_info
def _print_channels_from_blockchain(self, channels_ids): self._printout( "#channelId nonce recipient groupId(base64) value(AGI) expiration(blocks)" ) for i in sorted(channels_ids): channel = self._get_channel_state_from_blockchain(i) value_agi = cogs2stragi(channel["value"]) group_id_base64 = base64.b64encode( channel["groupId"]).decode("ascii") self._printout("%i %i %s %s %s %i" % (i, channel["nonce"], channel["recipient"], group_id_base64, value_agi, channel["expiration"]))
def _print_channels_from_blockchain(self, channels_ids): channels_ids = sorted(channels_ids) if (self.args.only_id): self._printout("#channelId") [self._printout(str(i)) for i in channels_ids] return self._printout("#channelId nonce recipient groupId(base64) value(AGI) expiration(blocks)") for i in channels_ids: channel = self._get_channel_state_from_blockchain(i) value_agi = cogs2stragi(channel["value"]) group_id_base64 = base64.b64encode(channel["groupId"]).decode("ascii") self._printout("%i %i %s %s %s %i"%(i, channel["nonce"], channel["recipient"], group_id_base64, value_agi, channel["expiration"]))
def _open_channel_for_service(self, metadata): mpe_cogs = self.call_contract_command("MultiPartyEscrow", "balances", [self.ident.address]) if (mpe_cogs < self.args.amount): raise Exception("insufficient funds. You MPE balance is %s AGI "%cogs2stragi(mpe_cogs)) group_id = metadata.get_group_id(self.args.group_name) recipient = metadata.get_payment_address(self.args.group_name) signer = self.get_address_from_arg_or_ident(self.args.signer) channel_info = {"sender": self.ident.address, "signer": signer, "recipient": recipient, "groupId" : group_id} expiration = self._get_expiration_from_args() params = [channel_info["signer"], channel_info["recipient"], channel_info["groupId"], self.args.amount, expiration] rez = self.transact_contract_command("MultiPartyEscrow", "openChannel", params) if (len(rez[1]) != 1 or rez[1][0]["event"] != "ChannelOpen"): raise Exception("We've expected only one ChannelOpen event after openChannel. Make sure that you use correct MultiPartyEscrow address") channel_info["channelId"] = rez[1][0]["args"]["channelId"] return channel_info
def _print_channels_dict_from_blockchain(self, channels_dict): # print only caption if (self.args.only_id): self._printout("#organization_id service_id channelId") else: self._printout("#organization_id service_id group_name channel_id nonce value(AGI) expiration(blocks)") for org_id, service_id in channels_dict: channels = self._filter_channels_sender_or_signer(channels_dict[org_id, service_id]) metadata = self._read_metadata_for_service(org_id, service_id) for channel in channels: channel_id = channel["channelId"] group = metadata.get_group_by_group_id(channel["groupId"]) if (group is None): group_name = "UNDIFINED" else: group_name = group["group_name"] if (self.args.only_id): self._printout("%s %s %s %i"%(org_id, service_id, group_name, channel_id)) else: channel_blockchain = self._get_channel_state_from_blockchain(channel_id) value_agi = cogs2stragi(channel_blockchain["value"]) self._printout("%s %s %s %i %i %s %i"%(org_id, service_id, group_name, channel_id, channel_blockchain["nonce"], value_agi, channel_blockchain["expiration"]))
def call_server_statelessly_with_params(self, params): # if service is not initilized we will initialize it (unless we want skip registry check for update) if (not self.args.skip_update_check): self._init_or_update_registered_service_if_needed() service_metadata = self._read_metadata_for_service(self.args.org_id, self.args.service_id) endpoint = self._get_endpoint_from_metadata_or_args(service_metadata) grpc_channel = open_grpc_channel(endpoint) # if channel was not initilized we will try to initailize it (it will work only in simple case of signer == sender) channel = self._smart_get_initialized_channel_for_service(service_metadata, filter_by = "signer") channel_id = channel["channelId"] price = self._get_price_from_metadata(service_metadata) server_state = self._get_channel_state_from_server(grpc_channel, channel_id) proceed = self.args.yes or input("Price for this call will be %s AGI (use -y to remove this warning). Proceed? (y/n): "%(cogs2stragi(price))) == "y" if (not proceed): self._error("Cancelled") return self._call_server_via_grpc_channel(grpc_channel, channel_id, server_state["current_nonce"], server_state["current_signed_amount"] + price, params, service_metadata)