-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(trader): Add action for trading using arbie
- Loading branch information
Showing
19 changed files
with
324 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
"""Trader contains actions for executing trades.""" | ||
|
||
import logging | ||
|
||
from Arbie.Actions import Action | ||
from Arbie.Contracts import GenericToken | ||
from Arbie.Variables import BigNumber, PoolType | ||
|
||
logger = logging.getLogger() | ||
|
||
|
||
def _is_trade_uni(trade): | ||
for pool in trade.pools: | ||
if pool.pool_type is not PoolType.uniswap: | ||
return False | ||
return True | ||
|
||
|
||
async def get_balance(weth: GenericToken, web3, trader_address): | ||
balance_pre = await weth.balance_of(trader_address) | ||
account_balance = BigNumber.from_value(web3.eth.getBalance(trader_address)) | ||
return balance_pre.to_number() + account_balance.to_number() | ||
|
||
|
||
def _perform_trade(trade, arbie, address, min_profit): | ||
if not _is_trade_uni(trade): | ||
return False | ||
|
||
amount_out = arbie.check_out_given_in(trade) | ||
if amount_out - trade.amount_in > min_profit: | ||
logger.info(f"Executing trade with return: {amount_out}, trade: {trade}") | ||
return arbie.swap(trade, address) | ||
return False | ||
|
||
|
||
def perform_trade(data, trader_address): | ||
arbie = data.arbie() | ||
min_profit = data.min_profit() | ||
for trade in data.trades(): | ||
if _perform_trade(trade, arbie, trader_address, min_profit): | ||
return True | ||
return False | ||
|
||
|
||
class Trader(Action): | ||
"""Find optimal arbitrage opportunity for a list sorted trades. | ||
Remove all trades that are not profitable. | ||
[Settings] | ||
input: | ||
web3: web3 | ||
arbie: arbie | ||
trades: filtered_trades | ||
min_profit: 0.3 | ||
weth: weth | ||
trader_address: trader_address | ||
output: | ||
profit: profit | ||
""" | ||
|
||
async def on_next(self, data): | ||
trader_address = data.trader_address() | ||
balance_pre = await get_balance(data.weth(), data.web3(), trader_address) | ||
|
||
if not perform_trade(data, trader_address): | ||
raise Exception("No trade performed") | ||
|
||
balance_post = await get_balance(data.weth(), data.web3(), trader_address) | ||
|
||
data.profit(balance_post - balance_pre) | ||
logger.info("Finished trading") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
"""System tests for PoolUpdater.""" | ||
|
||
import pytest | ||
|
||
from Arbie.Actions import ActionTree, Store, Trader | ||
from Arbie.Contracts import GenericToken | ||
from Arbie.Variables import BigNumber | ||
|
||
|
||
class TestTrader(object): | ||
@pytest.fixture | ||
def trader_address(self, w3, weth: GenericToken, arbie, dummy_address): | ||
weth.transfer(dummy_address, BigNumber(2)) | ||
weth.approve(arbie.get_address(), BigNumber(2), dummy_address) | ||
return dummy_address | ||
|
||
@pytest.fixture | ||
def trade_store(self, w3, arbie, trade, weth, trader_address): | ||
store = Store() | ||
store.add("arbie", arbie) | ||
store.add("filtered_trades", [trade]) | ||
store.add("weth", weth) | ||
store.add("web3", w3) | ||
store.add("trader_address", trader_address) | ||
return store | ||
|
||
@pytest.mark.asyncio | ||
async def test_on_next(self, trade_store, trade): | ||
trade.amount_in = 1 | ||
trade_store.add("min_profit", 0) | ||
tree = ActionTree(trade_store) | ||
tree.add_action(Trader({"input": {"min_profit": "min_profit"}, "output": {}})) | ||
await tree.run() | ||
assert trade_store.get("profit") > 0.036 # noqa: WPS432 | ||
|
||
@pytest.mark.asyncio | ||
async def test_no_profit(self, trade_store, trade): | ||
trade.amount_in = 1 | ||
tree = ActionTree(trade_store) | ||
tree.add_action(Trader()) | ||
with pytest.raises(Exception): | ||
await tree.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.