253 lines
8.3 KiB
Python
253 lines
8.3 KiB
Python
'''
|
|
https://www.okx.com/docs-v5/en/#financial-product-simple-earn-flexible
|
|
|
|
https://github.com/okxapi/python-okx/blob/master/okx/Earning.py
|
|
|
|
'''
|
|
|
|
import okx.Earning as Earning
|
|
import okx.Funding as Funding
|
|
import okx.Account as Account
|
|
from credentials import get_api_key
|
|
|
|
|
|
class okx_earn:
|
|
def __init__(self):
|
|
api_key, api_secret, passphrase = get_api_key("okx")
|
|
self.earning_api = Earning.EarningAPI(api_key, api_secret, passphrase, False, "0")
|
|
self.funding_api = Funding.FundingAPI(api_key, api_secret, passphrase, False, "0")
|
|
self.account_api = Account.AccountAPI(api_key, api_secret, passphrase, False, "0")
|
|
|
|
|
|
def __str__(self):
|
|
return "okx"
|
|
|
|
|
|
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)
|
|
'''
|
|
|
|
balances = self.account_api.get_account_balance()
|
|
for item in balances["data"][0]["details"]:
|
|
if item["ccy"] == coin:
|
|
return {coin: item["availBal"]}
|
|
return {"Error": "Coin not found"}
|
|
|
|
|
|
def get_funding_balance(self, coin):
|
|
'''
|
|
Args:
|
|
coin (str): The coin to get the balance of
|
|
Returns:
|
|
dict: The free available balance of the coin in the funding account (or the equivalent account in the exchange)
|
|
'''
|
|
|
|
balances = self.funding_api.get_balances()
|
|
for item in balances["data"]:
|
|
if item["ccy"] == coin:
|
|
return {coin: item["availBal"]}
|
|
return {"Error": "Coin not found"}
|
|
|
|
|
|
def transfer_to_funding(self,coin,amount):
|
|
'''
|
|
Transfer funds from the trading account to the funding account
|
|
|
|
Response example:
|
|
{
|
|
"code": "0",
|
|
"msg": "",
|
|
"data": [
|
|
{
|
|
"transId": "754147",
|
|
"ccy": "USDT",
|
|
"clientId": "",
|
|
"from": "6",
|
|
"amt": "0.1",
|
|
"to": "18"
|
|
}
|
|
]
|
|
}
|
|
|
|
response: {'code': '0', 'data': [{'amt': '10', 'ccy': 'USDT', 'clientId': '', 'from': '18', 'to': '6', 'transId': '1064005710'}], 'msg': ''}
|
|
'''
|
|
|
|
response = self.funding_api.funds_transfer(coin,amount,"18","6","0")
|
|
if response["data"]!=[]:
|
|
return {"Success": "",
|
|
"transId": response["data"][0]["transId"]}
|
|
else:
|
|
return {"Error": response}
|
|
|
|
|
|
def transfer_to_trading(self,coin,amount):
|
|
'''
|
|
Transfer funds from the funding account to the trading account
|
|
|
|
Response example:
|
|
{
|
|
"code": "0",
|
|
"msg": "",
|
|
"data": [
|
|
{
|
|
"transId": "754147",
|
|
"ccy": "USDT",
|
|
"clientId": "",
|
|
"from": "6",
|
|
"amt": "0.1",
|
|
"to": "18"
|
|
}
|
|
]
|
|
}
|
|
|
|
response: {'code': '0', 'data': [{'amt': '20', 'ccy': 'USDT', 'clientId': '', 'from': '6', 'to': '18', 'transId': '1064008141'}], 'msg': ''}
|
|
'''
|
|
response = self.funding_api.funds_transfer(coin,amount,"6","18","0")
|
|
if response["data"]!=[]:
|
|
return {"Success": "",
|
|
"amount": response["data"][0]["amt"],
|
|
"transId": response["data"][0]["transId"]}
|
|
else:
|
|
return {"Error": response}
|
|
|
|
|
|
def get_transfer_state(self, transaction_id):
|
|
'''
|
|
Gets the state of a transfer.
|
|
Sometimes the transfer request fails, even if the transfer was successful.
|
|
|
|
Sample response:
|
|
{
|
|
"code": "0",
|
|
"data": [
|
|
{
|
|
"amt": "1.5",
|
|
"ccy": "USDT",
|
|
"clientId": "",
|
|
"from": "18",
|
|
"instId": "", //deprecated
|
|
"state": "success",
|
|
"subAcct": "test",
|
|
"to": "6",
|
|
"toInstId": "", //deprecated
|
|
"transId": "1",
|
|
"type": "1"
|
|
}
|
|
],
|
|
"msg": ""
|
|
}
|
|
|
|
response {'code': '0', 'data': [{'amt': '10', 'ccy': 'USDT', 'clientId': '', 'from': '18', 'instId': '', 'state': 'success', 'subAcct': '', 'to': '6', 'toInstId': '', 'transId': '1064005710', 'type': '0'}], 'msg': ''}
|
|
'''
|
|
response = self.funding_api.transfer_state(transaction_id)
|
|
if response["data"][0]["state"]=="success":
|
|
return {"Success": "",
|
|
"amount": response["data"][0]["amt"],
|
|
"transId": response["data"][0]["transId"]}
|
|
else:
|
|
return {"Error": response}
|
|
|
|
|
|
def get_available_products(self, coin):
|
|
response = self.earning_api.get_public_borrow_info(coin)
|
|
return {"coin": response["data"][0]["ccy"],
|
|
"product_id": response["data"][0]["ccy"],
|
|
"rate": response["data"][0]["avgRate"],
|
|
"min_amount": "0",
|
|
"isActive": True}
|
|
|
|
|
|
def subscribe_product(self, coin, amount, rate=None):
|
|
'''
|
|
ONLY ASSETS IN THE FUNDING ACCOUNT CAN BE USED FOR SUBSCRIPTION, MOVE THE FUNDS FROM
|
|
THE TRADING ACCOUNT TO THE FUNDING ACCOUNT BEFORE SUBSCRIBING.
|
|
|
|
response: {'code': '0', 'data': [{'amt': '10', 'ccy': 'USDT', 'rate': '0.1091', 'side': 'purchase'}], 'msg': ''}
|
|
'''
|
|
if rate is None:
|
|
rate = self.get_avg_rate(coin)
|
|
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):
|
|
'''
|
|
ASSETS REDEEMED WILL BE PLACED IN THE FUNDING ACCOUNT, MOVE THE FUNDS FROM THE FUNDING ACCOUNT
|
|
TO THE TRADING ACCOUNT AFTER REDEMPTION.
|
|
|
|
response: {'code': '0', 'data': [{'amt': '20', 'ccy': 'USDT', 'rate': '', 'side': 'redempt'}], 'msg': ''}
|
|
'''
|
|
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):
|
|
return self.earning_api.set_lending_rate(coin, str(rate))
|
|
|
|
|
|
def get_avg_rate(self,coin):
|
|
'''
|
|
Returns the 24hs average lending rate
|
|
'''
|
|
rate = self.earning_api.get_public_borrow_info(coin)
|
|
return str(rate["data"][0]["avgRate"])
|
|
|
|
|
|
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):
|
|
return self.earning_api.get_saving_balance()
|
|
|
|
|
|
def get_lending_history(self,coin=None):
|
|
return self.earning_api.get_lending_history(coin)
|
|
|
|
|
|
def get_personal_left_quota(self, product_id, **kwargs):
|
|
return {"Error": "N/A"}
|
|
|
|
|
|
def get_subscription_record(self, **kwargs):
|
|
return {"Error": "N/A"}
|
|
|
|
|
|
def get_redemption_record(self, **kwargs):
|
|
return {"Error": "N/A"}
|
|
|
|
|
|
def get_rewards_history(self, type="ALL", **kwargs):
|
|
return {"Error": "N/A"}
|
|
|
|
|
|
def get_subscription_preview(self, product_id, amount, **kwargs):
|
|
return {"Error": "N/A"} |