118 lines
6.1 KiB
Python
118 lines
6.1 KiB
Python
"""Offline tests: symbol parsing, coin identification by price, DEX pool choice, statuses, CSV."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
|
|
import meme_matcher as mm # noqa: E402
|
|
|
|
|
|
def coin(cg_id, symbol, price, platforms=None, mcap=1e6):
|
|
return mm.MemeCoin(cg_id, symbol, cg_id, price, mcap, platforms or {})
|
|
|
|
|
|
def ds_pair(chain, address, price, liquidity, symbol="X", pair_address=None, dex="uniswap"):
|
|
return {"chainId": chain, "dexId": dex, "pairAddress": pair_address or f"{address}-{liquidity}",
|
|
"priceUsd": str(price), "liquidity": {"usd": liquidity}, "volume": {"h24": liquidity / 10},
|
|
"baseToken": {"address": address, "symbol": symbol}, "quoteToken": {"symbol": "WETH"},
|
|
"url": f"https://dexscreener.com/{chain}/{address}"}
|
|
|
|
|
|
class SymbolTests(unittest.TestCase):
|
|
def test_size_prefixes_and_suffixes(self):
|
|
self.assertEqual(mm.split_base("1000PEPE"), ("PEPE", 1000.0))
|
|
self.assertEqual(mm.split_base("1000000BABYDOGE"), ("BABYDOGE", 1_000_000.0))
|
|
self.assertEqual(mm.split_base("1MBABYDOGE"), ("BABYDOGE", 1_000_000.0))
|
|
self.assertEqual(mm.split_base("SHIB1000"), ("SHIB", 1000.0))
|
|
self.assertEqual(mm.split_base("1INCH"), ("1INCH", 1.0))
|
|
self.assertEqual(mm.split_base("WIF"), ("WIF", 1.0))
|
|
|
|
def test_exchange_parsers_give_price_per_coin(self):
|
|
bybit = mm.parse_bybit({"result": {"list": [
|
|
{"symbol": "1000PEPEUSDT", "lastPrice": "0.004", "turnover24h": "100", "fundingRate": "0.0001"},
|
|
{"symbol": "BTCPERP", "lastPrice": "1", "turnover24h": "1"}]}})
|
|
self.assertEqual(len(bybit), 1)
|
|
self.assertAlmostEqual(bybit[0].price, 0.000004)
|
|
self.assertEqual((bybit[0].base, bybit[0].funding), ("PEPE", 0.0001))
|
|
okx = mm.parse_okx({"data": [{"instId": "WIF-USDT-SWAP", "last": "2", "volCcy24h": "50"}]})
|
|
self.assertEqual((okx[0].base, okx[0].volume_usd), ("WIF", 100.0))
|
|
gate = mm.parse_gate([{"contract": "BONK_USDT", "last": "0.00002", "volume_24h_quote": "7"}])
|
|
self.assertEqual(gate[0].base, "BONK")
|
|
binance = mm.parse_binance([{"symbol": "BTCUSDT_261225", "lastPrice": "1", "quoteVolume": "1"},
|
|
{"symbol": "DOGEUSDT", "lastPrice": "0.1", "quoteVolume": "5"}])
|
|
self.assertEqual([l.base for l in binance], ["DOGE"])
|
|
|
|
|
|
class MatchTests(unittest.TestCase):
|
|
def test_coin_is_picked_by_price_among_same_tickers(self):
|
|
by_symbol = {"PEPE": [coin("baby-pepe", "PEPE", 0.5), coin("pepe", "PEPE", 0.000004)]}
|
|
self.assertEqual(mm.pick_coin("PEPE", 0.0000041, by_symbol).cg_id, "pepe")
|
|
self.assertIsNone(mm.pick_coin("PEPE", 0.01, by_symbol)) # no coin at that price
|
|
self.assertIsNone(mm.pick_coin("DOGE", 0.1, by_symbol)) # not a meme coin
|
|
|
|
def test_two_contracts_with_one_ticker_are_two_coins(self):
|
|
listings = [mm.Listing("Binance", "CATUSDT", "CAT", 1, 0.02, 10),
|
|
mm.Listing("Binance", "1000CATUSDT", "CAT", 1000, 0.000005, 20)]
|
|
memes = [coin("cat-a", "CAT", 0.02), coin("simons-cat", "CAT", 0.000005)]
|
|
result = {m.cg_id: m for m in mm.match(listings, memes, {}, {})}
|
|
self.assertEqual(set(result), {"cat-a", "simons-cat"})
|
|
self.assertEqual(len(result["simons-cat"].cex), 1)
|
|
|
|
def test_deepest_agreeing_pool_wins_and_fakes_are_flagged(self):
|
|
address = "0xAbC"
|
|
memes = [coin("pepe", "PEPE", 0.000004, {"ethereum": address, "solana": "SoLpEpE"})]
|
|
pools = mm.all_pairs([
|
|
ds_pair("ethereum", address, 0.000004, 1_000_000),
|
|
ds_pair("ethereum", address, 0.0000041, 5_000_000),
|
|
ds_pair("solana", "SoLpEpE", 0.9, 50_000_000), # a fake: price way off, ignored
|
|
])
|
|
listing = mm.Listing("Bybit", "1000PEPEUSDT", "PEPE", 1000, 0.000004, 100)
|
|
m = mm.match([listing], memes, pools, {})[0]
|
|
self.assertEqual(m.dex.liquidity_usd, 5_000_000)
|
|
self.assertEqual(m.status, "совпадает")
|
|
self.assertEqual(m.dex_count, 1)
|
|
# only a fake pool: still shown, but flagged
|
|
fake_only = mm.all_pairs([ds_pair("solana", "SoLpEpE", 0.9, 1_000)])
|
|
m = mm.match([listing], memes, fake_only, {})[0]
|
|
self.assertEqual(m.status, "разные монеты?")
|
|
|
|
def test_pool_where_token_is_quote_is_ignored(self):
|
|
pair = ds_pair("ethereum", "0xother", 1.0, 1e6)
|
|
pair["quoteToken"] = {"address": "0xabc", "symbol": "PEPE"}
|
|
self.assertNotIn(("ethereum", "0xabc"), mm.all_pairs([pair]))
|
|
|
|
def test_hyperliquid_spot_and_status(self):
|
|
data = ({"tokens": [{"index": 0, "name": "USDC"}, {"index": 1, "name": "WIF", "tokenId": "0x1"}],
|
|
"universe": [{"tokens": [1, 0], "name": "@7", "index": 0}]},
|
|
[{"midPx": "1.01", "dayNtlVlm": "5000"}])
|
|
hl = mm.parse_hyperliquid_spot(data)
|
|
listing = mm.Listing("OKX", "WIF-USDT-SWAP", "WIF", 1, 1.0, 50)
|
|
m = mm.match([listing], [coin("dogwifcoin", "WIF", 1.0)], {}, hl)[0]
|
|
self.assertEqual((m.dex.source, m.status), ("HyperLiquid", "совпадает"))
|
|
self.assertEqual(mm.status_of(None), "нет на DEX")
|
|
self.assertEqual(mm.status_of(10.0), "проверить")
|
|
self.assertEqual(mm.status_of(-40.0), "разные монеты?")
|
|
|
|
def test_contracts_keep_case_for_solana_only(self):
|
|
out = mm.contracts_for([coin("x", "X", 1, {"ethereum": "0xAbC", "solana": "AbC", "unknown-chain": "q"})])
|
|
self.assertEqual(out, {"ethereum": {"0xabc"}, "solana": {"AbC"}})
|
|
|
|
def test_csv(self):
|
|
listing = mm.Listing("Bybit", "WIFUSDT", "WIF", 1, 1.0, 50, 0.0001)
|
|
m = mm.match([listing], [coin("dogwifcoin", "WIF", 1.0)], {}, {})[0]
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
path = Path(folder) / "m.csv"
|
|
mm.save_csv([m], path)
|
|
text = path.read_text(encoding="utf-8-sig")
|
|
self.assertIn("WIF;dogwifcoin", text)
|
|
self.assertIn("нет на DEX", text)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|