Skip to content

Commit

Permalink
chore: remove uniswap code & set slippage on lifi swap
Browse files Browse the repository at this point in the history
  • Loading branch information
cbrzn committed Apr 17, 2024
1 parent c1fb915 commit eccbad2
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 434 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Below is a list of existing and anticipated agents that AutoTx can use. If you'd
| Agent | Description | Status |
|-|-|-|
| [Send Tokens](./autotx/agents/SendTokensAgent.py) | Send tokens (ERC20 & ETH) to a receiving address. | :rocket: |
| [Swap Tokens](./autotx/agents/SwapTokensAgent.py) | Swap from one token to another. Currently integrated with Uniswap. | :rocket: |
| [Swap Tokens](./autotx/agents/SwapTokensAgent.py) | Swap from one token to another. Currently integrated with Lifi. | :rocket: |
| [Token Research](./autotx/agents/ResearchTokensAgent.py) | Research tokens, liquidity, prices, graphs, etc. | :rocket: |
| Earn Yield | Stake assets to earn yield. | :memo: [draft](https://github.com/polywrap/AutoTx/issues/98) |
| Bridge Tokens | Bridge tokens from one chain to another. | :memo: [draft](https://github.com/polywrap/AutoTx/issues/46) |
Expand Down
7 changes: 0 additions & 7 deletions autotx/agents/SwapTokensAgent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
from autotx.utils.ethereum.eth_address import ETHAddress
from autotx.utils.ethereum.lifi.swap import build_swap_transaction
from autotx.utils.ethereum.networks import NetworkInfo
# from autotx.utils.ethereum.uniswap.swap import SUPPORTED_UNISWAP_V3_NETWORKS, build_swap_transaction
# from gnosis.eth import EthereumNetworkNotSupported as ChainIdNotSupported

name = "swap-tokens"

Expand Down Expand Up @@ -77,11 +75,6 @@ def get_tokens_address(token_in: str, token_out: str, network_info: NetworkInfo)
token_in = token_in.lower()
token_out = token_out.lower()

# if not network_info.chain_id in SUPPORTED_UNISWAP_V3_NETWORKS:
# raise ChainIdNotSupported(
# f"Network {network_info.chain_id.name.lower()} not supported for swap"
# )

if token_in not in network_info.tokens:
raise Exception(f"Token {token_in} is not supported in network {network_info.chain_id.name.lower()}")

Expand Down
2 changes: 1 addition & 1 deletion autotx/tests/agents/token/test_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_auto_tx_swap_native(configuration, auto_tx):
auto_tx.run(prompt, non_interactive=True)

new_balance = manager.balance_of(usdc_address)

print(new_balance)
assert balance + 100 <= new_balance

def test_auto_tx_swap_multiple_1(configuration, auto_tx):
Expand Down
86 changes: 0 additions & 86 deletions autotx/tests/integration/test_lifi_swap.py

This file was deleted.

167 changes: 58 additions & 109 deletions autotx/tests/integration/test_swap.py
Original file line number Diff line number Diff line change
@@ -1,121 +1,70 @@
from autotx.utils.ethereum import (
get_erc20_balance,
get_native_balance,
)
from autotx.utils.ethereum.networks import NetworkInfo
from autotx.utils.ethereum.eth_address import ETHAddress
from autotx.utils.ethereum.uniswap.swap import build_swap_transaction
from autotx.utils.ethereum.lifi.swap import build_swap_transaction
from autotx.utils.ethereum.networks import NetworkInfo

def test_swap(configuration):
(user, _, client, _) = configuration

def test_swap_through_safe(configuration):
(_, _, client, manager) = configuration
network_info = NetworkInfo(client.w3.eth.chain_id)
weth_address = ETHAddress(network_info.tokens["weth"])
wbtc_address = ETHAddress(network_info.tokens["wbtc"])

user_addr = ETHAddress(user.address)

balance = get_erc20_balance(client.w3, wbtc_address, user_addr)
assert balance == 0

txs = build_swap_transaction(
client, 0.05, weth_address.hex, wbtc_address.hex, user_addr.hex, False
)

for i, tx in enumerate(txs):
transaction = user.sign_transaction(
{
**tx.tx,
"nonce": client.w3.eth.get_transaction_count(user_addr.hex),
"gas": 200000,
}
)

hash = client.w3.eth.send_raw_transaction(transaction.rawTransaction)

receipt = client.w3.eth.wait_for_transaction_receipt(hash)

if receipt["status"] == 0:
print(f"Transaction #{i} failed ")
break

new_balance = get_erc20_balance(client.w3, wbtc_address, user_addr)
assert new_balance == 0.05

def test_swap_recieve_native(configuration):
(user, _, client, _) = configuration

network_info = NetworkInfo(client.w3.eth.chain_id)
eth_address = ETHAddress(network_info.tokens["eth"])
usdc_address = ETHAddress(network_info.tokens["usdc"])

user_addr = ETHAddress(user.address)

balance = get_native_balance(client.w3, user_addr)
assert int(balance) == 9989

tx = build_swap_transaction(
client, 5, eth_address.hex, usdc_address.hex, user_addr.hex, True
)

transaction = user.sign_transaction(
{
**tx[0].tx,
"nonce": client.w3.eth.get_transaction_count(user_addr.hex),
"gas": 200000,
}
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,
eth_address,
usdc_address,
manager.address,
True,
network_info.chain_id,
)

hash = client.w3.eth.send_raw_transaction(transaction.rawTransaction)

receipt = client.w3.eth.wait_for_transaction_receipt(hash)

if receipt["status"] == 0:
print(f"Transaction to swap ETH -> USDC failed ")

balance = get_native_balance(client.w3, user_addr)
assert int(balance) == 9984

txs = build_swap_transaction(
client, 4, usdc_address.hex, eth_address.hex, user_addr.hex, False
hash = manager.send_tx(sell_eth_for_usdc_transaction[0].tx)
manager.wait(hash)
usdc_balance = manager.balance_of(usdc_address)
assert usdc_balance > 2900

wbtc_balance = manager.balance_of(wbtc_address)
assert wbtc_balance == 0

buy_wbtc_with_usdc_transaction = build_swap_transaction(
client,
0.01,
usdc_address,
wbtc_address,
manager.address,
False,
network_info.chain_id,
)

for i, tx in enumerate(txs):
transaction = user.sign_transaction(
{
**tx.tx,
"nonce": client.w3.eth.get_transaction_count(user_addr.hex),
"gas": 200000,
}
)

hash = client.w3.eth.send_raw_transaction(transaction.rawTransaction)

receipt = client.w3.eth.wait_for_transaction_receipt(hash)

if receipt["status"] == 0:
print(f"Transaction #{i} to swap USDC -> ETH failed ")
break

balance = get_native_balance(client.w3, user_addr)
assert int(balance) == 9988

def test_swap_through_safe(configuration):
(_, _, client, manager) = configuration

network_info = NetworkInfo(client.w3.eth.chain_id)
weth_address = ETHAddress(network_info.tokens["weth"])
usdc_address = ETHAddress(network_info.tokens["usdc"])

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

txs = build_swap_transaction(
client, 6000, weth_address.hex, usdc_address.hex, manager.address.hex, False
hash = manager.send_tx(buy_wbtc_with_usdc_transaction[0].tx)
manager.wait(hash)
hash = manager.send_tx(buy_wbtc_with_usdc_transaction[1].tx)
manager.wait(hash)
wbtc_balance = manager.balance_of(wbtc_address)
assert wbtc_balance >= 0.01

shib_balance = manager.balance_of(shib_address)
assert shib_balance == 0

sell_wbtc_for_shib = build_swap_transaction(
client,
0.005,
wbtc_address,
shib_address,
manager.address,
True,
network_info.chain_id,
)

hash = manager.send_tx_batch(txs, require_approval=False)
hash = manager.send_tx(sell_wbtc_for_shib[0].tx)
manager.wait(hash)

new_balance = manager.balance_of(usdc_address)
assert new_balance == 6000
hash = manager.send_tx(sell_wbtc_for_shib[1].tx)
manager.wait(hash)
shib_balance = manager.balance_of(shib_address)
shib_balance = manager.balance_of(shib_address)
assert shib_balance > 0
2 changes: 1 addition & 1 deletion autotx/utils/ethereum/helpers/get_native_token_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ def get_native_token_symbol(network: ChainId) -> str:
if not native_token_symbol:
raise Exception(f"Native token not found for network {network.name}")

return native_token_symbol
return native_token_symbol.upper()
2 changes: 2 additions & 0 deletions autotx/utils/ethereum/lifi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def get_quote(
amount: int,
_from: ETHAddress,
chain: ChainId,
slippage: float
) -> dict[str, Any]:
params = {
"fromToken": from_token.hex,
Expand All @@ -26,6 +27,7 @@ def get_quote(
"fromAddress": _from.hex,
"fromChain": chain.value,
"toChain": chain.value,
"slippage": slippage
}
response = requests.get(cls.BASE_URL + "/quote", params=params) # type: ignore
if response.status_code == 200:
Expand Down
7 changes: 4 additions & 3 deletions autotx/utils/ethereum/lifi/swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from gnosis.eth import EthereumClient
from web3.types import TxParams, Wei

SLIPPAGE = 0.01 # 1%

def build_swap_transaction(
ethereum_client: EthereumClient,
Expand All @@ -31,11 +32,11 @@ def build_swap_transaction(
token_out_price_in_usd = Lifi.get_token_price(token_out_address, chain)
amount_token_to_buy = (token_out_price_in_usd * amount) / token_in_price_in_usd
amount_in_integer = int(amount_token_to_buy * (10**decimals))
# add slippage (default is 0.5%) to ensure we get the expected amount
amount_in_integer = int(amount_in_integer * 0.005 + amount_in_integer)
# add slippage plus 0.05% to ensure we get the expected amount
amount_in_integer = int(amount_in_integer * (SLIPPAGE + 0.005) + amount_in_integer)

quote = Lifi.get_quote(
token_in_address, token_out_address, amount_in_integer, _from, chain
token_in_address, token_out_address, amount_in_integer, _from, chain, SLIPPAGE
)
transactions: list[PreparedTx] = []

Expand Down
Empty file.
Loading

0 comments on commit eccbad2

Please sign in to comment.