Fix public ad related LBTC calls and tests

This commit is contained in:
Mark Veidemanis 2022-04-23 15:02:29 +01:00
parent ad21f6fb67
commit 69e44b9fdb
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
4 changed files with 49 additions and 29 deletions

View File

@ -51,6 +51,8 @@ class Markets(util.Base):
assets = self.get_all_assets(platform) assets = self.get_all_assets(platform)
currencies = self.get_all_currencies(platform) currencies = self.get_all_currencies(platform)
providers = self.get_all_providers(platform) providers = self.get_all_providers(platform)
if platform == "lbtc":
providers = [self.sources.lbtc.map_provider(x, reverse=True) for x in providers]
sinks_currencies = self.sinks.currencies sinks_currencies = self.sinks.currencies
supported_currencies = [currency for currency in currencies if currency in sinks_currencies] supported_currencies = [currency for currency in currencies if currency in sinks_currencies]
currencies = supported_currencies currencies = supported_currencies
@ -69,10 +71,8 @@ class Markets(util.Base):
# Filter asset # Filter asset
public_ads_filtered = [ad for ad in public_ads_currency if ad[4] == asset] public_ads_filtered = [ad for ad in public_ads_currency if ad[4] == asset]
# Filter provider # Filter provider
public_ads_filtered = [ad for ad in public_ads_filtered if ad[3] == provider] public_ads_filtered = [ad for ad in public_ads_filtered if ad[3] == provider]
our_ads = [ad for ad in public_ads_filtered if ad[1] == username] our_ads = [ad for ad in public_ads_filtered if ad[1] == username]
if not our_ads: if not our_ads:
self.log.warning(f"No ads found in public listing for {asset} {currency} {provider}") self.log.warning(f"No ads found in public listing for {asset} {currency} {provider}")

View File

@ -250,8 +250,14 @@ class LBTC(util.Base):
ads_total.append([ad[0], ad[1], ad[2], ad[3], ad[4]]) ads_total.append([ad[0], ad[1], ad[2], ad[3], ad[4]])
return ads_total return ads_total
def map_provider(self, provider): def map_provider(self, provider, reverse=False):
provider_map = {"NATIONAL_BANK": "national-bank-transfer"} provider_map = {"NATIONAL_BANK": "national-bank-transfer"}
if reverse:
try:
return next(key for key, value in provider_map.items() if value == provider)
except StopIteration:
return False
else:
try: try:
return provider_map[provider] return provider_map[provider]
except KeyError: except KeyError:
@ -282,26 +288,19 @@ class LBTC(util.Base):
# f.write(json.dumps([page, currency, asset, ads]) + "\n") # f.write(json.dumps([page, currency, asset, ads]) + "\n")
# f.close() # f.close()
if ads is None: if ads is None:
print("ASDS IS NONE")
return False return False
if ads is False: if ads is False:
print("ADS IS FALSE")
return False return False
if ads["response"] is None: if ads["response"] is None:
print("NO RESPONSE")
return False return False
if "data" not in ads["response"]: if "data" not in ads["response"]:
print("NO DATAS")
return False return False
for ad in ads["response"]["data"]["ad_list"]: for ad in ads["response"]["data"]["ad_list"]:
if self.map_provider(ad["data"]["online_provider"]) not in providers: if self.map_provider(ad["data"]["online_provider"]) not in providers:
print(ad["data"]["online_provider"], "is not in", providers)
print(ad["data"]["online_provider"] in providers)
continue continue
date_last_seen = ad["data"]["profile"]["last_online"] date_last_seen = ad["data"]["profile"]["last_online"]
# Check if this person was seen recently # Check if this person was seen recently
if not util.last_online_recent(date_last_seen): if not util.last_online_recent(date_last_seen):
print("NOT SEEN RECENTLTY")
continue continue
ad_id = str(ad["data"]["ad_id"]) ad_id = str(ad["data"]["ad_id"])
username = ad["data"]["profile"]["username"] username = ad["data"]["profile"]["username"]
@ -348,16 +347,13 @@ class LBTC(util.Base):
@util.handle_exceptions @util.handle_exceptions
def update_prices(self, assets=None): def update_prices(self, assets=None):
print("UPDATE_PRICES", assets)
# Get all public ads for the given assets # Get all public ads for the given assets
public_ads = self.get_all_public_ads(assets) public_ads = self.get_all_public_ads(assets)
if not public_ads: if not public_ads:
print("NOT PUBLIC ADS")
return False return False
# Get the ads to update # Get the ads to update
to_update = self.markets.get_new_ad_equations(self.platform, public_ads, assets) to_update = self.markets.get_new_ad_equations(self.platform, public_ads, assets)
print("TO UPdATE", to_update)
self.slow_ad_update(to_update) self.slow_ad_update(to_update)
# TODO: make generic and move to markets # TODO: make generic and move to markets
@ -368,7 +364,6 @@ class LBTC(util.Base):
:return: dict of public ads keyed by currency :return: dict of public ads keyed by currency
:rtype: dict :rtype: dict
""" """
print("GET ALL PUBLIC ADS")
public_ads = {} public_ads = {}
crypto_map = { crypto_map = {
"BTC": "bitcoin", "BTC": "bitcoin",
@ -394,14 +389,11 @@ class LBTC(util.Base):
except KeyError: except KeyError:
# self.log.error("Error getting public ads for currency {currency}", currency=currency) # self.log.error("Error getting public ads for currency {currency}", currency=currency)
continue continue
print("ABOUT TO RUN ENUM PUBLIC ADS WITH ", asset, currency, providers)
ads_list = self.enum_public_ads(asset, currency, providers) ads_list = self.enum_public_ads(asset, currency, providers)
print("ADS LIST", ads_list)
if not ads_list: if not ads_list:
continue continue
ads = self.money.lookup_rates(self.platform, ads_list, rates=rates) ads = self.money.lookup_rates(self.platform, ads_list, rates=rates)
if not ads: if not ads:
print("NOT ADS CONTINUE")
continue continue
self.write_to_es_ads("ads", ads) self.write_to_es_ads("ads", ads)
if currency in public_ads: if currency in public_ads:
@ -410,7 +402,6 @@ class LBTC(util.Base):
public_ads[currency].append(ad) public_ads[currency].append(ad)
else: else:
public_ads[currency] = ads public_ads[currency] = ads
print("RETURNING PUBLIC ADS", public_ads)
return public_ads return public_ads
def write_to_es_ads(self, msgtype, ads): def write_to_es_ads(self, msgtype, ads):

View File

@ -1,3 +1,6 @@
expected_to_update_lbtc = [["1452045", "btc_in_usd*1.1", "BTC", "GBP", False]]
expected_to_update = [ expected_to_update = [
[ [
"2caa4afa-a1c7-4683-aa4a-faa1c7a683dc", "2caa4afa-a1c7-4683-aa4a-faa1c7a683dc",
@ -224,6 +227,29 @@ cg_prices = {
"sek": 1620.26, "sek": 1620.26,
}, },
} }
fake_public_ads_lbtc = {
"GBP": [
["1452045", "Harrey", "30800.00", "NATIONAL_BANK", "BTC", "GBP", 0.95],
["1359267", "Angelchira", "32201.41", "NATIONAL_BANK", "BTC", "GBP", 1.0],
["1434612", "Bittylicious.com-James", "32308.88", "NATIONAL_BANK", "BTC", "GBP", 1.0],
["1451964", "tutsidelopez", "32308.88", "NATIONAL_BANK", "BTC", "GBP", 1.0],
["1354377", "DeltaTip", "32328.19", "NATIONAL_BANK", "BTC", "GBP", 1.0],
["1330431", "starclicks_co", "33603.21", "NATIONAL_BANK", "BTC", "GBP", 1.04],
["1431277", "bitotal", "34712.75", "NATIONAL_BANK", "BTC", "GBP", 1.08],
["1382819", "Bitcoinmarketprice", "34871.26", "NATIONAL_BANK", "BTC", "GBP", 1.08],
["1068826", "JDRbitcoins", "36454.75", "NATIONAL_BANK", "BTC", "GBP", 1.13],
["1170564", "mommabear007", "36456.32", "NATIONAL_BANK", "BTC", "GBP", 1.13],
["1331232", "ParadoxHD", "36765.39", "NATIONAL_BANK", "BTC", "GBP", 1.14],
["1445019", "demoric102", "37090.34", "NATIONAL_BANK", "BTC", "GBP", 1.15],
["1379791", "eenitan161", "37850.00", "NATIONAL_BANK", "BTC", "GBP", 1.17],
["1435762", "btcladybd", "41202.59", "NATIONAL_BANK", "BTC", "GBP", 1.28],
["1445674", "CarloMazzillo", "41202.59", "NATIONAL_BANK", "BTC", "GBP", 1.28],
["1428769", "miguelnicotra", "44807.39", "NATIONAL_BANK", "BTC", "GBP", 1.39],
["326809", "Ceres", "50710.88", "NATIONAL_BANK", "BTC", "GBP", 1.57],
]
}
fake_public_ads = { fake_public_ads = {
"AUD": [ "AUD": [
[ [

View File

@ -3,12 +3,13 @@ from unittest.mock import MagicMock, patch
from json import loads from json import loads
from copy import deepcopy from copy import deepcopy
from tests.common import fake_public_ads, cg_prices, expected_to_update from tests.common import fake_public_ads_lbtc, cg_prices, expected_to_update_lbtc
from sources.localbitcoins import LBTC from sources.localbitcoins import LBTC
from markets import Markets from markets import Markets
from money import Money from money import Money
import util import util
import settings import settings
import sources
class TestLBTC(TestCase): class TestLBTC(TestCase):
@ -25,6 +26,8 @@ class TestLBTC(TestCase):
self.markets = Markets() self.markets = Markets()
self.lbtc = LBTC() self.lbtc = LBTC()
self.money = Money() self.money = Money()
self.sources = sources.Sources()
setattr(self.markets, "sources", self.sources)
setattr(self.lbtc, "markets", self.markets) setattr(self.lbtc, "markets", self.markets)
setattr(self.money, "markets", self.markets) setattr(self.money, "markets", self.markets)
setattr(self.lbtc, "money", self.money) setattr(self.lbtc, "money", self.money)
@ -63,7 +66,9 @@ class TestLBTC(TestCase):
self.lbtc.markets.get_all_providers.return_value = self.all_providers self.lbtc.markets.get_all_providers.return_value = self.all_providers
public_ads = self.lbtc.get_all_public_ads() public_ads = self.lbtc.get_all_public_ads()
self.assertDictEqual(public_ads, fake_public_ads) print("public_ads", public_ads)
# print("fake_public_ads", fake_public_ads)
self.assertDictEqual(public_ads, fake_public_ads_lbtc)
for currency, ads in public_ads.items(): for currency, ads in public_ads.items():
ad_ids = [ad[0] for ad in ads] ad_ids = [ad[0] for ad in ads]
@ -91,7 +96,7 @@ class TestLBTC(TestCase):
def test_update_prices(self): def test_update_prices(self):
settings.settings.LocalBitcoins.Username = "Harrey" settings.settings.LocalBitcoins.Username = "Harrey"
# Override the providers # Override the providers
settings.settings.LocalBitcoins.ProviderList = '["REVOLUT", "NATIONAL_BANK"]' settings.settings.LocalBitcoins.ProviderList = '["national-bank-transfer"]'
# Override enum_public_ads # Override enum_public_ads
self.lbtc.lbtc._api_call = self.mock_enum_public_ads_api_call self.lbtc.lbtc._api_call = self.mock_enum_public_ads_api_call
@ -104,10 +109,8 @@ class TestLBTC(TestCase):
self.lbtc.slow_ad_update = MagicMock() self.lbtc.slow_ad_update = MagicMock()
self.lbtc.update_prices() self.lbtc.update_prices()
print("CALL ARGS", self.lbtc.slow_ad_update.call_args_list)
call_args = self.lbtc.slow_ad_update.call_args_list[0][0][0] call_args = self.lbtc.slow_ad_update.call_args_list[0][0][0]
print("EXPECTED", expected_to_update) self.assertCountEqual(call_args, expected_to_update_lbtc)
self.assertCountEqual(call_args, expected_to_update)
def test_enum_public_ads(self): def test_enum_public_ads(self):
# Override enum_public_ads # Override enum_public_ads
@ -169,7 +172,7 @@ class TestLBTC(TestCase):
expected_return = [] expected_return = []
# Let's manually calculate what it's supposed to look like # Let's manually calculate what it's supposed to look like
price_btc = cg_prices["bitcoin"]["usd"] price_btc = cg_prices["bitcoin"]["gbp"]
for ad in deepcopy(enum_ads_return): for ad in deepcopy(enum_ads_return):
price = float(ad[2]) price = float(ad[2])
margin = round(price / price_btc, 2) margin = round(price / price_btc, 2)