From 00cab673fb888b4282bdebe5d764b7c19ab23756 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20S=C3=A1nchez?= Date: Sun, 5 Jan 2025 13:44:30 -0300 Subject: [PATCH] subscribe/redeem unified --- libraries/wrappers/earn_binance.py | 18 ++++++++++++++++-- libraries/wrappers/earn_gateio.py | 16 ++++++++++++---- libraries/wrappers/earn_kucoin.py | 24 ++++++++++++++++++++---- libraries/wrappers/earn_okx.py | 20 ++++++++++++++++++-- test.py => main.py | 16 +++------------- 5 files changed, 69 insertions(+), 25 deletions(-) rename test.py => main.py (83%) diff --git a/libraries/wrappers/earn_binance.py b/libraries/wrappers/earn_binance.py index 327e9f4..aca9fb3 100644 --- a/libraries/wrappers/earn_binance.py +++ b/libraries/wrappers/earn_binance.py @@ -59,7 +59,14 @@ class binance_earn: ''' try: response = self.client.subscribe_flexible_product(productId=product_id, amount=amount, autoSubscribe=auto_subscribe, sourceAccount=source_account, recvWindow=5000) - return response + if response["success"]: + return {"Success": "", + "orderId": response["purchaseId"], + "txId": "", + "amount": response["amount"] + } + 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 @@ -76,7 +83,14 @@ class binance_earn: ''' try: response = self.client.redeem_flexible_product(productId=product_id, redeemAll=redeem_all, amount=amount, destAccount=destination_account, recvWindow=5000) - return response + if response["success"]: + return {"Success": "", + "orderId": response["redeemId"], + "txId": "", + "amount": amount + } + 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 diff --git a/libraries/wrappers/earn_gateio.py b/libraries/wrappers/earn_gateio.py index ab9f505..614fb8c 100644 --- a/libraries/wrappers/earn_gateio.py +++ b/libraries/wrappers/earn_gateio.py @@ -117,8 +117,12 @@ class gateio_earn: 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 == 200: - return response.json() + if response.status_code == 204: + return {"Success": "", + "orderId": "", + "txId": "", + "amount": amount + } else: return {"Error code": response.status_code, "Error content": response.text, @@ -133,8 +137,12 @@ class gateio_earn: 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 == 200: - return response.json() + if response.status_code == 204: + return {"Success": "", + "orderId": "", + "txId": "", + "amount": amount + } else: return {"Error": response.text} diff --git a/libraries/wrappers/earn_kucoin.py b/libraries/wrappers/earn_kucoin.py index af1e9b7..f2c466f 100644 --- a/libraries/wrappers/earn_kucoin.py +++ b/libraries/wrappers/earn_kucoin.py @@ -87,8 +87,16 @@ class kucoin_earn: return {"Error": "Invalid source_account. Values should be TRADE or MAIN for kucoin, SPOT, FUND or ALL for binance"} request = PurchaseReqBuilder().set_product_id(product_id).set_amount(str(amount)).set_account_type(source).build() - response = self.earn_api().purchase(request) - return response.to_dict() + response = self.earn_api().purchase(request).to_dict() + response_dict = response["common_response"]["data"] + if response_dict!={}: + return {"Success": "", + "orderId": response["orderId"], + "txId": response["orderTxId"], + "amount": amount + } + else: + return {"Error": response} def redeem_product(self, order_id, amount="0", source_account="SPOT"): @@ -107,8 +115,16 @@ class kucoin_earn: return {"Error": "Invalid source_account. Values should be TRADE or MAIN for kucoin, SPOT, FUND or ALL for binance"} request = RedeemReqBuilder().set_order_id(order_id).set_amount(str(amount)).set_from_account_type(source).build() - response = self.earn_api().redeem(request) - return response.to_dict() + response = self.earn_api().redeem(request).to_dict() + response_dict = response["common_response"]["data"] + if response_dict["status"]=="SUCCESS": + return {"Success": "", + "orderId": "", + "txId": response["orderTxId"], + "amount": response["amount"] + } + else: + return {"Error": response} def get_position(self, coin): diff --git a/libraries/wrappers/earn_okx.py b/libraries/wrappers/earn_okx.py index ad4ea40..38d917b 100644 --- a/libraries/wrappers/earn_okx.py +++ b/libraries/wrappers/earn_okx.py @@ -156,7 +156,15 @@ class okx_earn: ''' if rate is None: rate = self.get_avg_rate(coin) - return self.earning_api.savings_purchase_redemption(coin, str(amount), "purchase", str(rate)) + response = self.earning_api.savings_purchase_redemption(coin, str(amount), "purchase", str(rate)) + if response["data"]!=[]: + return {"Success": "", + "orderId": "", + "txId": "", + "amount": response["data"][0]["amt"] + } + else: + return {"Error": response} def redeem_product(self, coin, amount): @@ -166,7 +174,15 @@ class okx_earn: response: {'code': '0', 'data': [{'amt': '20', 'ccy': 'USDT', 'rate': '', 'side': 'redempt'}], 'msg': ''} ''' - return self.earning_api.savings_purchase_redemption(coin, str(amount), "redempt", "0") + response = self.earning_api.savings_purchase_redemption(coin, str(amount), "redempt", "0") + if response["data"]!=[]: + return {"Success": "", + "orderId": "", + "txId": "", + "amount": response["data"][0]["amt"] + } + else: + return {"Error": response} def set_rate(self, coin, rate): diff --git a/test.py b/main.py similarity index 83% rename from test.py rename to main.py index 4aaa0fc..95c7030 100644 --- a/test.py +++ b/main.py @@ -11,16 +11,6 @@ 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: @@ -33,7 +23,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="10.00003989")) + #print(binance.redeem_product("USDT001", amount="10")) @@ -66,12 +56,12 @@ if __name__=="__main__": ''' #print(okx.get_available_products("USDT")) #print(okx.transfer_to_funding("USDT","10")) - #print(okx.get_transfer_state("1064007151")) + #print(okx.get_transfer_state("1064667293")) #print(okx.subscribe_product("USDT", "10")) #print(okx.get_position("USDT")) #print(okx.redeem_product("USDT", "10")) #print(okx.transfer_to_trading("USDT", "10")) - #print(okx.get_transfer_state("1064541068")) + #print(okx.get_transfer_state("1064667720")) '''