sfccxt
Version:
A JavaScript / Python / PHP cryptocurrency trading library with support for 130+ exchanges
1,082 lines (1,055 loc) • 71.2 kB
JavaScript
'use strict';
// ----------------------------------------------------------------------------
const Exchange = require ('./base/Exchange');
const { ExchangeError, ArgumentsRequired, AuthenticationError, RateLimitExceeded, InvalidNonce } = require ('./base/errors');
const { TICK_SIZE } = require ('./base/functions/number');
const Precise = require ('./base/Precise');
// ----------------------------------------------------------------------------
module.exports = class coinbase extends Exchange {
describe () {
return this.deepExtend (super.describe (), {
'id': 'coinbase',
'name': 'Coinbase',
'countries': [ 'US' ],
'rateLimit': 400, // 10k calls per hour
'version': 'v2',
'userAgent': this.userAgents['chrome'],
'headers': {
'CB-VERSION': '2018-05-30',
},
'has': {
'CORS': true,
'spot': true,
'margin': false,
'swap': false,
'future': false,
'option': false,
'addMargin': false,
'cancelOrder': undefined,
'createDepositAddress': true,
'createOrder': undefined,
'createReduceOnlyOrder': false,
'createStopLimitOrder': false,
'createStopMarketOrder': false,
'createStopOrder': false,
'fetchAccounts': true,
'fetchBalance': true,
'fetchBidsAsks': undefined,
'fetchBorrowRate': false,
'fetchBorrowRateHistories': false,
'fetchBorrowRateHistory': false,
'fetchBorrowRates': false,
'fetchBorrowRatesPerSymbol': false,
'fetchClosedOrders': undefined,
'fetchCurrencies': true,
'fetchDepositAddress': undefined,
'fetchDeposits': true,
'fetchFundingHistory': false,
'fetchFundingRate': false,
'fetchFundingRateHistory': false,
'fetchFundingRates': false,
'fetchIndexOHLCV': false,
'fetchL2OrderBook': false,
'fetchLedger': true,
'fetchLeverage': false,
'fetchLeverageTiers': false,
'fetchMarginMode': false,
'fetchMarkets': true,
'fetchMarkOHLCV': false,
'fetchMyBuys': true,
'fetchMySells': true,
'fetchMyTrades': undefined,
'fetchOHLCV': false,
'fetchOpenInterestHistory': false,
'fetchOpenOrders': undefined,
'fetchOrder': undefined,
'fetchOrderBook': false,
'fetchOrders': undefined,
'fetchPosition': false,
'fetchPositionMode': false,
'fetchPositions': false,
'fetchPositionsRisk': false,
'fetchPremiumIndexOHLCV': false,
'fetchTicker': true,
'fetchTickers': true,
'fetchTime': true,
'fetchTrades': undefined,
'fetchTradingFee': false,
'fetchTradingFees': false,
'fetchTransactions': undefined,
'fetchWithdrawals': true,
'reduceMargin': false,
'setLeverage': false,
'setMarginMode': false,
'setPositionMode': false,
'withdraw': undefined,
},
'urls': {
'logo': 'https://user-images.githubusercontent.com/1294454/40811661-b6eceae2-653a-11e8-829e-10bfadb078cf.jpg',
'api': {
'rest': 'https://api.coinbase.com',
},
'www': 'https://www.coinbase.com',
'doc': 'https://developers.coinbase.com/api/v2',
'fees': 'https://support.coinbase.com/customer/portal/articles/2109597-buy-sell-bank-transfer-fees',
'referral': 'https://www.coinbase.com/join/58cbe25a355148797479dbd2',
},
'requiredCredentials': {
'apiKey': true,
'secret': true,
},
'api': {
'public': {
'get': [
'currencies',
'time',
'exchange-rates',
'users/{user_id}',
'prices/{symbol}/buy',
'prices/{symbol}/sell',
'prices/{symbol}/spot',
],
},
'private': {
'get': [
'accounts',
'accounts/{account_id}',
'accounts/{account_id}/addresses',
'accounts/{account_id}/addresses/{address_id}',
'accounts/{account_id}/addresses/{address_id}/transactions',
'accounts/{account_id}/transactions',
'accounts/{account_id}/transactions/{transaction_id}',
'accounts/{account_id}/buys',
'accounts/{account_id}/buys/{buy_id}',
'accounts/{account_id}/sells',
'accounts/{account_id}/sells/{sell_id}',
'accounts/{account_id}/deposits',
'accounts/{account_id}/deposits/{deposit_id}',
'accounts/{account_id}/withdrawals',
'accounts/{account_id}/withdrawals/{withdrawal_id}',
'payment-methods',
'payment-methods/{payment_method_id}',
'user',
'user/auth',
],
'post': [
'accounts',
'accounts/{account_id}/primary',
'accounts/{account_id}/addresses',
'accounts/{account_id}/transactions',
'accounts/{account_id}/transactions/{transaction_id}/complete',
'accounts/{account_id}/transactions/{transaction_id}/resend',
'accounts/{account_id}/buys',
'accounts/{account_id}/buys/{buy_id}/commit',
'accounts/{account_id}/sells',
'accounts/{account_id}/sells/{sell_id}/commit',
'accounts/{account_id}/deposits',
'accounts/{account_id}/deposits/{deposit_id}/commit',
'accounts/{account_id}/withdrawals',
'accounts/{account_id}/withdrawals/{withdrawal_id}/commit',
],
'put': [
'accounts/{account_id}',
'user',
],
'delete': [
'accounts/{id}',
'accounts/{account_id}/transactions/{transaction_id}',
],
},
},
'precisionMode': TICK_SIZE,
'exceptions': {
'exact': {
'two_factor_required': AuthenticationError, // 402 When sending money over 2fa limit
'param_required': ExchangeError, // 400 Missing parameter
'validation_error': ExchangeError, // 400 Unable to validate POST/PUT
'invalid_request': ExchangeError, // 400 Invalid request
'personal_details_required': AuthenticationError, // 400 User’s personal detail required to complete this request
'identity_verification_required': AuthenticationError, // 400 Identity verification is required to complete this request
'jumio_verification_required': AuthenticationError, // 400 Document verification is required to complete this request
'jumio_face_match_verification_required': AuthenticationError, // 400 Document verification including face match is required to complete this request
'unverified_email': AuthenticationError, // 400 User has not verified their email
'authentication_error': AuthenticationError, // 401 Invalid auth (generic)
'invalid_authentication_method': AuthenticationError, // 401 API access is blocked for deleted users.
'invalid_token': AuthenticationError, // 401 Invalid Oauth token
'revoked_token': AuthenticationError, // 401 Revoked Oauth token
'expired_token': AuthenticationError, // 401 Expired Oauth token
'invalid_scope': AuthenticationError, // 403 User hasn’t authenticated necessary scope
'not_found': ExchangeError, // 404 Resource not found
'rate_limit_exceeded': RateLimitExceeded, // 429 Rate limit exceeded
'internal_server_error': ExchangeError, // 500 Internal server error
},
'broad': {
'request timestamp expired': InvalidNonce, // {"errors":[{"id":"authentication_error","message":"request timestamp expired"}]}
},
},
'commonCurrencies': {
'CGLD': 'CELO',
},
'options': {
'fetchCurrencies': {
'expires': 5000,
},
'accounts': [
'wallet',
'fiat',
// 'vault',
],
},
});
}
async fetchTime (params = {}) {
/**
* @method
* @name coinbase#fetchTime
* @description fetches the current integer timestamp in milliseconds from the exchange server
* @param {object} params extra parameters specific to the coinbase api endpoint
* @returns {int} the current integer timestamp in milliseconds from the exchange server
*/
const response = await this.publicGetTime (params);
//
// {
// "data": {
// "epoch": 1589295679,
// "iso": "2020-05-12T15:01:19Z"
// }
// }
//
const data = this.safeValue (response, 'data', {});
return this.safeTimestamp (data, 'epoch');
}
async fetchAccounts (params = {}) {
/**
* @method
* @name coinbase#fetchAccounts
* @description fetch all the accounts associated with a profile
* @param {object} params extra parameters specific to the coinbase api endpoint
* @returns {object} a dictionary of [account structures]{@link https://docs.ccxt.com/en/latest/manual.html#account-structure} indexed by the account type
*/
await this.loadMarkets ();
const request = {
'limit': 100,
};
const response = await this.privateGetAccounts (this.extend (request, params));
//
// {
// "id": "XLM",
// "name": "XLM Wallet",
// "primary": false,
// "type": "wallet",
// "currency": {
// "code": "XLM",
// "name": "Stellar Lumens",
// "color": "#000000",
// "sort_index": 127,
// "exponent": 7,
// "type": "crypto",
// "address_regex": "^G[A-Z2-7]{55}$",
// "asset_id": "13b83335-5ede-595b-821e-5bcdfa80560f",
// "destination_tag_name": "XLM Memo ID",
// "destination_tag_regex": "^[ -~]{1,28}$"
// },
// "balance": {
// "amount": "0.0000000",
// "currency": "XLM"
// },
// "created_at": null,
// "updated_at": null,
// "resource": "account",
// "resource_path": "/v2/accounts/XLM",
// "allow_deposits": true,
// "allow_withdrawals": true
// }
//
const data = this.safeValue (response, 'data', []);
return this.parseAccounts (data, params);
}
parseAccount (account) {
//
// {
// "id": "XLM",
// "name": "XLM Wallet",
// "primary": false,
// "type": "wallet",
// "currency": {
// "code": "XLM",
// "name": "Stellar Lumens",
// "color": "#000000",
// "sort_index": 127,
// "exponent": 7,
// "type": "crypto",
// "address_regex": "^G[A-Z2-7]{55}$",
// "asset_id": "13b83335-5ede-595b-821e-5bcdfa80560f",
// "destination_tag_name": "XLM Memo ID",
// "destination_tag_regex": "^[ -~]{1,28}$"
// },
// "balance": {
// "amount": "0.0000000",
// "currency": "XLM"
// },
// "created_at": null,
// "updated_at": null,
// "resource": "account",
// "resource_path": "/v2/accounts/XLM",
// "allow_deposits": true,
// "allow_withdrawals": true
// }
//
const currency = this.safeValue (account, 'currency', {});
const currencyId = this.safeString (currency, 'code');
const code = this.safeCurrencyCode (currencyId);
return {
'id': this.safeString (account, 'id'),
'type': this.safeString (account, 'type'),
'code': code,
'info': account,
};
}
async createDepositAddress (code, params = {}) {
/**
* @method
* @name coinbase#createDepositAddress
* @description create a currency deposit address
* @param {string} code unified currency code of the currency for the deposit address
* @param {object} params extra parameters specific to the coinbase api endpoint
* @returns {object} an [address structure]{@link https://docs.ccxt.com/en/latest/manual.html#address-structure}
*/
let accountId = this.safeString (params, 'account_id');
params = this.omit (params, 'account_id');
if (accountId === undefined) {
await this.loadAccounts ();
for (let i = 0; i < this.accounts.length; i++) {
const account = this.accounts[i];
if (account['code'] === code && account['type'] === 'wallet') {
accountId = account['id'];
break;
}
}
}
if (accountId === undefined) {
throw new ExchangeError (this.id + ' createDepositAddress() could not find the account with matching currency code, specify an `account_id` extra param');
}
const request = {
'account_id': accountId,
};
const response = await this.privatePostAccountsAccountIdAddresses (this.extend (request, params));
//
// {
// "data": {
// "id": "05b1ebbf-9438-5dd4-b297-2ddedc98d0e4",
// "address": "coinbasebase",
// "address_info": {
// "address": "coinbasebase",
// "destination_tag": "287594668"
// },
// "name": null,
// "created_at": "2019-07-01T14:39:29Z",
// "updated_at": "2019-07-01T14:39:29Z",
// "network": "eosio",
// "uri_scheme": "eosio",
// "resource": "address",
// "resource_path": "/v2/accounts/14cfc769-e852-52f3-b831-711c104d194c/addresses/05b1ebbf-9438-5dd4-b297-2ddedc98d0e4",
// "warnings": [
// {
// "title": "Only send EOS (EOS) to this address",
// "details": "Sending any other cryptocurrency will result in permanent loss.",
// "image_url": "https://dynamic-assets.coinbase.com/deaca3d47b10ed4a91a872e9618706eec34081127762d88f2476ac8e99ada4b48525a9565cf2206d18c04053f278f693434af4d4629ca084a9d01b7a286a7e26/asset_icons/1f8489bb280fb0a0fd643c1161312ba49655040e9aaaced5f9ad3eeaf868eadc.png"
// },
// {
// "title": "Both an address and EOS memo are required to receive EOS",
// "details": "If you send funds without an EOS memo or with an incorrect EOS memo, your funds cannot be credited to your account.",
// "image_url": "https://www.coinbase.com/assets/receive-warning-2f3269d83547a7748fb39d6e0c1c393aee26669bfea6b9f12718094a1abff155.png"
// }
// ],
// "warning_title": "Only send EOS (EOS) to this address",
// "warning_details": "Sending any other cryptocurrency will result in permanent loss.",
// "destination_tag": "287594668",
// "deposit_uri": "eosio:coinbasebase?dt=287594668",
// "callback_url": null
// }
// }
//
const data = this.safeValue (response, 'data', {});
const tag = this.safeString (data, 'destination_tag');
const address = this.safeString (data, 'address');
return {
'currency': code,
'tag': tag,
'address': address,
'info': response,
};
}
async fetchMySells (symbol = undefined, since = undefined, limit = undefined, params = {}) {
/**
* @method
* @name coinbase#fetchMySells
* @description fetch sells
* @param {string|undefined} symbol not used by coinbase fetchMySells ()
* @param {int|undefined} since timestamp in ms of the earliest sell, default is undefined
* @param {int|undefined} limit max number of sells to return, default is undefined
* @param {object} params extra parameters specific to the coinbase api endpoint
* @returns {object} a [list of order structures]{@link https://docs.ccxt.com/en/latest/manual.html#order-structure}
*/
// they don't have an endpoint for all historical trades
const request = this.prepareAccountRequest (limit, params);
await this.loadMarkets ();
const query = this.omit (params, [ 'account_id', 'accountId' ]);
const sells = await this.privateGetAccountsAccountIdSells (this.extend (request, query));
return this.parseTrades (sells['data'], undefined, since, limit);
}
async fetchMyBuys (symbol = undefined, since = undefined, limit = undefined, params = {}) {
/**
* @method
* @name coinbase#fetchMyBuys
* @description fetch buys
* @param {string|undefined} symbol not used by coinbase fetchMyBuys ()
* @param {int|undefined} since timestamp in ms of the earliest buy, default is undefined
* @param {int|undefined} limit max number of buys to return, default is undefined
* @param {object} params extra parameters specific to the coinbase api endpoint
* @returns {object} a list of [order structures]{@link https://docs.ccxt.com/en/latest/manual.html#order-structure}
*/
// they don't have an endpoint for all historical trades
const request = this.prepareAccountRequest (limit, params);
await this.loadMarkets ();
const query = this.omit (params, [ 'account_id', 'accountId' ]);
const buys = await this.privateGetAccountsAccountIdBuys (this.extend (request, query));
return this.parseTrades (buys['data'], undefined, since, limit);
}
async fetchTransactionsWithMethod (method, code = undefined, since = undefined, limit = undefined, params = {}) {
const request = await this.prepareAccountRequestWithCurrencyCode (code, limit, params);
await this.loadMarkets ();
const query = this.omit (params, [ 'account_id', 'accountId' ]);
const response = await this[method] (this.extend (request, query));
return this.parseTransactions (response['data'], undefined, since, limit);
}
async fetchWithdrawals (code = undefined, since = undefined, limit = undefined, params = {}) {
/**
* @method
* @name coinbase#fetchWithdrawals
* @description fetch all withdrawals made from an account
* @param {string|undefined} code unified currency code
* @param {int|undefined} since the earliest time in ms to fetch withdrawals for
* @param {int|undefined} limit the maximum number of withdrawals structures to retrieve
* @param {object} params extra parameters specific to the coinbase api endpoint
* @returns {[object]} a list of [transaction structures]{@link https://docs.ccxt.com/en/latest/manual.html#transaction-structure}
*/
// fiat only, for crypto transactions use fetchLedger
return await this.fetchTransactionsWithMethod ('privateGetAccountsAccountIdWithdrawals', code, since, limit, params);
}
async fetchDeposits (code = undefined, since = undefined, limit = undefined, params = {}) {
/**
* @method
* @name coinbase#fetchDeposits
* @description fetch all deposits made to an account
* @param {string|undefined} code unified currency code
* @param {int|undefined} since the earliest time in ms to fetch deposits for
* @param {int|undefined} limit the maximum number of deposits structures to retrieve
* @param {object} params extra parameters specific to the coinbase api endpoint
* @returns {[object]} a list of [transaction structures]{@link https://docs.ccxt.com/en/latest/manual.html#transaction-structure}
*/
// fiat only, for crypto transactions use fetchLedger
return await this.fetchTransactionsWithMethod ('privateGetAccountsAccountIdDeposits', code, since, limit, params);
}
parseTransactionStatus (status) {
const statuses = {
'created': 'pending',
'completed': 'ok',
'canceled': 'canceled',
};
return this.safeString (statuses, status, status);
}
parseTransaction (transaction, market = undefined) {
//
// fiat deposit
//
// {
// "id": "f34c19f3-b730-5e3d-9f72",
// "status": "completed",
// "payment_method": {
// "id": "a022b31d-f9c7-5043-98f2",
// "resource": "payment_method",
// "resource_path": "/v2/payment-methods/a022b31d-f9c7-5043-98f2"
// },
// "transaction": {
// "id": "04ed4113-3732-5b0c-af86-b1d2146977d0",
// "resource": "transaction",
// "resource_path": "/v2/accounts/91cd2d36-3a91-55b6-a5d4-0124cf105483/transactions/04ed4113-3732-5b0c-af86"
// },
// "user_reference": "2VTYTH",
// "created_at": "2017-02-09T07:01:18Z",
// "updated_at": "2017-02-09T07:01:26Z",
// "resource": "deposit",
// "resource_path": "/v2/accounts/91cd2d36-3a91-55b6-a5d4-0124cf105483/deposits/f34c19f3-b730-5e3d-9f72",
// "committed": true,
// "payout_at": "2017-02-12T07:01:17Z",
// "instant": false,
// "fee": { "amount": "0.00", "currency": "EUR" },
// "amount": { "amount": "114.02", "currency": "EUR" },
// "subtotal": { "amount": "114.02", "currency": "EUR" },
// "hold_until": null,
// "hold_days": 0,
// "hold_business_days": 0,
// "next_step": null
// }
//
// fiat_withdrawal
//
// {
// "id": "cfcc3b4a-eeb6-5e8c-8058",
// "status": "completed",
// "payment_method": {
// "id": "8b94cfa4-f7fd-5a12-a76a",
// "resource": "payment_method",
// "resource_path": "/v2/payment-methods/8b94cfa4-f7fd-5a12-a76a"
// },
// "transaction": {
// "id": "fcc2550b-5104-5f83-a444",
// "resource": "transaction",
// "resource_path": "/v2/accounts/91cd2d36-3a91-55b6-a5d4-0124cf105483/transactions/fcc2550b-5104-5f83-a444"
// },
// "user_reference": "MEUGK",
// "created_at": "2018-07-26T08:55:12Z",
// "updated_at": "2018-07-26T08:58:18Z",
// "resource": "withdrawal",
// "resource_path": "/v2/accounts/91cd2d36-3a91-55b6-a5d4-0124cf105483/withdrawals/cfcc3b4a-eeb6-5e8c-8058",
// "committed": true,
// "payout_at": "2018-07-31T08:55:12Z",
// "instant": false,
// "fee": { "amount": "0.15", "currency": "EUR" },
// "amount": { "amount": "13130.69", "currency": "EUR" },
// "subtotal": { "amount": "13130.84", "currency": "EUR" },
// "idem": "e549dee5-63ed-4e79-8a96",
// "next_step": null
// }
//
const subtotalObject = this.safeValue (transaction, 'subtotal', {});
const feeObject = this.safeValue (transaction, 'fee', {});
const id = this.safeString (transaction, 'id');
const timestamp = this.parse8601 (this.safeValue (transaction, 'created_at'));
const updated = this.parse8601 (this.safeValue (transaction, 'updated_at'));
const type = this.safeString (transaction, 'resource');
const amount = this.safeNumber (subtotalObject, 'amount');
const currencyId = this.safeString (subtotalObject, 'currency');
const currency = this.safeCurrencyCode (currencyId);
const feeCost = this.safeNumber (feeObject, 'amount');
const feeCurrencyId = this.safeString (feeObject, 'currency');
const feeCurrency = this.safeCurrencyCode (feeCurrencyId);
const fee = {
'cost': feeCost,
'currency': feeCurrency,
};
let status = this.parseTransactionStatus (this.safeString (transaction, 'status'));
if (status === undefined) {
const committed = this.safeValue (transaction, 'committed');
status = committed ? 'ok' : 'pending';
}
return {
'info': transaction,
'id': id,
'txid': id,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'network': undefined,
'address': undefined,
'addressTo': undefined,
'addressFrom': undefined,
'tag': undefined,
'tagTo': undefined,
'tagFrom': undefined,
'type': type,
'amount': amount,
'currency': currency,
'status': status,
'updated': updated,
'fee': fee,
};
}
parseTrade (trade, market = undefined) {
//
// {
// "id": "67e0eaec-07d7-54c4-a72c-2e92826897df",
// "status": "completed",
// "payment_method": {
// "id": "83562370-3e5c-51db-87da-752af5ab9559",
// "resource": "payment_method",
// "resource_path": "/v2/payment-methods/83562370-3e5c-51db-87da-752af5ab9559"
// },
// "transaction": {
// "id": "441b9494-b3f0-5b98-b9b0-4d82c21c252a",
// "resource": "transaction",
// "resource_path": "/v2/accounts/2bbf394c-193b-5b2a-9155-3b4732659ede/transactions/441b9494-b3f0-5b98-b9b0-4d82c21c252a"
// },
// "amount": { "amount": "1.00000000", "currency": "BTC" },
// "total": { "amount": "10.25", "currency": "USD" },
// "subtotal": { "amount": "10.10", "currency": "USD" },
// "created_at": "2015-01-31T20:49:02Z",
// "updated_at": "2015-02-11T16:54:02-08:00",
// "resource": "buy",
// "resource_path": "/v2/accounts/2bbf394c-193b-5b2a-9155-3b4732659ede/buys/67e0eaec-07d7-54c4-a72c-2e92826897df",
// "committed": true,
// "instant": false,
// "fee": { "amount": "0.15", "currency": "USD" },
// "payout_at": "2015-02-18T16:54:00-08:00"
// }
//
let symbol = undefined;
const totalObject = this.safeValue (trade, 'total', {});
const amountObject = this.safeValue (trade, 'amount', {});
const subtotalObject = this.safeValue (trade, 'subtotal', {});
const feeObject = this.safeValue (trade, 'fee', {});
const id = this.safeString (trade, 'id');
const timestamp = this.parse8601 (this.safeValue (trade, 'created_at'));
if (market === undefined) {
const baseId = this.safeString (amountObject, 'currency');
const quoteId = this.safeString (totalObject, 'currency');
if ((baseId !== undefined) && (quoteId !== undefined)) {
const base = this.safeCurrencyCode (baseId);
const quote = this.safeCurrencyCode (quoteId);
symbol = base + '/' + quote;
}
}
const orderId = undefined;
const side = this.safeString (trade, 'resource');
const type = undefined;
const costString = this.safeString (subtotalObject, 'amount');
const amountString = this.safeString (amountObject, 'amount');
const cost = this.parseNumber (costString);
const amount = this.parseNumber (amountString);
const price = this.parseNumber (Precise.stringDiv (costString, amountString));
const feeCost = this.safeNumber (feeObject, 'amount');
const feeCurrencyId = this.safeString (feeObject, 'currency');
const feeCurrency = this.safeCurrencyCode (feeCurrencyId);
const fee = {
'cost': feeCost,
'currency': feeCurrency,
};
return {
'info': trade,
'id': id,
'order': orderId,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'symbol': symbol,
'type': type,
'side': side,
'takerOrMaker': undefined,
'price': price,
'amount': amount,
'cost': cost,
'fee': fee,
};
}
async fetchMarkets (params = {}) {
/**
* @method
* @name coinbase#fetchMarkets
* @description retrieves data on all markets for coinbase
* @param {object} params extra parameters specific to the exchange api endpoint
* @returns {[object]} an array of objects representing market data
*/
const response = await this.fetchCurrenciesFromCache (params);
const currencies = this.safeValue (response, 'currencies', {});
const exchangeRates = this.safeValue (response, 'exchangeRates', {});
const data = this.safeValue (currencies, 'data', []);
const dataById = this.indexBy (data, 'id');
const rates = this.safeValue (this.safeValue (exchangeRates, 'data', {}), 'rates', {});
const baseIds = Object.keys (rates);
const result = [];
for (let i = 0; i < baseIds.length; i++) {
const baseId = baseIds[i];
const base = this.safeCurrencyCode (baseId);
const type = (baseId in dataById) ? 'fiat' : 'crypto';
// https://github.com/ccxt/ccxt/issues/6066
if (type === 'crypto') {
for (let j = 0; j < data.length; j++) {
const quoteCurrency = data[j];
const quoteId = this.safeString (quoteCurrency, 'id');
const quote = this.safeCurrencyCode (quoteId);
result.push ({
'id': baseId + '-' + quoteId,
'symbol': base + '/' + quote,
'base': base,
'quote': quote,
'settle': undefined,
'baseId': baseId,
'quoteId': quoteId,
'settleId': undefined,
'type': 'spot',
'spot': true,
'margin': false,
'swap': false,
'future': false,
'option': false,
'active': undefined,
'contract': false,
'linear': undefined,
'inverse': undefined,
'contractSize': undefined,
'expiry': undefined,
'expiryDatetime': undefined,
'strike': undefined,
'optionType': undefined,
'precision': {
'amount': undefined,
'price': undefined,
},
'limits': {
'leverage': {
'min': undefined,
'max': undefined,
},
'amount': {
'min': undefined,
'max': undefined,
},
'price': {
'min': undefined,
'max': undefined,
},
'cost': {
'min': this.safeNumber (quoteCurrency, 'min_size'),
'max': undefined,
},
},
'info': quoteCurrency,
});
}
}
}
return result;
}
async fetchCurrenciesFromCache (params = {}) {
const options = this.safeValue (this.options, 'fetchCurrencies', {});
const timestamp = this.safeInteger (options, 'timestamp');
const expires = this.safeInteger (options, 'expires', 1000);
const now = this.milliseconds ();
if ((timestamp === undefined) || ((now - timestamp) > expires)) {
const currencies = await this.publicGetCurrencies (params);
const exchangeRates = await this.publicGetExchangeRates (params);
this.options['fetchCurrencies'] = this.extend (options, {
'currencies': currencies,
'exchangeRates': exchangeRates,
'timestamp': now,
});
}
return this.safeValue (this.options, 'fetchCurrencies', {});
}
async fetchCurrencies (params = {}) {
/**
* @method
* @name coinbase#fetchCurrencies
* @description fetches all available currencies on an exchange
* @param {object} params extra parameters specific to the coinbase api endpoint
* @returns {object} an associative dictionary of currencies
*/
const response = await this.fetchCurrenciesFromCache (params);
const currencies = this.safeValue (response, 'currencies', {});
//
// {
// "data":[
// {"id":"AED","name":"United Arab Emirates Dirham","min_size":"0.01000000"},
// {"id":"AFN","name":"Afghan Afghani","min_size":"0.01000000"},
// {"id":"ALL","name":"Albanian Lek","min_size":"0.01000000"},
// {"id":"AMD","name":"Armenian Dram","min_size":"0.01000000"},
// {"id":"ANG","name":"Netherlands Antillean Gulden","min_size":"0.01000000"},
// // ...
// ],
// }
//
const exchangeRates = this.safeValue (response, 'exchangeRates', {});
//
// {
// "data":{
// "currency":"USD",
// "rates":{
// "AED":"3.67",
// "AFN":"78.21",
// "ALL":"110.42",
// "AMD":"474.18",
// "ANG":"1.75",
// // ...
// },
// }
// }
//
const data = this.safeValue (currencies, 'data', []);
const dataById = this.indexBy (data, 'id');
const rates = this.safeValue (this.safeValue (exchangeRates, 'data', {}), 'rates', {});
const keys = Object.keys (rates);
const result = {};
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const type = (key in dataById) ? 'fiat' : 'crypto';
const currency = this.safeValue (dataById, key, {});
const id = this.safeString (currency, 'id', key);
const name = this.safeString (currency, 'name');
const code = this.safeCurrencyCode (id);
result[code] = {
'id': id,
'code': code,
'info': currency, // the original payload
'type': type,
'name': name,
'active': true,
'deposit': undefined,
'withdraw': undefined,
'fee': undefined,
'precision': undefined,
'limits': {
'amount': {
'min': this.safeNumber (currency, 'min_size'),
'max': undefined,
},
'withdraw': {
'min': undefined,
'max': undefined,
},
},
};
}
return result;
}
async fetchTickers (symbols = undefined, params = {}) {
/**
* @method
* @name coinbase#fetchTickers
* @description fetches price tickers for multiple markets, statistical calculations with the information calculated over the past 24 hours each market
* @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 coinbase api endpoint
* @returns {object} an array of [ticker structures]{@link https://docs.ccxt.com/en/latest/manual.html#ticker-structure}
*/
await this.loadMarkets ();
symbols = this.marketSymbols (symbols);
const request = {
// 'currency': 'USD',
};
const response = await this.publicGetExchangeRates (this.extend (request, params));
//
// {
// "data":{
// "currency":"USD",
// "rates":{
// "AED":"3.6731",
// "AFN":"103.163942",
// "ALL":"106.973038",
// }
// }
// }
//
const data = this.safeValue (response, 'data', {});
const rates = this.safeValue (data, 'rates', {});
const quoteId = this.safeString (data, 'currency');
const result = {};
const baseIds = Object.keys (rates);
const delimiter = '-';
for (let i = 0; i < baseIds.length; i++) {
const baseId = baseIds[i];
const marketId = baseId + delimiter + quoteId;
const market = this.safeMarket (marketId, undefined, delimiter);
const symbol = market['symbol'];
result[symbol] = this.parseTicker (rates[baseId], market);
}
return this.filterByArray (result, 'symbol', symbols);
}
async fetchTicker (symbol, params = {}) {
/**
* @method
* @name coinbase#fetchTicker
* @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 coinbase api endpoint
* @returns {object} a [ticker structure]{@link https://docs.ccxt.com/en/latest/manual.html#ticker-structure}
*/
await this.loadMarkets ();
const market = this.market (symbol);
const request = this.extend ({
'symbol': market['id'],
}, params);
const spot = await this.publicGetPricesSymbolSpot (request);
//
// {"data":{"base":"BTC","currency":"USD","amount":"48691.23"}}
//
const buy = await this.publicGetPricesSymbolBuy (request);
//
// {"data":{"base":"BTC","currency":"USD","amount":"48691.23"}}
//
const sell = await this.publicGetPricesSymbolSell (request);
//
// {"data":{"base":"BTC","currency":"USD","amount":"48691.23"}}
//
return this.parseTicker ([ spot, buy, sell ], market);
}
parseTicker (ticker, market = undefined) {
//
// fetchTicker
//
// [
// "48691.23", // spot
// "48691.23", // buy
// "48691.23", // sell
// ]
//
// fetchTickers
//
// "48691.23"
//
const symbol = this.safeSymbol (undefined, market);
let ask = undefined;
let bid = undefined;
let last = undefined;
const timestamp = this.milliseconds ();
if (typeof ticker !== 'string') {
const [ spot, sell, buy ] = ticker;
const spotData = this.safeValue (spot, 'data', {});
const buyData = this.safeValue (buy, 'data', {});
const sellData = this.safeValue (sell, 'data', {});
last = this.safeString (spotData, 'amount');
bid = this.safeString (buyData, 'amount');
ask = this.safeString (sellData, 'amount');
}
return this.safeTicker ({
'symbol': symbol,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'bid': bid,
'ask': ask,
'last': last,
'high': undefined,
'low': undefined,
'bidVolume': undefined,
'askVolume': undefined,
'vwap': undefined,
'open': undefined,
'close': last,
'previousClose': undefined,
'change': undefined,
'percentage': undefined,
'average': undefined,
'baseVolume': undefined,
'quoteVolume': undefined,
'info': ticker,
}, market);
}
parseBalance (response, params = {}) {
const balances = this.safeValue (response, 'data', []);
const accounts = this.safeValue (params, 'type', this.options['accounts']);
const result = { 'info': response };
for (let b = 0; b < balances.length; b++) {
const balance = balances[b];
const type = this.safeString (balance, 'type');
if (this.inArray (type, accounts)) {
const value = this.safeValue (balance, 'balance');
if (value !== undefined) {
const currencyId = this.safeString (value, 'currency');
const code = this.safeCurrencyCode (currencyId);
const total = this.safeString (value, 'amount');
const free = total;
let account = this.safeValue (result, code);
if (account === undefined) {
account = this.account ();
account['free'] = free;
account['total'] = total;
} else {
account['free'] = Precise.stringAdd (account['free'], total);
account['total'] = Precise.stringAdd (account['total'], total);
}
result[code] = account;
}
}
}
return this.safeBalance (result);
}
async fetchBalance (params = {}) {
/**
* @method
* @name coinbase#fetchBalance
* @description query for balance and get the amount of funds available for trading or funds locked in orders
* @param {object} params extra parameters specific to the coinbase api endpoint
* @returns {object} a [balance structure]{@link https://docs.ccxt.com/en/latest/manual.html?#balance-structure}
*/
await this.loadMarkets ();
const request = {
'limit': 100,
};
const response = await this.privateGetAccounts (this.extend (request, params));
//
// {
// "pagination":{
// "ending_before":null,
// "starting_after":null,
// "previous_ending_before":null,
// "next_starting_after":"6b17acd6-2e68-5eb0-9f45-72d67cef578b",
// "limit":100,
// "order":"desc",
// "previous_uri":null,
// "next_uri":"/v2/accounts?limit=100\u0026starting_after=6b17acd6-2e68-5eb0-9f45-72d67cef578b"
// },
// "data":[
// {
// "id":"94ad58bc-0f15-5309-b35a-a4c86d7bad60",
// "name":"MINA Wallet",
// "primary":false,
// "type":"wallet",
// "currency":{
// "code":"MINA",
// "name":"Mina",
// "color":"#EA6B48",
// "sort_index":397,
// "exponent":9,
// "type":"crypto",
// "address_regex":"^(B62)[A-Za-z0-9]{52}$",
// "asset_id":"a4ffc575-942c-5e26-b70c-cb3befdd4229",
// "slug":"mina"
// },
// "balance":{"amount":"0.000000000","currency":"MINA"},
// "created_at":"2022-03-25T00:36:16Z",
// "updated_at":"2022-03-25T00:36:16Z",
// "resource":"account",
// "resource_path":"/v2/accounts/94ad58bc-0f15-5309-b35a-a4c86d7bad60",
// "allow_deposits":true,
// "allow_withdrawals":true
// },
// ]
// }
//
return this.parseBalance (response, params);
}
async fetchLedger (code = undefined, since = undefined, limit = undefined, params = {}) {
/**
* @method
* @name coinbase#fetchLedger
* @description fetch the history of changes, actions done by the user or operations that altered balance of the user
* @param {string|undefined} code unified currency code, default is undefined
* @param {int|undefined} since timestamp in ms of the earliest ledger entry, default is undefined
* @param {int|undefined} limit max number of ledger entrys to return, default is undefined
* @param {object} params extra parameters specific to the coinbase api endpoint
* @returns {object} a [ledger structure]{@link https://docs.ccxt.com/en/latest/manual.html#ledger-structure}
*/
await this.loadMarkets ();
let currency = undefined;
if (code !== undefined) {
currency = this.currency (code);
}
const request = await this.prepareAccountRequestWithCurrencyCode (code, limit, params);
const query = this.omit (params, [ 'account_id', 'accountId' ]);
// for pagination use parameter 'starting_after'
// the value for the next page can be obtained from the result of the previous call in the 'pagination' field
// eg: instance.last_json_response.pagination.next_starting_after
const response = await this.privateGetAccountsAccountIdTransactions (this.extend (request, query));
return this.parseLedger (response['data'], currency, since, limit);
}
parseLedgerEntryStatus (status) {
const types = {
'completed': 'ok',
};
return this.safeString (types, status, status);
}
parseLedgerEntryType (type) {
const types = {
'buy': 'trade',
'sell': 'trade',
'fiat_deposit': 'transaction',
'fiat_withdrawal': 'transaction',
'exchange_deposit': 'transaction', // fiat withdrawal (from coinbase to coinbasepro)
'exchange_withdrawal': 'transaction', // fiat depo