@bithomp/xrpl-api
Version:
A Bithomp JavaScript/TypeScript library for interacting with the XRP Ledger
155 lines (154 loc) • 5.03 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 __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.logger = exports.feeCushion = void 0;
exports.setup = setup;
exports.connect = connect;
exports.disconnect = disconnect;
exports.getNativeCurrency = getNativeCurrency;
exports.findConnection = findConnection;
const connection_1 = require("./connection");
const common_1 = require("./common");
__exportStar(require("./ledger"), exports);
exports.feeCushion = 1.3;
let clientConnections = [];
let loadBalancing = false;
let nativeCurrency = common_1.MAINNET_NATIVE_CURRENCY;
function setup(servers, options = {}) {
exports.logger = options.logger;
exports.logger?.debug({
service: "Bithomp::XRPL::Client",
function: "setup",
});
if (servers) {
disconnect();
clientConnections = [];
for (const server of servers) {
clientConnections.push(new connection_1.Connection(server.url, server.type, {
logger: options.logger,
timeout: server.timeout,
connectionTimeout: server.connectionTimeout,
networkID: server.networkID,
apiVersion: options.apiVersion,
}));
}
}
if (options.feeCushion) {
exports.feeCushion = options.feeCushion;
}
if (options.nativeCurrency) {
nativeCurrency = options.nativeCurrency;
}
loadBalancing = options.loadBalancing === true;
}
async function connect() {
exports.logger?.debug({
service: "Bithomp::XRPL::Client",
function: "connect",
});
for (const connection of clientConnections) {
await connection.connect();
}
}
async function disconnect() {
exports.logger?.debug({
service: "Bithomp::XRPL::Client",
function: "disconnect",
});
for (const connection of clientConnections) {
await connection.disconnect();
}
}
function getNativeCurrency() {
return nativeCurrency || common_1.MAINNET_NATIVE_CURRENCY;
}
function findConnection(type, url, strongFilter, hash, networkID) {
if (!strongFilter) {
if (clientConnections.length === 0) {
return null;
}
else if (clientConnections.length === 1) {
return clientConnections[0];
}
}
let connections = clientConnections.filter((con) => {
if (!con.isConnected()) {
return false;
}
if (typeof networkID === "number" && typeof con.getNetworkID() === "number") {
if (con.getNetworkID() !== networkID) {
return false;
}
}
if (typeof url === "string" && con.url !== url) {
return false;
}
if (typeof hash === "string" && con.hash !== hash) {
return false;
}
if (typeof type !== "string") {
return true;
}
if (con.types.length === 0) {
return false;
}
const foundTypes = type.split(",").map((t) => {
t = t.trim();
if (t[0] === "!") {
return !con.types.includes(t.slice(1));
}
else {
return con.types.includes(t);
}
});
for (const found of foundTypes) {
if (!found) {
return false;
}
}
return true;
});
if (strongFilter === undefined || strongFilter === false) {
if (connections.length === 0) {
connections = clientConnections.filter((con) => {
if (!con.isConnected()) {
return false;
}
if (typeof networkID === "number" && typeof con.getNetworkID() === "number") {
if (con.getNetworkID() !== networkID) {
return false;
}
}
return true;
});
}
}
if (loadBalancing) {
const index = Math.floor(Math.random() * connections.length);
return connections[index];
}
connections = connections.sort(sortHelperConnections);
return connections[0];
}
function sortHelperConnections(a, b) {
if (a.getLatencyMs() < b.getLatencyMs()) {
return -1;
}
if (a.getLatencyMs() > b.getLatencyMs()) {
return 1;
}
return 0;
}
;