def post_challange(guid): offer_channel = channel_to_dict( g.chain.offer_registry.contract.functions.guidToChannel( guid.int).call()) msig_address = offer_channel['msig_address'] offer_msig = g.chain.offer_multi_sig.bind(msig_address) account = g.chain.w3.toChecksumAddress(g.eth_address) base_nonce = int( request.args.get('base_nonce', g.chain.w3.eth.getTransactionCount(account))) body = request.get_json() try: _post_challange_schema(body) except fastjsonschema.JsonSchemaException as e: return failure('Invalid JSON: ' + e.message) state = g.chain.w3.toBytes(hexstr=body['state']) v = body['v'] r = list(map(lambda s: g.chain.w3.toBytes(hexstr=s), body['r'])) s = list(map(lambda s: g.chain.w3.toBytes(hexstr=s), body['s'])) transactions = [ build_transaction(offer_msig.functions.challengeSettle(state, v, r, s), base_nonce), ] return success({'transactions': transactions})
def post_uri(guid): offer_channel = channel_to_dict( g.chain.offer_registry.contract.functions.guidToChannel( guid.int).call()) msig_address = offer_channel['msig_address'] offer_msig = g.chain.offer_multi_sig.bind(msig_address) account = g.chain.w3.toChecksumAddress(g.eth_address) base_nonce = int( request.args.get('base_nonce', g.chain.w3.eth.getTransactionCount(account))) body = request.get_json() try: _post_uri_schema(body) except fastjsonschema.JsonSchemaException as e: return failure('Invalid JSON: ' + e.message) websocket_uri = body['websocketUri'] transactions = [ build_transaction( offer_msig.functions.setCommunicationUri( g.chain.w3.toHex(text=websocket_uri)), base_nonce), ] return success({'transactions': transactions})
def post_join(guid): offer_channel = channel_to_dict( g.chain.offer_registry.contract.functions.guidToChannel( guid.int).call()) msig_address = offer_channel['msig_address'] offer_msig = g.chain.offer_multi_sig.bind(msig_address) account = g.chain.w3.toChecksumAddress(g.eth_address) base_nonce = int( request.args.get('base_nonce', g.chain.w3.eth.getTransactionCount(account))) body = request.get_json() try: _post_join_schema(body) except fastjsonschema.JsonSchemaException as e: return failure('Invalid JSON: ' + e.message) state = body['state'] v = body['v'] r = body['r'] s = body['s'] transactions = [ build_transaction( offer_msig.functions.joinAgreement(state, v, to_padded_hex(r), to_padded_hex(s)), base_nonce), ] return success({'transactions': transactions})
def get_settlement_period(guid): offer_channel = g.chain.offer_registry.contract.functions.guidToChannel( guid.int).call() channel_data = channel_to_dict(offer_channel) msig_address = channel_data['msig_address'] offer_msig = g.chain.offer_multi_sig.bind(msig_address) settlement_period_end = offer_msig.functions.settlementPeriodEnd().call() return success({'settlementPeriodEnd': settlement_period_end})
def get_websocket(guid): offer_channel = g.chain.offer_registry.contract.functions.guidToChannel( guid.int).call() channel_data = channel_to_dict(offer_channel) msig_address = channel_data['msig_address'] offer_msig = g.chain.offer_multi_sig.bind(msig_address) socket_uri = offer_msig.functions.websocketUri().call() # TODO find a better way than replace socket_uri = g.chain.w3.toText(socket_uri).replace('\u0000', '') if not validate_ws_url(socket_uri): return failure('Contract does not have a valid WebSocket uri', 400) return success({'websocket': socket_uri})
def post_cancel(guid): offer_channel = channel_to_dict( g.chain.offer_registry.contract.functions.guidToChannel( guid.int).call()) msig_address = offer_channel['msig_address'] offer_msig = g.chain.offer_multi_sig.bind(msig_address) account = g.chain.w3.toChecksumAddress(g.eth_address) base_nonce = int( request.args.get('base_nonce', g.chain.w3.eth.getTransactionCount(account))) transactions = [ build_transaction(offer_msig.functions.cancel(), base_nonce), ] return success({'transactions': transactions})
def get_closed(): offers_closed = [] num_of_offers = g.chain.offer_registry.contract.functions.getNumberOfOffers( ).call() for i in range(0, num_of_offers): guid = g.chain.offer_registry.contract.functions.channelsGuids( i).call() offer_channel = g.chain.offer_registry.contract.functions.guidToChannel( guid).call() channel_data = channel_to_dict(offer_channel) msig_address = channel_data['msig_address'] offer_msig = g.chain.offer_multi_sig.bind(msig_address) closed_channel = offer_msig.functions.isClosed().call() if closed_channel: offers_closed.append({'guid': guid, 'address': msig_address}) return success(offers_closed)
def channel_events(ws, guid): offer_channel = channel_to_dict( g.chain.offer_registry.contract.functions.guidToChannel( guid.int).call()) msig_address = offer_channel['msig_address'] offer_msig = g.chain.offer_multi_sig.bind(msig_address) filter_manager = FilterManager() filter_events: Any[Type[WebsocketFilterMessage]] = [ ClosedAgreement, StartedSettle, SettleStateChallenged, ] for evt in filter_events: filter_manager.register(offer_msig.eventFilter, evt) with filter_manager.fetch() as results: for messages in results: if ws.closed: raise RuntimeError("WebSocket is closed") for msg in messages: return ws.send(msg)
def get_myoffers(): account = g.chain.w3.toChecksumAddress(g.eth_address) my_offers = [] num_of_offers = g.chain.offer_registry.contract.functions.getNumberOfOffers( ).call() for i in range(0, num_of_offers): guid = g.chain.offer_registry.contract.functions.channelsGuids( i).call() offer_channel = g.chain.offer_registry.contract.functions.guidToChannel( guid).call() channel_data = channel_to_dict(offer_channel) msig_address = channel_data['msig_address'] offer_msig = g.chain.offer_multi_sig.bind(msig_address) expert = offer_msig.functions.expert().call() ambassador = offer_msig.functions.ambassador().call() if account == expert or account == ambassador: my_offers.append({'guid': guid, 'address': msig_address}) return success(my_offers)
def get_channel_address(guid): offer_channel = g.chain.offer_registry.contract.functions.guidToChannel( guid.int).call() return success({'offer_channel': channel_to_dict(offer_channel)})