Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: support lifi api key #242

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions autotx/utils/ethereum/lifi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
from typing import Any
import requests
import re
Expand Down Expand Up @@ -36,19 +37,28 @@ def handle_lifi_response(response: requests.Response) -> dict[str, Any]:
raise LifiApiError(f"Fetch quote failed with error: {response_json['message']}")


def add_authorization_info_if_provided(params: dict[str, Any]) -> dict[str, Any] | None:
headers: dict[str, Any] | None = None
lifi_api_key = os.getenv("LIFI_API_KEY")
if lifi_api_key:
headers = {"x-lifi-api-key": lifi_api_key}
params["integrator"] = "polywrap"
return headers


class Lifi:
BASE_URL = "https://li.quest/v1"

@classmethod
def get_quote_to_amount( # type: ignore
def get_quote_to_amount( # type: ignore
cls,
from_token: ETHAddress,
to_token: ETHAddress,
amount: int,
_from: ETHAddress,
chain: ChainId,
slippage: float,
) -> dict[str, Any]:
) -> dict[str, Any]:
params: dict[str, Any] = {
"fromToken": from_token.hex,
"toToken": to_token.hex,
Expand All @@ -59,9 +69,10 @@ def get_quote_to_amount( # type: ignore
"slippage": slippage,
"contractCalls": [],
}
headers = add_authorization_info_if_provided(params)
attempt_count = 0
while attempt_count < 2:
response = requests.post(cls.BASE_URL + "/quote/contractCalls", json=params)
response = requests.post(cls.BASE_URL + "/quote/contractCalls", json=params, headers=headers)
try:
return handle_lifi_response(response)
except LifiApiError as e:
Expand Down Expand Up @@ -94,5 +105,6 @@ def get_quote_from_amount(
"toChain": chain.value,
"slippage": slippage,
}
response = requests.get(cls.BASE_URL + "/quote", params=params) # type: ignore
headers = add_authorization_info_if_provided(params)
response = requests.get(cls.BASE_URL + "/quote", params=params, headers=headers) # type: ignore
return handle_lifi_response(response)
Loading