Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Tcintra committed Dec 27, 2023
1 parent ba0e3b3 commit d258fc4
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Custom
*logs/
*scripts/dev.ipynb

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
129 changes: 129 additions & 0 deletions notebooks/dev.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from dotenv import load_dotenv\n",
"from itertools import permutations\n",
"import requests as req\n",
"\n",
"import sys\n",
"\n",
"sys.path.insert(0, \"../\")\n",
"from src.logging import get_logger\n",
"from src.network.oneinch import OneInchQuotes\n",
"from src.configs import TOKEN_DTOs\n",
"\n",
"logger = get_logger(__name__)\n",
"\n",
"load_dotenv()\n",
"INCH_API_KEY = os.getenv(\"1INCH_API_KEY\")\n",
"assert INCH_API_KEY, \"Missing API Key in .env\"\n",
"\n",
"quoter = OneInchQuotes(INCH_API_KEY, TOKEN_DTOs, calls=20)\n",
"tokens = list(TOKEN_DTOs.keys())\n",
"# payload = quoter.all_quotes(tokens)\n",
"# df = quoter.to_df(payload)\n",
"# dh = DataHandler()\n",
"# logger.info(\"Inserting...\")\n",
"# dh.insert_quotes(df)\n",
"pairs = list(permutations(tokens, 2))\n",
"pair = pairs[0]\n",
"calls = 1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"quotes = quoter.quotes_for_pair(pair, calls=calls)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"in_token, out_token = pair\n",
"in_amount = 1e18\n",
"params = {\n",
" \"src\": in_token,\n",
" \"dst\": out_token,\n",
" \"amount\": str(in_amount),\n",
" \"includeGas\": True,\n",
" \"includeTokensInfo\": True,\n",
" \"includeProtocols\": True,\n",
"}\n",
"res = req.get(quoter.quote_url, params=params, headers=quoter.header)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'_content': b'',\n",
" '_content_consumed': True,\n",
" '_next': None,\n",
" 'status_code': 429,\n",
" 'headers': {'Date': 'Wed, 27 Dec 2023 16:45:23 GMT', 'Content-Length': '0', 'Connection': 'keep-alive', 'x-envoy-upstream-service-time': '1', 'CF-Cache-Status': 'DYNAMIC', 'Set-Cookie': '__cf_bm=nst4HJMsliPBaeOoWX0409tiJWUNlgjQ8IIST56DRZo-1703695523-1-AWEPmPSW7WTtTnfQkkO8mNVq4zSnpgC6XYVtW32LmXF3FgBe7olsGYaVyrIDfSZpNR/hmCWc0HhqgvtGYWHgXdM=; path=/; expires=Wed, 27-Dec-23 17:15:23 GMT; domain=.1inch.dev; HttpOnly; Secure; SameSite=None', 'Server': 'cloudflare', 'CF-RAY': '83c3089a6a0d0f75-EWR'},\n",
" 'raw': <urllib3.response.HTTPResponse at 0x7f6b816b6c50>,\n",
" 'url': 'https://api.1inch.dev/swap/v5.2/1/quote?src=0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48&dst=0xdac17f958d2ee523a2206206994597c13d831ec7&amount=1e%2B18&includeGas=True&includeTokensInfo=True&includeProtocols=True',\n",
" 'encoding': None,\n",
" 'history': [],\n",
" 'reason': 'Too Many Requests',\n",
" 'cookies': <RequestsCookieJar[Cookie(version=0, name='__cf_bm', value='nst4HJMsliPBaeOoWX0409tiJWUNlgjQ8IIST56DRZo-1703695523-1-AWEPmPSW7WTtTnfQkkO8mNVq4zSnpgC6XYVtW32LmXF3FgBe7olsGYaVyrIDfSZpNR/hmCWc0HhqgvtGYWHgXdM=', port=None, port_specified=False, domain='.1inch.dev', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=True, expires=1703697323, discard=False, comment=None, comment_url=None, rest={'HttpOnly': None, 'SameSite': 'None'}, rfc2109=False)]>,\n",
" 'elapsed': datetime.timedelta(microseconds=130544),\n",
" 'request': <PreparedRequest [GET]>,\n",
" 'connection': <requests.adapters.HTTPAdapter at 0x7f6b814f69d0>}"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res.__dict__"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
5 changes: 3 additions & 2 deletions scripts/insert_quotes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os

import traceback
from datetime import datetime
from dotenv import load_dotenv
from src.logging import get_logger
Expand Down Expand Up @@ -33,7 +33,8 @@ def main():
logger.info("Inserting...")
dh.insert_quotes(df)
except Exception as e:
logger.error(e)
logger.error("Error %s\n%s", e, traceback.print_exc())


if __name__ == "__main__":
main()
5 changes: 4 additions & 1 deletion src/network/oneinch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
to store the response data.
"""
import json
import time
from datetime import datetime
from dataclasses import dataclass
from itertools import permutations
Expand Down Expand Up @@ -147,7 +148,7 @@ def protocols(self) -> dict:

@retry(
stop=stop_after_attempt(MAX_RETRIES),
wait=wait_exponential(multiplier=1, min=1, max=5),
wait=wait_exponential(multiplier=1, min=1, max=20),
retry=retry_if_exception_type(req.HTTPError),
)
def quote(self, in_token: str, out_token: str, in_amount: int) -> QuoteResponse:
Expand Down Expand Up @@ -194,6 +195,7 @@ def quotes_for_pair(
int(in_amount),
)
responses.append(res)
time.sleep(2) # Avoid rate limit
return responses

def all_quotes(
Expand All @@ -206,6 +208,7 @@ def all_quotes(
for i, pair in enumerate(pairs):
logger.info("Fetching: %s... %d/%d", pair, i + 1, n)
responses.extend(self.quotes_for_pair(pair, calls=calls))
time.sleep(2) # Avoid rate limit
return responses

def to_df(
Expand Down

0 comments on commit d258fc4

Please sign in to comment.