Skip to content

Commit

Permalink
try-catch on client
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasalexander committed Sep 1, 2022
1 parent 9848908 commit 81ac2f4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 40 deletions.
53 changes: 39 additions & 14 deletions client/client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import json
from starknet_py.contract import Contract
import logging
import time
from typing import Union

from client_tools import fetch_coinbase, fetch_okx, prepare_contract_call_args
from empiric.core.base_client import EmpiricBaseClient, EmpiricAccountClient
from empiric.core.base_client import EmpiricAccountClient, EmpiricBaseClient
from empiric.core.types import HEX_STR
from starknet_py.contract import Contract
from starknet_py.net.client_errors import ClientError

logger = logging.getLogger(__name__)


class OpenOracleClient(EmpiricBaseClient):
Expand Down Expand Up @@ -62,9 +68,7 @@ async def publish_open_oracle_entries_okx_sequential(self, assets: list):
}
return results

async def publish_open_oracle_entries_coinbase(
self, assets=["btc", "eth", "dai"]
) -> hex:
async def publish_open_oracle_entries_coinbase(self, assets: list) -> hex:
coinbase_oracle_data = fetch_coinbase(assets=assets)
calls = [
self.open_oracle_contract.functions["publish_entry"].prepare(
Expand All @@ -75,26 +79,23 @@ async def publish_open_oracle_entries_coinbase(

return await self.send_transactions(calls=calls)

async def publish_open_oracle_entries_coinbase_sequential(
self, assets=["btc", "eth"]
):
async def publish_open_oracle_entries_coinbase_sequential(self, assets: list):
coinbase_oracle_data = fetch_coinbase(assets=assets)
calls = [
self.open_oracle_contract.functions["publish_entry"].prepare(
prepare_contract_call_args(*oracle_data)
)
for oracle_data in coinbase_oracle_data
]

results = {
"Coinbase:"
+ asset_call[0].upper(): await self.send_transactions(calls=[asset_call[1]])
for asset_call in zip(assets, calls)
}
return results

async def publish_open_oracle_entries_all_publishers(
self, assets=["btc", "eth", "dai"]
) -> hex:
async def publish_open_oracle_entries_all_publishers(self, assets: list) -> hex:
okx_oracle_data = fetch_okx(assets=assets)
coinbase_oracle_data = fetch_coinbase(assets=assets)
all_data = okx_oracle_data + coinbase_oracle_data
Expand All @@ -108,9 +109,33 @@ async def publish_open_oracle_entries_all_publishers(
return await self.send_transactions(calls=calls)

async def publish_open_oracle_entries_all_publishers_sequential(
self, assets: list
self, assets: list, n_retries=3
) -> hex:
results_okx = await self.publish_open_oracle_entries_okx_sequential(assets)
results_cb = await self.publish_open_oracle_entries_coinbase_sequential(assets)
results_okx = {}
for attempt in range(n_retries):
try:
results_okx = await self.publish_open_oracle_entries_okx_sequential(
assets
)
break
except ClientError as e:
logger.warning(
f"Client error {e} at {attempt} attempt for OKX, retrying"
)
time.sleep(10)

results_cb = {}
for attempt in range(n_retries):
try:
results_cb = await self.publish_open_oracle_entries_coinbase_sequential(
assets
)
break
except ClientError as e:
logger.warning(
f"Client error {e} at {attempt} attempt for Coinbase, retrying"
)
time.sleep(10)

results = {**results_okx, **results_cb}
return results
9 changes: 5 additions & 4 deletions client/client_tools.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os
import base64
import datetime
import hmac
import requests
from hashlib import sha256
from typing import Tuple, List
import logging
import os
from hashlib import sha256
from typing import List, Tuple

import requests

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
Expand Down
25 changes: 8 additions & 17 deletions client/main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import asyncio
from client import OpenOracleClient
import logging
import os
import time

from dotenv import load_dotenv
from starknet_py.net.client_errors import ClientError
import logging

from client import OpenOracleClient

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

load_dotenv("client/.env")
Expand All @@ -29,18 +29,9 @@ async def main():
)

assets = ["btc", "eth", "dai"]

for attempt in range(3):
try:
results = await c.publish_open_oracle_entries_all_publishers_sequential(
assets
)
except ClientError as e:
logger.warning(f"Client error {e} at {attempt} attempt, retrying")
time.sleep(10)

else:
break
results = await c.publish_open_oracle_entries_all_publishers_sequential(
assets, n_retries=3
)

for k in results:
print(f"Published latest Open Oracle {k} data with tx: {results[k]}")
Expand Down
7 changes: 2 additions & 5 deletions contracts/account/private_key_gen.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from starkware.crypto.signature.signature import (
get_random_private_key,
private_to_stark_key,
)

from starkware.crypto.signature.signature import (get_random_private_key,
private_to_stark_key)

prk = get_random_private_key()
print(f"STARKNET_PRIVATE_KEY : {prk}")
Expand Down

0 comments on commit 81ac2f4

Please sign in to comment.