Skip to content

Commit

Permalink
broken commit point
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkel committed Oct 31, 2024
1 parent 386a24d commit eb64d06
Showing 1 changed file with 73 additions and 1 deletion.
74 changes: 73 additions & 1 deletion oracle/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import traceback
import os
import urllib.request
import time

from core_functions import handle_buy, handle_sell, fulfill_deposit, ft_balance, fulfill_withdraw, ft_total_supply
from exchange import swap_near_to_usdc, calculate_usdc_total_from_holdings, rebalance_portfolio, swap_usdc_to_near, decimal_to_str
Expand All @@ -12,6 +13,7 @@
# Set up PoolApiClient with the AILP URL
AILP_URL = os.getenv('AILPURL', 'http://localhost:3000')
NEAR_CONFIG=os.getenv("NEAR_CONFIG", "")
NEAR_CONFIG_THREAD=os.getenv("NEAR_CONFIG_THREAD", "")
pool_api = PoolApiClient(AILP_URL)

def fetch_jobs():
Expand All @@ -22,6 +24,76 @@ def update_job_status(job_id, status, details=None):
"""Updates the job status via the Pool API."""
pool_api.update_job_status(job_id, status, details)

def near_thread_html(thread_id):
base_url = "https://app.near.ai/api/trpc/hub.thread"

# Encode the `input` parameter with the dynamic thread_id
input_param = {
"0": {
"json": {
"threadId": thread_id.decode('utf-8')
}
}
}
encoded_input = urllib.parse.quote(json.dumps(input_param))

# Construct the final URL with batch and encoded input
url = f"{base_url}?batch=1&input={encoded_input}"

headers = {
'Content-Type': 'application/json',
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br, zstd',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive',
'Referer': 'https://app.near.ai/agents/smartpool.near/prediction-market-assistant/0.0.14/run?threadId=thread_17bba66a1e69449f8d113c25',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'Sec-GPC': '1',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',
'Authorization': f'Bearer {NEAR_CONFIG_THREAD}',
'sec-ch-ua-platform': '"Linux"',
'trpc-accept': 'application/jsonl',
'x-trpc-source': 'nextjs-react'
}
print("HEADERS", headers)

req = urllib.request.Request(url, headers=headers)
print("Calling threadurl...", url)
htmlresult = None
while htmlresult is None:
time.sleep(10)
with urllib.request.urlopen(req) as response:
result = response.read()
print("______---")
print(result)
jsonresult = json.loads(result)
htmlresult = find_html_content(jsonresult)
print("html")
print(htmlresult)
return htmlresult



# Function to recursively search for 'content' in nested JSON structure
def find_html_content(data):
if isinstance(data, dict):
# Check for the content in a dict format
if "filename" in data and data["filename"] == "index.html":
return data.get("content")
for key, value in data.items():
result = find_html_content(value)
if result:
return result
elif isinstance(data, list):
# Loop through list elements
for item in data:
result = find_html_content(item)
if result:
return result
return None

def call_near_ai_api(prediction_market_url, liquid_usdc, holdings):
url = "https://api.near.ai/v1/agent/runs"
print("NEAR_C", NEAR_CONFIG)
Expand Down Expand Up @@ -49,7 +121,7 @@ def call_near_ai_api(prediction_market_url, liquid_usdc, holdings):
result = response.read()
print("RESULT")
print(result)
return json.loads(result)
return near_thread_html(result)
except Exception as e:
print(f"Error calling NEAR AI API: {e}")
return None
Expand Down

0 comments on commit eb64d06

Please sign in to comment.