UNPKG

coinbase-pro-node-api

Version:
169 lines (168 loc) 4.79 kB
import { stringify } from "node:querystring"; import { PublicClient } from "./public.js"; import { Signer } from "./signer.js"; export class AuthenticatedClient extends PublicClient { #key; #secret; #passphrase; constructor({ key, secret, passphrase, ...rest }) { super(rest); this.#key = key; this.#secret = secret; this.#passphrase = passphrase; } fetch(path, { method, body }) { const timestamp = Date.now() / 1000; const headers = Signer({ timestamp, method, key: this.#key, secret: this.#secret, passphrase: this.#passphrase, body, url: new URL(path, this.apiUri), }); return super.fetch(path, { method, headers: { ...headers }, body, }); } /** Get a list of trading accounts from the profile of the API key. */ getAccounts() { return this.get("/accounts"); } getAccount({ account_id }) { return this.get(`/accounts/${account_id}`); } /** List account activity of the API key’s profile. */ getAccountHistory({ account_id, ...qs }) { const url = new URL(`/accounts/${account_id}/ledger`, this.apiUri); url.search = stringify({ ...qs }); return this.get(url.toString()); } /** List holds of an account that belong to the same profile as the API key. */ getHolds({ account_id, ...qs }) { const url = new URL(`/accounts/${account_id}/holds`, this.apiUri); url.search = stringify({ ...qs }); return this.get(url.toString()); } placeOrder({ product_id = this.product_id, ...rest }) { return this.post("/orders", { body: JSON.stringify({ product_id, ...rest }), }); } cancelOrder(params) { return this.delete( "client_oid" in params ? `/orders/client:${params.client_oid}` : `/orders/${params.id}` ); } cancelAll(qs = {}) { const url = new URL(`/orders`, this.apiUri); url.search = stringify({ ...qs }); return this.delete(url.toString()); } getOrders(qs = {}) { const url = new URL(`/orders`, this.apiUri); url.search = stringify({ ...qs }); return this.get(url.toString()); } getOrder(params) { return this.get( "client_oid" in params ? `/orders/client:${params.client_oid}` : `/orders/${params.id}` ); } getFills(qs = {}) { if (!qs.order_id && !qs.product_id) { qs.product_id = this.product_id; } const url = new URL("/fills", this.apiUri); url.search = stringify({ ...qs }); return this.get(url.toString()); } deposit(params) { return this.post("/deposits/payment-method", { body: JSON.stringify(params), }); } depositCoinbase(params) { return this.post("/deposits/coinbase-account", { body: JSON.stringify(params), }); } withdraw(params) { return this.post("/withdrawals/payment-method", { body: JSON.stringify(params), }); } withdrawCoinbase(params) { return this.post("/withdrawals/coinbase-account", { body: JSON.stringify(params), }); } withdrawCrypto(params) { return this.post("/withdrawals/crypto", { body: JSON.stringify(params), }); } /** Get the network fee estimate when sending to the given address. */ feeEstimate(qs) { const url = new URL(`/withdrawals/fee-estimate`, this.apiUri); url.search = stringify({ ...qs }); return this.get(url.toString()); } convert(params) { return this.post("/conversions", { body: JSON.stringify(params), }); } getPaymentMethods() { return this.get("/payment-methods"); } getCoinbaseAccounts() { return this.get("/coinbase-accounts"); } getFees() { return this.get("/fees"); } createReport(params) { return new Promise((resolve, reject) => { if (params.type === "fills" && !params.product_id) { params.product_id = this.product_id; } else if (params.type === "account" && !params.account_id) { reject(new Error("`account_id` is missing")); return; } this.post("/reports", { body: JSON.stringify(params) }) .then((data) => { resolve(data); }) .catch(reject); }); } getReport({ id }) { return this.get(`/reports/${id}`); } /** List your profiles. */ getProfiles() { return this.get("/profiles"); } /** Get a single profile by profile id. */ getProfile({ id }) { return this.get(`/profiles/${id}`); } /** Transfer funds from API key’s profile to another user owned profile. */ transfer(params) { return this.post("/profiles/transfer", { body: JSON.stringify(params), }); } /** Get your 30-day trailing volume for all products of the API key’s profile. */ getTrailingVolume() { return this.get("/users/self/trailing-volume"); } }