Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions collection/bybit/currency_all.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
"DLC",
"DMAIL",
"DOGE",
"DOGS",
"DOME",
"DOP1",
"DOT",
Expand Down
13 changes: 7 additions & 6 deletions configuration/coinbase.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,27 @@
},
"PATH": {
"ACCOUNT_ALL": "/api/v3/brokerage/portfolios",
"ACCOUNT_BALANCE": "/api/v3/brokerage/portfolios/${portfolio_uuid}",
"ACCOUNT_BALANCE": "/api/v3/brokerage/portfolios/${portfolio_id}",
"ACCOUNT_INFORMATION": "",
"ACCOUNT_WALLETS": "/api/v3/brokerage/accounts",
"ADDRESS_ALL": "/v2/accounts/${account_uuid}/addresses",
"CURRENCY_ALL": "/api/v2/currencies/crypto",
"CURRENCY_ONE": "",
"DEPOSIT_ALL": "/v2/accounts/${account_uuid}/deposits",
"DEPOSIT_NEW": "/v2/accounts/${account_uuid}/addresses",
"DEPOSIT_ONE": "",
"KEY_INFORMATION": "",
"MARKET_HISTORY": "",
"MARKET_INFO": "",
"MARKET_TICKERS": "/api/v3/brokerage/market/products/${product_id}/ticker",
"MARKET_HISTORY": "/api/v3/brokerage/market/products/${product_id}/ticker",
"MARKET_INFO": "/api/v3/brokerage/market/products",
"MARKET_TICKERS": "/api/v3/brokerage/market/products/${product_id}",
"NETWORK_ALL": "",
"ORDER_ALL": "/api/v3/brokerage/orders/historical/batch",
"ORDER_BOOK": "",
"ORDER_CANCEL_ALL": "",
"ORDER_CANCEL_ALL": "/api/v3/brokerage/orders/batch_cancel",
"ORDER_CANCEL_ONE": "",
"ORDER_HISTORY_ALL": "",
"ORDER_HISTORY_ONE": "",
"ORDER_ONE": "",
"ORDER_ONE": "/api/v3/brokerage/orders/historical/${order_id}",
"ORDER_PLACE": "/api/v3/brokerage/orders",
"TRADE_FEES": "",
"TRADE_HISTORY_ALL": "",
Expand Down
34 changes: 19 additions & 15 deletions lib/authentication.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ import nodeCrypto from "node:crypto";
import jsonwebtoken from "jsonwebtoken";
import { dirObject } from "./output.mjs";


export const hide = (secret, hidden = "full", visibility = 2) => {
const output = {
"full": "****************",
"mask": secret.slice(0, visibility) +
secret.slice(visibility, -visibility).replace(/.|\n/g, "*") +
secret.slice(-visibility),
"none": secret
}[hidden];

return output
};

/**
* Sign a request with Hash-based Message Authentication Code.
*/
Expand Down Expand Up @@ -50,24 +63,15 @@ export const signJwt = (encoding, payload, secret, key) => {

dirObject("JWT", {
encoding,
key,
key: hide(key, "mask"),
nonce,
payload,
payload: {
...payload,
sub: hide(key, "mask")
},
secret: hide(secret),
token: hide(token),
});

return token
};

export const hide = (secret, hidden = "full", visibility = 2) => {
const output = {
"full": "****************",
"mask": secret.slice(0, visibility) +
secret.slice(visibility, -visibility).replace(/.|\n/g, "*") +
secret.slice(-visibility),
"none": secret
}[hidden];

return output
};
};
25 changes: 19 additions & 6 deletions lib/fetch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,38 @@ import { hide } from "./authentication.mjs";
import { dirObject } from "./output.mjs";

export const fetchData = (method, url, data, headers) => {
const authn = headers.Authorization,
const headerAuth = headers.Authorization,
timeStart = Date.now(),
intervalId = global.setInterval(() => {
process.stdout.write(`\rFetching${".".repeat(intervalCount)}`);
intervalCount++;
}, 100),
options = {
headers,
method,
};
let intervalCount = 0;

if (method === "POST") {
options.body = JSON.stringify(data);
}
dirObject(method, {
data,
headers: authn ? {
headers: headerAuth ? {
...headers,
Authorization: hide(authn),
Authorization: hide(headerAuth),
} : headers,
url
});
console.info("Fetching\u2026");

return global.fetch(url, options)
.then(async (response) => {
const text = await response.text();
const timeEnd = Date.now()

global.clearInterval(intervalId);
process.stdout.write(`\rFetching done in ${timeEnd - timeStart} ms.\n`);

const text = await response.text();
let json;

try {
Expand All @@ -44,7 +53,11 @@ export const fetchData = (method, url, data, headers) => {
json = text || error.message;
}

return { json, status: response.status }
return {
json,
status: response.status,
statusText: response.statusText
}
})
},
stringifyQuery = (data) => {
Expand Down
1 change: 0 additions & 1 deletion lib/utility.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { dirObject } from "./output.mjs";
* Obtain path name key from paths object by value.
*/
export const obtainName = (path, pathAll) => {
console.log({ path, pathAll });
const name = Object.entries(pathAll).find(entry =>
entry[1] === path
)[0];
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion request/bybit.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ const {
case "keyInformation": return keyInformation(...params);
case "marketHistory": return marketHistory(...params);
case "market":
case "marketInfo": return marketInfo(...params);
case "marketInfo":
case "marketInformation": return marketInfo(...params);
case "marketTickers": return marketTickers(...params);
case "networkAll": return networkAll(...params);
case "orderAll": return orderAll(...params);
Expand Down
2 changes: 1 addition & 1 deletion request/bybit/deposit/all.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const {
} = settings;

/**
* Documentation do not describe `txID`, but similar endpoint for withdraw also has this parameter.
* Note: documentation do not describe `txID`, but similar endpoint for withdraw also has this parameter.
* @see https://bybit-exchange.github.io/docs/v5/asset/deposit/deposit-record
*/
const depositAll = (coin, { cursor, endTime, limit, startTime } = {}) => {
Expand Down
2 changes: 1 addition & 1 deletion request/bybit/order/cancel-all.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Bybit API order cancel all endpoint.
*
* @module request/bybit/order/cancel
* @module request/bybit/order/cancel-all
*/

import config from "../../../configuration/bybit.json" with { type: "json" };
Expand Down
2 changes: 1 addition & 1 deletion request/bybit/order/cancel-one.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Bybit API order cancel one endpoint.
*
* @module request/bybit/order/cancel_one
* @module request/bybit/order/cancel-one
*/

import config from "../../../configuration/bybit.json" with { type: "json" };
Expand Down
12 changes: 6 additions & 6 deletions request/bybit/order/one.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Bybit API order all endpoint.
* Handle Bybit API order one endpoint.
*
* @module request/bybit/order/all
* @module request/bybit/order/one
*/

import config from "../../../configuration/bybit.json" with { type: "json" };
Expand All @@ -11,7 +11,7 @@ import bybitGet from "../get.mjs";

const {
PATH: {
ORDER_ALL
ORDER_ONE
},
} = config,
{
Expand Down Expand Up @@ -48,14 +48,14 @@ const orderOne = (orderId, {

if (Number(orderId))
data.orderId = orderId
else throwRequired(PATH, ORDER_ALL, "orderId");
else throwRequired(PATH, ORDER_ONE, "orderId");
if (symbol) {
if (typeof symbol === "string") {
data.symbol = symbol
} else warnOptional(PATH, ORDER_ALL, "symbol", data.symbol);
} else warnOptional(PATH, ORDER_ONE, "symbol", data.symbol);
}

return bybitGet(sign, ORDER_ALL, data)
return bybitGet(sign, ORDER_ONE, data)
};

export default orderOne;
15 changes: 14 additions & 1 deletion request/coinbase.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ import settings from "../settings/coinbase.json" with { type: "json" };
import accountAll from "./coinbase/account/all.mjs";
import accountBalance from "./coinbase/account/balance.mjs";
import accountWallets from "./coinbase/account/wallets.mjs";
import addressAll from "./coinbase/address/all.mjs";
import currencyAll from "./coinbase/currency/all.mjs";
import depositNew from "./coinbase/deposit/new.mjs";
import orderAll from "./coinbase/order/all.mjs";
import marketInfo from "./coinbase/market/info.mjs";
import marketHistory from "./coinbase/market/history.mjs";
import marketTickers from "./coinbase/market/tickers.mjs";
import orderAll from "./coinbase/order/all.mjs";
import orderCancelAll from "./coinbase/order/cancel-all.mjs";
import orderOne from "./coinbase/order/one.mjs";

const {
ACCOUNT,
Expand All @@ -35,10 +40,18 @@ const {
case "accountAll": return accountAll(...params);
case "accountBalance": return accountBalance(...params);
case "accountWallets": return accountWallets(...params);
case "addressAll": return addressAll(...params);
case "currencyAll": return currencyAll(...params);
case "depositNew": return depositNew(...params);
case "marketHistory": return marketHistory(...params);
case "market":
case "marketInfo":
case "marketInformation": return marketInfo(...params);
case "marketTickers": return marketTickers(...params);
case "orderAll": return orderAll(...params);
case "orderCancelAll": return orderCancelAll(...params);
case "order":
case "orderOne": return orderOne(...params);
default: throw new Error(requestCoinbase.name + ": " + optional(handler))
}
} else {
Expand Down
26 changes: 8 additions & 18 deletions request/coinbase/account/all.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,19 @@
*/

import coinbaseGet from "../get.mjs";
import isValid from "../validate.mjs";
import config from "../../../configuration/coinbase.json" with { type: "json" };
import validate from "../../../lib/validation.mjs";
import settings from "../../../settings/coinbase.json" with { type: "json" };

const {
PATH: {
ACCOUNT_ALL,
},
} = config,
{
authentication: {
sign
},
} = settings;
import isValidParams from "../validate.mjs";
import validateParams from "../../validate.mjs";

/**
* @see https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_getportfolios
*/
const accountAll = (memberIds) => {
const defaults = {},
data = validate(
ACCOUNT_ALL, isValid, defaults,
const { config, settings } = global.apiTools,
{ PATH: { ACCOUNT_ALL } } = config,
{ authentication: { sign } } = settings,
defaults = {},
data = validateParams(
ACCOUNT_ALL, isValidParams, defaults,
{ warnRequired: { memberIds } },
);

Expand Down
43 changes: 15 additions & 28 deletions request/coinbase/account/balance.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,27 @@
*/

import coinbaseGet from "../get.mjs";
import config from "../../../configuration/coinbase.json" with { type: "json" };
import validate from "../../../lib/validation.mjs";
import isValid from "../validate.mjs";
import settings from "../../../settings/coinbase.json" with { type: "json" };

const {
PATH: {
ACCOUNT_BALANCE,
},
} = config,
{
account,
account: {
wallet
},
authentication: {
sign
},
/* currency: {
base
} */
} = settings;
import isValidParams from "../validate.mjs";
import validateParams from "../../validate.mjs";

/**
* @see https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_getportfoliobreakdown
*/
const accountBalanceWallet = (asset, portfolio_uuid) => {
const defaults = {
const accountBalanceWallet = (asset, portfolio_id) => {
const { config, settings } = global.apiTools,
{ PATH: { ACCOUNT_BALANCE } } = config,
{
account,
account: { wallet },
authentication: { sign },
} = settings,
defaults = {
currency: "ETH",
portfolio_uuid: account[wallet],
portfolio_id: account[wallet],
},
data = validate(
ACCOUNT_BALANCE, isValid, defaults,
{ warnRequired: { portfolio_uuid } },
data = validateParams(
ACCOUNT_BALANCE, isValidParams, defaults,
{ warnRequired: { portfolio_id } },
);

return coinbaseGet(sign, ACCOUNT_BALANCE, data, { asset })
Expand Down
Loading