From dfbf6d62de6dcb652a9d097b9fa073fa19615834 Mon Sep 17 00:00:00 2001 From: abuztrade <58076271+makarworld@users.noreply.github.com> Date: Thu, 5 Oct 2023 11:40:11 +0300 Subject: [PATCH] add affiliate endpoints. Pull Request #2 --- pymexc/base.py | 6 +- pymexc/base_websocket.py | 3 +- pymexc/futures.py | 18 ++--- pymexc/spot.py | 137 ++++++++++++++++++++++++++++++++++----- setup.py | 2 +- 5 files changed, 136 insertions(+), 30 deletions(-) diff --git a/pymexc/base.py b/pymexc/base.py index 8186082..e754d1f 100644 --- a/pymexc/base.py +++ b/pymexc/base.py @@ -1,6 +1,7 @@ from abc import ABC, abstractclassmethod from typing import Union, Literal -import hmac, hashlib +import hmac +import hashlib import requests from urllib.parse import urlencode import logging @@ -8,7 +9,8 @@ logger = logging.getLogger(__name__) -class MexcAPIError(Exception): pass +class MexcAPIError(Exception): + pass class MexcSDK(ABC): """ diff --git a/pymexc/base_websocket.py b/pymexc/base_websocket.py index 5729a3c..91bbbc7 100644 --- a/pymexc/base_websocket.py +++ b/pymexc/base_websocket.py @@ -184,7 +184,8 @@ def _auth(self): # make auth if futures. spot has a different auth system. isspot = self.endpoint.startswith(SPOT) - if isspot: return + if isspot: + return timestamp = str(int(time.time() * 1000)) _val = self.api_key + timestamp diff --git a/pymexc/futures.py b/pymexc/futures.py index c71f998..bfbba03 100644 --- a/pymexc/futures.py +++ b/pymexc/futures.py @@ -39,7 +39,7 @@ def handle_message(message): try: from base import _FuturesHTTP from base_websocket import _FuturesWebSocket -except: +except ImportError: from .base import _FuturesHTTP from .base_websocket import _FuturesWebSocket @@ -731,7 +731,7 @@ def order_deals(self, :return: response dictionary :rtype: dict """ - return self.call("GET", f"api/v1/private/order/list/order_deals", + return self.call("GET", "api/v1/private/order/list/order_deals", params = dict( symbol = symbol, start_time = start_time, @@ -771,7 +771,7 @@ def get_trigger_orders(self, :return: response dictionary :rtype: dict """ - return self.call("GET", f"api/v1/private/planorder/list/orders", + return self.call("GET", "api/v1/private/planorder/list/orders", params = dict( symbol = symbol, states = states, @@ -811,7 +811,7 @@ def get_stop_limit_orders(self, :rtype: dict """ - return self.call("GET", f"api/v1/private/stoporder/list/orders", + return self.call("GET", "api/v1/private/stoporder/list/orders", params = dict( symbol = symbol, is_finished = is_finished, @@ -836,7 +836,7 @@ def risk_limit(self, symbol: Optional[str] = None) -> dict: :return: response dictionary :rtype: dict """ - return self.call("GET", f"api/v1/private/account/risk_limit", + return self.call("GET", "api/v1/private/account/risk_limit", params = dict( symbol = symbol )) @@ -857,7 +857,7 @@ def tiered_fee_rate(self, symbol: Optional[str] = None) -> dict: :rtype: dict """ - return self.call("GET", f"api/v1/private/account/tiered_fee_rate", + return self.call("GET", "api/v1/private/account/tiered_fee_rate", params = dict( symbol = symbol )) @@ -884,7 +884,7 @@ def change_margin(self, :return: response dictionary :rtype: dict """ - return self.call("POST", f"api/v1/private/position/change_margin", + return self.call("POST", "api/v1/private/position/change_margin", params = dict( positionId = position_id, amount = amount, @@ -907,7 +907,7 @@ def get_leverage(self, symbol: str) -> dict: :rtype: dict """ - return self.call("GET", f"api/v1/private/position/leverage", + return self.call("GET", "api/v1/private/position/leverage", params = dict( symbol = symbol )) @@ -941,7 +941,7 @@ def change_leverage(self, :rtype: dict """ - return self.call("POST", f"api/v1/private/position/change_leverage", + return self.call("POST", "api/v1/private/position/change_leverage", params = dict( positionId = position_id, leverage = leverage, diff --git a/pymexc/spot.py b/pymexc/spot.py index d8b2fa7..864691c 100644 --- a/pymexc/spot.py +++ b/pymexc/spot.py @@ -32,14 +32,16 @@ def handle_message(message): """ from typing import Callable, Literal, List, Optional, Union -import threading, time, logging +import threading +import time +import logging logger = logging.getLogger(__name__) try: from base import _SpotHTTP from base_websocket import _SpotWebSocket -except: +except ImportError: from .base import _SpotHTTP from .base_websocket import _SpotWebSocket @@ -551,20 +553,6 @@ def query_universal_transfer_history(self, # # <=================================================================> - def all_orders(self, - symbol: str, - start_time: Optional[int] = None, - end_time: Optional[int] = None, - limit: Optional[int] = 500) -> dict: - - return self.call("GET", "/api/v3/allOrders", - params = dict( - symbol = symbol, - startTime = start_time, - endTime = end_time, - limit = limit - )) - def get_default_symbols(self) -> dict: """ ### User API default symbol. @@ -1315,7 +1303,7 @@ def user_universal_transfer_history_by_tranid(self, tran_id: str) -> dict: :return: response dictionary :rtype: dict """ - return self.call("GET", f"api/v3/capital/transfer/tranId", params=dict(tranId=tran_id)) + return self.call("GET", "api/v3/capital/transfer/tranId", params=dict(tranId=tran_id)) def get_assets_convert_into_mx(self) -> dict: """ @@ -1577,6 +1565,121 @@ def query_refercode(self) -> dict: """ return self.call("GET", "api/v3/rebate/referCode", params=dict(please_sign_me = None)) + def affiliate_commission_record(self, + start_time: Optional[int] = None, + end_time: Optional[int] = None, + invite_code: Optional[int] = None, + page: Optional[int] = None, + page_size: Optional[int] = None) -> dict: + """ + ### Get Affiliate Commission Record (affiliate only) + #### Required permission: SPOT_ACCOUNT_READ + + Weight(IP): 1 + + https://mexcdevelop.github.io/apidocs/spot_v3_en/#get-affiliate-commission-record-affiliate-only + + :param start_time: (optional) + :type start_time: int + :param end_time: (optional) + :type end_time: int + :param invite_code: (optional) + :type invite_code: int + :param page: (optional) default 1 + :type page: int + :param page_size: (optional) default 10 + :type page_size: int + + :return: response dictionary + :rtype: dict + """ + return self.call("GET", "/api/v3/rebate/affiliate/commission", + params = dict( + startTime = start_time, + endTime = end_time, + inviteCode = invite_code, + page = page, + pageSize = page_size + )) + + def affiliate_withdraw_record(self, + start_time: Optional[int] = None, + end_time: Optional[int] = None, + invite_code: Optional[int] = None, + page: Optional[int] = None, + page_size: Optional[int] = None) -> dict: + """ + ### Get Affiliate Withdraw Record (affiliate only) + #### Required permission: SPOT_ACCOUNT_READ + + Weight(IP): 1 + + https://mexcdevelop.github.io/apidocs/spot_v3_en/#get-affiliate-withdraw-record-affiliate-only + + :param start_time: (optional) + :type start_time: int + :param end_time: (optional) + :type end_time: int + :param invite_code: (optional) + :type invite_code: int + :param page: (optional) default 1 + :type page: int + :param page_size: (optional) default 10 + :type page_size: int + + :return: response dictionary + :rtype: dict + """ + return self.call("GET", "/api/v3/rebate/affiliate/withdraw", + params = dict( + startTime = start_time, + endTime = end_time, + inviteCode = invite_code, + page = page, + pageSize = page_size + )) + + def affiliate_commission_detail_record(self, + start_time: Optional[int] = None, + end_time: Optional[int] = None, + invite_code: Optional[int] = None, + page: Optional[int] = None, + page_size: Optional[int] = None, + type: Optional[Literal[1, 2, 3]] = None) -> dict: + """ + ### Get Affiliate Withdraw Record (affiliate only) + #### Required permission: SPOT_ACCOUNT_READ + + Weight(IP): 1 + + https://mexcdevelop.github.io/apidocs/spot_v3_en/#get-affiliate-withdraw-record-affiliate-only + + :param start_time: (optional) + :type start_time: int + :param end_time: (optional) + :type end_time: int + :param invite_code: (optional) + :type invite_code: int + :param page: (optional) default 1 + :type page: int + :param page_size: (optional) default 10 + :type page_size: int + :param type: (optional) commission type, 1:spot, 2:futures, 3:ETF + :type type: int + + :return: response dictionary + :rtype: dict + """ + return self.call("GET", "/api/v3/rebate/affiliate/commission/detail", + params = dict( + startTime = start_time, + endTime = end_time, + inviteCode = invite_code, + page = page, + pageSize = page_size, + type = type + )) + class WebSocket(_SpotWebSocket): def __init__(self, api_key: Optional[str] = None, diff --git a/setup.py b/setup.py index 95b1c0b..afb4956 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ """ -version = '1.0.9' +version = '1.0.10' with open("README.md", "r") as f: long_description = f.read()