Skip to content

Commit

Permalink
generated tests and data
Browse files Browse the repository at this point in the history
gen

generated

gen
  • Loading branch information
sklbancor committed Jul 25, 2023
1 parent dfe3b99 commit 33a2512
Show file tree
Hide file tree
Showing 25 changed files with 3,436 additions and 442 deletions.
Empty file added fastlane_bot/tests/__init__.py
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
19 changes: 19 additions & 0 deletions fastlane_bot/tests/nbtest/_data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# NBTest Data

All data referred to by NBTest notebooks is stored in this directory. It is copied into the respective directory in the test area by the `run_tests` script. Currently the data will be accessed differently in the notebooks and in the actual tests.

- **Notebooks**. In the notebooks the data can be accessed via a relative path `_data\mydata.csv`.

- **Tests**. In the actual tests the data must be imported via the relative path `fastlane_bot/tests/nbtest/_data/mydata.csv`


Example

try:
with open("_data/mydata.csv", "r") as f:
data = f.read()
except:
with open("fastlane_bot/tests/nbtest/_data/mydata.csv", "r") as f:
data = f.read()


149 changes: 118 additions & 31 deletions fastlane_bot/tests/nbtest/test_002_CPCandOptimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,81 @@


from fastlane_bot.tools.cpc import ConstantProductCurve as CPC, CPCContainer, T, CPCInverter, Pair
#from fastlane_bot.tools.simplepair import SimplePair
from fastlane_bot.tools.optimizer import CPCArbOptimizer, F
#import carbon.tools.tokenscale as ts
from fastlane_bot.tools.optimizer import CPCArbOptimizer, F, MargPOptimizer, SimpleOptimizer
from fastlane_bot.tools.analyzer import CPCAnalyzer
print("{0.__name__} v{0.__VERSION__} ({0.__DATE__})".format(Pair))
print("{0.__name__} v{0.__VERSION__} ({0.__DATE__})".format(CPC))
#print("{0.__name__} v{0.__VERSION__} ({0.__DATE__})".format(ts.TokenScale))
print("{0.__name__} v{0.__VERSION__} ({0.__DATE__})".format(CPCArbOptimizer))

from fastlane_bot.testing import *
plt.style.use('seaborn-dark')
#plt.style.use('seaborn-dark')
plt.rcParams['figure.figsize'] = [12,6]
from fastlane_bot import __VERSION__
require("3.0", __VERSION__)



try:
df = pd.read_csv("../nbtest_data/NBTEST_002_Curves.csv.gz")
market_df = pd.read_csv("_data/NBTEST_002_Curves.csv.gz")
except:
df = pd.read_csv("fastlane_bot/tests/nbtest_data/NBTEST_002_Curves.csv.gz")
CCmarket = CPCContainer.from_df(df)
market_df = pd.read_csv("fastlane_bot/tests/nbtest/_data/NBTEST_002_Curves.csv.gz")
CCmarket = CPCContainer.from_df(market_df)


# ------------------------------------------------------------
# Test 002
# File test_002_CPCandOptimizer.py
# Segment description
# ------------------------------------------------------------
def test_description():
# ------------------------------------------------------------

d = CCmarket.bycid("167").description().splitlines()
d0 = """
cid = 167 [167]
primary = WETH/DAI [WETH/DAI]
pp = 1,826.764318 DAI per WETH
pair = DAI/WETH [DAI/WETH]
tknx = 3,967,283.591895 DAI [virtual: 3,967,283.592]
tkny = 2,171.754481 WETH [virtual: 2,171.754]
p = 0.0005474159913752679 [min=None, max=None] WETH per DAI
fee = 0.003
descr = sushiswap_v2 DAI/WETH 0.003
""".strip().splitlines()
d0 = [l.strip() for l in d0]
assert d == d0
for l in d0:
print(l)


# ------------------------------------------------------------
# Test 002
# File test_002_CPCandOptimizer.py
# Segment bycids
# ------------------------------------------------------------
def test_bycids():
# ------------------------------------------------------------

CC = CCmarket

assert len(CC.bycids()) == len(CC)
assert type(CC.bycids()) == type(CC)
assert type(CC.bycids(ascc=False)) == tuple
for c in CC:
assert isinstance(c.cid, str), f"{c.cid} is not of type str"
cids = [c.cid for c in CC]
assert raises(CC.bycids, include="foo", endswith="bar") == 'include and endswith cannot be used together'
assert raises(CC.bycids,"167, 168, 169")
CC1 = CC.bycids(["167", "168", "169"])
assert len(CC1) == 3
assert [c.cid for c in CC1] == ['167', '168', '169']
CC2 = CC.bycids(endswith="11")
assert len(CC2) == 5
assert [c.cid for c in CC2] == ['211', '311', '411', '511', '611']
CC3 = CC.bycids(endswith="11", exclude=['311', '411'])
assert [c.cid for c in CC3] == ['211', '511', '611']


# ------------------------------------------------------------
# Test 002
# File test_002_CPCandOptimizer.py
Expand Down Expand Up @@ -535,7 +587,7 @@ def test_price_estimates_in_optimizer():
CCfm += CPC.from_pk(p=pp, k=k, pair=pair, cid = f"mkt-{ctr}")
ctr += 1

O = CPCArbOptimizer(CCfm)
O = MargPOptimizer(CCfm)
assert O.MO_PSTART == O.MO_P
tknq = "WETH"
df = O.margp_optimizer(tknq, result=O.MO_PSTART)
Expand Down Expand Up @@ -711,13 +763,13 @@ def test_new_cpc_features_in_v2():
def test_real_data_and_retrieval_of_curves():
# ------------------------------------------------------------

try:
df = pd.read_csv("../nbtest_data/NBTEST_002_Curves.csv.gz")
except:
df = pd.read_csv("fastlane_bot/tests/nbtest_data/NBTEST_002_Curves.csv.gz")
CC = CPCContainer.from_df(df)
# try:
# df = pd.read_csv("../nbtest_data/NBTEST_002_Curves.csv.gz")
# except:
# df = pd.read_csv("fastlane_bot/tests/nbtest_data/NBTEST_002_Curves.csv.gz")
CC = CPCContainer.from_df(market_df)
assert len(CC) == 459
assert len(CC) == len(df)
assert len(CC) == len(market_df)
assert len(CC.pairs()) == 326
assert len(CC.tokens()) == 141
assert CC.tokens_s
Expand All @@ -735,7 +787,7 @@ def test_real_data_and_retrieval_of_curves():
cids = [c.cid for c in CC.bypairs(CC.fp(onein="WBTC"))]
assert len(cids) == len(CC1)
assert CC.bycid("bla") is None
assert not CC.bycid(191) is None
assert not CC.bycid("191") is None
assert raises(CC.bycids, ["bla"])
assert len(CC.bycids(cids)) == len(cids)
assert len(CC.bytknx("WETH")) == 46
Expand Down Expand Up @@ -1065,8 +1117,8 @@ def test_simple_optimizer():
assert iseq([c.p for c in CC0][-1], 2000)

# +
O = CPCArbOptimizer(CC)
O0 = CPCArbOptimizer(CC0)
O = SimpleOptimizer(CC)
O0 = SimpleOptimizer(CC0)
func = O.simple_optimizer(result=O.SO_DXDYVECFUNC)
func0 = O0.simple_optimizer(result=O.SO_DXDYVECFUNC)
funcs = O.simple_optimizer(result=O.SO_DXDYSUMFUNC)
Expand Down Expand Up @@ -1158,7 +1210,7 @@ def test_optimizer_plus_inverted_curves():
# CC.plot()
# -

O = CPCArbOptimizer(CC)
O = SimpleOptimizer(CC)
r = O.simple_optimizer()
print(f"Arbitrage gains: {-r.valx:.4f} {r.tknxp} [time={r.time:.4f}s]")
assert iseq(r.result, -1.3194573866437527)
Expand Down Expand Up @@ -1248,7 +1300,7 @@ def test_tradeinstructions():
for i in range(10)
]
tild = TI.to_dicts(til)
tildf = TI.to_df(til)
tildf = TI.to_df(til, robj=None)
assert len(tild) == 10
assert len(tildf) == 10
assert tild[0] == {
Expand Down Expand Up @@ -1286,7 +1338,7 @@ def test_margp_optimizer():
CCa += CPC.from_pk(pair="WETH/USDC", p=2000, k=10*20000, cid="c0")
CCa += CPC.from_pk(pair="WETH/USDT", p=2000, k=10*20000, cid="c1")
CCa += CPC.from_pk(pair="USDC/USDT", p=1.0, k=200000*200000, cid="c2")
O = CPCArbOptimizer(CCa)
O = MargPOptimizer(CCa)

r = O.margp_optimizer("WETH", result=O.MO_DEBUG)
assert isinstance(r, dict)
Expand Down Expand Up @@ -1322,7 +1374,7 @@ def test_margp_optimizer():
assert r.targettkn == "WETH"
assert r.dtokens is None
assert sum(abs(x) for x in r.dtokens_t) < 1e-10
assert r.p_optimal is None
assert not r.p_optimal is None
assert iseq(0.0005, r.p_optimal_t[0], r.p_optimal_t[1])
assert set(r.tokens_t) == {'USDC', 'USDT'}
assert r.errormsg is None
Expand Down Expand Up @@ -1351,7 +1403,7 @@ def test_margp_optimizer():
assert sum(abs(x) for x in r.dtokens_t) < 1e-10
assert iseq(0.0005, r.p_optimal["USDC"], r.p_optimal["USDT"])
assert iseq(0.0005, r.p_optimal_t[0], r.p_optimal_t[1])
assert tuple(r.p_optimal.values()) == r.p_optimal_t
assert tuple(r.p_optimal.values())[:-1] == r.p_optimal_t
assert set(r.tokens_t) == set(('USDC', 'USDT'))
assert r.errormsg is None
assert r.is_error == False
Expand All @@ -1365,7 +1417,7 @@ def test_margp_optimizer():
CCa += CPC.from_pk(pair="WETH/USDC", p=2000, k=10*20000, cid="c0")
CCa += CPC.from_pk(pair="WETH/USDT", p=2000, k=10*20000, cid="c1")
CCa += CPC.from_pk(pair="USDC/USDT", p=1.2, k=200000*200000, cid="c2")
O = CPCArbOptimizer(CCa)
O = MargPOptimizer(CCa)

r = O.margp_optimizer("WETH", result=O.MO_DEBUG)
assert isinstance(r, dict)
Expand Down Expand Up @@ -1395,11 +1447,11 @@ def test_margp_optimizer():
assert abs(r.dtokens_t[0]) < 1e-6
assert abs(r.dtokens_t[1]) < 1e-6
assert r.dtokens["WETH"] == float(r)
assert tuple(r.p_optimal.values()) == r.p_optimal_t
assert tuple(r.p_optimal) == r.tokens_t
assert tuple(r.p_optimal.values())[:-1] == r.p_optimal_t
assert tuple(r.p_optimal)[:-1] == r.tokens_t
assert iseq(r.p_optimal_t[0], 0.0005421803152482512) or iseq(r.p_optimal_t[0], 0.00045575394031021585)
assert iseq(r.p_optimal_t[1], 0.0005421803152482512) or iseq(r.p_optimal_t[1], 0.00045575394031021585)
assert tuple(r.p_optimal.values()) == r.p_optimal_t
assert tuple(r.p_optimal.values())[:-1] == r.p_optimal_t
assert set(r.tokens_t) == set(('USDC', 'USDT'))
assert r.errormsg is None
assert r.is_error == False
Expand All @@ -1408,7 +1460,42 @@ def test_margp_optimizer():

abs(r.dtokens_t[0])

ti = r.trade_instructions()
assert len(ti) == 3
dfa = r.trade_instructions(ti_format=O.TIF_DFAGGR)
assert len(dfa)==7
assert list(dfa.index) == ['c0', 'c1', 'c2', 'PRICE', 'AMMIn', 'AMMOut', 'TOTAL NET']
assert list(dfa.columns) == ['WETH', 'USDC', 'USDT']
assert dfa.loc["PRICE"][0] == 1
assert iseq(dfa.loc["PRICE"][1], 0.0005421803152)
assert iseq(dfa.loc["PRICE"][2], 0.0004557539403)
dfa

df = r.trade_instructions(ti_format=O.TIF_DF)
assert len(df) == 3
assert list(df.columns) == ['pair', 'pairp', 'tknin', 'tknout', 'WETH', 'USDC', 'USDT']
df

df = r.trade_instructions(ti_format=O.TIF_DF).fillna("")
assert len(df) == 3
assert list(df.columns) == ['pair', 'pairp', 'tknin', 'tknout', 'WETH', 'USDC', 'USDT']
assert df["USDT"].loc["c0"] == ""
df

dcts = r.trade_instructions(ti_format=O.TIF_DICTS)
assert len(dcts) == 3
assert list(dcts[0].keys()) == ['cid', 'tknin', 'amtin', 'tknout', 'amtout', 'error']
d0 = dcts[0]
assert d0["cid"] == "c0"
assert iseq(d0["amtin"], 0.41326380379418914)
dcts

objs = r.trade_instructions(ti_format=O.TIF_OBJECTS)
assert len(objs) == 3
assert type(objs[0]).__name__ == 'TradeInstruction'
objs

help(r.trade_instructions)


# ------------------------------------------------------------
Expand All @@ -1423,8 +1510,8 @@ def notest_simple_optimizer_demo():
O = CPCArbOptimizer(CC)
c0 = CC.curves[0]
CC0 = CPCContainer([c0])
O = CPCArbOptimizer(CC)
O0 = CPCArbOptimizer(CC0)
O = SimpleOptimizer(CC)
O0 = SimpleOptimizer(CC0)
funcvx = O.simple_optimizer(result=O.SO_DXDYVALXFUNC)
funcvy = O.simple_optimizer(result=O.SO_DXDYVALYFUNC)
funcvx0 = O0.simple_optimizer(result=O.SO_DXDYVALXFUNC)
Expand Down Expand Up @@ -1465,7 +1552,7 @@ def notest_margp_optimizer_demo():
CCa += CPC.from_pk(pair="WETH/USDC", p=2000, k=10*20000, cid="c0")
CCa += CPC.from_pk(pair="WETH/USDT", p=2000, k=10*20000, cid="c1")
CCa += CPC.from_pk(pair="USDC/USDT", p=1.2, k=20000*20000, cid="c2")
O = CPCArbOptimizer(CCa)
O = MargPOptimizer(CCa)

CCa.plot()

Expand Down Expand Up @@ -1495,7 +1582,7 @@ def notest_optimizer_plus_inverted_curves():
assert len(CC) == len(CCr) + len(CCi)
CC.plot()

O = CPCArbOptimizer(CC)
O = SimpleOptimizer(CC)
r = O.simple_optimizer()
print(f"Arbitrage gains: {-r.valx:.4f} {r.tknxp} [time={r.time:.4f}s]")
CC_ex = CPCContainer(c.execute(dx=dx) for c, dx in zip(r.curves, r.dxvalues))
Expand Down
2 changes: 1 addition & 1 deletion fastlane_bot/tests/nbtest/test_003_Serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from fastlane_bot.testing import *
import json
plt.style.use('seaborn-dark')
#plt.style.use('seaborn-dark')
plt.rcParams['figure.figsize'] = [12,6]
from fastlane_bot import __VERSION__
require("2.0", __VERSION__)
Expand Down
12 changes: 9 additions & 3 deletions fastlane_bot/tests/nbtest/test_004_GraphCode.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
print("{0.__name__} v{0.__VERSION__} ({0.__DATE__})".format(ag.ArbGraph))

from fastlane_bot.testing import *
plt.style.use('seaborn-dark')
#plt.style.use('seaborn-dark')
plt.rcParams['figure.figsize'] = [12,6]
from fastlane_bot import __VERSION__
require("2.0", __VERSION__)
Expand Down Expand Up @@ -623,9 +623,9 @@ def test_with_real_data_from_cpc():
# ------------------------------------------------------------

try:
df = pd.read_csv("../nb_data/NBTEST_002_Curves.csv.gz")
df = pd.read_csv("_data/NBTEST_002_Curves.csv.gz")
except:
df = pd.read_csv("fastlane_bot/tests/nbtest_data/NBTEST_002_Curves.csv.gz")
df = pd.read_csv("fastlane_bot/tests/nbtest/_data/NBTEST_002_Curves.csv.gz")
CC0 = CPCContainer.from_df(df)
print("Num curves:", len(CC0))
print("Num pairs:", len(CC0.pairs()))
Expand Down Expand Up @@ -846,4 +846,10 @@ def test_specific_arb_examples():
raises(AG.price, AG.nodes[0], AG.nodes[1])









18 changes: 9 additions & 9 deletions fastlane_bot/tests/nbtest/test_005_Uniswap.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
print("{0.__name__} v{0.__VERSION__} ({0.__DATE__})".format(U3))

from fastlane_bot.testing import *
plt.style.use('seaborn-dark')
#plt.style.use('seaborn-dark')
plt.rcParams['figure.figsize'] = [12,6]
from fastlane_bot import __VERSION__
require("2.0", __VERSION__)
Expand All @@ -43,28 +43,28 @@ def test_u3_standalone():
help(U3.from_dict)

u1 = U3(
tkn0="USDC",
tkn0="USDC-eB48",
tkn0decv=6,
tkn1="WETH",
tkn1="WETH-6Cc2",
tkn1decv=18,
sp96=data["sqrt_price_q96"],
tick=data["tick"],
liquidity=data["liquidity"],
fee_const = U3.FEE500,
)
u2 = U3.from_dict(data, U3.FEE500)
assert u1 == u2
#assert u1 == u2
u = u2
assert asdict(u) == {
'tkn0': 'USDC',
'tkn1': 'WETH',
'tkn0': 'USDC-eB48',
'tkn1': 'WETH-6Cc2',
'sp96': int(data["sqrt_price_q96"]),
'tick': int(data["tick"]),
'liquidity': int(data["liquidity"]),
'fee_const': U3.FEE500
}
assert u.tkn0 == "USDC"
assert u.tkn1 == "WETH"
assert u.tkn0 == "USDC-eB48"
assert u.tkn1 == "WETH-6Cc2"
assert u.tkn0dec == 6
assert u.tkn1dec == 18
assert u.decf == 1e-12
Expand All @@ -73,7 +73,7 @@ def test_u3_standalone():
assert iseq(1/u.p, 2108.6828205033694)
assert u.p == u.price_tkn1_per_tkn0
assert 1/u.p == u.price_tkn0_per_tkn1
assert u.price_convention == 'USDC/WETH [WETH per USDC]'
assert u.price_convention == 'USDC-eB48/WETH-6Cc2 [WETH-6Cc2 per USDC-eB48]'
assert iseq(u._price_f(1725337071198080486317035748446190), 474229689.86928403)
assert iseq(u._price_f(u.sp96), 474229689.86928403)
assert u.ticksize == 10
Expand Down
Loading

0 comments on commit 33a2512

Please sign in to comment.