Skip to content

Commit

Permalink
Remove DutchX and Binance oracles
Browse files Browse the repository at this point in the history
  • Loading branch information
Uxio0 committed Dec 19, 2022
1 parent da238d7 commit 465198c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.1.3 on 2022-12-19 10:42

from django.db import migrations


def remove_price_oracles(apps, schema_editor):
PriceOracle = apps.get_model("tokens", "PriceOracle")
PriceOracle.objects.filter(name__in=["DutchX", "Binance"]).delete()


class Migration(migrations.Migration):

dependencies = [
("tokens", "0015_auto_20210610_1502"),
]

operations = [migrations.RunPython(remove_price_oracles)]
43 changes: 0 additions & 43 deletions safe_relay_service/tokens/price_oracles.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,47 +29,6 @@ def get_price(self, ticker) -> float:
pass


class Binance(PriceOracle):
"""
Get valid symbols from https://api.binance.com/api/v1/exchangeInfo
Remember to always use USDT instead of USD
"""

@cached(cache=TTLCache(maxsize=1024, ttl=60))
def get_price(self, ticker) -> float:
url = "https://api.binance.com/api/v3/avgPrice?symbol=" + ticker
response = requests.get(url)
api_json = response.json()
if not response.ok:
logger.warning("Cannot get price from url=%s", url)
raise CannotGetTokenPriceFromApi(api_json.get("msg"))
return float(api_json["price"])


class DutchX(PriceOracle):
def validate_ticker(self, ticker: str):
# Example ticker `0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359-WETH`
if "WETH" not in ticker:
raise InvalidTicker(ticker)

def reverse_ticker(self, ticker: str):
return "-".join(reversed(ticker.split("-")))

@cached(cache=TTLCache(maxsize=1024, ttl=1200))
def get_price(self, ticker: str) -> float:
self.validate_ticker(ticker)
url = (
"https://dutchx.d.exchange/api/v1/markets/{}/prices/custom-median?requireWhitelisted=false&"
"maximumTimePeriod=388800&numberOfAuctions=9".format(ticker)
)
response = requests.get(url)
api_json = response.json()
if not response.ok or api_json is None:
logger.warning("Cannot get price from url=%s", url)
raise CannotGetTokenPriceFromApi(api_json)
return float(api_json)


class Huobi(PriceOracle):
"""
Get valid symbols from https://api.huobi.pro/v1/common/symbols
Expand Down Expand Up @@ -141,8 +100,6 @@ def get_price(self, ticker: str) -> float:

def get_price_oracle(name: str, configuration: Dict[Any, Any] = {}) -> PriceOracle:
oracles = {
"binance": Binance,
"dutchx": DutchX,
"huobi": Huobi,
"kraken": Kraken,
"kyber": Kyber,
Expand Down
30 changes: 0 additions & 30 deletions safe_relay_service/tokens/tests/test_exchanges.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

from django.test import TestCase

import pytest

from ..price_oracles import (
Binance,
DutchX,
ExchangeApiException,
Huobi,
InvalidTicker,
Kraken,
PriceOracle,
get_price_oracle,
Expand All @@ -18,11 +13,8 @@

class TestExchanges(TestCase):
def test_get_price_oracle(self):
self.assertIsInstance(get_price_oracle("Binance"), Binance)
self.assertIsInstance(get_price_oracle("BINANCE"), Binance)
self.assertIsInstance(get_price_oracle("KRAKEN"), Kraken)
self.assertIsInstance(get_price_oracle("kraKen"), Kraken)
self.assertIsInstance(get_price_oracle("DutchX"), DutchX)
self.assertIsInstance(get_price_oracle("Huobi"), Huobi)
self.assertIsInstance(get_price_oracle("huobI"), Huobi)
with self.assertRaises(NotImplementedError):
Expand All @@ -40,28 +32,6 @@ def exchange_helper(
with self.assertRaises(ExchangeApiException):
exchange.get_price(ticker)

def test_binance(self):
exchange = Binance()
self.exchange_helper(exchange, ["BTCUSDT", "ETHUSDT"], ["BADTICKER"])

@pytest.mark.xfail
def test_dutchx(self):
exchange = DutchX()
# Dai address is 0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359
# Gno address is 0x6810e776880C02933D47DB1b9fc05908e5386b96
self.exchange_helper(
exchange,
[
"0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359-WETH", # DAI
"0x6810e776880C02933D47DB1b9fc05908e5386b96-WETH",
],
["WETH-0x11abca6b4ccb1b6faa2625fe562bdd9a23260359"],
)
with self.assertRaises(InvalidTicker):
exchange.get_price(
"0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359-0x11abca6b4ccb1b6faa2625fe562bdd9a23260359"
)

def test_huobi(self):
exchange = Huobi()
# Dai address is 0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359
Expand Down
12 changes: 6 additions & 6 deletions safe_relay_service/tokens/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
from web3 import Web3

from ..models import PriceOracle
from ..price_oracles import CannotGetTokenPriceFromApi, DutchX
from ..price_oracles import CannotGetTokenPriceFromApi, Kraken
from .factories import PriceOracleTickerFactory, TokenFactory


class TestModels(TestCase):
def test_price_oracles(self):
self.assertEqual(PriceOracle.objects.count(), 6)
self.assertEqual(PriceOracle.objects.count(), 4)

def test_token_calculate_payment(self):
token = TokenFactory(fixed_eth_conversion=0.1)
Expand Down Expand Up @@ -46,9 +46,9 @@ def test_token_calculate_payment(self):
token.calculate_payment(Web3.toWei(1, "ether")), Web3.toWei(0.1, "ether")
)

@mock.patch.object(DutchX, "get_price", return_value=3.8, autospec=True)
@mock.patch.object(Kraken, "get_price", return_value=3.8, autospec=True)
def test_token_eth_value(self, get_price_mock):
price_oracle = PriceOracle.objects.get(name="DutchX")
price_oracle = PriceOracle.objects.get(name="Kraken")
token = TokenFactory(fixed_eth_conversion=None)
with self.assertRaises(CannotGetTokenPriceFromApi):
token.get_eth_value()
Expand All @@ -71,9 +71,9 @@ def test_token_eth_value(self, get_price_mock):
with self.assertRaises(CannotGetTokenPriceFromApi):
token.get_eth_value()

@mock.patch.object(DutchX, "get_price", return_value=3.8, autospec=True)
@mock.patch.object(Kraken, "get_price", return_value=3.8, autospec=True)
def test_token_eth_value_inverted(self, get_price_mock):
price_oracle = PriceOracle.objects.get(name="DutchX")
price_oracle = PriceOracle.objects.get(name="Kraken")

token = TokenFactory(fixed_eth_conversion=None)
PriceOracleTickerFactory(
Expand Down

0 comments on commit 465198c

Please sign in to comment.