Skip to content

Commit

Permalink
Merge pull request #69 from fatchan/main
Browse files Browse the repository at this point in the history
Add kucoin rates module
  • Loading branch information
yukonet authored Oct 2, 2024
2 parents 47a1057 + 4b5b93a commit cc1d71b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
3 changes: 3 additions & 0 deletions shkeeper/modules/classes/rate_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
class RateSource(metaclass=ABCMeta):
instances = {}

USDT_CRYPTOS = {"USDT", "ETH-USDT", "BNB-USDT", "POLYGON-USDT", "AVALANCHE-USDT"}
USDC_CRYPTOS = {"ETH-USDC", "BNB-USDC", "POLYGON-USDC", "AVALANCHE-USDC"}

def __init_subclass__(cls, **kwargs):
super().__init_subclass__(**kwargs)
instance = cls()
Expand Down
11 changes: 3 additions & 8 deletions shkeeper/modules/rates/binance.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,15 @@ class Binance(RateSource):
name = "binance"

def get_rate(self, fiat, crypto):
if fiat == "USD" and crypto in {
"USDT",
"ETH-USDT",
"BNB-USDT",
"POLYGON-USDT",
"AVALANCHE-USDT",
}:
if fiat == "USD" and crypto in self.USDT_CRYPTOS:
return Decimal(1.0)

if crypto in {"ETH-USDC", "BNB-USDC", "POLYGON-USDC", "AVALANCHE-USDC"}:
if crypto in self.USDC_CRYPTOS:
crypto = "USDC"

if fiat == "USD":
fiat = "USDT"

url = f"https://api.binance.com/api/v3/ticker/price?symbol={crypto}{fiat}"
answer = requests.get(url)
if answer.status_code == requests.codes.ok:
Expand Down
30 changes: 30 additions & 0 deletions shkeeper/modules/rates/kucoin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import json
from decimal import Decimal

from shkeeper import requests

from shkeeper.modules.classes.rate_source import RateSource


class KuCoin(RateSource):
name = "kucoin"

def get_rate(self, fiat, crypto):
if crypto in self.USDT_CRYPTOS:
crypto = "USDT"

if crypto in self.USDC_CRYPTOS:
crypto = "USDC"

# https://www.kucoin.com/docs/beginners/introduction
url = f"https://api.kucoin.com/api/v1/prices?base={fiat}&currencies={crypto}"
answer = requests.get(url)
if answer.status_code == requests.codes.ok:
data = json.loads(answer.text)
if data.get("code") == "200000":
#data will be empty dict if symbol doesnt exist even though code is 200000
price = data.get("data", {}).get(crypto)
if price is not None:
return Decimal(price)

raise Exception(f"Can't get rate for {crypto} in {fiat}")

0 comments on commit cc1d71b

Please sign in to comment.