From 74ddcbb28a72649cca046bcc9df564ee34ba23d6 Mon Sep 17 00:00:00 2001 From: Ricky Roller Date: Thu, 5 Dec 2024 13:05:35 -0800 Subject: [PATCH] Fix some errors in lifi formatting --- src/wallet/adapters/lifi/lifi_adapter.py | 31 ++++++++++-------------- src/wallet/wallet.py | 5 ++-- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/wallet/adapters/lifi/lifi_adapter.py b/src/wallet/adapters/lifi/lifi_adapter.py index 876a2b2..d44571c 100644 --- a/src/wallet/adapters/lifi/lifi_adapter.py +++ b/src/wallet/adapters/lifi/lifi_adapter.py @@ -57,8 +57,9 @@ def get_quote( Get a quote for a token swap using LiFi API Args: - token_in: Address of input token - token_out: Address of output token + chain_id: Chain ID for the transaction + token_in: Input token information + token_out: Output token information amount_in: Amount of input token (in decimal) Returns: @@ -71,8 +72,8 @@ def get_quote( amount_base_units = str(int(amount_in * Decimal(10 ** token_in["decimals"]))) params = { - "fromChain": chain_id, - "toChain": chain_id, + "fromChain": str(chain_id), + "toChain": str(chain_id), "fromToken": token_in["address"], "toToken": token_out["address"], "fromAmount": amount_base_units, @@ -81,7 +82,11 @@ def get_quote( } try: - response = requests.get(f"{self.LIFI_API_URL}/quote", params=params) + response = requests.get( + f"{self.LIFI_API_URL}/quote", + params=params, + headers={"accept": "application/json"}, + ) response.raise_for_status() quote_data = response.json() @@ -107,9 +112,6 @@ async def swap(self, quote: Dict[str, Any]) -> AsyncGenerator[Dict[str, Any], No Returns: Final swap result with transaction details """ - from_token = quote["action"]["fromToken"] - to_token = quote["action"]["toToken"] - transaction_request = { "data": quote["transactionRequest"]["data"], "to": quote["transactionRequest"]["to"], @@ -130,23 +132,16 @@ async def swap(self, quote: Dict[str, Any]) -> AsyncGenerator[Dict[str, Any], No transaction_request, self._wallet._account.key ) - # Yield broadcasting status yield { - "status": "broadcasting", - "message": "Broadcasting transaction to network...", + "status": "pending", + "message": "Transaction submitted, waiting for confirmation...", + "transaction_hash": tx_hash.hex(), } tx_hash = await self._wallet._web3.eth.send_raw_transaction( signed_tx.raw_transaction ) - # Yield pending status with transaction hash - yield { - "status": "pending", - "message": "Transaction submitted, waiting for confirmation...", - "transaction_hash": tx_hash.hex(), - } - receipt = await self._wallet._web3.eth.wait_for_transaction_receipt(tx_hash) # Return final result diff --git a/src/wallet/wallet.py b/src/wallet/wallet.py index 04688b6..2dff120 100644 --- a/src/wallet/wallet.py +++ b/src/wallet/wallet.py @@ -32,10 +32,11 @@ def __init__(self, key_path: Optional[str] = None): assert rpc_url, "RPC_URL is not set" self._web3 = AsyncWeb3(AsyncWeb3.AsyncHTTPProvider(rpc_url)) hardhat_private_key = os.getenv("HARDHAT_PRIVATE_KEY") + keyfile = key_path if key_path else "./keyfile" if hardhat_private_key: self._account = self._web3.eth.account.from_key(hardhat_private_key) - else: - private_key = self.load_private_key(key_path if key_path else "./keyfile") + elif keyfile: + private_key = self.load_private_key(keyfile) self._account = ( None if not private_key