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: l2 swaps #241

Merged
merged 7 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
6 changes: 4 additions & 2 deletions autotx/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from autotx.agents.SwapTokensAgent import SwapTokensAgent

from autotx.utils.constants import COINGECKO_API_KEY, OPENAI_API_KEY, OPENAI_MODEL_NAME
from autotx.utils.ethereum.networks import NetworkInfo
from autotx.utils.ethereum.networks import ChainId, NetworkInfo
from autotx.utils.ethereum.helpers.get_dev_account import get_dev_account
from autotx.AutoTx import AutoTx, Config
from autotx.utils.ethereum.agent_account import get_or_create_agent_account
Expand Down Expand Up @@ -92,7 +92,9 @@ def run(prompt: str | None, non_interactive: bool, verbose: bool, logs: str | No
print(f"Smart account deployed: {manager.address}")

if not is_safe_deployed:
send_native(dev_account, manager.address, 10, web3)
# XDAI or MATIC doesn't have the same value as ETH, so we need to fill more
amount_to_fill = 3000 if network_info.chain_id in [ChainId.POLYGON, ChainId.GNOSIS] else 10
send_native(dev_account, manager.address, amount_to_fill, web3)
fill_dev_account_with_erc20(client, dev_account, manager.address, network_info)
print(f"Funds sent to smart account for testing purposes")

Expand Down
4 changes: 3 additions & 1 deletion autotx/tests/integration/test_swap.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from decimal import Decimal
from autotx.utils.ethereum.eth_address import ETHAddress
from autotx.utils.ethereum.helpers.swap_from_eoa import swap
from autotx.utils.ethereum.lifi.swap import build_swap_transaction
from autotx.utils.ethereum.networks import NetworkInfo

Expand Down Expand Up @@ -134,14 +135,15 @@ def test_swap_multiple_tokens(configuration):
(_, _, client, manager) = configuration
network_info = NetworkInfo(client.w3.eth.chain_id)

eth_address = ETHAddress(network_info.tokens["eth"])
eth_address = ETHAddress(network_info.tokens["xdai"])
usdc_address = ETHAddress(network_info.tokens["usdc"])
wbtc_address = ETHAddress(network_info.tokens["wbtc"])
shib_address = ETHAddress(network_info.tokens["shib"])

usdc_balance = manager.balance_of(usdc_address)
assert usdc_balance == 0


sell_eth_for_usdc_transaction = build_swap_transaction(
client,
1,
Expand Down
13 changes: 8 additions & 5 deletions autotx/utils/ethereum/helpers/fill_dev_account_with_erc20.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from autotx.utils.ethereum.constants import NATIVE_TOKEN_ADDRESS
from autotx.utils.ethereum.eth_address import ETHAddress
from autotx.utils.ethereum.helpers.swap_from_eoa import swap
from autotx.utils.ethereum.networks import NetworkInfo
from autotx.utils.ethereum.networks import ChainId, NetworkInfo
from eth_account.signers.local import LocalAccount
from gnosis.eth import EthereumClient

Expand All @@ -14,6 +14,11 @@ def fill_dev_account_with_erc20(
network_info: NetworkInfo,
) -> None:
tokens_to_transfer = {"usdc": 3500, "dai": 3500, "wbtc": 0.1}
if network_info.chain_id is ChainId.GNOSIS:
tokens_to_transfer = {"usdc": 2000, "gno": 5, "cow": 4000 }
if network_info.chain_id is ChainId.POLYGON:
tokens_to_transfer = {"usdc": 2000, "wbtc": 0.01, "dai": 2000 }

native_token_address = ETHAddress(NATIVE_TOKEN_ADDRESS)
for token in network_info.tokens:
if token in tokens_to_transfer:
Expand All @@ -25,8 +30,6 @@ def fill_dev_account_with_erc20(
amount,
native_token_address,
token_address,
network_info.chain_id
)
transfer_erc20(
client.w3, token_address, dev_account, safe_address, amount
network_info.chain_id,
)
transfer_erc20(client.w3, token_address, dev_account, safe_address, amount)
15 changes: 10 additions & 5 deletions autotx/utils/ethereum/helpers/swap_from_eoa.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from decimal import Decimal
import json
from autotx.utils.ethereum.eth_address import ETHAddress
from eth_account.signers.local import LocalAccount
from gnosis.eth import EthereumClient
Expand All @@ -16,19 +17,24 @@ def swap(
) -> None:
txs = build_swap_transaction(
client,
Decimal(amount),
Decimal(str(amount)),
from_token,
to_token,
ETHAddress(user.address),
False,
chain
)

for i, tx in enumerate(txs):
for tx in txs:
if chain in [ChainId.GNOSIS, ChainId.BASE_MAINNET]:
del tx.tx["gas"]
gas = client.w3.eth.estimate_gas(tx.tx)
tx.tx.update({"gas": gas})

transaction = user.sign_transaction( # type: ignore
{
**tx.tx,
"nonce": client.w3.eth.get_transaction_count(user.address)
"nonce": client.w3.eth.get_transaction_count(user.address),
}
)

Expand All @@ -37,5 +43,4 @@ def swap(
receipt = client.w3.eth.wait_for_transaction_receipt(hash)

if receipt["status"] == 0:
print(f"Transaction #{i} failed ")
break
print(f"Transaction to swap {from_token.hex} to {amount} {to_token.hex} failed")
2 changes: 1 addition & 1 deletion autotx/utils/ethereum/lifi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def get_quote_to_amount( # type: ignore
_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 Down
1 change: 1 addition & 0 deletions autotx/utils/ethereum/lifi/swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def get_quote(
"gasPrice": quote["transactionRequest"]["gasPrice"],
"gas": quote["transactionRequest"]["gasLimit"],
"value": Wei(int(quote["transactionRequest"]["value"], 0)),
"chainId": quote["transactionRequest"]["chainId"]
}
)
return QuoteInformation(
Expand Down
6 changes: 5 additions & 1 deletion autotx/utils/ethereum/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def fetch_tokens_for_chain(self, chain_id: int) -> dict[str, str]:
return {
cast(str, token["symbol"]).lower(): Web3.to_checksum_address(cast(str, token["address"]))
for token in token_list
if token["chainId"] == chain_id
if token["chainId"] == chain_id and Web3.is_checksum_address(cast(str, token["address"]))
}


Expand Down Expand Up @@ -80,6 +80,7 @@ class NetworkConfiguration:
"usdc": "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359",
"dai": "0x8f3cf7ad23cd3cadbd9735aff958023239c6a063",
"usdt": "0xc2132d05d31c914a87c6611c10748aeb04b58e8f",
"wbtc": "0x1BFD67037B42Cf73acF2047067bd4F2C47D9BfD6"
},
),
ChainId.BASE_MAINNET: NetworkConfiguration(
Expand Down Expand Up @@ -113,6 +114,9 @@ class NetworkConfiguration:
{
"xdai": NATIVE_TOKEN_ADDRESS,
"wxdai": "0xe91d153e0b41518a2ce8dd3d7944fa863463a97d",
"usdc": "0xDDAfbb505ad214D7b80b1f830fcCc89B60fb7A83",
"cow": "0x177127622c4A00F3d409B75571e12cB3c8973d3c",
"gno": "0x9C58BAcC331c9aa871AFD802DB6379a98e80CEdb"
},
),
}
Expand Down
Loading