nestjs-ccxt
Version:
CCXT module for Nest framework (node.js) 🚀
130 lines • 6.44 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CcxtService = void 0;
const ccxt = __importStar(require("ccxt"));
const common_1 = require("@nestjs/common");
const ccxt_constants_1 = require("./ccxt.constants");
let CcxtService = class CcxtService {
constructor(options) {
this.options = options;
this._clients = {};
this._lastMarketsFetch = {};
}
onModuleInit() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
if ((_a = this.options) === null || _a === void 0 ? void 0 : _a.defaultExchanges) {
const exchanges = Array.isArray(this.options.defaultExchanges)
? this.options.defaultExchanges
: [this.options.defaultExchanges];
const { ccxtOptions, loadMarketsOnStartup } = this.options;
yield Promise.all(exchanges.map(exchangeId => this.loadExchange(exchangeId, ccxtOptions, loadMarketsOnStartup)));
}
});
}
loadExchange(exchangeId, options, loadMarkets) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
this._clients[exchangeId] = new ccxt[exchangeId](Object.assign(Object.assign({}, (_a = this.options) === null || _a === void 0 ? void 0 : _a.ccxtOptions), options));
if (((_b = this.options) === null || _b === void 0 ? void 0 : _b.sandboxMode) === true)
this._clients[exchangeId].setSandboxMode(true);
if (((_c = this.options) === null || _c === void 0 ? void 0 : _c.verbose) === true)
this._clients[exchangeId].verbose = true;
if (loadMarkets === true)
yield this.loadExchangeMarkets(exchangeId);
});
}
loadExchangeMarkets(exchangeId) {
return __awaiter(this, void 0, void 0, function* () {
yield this._clients[exchangeId].loadMarkets(true);
this._lastMarketsFetch[exchangeId] = Date.now();
});
}
getExchange(exchangeId, options) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
if (!this._clients[exchangeId]) {
yield this.loadExchange(exchangeId, options, true);
}
if (this.marketsCacheExpired(exchangeId))
yield this.loadExchangeMarkets(exchangeId);
const userClient = new ccxt[exchangeId](options);
[
'ids',
'markets',
'markets_by_id',
'marketsById',
'currencies',
'currencies_by_id',
'baseCurrencies',
'quoteCurrencies',
'symbols',
'verbose',
].forEach(prop => {
userClient[prop] = this._clients[exchangeId][prop];
});
if (((_a = this.options) === null || _a === void 0 ? void 0 : _a.sandboxMode) === true)
userClient.setSandboxMode(true);
return userClient;
});
}
marketsCacheExpired(exchangeId) {
var _a;
const lastFetch = ((_a = this._lastMarketsFetch) === null || _a === void 0 ? void 0 : _a[exchangeId]) || 0;
return Date.now() - lastFetch > this.options.marketsCacheExpireMs;
}
getClient(exchangeId, options) {
return this.getExchange(exchangeId, options);
}
};
CcxtService = __decorate([
(0, common_1.Injectable)(),
__param(0, (0, common_1.Inject)(ccxt_constants_1.CCXT_MODULE_OPTIONS)),
__metadata("design:paramtypes", [Object])
], CcxtService);
exports.CcxtService = CcxtService;
//# sourceMappingURL=ccxt.service.js.map