okx-typescript-api
Version:
typescript module for okx.com API (hscomp2002@gmail.com)
183 lines (182 loc) • 7.24 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = __importDefault(require("axios"));
const Siner_1 = require("./Siner");
const query_string_1 = __importDefault(require("query-string"));
class HttpApi {
constructor(apiKey, apiSecret, passphrase, baseURL = "") {
baseURL = baseURL || "https://www.okx.com/";
this.apiSecret = apiSecret;
this.apiKey = apiKey;
this.passphrase = passphrase;
const config = {
baseURL,
};
this.apiClient = axios_1.default.create(config);
}
getSignedHeader(method, path, params = "") {
const signData = (0, Siner_1.sign)(this.apiSecret, path, params, method);
return {
"OK-ACCESS-KEY": this.apiKey,
"OK-ACCESS-TIMESTAMP": signData[0],
"OK-ACCESS-SIGN": signData[1],
"OK-ACCESS-PASSPHRASE": this.passphrase,
};
}
get(path, params = "") {
return __awaiter(this, void 0, void 0, function* () {
if (params) {
for (const key of Object.keys(params || {})) {
if (params[key] === null || params[key] === undefined)
delete params[key];
}
path += "?" + query_string_1.default.stringify(params);
}
const res = yield this.apiClient.get(path, {
headers: this.getSignedHeader("GET", path),
});
return res.data;
});
}
post(path, body) {
return __awaiter(this, void 0, void 0, function* () {
const headers = Object.assign(Object.assign({}, this.getSignedHeader("POST", path, body)), { "Content-Type": "application/json" });
const res = yield this.apiClient.post(path, body, { headers });
return res.data;
});
}
getTickers(instType) {
return __awaiter(this, void 0, void 0, function* () {
const { data: { data }, } = yield this.apiClient.get("/api/v5/market/tickers", {
params: { instType },
});
return data;
});
}
getAccountConfig() {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.get("/api/v5/account/config");
return res.data[0];
});
}
getBalance(coins = "") {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.get("/api/v5/account/balance", { ccy: coins });
return res.data[0];
});
}
getPositions(instType = "", instId = "", posId = "") {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.get("/api/v5/account/positions", { instType, instId, posId });
return res.data;
});
}
setLeverage(input) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.post("/api/v5/account/set-leverage", input);
return res.data[0];
});
}
placeOrder(input) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.post("/api/v5/trade/order", input);
return res.data[0];
});
}
placeMultipleOrders(input) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.post("/api/v5/trade/batch-orders", input);
return res.data;
});
}
cancelOrder(input) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.post("/api/v5/trade/cancel-order", input);
return res.data[0];
});
}
cancelMultipleOrders(input) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.post("/api/v5/trade/cancel-batch-orders", input);
return res.data;
});
}
getOrderDetails(instId, ordId = "", clOrdId = "") {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.get("/api/v5/trade/order", { instId, ordId, clOrdId });
return res.data[0];
});
}
getOrderList(input = undefined) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.get("/api/v5/trade/orders-pending", input);
return res.data;
});
}
getOrderHistory7DaysBefore(input) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.get("/api/v5/trade/orders-history", input);
return res.data;
});
}
getAlgoOrderList(input = undefined) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.get("/api/v5/trade/orders-algo-pending", input);
return res.data;
});
}
getAlgoOrderHistory(input = undefined) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.get("/api/v5/trade/orders-algo-history", input);
return res.data;
});
}
closePositions(input) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.post("/api/v5/trade/close-position", input);
return res.data;
});
}
placeAlgoOrder(input, input2) {
return __awaiter(this, void 0, void 0, function* () {
const data = Object.assign(Object.assign({}, input), input2);
const res = yield this.post("/api/v5/trade/order-algo", data);
return res.data[0];
});
}
cancelAlgoOrders(input) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.post("/api/v5/trade/cancel-algos", input);
return res.data[0];
});
}
getCurrencies(ccy = "") {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.get("/api/v5/asset/currencies", { ccy });
return res.data;
});
}
getInstruments(instType, uly = "", instId = "") {
return __awaiter(this, void 0, void 0, function* () {
const res = yield this.get("/api/v5/public/instruments", {
instType,
uly,
instId,
});
return res.data;
});
}
}
exports.default = HttpApi;