gateio implemented

This commit is contained in:
Nicolás Sánchez 2025-01-01 21:12:39 -03:00
parent f311b1b96d
commit b89f70371e
13 changed files with 119 additions and 16 deletions

Binary file not shown.

View File

@ -19,7 +19,7 @@ class binance_earn:
account = self.client.account()
for item in account["balances"]:
if item["asset"]==coin:
return item["free"]
return {coin: item["free"]}
def get_available_products(self, coin):

View File

@ -4,49 +4,152 @@ 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):
api_key, api_secret = get_api_key("gateio")
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)
'''
return {"Error": "To be implemented"}
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):
return {"Error": "To be implemented"}
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):
return {"Error": "To be implemented"}
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):
return {"Error": "To be implemented"}
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 redeem_product(self, product_id, redeem_all=True, amount=0, destination_account="SPOT"):
return {"Error": "To be implemented"}
def get_position(self, **kwargs):
return {"Error": "To be implemented"}
def get_account(self, coin, recv_window=5000):
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"}

View File

@ -51,7 +51,7 @@ class kucoin_earn:
request = GetSpotAccountListReqBuilder().set_currency(coin).set_type("trade").build()
response = self.account_api().get_spot_account_list(request)
balance = response.to_dict()["data"][0]["available"]
return balance
return {coin: balance}
def get_available_products(self, coin):

View File

@ -26,16 +26,16 @@ class okx_earn:
balances = self.account_api.get_account_balance()
for item in balances["data"][0]["details"]:
if item["ccy"] == coin:
return item["availBal"]
return 0.0
return {coin: item["availBal"]}
return {"Error": "Coin not found"}
def get_funding_balance(self, coin):
balances = self.funding_api.get_balances()
for item in balances["data"]:
if item["ccy"] == coin:
return item["availBal"]
return 0.0
return {coin: item["availBal"]}
return {"Error": "Coin not found"}
def transfer_to_funding(self,coin,amount):

View File

@ -12,6 +12,6 @@ gateio = earn_gateio.gateio_earn()
if __name__=="__main__":
#Available products
print(gateio.get_product_detail("USDT"))
print(gateio.get_trading_balance("USDT"))