def sync(handle=None): """ Sync payments and recover funds from closed connections. This WILL cost a fee per channnel synced as defined in the hub terms. * Synchronize open connections to send/receive payments. * Recover funds of closed connections. Args: handle (str, default=None): Optionally limit to given handle. Returns: { "connection handle": { "rawtxs": ["of transactions publish while recovering funds"], "received_payments": [ { "payer_handle": "sender handle", "amount": 1337, "token": "provided by sender" } ] } } """ result = {} hub_api = _hub_api() data = _load_data() for _handle, connection_data in copy.deepcopy(data)["connections"].items(): if handle is not None and _handle != handle: continue client = Mph.deserialize(hub_api, connection_data) if client.can_cull(): continue # dont sync closed inactive status = client.get_status() # sync open connections if status["status"] == "open": result[_handle] = { "rawtxs": [], "received_payments": client.sync() } # update closed connections elif status["status"] == "closed": result[_handle] = { "rawtxs": client.update(), "received_payments": [] } data["connections"][client.handle] = client.serialize() _save_data(data) return result
def test_serialization(): auth_wif = util.gen_funded_wif("XCP", 1000000, 1000000) hub_api = util.MockAPI(auth_wif=auth_wif) client_alpha = Mph(hub_api) serialized_alpha = client_alpha.serialize() client_beta = Mph.deserialize(api=hub_api, data=serialized_alpha) serialized_beta = client_beta.serialize() assert serialized_alpha == serialized_beta
def status(handle=None, verbose=False): """ Get status of connections and wallet. Args: handle (str, default=None): Optionally limit to given handle. verbose (bool, default=False): Optionally show additional information. Returns: { "connections": { "a0b206d1f68edb1aa24084752b5693a9022349dc547fb9952aa510003e93": { "asset": "XCP", "balance": 31337, "status": "open", "ttl": 404 } }, "wallet": { "address": "n2WQGAvnDS1vf7uXToLou6kLxJXRGFHo2b", "balances": { "BTC": 926109330, "XCP": 140982404156 } } } """ data = _load_data() hub_api = _hub_api() result = { "connections": {}, "wallet": { "address": keys.address_from_wif(load_wif()), "balances": balances() } } for _handle, connection_data in data["connections"].items(): if handle is not None and _handle != handle: continue client = Mph.deserialize(hub_api, connection_data) status = client.get_status() if verbose: status["data"] = connection_data result["connections"][_handle] = status else: result["connections"][_handle] = { "asset": status["asset"], "balance": status["balance"], "ttl": status["ttl"], "status": status["status"], "payments_queued": connection_data["payments_queued"] } return result
def close(handle): """ Close open connection and settle to blockchain. Args: handle (str): Handle of connection to close. Returns: Commit txid or None if no assets received from hub. """ hub_api = _hub_api() data = _load_data() client = Mph.deserialize(hub_api, data["connections"][handle]) commit_txid = client.close() # FIXME recover now if possible data["connections"][handle] = client.serialize() _save_data(data) return commit_txid
def queuepayment(source, destination, quantity, token=None): """ Queue micropayment channel send (sent on sync). Args: source (str): Handle of connection to send funds from. destination (str): Handle of connection to receive funds. quantity (int): Quantity of channel asset to transfer. token (str, default=None): Optional token payee will receive with the payment. Returns: Provided token or generated token if None given. """ hub_api = _hub_api() data = _load_data() client = Mph.deserialize(hub_api, data["connections"][source]) # FIXME check dest can receive payment result = client.micro_send(destination, quantity, token=token) data["connections"][source] = client.serialize() _save_data(data) return result
def cull(handle=None): """ Removes closed channels if all funds have been recovered. Args: handle (str): Optional handle of specific connection to be cull. Returns: List of with handles of culled connections. """ data = _load_data() hub_api = _hub_api() culled = [] for _handle, connection_data in copy.deepcopy(data)["connections"].items(): if handle is not None and _handle != handle: continue client = Mph.deserialize(hub_api, connection_data) if client.can_cull(): culled.append(_handle) del data["connections"][_handle] _save_data(data) return culled