UNPKG

test-zexcu

Version:

test securo

322 lines (321 loc) 14.7 kB
"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 }); exports.Securo = void 0; const moment_1 = __importDefault(require("moment")); const crypto = require('crypto'); const axios_1 = __importDefault(require("axios")); const baseUrl = "https://api.securo.dev/api/v1/"; class Securo { constructor(apiKey, secretKey) { this.getSession = (sessionId = null) => __awaiter(this, void 0, void 0, function* () { let url = this.baseUrl + "sessions"; if (sessionId) url = url + `/${sessionId}`; const config = this.configGeneration("GET", url); try { const { data } = yield (0, axios_1.default)(config); return data.data; } catch (error) { throw error.response.data.message; } }); this.expireSession = (sessionId) => __awaiter(this, void 0, void 0, function* () { let url = this.baseUrl + `sessions/${sessionId}/expire`; const config = this.configGeneration("POST", url); try { const { data } = yield (0, axios_1.default)(config); return data.message; } catch (error) { throw error.response.data.message; } }); this.indexFund = { getProducts: () => __awaiter(this, void 0, void 0, function* () { const url = this.baseUrl + "product/get-products"; const config = this.configGeneration("GET", url); try { const { data } = yield (0, axios_1.default)(config); return data.data; } catch (error) { throw error.response.data.message; } }), getPriceOf: (type) => __awaiter(this, void 0, void 0, function* () { const url = this.baseUrl + `product/price?symbol=${type}`; const config = this.configGeneration("GET", url); try { const { data } = yield (0, axios_1.default)(config); if (!data.data) return data.message; return data.data; } catch (error) { throw error.response.data.message; } }), getPoolValueOf: (type) => __awaiter(this, void 0, void 0, function* () { const url = this.baseUrl + `product/pool?symbol=${type}`; const config = this.configGeneration("GET", url); try { const { data } = yield (0, axios_1.default)(config); if (!data.data) return data.message; return data.data; } catch (error) { throw error.response.data.message; } }), createSession: (body) => __awaiter(this, void 0, void 0, function* () { const url = this.baseUrl + `sessions`; const config = this.configGeneration("POST", url, JSON.stringify(body)); try { const { data } = yield (0, axios_1.default)(config); if (!data.data) return data.message; return data.data; } catch (error) { throw error.response.data.message; } }) }; this.dexSwap = { getSupportedTokens: (chain) => __awaiter(this, void 0, void 0, function* () { const url = this.baseUrl + `web3/dex/swap/listTokens/${chain}`; const config = this.configGeneration("GET", url); try { const { data } = yield (0, axios_1.default)(config); return data.data; } catch (error) { throw error.response.data.message; } }), getSupportedPools: (chain) => __awaiter(this, void 0, void 0, function* () { const url = this.baseUrl + `web3/dex/swap/listPools/${chain}`; const config = this.configGeneration("GET", url); try { const { data } = yield (0, axios_1.default)(config); return data.data; } catch (error) { throw error.response.data.message; } }), getTokenPrice: (token) => __awaiter(this, void 0, void 0, function* () { const url = this.baseUrl + `web3/dex/price`; const config = this.configGeneration("POST", url, JSON.stringify({ token: token })); try { const { data } = yield (0, axios_1.default)(config); return data.data; } catch (error) { throw error.response.data.message; } }), getTokenAddress: (chainId, token) => __awaiter(this, void 0, void 0, function* () { const url = this.baseUrl + `web3/dex/contractAddressAndData`; const config = this.configGeneration("POST", url, JSON.stringify({ chainId: chainId, token: token })); try { const { data } = yield (0, axios_1.default)(config); return data.data; } catch (error) { throw error.response.data.message; } }), getEstimatedSwap: (chain, tokenIn, tokenOut, enteredAmount, slippagePerc, exactIn = true, chainId) => __awaiter(this, void 0, void 0, function* () { const url = this.baseUrl + `web3/dex/swap/estimate/?tokenOut=${tokenOut}&exactIn=${exactIn}&chain=${chain}&tokenIn=${tokenIn}&enteredAmount=${enteredAmount}`; const config = this.configGeneration("GET", url); try { const { data } = yield (0, axios_1.default)(config); return data.data; } catch (error) { throw error.response.data.message; } }), getLiquidity: (chain, tokenA, tokenB, amountA) => __awaiter(this, void 0, void 0, function* () { const url = this.baseUrl + `web3/dex/lpAmounts/?chain=${chain}&tokenA=${tokenA}&tokenB=${tokenB}&amountA=${amountA}`; const config = this.configGeneration("GET", url); try { const { data } = yield (0, axios_1.default)(config); return data.data; } catch (error) { throw error.response.data.message; } }), createSwapSession: (body) => __awaiter(this, void 0, void 0, function* () { const url = this.baseUrl + `sessions/swap`; const config = this.configGeneration("POST", url, JSON.stringify(body)); try { const { data } = yield (0, axios_1.default)(config); return data.data; } catch (error) { throw error.response.data.message; } }), createLiquiditySession: (body) => __awaiter(this, void 0, void 0, function* () { const url = this.baseUrl + `sessions/liquidity`; const config = this.configGeneration("POST", url, JSON.stringify(body)); try { const { data } = yield (0, axios_1.default)(config); return data.data; } catch (error) { throw error.response.data.message; } }) }; this.fiatToCrypto = { getPaymentCountries: () => __awaiter(this, void 0, void 0, function* () { const url = this.baseUrl + `payment/countries`; const config = this.configGeneration("GET", url); try { const { data } = yield (0, axios_1.default)(config); return data.data; } catch (error) { throw error.response.data.message; } }), getPaymentCryptoCurrencies: () => __awaiter(this, void 0, void 0, function* () { const url = this.baseUrl + `payment/crypto-currencies`; const config = this.configGeneration("GET", url); try { const { data } = yield (0, axios_1.default)(config); return data.data; } catch (error) { throw error.response.data.message; } }), getFiatCurrencies: () => __awaiter(this, void 0, void 0, function* () { const url = this.baseUrl + `payment/fiat-currencies`; const config = this.configGeneration("GET", url); try { const { data } = yield (0, axios_1.default)(config); return data.data; } catch (error) { throw error.response.data.message; } }), getEstimatedCurrencyRate: (body) => __awaiter(this, void 0, void 0, function* () { const url = this.baseUrl + `payment/currency-price`; const config = this.configGeneration("POST", url, JSON.stringify(body)); try { const { data } = yield (0, axios_1.default)(config); return data.data; } catch (error) { throw error.response.data.message; } }), createPaymentRequest: (body) => __awaiter(this, void 0, void 0, function* () { const url = this.baseUrl + `payment`; const config = this.configGeneration("POST", url, JSON.stringify(body)); try { const { data } = yield (0, axios_1.default)(config); return data.data; } catch (error) { throw error.response.data.message; } }), expirePaymentRequest: (invoiceId) => __awaiter(this, void 0, void 0, function* () { const url = this.baseUrl + `payment/expire/${invoiceId}`; const config = this.configGeneration("PATCH", url); try { const { data } = yield (0, axios_1.default)(config); return data; } catch (error) { throw error.response.data.message; } }), getPaymentHistory: (body) => __awaiter(this, void 0, void 0, function* () { const url = this.baseUrl + `payment/payment-history`; const config = this.configGeneration("GET", url, JSON.stringify(body)); try { const { data } = yield (0, axios_1.default)(config); return data.data; } catch (error) { throw error.response.data.message; } }), getByInvoiceId: (invoiceId) => __awaiter(this, void 0, void 0, function* () { const url = this.baseUrl + `payment/${invoiceId}`; const config = this.configGeneration("GET", url); try { const { data } = yield (0, axios_1.default)(config); return data.data; } catch (error) { throw error.response.data.message; } }), }; this.configGeneration = (method, url, body) => { const apiKey = this.apiKey; const secretKey = this.secretKey; const timeStamp = (0, moment_1.default)().unix(); console.log(timeStamp); let baseString = `${url}&method=${method}&timestamp=${timeStamp}`; if (body) { baseString += `&body=${JSON.stringify(JSON.parse(body))}`; } const hash = crypto.createHmac('sha256', secretKey).update(baseString).digest('hex'); let config = { method: method, url, headers: { 'x-sec-key': apiKey, 'x-sec-ts': timeStamp, 'x-sec-sign': hash, 'Content-Type': 'application/json', } }; if (body) { config = Object.assign(Object.assign({}, config), { data: body }); } return config; }; this.apiKey = apiKey; this.secretKey = secretKey; this.anything = "tester"; this.baseUrl = baseUrl; } sandboxMode() { this.baseUrl = "https://sandbox.securo.dev/api/v1/"; } maintenanceMode() { this.baseUrl = "https://test-api.securo.dev/api/v1/"; } getVersion() { this.anything = "1.0.0"; return { test: () => { return this.anything; } }; } } exports.Securo = Securo;