ccxt
Version:
1,181 lines (1,179 loc) • 94.6 kB
JavaScript
// ----------------------------------------------------------------------------
// 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
// EDIT THE CORRESPONDENT .ts FILE INSTEAD
// ----------------------------------------------------------------------------
import Exchange from './abstract/coinbaseexchange.js';
import { InsufficientFunds, ArgumentsRequired, ExchangeError, InvalidOrder, InvalidAddress, AuthenticationError, OrderNotFound, OnMaintenance, PermissionDenied, RateLimitExceeded } from './base/errors.js';
import { Precise } from './base/Precise.js';
import { TICK_SIZE } from './base/functions/number.js';
import { sha256 } from './static_dependencies/noble-hashes/sha256.js';
// ----------------------------------------------------------------------------
/**
* @class coinbaseexchange
* @augments Exchange
*/
export default class coinbaseexchange extends Exchange {
describe() {
return this.deepExtend(super.describe(), {
'id': 'coinbaseexchange',
'name': 'Coinbase Exchange',
'countries': ['US'],
'rateLimit': 100,
'userAgent': this.userAgents['chrome'],
'pro': true,
'has': {
'CORS': true,
'spot': true,
'margin': false,
'swap': false,
'future': false,
'option': false,
'addMargin': false,
'borrowCrossMargin': false,
'borrowIsolatedMargin': false,
'borrowMargin': false,
'cancelAllOrders': true,
'cancelOrder': true,
'closeAllPositions': false,
'closePosition': false,
'createDepositAddress': true,
'createOrder': true,
'createOrderWithTakeProfitAndStopLoss': false,
'createOrderWithTakeProfitAndStopLossWs': false,
'createPostOnlyOrder': false,
'createReduceOnlyOrder': false,
'createStopLimitOrder': true,
'createStopMarketOrder': true,
'createStopOrder': true,
'fetchAccounts': true,
'fetchBalance': true,
'fetchBorrowInterest': false,
'fetchBorrowRate': false,
'fetchBorrowRateHistories': false,
'fetchBorrowRateHistory': false,
'fetchBorrowRates': false,
'fetchBorrowRatesPerSymbol': false,
'fetchClosedOrders': true,
'fetchCrossBorrowRate': false,
'fetchCrossBorrowRates': false,
'fetchCurrencies': true,
'fetchDepositAddress': false,
'fetchDeposits': true,
'fetchDepositsWithdrawals': true,
'fetchFundingHistory': false,
'fetchFundingInterval': false,
'fetchFundingIntervals': false,
'fetchFundingRate': false,
'fetchFundingRateHistory': false,
'fetchFundingRates': false,
'fetchGreeks': false,
'fetchIndexOHLCV': false,
'fetchIsolatedBorrowRate': false,
'fetchIsolatedBorrowRates': false,
'fetchIsolatedPositions': false,
'fetchLedger': true,
'fetchLeverage': false,
'fetchLeverages': false,
'fetchLeverageTiers': false,
'fetchLiquidations': false,
'fetchLongShortRatio': false,
'fetchLongShortRatioHistory': false,
'fetchMarginAdjustmentHistory': false,
'fetchMarginMode': false,
'fetchMarginModes': false,
'fetchMarketLeverageTiers': false,
'fetchMarkets': true,
'fetchMarkOHLCV': false,
'fetchMarkPrices': false,
'fetchMyLiquidations': false,
'fetchMySettlementHistory': false,
'fetchMyTrades': true,
'fetchOHLCV': true,
'fetchOpenInterest': false,
'fetchOpenInterestHistory': false,
'fetchOpenInterests': false,
'fetchOpenOrders': true,
'fetchOption': false,
'fetchOptionChain': false,
'fetchOrder': true,
'fetchOrderBook': true,
'fetchOrders': true,
'fetchOrderTrades': true,
'fetchPosition': false,
'fetchPositionHistory': false,
'fetchPositionMode': false,
'fetchPositions': false,
'fetchPositionsForSymbol': false,
'fetchPositionsHistory': false,
'fetchPositionsRisk': false,
'fetchPremiumIndexOHLCV': false,
'fetchSettlementHistory': false,
'fetchTicker': true,
'fetchTickers': true,
'fetchTime': true,
'fetchTrades': true,
'fetchTradingFee': false,
'fetchTradingFees': true,
'fetchTransactions': 'emulated',
'fetchVolatilityHistory': false,
'fetchWithdrawals': true,
'reduceMargin': false,
'repayCrossMargin': false,
'repayIsolatedMargin': false,
'repayMargin': false,
'setLeverage': false,
'setMargin': false,
'setMarginMode': false,
'setPositionMode': false,
'withdraw': true,
},
'timeframes': {
'1m': 60,
'5m': 300,
'15m': 900,
'1h': 3600,
'6h': 21600,
'1d': 86400,
},
'hostname': 'exchange.coinbase.com',
'urls': {
'test': {
'public': 'https://api-public.sandbox.exchange.coinbase.com',
'private': 'https://api-public.sandbox.exchange.coinbase.com',
},
'logo': 'https://github.com/ccxt/ccxt/assets/43336371/34a65553-88aa-4a38-a714-064bd228b97e',
'api': {
'public': 'https://api.{hostname}',
'private': 'https://api.{hostname}',
},
'www': 'https://coinbase.com/',
'doc': 'https://docs.cloud.coinbase.com/exchange/docs/',
'fees': [
'https://docs.pro.coinbase.com/#fees',
'https://support.pro.coinbase.com/customer/en/portal/articles/2945310-fees',
],
},
'requiredCredentials': {
'apiKey': true,
'secret': true,
'password': true,
},
'api': {
'public': {
'get': [
'currencies',
'products',
'products/{id}',
'products/{id}/book',
'products/{id}/candles',
'products/{id}/stats',
'products/{id}/ticker',
'products/{id}/trades',
'time',
'products/spark-lines',
'products/volume-summary',
],
},
'private': {
'get': [
'address-book',
'accounts',
'accounts/{id}',
'accounts/{id}/holds',
'accounts/{id}/ledger',
'accounts/{id}/transfers',
'coinbase-accounts',
'fills',
'funding',
'fees',
'margin/profile_information',
'margin/buying_power',
'margin/withdrawal_power',
'margin/withdrawal_power_all',
'margin/exit_plan',
'margin/liquidation_history',
'margin/position_refresh_amounts',
'margin/status',
'oracle',
'orders',
'orders/{id}',
'orders/client:{client_oid}',
'otc/orders',
'payment-methods',
'position',
'profiles',
'profiles/{id}',
'reports/{report_id}',
'transfers',
'transfers/{transfer_id}',
'users/self/exchange-limits',
'users/self/hold-balances',
'users/self/trailing-volume',
'withdrawals/fee-estimate',
'conversions/{conversion_id}',
'conversions/fees',
],
'post': [
'conversions',
'deposits/coinbase-account',
'deposits/payment-method',
'coinbase-accounts/{id}/addresses',
'funding/repay',
'orders',
'position/close',
'profiles/margin-transfer',
'profiles/transfer',
'reports',
'withdrawals/coinbase',
'withdrawals/coinbase-account',
'withdrawals/crypto',
'withdrawals/payment-method',
],
'delete': [
'orders',
'orders/client:{client_oid}',
'orders/{id}',
],
},
},
'commonCurrencies': {
'CGLD': 'CELO',
},
'precisionMode': TICK_SIZE,
'fees': {
'trading': {
'tierBased': true,
'percentage': true,
'maker': this.parseNumber('0.004'),
'taker': this.parseNumber('0.006'), // highest fee of all tiers
},
'funding': {
'tierBased': false,
'percentage': false,
'withdraw': {
'BCH': 0,
'BTC': 0,
'LTC': 0,
'ETH': 0,
'EUR': 0.15,
'USD': 25,
},
'deposit': {
'BCH': 0,
'BTC': 0,
'LTC': 0,
'ETH': 0,
'EUR': 0.15,
'USD': 10,
},
},
},
'features': {
'default': {
'sandbox': true,
'createOrder': {
'marginMode': true,
'triggerPrice': true,
'triggerPriceType': undefined,
'triggerDirection': false,
'stopLossPrice': false,
'takeProfitPrice': false,
'attachedStopLossTakeProfit': undefined,
'timeInForce': {
'IOC': true,
'FOK': true,
'PO': true,
'GTD': true,
},
'hedged': false,
'trailing': false,
'leverage': false,
'marketBuyByCost': false,
'marketBuyRequiresPrice': false,
'selfTradePrevention': false,
'iceberg': true, // todo: implement
},
'createOrders': undefined,
'fetchMyTrades': {
'marginMode': false,
'limit': 100,
'daysBack': 100000,
'untilDays': 100000,
'symbolRequired': true,
},
'fetchOrder': {
'marginMode': false,
'trigger': false,
'trailing': false,
'symbolRequired': false,
},
'fetchOpenOrders': {
'marginMode': false,
'limit': 100,
'trigger': false,
'trailing': false,
'symbolRequired': false,
},
'fetchOrders': {
'marginMode': false,
'limit': 100,
'daysBack': 100000,
'untilDays': 100000,
'trigger': false,
'trailing': false,
'symbolRequired': false,
},
'fetchClosedOrders': {
'marginMode': false,
'limit': 100,
'daysBack': 100000,
'daysBackCanceled': 1,
'untilDays': 100000,
'trigger': false,
'trailing': false,
'symbolRequired': false,
},
'fetchOHLCV': {
'limit': 300,
},
},
'spot': {
'extends': 'default',
},
'swap': {
'linear': undefined,
'inverse': undefined,
},
'future': {
'linear': undefined,
'inverse': undefined,
},
},
'options': {
'networks': {
'BTC': 'bitcoin',
// LIGHTNING unsupported
'ETH': 'ethereum',
// TRON unsupported
'SOL': 'solana',
// BSC unsupported
'ARBONE': 'arbitrum',
'AVAXC': 'avacchain',
'MATIC': 'polygon',
'BASE': 'base',
'SUI': 'sui',
'OP': 'optimism',
'NEAR': 'near',
// CRONOS unsupported
// GNO unsupported
'APT': 'aptos',
// SCROLL unsupported
'KAVA': 'kava',
// TAIKO unsupported
// BOB unsupported
// LINEA unsupported
'BLAST': 'blast',
'XLM': 'stellar',
// RSK unsupported
'SEI': 'sei',
// TON unsupported
// MANTLE unsupported
'ADA': 'cardano',
// HYPE unsupported
'CORE': 'coredao',
'ALGO': 'algorand',
// RUNE unsupported
'OSMO': 'osmosis',
// XIN unsupported
'CELO': 'celo',
'HBAR': 'hedera',
// FTM unsupported
// WEMIX unsupported
'ZKSYNC': 'zksync',
// KLAY unsupported
// HT unsupported
// FSN unsupported
// EOS unsupported, eosio?
// ACA unsupported
'STX': 'stacks',
'XTZ': 'tezos',
// NEO unsupported
// METIS unsupported
// TLOS unsupported
'EGLD': 'elrond',
// ASTR unsupported
// CFX unsupported
// GLMR unsupported
// CANTO unsupported
// SCRT unsupported
'LTC': 'litecoin',
// AURORA unsupported
// ONG unsupported
'ATOM': 'cosmos',
// CHZ unsupported
'FIL': 'filecoin',
'DOT': 'polkadot',
'DOGE': 'dogecoin',
// BRC20 unsupported
'XRP': 'ripple',
// XMR unsupported
'DASH': 'dash',
// akash, aleo, axelar, bitcoincash, berachain, deso, ethereumclassic, unichain, flow, flare, dfinity, story,kusama, mina, ronin, oasis, bittensor, celestia, noble, vara, vechain, zcash, horizen, zetachain
},
},
'exceptions': {
'exact': {
'Insufficient funds': InsufficientFunds,
'NotFound': OrderNotFound,
'Invalid API Key': AuthenticationError,
'invalid signature': AuthenticationError,
'Invalid Passphrase': AuthenticationError,
'Invalid order id': InvalidOrder,
'Private rate limit exceeded': RateLimitExceeded,
'Trading pair not available': PermissionDenied,
'Product not found': InvalidOrder,
},
'broad': {
'Order already done': OrderNotFound,
'order not found': OrderNotFound,
'price too small': InvalidOrder,
'price too precise': InvalidOrder,
'under maintenance': OnMaintenance,
'size is too small': InvalidOrder,
'Cancel only mode': OnMaintenance, // https://github.com/ccxt/ccxt/issues/7690
},
},
});
}
/**
* @method
* @name coinbaseexchange#fetchCurrencies
* @description fetches all available currencies on an exchange
* @see https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getcurrencies
* @param {object} [params] extra parameters specific to the exchange API endpoint
* @returns {object} an associative dictionary of currencies
*/
async fetchCurrencies(params = {}) {
const response = await this.publicGetCurrencies(params);
//
// {
// "id": "USDT",
// "name": "Tether",
// "min_size": "0.000001",
// "status": "online",
// "message": "",
// "max_precision": "0.000001",
// "convertible_to": [],
// "details": {
// "type": "crypto",
// "symbol": null,
// "network_confirmations": 14,
// "sort_order": 0,
// "crypto_address_link": "https://etherscan.io/token/0xdac17f958d2ee523a2206206994597c13d831ec7?a={{address}}",
// "crypto_transaction_link": "https://etherscan.io/tx/0x{{txId}}",
// "push_payment_methods": [],
// "group_types": [],
// "display_name": null,
// "processing_time_seconds": null,
// "min_withdrawal_amount": 0.000001,
// "max_withdrawal_amount": 20000000
// },
// "default_network": "ethereum",
// "supported_networks": [
// {
// "id": "ethereum",
// "name": "Ethereum",
// "status": "online",
// "contract_address": "0xdac17f958d2ee523a2206206994597c13d831ec7",
// "crypto_address_link": "https://etherscan.io/token/0xdac17f958d2ee523a2206206994597c13d831ec7?a={{address}}",
// "crypto_transaction_link": "https://etherscan.io/tx/0x{{txId}}",
// "min_withdrawal_amount": 0.000001,
// "max_withdrawal_amount": 20000000,
// "network_confirmations": 14,
// "processing_time_seconds": null
// }
// ],
// "display_name": "USDT"
// }
//
const result = {};
for (let i = 0; i < response.length; i++) {
const currency = response[i];
const id = this.safeString(currency, 'id');
const name = this.safeString(currency, 'name');
const code = this.safeCurrencyCode(id);
const details = this.safeDict(currency, 'details', {});
const networks = {};
const supportedNetworks = this.safeList(currency, 'supported_networks', []);
for (let j = 0; j < supportedNetworks.length; j++) {
const network = supportedNetworks[j];
const networkId = this.safeString(network, 'id');
const networkCode = this.networkIdToCode(networkId);
networks[networkCode] = {
'id': networkId,
'name': this.safeString(network, 'name'),
'network': networkCode,
'active': this.safeString(network, 'status') === 'online',
'withdraw': undefined,
'deposit': undefined,
'fee': undefined,
'precision': undefined,
'limits': {
'withdraw': {
'min': this.safeNumber(network, 'min_withdrawal_amount'),
'max': this.safeNumber(network, 'max_withdrawal_amount'),
},
},
'contract': this.safeString(network, 'contract_address'),
'info': network,
};
}
result[code] = this.safeCurrencyStructure({
'id': id,
'code': code,
'info': currency,
'type': this.safeString(details, 'type'),
'name': name,
'active': this.safeString(currency, 'status') === 'online',
'deposit': undefined,
'withdraw': undefined,
'fee': undefined,
'precision': this.safeNumber(currency, 'max_precision'),
'limits': {
'amount': {
'min': this.safeNumber(details, 'min_size'),
'max': undefined,
},
'withdraw': {
'min': this.safeNumber(details, 'min_withdrawal_amount'),
'max': this.safeNumber(details, 'max_withdrawal_amount'),
},
},
'networks': networks,
});
}
return result;
}
/**
* @method
* @name coinbaseexchange#fetchMarkets
* @description retrieves data on all markets for coinbaseexchange
* @see https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getproducts
* @param {object} [params] extra parameters specific to the exchange API endpoint
* @returns {object[]} an array of objects representing market data
*/
async fetchMarkets(params = {}) {
const response = await this.publicGetProducts(params);
//
// [
// {
// "id": "BTCAUCTION-USD",
// "base_currency": "BTC",
// "quote_currency": "USD",
// "base_min_size": "0.000016",
// "base_max_size": "1500",
// "quote_increment": "0.01",
// "base_increment": "0.00000001",
// "display_name": "BTCAUCTION/USD",
// "min_market_funds": "1",
// "max_market_funds": "20000000",
// "margin_enabled": false,
// "fx_stablecoin": false,
// "max_slippage_percentage": "0.02000000",
// "post_only": false,
// "limit_only": false,
// "cancel_only": true,
// "trading_disabled": false,
// "status": "online",
// "status_message": '',
// "auction_mode": false
// },
// {
// "id": "BTC-USD",
// "base_currency": "BTC",
// "quote_currency": "USD",
// "base_min_size": "0.000016",
// "base_max_size": "1500",
// "quote_increment": "0.01",
// "base_increment": "0.00000001",
// "display_name": "BTC/USD",
// "min_market_funds": "1",
// "max_market_funds": "20000000",
// "margin_enabled": false,
// "fx_stablecoin": false,
// "max_slippage_percentage": "0.02000000",
// "post_only": false,
// "limit_only": false,
// "cancel_only": false,
// "trading_disabled": false,
// "status": "online",
// "status_message": '',
// "auction_mode": false
// }
// ]
//
const result = [];
for (let i = 0; i < response.length; i++) {
const market = response[i];
const id = this.safeString(market, 'id');
const [baseId, quoteId] = id.split('-');
// BTCAUCTION-USD vs BTC-USD conflict workaround, see the output sample above
// const baseId = this.safeString (market, 'base_currency');
// const quoteId = this.safeString (market, 'quote_currency');
const base = this.safeCurrencyCode(baseId);
const quote = this.safeCurrencyCode(quoteId);
const status = this.safeString(market, 'status');
result.push(this.extend(this.fees['trading'], {
'id': id,
'symbol': base + '/' + quote,
'base': base,
'quote': quote,
'settle': undefined,
'baseId': baseId,
'quoteId': quoteId,
'settleId': undefined,
'type': 'spot',
'spot': true,
'margin': this.safeValue(market, 'margin_enabled'),
'swap': false,
'future': false,
'option': false,
'active': (status === 'online'),
'contract': false,
'linear': undefined,
'inverse': undefined,
'contractSize': undefined,
'expiry': undefined,
'expiryDatetime': undefined,
'strike': undefined,
'optionType': undefined,
'precision': {
'amount': this.safeNumber(market, 'base_increment'),
'price': this.safeNumber(market, 'quote_increment'),
},
'limits': {
'leverage': {
'min': undefined,
'max': undefined,
},
'amount': {
'min': undefined,
'max': undefined,
},
'price': {
'min': undefined,
'max': undefined,
},
'cost': {
'min': this.safeNumber(market, 'min_market_funds'),
'max': undefined,
},
},
'created': undefined,
'info': market,
}));
}
return result;
}
/**
* @method
* @name coinbaseexchange#fetchAccounts
* @description fetch all the accounts associated with a profile
* @see https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getaccounts
* @param {object} [params] extra parameters specific to the exchange API endpoint
* @returns {object} a dictionary of [account structures]{@link https://docs.ccxt.com/#/?id=account-structure} indexed by the account type
*/
async fetchAccounts(params = {}) {
await this.loadMarkets();
const response = await this.privateGetAccounts(params);
//
// [
// {
// "id": "4aac9c60-cbda-4396-9da4-4aa71e95fba0",
// "currency": "BTC",
// "balance": "0.0000000000000000",
// "available": "0",
// "hold": "0.0000000000000000",
// "profile_id": "b709263e-f42a-4c7d-949a-a95c83d065da"
// },
// {
// "id": "f75fa69a-1ad1-4a80-bd61-ee7faa6135a3",
// "currency": "USDC",
// "balance": "0.0000000000000000",
// "available": "0",
// "hold": "0.0000000000000000",
// "profile_id": "b709263e-f42a-4c7d-949a-a95c83d065da"
// },
// ]
//
return this.parseAccounts(response, params);
}
parseAccount(account) {
//
// {
// "id": "4aac9c60-cbda-4396-9da4-4aa71e95fba0",
// "currency": "BTC",
// "balance": "0.0000000000000000",
// "available": "0",
// "hold": "0.0000000000000000",
// "profile_id": "b709263e-f42a-4c7d-949a-a95c83d065da"
// }
//
const currencyId = this.safeString(account, 'currency');
return {
'id': this.safeString(account, 'id'),
'type': undefined,
'code': this.safeCurrencyCode(currencyId),
'info': account,
};
}
parseBalance(response) {
const result = { 'info': response };
for (let i = 0; i < response.length; i++) {
const balance = response[i];
const currencyId = this.safeString(balance, 'currency');
const code = this.safeCurrencyCode(currencyId);
const account = this.account();
account['free'] = this.safeString(balance, 'available');
account['used'] = this.safeString(balance, 'hold');
account['total'] = this.safeString(balance, 'balance');
result[code] = account;
}
return this.safeBalance(result);
}
/**
* @method
* @name coinbaseexchange#fetchBalance
* @description query for balance and get the amount of funds available for trading or funds locked in orders
* @see https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getaccounts
* @param {object} [params] extra parameters specific to the exchange API endpoint
* @returns {object} a [balance structure]{@link https://docs.ccxt.com/#/?id=balance-structure}
*/
async fetchBalance(params = {}) {
await this.loadMarkets();
const response = await this.privateGetAccounts(params);
return this.parseBalance(response);
}
/**
* @method
* @name coinbaseexchange#fetchOrderBook
* @see https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getproductbook
* @description fetches information on open orders with bid (buy) and ask (sell) prices, volumes and other data
* @param {string} 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 {object} [params] extra parameters specific to the exchange API endpoint
* @returns {object} A dictionary of [order book structures]{@link https://docs.ccxt.com/#/?id=order-book-structure} indexed by market symbols
*/
async fetchOrderBook(symbol, limit = undefined, params = {}) {
await this.loadMarkets();
// level 1 - only the best bid and ask
// level 2 - top 50 bids and asks (aggregated)
// level 3 - full order book (non aggregated)
const request = {
'id': this.marketId(symbol),
'level': 2, // 1 best bidask, 2 aggregated, 3 full
};
const response = await this.publicGetProductsIdBook(this.extend(request, params));
//
// {
// "sequence":1924393896,
// "bids":[
// ["0.01825","24.34811287",2],
// ["0.01824","72.5463",3],
// ["0.01823","424.54298049",6],
// ],
// "asks":[
// ["0.01826","171.10414904",4],
// ["0.01827","22.60427028",1],
// ["0.01828","397.46018784",7],
// ]
// }
//
const orderbook = this.parseOrderBook(response, symbol);
orderbook['nonce'] = this.safeInteger(response, 'sequence');
return orderbook;
}
parseTicker(ticker, market = undefined) {
//
// fetchTickers
//
// [
// 1639472400, // timestamp
// 4.26, // low
// 4.38, // high
// 4.35, // open
// 4.27 // close
// ]
//
// fetchTicker
//
// publicGetProductsIdTicker
//
// {
// "trade_id":843439,
// "price":"0.997999",
// "size":"80.29769",
// "time":"2020-01-28T02:13:33.012523Z",
// "bid":"0.997094",
// "ask":"0.998",
// "volume":"1903188.03750000"
// }
//
// publicGetProductsIdStats
//
// {
// "open": "34.19000000",
// "high": "95.70000000",
// "low": "7.06000000",
// "volume": "2.41000000"
// }
//
let timestamp = undefined;
let bid = undefined;
let ask = undefined;
let last = undefined;
let high = undefined;
let low = undefined;
let open = undefined;
let volume = undefined;
const symbol = (market === undefined) ? undefined : market['symbol'];
if (Array.isArray(ticker)) {
last = this.safeString(ticker, 4);
timestamp = this.milliseconds();
}
else {
timestamp = this.parse8601(this.safeValue(ticker, 'time'));
bid = this.safeString(ticker, 'bid');
ask = this.safeString(ticker, 'ask');
high = this.safeString(ticker, 'high');
low = this.safeString(ticker, 'low');
open = this.safeString(ticker, 'open');
last = this.safeString2(ticker, 'price', 'last');
volume = this.safeString(ticker, 'volume');
}
return this.safeTicker({
'symbol': symbol,
'timestamp': timestamp,
'datetime': this.iso8601(timestamp),
'high': high,
'low': low,
'bid': bid,
'bidVolume': undefined,
'ask': ask,
'askVolume': undefined,
'vwap': undefined,
'open': open,
'close': last,
'last': last,
'previousClose': undefined,
'change': undefined,
'percentage': undefined,
'average': undefined,
'baseVolume': volume,
'quoteVolume': undefined,
'info': ticker,
}, market);
}
/**
* @method
* @name coinbaseexchange#fetchTickers
* @description fetches price tickers for multiple markets, statistical information calculated over the past 24 hours for each market
* @see https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getproduct
* @param {string[]|undefined} symbols unified symbols of the markets to fetch the ticker for, all market tickers are returned if not assigned
* @param {object} [params] extra parameters specific to the exchange API endpoint
* @returns {object} a dictionary of [ticker structures]{@link https://docs.ccxt.com/#/?id=ticker-structure}
*/
async fetchTickers(symbols = undefined, params = {}) {
await this.loadMarkets();
symbols = this.marketSymbols(symbols);
const request = {};
const response = await this.publicGetProductsSparkLines(this.extend(request, params));
//
// {
// YYY-USD: [
// [
// 1639472400, // timestamp
// 4.26, // low
// 4.38, // high
// 4.35, // open
// 4.27 // close
// ],
// [
// 1639468800,
// 4.31,
// 4.45,
// 4.35,
// 4.35
// ],
// ]
// }
//
const result = {};
const marketIds = Object.keys(response);
const delimiter = '-';
for (let i = 0; i < marketIds.length; i++) {
const marketId = marketIds[i];
const entry = this.safeValue(response, marketId, []);
const first = this.safeValue(entry, 0, []);
const market = this.safeMarket(marketId, undefined, delimiter);
const symbol = market['symbol'];
result[symbol] = this.parseTicker(first, market);
}
return this.filterByArrayTickers(result, 'symbol', symbols);
}
/**
* @method
* @name coinbaseexchange#fetchTicker
* @see https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getproductticker
* @description fetches a price ticker, a statistical calculation with the information calculated over the past 24 hours for a specific market
* @param {string} symbol unified symbol of the market to fetch the ticker for
* @param {object} [params] extra parameters specific to the exchange API endpoint
* @returns {object} a [ticker structure]{@link https://docs.ccxt.com/#/?id=ticker-structure}
*/
async fetchTicker(symbol, params = {}) {
await this.loadMarkets();
const market = this.market(symbol);
const request = {
'id': market['id'],
};
// publicGetProductsIdTicker or publicGetProductsIdStats
const method = this.safeString(this.options, 'fetchTickerMethod', 'publicGetProductsIdTicker');
const response = await this[method](this.extend(request, params));
//
// publicGetProductsIdTicker
//
// {
// "trade_id":843439,
// "price":"0.997999",
// "size":"80.29769",
// "time":"2020-01-28T02:13:33.012523Z",
// "bid":"0.997094",
// "ask":"0.998",
// "volume":"1903188.03750000"
// }
//
// publicGetProductsIdStats
//
// {
// "open": "34.19000000",
// "high": "95.70000000",
// "low": "7.06000000",
// "volume": "2.41000000"
// }
//
return this.parseTicker(response, market);
}
parseTrade(trade, market = undefined) {
//
// {
// "type": "match",
// "trade_id": 82047307,
// "maker_order_id": "0f358725-2134-435e-be11-753912a326e0",
// "taker_order_id": "252b7002-87a3-425c-ac73-f5b9e23f3caf",
// "order_id": "d50ec984-77a8-460a-b958-66f114b0de9b",
// "side": "sell",
// "size": "0.00513192",
// "price": "9314.78",
// "product_id": "BTC-USD",
// "profile_id": "6244401d-c078-40d9-b305-7ad3551bc3b0",
// "sequence": 12038915443,
// "time": "2020-01-31T20:03:41.158814Z"
// "created_at": "2014-11-07T22:19:28.578544Z",
// "liquidity": "T",
// "fee": "0.00025",
// "settled": true,
// "usd_volume": "0.0924556000000000",
// "user_id": "595eb864313c2b02ddf2937d"
// }
//
const timestamp = this.parse8601(this.safeString2(trade, 'time', 'created_at'));
const marketId = this.safeString(trade, 'product_id');
market = this.safeMarket(marketId, market, '-');
let feeRate = undefined;
let takerOrMaker = undefined;
let cost = undefined;
const feeCurrencyId = this.safeStringLower(market, 'quoteId');
if (feeCurrencyId !== undefined) {
const costField = feeCurrencyId + '_value';
cost = this.safeString(trade, costField);
const liquidity = this.safeString(trade, 'liquidity');
if (liquidity !== undefined) {
takerOrMaker = (liquidity === 'T') ? 'taker' : 'maker';
feeRate = this.safeString(market, takerOrMaker);
}
}
const feeCost = this.safeString2(trade, 'fill_fees', 'fee');
const fee = {
'cost': feeCost,
'currency': market['quote'],
'rate': feeRate,
};
const id = this.safeString(trade, 'trade_id');
let side = (trade['side'] === 'buy') ? 'sell' : 'buy';
const orderId = this.safeString(trade, 'order_id');
// Coinbase Pro returns inverted side to fetchMyTrades vs fetchTrades
const makerOrderId = this.safeString(trade, 'maker_order_id');
const takerOrderId = this.safeString(trade, 'taker_order_id');
if ((orderId !== undefined) || ((makerOrderId !== undefined) && (takerOrderId !== undefined))) {
side = (trade['side'] === 'buy') ? 'buy' : 'sell';
}
const price = this.safeString(trade, 'price');
const amount = this.safeString(trade, 'size');
return this.safeTrade({
'id': id,
'order': orderId,
'info': trade,
'timestamp': timestamp,
'datetime': this.iso8601(timestamp),
'symbol': market['symbol'],
'type': undefined,
'takerOrMaker': takerOrMaker,
'side': side,
'price': price,
'amount': amount,
'fee': fee,
'cost': cost,
}, market);
}
/**
* @method
* @name coinbaseexchange#fetchMyTrades
* @see https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getfills
* @description fetch all trades made by the user
* @param {string} 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 {object} [params] extra parameters specific to the exchange API endpoint
* @param {int} [params.until] the latest time in ms to fetch trades for
* @param {boolean} [params.paginate] default false, when true will automatically paginate by calling this endpoint multiple times. See in the docs all the [availble parameters](https://github.com/ccxt/ccxt/wiki/Manual#pagination-params)
* @returns {Trade[]} a list of [trade structures]{@link https://docs.ccxt.com/#/?id=trade-structure}
*/
async fetchMyTrades(symbol = undefined, since = undefined, limit = undefined, params = {}) {
if (symbol === undefined) {
throw new ArgumentsRequired(this.id + ' fetchMyTrades() requires a symbol argument');
}
let paginate = false;
[paginate, params] = this.handleOptionAndParams(params, 'fetchMyTrades', 'paginate');
if (paginate) {
return await this.fetchPaginatedCallDynamic('fetchMyTrades', symbol, since, limit, params, 100);
}
await this.loadMarkets();
const market = this.market(symbol);
const request = {
'product_id': market['id'],
};
if (limit !== undefined) {
request['limit'] = limit;
}
if (since !== undefined) {
request['start_date'] = this.iso8601(since);
}
const until = this.safeValue2(params, 'until', 'end_date');
if (until !== undefined) {
params = this.omit(params, ['until']);
request['end_date'] = this.iso8601(until);
}
const response = await this.privateGetFills(this.extend(request, params));
return this.parseTrades(response, market, since, limit);
}
/**
* @method
* @name coinbaseexchange#fetchTrades
* @see https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getproducttrades
* @description get the list of most recent trades for a particular symbol
* @param {string} 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 {object} [params] extra parameters specific to the exchange API endpoint
* @returns {Trade[]} a list of [trade structures]{@link https://docs.ccxt.com/#/?id=public-trades}
*/
async fetchTrades(symbol, since = undefined, limit = undefined, params = {}) {
await this.loadMarkets();
const market = this.market(symbol);
const request = {
'id': market['id'], // fixes issue #2
};
if (limit !== undefined) {
request['limit'] = limit; // default 100
}
const response = await this.publicGetProductsIdTrades(this.extend(request, params));
//
// [
// {
// "trade_id": "15035219",
// "side": "sell",
// "size": "0.27426731",
// "price": "25820.42000000",
// "time": "2023-09-10T13:47:41.447577Z"
// },
// ]
//
return this.parseTrades(response, market, since, limit);
}
/**
* @method
* @name coinbaseexchange#fetchTradingFees
* @description fetch the trading fees for multiple markets
* @see https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getfees
* @param {object} [params] extra parameters specific to the exchange API endpoint
* @returns {object} a dictionary of [fee structures]{@link https://docs.ccxt.com/#/?id=fee-structure} indexed by market symbols
*/
async fetchTradingFees(params = {}) {
await this.loadMarkets();
const response = await this.privateGetFees(params);
//
// {
// "maker_fee_rate": "0.0050",
// "taker_fee_rate": "0.0050",
// "usd_volume": "43806.92"
// }
//
const maker = this.safeNumber(response, 'maker_fee_rate');
const taker = this.safeNumber(response, 'taker_fee_rate');
const result = {};
for (let i = 0; i < this.symbols.length; i++) {
const symbol = this.symbols[i];
result[symbol] = {
'info': response,
'symbol': symbol,
'maker': maker,
'taker': taker,
'percentage': true,
'tierBased': true,
};
}
return result;
}
parseOHLCV(ohlcv, market = undefined) {
//
// [
// 1591514160,
// 0.02507,
// 0.02507,
// 0.02507,
// 0.02507,
// 0.02816506
// ]
//
return [
this.safeTimestamp(ohlcv, 0),
this.safeNumber(ohlcv, 3),
this.safeNumber(ohlcv, 2),
this.safeNumber(ohlcv, 1),
this.safeNumber(ohlcv, 4),
this.safeNumber(ohlcv, 5),
];
}
/**
* @method
* @name coinbaseexchange#fetchOHLCV
* @see https://docs.cloud.coinbase.com/exchange/reference/exchangerestapi_getproductcandles
* @description fetches historical candlestick data containing the open, high, low, and close price, and the volume of a market
* @param {string} symbol unified symbol of the market to fetch OHLCV data for
* @param {string} 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 {object} [params] extra parameters specific to the exchange API endpoint
* @param {int} [params.until] the latest time in ms to fetch trades for
* @param {boolean} [params.paginate] default false, when true will automatically paginate by calling this endpoint multiple times. See in the docs all the [availble parameters](https://githu