@renegade-fi/core
Version:
VanillaJS library for Renegade
56 lines • 2.38 kB
JavaScript
import axios from 'axios';
import { ResultAsync, errAsync, okAsync } from 'neverthrow';
import { PRICE_REPORTER_ROUTE } from '../../constants.js';
import { getDefaultQuoteToken } from '../../types/token.js';
import { HttpError, PriceReporterError } from './error.js';
const ERR_NO_PRICE_REPORTER_URL = 'PRICE_REPORTER_URL environment variable is not set';
const ERR_INVALID_URL = 'PRICE_REPORTER_URL must be a valid URL including protocol and port';
export class PriceReporterClient {
constructor(baseUrl) {
Object.defineProperty(this, "baseUrl", {
enumerable: true,
configurable: true,
writable: true,
value: baseUrl
});
}
/**
* Creates a new PriceReporterClient instance
* @returns A Result containing either a PriceReporterClient or a PriceReporterError
*/
static new() {
if (!process.env.PRICE_REPORTER_URL) {
return errAsync(new PriceReporterError(ERR_NO_PRICE_REPORTER_URL));
}
try {
const baseUrl = new URL(`https://${process.env.PRICE_REPORTER_URL}:3000`);
return ResultAsync.fromPromise(Promise.resolve(new PriceReporterClient(baseUrl.toString())), (err) => new PriceReporterError(err instanceof Error ? err.message : String(err)));
}
catch {
return errAsync(new PriceReporterError(ERR_INVALID_URL));
}
}
static getBinancePrice(address) {
const exchange = 'binance';
const quote = getDefaultQuoteToken(exchange).address;
return PriceReporterClient.new().andThen((client) => client.getPrice(exchange, address, quote));
}
getPrice(exchange, address, quote) {
const route = PRICE_REPORTER_ROUTE(exchange, address, quote);
return this.get(route).andThen((textPrice) => {
const price = Number(textPrice);
return okAsync(price);
});
}
get(route) {
const url = new URL(route, this.baseUrl);
const endpointUrl = url.toString();
const config = {
method: 'GET',
url: endpointUrl,
};
const safeRequest = ResultAsync.fromThrowable(() => axios.request(config), (error) => new HttpError(error, endpointUrl, config.method || 'GET'));
return safeRequest().map((response) => response.data);
}
}
//# sourceMappingURL=client.js.map