DCAv2Earn/libraries/wrappers/earn_binance.py

170 lines
6.9 KiB
Python

'''
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 get_trading_balance(self, coin):
'''
Returns the free available balance of a 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 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)
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=True, source_account="SPOT"):
'''
autoSubscribe (boolean, optional): true or false, default true.
sourceAccount (str, optional): SPOT,FUND,ALL, default SPOT
recvWindow (int, optional): The value cannot be greater than 60000
'''
try:
response = self.client.subscribe_flexible_product(productId=product_id, amount=amount, autoSubscribe=auto_subscribe, sourceAccount=source_account, recvWindow=5000)
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 redeem_product(self, product_id, redeem_all=True, amount=0, destination_account="SPOT"):
'''
redeemAll (boolean, optional): true or false, default to false
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
'''
try:
response = self.client.redeem_flexible_product(productId=product_id, redeemAll=redeem_all, amount=amount, destAccount=destination_account, recvWindow=5000)
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 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
'''
try:
response = self.client.get_flexible_product_position(asset=coin, **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))
return None
def get_account(self, recv_window=5000):
'''
recvWindow (int, optional): The value cannot be greater than 60000
'''
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))