From 8ffca385e7a9cc18986f623f4754b5681ba285a1 Mon Sep 17 00:00:00 2001 From: gitmp01 Date: Sun, 24 Apr 2022 07:50:41 +0100 Subject: [PATCH] Add oracle module --- demo-app/relayer/provable.py | 42 ++++++++++++++++++++++++++++++++++-- demo-app/relayer/twitter.py | 15 +++++++------ 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/demo-app/relayer/provable.py b/demo-app/relayer/provable.py index 587539e..a73f691 100644 --- a/demo-app/relayer/provable.py +++ b/demo-app/relayer/provable.py @@ -1,2 +1,40 @@ -def run(args, identity): - print('provable: Not implemented') \ No newline at end of file +import json +import requests + +oracle_endpoint = "https://api.provable.xyz/api/v1" + +def query_status(id): + resp = requests.get(f"{oracle_endpoint}/api/v1/query/{id}/status").json() + + result = None + try: + result = resp['result']['checks'][0]['results'][0] + except Exception as e: + + return False + + return (result, None) + +def fetch_with_proof(url): + payload = { + 'datasource': 'URL', + 'query': url, + 'proof_type': 0, + } + resp = requests.post(oracle_endpoint + "/query/create", data=payload).json() + id = resp['result']['id'] + + resp = requests.get(oracle_endpoint + '/api/v1/query/{}/status'.format(id)).json() + + result = None + + # while not result: + # (result, proof) = query_status(id) + + result, proof = query_status(id) + + if not proof: + proof = b'C0FFEE' # FIXME: handle the case when the proof is missing + return (result, proof) + + \ No newline at end of file diff --git a/demo-app/relayer/twitter.py b/demo-app/relayer/twitter.py index ae1a239..5fb3c5e 100644 --- a/demo-app/relayer/twitter.py +++ b/demo-app/relayer/twitter.py @@ -137,12 +137,13 @@ def get_stream(config, identity, oracle_module, set): text_data = bytes(text, 'utf-8') print('Fetching the proof from the selected oracle...') - time.sleep(10) - - proof = b'C0FFEE' - - print('Proof successfully retrieved!') + try: + (result, proof) = oracle_module.fetch_with_proof(get_url_from_username(username)) + except: + # FIXME: handle the case when the oracle call fails to fetch data + proof = b'C0FFEE' + print('Submitting tx onchain...') tx = contract.functions.sendHexagonsProtocolMessage( @@ -167,9 +168,11 @@ def get_stream(config, identity, oracle_module, set): else: print("The author is out of scope, skipping...") +def get_url_from_username(username): + return f"https://api.hexagons.cafe/getNftDetailsByUsername?username={username}" def nft_from_username(username): - res = requests.get(f"https://api.hexagons.cafe/getNftDetailsByUsername?username={username}").json() + res = requests.get(get_url_from_username(username)).json() if "err" in res.keys(): if username == "stylishbuidler": return {