Add oracle rates
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { EventEmitter } from '../event-emitter.js';
|
||||
|
||||
/**
|
||||
* Events emitted by our Rates Adapters
|
||||
*/
|
||||
export type RatesEventMap = {
|
||||
rateUpdated: {
|
||||
numeratorUnitCode: string;
|
||||
denominatorUnitCode: string;
|
||||
price: number;
|
||||
};
|
||||
};
|
||||
|
||||
export abstract class BaseRates<
|
||||
T extends RatesEventMap = RatesEventMap,
|
||||
> extends EventEmitter<T> {
|
||||
/** Starts the given rates adapter so that it will emit events on price updates. */
|
||||
public abstract start(): Promise<void>;
|
||||
|
||||
/** Stops the given rates adapter so that it will stop checking for price updates. */
|
||||
public abstract stop(): Promise<void>;
|
||||
|
||||
/**
|
||||
* List all available market products (pairs).
|
||||
* @returns A set of strings in the format "NUMERATOR/DENOMINATOR"
|
||||
*/
|
||||
public abstract listPairs(): Promise<Set<string>>;
|
||||
|
||||
// TODO: Consider whether we actually want the below.
|
||||
// Ideally, we will want to replace this with something like the Units class:
|
||||
// See: https://gitlab.com/GeneralProtocols/xo/stack/-/issues/44
|
||||
/**
|
||||
* Format the amount in the target currency to the correct number of decimal places.
|
||||
*
|
||||
* @param {number} amount - The amount to format.
|
||||
* @param {string} targetCurrency - The target currency.
|
||||
*
|
||||
* @returns The formatted amount.
|
||||
*/
|
||||
public formatCurrency(amount: number, targetCurrency: string): string {
|
||||
const minimumFractionDigitsMap: { [currency: string]: number } = {
|
||||
AUD: 2,
|
||||
BCH: 8,
|
||||
USD: 2,
|
||||
};
|
||||
|
||||
const formatter = new Intl.NumberFormat('en-US', {
|
||||
style: 'currency',
|
||||
currency: targetCurrency,
|
||||
currencyDisplay: 'narrowSymbol',
|
||||
minimumFractionDigits: minimumFractionDigitsMap[targetCurrency] || 0,
|
||||
});
|
||||
|
||||
return formatter.format(amount);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user