Files
beast-trader/dashboard/venv/lib/python3.12/site-packages/ccxt/aster.py

4074 lines
187 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
# PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
# https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code
from ccxt.base.exchange import Exchange
from ccxt.abstract.aster import ImplicitAPI
from ccxt.base.types import Any, Balances, Currencies, Currency, Int, LedgerEntry, Leverage, Leverages, MarginMode, MarginModes, MarginModification, Market, Num, Order, OrderBook, OrderRequest, OrderSide, OrderType, Position, Str, Strings, Ticker, Tickers, FundingRate, FundingRates, Trade, TradingFeeInterface, Transaction, TransferEntry
from typing import List
from ccxt.base.errors import ExchangeError
from ccxt.base.errors import AuthenticationError
from ccxt.base.errors import PermissionDenied
from ccxt.base.errors import AccountNotEnabled
from ccxt.base.errors import AccountSuspended
from ccxt.base.errors import ArgumentsRequired
from ccxt.base.errors import BadRequest
from ccxt.base.errors import BadSymbol
from ccxt.base.errors import OperationRejected
from ccxt.base.errors import NoChange
from ccxt.base.errors import MarketClosed
from ccxt.base.errors import InsufficientFunds
from ccxt.base.errors import InvalidOrder
from ccxt.base.errors import OrderNotFound
from ccxt.base.errors import OrderImmediatelyFillable
from ccxt.base.errors import OrderNotFillable
from ccxt.base.errors import DuplicateOrderId
from ccxt.base.errors import NotSupported
from ccxt.base.errors import ExchangeClosedByUser
from ccxt.base.errors import OperationFailed
from ccxt.base.errors import NetworkError
from ccxt.base.errors import RateLimitExceeded
from ccxt.base.errors import InvalidNonce
from ccxt.base.errors import RequestTimeout
from ccxt.base.errors import BadResponse
from ccxt.base.decimal_to_precision import TRUNCATE
from ccxt.base.decimal_to_precision import TICK_SIZE
from ccxt.base.precise import Precise
class aster(Exchange, ImplicitAPI):
def describe(self) -> Any:
return self.deep_extend(super(aster, self).describe(), {
'id': 'aster',
'name': 'Aster',
'countries': ['US'],
# 3 req/s for free
# 150 req/s for subscribers: https://aster.markets/data
# for brokers: https://aster.markets/docs/api-references/broker-api/#authentication-and-rate-limit
'rateLimit': 333,
'hostname': 'aster.markets',
'certified': False,
'pro': True,
'dex': True,
'urls': {
'logo': 'https://github.com/user-attachments/assets/4982201b-73cd-4d7a-8907-e69e239e9609',
'www': 'https://www.asterdex.com/en',
'api': {
'fapiPublic': 'https://fapi.asterdex.com/fapi',
'fapiPrivate': 'https://fapi.asterdex.com/fapi',
'sapiPublic': 'https://sapi.asterdex.com/api',
'sapiPrivate': 'https://sapi.asterdex.com/api',
},
'doc': 'https://github.com/asterdex/api-docs',
'fees': 'https://docs.asterdex.com/product/asterex-simple/fees-and-slippage',
'referral': {
'url': 'https://www.asterdex.com/en/referral/aA1c2B',
'discount': 0.1,
},
},
'has': {
'CORS': None,
'spot': False,
'margin': False,
'swap': False,
'future': False,
'option': False,
'addMargin': True,
'borrowCrossMargin': False,
'borrowIsolatedMargin': False,
'cancelAllOrders': True,
'cancelOrder': True,
'cancelOrders': True,
'closeAllPositions': False,
'closePosition': False,
'createConvertTrade': False,
'createDepositAddress': False,
'createLimitBuyOrder': False,
'createLimitSellOrder': False,
'createMarketBuyOrder': False,
'createMarketBuyOrderWithCost': False,
'createMarketOrderWithCost': False,
'createMarketSellOrder': False,
'createMarketSellOrderWithCost': False,
'createOrder': True,
'createOrders': False,
'createOrderWithTakeProfitAndStopLoss': False,
'createPostOnlyOrder': False,
'createReduceOnlyOrder': False,
'createStopLimitOrder': False,
'createStopLossOrder': False,
'createStopMarketOrder': False,
'createStopOrder': False,
'createTakeProfitOrder': False,
'createTrailingPercentOrder': False,
'createTriggerOrder': False,
'editOrder': False,
'editOrders': False,
'fetchAccounts': None,
'fetchBalance': True,
'fetchBidsAsks': False,
'fetchBorrowInterest': False,
'fetchBorrowRateHistories': False,
'fetchBorrowRateHistory': False,
'fetchCanceledAndClosedOrders': 'emulated',
'fetchCanceledOrders': 'emulated',
'fetchClosedOrder': False,
'fetchClosedOrders': 'emulated',
'fetchConvertCurrencies': False,
'fetchConvertQuote': False,
'fetchConvertTrade': False,
'fetchConvertTradeHistory': False,
'fetchCrossBorrowRate': False,
'fetchCrossBorrowRates': False,
'fetchCurrencies': True,
'fetchDeposit': False,
'fetchDepositAddress': False,
'fetchDepositAddresses': False,
'fetchDepositAddressesByNetwork': False,
'fetchDeposits': False,
'fetchDepositsWithdrawals': False,
'fetchDepositWithdrawFee': 'emulated',
'fetchDepositWithdrawFees': False,
'fetchFundingHistory': True,
'fetchFundingInterval': 'emulated',
'fetchFundingIntervals': True,
'fetchFundingRate': True,
'fetchFundingRateHistory': True,
'fetchFundingRates': True,
'fetchGreeks': False,
'fetchIndexOHLCV': False,
'fetchIsolatedBorrowRate': 'emulated',
'fetchIsolatedBorrowRates': False,
'fetchL3OrderBook': False,
'fetchLastPrices': False,
'fetchLedger': True,
'fetchLedgerEntry': False,
'fetchLeverage': 'emulated',
'fetchLeverages': True,
'fetchLeverageTiers': False,
'fetchLiquidations': False,
'fetchLongShortRatio': False,
'fetchLongShortRatioHistory': False,
'fetchMarginAdjustmentHistory': True,
'fetchMarginMode': 'emulated',
'fetchMarginModes': True,
'fetchMarketLeverageTiers': 'emulated',
'fetchMarkets': True,
'fetchMarkOHLCV': False,
'fetchMarkPrice': False,
'fetchMarkPrices': False,
'fetchMyLiquidations': False,
'fetchMySettlementHistory': False,
'fetchMyTrades': True,
'fetchOHLCV': True,
'fetchOpenInterest': False,
'fetchOpenInterestHistory': False,
'fetchOpenOrder': True,
'fetchOpenOrders': True,
'fetchOption': False,
'fetchOptionChain': False,
'fetchOrder': True,
'fetchOrderBook': True,
'fetchOrderBooks': False,
'fetchOrders': True,
'fetchOrderTrades': False,
'fetchPosition': False,
'fetchPositionHistory': False,
'fetchPositionMode': True,
'fetchPositions': True,
'fetchPositionsHistory': False,
'fetchPositionsRisk': True,
'fetchPremiumIndexOHLCV': False,
'fetchSettlementHistory': False,
'fetchStatus': False,
'fetchTicker': True,
'fetchTickers': True,
'fetchTime': True,
'fetchTrades': True,
'fetchTradingFee': True,
'fetchTradingFees': False,
'fetchTradingLimits': 'emulated',
'fetchTransactionFee': 'emulated',
'fetchTransactionFees': False,
'fetchTransactions': False,
'fetchTransfer': False,
'fetchTransfers': False,
'fetchUnderlyingAssets': False,
'fetchVolatilityHistory': False,
'fetchWithdrawAddresses': False,
'fetchWithdrawal': False,
'fetchWithdrawals': False,
'fetchWithdrawalWhitelist': False,
'reduceMargin': True,
'repayCrossMargin': False,
'repayIsolatedMargin': False,
'sandbox': False,
'setLeverage': True,
'setMargin': False,
'setMarginMode': True,
'setPositionMode': True,
'signIn': True,
'transfer': True,
'withdraw': True,
},
'api': {
'fapiPublic': {
'get': {
'v1/ping': 1,
'v3/ping': 1,
'v1/time': 1,
'v3/time': 1,
'v1/exchangeInfo': 1,
'v3/exchangeInfo': 1,
'v1/depth': 1,
'v3/depth': 2, # dynamic: 5, 10, 20, 50->2, 100->5, 500->10, 1000->20
'v1/trades': 1,
'v3/trades': 1,
'v1/historicalTrades': 1,
'v3/historicalTrades': 20,
'v1/aggTrades': 1,
'v3/aggTrades': 20,
'v1/klines': 1,
'v3/klines': 1, # dynamic [1,100) ->1, [100, 500)->2, [500, 1000]->5, [1000 -> 10
'v1/indexPriceKlines': 1,
'v3/indexPriceKlines': 1, # same
'v1/markPriceKlines': 1,
'v3/markPriceKlines': 1, # same
'v1/premiumIndex': 1,
'v3/premiumIndex': 1,
'v1/fundingRate': 1,
'v3/fundingRate': 1,
'v1/fundingInfo': 1,
'v3/fundingInfo': 1,
'v1/ticker/24hr': 1,
'v3/ticker/24hr': 1, # 1 single-symbol, otherwise 40
'v1/ticker/price': 1,
'v3/ticker/price': 1, # 1 single-symbol, otherwise 2
'v1/ticker/bookTicker': 1,
'v3/ticker/bookTicker': 1, # 1 single-symbol, otherwise 2
# different endpoints
'v1/adlQuantile': 1,
'v1/forceOrders': 1,
'v3/indexreferences': 1,
},
},
'fapiPrivate': {
'get': {
'v1/positionSide/dual': 1,
'v3/positionSide/dual': 30,
'v1/multiAssetsMargin': 1,
'v3/multiAssetsMargin': 1,
'v1/order': 1,
'v3/order': 1,
'v1/openOrder': 1,
'v3/openOrder': 1,
'v1/openOrders': 1,
'v3/openOrders': 1,
'v1/allOrders': 1,
'v3/allOrders': 1,
'v2/balance': 1,
'v3/balance': 1,
'v3/account': 1,
'v1/positionMargin/history': 1,
'v3/positionMargin/history': 1,
'v2/positionRisk': 1,
'v3/positionRisk': 1,
'v1/userTrades': 1,
'v3/userTrades': 5,
'v1/income': 1,
'v3/income': 1,
'v1/leverageBracket': 1,
'v3/leverageBracket': 1,
'v1/commissionRate': 1,
'v3/commissionRate': 1,
# others
'v3/adlQuantile': 1,
'v3/forceOrders': 1,
'v3/mmp': 1,
'v3/accountWithJoinMargin': 1,
'v4/account': 1,
# builder
'v3/agent': 1,
'v3/builder': 1,
},
'post': {
'v1/positionSide/dual': 1,
'v3/positionSide/dual': 1,
'v1/multiAssetsMargin': 1,
'v3/multiAssetsMargin': 1,
'v1/order': 1,
'v3/order': 1,
'v1/order/test': 1,
'v3/order/test': 1,
'v1/batchOrders': 1,
'v3/batchOrders': 1,
'v1/asset/wallet/transfer': 1,
'v3/asset/wallet/transfer': 1,
'v1/countdownCancelAll': 1,
'v3/countdownCancelAll': 1,
'v1/leverage': 1,
'v3/leverage': 1,
'v1/marginType': 1,
'v3/marginType': 1,
'v1/positionMargin': 1,
'v3/positionMargin': 1,
'v1/listenKey': 1,
'v3/listenKey': 1,
# others
'v3/mmp': 1,
'v3/mmpReset': 1,
'v3/noop': 1,
# builder
'v3/approveAgent': 1,
'v3/updateAgent': 1,
'v3/approveBuilder': 1,
'v3/updateBuilder': 1,
},
'put': {
'v1/listenKey': 1,
'v3/listenKey': 1,
},
'delete': {
'v1/order': 1,
'v3/order': 1,
'v1/allOpenOrders': 1,
'v3/allOpenOrders': 1,
'v1/batchOrders': 1,
'v3/batchOrders': 1,
'v3/mmp': 1,
'v1/listenKey': 1,
'v3/listenKey': 1,
# builder
'v3/agent': 1,
'v3/builder': 1,
},
},
'sapiPublic': {
'get': {
# v1
'v1/ping': 1,
'v1/time': 1,
'v1/exchangeInfo': 1,
'v1/depth': 1,
'v1/trades': 1,
'v1/historicalTrades': 1,
'v1/aggTrades': 1,
'v1/klines': 1,
'v1/ticker/24hr': 1,
'v1/ticker/price': 1,
'v1/ticker/bookTicker': 1,
'v1/aster/withdraw/estimateFee': 1,
# v3
'v3/ping': 1,
'v3/time': 1,
'v3/exchangeInfo': 1,
'v3/depth': {'cost': 2, 'byLimit': [[50, 2], [100, 5], [500, 10], [1000, 20]]},
'v3/trades': 1,
'v3/historicalTrades': 20,
'v3/aggTrades': 20,
'v3/klines': {'cost': 1, 'byLimit': [[99, 1], [499, 2], [1000, 5], [10000, 10]]}, # todo: not specified in docs
'v3/ticker/24hr': {'cost': 1, 'noSymbol': 40},
'v3/ticker/price': {'cost': 1, 'noSymbol': 2},
'v3/ticker/bookTicker': {'cost': 1, 'noSymbol': 2},
'v3/aster/withdraw/estimateFee': 1,
},
},
'sapiPrivate': {
'get': {
# v1
'v1/commissionRate': 1,
'v1/order': 1,
'v1/openOrders': 1,
'v1/allOrders': 1,
'v1/transactionHistory': 1,
'v1/account': 1,
'v1/userTrades': 1,
# v3
'v3/commissionRate': {'cost': 1, 'noSymbol': 2},
'v3/order': 1,
'v3/openOrders': 1, # with symbol 1, otherwise 40
'v3/allOrders': 5,
'v3/account': 5,
'v3/userTrades': 5,
'v3/openOrder': 1,
},
'post': {
# v1
'v1/order': 1,
'v1/asset/wallet/transfer': 5,
'v1/asset/sendToAddress': 1, # inexistent in v3
'v1/listenKey': 1,
# v3
'v3/order': 1,
'v3/asset/wallet/transfer': 5,
'v3/aster/user-withdraw': 1,
'v3/listenKey': 1,
},
'put': [
'v1/listenKey',
'v3/listenKey',
],
'delete': {
# v1
'v1/order': 1,
'v1/allOpenOrders': 1,
'v1/listenKey': 1,
# v3
'v3/allOpenOrders': 1,
'v3/order': 1,
'v3/listenKey': 1,
},
},
},
'timeframes': {
'1m': '1m',
'3m': '3m',
'5m': '5m',
'15m': '15m',
'30m': '30m',
'1h': '1h',
'2h': '2h',
'4h': '4h',
'6h': '6h',
'8h': '8h',
'12h': '12h',
'1d': '1d',
'3d': '3d',
'1w': '1w',
'1M': '1M',
},
'precisionMode': TICK_SIZE,
'requiredCredentials': {
'apiKey': False,
'secret': False,
'privateKey': True,
},
'fees': {
'trading': {
'tierBased': True,
'percentage': True,
'maker': self.parse_number('0.0001'),
'taker': self.parse_number('0.00035'),
},
},
'options': {
'defaultType': 'spot',
'recvWindow': 10 * 1000, # 10 sec
'defaultTimeInForce': 'GTC', # 'GTC' = Good To Cancel(default), 'IOC' = Immediate Or Cancel
'zeroAddress': '0x0000000000000000000000000000000000000000',
'v3ChainId': 1666, # Aster chain ID used for EIP-712 v3 signing
'quoteOrderQty': True, # whether market orders support amounts in quote currency
'accountsByType': {
'spot': 'SPOT',
'swap': 'FUTURE',
'future': 'FUTURE',
'linear': 'FUTURE',
},
'networks': {
'ERC20': 'ETH',
'BEP20': 'BSC',
'ARBONE': 'Arbitrum',
},
'networksToChainId': {
'ETH': 1,
'BSC': 56,
'Arbitrum': 42161,
},
'fetchOpenOrders': {
'warnIfNoSymbol': True, # set to False to suppress warning when calling fetchOpenOrders without symbol
},
'builderFee': True,
'builder': '0x1F5877C19e3777Cfd15F9d57253eA4aA5254Ec39',
'builderRate': '0.001',
},
'exceptions': {
'exact': {
# 10xx - General Server or Network issues
'-1000': OperationRejected, # UNKNOWN
'-1001': NetworkError, # DISCONNECTED
'-1002': AuthenticationError, # UNAUTHORIZED
'-1003': RateLimitExceeded, # TOO_MANY_REQUESTS
'-1004': DuplicateOrderId, # DUPLICATE_IP
'-1005': BadRequest, # NO_SUCH_IP
'-1006': BadResponse, # UNEXPECTED_RESP
'-1007': RequestTimeout, # TIMEOUT
'-1010': OperationRejected, # ERROR_MSG_RECEIVED
'-1011': PermissionDenied, # NON_WHITE_LIST
'-1013': BadRequest, # INVALID_MESSAGE
'-1014': OrderNotFillable, # UNKNOWN_ORDER_COMPOSITION
'-1015': RateLimitExceeded, # TOO_MANY_ORDERS
'-1016': ExchangeClosedByUser, # SERVICE_SHUTTING_DOWN
'-1020': NotSupported, # UNSUPPORTED_OPERATION
'-1021': InvalidNonce, # INVALID_TIMESTAMP
'-1022': AuthenticationError, # INVALID_SIGNATURE
'-1023': BadRequest, # START_TIME_GREATER_THAN_END_TIME
# 11xx - Request issues
'-1100': BadRequest, # ILLEGAL_CHARS
'-1101': BadRequest, # TOO_MANY_PARAMETERS
'-1102': ArgumentsRequired, # MANDATORY_PARAM_EMPTY_OR_MALFORMED
'-1103': BadRequest, # UNKNOWN_PARAM
'-1104': BadRequest, # UNREAD_PARAMETERS
'-1105': ArgumentsRequired, # PARAM_EMPTY
'-1106': BadRequest, # PARAM_NOT_REQUIRED
'-1108': BadRequest, # BAD_ASSET
'-1109': BadRequest, # BAD_ACCOUNT
'-1110': BadSymbol, # BAD_INSTRUMENT_TYPE
'-1111': BadRequest, # BAD_PRECISION
'-1112': BadRequest, # NO_DEPTH
'-1113': BadRequest, # WITHDRAW_NOT_NEGATIVE
'-1114': BadRequest, # TIF_NOT_REQUIRED
'-1115': InvalidOrder, # INVALID_TIF
'-1116': InvalidOrder, # INVALID_ORDER_TYPE
'-1117': InvalidOrder, # INVALID_SIDE
'-1118': InvalidOrder, # EMPTY_NEW_CL_ORD_ID
'-1119': InvalidOrder, # EMPTY_ORG_CL_ORD_ID
'-1120': BadRequest, # BAD_INTERVAL
'-1121': BadSymbol, # BAD_SYMBOL
'-1125': AuthenticationError, # INVALID_LISTEN_KEY
'-1127': BadRequest, # MORE_THAN_XX_HOURS
'-1128': BadRequest, # OPTIONAL_PARAMS_BAD_COMBO
'-1130': BadRequest, # INVALID_PARAMETER
'-1136': InvalidOrder, # INVALID_NEW_ORDER_RESP_TYPE
# 20xx - Processing Issues
'-2010': InvalidOrder, # NEW_ORDER_REJECTED
'-2011': OrderNotFound, # CANCEL_REJECTED
'-2013': OrderNotFound, # NO_SUCH_ORDER
'-2014': AuthenticationError, # BAD_API_KEY_FMT
'-2015': AuthenticationError, # REJECTED_MBX_KEY
'-2016': MarketClosed, # NO_TRADING_WINDOW
'-2018': InsufficientFunds, # BALANCE_NOT_SUFFICIENT
'-2019': InsufficientFunds, # MARGIN_NOT_SUFFICIEN
'-2020': OrderNotFillable, # UNABLE_TO_FILL
'-2021': OrderImmediatelyFillable, # ORDER_WOULD_IMMEDIATELY_TRIGGER
'-2022': OperationRejected, # REDUCE_ONLY_REJECT
'-2023': AccountSuspended, # USER_IN_LIQUIDATION
'-2024': InsufficientFunds, # POSITION_NOT_SUFFICIENT
'-2025': RateLimitExceeded, # MAX_OPEN_ORDER_EXCEEDED
'-2026': NotSupported, # REDUCE_ONLY_ORDER_TYPE_NOT_SUPPORTED
'-2027': BadRequest, # MAX_LEVERAGE_RATIO
'-2028': BadRequest, # MIN_LEVERAGE_RATIO
# 40xx - Filters and other Issues
'-4000': InvalidOrder, # INVALID_ORDER_STATUS
'-4001': InvalidOrder, # PRICE_LESS_THAN_ZERO
'-4002': InvalidOrder, # PRICE_GREATER_THAN_MAX_PRICE
'-4003': InvalidOrder, # QTY_LESS_THAN_ZERO
'-4004': InvalidOrder, # QTY_LESS_THAN_MIN_QTY
'-4005': InvalidOrder, # QTY_GREATER_THAN_MAX_QTY
'-4006': InvalidOrder, # STOP_PRICE_LESS_THAN_ZERO
'-4007': InvalidOrder, # STOP_PRICE_GREATER_THAN_MAX_PRICE
'-4008': InvalidOrder, # TICK_SIZE_LESS_THAN_ZERO
'-4009': InvalidOrder, # MAX_PRICE_LESS_THAN_MIN_PRICE
'-4010': InvalidOrder, # MAX_QTY_LESS_THAN_MIN_QTY
'-4011': InvalidOrder, # STEP_SIZE_LESS_THAN_ZERO
'-4012': RateLimitExceeded, # MAX_NUM_ORDERS_LESS_THAN_ZERO
'-4013': InvalidOrder, # PRICE_LESS_THAN_MIN_PRICE
'-4014': InvalidOrder, # PRICE_NOT_INCREASED_BY_TICK_SIZE
'-4015': InvalidOrder, # INVALID_CL_ORD_ID_LEN
'-4016': InvalidOrder, # PRICE_HIGHTER_THAN_MULTIPLIER_UP
'-4017': InvalidOrder, # MULTIPLIER_UP_LESS_THAN_ZERO
'-4018': InvalidOrder, # MULTIPLIER_DOWN_LESS_THAN_ZERO
'-4019': BadRequest, # COMPOSITE_SCALE_OVERFLOW
'-4020': BadRequest, # TARGET_STRATEGY_INVALID
'-4021': BadRequest, # INVALID_DEPTH_LIMIT
'-4022': MarketClosed, # WRONG_MARKET_STATUS
'-4023': InvalidOrder, # QTY_NOT_INCREASED_BY_STEP_SIZE
'-4024': InvalidOrder, # PRICE_LOWER_THAN_MULTIPLIER_DOWN
'-4025': BadRequest, # MULTIPLIER_DECIMAL_LESS_THAN_ZERO
'-4026': BadRequest, # COMMISSION_INVALID
'-4027': BadRequest, # INVALID_ACCOUNT_TYPE
'-4028': BadRequest, # INVALID_LEVERAGE
'-4029': BadRequest, # INVALID_TICK_SIZE_PRECISION
'-4030': BadRequest, # INVALID_STEP_SIZE_PRECISION
'-4031': BadRequest, # INVALID_WORKING_TYPE
'-4032': RateLimitExceeded, # EXCEED_MAX_CANCEL_ORDER_SIZE
'-4033': AccountNotEnabled, # INSURANCE_ACCOUNT_NOT_FOUND
'-4044': BadRequest, # INVALID_BALANCE_TYPE
'-4045': RateLimitExceeded, # MAX_STOP_ORDER_EXCEEDED
'-4046': NoChange, # NO_NEED_TO_CHANGE_MARGIN_TYPE
'-4047': OperationRejected, # THERE_EXISTS_OPEN_ORDERS
'-4048': OperationRejected, # THERE_EXISTS_QUANTITY
'-4049': OperationRejected, # ADD_ISOLATED_MARGIN_REJECT
'-4050': InsufficientFunds, # CROSS_BALANCE_INSUFFICIENT
'-4051': InsufficientFunds, # ISOLATED_BALANCE_INSUFFICIENT
'-4052': NoChange, # NO_NEED_TO_CHANGE_AUTO_ADD_MARGIN
'-4053': OperationRejected, # AUTO_ADD_CROSSED_MARGIN_REJECT
'-4054': OperationRejected, # ADD_ISOLATED_MARGIN_NO_POSITION_REJECT
'-4055': ArgumentsRequired, # AMOUNT_MUST_BE_POSITIVE
'-4056': AuthenticationError, # INVALID_API_KEY_TYPE
'-4057': AuthenticationError, # INVALID_RSA_PUBLIC_KEY
'-4058': InvalidOrder, # MAX_PRICE_TOO_LARGE
'-4059': NoChange, # NO_NEED_TO_CHANGE_POSITION_SIDE
'-4060': InvalidOrder, # INVALID_POSITION_SIDE
'-4061': InvalidOrder, # POSITION_SIDE_NOT_MATCH
'-4062': OperationRejected, # REDUCE_ONLY_CONFLICT
'-4063': BadRequest, # INVALID_OPTIONS_REQUEST_TYPE
'-4064': BadRequest, # INVALID_OPTIONS_TIME_FRAME
'-4065': BadRequest, # INVALID_OPTIONS_AMOUNT
'-4066': BadRequest, # INVALID_OPTIONS_EVENT_TYPE
'-4067': OperationRejected, # POSITION_SIDE_CHANGE_EXISTS_OPEN_ORDERS
'-4068': OperationRejected, # POSITION_SIDE_CHANGE_EXISTS_QUANTITY
'-4069': BadRequest, # INVALID_OPTIONS_PREMIUM_FEE
'-4070': InvalidOrder, # INVALID_CL_OPTIONS_ID_LEN
'-4071': InvalidOrder, # INVALID_OPTIONS_DIRECTION
'-4072': NoChange, # OPTIONS_PREMIUM_NOT_UPDATE
'-4073': BadRequest, # OPTIONS_PREMIUM_INPUT_LESS_THAN_ZERO
'-4074': InvalidOrder, # OPTIONS_AMOUNT_BIGGER_THAN_UPPER
'-4075': OperationRejected, # OPTIONS_PREMIUM_OUTPUT_ZERO
'-4076': OperationRejected, # OPTIONS_PREMIUM_TOO_DIFF
'-4077': RateLimitExceeded, # OPTIONS_PREMIUM_REACH_LIMIT
'-4078': BadRequest, # OPTIONS_COMMON_ERROR
'-4079': BadRequest, # INVALID_OPTIONS_ID
'-4080': BadRequest, # OPTIONS_USER_NOT_FOUND
'-4081': BadRequest, # OPTIONS_NOT_FOUND
'-4082': RateLimitExceeded, # INVALID_BATCH_PLACE_ORDER_SIZE
'-4083': OperationFailed, # PLACE_BATCH_ORDERS_FAIL
'-4084': NotSupported, # UPCOMING_METHOD
'-4085': BadRequest, # INVALID_NOTIONAL_LIMIT_COEF
'-4086': BadRequest, # INVALID_PRICE_SPREAD_THRESHOLD
'-4087': PermissionDenied, # REDUCE_ONLY_ORDER_PERMISSION
'-4088': PermissionDenied, # NO_PLACE_ORDER_PERMISSION
'-4104': BadSymbol, # INVALID_CONTRACT_TYPE
'-4114': InvalidOrder, # INVALID_CLIENT_TRAN_ID_LEN
'-4115': DuplicateOrderId, # DUPLICATED_CLIENT_TRAN_ID
'-4118': InsufficientFunds, # REDUCE_ONLY_MARGIN_CHECK_FAILED
'-4131': InvalidOrder, # MARKET_ORDER_REJECT
'-4135': InvalidOrder, # INVALID_ACTIVATION_PRICE
'-4137': InvalidOrder, # QUANTITY_EXISTS_WITH_CLOSE_POSITION
'-4138': OperationRejected, # REDUCE_ONLY_MUST_BE_TRUE
'-4139': InvalidOrder, # ORDER_TYPE_CANNOT_BE_MKT
'-4140': OperationRejected, # INVALID_OPENING_POSITION_STATUS
'-4141': MarketClosed, # SYMBOL_ALREADY_CLOSED
'-4142': InvalidOrder, # STRATEGY_INVALID_TRIGGER_PRICE
'-4144': BadSymbol, # INVALID_PAIR
'-4161': OperationRejected, # ISOLATED_LEVERAGE_REJECT_WITH_POSITION
'-4164': InvalidOrder, # MIN_NOTIONAL
'-4165': BadRequest, # INVALID_TIME_INTERVAL
'-4183': InvalidOrder, # PRICE_HIGHTER_THAN_STOP_MULTIPLIER_UP
'-4184': InvalidOrder, # PRICE_LOWER_THAN_STOP_MULTIPLIER_DOWN
'-5060': OperationRejected, # {"code":-5060,"msg":"The limit order price does not meet the PERCENT_PRICE filter limit."}
'-5076': OperationRejected, # {"code":-5076,"msg":"Total order value should be more than 5 USDT"}
# occured errors:
'-4168': OperationRejected, # Unable to adjust to isolated-margin mode under the Multi-Assets mode.
},
'broad': {
},
},
})
def is_inverse(self, type: str, subType: Str = None) -> bool:
if subType is None:
return(type == 'delivery')
else:
return subType == 'inverse'
def is_linear(self, type: str, subType: Str = None) -> bool:
if subType is None:
return(type == 'future') or (type == 'swap')
else:
return subType == 'linear'
def fetch_currencies(self, params={}) -> Currencies:
"""
fetches all available currencies on an exchange
https://asterdex.github.io/aster-api-website/spot-v3/market-data/#trading-specification-information
https://asterdex.github.io/aster-api-website/futures-v3/market-data/#exchange-information
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: an associative dictionary of currencies
"""
sapiResult = self.sapiPublicGetV3ExchangeInfo(params)
sapiRows = self.safe_list(sapiResult, 'assets', [])
#
# [
# {
# "asset": "USDT",
# "marginAvailable": True, # only in PERP
# "autoAssetExchange": "-10000" # only in PERP
# }
# ]
#
return self.parse_currencies(sapiRows)
def parse_currency(self, rawCurrency: dict) -> Currency:
currencyId = self.safe_string(rawCurrency, 'asset')
code = self.safe_currency_code(currencyId)
return self.safe_currency_structure({
'info': rawCurrency,
'code': code,
'id': currencyId,
'name': code,
'active': None,
'deposit': None,
'withdraw': None,
'fee': None,
'precision': None,
'margin': self.safe_bool(rawCurrency, 'marginAvailable'),
'limits': {
'amount': {
'min': None,
'max': None,
},
'withdraw': {
'min': None,
'max': None,
},
'deposit': {
'min': None,
'max': None,
},
},
'networks': None,
'type': 'crypto', # atm exchange api provides only cryptos
})
def fetch_markets(self, params={}) -> List[Market]:
"""
retrieves data on all markets for bigone
https://asterdex.github.io/aster-api-website/spot-v3/market-data/#trading-specification-information
https://asterdex.github.io/aster-api-website/futures-v3/market-data/#exchange-information
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: an array of objects representing market data
"""
promises = [
self.sapiPublicGetV3ExchangeInfo(params),
self.fapiPublicGetV3ExchangeInfo(params),
]
promises.append(self.sign_in())
results = promises
sapiResult = self.safe_dict(results, 0, {})
sapiRows = self.safe_list(sapiResult, 'symbols', [])
fapiResult = self.safe_dict(results, 1, {})
fapiRows = self.safe_list(fapiResult, 'symbols', [])
#
# example:
#
# [
# {
# symbol: "TESTUSDT",
# status: "TRADING",
# baseAsset: "TEST",
# quoteAsset: "USDT",
# pricePrecision: "2",
# quantityPrecision: "5",
# baseAssetPrecision: "8",
# quotePrecision: "8",
# listingTime: "1756289680210", # only in SPOT
# baseAssetAddress: null, # only in SPOT
# ocoAllowed: False, # only in SPOT
# pair: "ASTERUSDT", # only in PERP
# contractType: "PERPETUAL", # only in PERP
# deliveryDate: "4133404800000", # only in PERP
# onboardDate: "1758178800000", # only in PERP
# maintMarginPercent: "12.5000", # only in PERP
# requiredMarginPercent: "25.0000", # only in PERP
# marginAsset: "USDT", # only in PERP
# underlyingType: "COIN", # only in PERP
# underlyingSubType: ["Top",], # only in PERP
# symbolType: "0", # only in PERP
# tradingMode: "0", # only in PERP
# name: "", # only in PERP
# channel: "{}", # only in PERP
# sequenceNo: "100", # only in PERP
# twapMinNotional: "1000", # only in PERP
# imn: "4000.00", # only in PERP
# tags: [], # only in PERP
# settlePlan: "0", # only in PERP
# triggerProtect: "0.1500", # only in PERP
# liquidationFee: "0.025000", # only in PERP
# marketTakeBound: "0.05", # only in PERP
# createTime: "1758215451058", # only in PERP
# filters: [
# {
# minPrice: "0.01",
# maxPrice: "1000000",
# filterType: "PRICE_FILTER",
# tickSize: "0.01",
# },
# {
# stepSize: "0.00001",
# filterType: "LOT_SIZE",
# maxQty: "9000",
# minQty: "0.00001",
# },
# {
# stepSize: "0.00001",
# filterType: "MARKET_LOT_SIZE",
# maxQty: "9000",
# minQty: "0.00001",
# },
# {
# limit: "200",
# filterType: "MAX_NUM_ORDERS",
# },
# {
# minNotional: "5",
# filterType: "MIN_NOTIONAL",
# },
# {
# minNotional: "5",
# avgPriceMins: "5",
# applyMinToMarket: True,
# filterType: "NOTIONAL", # only in SPOT
# applyMaxToMarket: True,
# },
# {
# multiplierDown: "0.2",
# multiplierUp: "5",
# multiplierDecimal: "1",
# filterType: "PERCENT_PRICE",
# },
# {
# bidMultiplierUp: "5",
# askMultiplierUp: "5",
# bidMultiplierDown: "0.2",
# avgPriceMins: "5",
# multiplierDecimal: "1",
# filterType: "PERCENT_PRICE_BY_SIDE", # only in SPOT
# askMultiplierDown: "0.2",
# },
# ],
# orderTypes: ["LIMIT", "MARKET", "STOP", "STOP_MARKET", "TAKE_PROFIT", "TAKE_PROFIT_MARKET", "TRAILING_STOP_MARKET",],
# timeInForce: ["GTC", "IOC", "FOK", "GTX", "HIDDEN",],
# }
# ]
#
#
fapiRowsFiltered = []
for i in range(0, len(fapiRows)):
market = fapiRows[i]
# tmp skip some markets with base = None
if self.safe_string(market, 'baseAsset'):
fapiRowsFiltered.append(market)
rows = self.array_concat(sapiRows, fapiRowsFiltered)
return self.parse_markets(rows)
def parse_market(self, market: dict) -> Market:
id = self.safe_string(market, 'symbol')
baseId = self.safe_string(market, 'baseAsset')
quoteId = self.safe_string(market, 'quoteAsset')
base = self.safe_currency_code(baseId)
quote = self.safe_currency_code(quoteId)
active = self.safe_string(market, 'status') == 'TRADING'
spot = None
symbol: Str = None
settle = None
settleId = None
swap = None
linear = None
inverse = None
contractSize = None
contractType = self.safe_string(market, 'contractType')
isContract = contractType is not None
if isContract:
# currently, there is only perpetuals, not futures
spot = False
swap = True
settleId = self.safe_string(market, 'marginAsset')
settle = self.safe_currency_code(settleId)
symbol = base + '/' + quote + ':' + settle
linear = settle == quote
inverse = settle == base
contractSize = self.safe_number_2(market, 'contractSize', 'unit', self.parse_number('1'))
else:
spot = True
swap = False
symbol = base + '/' + quote
# filters
filters = self.safe_list(market, 'filters', [])
filtersByType = self.index_by(filters, 'filterType')
filterNotional = self.safe_dict_2(filtersByType, 'MIN_NOTIONAL', 'NOTIONAL')
filterPrice = self.safe_dict(filtersByType, 'PRICE_FILTER')
filterLotSize = self.safe_dict(filtersByType, 'LOT_SIZE')
filterMarketLotSize = self.safe_dict(filtersByType, 'MARKET_LOT_SIZE', {})
pricePrecision = self.safe_number(filterPrice, 'tickSize')
if pricePrecision is None:
pricePrecision = self.parse_number(self.parse_precision(self.safe_string(market, 'pricePrecision')))
amountPrecision = self.safe_number(filterLotSize, 'stepSize') if (filterLotSize is not None) else self.parse_number(self.parse_precision(self.safe_string(market, 'quantityPrecision')))
return self.safe_market_structure({
'id': id,
'symbol': symbol,
'base': base,
'quote': quote,
'settle': settle,
'baseId': baseId,
'quoteId': quoteId,
'settleId': settleId,
'type': 'swap' if isContract else 'spot',
'spot': spot,
'margin': False,
'swap': swap,
'future': False,
'option': False,
'active': active,
'contract': isContract,
'linear': linear,
'inverse': inverse,
'taker': self.fees['trading']['taker'],
'maker': self.fees['trading']['maker'],
'contractSize': contractSize,
'expiry': None,
'expiryDatetime': None,
'strike': None,
'optionType': None,
'precision': {
'amount': amountPrecision,
'price': pricePrecision,
'base': self.parse_number(self.parse_precision(self.safe_string(market, 'baseAssetPrecision'))),
'quote': self.parse_number(self.parse_precision(self.safe_string(market, 'quotePrecision'))),
},
'limits': {
'leverage': {
'min': None,
'max': None,
},
'amount': {
'min': self.safe_number(filterLotSize, 'minQty'),
'max': self.safe_number(filterLotSize, 'maxQty'),
},
'price': {
'min': self.safe_number(filterPrice, 'minPrice'),
'max': self.safe_number(filterPrice, 'maxPrice'),
},
'cost': {
'min': self.safe_number_2(filterNotional, 'notional', 'minNotional'),
'max': None,
},
'market': {
'min': self.safe_number(filterMarketLotSize, 'minQty'),
'max': self.safe_number(filterMarketLotSize, 'maxQty'),
},
},
'created': self.safe_integer_2(market, 'listingTime', 'createTime'),
'info': market,
})
def fetch_time(self, params={}) -> Int:
"""
fetches the current integer timestamp in milliseconds from the exchange server
https://asterdex.github.io/aster-api-website/spot-v3/market-data/#get-server-time
https://asterdex.github.io/aster-api-website/futures-v3/market-data/#check-server-time
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns int: the current integer timestamp in milliseconds from the exchange server
"""
marketType = None
marketType, params = self.handle_market_type_and_params('fetchTime', None, params)
response = None
if marketType == 'swap':
response = self.fapiPublicGetV3Time(params)
else:
response = self.sapiPublicGetV3Time(params)
#
# both SPOT & PERP has same format
#
# {
# "serverTime": 1499827319559
# }
#
return self.safe_integer(response, 'serverTime')
def parse_ohlcv(self, ohlcv, market: Market = None) -> list:
#
# spot:
#
# [
# 1499040000000, # Open time
# "0.01634790", # Open
# "0.80000000", # High
# "0.01575800", # Low
# "0.01577100", # Close
# "148976.11427815", # Volume
# 1499644799999, # Close time
# "2434.19055334", # Quote asset volume
# 308, # Number of trades
# "1756.87402397", # Taker buy base asset volume
# "28.46694368", # Taker buy quote asset volume
# "0" # ??
# ]
#
return [
self.safe_integer(ohlcv, 0),
self.safe_number(ohlcv, 1),
self.safe_number(ohlcv, 2),
self.safe_number(ohlcv, 3),
self.safe_number(ohlcv, 4),
self.safe_number(ohlcv, 5),
]
def fetch_ohlcv(self, symbol: str, timeframe='1m', since: Int = None, limit: Int = None, params={}) -> List[list]:
"""
fetches historical candlestick data containing the open, high, low, and close price, and the volume of a market
https://asterdex.github.io/aster-api-website/spot-v3/market-data/#k-line-data
https://asterdex.github.io/aster-api-website/futures-v3/market-data/#klinecandlestick-data
https://asterdex.github.io/aster-api-website/futures-v3/market-data/#index-price-klinecandlestick-data
https://asterdex.github.io/aster-api-website/futures-v3/market-data/#mark-price-klinecandlestick-data
:param str symbol: unified symbol of the market to fetch OHLCV data for
:param str timeframe: the length of time each candle represents
:param int [since]: timestamp in ms of the earliest candle to fetch
:param int [limit]: the maximum amount of candles to fetch
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.price]: "mark" or "index" for mark price and index price candles
:param int [params.until]: the latest time in ms to fetch orders for
:returns int[][]: A list of candles ordered, open, high, low, close, volume
"""
self.load_markets()
market = self.market(symbol)
request: dict = {}
if since is not None:
request['startTime'] = since
if limit is not None:
request['limit'] = min(limit, 1500)
request, params = self.handle_until_option('endTime', request, params)
request['interval'] = self.safe_string(self.timeframes, timeframe, timeframe)
price = self.safe_string(params, 'price')
isMark = (price == 'mark')
isIndex = (price == 'index')
params = self.omit(params, 'price')
response = None
if isMark:
request['symbol'] = market['id']
response = self.fapiPublicGetV3MarkPriceKlines(self.extend(request, params))
elif isIndex:
request['pair'] = market['id']
response = self.fapiPublicGetV3IndexPriceKlines(self.extend(request, params))
else:
request['symbol'] = market['id']
if market['linear']:
response = self.fapiPublicGetV3Klines(self.extend(request, params))
else:
response = self.sapiPublicGetV3Klines(self.extend(request, params))
#
# both SPOT & PERP has same format
#
# [
# [
# 1499040000000, # Open time
# "0.01634790", # Open
# "0.80000000", # High
# "0.01575800", # Low
# "0.01577100", # Close
# "148976.11427815", # Volume
# 1499644799999, # Close time
# "2434.19055334", # Quote asset volume
# 308, # Number of trades
# "1756.87402397", # Taker buy base asset volume
# "28.46694368", # Taker buy quote asset volume,
# "0"
# ]
# ]
#
return self.parse_ohlcvs(response, market, timeframe, since, limit)
def parse_trade(self, trade: dict, market: Market = None) -> Trade:
#
# fetchTrades
#
# recent trades:
#
# {
# "id": 3913206,
# "price": "644.100",
# "qty": "0.08",
# "quoteQty": "51.528", # present in PERP
# "baseQty": "4.95049505", # present in SPOT
# "time": 1749784506633,
# "isBuyerMaker": True
# }
#
# aggrTrades
#
# {
# "a": 26129, # Aggregate tradeId
# "p": "0.01633102", # Price
# "q": "4.70443515", # Quantity
# "f": 27781, # First tradeId
# "l": 27781, # Last tradeId
# "T": 1498793709153, # Timestamp
# "m": True, # Was the buyer the maker?
# }
#
# fetchMyTrades (SPOT & PERP have similar format)
#
# {
# "symbol": "ETHUSDT",
# "id": 2583152,
# "orderId": 418588675,
# "side": "SELL",
# "price": "2330.04",
# "qty": "0.0030",
# "quoteQty": "6.99000000",
# "commission": "0.00279605",
# "commissionAsset": "USDT",
# "time": 1776409179230,
# "counterpartyId": 5143150, # only in SPOT
# "createUpdateId": null, # only in SPOT
# "maker": False, # only in SPOT
# "buyer": False, # only in SPOT
# "realizedPnl": "0.00029999", # only in PERP
# "marginAsset": "USDT", # only in PERP
# "positionSide": "BOTH", # only in PERP
# }
#
id = self.safe_string_2(trade, 'id', 'a')
marketId = self.safe_string(trade, 'symbol')
marketType = 'swap' if ('positionSide' in trade) else 'spot'
market = self.safe_market(marketId, market, None, marketType)
currencyId = self.safe_string_2(trade, 'commissionAsset', 'marginAsset')
currencyCode = self.safe_currency_code(currencyId)
amountString = self.safe_string_2(trade, 'qty', 'q')
priceString = self.safe_string_2(trade, 'price', 'p')
costString = self.safe_string_2(trade, 'quoteQty', 'baseQty')
timestamp = self.safe_integer_2(trade, 'time', 'T')
side = self.safe_string_lower(trade, 'side')
isMaker = self.safe_bool(trade, 'maker')
takerOrMaker = None
if isMaker is not None:
takerOrMaker = 'maker' if isMaker else 'taker'
if side is None:
isBuyer = self.safe_bool(trade, 'buyer')
if isBuyer is not None:
side = 'buy' if isBuyer else 'sell'
isBuyerMaker = self.safe_bool_2(trade, 'isBuyerMaker', 'm')
if isBuyerMaker is not None:
side = 'sell' if isBuyerMaker else 'buy'
return self.safe_trade({
'id': id,
'info': trade,
'timestamp': timestamp,
'datetime': self.iso8601(timestamp),
'symbol': market['symbol'],
'order': self.safe_string(trade, 'orderId'),
'type': None,
'side': side,
'takerOrMaker': takerOrMaker,
'price': priceString,
'amount': amountString,
'cost': costString,
'fee': {
'cost': self.parse_number(Precise.string_abs(self.safe_string(trade, 'commission'))),
'currency': currencyCode,
},
}, market)
def fetch_trades(self, symbol: str, since: Int = None, limit: Int = None, params={}) -> List[Trade]:
"""
get the list of most recent trades for a particular symbol
https://asterdex.github.io/aster-api-website/spot-v3/market-data/#recent-trades-list
https://asterdex.github.io/aster-api-website/spot-v3/market-data/#recent-trades-aggregated
https://asterdex.github.io/aster-api-website/futures-v3/market-data/#recent-trades-list
https://asterdex.github.io/aster-api-website/futures-v3/market-data/#compressedaggregate-trades-list
:param str symbol: unified symbol of the market to fetch trades for
:param int [since]: timestamp in ms of the earliest trade to fetch
:param int [limit]: the maximum amount of trades to fetch
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns Trade[]: a list of `trade structures <https://docs.ccxt.com/?id=public-trades>`
"""
self.load_markets()
market = self.market(symbol)
request: dict = {
'symbol': market['id'],
}
if limit is not None:
request['limit'] = min(limit, 1000)
response = None
sinceDefined = since is not None
untilDefined = ('until' in params)
if sinceDefined:
request['startTime'] = since
if untilDefined:
request = self.handle_until_option('endTime', request, params)
# use historical endpoint for targeted requests
if 'startTime' in request:
if market['swap']:
response = self.fapiPublicGetV3AggTrades(self.extend(request, params))
else:
response = self.sapiPublicGetV3AggTrades(self.extend(request, params))
#
# both FAPI and SAPI have same response format
#
# [
# {
# "a": 26129, # Aggregate tradeId
# "p": "0.01633102", # Price
# "q": "4.70443515", # Quantity
# "f": 27781, # First tradeId
# "l": 27781, # Last tradeId
# "T": 1498793709153, # Timestamp
# "m": True, # Was the buyer the maker?
# }
# ]
#
else:
if market['swap']:
response = self.fapiPublicGetV3Trades(self.extend(request, params))
else:
response = self.sapiPublicGetV3Trades(self.extend(request, params))
#
# SAPI & FAPI have only one field difference
#
# [
# {
# "id": "73620768",
# "price": "2324.07",
# "qty": "0.430",
# "quoteQty": "999.35", # only in PERP
# "baseQty": "4.95049505", # only in SPOT
# "time": "1776407252900",
# "isBuyerMaker": False
# }, ...
#
return self.parse_trades(response, market, since, limit)
def fetch_my_trades(self, symbol: Str = None, since: Int = None, limit: Int = None, params={}):
"""
fetch all trades made by the user
https://asterdex.github.io/aster-api-website/spot-v3/account%26trades/#account-trade-history-user_data
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#account-trade-list-user_data
:param str [symbol]: unified market symbol
:param int [since]: the earliest time in ms to fetch trades for
:param int [limit]: the maximum number of trades structures to retrieve
:param dict [params]: extra parameters specific to the exchange API endpoint
:param int [params.until]: timestamp in ms for the ending date filter, default is None
:returns dict[]: a list of `trade structures <https://docs.ccxt.com/?id=trade-structure>`
"""
self.load_markets_and_sign_in()
request: dict = {}
market = None
if symbol is not None:
market = self.market(symbol)
request['symbol'] = market['id']
marketType = None
marketType, params = self.handle_market_type_and_params('fetchTickers', market, params)
if since is not None:
request['startTime'] = since
if limit is not None:
request['limit'] = min(limit, 1000)
request, params = self.handle_until_option('endTime', request, params)
response = None
if marketType == 'swap':
response = self.fapiPrivateGetV3UserTrades(self.extend(request, params))
else:
response = self.sapiPrivateGetV3UserTrades(self.extend(request, params))
#
# SPOT & PERP have similar format
#
# {
# "symbol": "ETHUSDT",
# "id": 2583152,
# "orderId": 418588675,
# "side": "SELL",
# "price": "2330.04",
# "qty": "0.0030",
# "quoteQty": "6.99000000",
# "commission": "0.00279605",
# "commissionAsset": "USDT",
# "time": 1776409179230,
# "counterpartyId": 5143150, # only in PERP
# "createUpdateId": null, # only in PERP
# "maker": False, # only in PERP
# "buyer": False, # only in PERP
# "realizedPnl": "0.00029999", # only in SPOT
# "marginAsset": "USDT", # only in SPOT
# "positionSide": "BOTH", # only in SPOT
# }
#
return self.parse_trades(response, market, since, limit, params)
def fetch_order_book(self, symbol: str, limit: Int = None, params={}) -> OrderBook:
"""
fetches information on open orders with bid(buy) and ask(sell) prices, volumes and other data
https://asterdex.github.io/aster-api-website/spot-v3/market-data/#depth-information
https://asterdex.github.io/aster-api-website/futures-v3/market-data/#order-book
:param str symbol: unified symbol of the market to fetch the order book for
:param int [limit]: the maximum amount of order book entries to return
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: A dictionary of `order book structures <https://docs.ccxt.com/?id=order-book-structure>` indexed by market symbols
"""
self.load_markets()
market = self.market(symbol)
request: dict = {
'symbol': market['id'],
}
response = None
if limit is not None:
request['limit'] = self.find_nearest_ceiling([5, 10, 20, 50, 100, 500, 1000], limit)
if market['swap']:
response = self.fapiPublicGetV3Depth(self.extend(request, params))
else:
response = self.sapiPublicGetV3Depth(self.extend(request, params))
#
# both SPOT & PERP has same format
#
# {
# "lastUpdateId": 1027024,
# "E": 1589436922972, # Message output time
# "T": 1589436922959, # Transaction time
# "bids": [
# [
# "4.00000000", # PRICE
# "431.00000000" # QTY
# ]
# ],
# "asks": [
# [
# "4.00000200",
# "12.00000000"
# ]
# ]
# }
#
timestamp = self.safe_integer(response, 'T')
return self.parse_order_book(response, symbol, timestamp, 'bids', 'asks')
def parse_ticker(self, ticker: dict, market: Market = None) -> Ticker:
#
# fetchTicker & fetchTickers: both SPOT & PERP has similar format
#
# {
# "symbol": "ETHUSDT",
# "priceChange": "6.54",
# "priceChangePercent": "0.279",
# "weightedAvgPrice": "2330.70",
# "lastPrice": "2350.00",
# "lastQty": "4.437",
# "openPrice": "2343.46",
# "highPrice": "2363.20",
# "lowPrice": "2283.86",
# "volume": "267154.248",
# "quoteVolume": "622657018.70",
# "openTime": "1776329400000",
# "closeTime": "1776415832593",
# "firstId": "73520536",
# "lastId": "73630176",
# "count": "109640",
# "baseAsset": "BTC", # only in SPOT
# "quoteAsset": "USDT", # only in SPOT
# "bidPrice": "71125.98", # only in SPOT
# "bidQty": "0.00737", # only in SPOT
# "askPrice": "71152.10", # only in SPOT
# "askQty": "0.32399" # only in SPOT
# }
#
#
# fetchBidsAsks: SPOT & PERP have only one field difference
#
# [
# {
# "symbol": "BMTUSDT",
# "bidPrice": "0.004000",
# "bidQty": "1250.0",
# "askPrice": "0.000000",
# "askQty": "0.0",
# "time": "1776411276072",
# "lastUpdateId": "453174307613" # only in PERP
# }, ...
#
timestamp = self.safe_integer(ticker, 'closeTime')
last = self.safe_string(ticker, 'lastPrice')
open = self.safe_string(ticker, 'openPrice')
percentage = self.safe_string(ticker, 'priceChangePercent')
percentage = Precise.string_mul(percentage, '100')
quoteVolume = self.safe_string(ticker, 'quoteVolume')
baseVolume = self.safe_string(ticker, 'volume')
high = self.safe_string(ticker, 'highPrice')
low = self.safe_string(ticker, 'lowPrice')
isTickerResponse = ('priceChange' in ticker)
marketType: Str = None
if isTickerResponse:
marketType = 'spot' if ('baseAsset' in ticker) else 'swap'
else:
marketType = 'swap' if ('lastUpdateId' in ticker) else 'spot'
marketId = self.safe_string(ticker, 'symbol')
market = self.safe_market(marketId, market, None, marketType)
return self.safe_ticker({
'symbol': market['symbol'],
'timestamp': timestamp,
'datetime': self.iso8601(timestamp),
'high': high,
'low': low,
'bid': self.safe_string(ticker, 'bidPrice'),
'bidVolume': self.safe_string(ticker, 'bidQty'),
'ask': self.safe_string(ticker, 'askPrice'),
'askVolume': self.safe_string(ticker, 'askQty'),
'vwap': None,
'open': open,
'close': last,
'last': last,
'previousClose': None,
'change': None,
'percentage': percentage,
'average': None,
'baseVolume': baseVolume,
'quoteVolume': quoteVolume,
'markPrice': None,
'indexPrice': None,
'info': ticker,
}, market)
def fetch_ticker(self, symbol: str, params={}) -> Ticker:
"""
fetches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market
https://asterdex.github.io/aster-api-website/spot-v3/market-data/#24h-price-change
https://asterdex.github.io/aster-api-website/futures-v3/market-data/#24hr-ticker-price-change-statistics
:param str symbol: unified symbol of the market to fetch the ticker for
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: a `ticker structure <https://docs.ccxt.com/?id=ticker-structure>`
"""
self.load_markets()
market = self.market(symbol)
request: dict = {
'symbol': market['id'],
}
response = None
if market['swap']:
response = self.fapiPublicGetV3Ticker24hr(self.extend(request, params))
else:
response = self.sapiPublicGetV3Ticker24hr(self.extend(request, params))
#
# both SPOT & PERP has same format
#
# {
# "symbol": "ETHUSDT",
# "priceChange": "6.54",
# "priceChangePercent": "0.279",
# "weightedAvgPrice": "2330.70",
# "lastPrice": "2350.00",
# "lastQty": "4.437",
# "openPrice": "2343.46",
# "highPrice": "2363.20",
# "lowPrice": "2283.86",
# "volume": "267154.248",
# "quoteVolume": "622657018.70",
# "openTime": "1776329400000",
# "closeTime": "1776415832593",
# "firstId": "73520536",
# "lastId": "73630176",
# "count": "109640",
# "baseAsset": "BTC", # only in SPOT
# "quoteAsset": "USDT", # only in SPOT
# "bidPrice": "71125.98", # only in SPOT
# "bidQty": "0.00737", # only in SPOT
# "askPrice": "71152.10", # only in SPOT
# "askQty": "0.32399" # only in SPOT
# }
#
return self.parse_ticker(response, market)
def fetch_tickers(self, symbols: Strings = None, params={}) -> Tickers:
"""
fetches price tickers for multiple markets, statistical information calculated over the past 24 hours for each market
https://asterdex.github.io/aster-api-website/spot-v3/market-data/#24h-price-change
https://asterdex.github.io/aster-api-website/futures-v3/market-data/#24hr-ticker-price-change-statistics
:param str[] symbols: unified symbols of the markets to fetch the ticker for, all market tickers are returned if not assigned
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.subType]: "linear" or "inverse"
:param str [params.type]: 'spot', 'option', use params["subType"] for swap and future markets
:returns dict: an array of `ticker structures <https://docs.ccxt.com/?id=ticker-structure>`
"""
self.load_markets()
symbols = self.market_symbols(symbols, None, True, True, True)
market = self.get_market_from_symbols(symbols)
marketType = None
marketType, params = self.handle_market_type_and_params('fetchTickers', market, params)
response = None
if marketType == 'swap':
response = self.fapiPublicGetV3Ticker24hr(params)
elif marketType == 'spot':
response = self.sapiPublicGetV3Ticker24hr(params)
#
# [
# {
# "symbol": "BTCUSDT",
# "priceChange": "1845.7",
# "priceChangePercent": "1.755",
# "weightedAvgPrice": "105515.5",
# "lastPrice": "107037.7",
# "lastQty": "0.004",
# "openPrice": "105192.0",
# "highPrice": "107223.5",
# "lowPrice": "104431.6",
# "volume": "8753.286",
# "quoteVolume": "923607368.61",
# "openTime": 1749976620000,
# "closeTime": 1750063053754,
# "firstId": 24195078,
# "lastId": 24375783,
# "count": 180706,
# "baseAsset": "BTC", # only in SPOT
# "quoteAsset": "USDT", # only in SPOT
# "bidPrice": "71125.98", # only in SPOT
# "bidQty": "0.00737", # only in SPOT
# "askPrice": "71152.10", # only in SPOT
# "askQty": "0.32399" # only in SPOT
# }
# ]
#
return self.parse_tickers(response, symbols)
def fetch_last_prices(self, symbols: Strings = None, params={}):
"""
fetches the last price for multiple markets
https://asterdex.github.io/aster-api-website/spot-v3/market-data/#latest-price
https://asterdex.github.io/aster-api-website/futures-v3/market-data/#symbol-price-ticker
:param str[]|None symbols: unified symbols of the markets to fetch the last prices
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.subType]: "linear" or "inverse"
:returns dict: a dictionary of lastprices structures
"""
self.load_markets()
symbols = self.market_symbols(symbols, None, True, True, True)
market = self.get_market_from_symbols(symbols)
marketType = None
marketType, params = self.handle_market_type_and_params('fetchLastPrices', market, params)
response = None
if marketType == 'swap':
response = self.fapiPublicGetV3TickerPrice(params)
elif marketType == 'spot':
response = self.sapiPublicGetV3TickerPrice(params)
#
# both SPOT & SWAP has same format
#
# [
# {
# "symbol": "LTCBTC",
# "price": "4.00000200"
# "time": "1649666690902"
# },
# ...
# ]
#
results = []
for i in range(0, len(response)):
marketId = self.safe_string(response[i], 'symbol')
safeMarket = self.safe_market(marketId, None, None, marketType)
priceData = self.extend(self.parse_last_price(response[i], safeMarket), params)
results.append(priceData)
symbols = self.market_symbols(symbols)
return self.filter_by_array(results, 'symbol', symbols)
def parse_last_price(self, entry, market: Market = None):
#
# spot & swap
#
# {
# "symbol": "LTCBTC",
# "price": "4.00000200"
# "time": "1649666690902"
# }
#
timestamp = self.safe_integer(entry, 'time')
return {
'symbol': market['symbol'],
'timestamp': timestamp,
'datetime': self.iso8601(timestamp),
'price': self.safe_number_omit_zero(entry, 'price'),
'side': None,
'info': entry,
}
def fetch_bids_asks(self, symbols: Strings = None, params={}):
"""
fetches the bid and ask price and volume for multiple markets
https://asterdex.github.io/aster-api-website/spot-v3/market-data/#current-best-order
https://asterdex.github.io/aster-api-website/futures-v3/market-data/#symbol-order-book-ticker
:param str[]|None symbols: unified symbols of the markets to fetch the bids and asks for, all markets are returned if not assigned
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.subType]: "linear" or "inverse"
:returns dict: a dictionary of `ticker structures <https://docs.ccxt.com/?id=ticker-structure>`
"""
self.load_markets()
symbols = self.market_symbols(symbols, None, True, True, True)
market = self.get_market_from_symbols(symbols)
marketType = None
marketType, params = self.handle_market_type_and_params('fetchBidsAsks', market, params)
response = None
if marketType == 'swap':
response = self.fapiPublicGetV3TickerBookTicker(params)
elif marketType == 'spot':
response = self.sapiPublicGetV3TickerBookTicker(params)
#
# SPOT & PERP have only one field difference
#
# [
# {
# "symbol": "BMTUSDT",
# "bidPrice": "0.004000",
# "bidQty": "1250.0",
# "askPrice": "0.000000",
# "askQty": "0.0",
# "time": "1776411276072",
# "lastUpdateId": "453174307613" # only in PERP
# }, ...
#
return self.parse_tickers(response, symbols)
def parse_funding_rate(self, contract, market: Market = None) -> FundingRate:
#
# fundingRate
#
# {
# "symbol": "BTCUSDT",
# "markPrice": "106729.84047826",
# "indexPrice": "106775.72673913",
# "estimatedSettlePrice": "106708.84997006",
# "lastFundingRate": "0.00010000",
# "interestRate": "0.00010000",
# "nextFundingTime": 1750147200000,
# "time": 1750146970000
# }
#
# funding interval
#
# {
# "symbol": "INJUSDT",
# "interestRate": "0.00010000",
# "time": 1756197479000,
# "fundingIntervalHours": 8,
# "fundingFeeCap": 0.03,
# "fundingFeeFloor": -0.03
# }
#
marketId = self.safe_string(contract, 'symbol')
nextFundingTimestamp = self.safe_integer(contract, 'nextFundingTime')
timestamp = self.safe_integer(contract, 'time')
interval = self.safe_string(contract, 'fundingIntervalHours')
intervalString = None
if interval is not None:
intervalString = interval + 'h'
return {
'info': contract,
'symbol': self.safe_symbol(marketId, market, None, 'contract'),
'markPrice': self.safe_number(contract, 'markPrice'),
'indexPrice': self.safe_number(contract, 'indexPrice'),
'interestRate': self.safe_number(contract, 'interestRate'),
'estimatedSettlePrice': self.safe_number(contract, 'estimatedSettlePrice'),
'timestamp': timestamp,
'datetime': self.iso8601(timestamp),
'fundingRate': self.safe_number(contract, 'lastFundingRate'),
'fundingTimestamp': None,
'fundingDatetime': None,
'nextFundingRate': None,
'nextFundingTimestamp': nextFundingTimestamp,
'nextFundingDatetime': self.iso8601(nextFundingTimestamp),
'previousFundingRate': None,
'previousFundingTimestamp': None,
'previousFundingDatetime': None,
'interval': intervalString,
}
def fetch_funding_rate(self, symbol: str, params={}) -> FundingRate:
"""
fetch the current funding rate
https://asterdex.github.io/aster-api-website/futures-v3/market-data/#symbol-price-ticker
:param str symbol: unified market symbol
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: a `funding rate structure <https://docs.ccxt.com/?id=funding-rate-structure>`
"""
if symbol is None:
raise ArgumentsRequired(self.id + ' fetchFundingRate() requires a symbol argument')
self.load_markets()
market = self.market(symbol)
request: dict = {
'symbol': market['id'],
}
response = self.fapiPublicGetV3PremiumIndex(self.extend(request, params))
#
# {
# "symbol": "BTCUSDT",
# "markPrice": "106729.84047826",
# "indexPrice": "106775.72673913",
# "estimatedSettlePrice": "106708.84997006",
# "lastFundingRate": "0.00010000",
# "interestRate": "0.00010000",
# "nextFundingTime": 1750147200000,
# "time": 1750146970000
# }
#
return self.parse_funding_rate(response, market)
def fetch_funding_rates(self, symbols: Strings = None, params={}) -> FundingRates:
"""
fetch the current funding rate for multiple symbols
https://asterdex.github.io/aster-api-website/futures-v3/market-data/#symbol-price-ticker
:param str[] [symbols]: list of unified market symbols
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: a list of `funding rate structures <https://docs.ccxt.com/?id=funding-rate-structure>`
"""
self.load_markets()
symbols = self.market_symbols(symbols)
response = self.fapiPublicGetV3PremiumIndex(self.extend(params))
#
# [
# {
# "symbol": "BTCUSDT",
# "markPrice": "106729.84047826",
# "indexPrice": "106775.72673913",
# "estimatedSettlePrice": "106708.84997006",
# "lastFundingRate": "0.00010000",
# "interestRate": "0.00010000",
# "nextFundingTime": 1750147200000,
# "time": 1750146970000
# }
# ]
#
return self.parse_funding_rates(response, symbols)
def fetch_funding_intervals(self, symbols: Strings = None, params={}) -> FundingRates:
"""
fetch the funding rate interval for multiple markets
https://asterdex.github.io/aster-api-website/futures-v3/market-data/#get-funding-rate-config
:param str[] [symbols]: list of unified market symbols
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: a list of `funding rate structures <https://docs.ccxt.com/?id=funding-rate-structure>`
"""
self.load_markets()
if symbols is not None:
symbols = self.market_symbols(symbols)
response = self.fapiPublicGetV3FundingInfo(params)
#
# [
# {
# "symbol": "INJUSDT",
# "interestRate": "0.00010000",
# "time": 1756197479000,
# "fundingIntervalHours": 8,
# "fundingFeeCap": 0.03,
# "fundingFeeFloor": -0.03
# }
# ]
#
return self.parse_funding_rates(response, symbols)
def fetch_funding_rate_history(self, symbol: Str = None, since: Int = None, limit: Int = None, params={}):
"""
fetches historical funding rate prices
https://asterdex.github.io/aster-api-website/futures-v3/market-data/#get-funding-rate-history
:param str symbol: unified symbol of the market to fetch the funding rate history for
:param int [since]: timestamp in ms of the earliest funding rate to fetch
:param int [limit]: the maximum amount of `funding rate structures <https://docs.ccxt.com/?id=funding-rate-history-structure>` to fetch
:param dict [params]: extra parameters specific to the exchange API endpoint
:param int [params.until]: timestamp in ms of the latest funding rate
:returns dict[]: a list of `funding rate structures <https://docs.ccxt.com/?id=funding-rate-history-structure>`
"""
self.load_markets()
request: dict = {}
market: Market = None
if symbol is not None:
market = self.market(symbol)
request['symbol'] = market['id']
if since is not None:
request['startTime'] = since
if limit is not None:
request['limit'] = min(limit, 1000)
request, params = self.handle_until_option('endTime', request, params)
response = self.fapiPublicGetV3FundingRate(self.extend(request, params))
#
# [
# {
# "symbol": "BTCUSDT",
# "fundingTime": 1747209600000,
# "fundingRate": "0.00010000"
# }
# ]
#
return self.parse_funding_rate_histories(response, market)
def parse_funding_rate_history(self, contract, market: Market = None):
#
# {
# "symbol": "BTCUSDT",
# "fundingRate": "0.00063521",
# "fundingTime": "1621267200000",
# }
#
timestamp = self.safe_integer(contract, 'fundingTime')
return {
'info': contract,
'symbol': self.safe_symbol(self.safe_string(contract, 'symbol'), None, None, 'swap'),
'fundingRate': self.safe_number(contract, 'fundingRate'),
'timestamp': timestamp,
'datetime': self.iso8601(timestamp),
}
def fetch_balance(self, params={}) -> Balances:
"""
query for balance and get the amount of funds available for trading or funds locked in orders
https://asterdex.github.io/aster-api-website/spot-v3/account%26trades/#account-information-user_data
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#futures-account-balance-v3-user_data
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.subType]: "linear" or "inverse"
:param str [params.type]: 'spot', 'option', use params["subType"] for swap and future markets
:returns dict: a `balance structure <https://docs.ccxt.com/?id=balance-structure>`
"""
self.load_markets_and_sign_in()
marketType = None
marketType, params = self.handle_market_type_and_params('fetchBalance', None, params)
response = None
data = None
if marketType == 'swap':
data = self.fapiPrivateGetV3Balance(params)
#
# [
# {
# "accountAlias": "FzXquXsRFzXqAufW",
# "asset": "CDL",
# "balance": "0.00000000",
# "crossWalletBalance": "0.00000000",
# "crossUnPnl": "0.00000000",
# "availableBalance": "878.90500233",
# "maxWithdrawAmount": "0.00000000",
# "marginAvailable": True,
# "updateTime": "0"
# }, ...
#
elif marketType == 'spot':
response = self.sapiPrivateGetV3Account(params)
data = self.safe_list(response, 'balances', [])
#
# [
# {
# "asset": "BTC",
# "free": "4723846.89208129",
# "locked": "0.00000000"
# }
# ]
#
return self.parse_balance(data)
def parse_balance(self, response) -> Balances:
result: dict = {'info': response}
for i in range(0, len(response)):
balance = response[i]
currencyId = self.safe_string(balance, 'asset')
code = self.safe_currency_code(currencyId)
account = self.account()
account['free'] = self.safe_string_2(balance, 'free', 'availableBalance')
account['used'] = self.safe_string(balance, 'locked')
account['total'] = self.safe_string(balance, 'balance')
result[code] = account
return self.safe_balance(result)
def set_margin_mode(self, marginMode: str, symbol: Str = None, params={}):
"""
set margin mode to 'cross' or 'isolated'
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#change-margin-type-trade
:param str marginMode: 'cross' or 'isolated'
:param str symbol: unified market symbol
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: response from the exchange
"""
if symbol is None:
raise ArgumentsRequired(self.id + ' setMarginMode() requires a symbol argument')
marginMode = marginMode.upper()
if marginMode == 'CROSS':
marginMode = 'CROSSED'
if (marginMode != 'ISOLATED') and (marginMode != 'CROSSED'):
raise BadRequest(self.id + ' marginMode must be either isolated or cross')
self.load_markets_and_sign_in()
market = self.market(symbol)
request: dict = {
'symbol': market['id'],
'marginType': marginMode,
}
response = self.fapiPrivatePostV3MarginType(self.extend(request, params))
#
# {"code": 200,"msg": "success"}
#
return response
def fetch_position_mode(self, symbol: Str = None, params={}):
"""
fetchs the position mode, hedged or one way, hedged for aster is set identically for all linear markets or all inverse markets
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#get-current-position-modeuser_data
:param str symbol: unified symbol of the market to fetch the order book for
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: an object detailing whether the market is in hedged or one-way mode
"""
response = self.fapiPrivateGetV3PositionSideDual(params)
#
# {
# "dualSidePosition": True # "true": Hedge Mode; "false": One-way Mode
# }
#
return {
'info': response,
'hedged': self.safe_bool(response, 'dualSidePosition'),
}
def set_position_mode(self, hedged: bool, symbol: Str = None, params={}):
"""
set hedged to True or False for a market
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#change-position-modetrade
:param bool hedged: set to True to use dualSidePosition
:param str symbol: not used by bingx setPositionMode()
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: response from the exchange
"""
strValue = 'true' if hedged else 'false'
request: dict = {
'dualSidePosition': strValue,
}
#
# {
# "code": 200,
# "msg": "success"
# }
#
return self.fapiPrivatePostV3PositionSideDual(self.extend(request, params))
def parse_trading_fee(self, fee: dict, market: Market = None) -> TradingFeeInterface:
marketId = self.safe_string(fee, 'symbol')
market = self.safe_market(marketId, market)
symbol = self.safe_symbol(marketId, market)
return {
'info': fee,
'symbol': symbol,
'maker': self.safe_number(fee, 'makerCommissionRate'),
'taker': self.safe_number(fee, 'takerCommissionRate'),
'percentage': False,
'tierBased': False,
}
def fetch_trading_fee(self, symbol: str, params={}) -> TradingFeeInterface:
"""
fetch the trading fees for a market
https://asterdex.github.io/aster-api-website/spot-v3/market-data/#get-symbol-fees
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#user-commission-rate-user_data
:param str symbol: unified market symbol
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: a `fee structure <https://docs.ccxt.com/?id=fee-structure>`
"""
self.load_markets_and_sign_in()
market = self.market(symbol)
request: dict = {
'symbol': market['id'],
}
response = None
if market['swap']:
response = self.fapiPrivateGetV3CommissionRate(self.extend(request, params))
else:
response = self.sapiPrivateGetV3CommissionRate(self.extend(request, params))
#
# both SPOT & SWAP has same format
#
# {
# "symbol": "BTCUSDT",
# "makerCommissionRate": "0.0002",
# "takerCommissionRate": "0.0004"
# }
#
return self.parse_trading_fee(response, market)
def parse_order_status(self, status: Str):
statuses: dict = {
'NEW': 'open',
'PARTIALLY_FILLED': 'open',
'FILLED': 'closed',
'CANCELED': 'canceled',
'REJECTED': 'canceled',
'EXPIRED': 'canceled',
}
return self.safe_string(statuses, status, status)
def parse_order_type(self, type: Str):
types: dict = {
'LIMIT': 'limit',
'MARKET': 'market',
'STOP': 'limit',
'STOP_MARKET': 'market',
'TAKE_PROFIT': 'limit',
'TAKE_PROFIT_MARKET': 'market',
'TRAILING_STOP_MARKET': 'market',
}
return self.safe_string(types, type, type)
def parse_order(self, order: dict, market: Market = None) -> Order:
#
# swap
# {
# "avgPrice": "0.00000",
# "clientOrderId": "abc",
# "cumQuote": "0",
# "executedQty": "0",
# "orderId": 1917641,
# "origQty": "0.40",
# "origType": "TRAILING_STOP_MARKET",
# "price": "0",
# "reduceOnly": False,
# "side": "BUY",
# "positionSide": "SHORT",
# "status": "NEW",
# "stopPrice": "9300",
# "closePosition": False,
# "symbol": "BTCUSDT",
# "time": 1579276756075,
# "timeInForce": "GTC",
# "type": "TRAILING_STOP_MARKET",
# "activatePrice": "9020",
# "priceRate": "0.3",
# "updateTime": 1579276756075,
# "workingType": "CONTRACT_PRICE",
# "priceProtect": False
# }
#
# spot
#
# fetchOrders, fetchOpenOrders, fetchOpenOrder, fetchOrder, cancelOrder, createOrder
#
# {
# "orderId": "417594542",
# "symbol": "ETHUSDT",
# "status": "FILLED",
# "clientOrderId": "web_qnvMAhOJsiVbSyu0BdKG",
# "price": "0", # value set for unfilled
# "avgPrice": "2351.580000", # value zero for unfilled
# "origQty": "0.0054",
# "executedQty": "0.0054", # value zero for unfilled
# "cumQuote": "12.69853200", # value zero for unfilled
# "timeInForce": "GTC",
# "type": "MARKET",
# "side": "SELL",
# "stopPrice": "0",
# "origType": "MARKET",
# "time": "1776274219582",
# "updateTime": "1776274219609",
# "orderListId": "-1"
# }
#
info = order
marketId = self.safe_string(order, 'symbol')
market = self.safe_market(marketId, market)
side = self.safe_string_lower(order, 'side')
timestamp = self.safe_integer(order, 'time')
statusId = self.safe_string_upper(order, 'status')
rawType = self.safe_string_upper(order, 'type')
stopPriceString = self.safe_string(order, 'stopPrice')
triggerPrice = self.parse_number(self.omit_zero(stopPriceString))
return self.safe_order({
'info': info,
'id': self.safe_string(order, 'orderId'),
'clientOrderId': self.safe_string(order, 'clientOrderId'),
'symbol': self.safe_symbol(marketId, market),
'timestamp': timestamp,
'datetime': self.iso8601(timestamp),
'lastTradeTimestamp': None,
'lastUpdateTimestamp': self.safe_integer(order, 'updateTime'),
'type': self.parse_order_type(rawType),
'timeInForce': self.safe_string(order, 'timeInForce'),
'postOnly': None,
'side': side,
'price': self.safe_string(order, 'price'),
'triggerPrice': triggerPrice,
'average': self.safe_string(order, 'avgPrice'),
'cost': self.safe_string(order, 'cumQuote'),
'amount': self.safe_string(order, 'origQty'),
'filled': self.safe_string(order, 'executedQty'),
'remaining': None,
'status': self.parse_order_status(statusId),
'fee': None,
'trades': None,
'reduceOnly': self.safe_bool_2(order, 'reduceOnly', 'ro'),
}, market)
def fetch_order(self, id: str, symbol: Str = None, params={}):
"""
fetches information on an order made by the user
https://asterdex.github.io/aster-api-website/spot-v3/account%26trades/#query-order-user_data
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#query-order-user_data
:param str id: the order id
:param str symbol: unified symbol of the market the order was made in
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.clientOrderId]: a unique id for the order
:returns dict: an `order structure <https://docs.ccxt.com/?id=order-structure>`
"""
if symbol is None:
raise ArgumentsRequired(self.id + ' fetchOrder() requires a symbol argument')
self.load_markets_and_sign_in()
market = self.market(symbol)
request: dict = {
'symbol': market['id'],
}
clientOrderId = self.safe_string_2(params, 'clientOrderId', 'clientOid')
params = self.omit(params, ['clientOrderId', 'clientOid'])
if clientOrderId is not None:
request['origClientOrderId'] = clientOrderId
else:
request['orderId'] = id
response = None
if market['swap']:
response = self.fapiPrivateGetV3Order(self.extend(request, params))
else:
response = self.sapiPrivateGetV3Order(self.extend(request, params))
#
# SPOT & SWAP has similar formats
#
# {
# "orderId": "17338441758",
# "symbol": "ETHUSDT",
# "status": "FILLED",
# "clientOrderId": "727Wt3TIUgkUCxXp20E543",
# "price": "0",
# "avgPrice": "2304.56000",
# "origQty": "0.010",
# "executedQty": "0.010",
# "cumQuote": "23.04560",
# "timeInForce": "GTC",
# "type": "MARKET",
# "side": "BUY",
# "stopPrice": "0",
# "origType": "MARKET",
# "time": "1776800300736",
# "updateTime": "1776800300700",
# "orderListId": "-1" # only in SPOT
# "positionSide": "BOTH", # only in SWAP
# "reduceOnly": False, # only in SWAP
# "closePosition": False, # only in SWAP
# "workingType": "CONTRACT_PRICE", # only in SWAP
# "priceProtect": False, # only in SWAP
# "newChainData": {"hash": "0x46aed5...67bdbec8ba"} # only in SWAP
# }
#
return self.parse_order(response, market)
def fetch_open_order(self, id: str, symbol: Str = None, params={}):
"""
fetch an open order by the id
https://asterdex.github.io/aster-api-website/spot-v3/account%26trades/#query-current-open-order-user_data
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#query-current-open-order-user_data
:param str id: order id
:param str symbol: unified market symbol
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: an `order structure <https://docs.ccxt.com/?id=order-structure>`
"""
if symbol is None:
raise ArgumentsRequired(self.id + ' fetchOpenOrder() requires a symbol argument')
self.load_markets_and_sign_in()
market = self.market(symbol)
request: dict = {
'symbol': market['id'],
}
clientOrderId = self.safe_string_2(params, 'clientOrderId', 'clientOid')
params = self.omit(params, ['clientOrderId', 'clientOid'])
if clientOrderId is not None:
request['origClientOrderId'] = clientOrderId
else:
request['orderId'] = id
response = None
if market['spot']:
response = self.sapiPrivateGetV3OpenOrder(self.extend(request, params))
else:
response = self.fapiPrivateGetV3OpenOrder(self.extend(request, params))
#
# SPOT & SWAP has similar formats
#
# {
# "orderId": "17338441758",
# "symbol": "ETHUSDT",
# "status": "FILLED",
# "clientOrderId": "727Wt3TIUgkUCxXp20E543",
# "price": "0",
# "avgPrice": "2304.56000",
# "origQty": "0.010",
# "executedQty": "0.010",
# "cumQuote": "23.04560",
# "timeInForce": "GTC",
# "type": "MARKET",
# "side": "BUY",
# "stopPrice": "0",
# "origType": "MARKET",
# "time": "1776800300736",
# "updateTime": "1776800300700",
# "orderListId": "-1" # only in SPOT
# "positionSide": "BOTH", # only in SWAP
# "reduceOnly": False, # only in SWAP
# "closePosition": False, # only in SWAP
# "workingType": "CONTRACT_PRICE", # only in SWAP
# "priceProtect": False, # only in SWAP
# "newChainData": {"hash": "0x46aed5...67bdbec8ba"} # only in SWAP
# }
#
return self.parse_order(response, market)
def fetch_orders(self, symbol: Str = None, since: Int = None, limit: Int = None, params={}) -> List[Order]:
"""
fetches information on multiple orders made by the user
https://asterdex.github.io/aster-api-website/spot-v3/account%26trades/#query-all-orders-user_data
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#all-orders-user_data
:param str symbol: unified market symbol of the market orders were made in
:param int [since]: the earliest time in ms to fetch orders for
:param int [limit]: the maximum number of order structures to retrieve
:param dict [params]: extra parameters specific to the exchange API endpoint
:param int [params.until]: the latest time in ms to fetch orders for
:returns Order[]: a list of `order structures <https://docs.ccxt.com/?id=order-structure>`
"""
if symbol is None:
raise ArgumentsRequired(self.id + ' fetchOrders() requires a symbol argument')
self.load_markets_and_sign_in()
market = self.market(symbol)
request: dict = {
'symbol': market['id'],
}
if limit is not None:
request['limit'] = min(limit, 1000)
if since is not None:
request['startTime'] = since
request, params = self.handle_until_option('endTime', request, params)
response = None
if market['swap']:
response = self.fapiPrivateGetV3AllOrders(self.extend(request, params))
else:
response = self.sapiPrivateGetV3AllOrders(self.extend(request, params))
#
# SPOT & SWAP has similar responses
#
# [
# {
# "orderId": "417594542",
# "symbol": "ETHUSDT",
# "status": "FILLED",
# "clientOrderId": "web_qnvMAhOJsiVbSyu0BdKG",
# "price": "0", # value set for unfilled
# "avgPrice": "2351.580000", # value zero for unfilled
# "origQty": "0.0054",
# "executedQty": "0.0054", # value zero for unfilled
# "cumQuote": "12.69853200", # value zero for unfilled
# "timeInForce": "GTC",
# "type": "MARKET",
# "side": "SELL",
# "stopPrice": "0",
# "origType": "MARKET",
# "time": "1776274219582",
# "updateTime": "1776274219609",
# "orderListId": "-1", # only in SPOT
# "reduceOnly": False, # only in PERP
# "closePosition": False, # only in PERP
# "positionSide": "BOTH", # only in PERP
# "workingType": "CONTRACT_PRICE", # only in PERP
# "priceProtect": False, # only in PERP
# "newChainData": {"hash": "0xe17d3d5b...dbca8b01"} # only in PERP
# }, ...
#
return self.parse_orders(response, market, since, limit)
def fetch_open_orders(self, symbol: Str = None, since: Int = None, limit: Int = None, params={}) -> List[Order]:
"""
fetch all unfilled currently open orders
https://asterdex.github.io/aster-api-website/spot-v3/account%26trades/#current-open-orders-user_data
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#current-all-open-orders-user_data
:param str symbol: unified market symbol
:param int [since]: the earliest time in ms to fetch open orders for
:param int [limit]: the maximum number of open orders structures to retrieve
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.subType]: "linear" or "inverse"
:param str [params.type]: 'spot', 'option', use params["subType"] for swap and future markets
:returns Order[]: a list of `order structures <https://docs.ccxt.com/?id=order-structure>`
"""
self.load_markets_and_sign_in()
request: dict = {}
market = None
marketType = None
if symbol is not None:
market = self.market(symbol)
request['symbol'] = market['id']
if symbol is None:
if self.options['fetchOpenOrders']['warnIfNoSymbol']:
raise ExchangeError(self.id + ' fetchOpenOrders(): WARNING - self method without providing "symbol" argument uses 40 times more rate-limit quota. If you acknowledge self warning, set ' + self.id + '.options["fetchOpenOrders"]["warnIfNoSymbol"] = False to suppress self warning message.')
else:
market = self.market(symbol)
request['symbol'] = market['id']
marketType, params = self.handle_market_type_and_params('fetchOpenOrders', market, params)
subType = None
subType, params = self.handle_sub_type_and_params('fetchOpenOrders', market, params)
response = None
if self.is_linear(marketType, subType):
response = self.fapiPrivateGetV3OpenOrders(self.extend(request, params))
elif marketType == 'spot':
response = self.sapiPrivateGetV3OpenOrders(self.extend(request, params))
#
# SPOT & SWAP has similar responses
#
# [
# {
# "orderId": "17338239315",
# "symbol": "ETHUSDT",
# "status": "NEW",
# "clientOrderId": "web_AD_mbhgla7k15gptmwyr_x",
# "price": "2216.62",
# "avgPrice": "0",
# "origQty": "0.012",
# "executedQty": "0",
# "cumQuote": "0",
# "timeInForce": "GTC",
# "type": "LIMIT",
# "side": "BUY",
# "stopPrice": "0",
# "origType": "LIMIT",
# "time": "1776798208476",
# "updateTime": "1776798208450",
# "orderListId": "-1" # only in SPOT
# "reduceOnly": False, # only in PERP
# "closePosition": False, # only in PERP
# "positionSide": "BOTH", # only in PERP
# "workingType": "CONTRACT_PRICE", # only in PERP
# "priceProtect": False, # only in PERP
# "newChainData": {"hash": "0xf8a496....a7fd5"} # only in PERP
# }
# ]
#
return self.parse_orders(response, market, since, limit)
def create_order(self, symbol: str, type: OrderType, side: OrderSide, amount: float, price: Num = None, params={}):
"""
create a trade order
https://asterdex.github.io/aster-api-website/spot-v3/account%26trades/#place-order-trade
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#new-order-trade
:param str symbol: unified symbol of the market to create an order in
:param str type: 'market' or 'limit' or 'STOP' or 'STOP_MARKET' or 'TAKE_PROFIT' or 'TAKE_PROFIT_MARKET' or 'TRAILING_STOP_MARKET'
:param str side: 'buy' or 'sell'
:param float amount: how much of you want to trade in units of the base currency
:param float [price]: the price that the order is to be fulfilled, in units of the quote currency, ignored in market orders
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.reduceOnly]: for swap and future reduceOnly is a string 'true' or 'false' that cant be sent with close position set to True or in hedge mode. For spot margin and option reduceOnly is a boolean.
:param boolean [params.test]: whether to use the test endpoint or not, default is False
:param float [params.trailingPercent]: the percent to trail away from the current market price
:param float [params.trailingTriggerPrice]: the price to trigger a trailing order, default uses the price argument
:param str [params.positionSide]: "BOTH" for one-way mode, "LONG" for buy side of hedged mode, "SHORT" for sell side of hedged mode
:param float [params.triggerPrice]: the price that a trigger order is triggered at
:param float [params.stopLossPrice]: the price that a stop loss order is triggered at
:param float [params.takeProfitPrice]: the price that a take profit order is triggered at
:returns dict: an `order structure <https://docs.ccxt.com/?id=order-structure>`
"""
self.load_markets_and_sign_in()
market = self.market(symbol)
request = self.create_order_request(symbol, type, side, amount, price, params)
response = None
if market['swap']:
response = self.fapiPrivatePostV3Order(request)
else:
response = self.sapiPrivatePostV3Order(request)
#
# SPOT & SWAP has similar responses
#
# {
# "orderId": "17338441758",
# "symbol": "ETHUSDT",
# "status": "NEW",
# "clientOrderId": "727Wt3TIUgkUCxXp20E543",
# "price": "0",
# "avgPrice": "0.00000",
# "origQty": "0.010",
# "executedQty": "0",
# "cumQty": "0",
# "cumQuote": "0",
# "timeInForce": "GTC",
# "type": "MARKET",
# "side": "BUY",
# "stopPrice": "0",
# "origType": "MARKET",
# "time": "1776800300700",
# "updateTime": "1776800300700",
# "orderListId": "-1", # only in SPOT
# "workingType": "CONTRACT_PRICE", # only in PERP
# "positionSide": "BOTH", # only in PERP
# "reduceOnly": False, # only in PERP
# "closePosition": False, # only in PERP
# "priceProtect": False, # only in PERP
# "newChainData": {"hash": "0x46ae....c8ba"} # only in PERP
# }
#
return self.parse_order(response, market)
def create_orders(self, orders: List[OrderRequest], params={}):
"""
create a list of trade orders
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#new-order-trade
:param Array orders: list of orders to create, each object should contain the parameters required by createOrder, namely symbol, type, side, amount, price and params
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: an `order structure <https://docs.ccxt.com/?id=order-structure>`
"""
self.load_markets_and_sign_in()
ordersRequests = []
orderSymbols = []
if len(orders) > 5:
raise InvalidOrder(self.id + ' createOrders() order list max 5 orders')
for i in range(0, len(orders)):
rawOrder = orders[i]
marketId = self.safe_string(rawOrder, 'symbol')
currentMarket = self.market(marketId)
orderSymbols.append(currentMarket['symbol'])
type = self.safe_string(rawOrder, 'type')
side = self.safe_string(rawOrder, 'side')
amount = self.safe_value(rawOrder, 'amount')
price = self.safe_value(rawOrder, 'price')
orderParams = self.safe_dict(rawOrder, 'params', {})
orderRequest = self.create_order_request(marketId, type, side, amount, price, orderParams)
ordersRequests.append(orderRequest)
orderSymbols = self.market_symbols(orderSymbols, None, False, True, True)
market = self.market(orderSymbols[0])
if market['spot']:
raise NotSupported(self.id + ' createOrders() does not support ' + market['type'] + ' orders')
request: dict = {
'batchOrders': ordersRequests,
}
response = self.fapiPrivatePostV3BatchOrders(self.extend(request, params))
#
# [
# {
# "orderId": 17338699853,
# "symbol": "ETHUSDT",
# "status": "NEW",
# "clientOrderId": "NxMWPvOEyiF6TWh5UB8BQf0",
# "price": "0",
# "avgPrice": "0.00000",
# "origQty": "0.010",
# "executedQty": "0",
# "cumQty": "0",
# "cumQuote": "0",
# "timeInForce": "GTC",
# "type": "MARKET",
# "reduceOnly": False,
# "closePosition": False,
# "side": "BUY",
# "positionSide": "BOTH",
# "stopPrice": "0",
# "workingType": "CONTRACT_PRICE",
# "priceProtect": False,
# "origType": "MARKET",
# "updateTime": 1776802276050,
# "newChainData": {
# "hash": "0x5e569d9794cf726f72c2d000d401d20315e78e4df7b58023a489864624527dfe"
# }
# }
# ]
#
return self.parse_orders(response)
def create_order_request(self, symbol: str, type: OrderType, side: OrderSide, amount: float, price: Num = None, params={}):
"""
@ignore
helper function to build the request
:param str symbol: unified symbol of the market to create an order in
:param str type: 'market' or 'limit'
:param str side: 'buy' or 'sell'
:param float amount: how much you want to trade in units of the base currency
:param float [price]: the price that the order is to be fulfilled, in units of the quote currency, ignored in market orders
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: request to be sent to the exchange
"""
market = self.market(symbol)
initialUppercaseType = type.upper()
isMarketOrder = initialUppercaseType == 'MARKET'
isLimitOrder = initialUppercaseType == 'LIMIT'
request: dict = {
'symbol': market['id'],
'side': side.upper(),
}
clientOrderId = self.safe_string_2(params, 'newClientOrderId', 'clientOrderId')
if clientOrderId is not None:
request['newClientOrderId'] = clientOrderId
triggerPrice = self.safe_string_2(params, 'triggerPrice', 'stopPrice')
stopLossPrice = self.safe_string(params, 'stopLossPrice', triggerPrice)
takeProfitPrice = self.safe_string(params, 'takeProfitPrice')
trailingDelta = self.safe_string(params, 'trailingDelta')
trailingTriggerPrice = self.safe_string_2(params, 'trailingTriggerPrice', 'activationPrice')
trailingPercent = self.safe_string_n(params, ['trailingPercent', 'callbackRate', 'trailingDelta'])
isTrailingPercentOrder = trailingPercent is not None
isStopLoss = stopLossPrice is not None or trailingDelta is not None
isTakeProfit = takeProfitPrice is not None
uppercaseType = initialUppercaseType
stopPrice = None
if isTrailingPercentOrder:
if market['swap']:
uppercaseType = 'TRAILING_STOP_MARKET'
request['callbackRate'] = trailingPercent
if trailingTriggerPrice is not None:
request['activationPrice'] = self.price_to_precision(symbol, trailingTriggerPrice)
elif isStopLoss:
stopPrice = stopLossPrice
if isMarketOrder:
uppercaseType = 'STOP_MARKET'
elif isLimitOrder:
uppercaseType = 'STOP'
elif isTakeProfit:
stopPrice = takeProfitPrice
if isMarketOrder:
uppercaseType = 'TAKE_PROFIT_MARKET'
elif isLimitOrder:
uppercaseType = 'TAKE_PROFIT'
postOnly = self.is_post_only(isMarketOrder, None, params)
if postOnly:
request['timeInForce'] = 'GTX'
#
# spot
# LIMIT timeInForce, quantity, price
# MARKET quantity or quoteOrderQty
# STOP and TAKE_PROFIT quantity, price, stopPrice
# STOP_MARKET and TAKE_PROFIT_MARKET quantity, stopPrice
# future
# LIMIT timeInForce, quantity, price
# MARKET quantity
# STOP/TAKE_PROFIT quantity, price, stopPrice
# STOP_MARKET/TAKE_PROFIT_MARKET stopPrice
# TRAILING_STOP_MARKET callbackRate
#
# additional required fields depending on the order type
closePosition = self.safe_bool(params, 'closePosition', False)
timeInForceIsRequired = False
priceIsRequired = False
triggerPriceIsRequired = False
quantityIsRequired = False
request['type'] = uppercaseType
if uppercaseType == 'MARKET':
if market['spot']:
quoteOrderQty = self.safe_bool(self.options, 'quoteOrderQty', True)
if quoteOrderQty:
quoteOrderQtyNew = self.safe_string_2(params, 'quoteOrderQty', 'cost')
precision = market['precision']['price']
if quoteOrderQtyNew is not None:
request['quoteOrderQty'] = self.decimal_to_precision(quoteOrderQtyNew, TRUNCATE, precision, self.precisionMode)
elif price is not None:
amountString = self.number_to_string(amount)
priceString = self.number_to_string(price)
quoteOrderQuantity = Precise.string_mul(amountString, priceString)
request['quoteOrderQty'] = self.decimal_to_precision(quoteOrderQuantity, TRUNCATE, precision, self.precisionMode)
else:
quantityIsRequired = True
else:
quantityIsRequired = True
else:
quantityIsRequired = True
elif uppercaseType == 'LIMIT':
timeInForceIsRequired = True
quantityIsRequired = True
priceIsRequired = True
elif (uppercaseType == 'STOP') or (uppercaseType == 'TAKE_PROFIT'):
quantityIsRequired = True
priceIsRequired = True
triggerPriceIsRequired = True
elif (uppercaseType == 'STOP_MARKET') or (uppercaseType == 'TAKE_PROFIT_MARKET'):
if not closePosition:
quantityIsRequired = True
triggerPriceIsRequired = True
elif uppercaseType == 'TRAILING_STOP_MARKET':
request['callbackRate'] = trailingPercent
if trailingTriggerPrice is not None:
request['activationPrice'] = self.price_to_precision(symbol, trailingTriggerPrice)
if quantityIsRequired:
marketAmountPrecision = self.safe_string(market['precision'], 'amount')
isPrecisionAvailable = (marketAmountPrecision is not None)
if isPrecisionAvailable:
request['quantity'] = self.amount_to_precision(symbol, amount)
else:
request['quantity'] = self.parse_to_numeric(amount)
if priceIsRequired:
if price is None:
raise InvalidOrder(self.id + ' createOrder() requires a price argument for a ' + type + ' order')
pricePrecision = self.safe_string(market['precision'], 'price')
isPricePrecisionAvailable = (pricePrecision is not None)
if isPricePrecisionAvailable:
request['price'] = self.price_to_precision(symbol, price)
else:
request['price'] = self.parse_to_numeric(price)
if triggerPriceIsRequired:
if stopPrice is None:
raise InvalidOrder(self.id + ' createOrder() requires a stopPrice extra param for a ' + type + ' order')
if stopPrice is not None:
request['stopPrice'] = self.price_to_precision(symbol, stopPrice)
if timeInForceIsRequired and (self.safe_string(params, 'timeInForce') is None) and (self.safe_string(request, 'timeInForce') is None):
request['timeInForce'] = self.safe_string(self.options, 'defaultTimeInForce') # 'GTC' = Good To Cancel(default), 'IOC' = Immediate Or Cancel
requestParams = self.omit(params, ['newClientOrderId', 'clientOrderId', 'stopPrice', 'triggerPrice', 'trailingTriggerPrice', 'trailingPercent', 'trailingDelta', 'stopPrice', 'stopLossPrice', 'takeProfitPrice'])
if self.safe_bool(self.options, 'builderFee') and market['swap']:
request['builder'] = self.safe_string(self.options, 'builder')
request['feeRate'] = self.safe_string(self.options, 'builderRate')
return self.extend(request, requestParams)
def cancel_all_orders(self, symbol: Str = None, params={}):
"""
cancel all open orders in a market
https://asterdex.github.io/aster-api-website/spot-v3/account%26trades/#cancel-all-open-orders-trade
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#current-all-open-orders-user_data
:param str symbol: unified market symbol of the market to cancel orders in
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict[]: a list of `order structures <https://docs.ccxt.com/?id=order-structure>`
"""
if symbol is None:
raise ArgumentsRequired(self.id + ' cancelAllOrders() requires a symbol argument')
self.load_markets_and_sign_in()
market = self.market(symbol)
request: dict = {
'symbol': market['id'],
}
response = None
if market['swap']:
response = self.fapiPrivateDeleteV3AllOpenOrders(self.extend(request, params))
else:
response = self.sapiPrivateDeleteV3AllOpenOrders(self.extend(request, params))
#
# SPOT & SWAP has same response
#
# {
# "code": "200",
# "msg": "The operation of cancel all open order is done."
# }
#
return [
self.safe_order({
'info': response,
}),
]
def cancel_order(self, id: str, symbol: Str = None, params={}):
"""
cancels an open order
https://asterdex.github.io/aster-api-website/spot-v3/account%26trades/#cancel-order-trade
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#cancel-order-trade
:param str id: order id
:param str symbol: unified symbol of the market the order was made in
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: An `order structure <https://docs.ccxt.com/?id=order-structure>`
"""
if symbol is None:
raise ArgumentsRequired(self.id + ' cancelOrder() requires a symbol argument')
self.load_markets_and_sign_in()
market = self.market(symbol)
request: dict = {
'symbol': market['id'],
}
clientOrderId = self.safe_string_n(params, ['origClientOrderId', 'clientOrderId'])
if clientOrderId is not None:
request['origClientOrderId'] = clientOrderId
else:
request['orderId'] = id
params = self.omit(params, ['origClientOrderId', 'clientOrderId'])
response = None
if market['swap']:
response = self.fapiPrivateDeleteV3Order(self.extend(request, params))
else:
response = self.sapiPrivateDeleteV3Order(self.extend(request, params))
return self.parse_order(response, market)
def cancel_orders(self, ids: List[str], symbol: Str = None, params={}):
"""
cancel multiple orders
https://asterdex.github.io/aster-api-website/spot-v3/account%26trades/#cancel-all-open-orders-trade
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#cancel-multiple-orders-trade
:param str[] ids: order ids
:param str [symbol]: unified market symbol
:param dict [params]: extra parameters specific to the exchange API endpoint
EXCHANGE SPECIFIC PARAMETERS
:param str[] [params.origClientOrderIdList]: max length 10 e.g. ["my_id_1","my_id_2"], encode the double quotes. No space after comma
:param int[] [params.recvWindow]:
:returns dict: an list of `order structures <https://docs.ccxt.com/?id=order-structure>`
"""
if symbol is None:
raise ArgumentsRequired(self.id + ' cancelOrders() requires a symbol argument')
self.load_markets_and_sign_in()
market = self.market(symbol)
request: dict = {
'symbol': market['id'],
}
clientOrderIdList = self.safe_list(params, 'origClientOrderIdList')
if clientOrderIdList is not None:
request['origClientOrderIdList'] = clientOrderIdList
else:
request['orderIdList'] = ids
response = None
if market['swap']:
response = self.fapiPrivateDeleteV3BatchOrders(self.extend(request, params))
#
# [
# {
# "clientOrderId": "myOrder1",
# "cumQty": "0",
# "cumQuote": "0",
# "executedQty": "0",
# "orderId": 283194212,
# "origQty": "11",
# "origType": "TRAILING_STOP_MARKET",
# "price": "0",
# "reduceOnly": False,
# "side": "BUY",
# "positionSide": "SHORT",
# "status": "CANCELED",
# "stopPrice": "9300", # please ignore when order type is TRAILING_STOP_MARKET
# "closePosition": False, # if Close-All
# "symbol": "BTCUSDT",
# "timeInForce": "GTC",
# "type": "TRAILING_STOP_MARKET",
# "activatePrice": "9020", # activation price, only return with TRAILING_STOP_MARKET order
# "priceRate": "0.3", # callback rate, only return with TRAILING_STOP_MARKET order
# "updateTime": 1571110484038,
# "workingType": "CONTRACT_PRICE",
# "priceProtect": False, # if conditional order trigger is protected
# },
# {
# "code": -2011,
# "msg": "Unknown order sent."
# }
# ]
#
else:
response = self.sapiPrivateDeleteV3AllOpenOrders(self.extend(request, params))
#
# {"code": 200,"msg": "The operation of cancel all open order is done."}
#
return self.parse_orders(response, market)
def set_leverage(self, leverage: int, symbol: Str = None, params={}):
"""
set the level of leverage for a market
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#change-initial-leverage-trade
:param float leverage: the rate of leverage
:param str symbol: unified market symbol
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: response from the exchange
"""
if symbol is None:
raise ArgumentsRequired(self.id + ' setLeverage() requires a symbol argument')
if (leverage < 1) or (leverage > 125):
raise BadRequest(self.id + ' leverage should be between 1 and 125')
self.load_markets_and_sign_in()
market = self.market(symbol)
request: dict = {
'symbol': market['id'],
'leverage': leverage,
}
response = self.fapiPrivatePostV3Leverage(self.extend(request, params))
#
# {
# "leverage": 21,
# "maxNotionalValue": "1000000",
# "symbol": "BTCUSDT"
# }
#
return response
def fetch_leverages(self, symbols: Strings = None, params={}) -> Leverages:
"""
fetch the set leverage for all markets
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#position-information-v3-user_data
:param str[] [symbols]: a list of unified market symbols
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: a list of `leverage structures <https://docs.ccxt.com/?id=leverage-structure>`
"""
self.load_markets_and_sign_in()
response = self.fapiPrivateGetV3PositionRisk(params)
#
# [
# {
# "symbol": "INJUSDT",
# "positionAmt": "0.0",
# "entryPrice": "0.0",
# "markPrice": "0.00000000",
# "unRealizedProfit": "0.00000000",
# "liquidationPrice": "0",
# "leverage": "20",
# "maxNotionalValue": "25000",
# "marginType": "cross",
# "isolatedMargin": "0.00000000",
# "isAutoAddMargin": "false",
# "positionSide": "BOTH",
# "notional": "0",
# "isolatedWallet": "0",
# "updateTime": 0
# }
# ]
#
return self.parse_leverages(response, symbols, 'symbol')
def parse_leverage(self, leverage: dict, market: Market = None) -> Leverage:
#
# {
# "symbol": "INJUSDT",
# "positionAmt": "0.0",
# "entryPrice": "0.0",
# "markPrice": "0.00000000",
# "unRealizedProfit": "0.00000000",
# "liquidationPrice": "0",
# "leverage": "20",
# "maxNotionalValue": "25000",
# "marginType": "cross",
# "isolatedMargin": "0.00000000",
# "isAutoAddMargin": "false",
# "positionSide": "BOTH",
# "notional": "0",
# "isolatedWallet": "0",
# "updateTime": 0
# }
#
marketId = self.safe_string(leverage, 'symbol')
marginMode = self.safe_string_lower(leverage, 'marginType')
side = self.safe_string_lower(leverage, 'positionSide')
longLeverage = None
shortLeverage = None
leverageValue = self.safe_integer(leverage, 'leverage')
if (side is None) or (side == 'both'):
longLeverage = leverageValue
shortLeverage = leverageValue
elif side == 'long':
longLeverage = leverageValue
elif side == 'short':
shortLeverage = leverageValue
return {
'info': leverage,
'symbol': self.safe_symbol(marketId, market),
'marginMode': marginMode,
'longLeverage': longLeverage,
'shortLeverage': shortLeverage,
}
def fetch_margin_modes(self, symbols: Strings = None, params={}) -> MarginModes:
"""
fetches margin mode of the user
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#position-information-v3-user_data
:param str[] symbols: unified market symbols
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: a list of `margin mode structures <https://docs.ccxt.com/?id=margin-mode-structure>`
"""
self.load_markets_and_sign_in()
response = self.fapiPrivateGetV3PositionRisk(params)
#
#
# [
# {
# "symbol": "INJUSDT",
# "positionAmt": "0.0",
# "entryPrice": "0.0",
# "markPrice": "0.00000000",
# "unRealizedProfit": "0.00000000",
# "liquidationPrice": "0",
# "leverage": "20",
# "maxNotionalValue": "25000",
# "marginType": "cross",
# "isolatedMargin": "0.00000000",
# "isAutoAddMargin": "false",
# "positionSide": "BOTH",
# "notional": "0",
# "isolatedWallet": "0",
# "updateTime": 0
# }
# ]
#
#
return self.parse_margin_modes(response, symbols, 'symbol', 'swap')
def parse_margin_mode(self, marginMode: dict, market=None) -> MarginMode:
#
# {
# "symbol": "INJUSDT",
# "positionAmt": "0.0",
# "entryPrice": "0.0",
# "markPrice": "0.00000000",
# "unRealizedProfit": "0.00000000",
# "liquidationPrice": "0",
# "leverage": "20",
# "maxNotionalValue": "25000",
# "marginType": "cross",
# "isolatedMargin": "0.00000000",
# "isAutoAddMargin": "false",
# "positionSide": "BOTH",
# "notional": "0",
# "isolatedWallet": "0",
# "updateTime": 0
# }
#
marketId = self.safe_string(marginMode, 'symbol')
market = self.safe_market(marketId, market)
return {
'info': marginMode,
'symbol': market['symbol'],
'marginMode': self.safe_string_lower(marginMode, 'marginType'),
}
def fetch_margin_adjustment_history(self, symbol: Str = None, type: Str = None, since: Num = None, limit: Num = None, params={}) -> List[MarginModification]:
"""
fetches the history of margin added or reduced from contract isolated positions
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#get-position-margin-change-history-trade
:param str symbol: unified market symbol
:param str [type]: "add" or "reduce"
:param int [since]: timestamp in ms of the earliest change to fetch
:param int [limit]: the maximum amount of changes to fetch
:param dict params: extra parameters specific to the exchange api endpoint
:param int [params.until]: timestamp in ms of the latest change to fetch
:returns dict[]: a list of `margin structures <https://docs.ccxt.com/?id=margin-loan-structure>`
"""
if symbol is None:
raise ArgumentsRequired(self.id + ' fetchMarginAdjustmentHistory() requires a symbol argument')
self.load_markets_and_sign_in()
market = self.market(symbol)
until = self.safe_integer(params, 'until')
params = self.omit(params, 'until')
request: dict = {
'symbol': market['id'],
}
if type is not None:
request['type'] = 1 if (type == 'add') else 2
if limit is not None:
request['limit'] = min(limit, 1000)
if since is not None:
request['startTime'] = since
if until is not None:
request['endTime'] = until
response = self.fapiPrivateGetV3PositionMarginHistory(self.extend(request, params))
#
# [
# {
# "amount": "23.36332311",
# "asset": "USDT",
# "symbol": "BTCUSDT",
# "time": 1578047897183,
# "type": 1,
# "positionSide": "BOTH"
# }
# ]
#
modifications = self.parse_margin_modifications(response)
return self.filter_by_symbol_since_limit(modifications, symbol, since, limit)
def parse_margin_modification(self, data: dict, market: Market = None) -> MarginModification:
#
# {
# "amount": "100",
# "asset": "USDT",
# "symbol": "BTCUSDT",
# "time": 1578047900425,
# "type": 1,
# "positionSide": "LONG"
# }
#
# {
# "amount": 100.0,
# "code": 200,
# "msg": "Successfully modify position margin.",
# "type": 1
# }
#
rawType = self.safe_integer(data, 'type')
errorCode = self.safe_string(data, 'code')
marketId = self.safe_string(data, 'symbol')
timestamp = self.safe_integer(data, 'time')
market = self.safe_market(marketId, market, None, 'swap')
noErrorCode = errorCode is None
success = errorCode == '200'
return {
'info': data,
'symbol': market['symbol'],
'type': 'add' if (rawType == 1) else 'reduce',
'marginMode': 'isolated',
'amount': self.safe_number(data, 'amount'),
'code': self.safe_string(data, 'asset'),
'total': None,
'status': 'ok' if (success or noErrorCode) else 'failed',
'timestamp': timestamp,
'datetime': self.iso8601(timestamp),
}
def modify_margin_helper(self, symbol: str, amount, addOrReduce, params={}):
self.load_markets_and_sign_in()
market = self.market(symbol)
amount = self.amount_to_precision(symbol, amount)
request: dict = {
'type': addOrReduce,
'symbol': market['id'],
'amount': amount,
}
code = market['quote']
response = self.fapiPrivatePostV3PositionMargin(self.extend(request, params))
#
# {
# "amount": 100.0,
# "code": 200,
# "msg": "Successfully modify position margin.",
# "type": 1
# }
#
return self.extend(self.parse_margin_modification(response, market), {'code': code})
def reduce_margin(self, symbol: str, amount: float, params={}) -> MarginModification:
"""
remove margin from a position
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#modify-isolated-position-margin-trade
:param str symbol: unified market symbol
:param float amount: the amount of margin to remove
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: a `margin structure <https://docs.ccxt.com/?id=reduce-margin-structure>`
"""
return self.modify_margin_helper(symbol, amount, 2, params)
def add_margin(self, symbol: str, amount: float, params={}) -> MarginModification:
"""
add margin
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#modify-isolated-position-margin-trade
:param str symbol: unified market symbol
:param float amount: amount of margin to add
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: a `margin structure <https://docs.ccxt.com/?id=add-margin-structure>`
"""
return self.modify_margin_helper(symbol, amount, 1, params)
def parse_income(self, income, market: Market = None):
#
# {
# "symbol": "ETHUSDT",
# "incomeType": "FUNDING_FEE",
# "income": "0.00134317",
# "asset": "USDT",
# "time": "1621584000000",
# "info": "FUNDING_FEE",
# "tranId": "4480321991774044580",
# "tradeId": ""
# }
#
marketId = self.safe_string(income, 'symbol')
currencyId = self.safe_string(income, 'asset')
timestamp = self.safe_integer(income, 'time')
return {
'info': income,
'symbol': self.safe_symbol(marketId, market, None, 'swap'),
'code': self.safe_currency_code(currencyId),
'timestamp': timestamp,
'datetime': self.iso8601(timestamp),
'id': self.safe_string(income, 'tranId'),
'amount': self.safe_number(income, 'income'),
}
def fetch_funding_history(self, symbol: Str = None, since: Int = None, limit: Int = None, params={}):
"""
fetch the history of funding payments paid and received on self account
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#get-income-historyuser_data
:param str symbol: unified market symbol
:param int [since]: the earliest time in ms to fetch funding history for
:param int [limit]: the maximum number of funding history structures to retrieve
:param dict [params]: extra parameters specific to the exchange API endpoint
:param int [params.until]: timestamp in ms of the latest funding history entry
:param boolean [params.portfolioMargin]: set to True if you would like to fetch the funding history for a portfolio margin account
:param str [params.subType]: "linear" or "inverse"
:returns dict: a `funding history structure <https://docs.ccxt.com/?id=funding-history-structure>`
"""
self.load_markets_and_sign_in()
market = None
request: dict = {
'incomeType': 'FUNDING_FEE', # "TRANSFER""WELCOME_BONUS", "REALIZED_PNL""FUNDING_FEE", "COMMISSION", "INSURANCE_CLEAR", and "MARKET_MERCHANT_RETURN_REWARD"
}
if symbol is not None:
market = self.market(symbol)
request['symbol'] = market['id']
request, params = self.handle_until_option('endTime', request, params)
if since is not None:
request['startTime'] = since
if limit is not None:
request['limit'] = min(limit, 1000) # max 1000
response = self.fapiPrivateGetV3Income(self.extend(request, params))
return self.parse_incomes(response, market, since, limit)
def parse_ledger_entry(self, item: dict, currency: Currency = None) -> LedgerEntry:
#
# {
# "symbol": "",
# "incomeType": "TRANSFER",
# "income": "10.00000000",
# "asset": "USDT",
# "time": 1677645250000,
# "info": "TRANSFER",
# "tranId": 131001573082,
# "tradeId": ""
# }
#
amount = self.safe_string(item, 'income')
direction = None
if Precise.string_le(amount, '0'):
direction = 'out'
amount = Precise.string_mul('-1', amount)
else:
direction = 'in'
currencyId = self.safe_string(item, 'asset')
code = self.safe_currency_code(currencyId, currency)
currency = self.safe_currency(currencyId, currency)
timestamp = self.safe_integer(item, 'time')
type = self.safe_string(item, 'incomeType')
return self.safe_ledger_entry({
'info': item,
'id': self.safe_string(item, 'tranId'),
'direction': direction,
'account': None,
'referenceAccount': None,
'referenceId': self.safe_string(item, 'tradeId'),
'type': self.parse_ledger_entry_type(type),
'currency': code,
'amount': self.parse_number(amount),
'timestamp': timestamp,
'datetime': self.iso8601(timestamp),
'before': None,
'after': None,
'status': None,
'fee': None,
}, currency)
def parse_ledger_entry_type(self, type):
ledgerType: dict = {
'TRANSFER': 'transfer',
'WELCOME_BONUS': 'cashback',
'REALIZED_PNL': 'trade',
'FUNDING_FEE': 'fee',
'COMMISSION': 'commission',
'INSURANCE_CLEAR': 'settlement',
'MARKET_MERCHANT_RETURN_REWARD': 'cashback',
}
return self.safe_string(ledgerType, type, type)
def fetch_ledger(self, code: Str = None, since: Int = None, limit: Int = None, params={}) -> List[LedgerEntry]:
"""
fetch the history of changes, actions done by the user or operations that altered the balance of the user
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#get-income-historyuser_data
:param str [code]: unified currency code
:param int [since]: timestamp in ms of the earliest ledger entry
:param int [limit]: max number of ledger entries to return
:param dict [params]: extra parameters specific to the exchange API endpoint
:param int [params.until]: timestamp in ms of the latest ledger entry
:returns dict: a `ledger structure <https://docs.ccxt.com/?id=ledger>`
"""
self.load_markets_and_sign_in()
currency = None
if code is not None:
currency = self.currency(code)
request: dict = {}
if since is not None:
request['startTime'] = since
if limit is not None:
request['limit'] = min(limit, 1000) # max 1000
until = self.safe_integer(params, 'until')
if until is not None:
params = self.omit(params, 'until')
request['endTime'] = until
response = self.fapiPrivateGetV3Income(self.extend(request, params))
#
# [
# {
# "symbol": "",
# "incomeType": "TRANSFER",
# "income": "10.00000000",
# "asset": "USDT",
# "time": 1677645250000,
# "info": "TRANSFER",
# "tranId": 131001573082,
# "tradeId": ""
# }
# ]
#
return self.parse_ledger(response, currency, since, limit)
def parse_position_risk(self, position, market: Market = None):
#
# {
# "entryPrice": "6563.66500",
# "marginType": "isolated",
# "isAutoAddMargin": "false",
# "isolatedMargin": "15517.54150468",
# "leverage": "10",
# "liquidationPrice": "5930.78",
# "markPrice": "6679.50671178",
# "maxNotionalValue": "20000000",
# "positionSide": "LONG",
# "positionAmt": "20.000",
# "symbol": "BTCUSDT",
# "unRealizedProfit": "2316.83423560",
# "updateTime": 1625474304765
# }
#
marketId = self.safe_string(position, 'symbol')
market = self.safe_market(marketId, market, None, 'contract')
symbol = self.safe_string(market, 'symbol')
isolatedMarginString = self.safe_string(position, 'isolatedMargin')
leverageBrackets = self.safe_dict(self.options, 'leverageBrackets', {})
leverageBracket = self.safe_list(leverageBrackets, symbol, [])
notionalString = self.safe_string_2(position, 'notional', 'notionalValue')
notionalStringAbs = Precise.string_abs(notionalString)
maintenanceMarginPercentageString = None
for i in range(0, len(leverageBracket)):
bracket = leverageBracket[i]
if Precise.string_lt(notionalStringAbs, bracket[0]):
break
maintenanceMarginPercentageString = bracket[1]
notional = self.parse_number(notionalStringAbs)
contractsAbs = Precise.string_abs(self.safe_string(position, 'positionAmt'))
contracts = self.parse_number(contractsAbs)
unrealizedPnlString = self.safe_string(position, 'unRealizedProfit')
unrealizedPnl = self.parse_number(unrealizedPnlString)
liquidationPriceString = self.omit_zero(self.safe_string(position, 'liquidationPrice'))
liquidationPrice = self.parse_number(liquidationPriceString)
collateralString = None
marginMode = self.safe_string(position, 'marginType')
if marginMode is None and isolatedMarginString is not None:
marginMode = 'cross' if Precise.string_eq(isolatedMarginString, '0') else 'isolated'
side = None
if Precise.string_gt(notionalString, '0'):
side = 'long'
elif Precise.string_lt(notionalString, '0'):
side = 'short'
entryPriceString = self.safe_string(position, 'entryPrice')
entryPrice = self.parse_number(entryPriceString)
contractSize = self.safe_value(market, 'contractSize')
contractSizeString = self.number_to_string(contractSize)
# to notionalValue
linear = ('notional' in position)
if marginMode == 'cross':
# calculate collateral
precision = self.safe_dict(market, 'precision', {})
basePrecisionValue = self.safe_string(precision, 'base')
quotePrecisionValue = self.safe_string_2(precision, 'quote', 'price')
precisionIsUndefined = (basePrecisionValue is None) and (quotePrecisionValue is None)
if not precisionIsUndefined:
if linear:
# walletBalance = (liquidationPrice * (±1 + mmp) ± entryPrice) * contracts
onePlusMaintenanceMarginPercentageString = None
entryPriceSignString = entryPriceString
if side == 'short':
onePlusMaintenanceMarginPercentageString = Precise.string_add('1', maintenanceMarginPercentageString)
entryPriceSignString = Precise.string_mul('-1', entryPriceSignString)
else:
onePlusMaintenanceMarginPercentageString = Precise.string_add('-1', maintenanceMarginPercentageString)
inner = Precise.string_mul(liquidationPriceString, onePlusMaintenanceMarginPercentageString)
leftSide = Precise.string_add(inner, entryPriceSignString)
quotePrecision = self.precision_from_string(self.safe_string_2(precision, 'quote', 'price'))
if quotePrecision is not None:
collateralString = Precise.string_div(Precise.string_mul(leftSide, contractsAbs), '1', quotePrecision)
else:
# walletBalance = (contracts * contractSize) * (±1/entryPrice - (±1 - mmp) / liquidationPrice)
onePlusMaintenanceMarginPercentageString = None
entryPriceSignString = entryPriceString
if side == 'short':
onePlusMaintenanceMarginPercentageString = Precise.string_sub('1', maintenanceMarginPercentageString)
else:
onePlusMaintenanceMarginPercentageString = Precise.string_sub('-1', maintenanceMarginPercentageString)
entryPriceSignString = Precise.string_mul('-1', entryPriceSignString)
leftSide = Precise.string_mul(contractsAbs, contractSizeString)
rightSide = Precise.string_sub(Precise.string_div('1', entryPriceSignString), Precise.string_div(onePlusMaintenanceMarginPercentageString, liquidationPriceString))
basePrecision = self.precision_from_string(self.safe_string(precision, 'base'))
if basePrecision is not None:
collateralString = Precise.string_div(Precise.string_mul(leftSide, rightSide), '1', basePrecision)
else:
collateralString = self.safe_string(position, 'isolatedMargin')
collateralString = '0' if (collateralString is None) else collateralString
collateral = self.parse_number(collateralString)
markPrice = self.parse_number(self.omit_zero(self.safe_string(position, 'markPrice')))
timestamp = self.safe_integer(position, 'updateTime')
if timestamp == 0:
timestamp = None
maintenanceMarginPercentage = self.parse_number(maintenanceMarginPercentageString)
maintenanceMarginString = Precise.string_mul(maintenanceMarginPercentageString, notionalStringAbs)
if maintenanceMarginString is None:
# for a while, self new value was a backup to the existing calculations, but in future we might prioritize self
maintenanceMarginString = self.safe_string(position, 'maintMargin')
maintenanceMargin = self.parse_number(maintenanceMarginString)
initialMarginString = None
initialMarginPercentageString = None
leverageString = self.safe_string(position, 'leverage')
if leverageString is not None:
leverage = int(leverageString)
rational = self.is_round_number(1000 % leverage)
initialMarginPercentageString = Precise.string_div('1', leverageString, 8)
if not rational:
initialMarginPercentageString = Precise.string_add(initialMarginPercentageString, '1e-8')
unrounded = Precise.string_mul(notionalStringAbs, initialMarginPercentageString)
initialMarginString = Precise.string_div(unrounded, '1', 8)
else:
initialMarginString = self.safe_string(position, 'initialMargin')
unrounded = Precise.string_mul(initialMarginString, '1')
initialMarginPercentageString = Precise.string_div(unrounded, notionalStringAbs, 8)
marginRatio = None
percentage = None
if not Precise.string_equals(collateralString, '0'):
marginRatio = self.parse_number(Precise.string_div(Precise.string_add(Precise.string_div(maintenanceMarginString, collateralString), '5e-5'), '1', 4))
percentage = self.parse_number(Precise.string_mul(Precise.string_div(unrealizedPnlString, initialMarginString, 4), '100'))
positionSide = self.safe_string(position, 'positionSide')
hedged = positionSide != 'BOTH'
return self.safe_position({
'info': position,
'id': None,
'symbol': symbol,
'contracts': contracts,
'contractSize': contractSize,
'unrealizedPnl': unrealizedPnl,
'leverage': self.parse_number(leverageString),
'liquidationPrice': liquidationPrice,
'collateral': collateral,
'notional': notional,
'markPrice': markPrice,
'entryPrice': entryPrice,
'timestamp': timestamp,
'initialMargin': self.parse_number(initialMarginString),
'initialMarginPercentage': self.parse_number(initialMarginPercentageString),
'maintenanceMargin': maintenanceMargin,
'maintenanceMarginPercentage': maintenanceMarginPercentage,
'marginRatio': marginRatio,
'datetime': self.iso8601(timestamp),
'marginMode': marginMode,
'side': side,
'hedged': hedged,
'percentage': percentage,
'stopLossPrice': None,
'takeProfitPrice': None,
})
def fetch_positions_risk(self, symbols: Strings = None, params={}):
"""
fetch positions risk
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#position-information-v3-user_data
:param str[]|None symbols: list of unified market symbols
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: data on the positions risk
"""
if symbols is not None:
if not isinstance(symbols, list):
raise ArgumentsRequired(self.id + ' fetchPositionsRisk() requires an array argument for symbols')
self.load_markets_and_sign_in()
self.load_leverage_brackets(False, params)
request: dict = {}
response = self.fapiPrivateGetV3PositionRisk(self.extend(request, params))
#
# [
# {
# "entryPrice": "6563.66500",
# "marginType": "isolated",
# "isAutoAddMargin": "false",
# "isolatedMargin": "15517.54150468",
# "leverage": "10",
# "liquidationPrice": "5930.78",
# "markPrice": "6679.50671178",
# "maxNotionalValue": "20000000",
# "positionSide": "LONG",
# "positionAmt": "20.000", # negative value for 'SHORT'
# "symbol": "BTCUSDT",
# "unRealizedProfit": "2316.83423560",
# "updateTime": 1625474304765
# }
# ]
#
result = []
for i in range(0, len(response)):
rawPosition = response[i]
entryPriceString = self.safe_string(rawPosition, 'entryPrice')
if Precise.string_gt(entryPriceString, '0'):
result.append(self.parse_position_risk(response[i]))
symbols = self.market_symbols(symbols)
return self.filter_by_array_positions(result, 'symbol', symbols, False)
def fetch_positions(self, symbols: Strings = None, params={}) -> List[Position]:
"""
fetch all open positions
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#position-information-v3-user_data
:param str[] [symbols]: list of unified market symbols
:param dict [params]: extra parameters specific to the exchange API endpoint
:param str [params.method]: method name to call, "positionRisk", "account" or "option", default is "positionRisk"
:returns dict[]: a list of `position structure <https://docs.ccxt.com/?id=position-structure>`
"""
defaultMethod = None
defaultMethod, params = self.handle_option_and_params(params, 'fetchPositions', 'method')
if defaultMethod is None:
options = self.safe_dict(self.options, 'fetchPositions')
if options is None:
defaultMethod = self.safe_string(self.options, 'fetchPositions', 'positionRisk')
else:
defaultMethod = 'positionRisk'
if defaultMethod == 'positionRisk':
return self.fetch_positions_risk(symbols, params)
elif defaultMethod == 'account':
return self.fetch_account_positions(symbols, params)
else:
raise NotSupported(self.id + '.options["fetchPositions"]["method"] or params["method"] = "' + defaultMethod + '" is invalid, please choose between "account" and "positionRisk"')
def parse_account_positions(self, account, filterClosed=False):
positions = self.safe_list(account, 'positions')
assets = self.safe_list(account, 'assets', [])
balances: dict = {}
for i in range(0, len(assets)):
entry = assets[i]
currencyId = self.safe_string(entry, 'asset')
code = self.safe_currency_code(currencyId)
crossWalletBalance = self.safe_string(entry, 'crossWalletBalance')
crossUnPnl = self.safe_string(entry, 'crossUnPnl')
balances[code] = {
'crossMargin': Precise.string_add(crossWalletBalance, crossUnPnl),
'crossWalletBalance': crossWalletBalance,
}
result = []
for i in range(0, len(positions)):
position = positions[i]
marketId = self.safe_string(position, 'symbol')
market = self.safe_market(marketId, None, None, 'contract')
code = market['quote'] if market['linear'] else market['base']
maintenanceMargin = self.safe_string(position, 'maintMargin')
# check for maintenance margin so empty positions are not returned
isPositionOpen = (maintenanceMargin != '0') and (maintenanceMargin != '0.00000000')
if not filterClosed or isPositionOpen:
# sometimes not all the codes are correctly returned...
if code in balances:
parsed = self.parse_account_position(self.extend(position, {
'crossMargin': balances[code]['crossMargin'],
'crossWalletBalance': balances[code]['crossWalletBalance'],
}), market)
result.append(parsed)
return result
def parse_account_position(self, position, market: Market = None):
marketId = self.safe_string(position, 'symbol')
market = self.safe_market(marketId, market, None, 'contract')
symbol = self.safe_string(market, 'symbol')
leverageString = self.safe_string(position, 'leverage')
leverage = int(leverageString) if (leverageString is not None) else None
initialMarginString = self.safe_string(position, 'initialMargin')
initialMargin = self.parse_number(initialMarginString)
initialMarginPercentageString = None
if leverageString is not None:
initialMarginPercentageString = Precise.string_div('1', leverageString, 8)
rational = self.is_round_number(1000 % leverage)
if not rational:
initialMarginPercentageString = Precise.string_div(Precise.string_add(initialMarginPercentageString, '1e-8'), '1', 8)
# to notionalValue
usdm = ('notional' in position)
maintenanceMarginString = self.safe_string(position, 'maintMargin')
maintenanceMargin = self.parse_number(maintenanceMarginString)
entryPriceString = self.safe_string(position, 'entryPrice')
entryPrice = self.parse_number(entryPriceString)
notionalString = self.safe_string_2(position, 'notional', 'notionalValue')
notionalStringAbs = Precise.string_abs(notionalString)
notional = self.parse_number(notionalStringAbs)
contractsString = self.safe_string(position, 'positionAmt')
contractsStringAbs = Precise.string_abs(contractsString)
if contractsString is None:
entryNotional = Precise.string_mul(Precise.string_mul(leverageString, initialMarginString), entryPriceString)
contractSizeNew = self.safe_string(market, 'contractSize')
contractsString = Precise.string_div(entryNotional, contractSizeNew)
contractsStringAbs = Precise.string_div(Precise.string_add(contractsString, '0.5'), '1', 0)
contracts = self.parse_number(contractsStringAbs)
leverageBrackets = self.safe_dict(self.options, 'leverageBrackets', {})
leverageBracket = self.safe_list(leverageBrackets, symbol, [])
maintenanceMarginPercentageString = None
for i in range(0, len(leverageBracket)):
bracket = leverageBracket[i]
if Precise.string_lt(notionalStringAbs, bracket[0]):
break
maintenanceMarginPercentageString = bracket[1]
maintenanceMarginPercentage = self.parse_number(maintenanceMarginPercentageString)
unrealizedPnlString = self.safe_string(position, 'unrealizedProfit')
unrealizedPnl = self.parse_number(unrealizedPnlString)
timestamp = self.safe_integer(position, 'updateTime')
if timestamp == 0:
timestamp = None
isolated = self.safe_bool(position, 'isolated')
if isolated is None:
isolatedMarginRaw = self.safe_string(position, 'isolatedMargin')
isolated = not Precise.string_eq(isolatedMarginRaw, '0')
marginMode = None
collateralString = None
walletBalance = None
if isolated:
marginMode = 'isolated'
walletBalance = self.safe_string(position, 'isolatedWallet')
collateralString = Precise.string_add(walletBalance, unrealizedPnlString)
else:
marginMode = 'cross'
walletBalance = self.safe_string(position, 'crossWalletBalance')
collateralString = self.safe_string(position, 'crossMargin')
collateral = self.parse_number(collateralString)
marginRatio = None
side = None
percentage = None
liquidationPriceStringRaw = None
liquidationPrice = None
contractSize = self.safe_value(market, 'contractSize')
contractSizeString = self.number_to_string(contractSize)
if Precise.string_equals(notionalString, '0'):
entryPrice = None
else:
side = 'short' if Precise.string_lt(notionalString, '0') else 'long'
marginRatio = self.parse_number(Precise.string_div(Precise.string_add(Precise.string_div(maintenanceMarginString, collateralString), '5e-5'), '1', 4))
percentage = self.parse_number(Precise.string_mul(Precise.string_div(unrealizedPnlString, initialMarginString, 4), '100'))
if usdm:
# calculate liquidation price
#
# liquidationPrice = (walletBalance / (contracts * (±1 + mmp))) + (±entryPrice / (±1 + mmp))
#
# mmp = maintenanceMarginPercentage
# where ± is negative for long and positive for short
# TODO: calculate liquidation price for coinm contracts
onePlusMaintenanceMarginPercentageString = None
entryPriceSignString = entryPriceString
if side == 'short':
onePlusMaintenanceMarginPercentageString = Precise.string_add('1', maintenanceMarginPercentageString)
else:
onePlusMaintenanceMarginPercentageString = Precise.string_add('-1', maintenanceMarginPercentageString)
entryPriceSignString = Precise.string_mul('-1', entryPriceSignString)
leftSide = Precise.string_div(walletBalance, Precise.string_mul(contractsStringAbs, onePlusMaintenanceMarginPercentageString))
rightSide = Precise.string_div(entryPriceSignString, onePlusMaintenanceMarginPercentageString)
liquidationPriceStringRaw = Precise.string_add(leftSide, rightSide)
else:
# calculate liquidation price
#
# liquidationPrice = (contracts * contractSize(±1 - mmp)) / (±1/entryPrice * contracts * contractSize - walletBalance)
#
onePlusMaintenanceMarginPercentageString = None
entryPriceSignString = entryPriceString
if side == 'short':
onePlusMaintenanceMarginPercentageString = Precise.string_sub('1', maintenanceMarginPercentageString)
else:
onePlusMaintenanceMarginPercentageString = Precise.string_sub('-1', maintenanceMarginPercentageString)
entryPriceSignString = Precise.string_mul('-1', entryPriceSignString)
size = Precise.string_mul(contractsStringAbs, contractSizeString)
leftSide = Precise.string_mul(size, onePlusMaintenanceMarginPercentageString)
rightSide = Precise.string_sub(Precise.string_mul(Precise.string_div('1', entryPriceSignString), size), walletBalance)
liquidationPriceStringRaw = Precise.string_div(leftSide, rightSide)
pricePrecision = self.precision_from_string(self.safe_string(market['precision'], 'price'))
pricePrecisionPlusOne = pricePrecision + 1
pricePrecisionPlusOneString = str(pricePrecisionPlusOne)
# round half up
rounder = Precise('5e-' + pricePrecisionPlusOneString)
rounderString = str(rounder)
liquidationPriceRoundedString = Precise.string_add(rounderString, liquidationPriceStringRaw)
truncatedLiquidationPrice = Precise.string_div(liquidationPriceRoundedString, '1', pricePrecision)
if truncatedLiquidationPrice[0] == '-':
# user cannot be liquidated
# since he has more collateral than the size of the position
truncatedLiquidationPrice = None
liquidationPrice = self.parse_number(truncatedLiquidationPrice)
positionSide = self.safe_string(position, 'positionSide')
hedged = positionSide != 'BOTH'
return {
'info': position,
'id': None,
'symbol': symbol,
'timestamp': timestamp,
'datetime': self.iso8601(timestamp),
'initialMargin': initialMargin,
'initialMarginPercentage': self.parse_number(initialMarginPercentageString),
'maintenanceMargin': maintenanceMargin,
'maintenanceMarginPercentage': maintenanceMarginPercentage,
'entryPrice': entryPrice,
'notional': notional,
'leverage': self.parse_number(leverageString),
'unrealizedPnl': unrealizedPnl,
'contracts': contracts,
'contractSize': contractSize,
'marginRatio': marginRatio,
'liquidationPrice': liquidationPrice,
'markPrice': None,
'collateral': collateral,
'marginMode': marginMode,
'side': side,
'hedged': hedged,
'percentage': percentage,
}
def fetch_account_positions(self, symbols: Strings = None, params={}):
"""
@ignore
fetch account positions
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#position-information-v3-user_data
:param str[] [symbols]: list of unified market symbols
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: data on account positions
"""
if symbols is not None:
if not isinstance(symbols, list):
raise ArgumentsRequired(self.id + ' fetchPositions() requires an array argument for symbols')
self.load_markets_and_sign_in()
self.load_leverage_brackets(False, params)
response = self.fapiPrivateGetV4Account(params)
filterClosed = None
filterClosed, params = self.handle_option_and_params(params, 'fetchAccountPositions', 'filterClosed', False)
result = self.parse_account_positions(response, filterClosed)
symbols = self.market_symbols(symbols)
return self.filter_by_array_positions(result, 'symbol', symbols, False)
def load_leverage_brackets(self, reload=False, params={}):
self.load_markets_and_sign_in()
# by default cache the leverage bracket
# it contains useful stuff like the maintenance margin and initial margin for positions
leverageBrackets = self.safe_dict(self.options, 'leverageBrackets')
if (leverageBrackets is None) or (reload):
response = self.fapiPrivateGetV3LeverageBracket(params)
#
# [
# {
# "symbol": "TRUTHUSDT",
# "brackets": [
# {
# "bracket": "1",
# "initialLeverage": "50",
# "notionalCap": "5000",
# "notionalFloor": "0",
# "maintMarginRatio": "0.01",
# "cum": "0.0"
# },
# {
# "bracket": "2",
# "initialLeverage": "20",
# "notionalCap": "10000",
# "notionalFloor": "5000",
# "maintMarginRatio": "0.025",
# "cum": "75.0"
# },
# ...
#
self.options['leverageBrackets'] = self.create_safe_dictionary()
for i in range(0, len(response)):
entry = response[i]
marketId = self.safe_string(entry, 'symbol')
symbol = self.safe_symbol(marketId, None, None, 'contract')
brackets = self.safe_list(entry, 'brackets', [])
result = []
for j in range(0, len(brackets)):
bracket = brackets[j]
floorValue = self.safe_string(bracket, 'notionalFloor')
maintenanceMarginPercentage = self.safe_string(bracket, 'maintMarginRatio')
result.append([floorValue, maintenanceMarginPercentage])
self.options['leverageBrackets'][symbol] = result
return self.options['leverageBrackets']
def keccak_message(self, message):
return '0x' + self.hash(message, 'keccak', 'hex')
def sign_message(self, message, privateKey):
return self.sign_hash(self.keccak_message(message), privateKey[-64:])
def sign_withdraw_payload(self, withdrawPayload, network) -> str:
chainId = self.safe_integer(withdrawPayload, 'chainId')
domain: dict = {
'chainId': chainId,
'name': 'Aster',
'verifyingContract': self.safe_string(self.options, 'zeroAddress'),
'version': '1',
}
messageTypes: dict = {
'Action': [
{'name': 'type', 'type': 'string'},
{'name': 'destination', 'type': 'address'},
{'name': 'destination Chain', 'type': 'string'},
{'name': 'token', 'type': 'string'},
{'name': 'amount', 'type': 'string'},
{'name': 'fee', 'type': 'string'},
{'name': 'nonce', 'type': 'uint256'},
{'name': 'aster chain', 'type': 'string'},
],
}
request = {
'type': 'Withdraw',
'destination': self.safe_string(withdrawPayload, 'receiver'),
'destination Chain': network,
'token': self.safe_string(withdrawPayload, 'asset'),
'amount': self.safe_string(withdrawPayload, 'amount'),
'fee': self.safe_string(withdrawPayload, 'fee'),
'nonce': self.safe_integer(withdrawPayload, 'userNonce'),
'aster chain': 'Mainnet',
}
msg = self.eth_encode_structured_data(domain, messageTypes, request)
signature = self.sign_message(msg, self.privateKey)
return signature
def withdraw(self, code: str, amount: float, address: str, tag: Str = None, params={}) -> Transaction:
"""
make a withdrawal
https://asterdex.github.io/aster-api-website/spot-v3/account%26trades/#withdraw-user_data
https://asterdex.github.io/aster-api-website/futures-v3/deposit%26withdrawal/#withdraw-by-fapiv3-evm-futures
https://asterdex.github.io/aster-api-website/futures-v3/deposit%26withdrawal/#withdraw-by-fapiv3-evm-spot
:param str code: unified currency code
:param float amount: the amount to withdraw
:param str address: the address to withdraw to
:param str tag:
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: a `transaction structure <https://docs.ccxt.com/?id=transaction-structure>`
"""
tag, params = self.handle_withdraw_tag_and_params(tag, params)
self.check_address(address)
self.load_markets_and_sign_in()
currency = self.currency(code)
nonce = self.milliseconds() * 1000
request: dict = {
'asset': currency['id'],
'receiver': address,
'userNonce': str(nonce),
}
chainId = self.safe_integer(params, 'chainId')
# TODO: check how ARBI signature would work
networks = self.safe_dict(self.options, 'networks', {})
network = self.safe_string_upper(params, 'network')
network = self.safe_string(networks, network, network)
if (chainId is None) and (network is not None):
chainIds = self.safe_dict(self.options, 'networksToChainId', {})
chainId = self.safe_integer(chainIds, network)
if chainId is None:
raise ArgumentsRequired(self.id + ' withdraw require chainId or network parameter')
request['chainId'] = chainId
fee = self.safe_string(params, 'fee')
if fee is None:
raise ArgumentsRequired(self.id + ' withdraw require fee parameter')
request['fee'] = fee
params = self.omit(params, ['chainId', 'network', 'fee'])
request['amount'] = self.currency_to_precision(code, amount, network)
request['userSignature'] = self.sign_withdraw_payload(request, network)
response = self.sapiPrivatePostV3AsterUserWithdraw(self.extend(request, params))
#
# {
# "withdrawId": "1097219372504338432",
# "hash": "0x9e6baa3eb75d92a1164eef51a0cc97b9591930518ba3e8e5ab40ce524ba4e463"
# }
#
return self.parse_transaction(response, currency)
def parse_transaction(self, transaction, currency: Currency = None) -> Transaction:
return {
'info': transaction,
'id': self.safe_string(transaction, 'withdrawId'),
'txid': self.safe_string(transaction, 'hash'),
'timestamp': None,
'datetime': None,
'network': None,
'address': None,
'addressTo': None,
'addressFrom': None,
'tag': None,
'tagTo': None,
'tagFrom': None,
'type': 'withdrawal',
'amount': None,
'currency': None,
'status': None,
'updated': None,
'internal': None,
'comment': None,
'fee': None,
}
def transfer(self, code: str, amount: float, fromAccount: str, toAccount: str, params={}) -> TransferEntry:
"""
transfer currency internally between wallets on the same account
https://asterdex.github.io/aster-api-website/spot-v3/account%26trades/#perp-spot-transfer-trade
https://asterdex.github.io/aster-api-website/futures-v3/account%26trades/#transfer-between-futures-and-spot-transfer
:param str code: unified currency code
:param float amount: amount to transfer
:param str fromAccount: account to transfer from
:param str toAccount: account to transfer to
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns dict: a `transfer structure <https://docs.ccxt.com/?id=transfer-structure>`
"""
self.load_markets_and_sign_in()
currency = self.currency(code)
request: dict = {
'asset': currency['id'],
'amount': self.currency_to_precision(code, amount),
}
type = None
fromId = None
if fromAccount is not None:
fromId = self.convert_type_to_account(fromAccount).upper()
toId = None
if toAccount is not None:
toId = self.convert_type_to_account(toAccount).upper()
if fromId == 'SPOT' and toId == 'FUTURE':
type = 'SPOT_FUTURE'
elif fromId == 'FUTURE' and toId == 'SPOT':
type = 'FUTURE_SPOT'
if type is None:
raise ArgumentsRequired(self.id + ' transfer() requires fromAccount and toAccount parameters to be either SPOT or FUTURE')
response = None
defaultClientTranId = self.number_to_string(self.milliseconds())
clientTranId = self.safe_string(params, 'clientTranId', defaultClientTranId)
request['kindType'] = type
request['clientTranId'] = clientTranId
response = self.sapiPrivatePostV3AssetWalletTransfer(self.extend(request, params))
return self.parse_transfer(response, currency)
def parse_transfer(self, transfer: dict, currency: Currency = None) -> TransferEntry:
currencyId = self.safe_string(transfer, 'code')
return {
'info': transfer,
'id': self.safe_string(transfer, 'tranId'),
'timestamp': None,
'datetime': None,
'currency': self.safe_currency_code(currencyId, currency),
'amount': None,
'fromAccount': None,
'toAccount': None,
'status': self.parse_transfer_status(self.safe_string(transfer, 'status')),
}
def parse_transfer_status(self, status: Str) -> Str:
statuses: dict = {
'SUCCESS': 'ok',
}
return self.safe_string(statuses, status, status)
def hash_message(self, binaryMessage):
# binaryMessage = self.encode(message)
binaryMessageLength = self.binary_length(binaryMessage)
x19 = self.base16_to_binary('19')
newline = self.base16_to_binary('0a')
prefix = self.binary_concat(x19, self.encode('Ethereum Signed Message:'), newline, self.encode(self.number_to_string(binaryMessageLength)))
return '0x' + self.hash(self.binary_concat(prefix, binaryMessage), 'keccak', 'hex')
def sign_hash(self, hash, privateKey):
self.check_required_credentials()
signature = self.ecdsa(hash[-64:], privateKey[-64:], 'secp256k1', None)
r = signature['r']
s = signature['s']
v = self.int_to_base16(self.sum(27, signature['v']))
return '0x' + r.rjust(64, '0') + s.rjust(64, '0') + v
def sign(self, path, api='public', method='GET', params={}, headers=None, body=None):
url = self.implode_hostname(self.urls['api'][api]) + '/' + path
if api == 'fapiPublic' or api == 'sapiPublic':
if params:
url += '?' + self.rawencode(params)
elif api == 'fapiPrivate' or api == 'sapiPrivate':
self.check_required_credentials()
nonce = self.milliseconds() * 1000
# Sign using EIP-712 typed data per the AsterSignTransaction spec
zeroAddress = self.safe_string(self.options, 'zeroAddress', '0x0000000000000000000000000000000000000000')
v3ChainId = self.safe_integer(self.options, 'v3ChainId', 1666)
signerAddress = self.safe_string(self.options, 'signerAddress')
if signerAddress is None:
raise ArgumentsRequired(self.id + ' requires signerAddress in options when use v3 api')
domain = {
'name': 'AsterSignTransaction',
'version': '1',
'chainId': v3ChainId,
'verifyingContract': zeroAddress,
}
messageTypes: dict = {
'Message': [
{'name': 'msg', 'type': 'string'},
],
}
# Build v3 params: original endpoint params + nonce(macroseconds) + user + signer
# Note: timestamp and recvWindow are not used for v3; nonce replaces timestamp
finalParams = self.extend({
'nonce': str(nonce),
'user': self.walletAddress,
'signer': signerAddress,
}, params)
paramString: Str = None
paramsToEncode: dict = None
isApproveBuilder = (path.find('/approveBuilder') >= 0)
if isApproveBuilder:
# domain['name'] = 'Aster'
messageTypes = {
'ApproveBuilder': [
{'name': 'Builder', 'type': 'string'},
{'name': 'MaxFeeRate', 'type': 'string'},
{'name': 'BuilderName', 'type': 'string'},
{'name': 'AsterChain', 'type': 'string'},
{'name': 'User', 'type': 'string'},
{'name': 'Nonce', 'type': 'uint256'},
],
}
del finalParams['signer'] # signer is not needed for approveBuilder endpoint
paramString = self.encode_values_with_json(finalParams)
paramsToEncode = self.capitalize_keys(finalParams)
else:
paramString = self.encode_values_with_json(finalParams)
paramsToEncode = {'msg': paramString}
encodedMessage = self.eth_encode_structured_data(domain, messageTypes, paramsToEncode)
signature = self.sign_message(encodedMessage, self.privateKey)
queryString = paramString + '&' + 'signature=' + signature
if method == 'GET':
url += '?' + queryString
else:
headers = {}
headers['Content-Type'] = 'application/x-www-form-urlencoded'
body = queryString
return {'url': url, 'method': method, 'body': body, 'headers': headers}
def encode_values_with_json(self, values: dict) -> str:
encodedString = ''
keys = list(values.keys())
for i in range(0, len(keys)):
key = keys[i]
value = values[key]
isObj = isinstance(value, list) or self.is_dictionary(value)
valueJsonified = self.json(value) if isObj else str(value)
encoded = self.encode_uri_component(valueJsonified)
encodedString += key + '=' + encoded + '&'
return encodedString[0:-1]
def capitalize_keys(self, dict: dict) -> dict:
capitalized: dict = {}
keys = list(dict.keys())
for i in range(0, len(keys)):
key = keys[i]
value = dict[key]
capitalizedKey = self.capitalize(key)
capitalized[capitalizedKey] = value
return capitalized
def load_markets_and_sign_in(self):
[self.load_markets(), self.sign_in()]
def sign_in(self, params={}):
"""
sign in, must be called prior to using other authenticated methods
https://asterdex.github.io/aster-api-website/asterCode/integration-flow/
:param dict [params]: extra parameters specific to the exchange API endpoint
:returns: response from exchange
"""
if self.is_empty_string(self.privateKey):
if not self.is_empty_string(self.apiKey) or not self.is_empty_string(self.secret):
raise NotSupported(self.id + 'after the latest upgrade(v4.5.52), CCXT now expects the l1 private key to be provided in the credentials.')
return False
if len(self.privateKey) > 66:
raise NotSupported(self.id + ' after the latest update(v4.5.52), CCXT now expects the l1 private key to be provided in the credentials.')
self.initialize_client(params)
return True
def initialize_client(self, params={}):
builderFee = self.safe_bool(params, 'builderFee', self.safe_bool(self.options, 'builderFee', True)) # we shouldn't omit here
if not builderFee:
return False # skip if builder fee is not enabled
approvedBuilderFee = self.safe_bool(self.options, 'approvedBuilderFee', False)
if approvedBuilderFee:
return True # skip if builder fee is already approved
result = self.fapiPrivateGetV3Builder()
#
# [
# {
# "userAddress": "0x35a5B33Be664B09F78b5089eb6185f71c8a7f11f",
# "builderAddress": "0x1F5877C19e3777Cfd15F9d57253eA4aA5254Ec39",
# "maxFeeRate": "0.001",
# "builderName": "ccxt"
# }
# ]
#
approvedBuilders = result
length = len(approvedBuilders)
found = False
for i in range(0, length):
builderInfo = self.safe_dict(approvedBuilders, i, {})
builderAccountId = self.safe_string(builderInfo, 'builderAddress')
if builderAccountId == self.safe_string(self.options, 'builder'):
found = True
break
if not found:
self.options['approvedBuilderFee'] = True
try:
request: dict = {
'builder': self.safe_string(self.options, 'builder'),
'builderName': self.safe_string(self.options, 'builderName', 'ccxt'),
'maxFeeRate': self.safe_string(self.options, 'builderRate'),
'signatureChainId': self.safe_integer(self.options, 'v3ChainId', 1666),
'asterChain': 'Mainnet',
}
authResponse = self.fapiPrivatePostV3ApproveBuilder(self.extend(request, params))
#
# {"code": 200,"msg": "success"}
#
codeRes = self.safe_integer(authResponse, 'code')
if codeRes != 200:
raise ExchangeError('Builder authorization failed, ' + self.json(authResponse))
except Exception as e:
self.options['approvedBuilderFee'] = False
self.options['builderFee'] = False # disable if err
return None # just c#
def handle_errors(self, httpCode: int, reason: str, url: str, method: str, headers: dict, body: str, response, requestHeaders, requestBody):
if response is None:
return None # fallback to default error handler
#
# {
# "code": -1121,
# "msg": "Invalid symbol.",
# }
#
code = self.safe_string(response, 'code')
message = self.safe_string(response, 'msg')
if code is not None and code != '200':
feedback = self.id + ' ' + body
self.throw_exactly_matched_exception(self.exceptions['exact'], message, feedback)
self.throw_exactly_matched_exception(self.exceptions['exact'], code, feedback)
self.throw_broadly_matched_exception(self.exceptions['broad'], message, feedback)
raise ExchangeError(feedback) # unknown message
return None