unified get_position
This commit is contained in:
parent
d3b7ccf72f
commit
26d8195fe0
|
|
@ -12,6 +12,10 @@ class binance_earn:
|
|||
self.client = Client(self.api_key, self.api_secret)
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return "binance"
|
||||
|
||||
|
||||
def get_trading_balance(self, coin):
|
||||
'''
|
||||
Args:
|
||||
|
|
@ -91,7 +95,16 @@ class binance_earn:
|
|||
|
||||
try:
|
||||
response = self.client.get_flexible_product_position(asset=coin, **kwargs)
|
||||
return response
|
||||
if response["total"]==0:
|
||||
return {}
|
||||
elif response["total"]==1:
|
||||
return {"asset": response["rows"][0]["asset"],
|
||||
"positionId": "",
|
||||
"productId": response["rows"][0]["productId"],
|
||||
"amount": response["rows"][0]["totalAmount"],
|
||||
"rate": response["rows"][0]["latestAnnualPercentageRate"]}
|
||||
else:
|
||||
return {"Error": response}
|
||||
except ClientError as error:
|
||||
print("Found error. status: {}, error code: {}, error message: {}".format(error.status_code, error.error_code, error.error_message))
|
||||
return None
|
||||
|
|
|
|||
|
|
@ -19,6 +19,10 @@ class gateio_earn:
|
|||
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
|
||||
|
|
@ -116,7 +120,9 @@ class gateio_earn:
|
|||
if response.status_code == 200:
|
||||
return response.json()
|
||||
else:
|
||||
return {"Error": response.text}
|
||||
return {"Error code": response.status_code,
|
||||
"Error content": response.text,
|
||||
"Error content": response.content}
|
||||
|
||||
|
||||
def redeem_product(self, coin, amount, rate="0"):
|
||||
|
|
@ -133,8 +139,27 @@ class gateio_earn:
|
|||
return {"Error": response.text}
|
||||
|
||||
|
||||
def get_position(self, **kwargs):
|
||||
return {"Error": "To be implemented"}
|
||||
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):
|
||||
|
|
|
|||
|
|
@ -44,6 +44,10 @@ class kucoin_earn:
|
|||
self.earn_api = kucoin_rest_service.get_earn_service().get_earn_api
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return "kucoin"
|
||||
|
||||
|
||||
def get_trading_balance(self, coin):
|
||||
'''
|
||||
Args:
|
||||
|
|
@ -107,13 +111,22 @@ class kucoin_earn:
|
|||
return response.to_dict()
|
||||
|
||||
|
||||
def get_position(self, coin, **kwargs):
|
||||
def get_position(self, coin):
|
||||
'''
|
||||
Return {'common_response': {'code': '200000', 'data': {'totalNum': 1, 'items': [{'orderId': '2987632', 'productId': '2152', 'productCategory': 'DEMAND', 'productType': 'DEMAND', 'currency': 'USDT', 'incomeCurrency': 'USDT', 'returnRate': '0.04767484', 'holdAmount': '20', 'redeemedAmount': '0', 'redeemingAmount': '0', 'lockStartTime': 1641806718000, 'lockEndTime': None, 'purchaseTime': 1736027283000, 'redeemPeriod': 0, 'status': 'LOCKED', 'earlyRedeemSupported': 0}], 'currentPage': 1, 'pageSize': 15, 'totalPage': 1}, 'rate_limit': {'limit': 2000, 'remaining': 1995, 'reset': 16550}}, 'totalNum': 1, 'items': [{'orderId': '2987632', 'productId': '2152', 'productCategory': 'DEMAND', 'productType': 'DEMAND', 'currency': 'USDT', 'incomeCurrency': 'USDT', 'returnRate': '0.04767484', 'holdAmount': '20', 'redeemedAmount': '0', 'redeemingAmount': '0', 'lockStartTime': 1641806718000, 'purchaseTime': 1736027283000, 'redeemPeriod': 0, 'status': <StatusEnum.LOCKED: 'LOCKED'>, 'earlyRedeemSupported': <EarlyRedeemSupportedEnum.T_0: 0>}], 'currentPage': 1, 'pageSize': 15, 'totalPage': 1}
|
||||
'''
|
||||
request = GetAccountHoldingReqBuilder().set_currency(coin).build()
|
||||
response = self.earn_api().get_account_holding(request)
|
||||
return response.to_dict()
|
||||
response = self.earn_api().get_account_holding(request).to_dict()
|
||||
if len(response["items"])==0:
|
||||
return {}
|
||||
elif len(response["items"])==1:
|
||||
return {"asset": response["items"][0]["currency"],
|
||||
"positionId": response["items"][0]["orderId"],
|
||||
"productId": response["items"][0]["productId"],
|
||||
"amount": response["items"][0]["holdAmount"],
|
||||
"rate": response["items"][0]["returnRate"]}
|
||||
else:
|
||||
return {"Error": response}
|
||||
|
||||
|
||||
def get_account(self,coin,**kwargs):
|
||||
|
|
|
|||
|
|
@ -19,6 +19,10 @@ class okx_earn:
|
|||
self.account_api = Account.AccountAPI(api_key, api_secret, passphrase, False, "0")
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return "okx"
|
||||
|
||||
|
||||
def get_trading_balance(self, coin):
|
||||
'''
|
||||
Args:
|
||||
|
|
@ -177,8 +181,18 @@ class okx_earn:
|
|||
return str(rate["data"][0]["avgRate"])
|
||||
|
||||
|
||||
def get_position(self, coin, **kwargs):
|
||||
return self.earning_api.get_saving_balance(coin)
|
||||
def get_position(self, coin):
|
||||
response = self.earning_api.get_saving_balance(coin)
|
||||
if len(response["data"])==0:
|
||||
return {}
|
||||
elif len(response["data"])==1:
|
||||
return {"asset": response["data"][0]["ccy"],
|
||||
"positionId": "",
|
||||
"productId": "",
|
||||
"amount": response["data"][0]["amt"],
|
||||
"rate": response["data"][0]["rate"]}
|
||||
else:
|
||||
return {"Error": response}
|
||||
|
||||
|
||||
def get_account(self, coin, recv_window=5000):
|
||||
|
|
|
|||
|
|
@ -11,6 +11,16 @@ okx = earn_okx.okx_earn()
|
|||
gateio = earn_gateio.gateio_earn()
|
||||
|
||||
if __name__=="__main__":
|
||||
'''
|
||||
DONE:
|
||||
1. Unified get_position
|
||||
2. Unified get_available_products
|
||||
|
||||
TODO:
|
||||
1. subscription and redeem returns unification
|
||||
2. Gate.io returns 204 code if subscription or redemption were successful (What were they thinking?)
|
||||
'''
|
||||
|
||||
|
||||
'''
|
||||
Subscribe workflow in Binance:
|
||||
|
|
@ -23,7 +33,7 @@ if __name__=="__main__":
|
|||
#print(binance.get_available_products("USDT"))
|
||||
#print(binance.subscribe_product("USDT001", "10", auto_subscribe=False))
|
||||
#print(binance.get_position("USDT"))
|
||||
#print(binance.redeem_product("USDT001", amount="5.00000746"))
|
||||
#print(binance.redeem_product("USDT001", amount="10.00003989"))
|
||||
|
||||
|
||||
|
||||
|
|
@ -39,7 +49,7 @@ if __name__=="__main__":
|
|||
#print(kucoin.get_available_products("USDT"))
|
||||
#print(kucoin.subscribe_product("2152", "10"))
|
||||
#print(kucoin.get_position("USDT"))
|
||||
#print(kucoin.redeem_product(order_id="2987632", amount="20"))
|
||||
#print(kucoin.redeem_product(order_id="2987632", amount="10"))
|
||||
|
||||
|
||||
'''
|
||||
|
|
@ -58,9 +68,10 @@ if __name__=="__main__":
|
|||
#print(okx.transfer_to_funding("USDT","10"))
|
||||
#print(okx.get_transfer_state("1064007151"))
|
||||
#print(okx.subscribe_product("USDT", "10"))
|
||||
#print(okx.redeem_product("USDT", "20"))
|
||||
#print(okx.transfer_to_trading("USDT", "20"))
|
||||
#print(okx.get_transfer_state("1064008141"))
|
||||
#print(okx.get_position("USDT"))
|
||||
#print(okx.redeem_product("USDT", "10"))
|
||||
#print(okx.transfer_to_trading("USDT", "10"))
|
||||
#print(okx.get_transfer_state("1064541068"))
|
||||
|
||||
|
||||
'''
|
||||
|
|
@ -75,8 +86,10 @@ if __name__=="__main__":
|
|||
'''
|
||||
|
||||
#print(gateio.get_min_rate("USDT"))
|
||||
#print(gateio.redeem_product("USDT", "10", "0.00000452"))
|
||||
#print(gateio.get_account())
|
||||
#print(gateio.subscribe_product("USDT", "10", "0.00000452"))
|
||||
#print(gateio.get_position("USDT"))
|
||||
#print(gateio.redeem_product("USDT", "10"))
|
||||
|
||||
pass
|
||||
|
||||
|
||||
Loading…
Reference in New Issue