''' https://www.gate.io/docs/developers/apiv4/#earnuni ''' # Implement from scratch, gateio python connector is AWFUL. import time import hashlib import hmac import requests import json from credentials import get_api_key class gateio_earn: def __init__(self): self.api_key, self.api_secret = get_api_key("gateio") self.host = "https://api.gateio.ws" self.prefix = "/api/v4" def gen_sign(self, method, url, query_string=None, payload_string=None): ''' Generates a signature for the API request From Gate.io API docs ''' t = time.time() m = hashlib.sha512() m.update((payload_string or "").encode("utf-8")) hashed_payload = m.hexdigest() s = '%s\n%s\n%s\n%s\n%s' % (method, url, query_string or "", hashed_payload, t) sign = hmac.new(self.api_secret.encode('utf-8'), s.encode('utf-8'), hashlib.sha512).hexdigest() return {'KEY': self.api_key, 'Timestamp': str(t), 'SIGN': sign} def get_trading_balance(self, coin): ''' Returns the free available balance of a coin in the trading account (or the equivalent account in the exchange) ''' url = f"/spot/accounts" query_param = "" headers = {"Accept": "application/json", "Content-Type": "application/json"} sign_headers = self.gen_sign("GET", self.prefix+url, query_param) headers.update(sign_headers) response = requests.get(self.host+self.prefix+url, params=query_param, headers=headers) if response.status_code == 200: for item in response.json(): if item["currency"] == coin: return {coin: item["available"]} return {"Error": "Coin not found"} else: return {"Error": response.text} def get_available_products(self,coin): url = "/earn/uni/currencies" headers = {"Accept": "application/json", "Content-Type": "application/json"} query_param = "" sign_headers = self.gen_sign("GET", self.prefix+url, query_param) headers.update(sign_headers) response = requests.get(self.host+self.prefix+url, params=query_param, headers=sign_headers) if response.status_code == 200: return response.json() else: return {"Error": response.text} def get_product_detail(self,coin): url = f"/earn/uni/currencies/{coin}" headers = {"Accept": "application/json", "Content-Type": "application/json"} sign_headers = self.gen_sign("GET", self.prefix+url) headers.update(sign_headers) response = requests.get(self.host+self.prefix+url, headers=sign_headers) if response.status_code == 200: return response.json() else: return {"Error": response.text} def subscribe_product(self, coin, amount, rate): url = "/earn/uni/lends" headers = {"Accept": "application/json", "Content-Type": "application/json"} query_params = "" body = {"currency": coin, "amount": str(amount), "min_rate": rate, "type": "lend"} sign_headers = self.gen_sign("POST", self.prefix+url, query_params, body) headers.update(sign_headers) response = requests.post(self.host+self.prefix+url, headers=sign_headers, data=body) if response.status_code == 200: return response.json() else: return {"Error": response.text} def redeem_product(self, coin, amount, rate): url = "/earn/uni/lends" headers = {"Accept": "application/json", "Content-Type": "application/json"} query_params = "" body = {"currency": coin, "amount": str(amount), "min_rate": rate, "type": "redeem"} sign_headers = self.gen_sign("POST", self.prefix+url, query_params, body) headers.update(sign_headers) response = requests.post(self.host+self.prefix+url, headers=sign_headers, data=body) if response.status_code == 200: return response.json() else: return {"Error": response.text} def get_position(self, **kwargs): return {"Error": "To be implemented"} def get_account(self): url = f"/earn/uni/lends" headers = {"Accept": "application/json", "Content-Type": "application/json"} sign_headers = self.gen_sign("GET", self.prefix+url) headers.update(sign_headers) response = requests.get(self.host+self.prefix+url, headers=sign_headers) if response.status_code == 200: return response.json() else: return {"Error": response.text} def amend_rate(self,coin,min_rate): url = f"/earn/uni/lends" body = {"currency": coin, "min_rate": min_rate} headers = {"Accept": "application/json", "Content-Type": "application/json"} sign_headers = self.gen_sign("PATCH", self.prefix+url, body) headers.update(sign_headers) response = requests.patch(self.host+self.prefix+url, headers=headers, data=body) if response.status_code == 200: return response.json() else: return {"Error": response.text} def get_personal_left_quota(self, product_id, **kwargs): return {"Error": "To be implemented"} def get_subscription_record(self, **kwargs): return {"Error": "To be implemented"} def get_redemption_record(self, **kwargs): return {"Error": "To be implemented"} def get_rewards_history(self, type="ALL", **kwargs): return {"Error": "To be implemented"} def get_subscription_preview(self, product_id, amount, **kwargs): return {"Error": "To be implemented"}