215 lines
8.2 KiB
Python
215 lines
8.2 KiB
Python
'''
|
|
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 __str__(self):
|
|
return "gateio"
|
|
|
|
|
|
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):
|
|
'''
|
|
Args:
|
|
coin (str): The coin to get the balance of
|
|
Returns:
|
|
dict: The free available balance of the 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):
|
|
'''
|
|
response: {'coin': 'USDT', 'product_id': 'USDT', 'rate': '0.00057', 'min_amount': '1', 'isActive': True}
|
|
'''
|
|
url = f"/earn/uni/currencies/{coin}"
|
|
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=headers)
|
|
if response.status_code == 200:
|
|
return {"coin": response.json()["currency"],
|
|
"product_id": response.json()["currency"],
|
|
"rate": response.json()["max_rate"],
|
|
"min_amount": response.json()["min_lend_amount"],
|
|
"isActive": True}
|
|
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=headers)
|
|
if response.status_code == 200:
|
|
return {"coin": response.json()["currency"],
|
|
"product_id": response.json()["currency"],
|
|
"rate": response.json()["max_rate"],
|
|
"min_amount": response.json()["min_lend_amount"],
|
|
"isActive": True}
|
|
else:
|
|
return {"Error": response.text}
|
|
|
|
|
|
def get_min_rate(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=headers)
|
|
if response.status_code == 200:
|
|
return {"min_rate": response.json()["min_rate"]}
|
|
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 = f'{{"currency": "{coin}", "amount": {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=headers, data=body)
|
|
if response.status_code == 204:
|
|
return {"Success": "",
|
|
"orderId": "",
|
|
"txId": "",
|
|
"amount": amount
|
|
}
|
|
else:
|
|
return {"Error code": response.status_code,
|
|
"Error content": response.text,
|
|
"Error content": response.content}
|
|
|
|
|
|
def redeem_product(self, coin, amount, rate="0"):
|
|
url = "/earn/uni/lends"
|
|
headers = {"Accept": "application/json", "Content-Type": "application/json"}
|
|
query_params = ""
|
|
body = f'{{"currency": "{coin}", "amount": {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=headers, data=body)
|
|
if response.status_code == 204:
|
|
return {"Success": "",
|
|
"orderId": "",
|
|
"txId": "",
|
|
"amount": amount
|
|
}
|
|
else:
|
|
return {"Error": response.text}
|
|
|
|
|
|
def get_position(self, coin):
|
|
url = "/earn/uni/lends"
|
|
headers = {"Accept": "application/json", "Content-Type": "application/json"}
|
|
query_param = f'{{"currency": "{coin}"}}'
|
|
sign_headers = self.gen_sign("GET", self.prefix+url, query_param)
|
|
headers.update(sign_headers)
|
|
response = requests.get(self.host+self.prefix+url, headers=headers, params=query_param)
|
|
json_response = response.json()
|
|
if response.status_code == 200:
|
|
if len(json_response)==0:
|
|
return {}
|
|
elif len(json_response)==1:
|
|
return {"asset": json_response[0]["currency"],
|
|
"positionId": "",
|
|
"productId": "",
|
|
"amount": json_response[0]["amount"],
|
|
"rate": json_response[0]["min_rate"]}
|
|
else:
|
|
return {"Error": "Multiple positions for the same coin"}
|
|
else:
|
|
return {"Error": response.text}
|
|
|
|
|
|
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=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 = str({"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"} |