''' https://github.com/binance/binance-connector-python/blob/master/binance/spot/_simple_earn.py ''' from credentials import get_api_key from binance.spot import Spot as Client from binance.error import ClientError class binance_earn: def __init__(self): self.api_key, self.api_secret = get_api_key("binance") self.client = Client(self.api_key, self.api_secret) def __str__(self): return "binance" 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) ''' account = self.client.account() for item in account["balances"]: if item["asset"]==coin: return {coin: item["free"]} def get_available_products(self, coin): try: response = self.client.get_simple_earn_flexible_product_list(asset=coin, current=1, size=100, recvWindow=5000) if response["total"]>0: return {"coin": response["rows"][0]["asset"], "product_id": response["rows"][0]["productId"], "rate": response["rows"][0]["latestAnnualPercentageRate"], "min_amount": response["rows"][0]["minPurchaseAmount"], "isActive": response["rows"][0]["canPurchase"] and response["rows"][0]["canRedeem"] and not response["rows"][0]["isSoldOut"]} return 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 def subscribe_product(self, product_id, amount, auto_subscribe=False, source_account="SPOT"): ''' autoSubscribe (boolean, optional): true or false, default false. sourceAccount (str, optional): SPOT,FUND,ALL, default SPOT recvWindow (int, optional): The value cannot be greater than 60000 returns { purchaseId: int, success: boolean, amount: str } example: {'purchaseId': 7531473612, 'success': True, 'amount': '10'} ''' try: response = self.client.subscribe_flexible_product(productId=product_id, amount=amount, autoSubscribe=auto_subscribe, sourceAccount=source_account, recvWindow=5000) 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 def redeem_product(self, product_id, redeem_all=False, amount=0, destination_account="SPOT"): ''' redeemAll (boolean, optional): true or false, default to false (Note: redeemAll=True didn't seem to work) amount (float, optional): if redeemAll is false, amount is mandatory destAccount (str, optional): SPOT,FUND,ALL, default SPOT recvWindow (int, optional): The value cannot be greater than 60000 example: {'redeemId': 483738233, 'success': True} ''' try: response = self.client.redeem_flexible_product(productId=product_id, redeemAll=redeem_all, amount=amount, destAccount=destination_account, recvWindow=5000) 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 def get_position(self, coin=None, **kwargs): ''' asset (str, optional) productId (str, optional) current (int, optional): Current querying page. Start from 1. Default:1 size (int, optional): Default:10 Max:100 recvWindow (int, optional): The value cannot be greater than 60000 example: {'total': 1, 'rows': [{'totalAmount': '10.00000299', 'tierAnnualPercentageRate': {'0-500USDT': '0.05000000'}, 'latestAnnualPercentageRate': '0.05229776', 'asset': 'USDT', 'canRedeem': True, 'collateralAmount': '0', 'productId': 'USDT001', 'yesterdayRealTimeRewards': '0', 'cumulativeBonusRewards': '0', 'cumulativeRealTimeRewards': '0.00000299', 'cumulativeTotalRewards': '0.00000299', 'autoSubscribe': False}]} ''' try: response = self.client.get_flexible_product_position(asset=coin, **kwargs) 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 def get_account(self, recv_window=5000): ''' recvWindow (int, optional): The value cannot be greater than 60000 example: {'totalAmountInBTC': '0.00010133', 'totalAmountInUSDT': '10.00000398', 'totalFlexibleAmountInBTC': '0.00010133', 'totalFlexibleAmountInUSDT': '10.00000398', 'totalLockedInBTC': '0', 'totalLockedInUSDT': '0'} ''' try: response = self.client.simple_account(recv_window=recv_window) return response except ClientError as error: print("Found error. status: {}, error code: {}, error message: {}".format(error.status_code, error.error_code, error.error_message)) def get_personal_left_quota(self, product_id, **kwargs): ''' ''' try: response = self.client.get_flexible_personal_left_quota(productId=product_id, **kwargs) return response except ClientError as error: print("Found error. status: {}, error code: {}, error message: {}".format(error.status_code, error.error_code, error.error_message)) def get_subscription_record(self, **kwargs): ''' productId (str, optional) purchaseId (str, optional) asset (str, optional) startTime (int, optional): UTC timestamp in ms endTime (int, optional): UTC timestamp in ms current (int, optional): Current querying page. Start from 1. Default:1 size (int, optional): Default:10 Max:100 recvWindow (int, optional): The value cannot be greater than 60000 ''' try: response = self.client.get_flexible_subscription_record(**kwargs) return response except ClientError as error: print("Found error. status: {}, error code: {}, error message: {}".format(error.status_code, error.error_code, error.error_message)) def get_redemption_record(self, **kwargs): ''' productId (str, optional) redeemId (str, optional) asset (str, optional) startTime (int, optional): UTC timestamp in ms endTime (int, optional): UTC timestamp in ms current (int, optional): Current querying page. Start from 1. Default:1 size (int, optional): Default:10 Max:100 ''' try: response = self.client.get_flexible_redemption_record(**kwargs) return response except ClientError as error: print("Found error. status: {}, error code: {}, error message: {}".format(error.status_code, error.error_code, error.error_message)) def get_rewards_history(self, type="ALL", **kwargs): ''' type (str): ALL, Bonus, REALTIME, REWARDS productId (str, optional) asset (str, optional) startTime (int, optional): UTC timestamp in ms endTime (int, optional): UTC timestamp in ms ''' try: response = self.client.get_flexible_rewards_history(type=type, **kwargs) return response except ClientError as error: print("Found error. status: {}, error code: {}, error message: {}".format(error.status_code, error.error_code, error.error_message)) def get_subscription_preview(self, product_id, amount, **kwargs): ''' args product_id (str) amount (float) kwargs recvWindow (int, optional): The value cannot be greater than 60000 ''' try: response = self.client.get_flexible_subscription_preview(productId=product_id, amount=amount, **kwargs) return response except ClientError as error: print("Found error. status: {}, error code: {}, error message: {}".format(error.status_code, error.error_code, error.error_message))