Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 72 additions & 15 deletions src/structures/CurrencyCache.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
import { CryptoCurrency, ExchangeRate, FiatCurrency, TipccClient } from '..';
import {
RESTGetAPICurrenciesCryptoCurrenciesResult,
RESTGetAPICurrenciesFiatsResult,
RESTGetAPICurrenciesRatesResult,
Routes,
} from '@tipccjs/tipcc-api-types/v0';

/**
* A class extending Array holding a cache of objects with type T.
*
* @category Client utilities
* @typeParam T - The object type this {@link CurrencyCache} will hold
*/
export class CurrencyCache<T> extends Array {
private refreshFunction: () => T[] | Promise<T[]>;
public client: TipccClient;

/**
* Create a CurrencyCache.
* @param refreshFunction The refresh function which returns new values to insert to this cache
* @param client The client which instantiated this CurrencyCache
*/
constructor(refreshFunction: () => T[] | Promise<T[]>) {
constructor(client: TipccClient) {
super();

this.refreshFunction = refreshFunction;
}

/**
* Refresh this CurrencyCache with new values received from refreshFunction.
*/
public async refresh(): Promise<CurrencyCache<T>> {
const refreshedData = await this.refreshFunction();
this.splice(0, this.length, refreshedData);

return this;
this.client = client;
}

/**
Expand All @@ -37,3 +34,63 @@ export class CurrencyCache<T> extends Array {
return null;
}
}

/**
* A class extending {@link CurrencyCache} holding cryptocurrencies.
*
* @category Client utilities
*/
export class CryptocurrencyCache extends CurrencyCache<CryptoCurrency> {
public async refresh(): Promise<CryptocurrencyCache> {
const { cryptocurrencies } = (await this.client.REST.get(
Routes.currenciesCryptocurrencies(),
)) as RESTGetAPICurrenciesCryptoCurrenciesResult;

this.splice(
0,
this.length,
...cryptocurrencies.map((c) => new CryptoCurrency(c)),
);

return this;
}
}

/**
* A class extending {@link CurrencyCache} holding fiats.
*
* @category Client utilities
*/
export class FiatCache extends CurrencyCache<FiatCurrency> {
public async refresh(): Promise<FiatCache> {
const { fiats } = (await this.client.REST.get(
Routes.currenciesFiats(),
)) as RESTGetAPICurrenciesFiatsResult;

this.splice(0, this.length, ...fiats.map((f) => new FiatCurrency(f)));

return this;
}
}

/**
* A class extending {@link CurrencyCache} holding exchange rates.
*
* @category Client utilities
*/
export class ExchangeRateCache extends CurrencyCache<ExchangeRate> {
public async refresh(): Promise<ExchangeRateCache> {
const { rates } = (await this.client.REST.get(
Routes.currenciesRates(),
)) as RESTGetAPICurrenciesRatesResult;
//return rates.map((r) => new ExchangeRate(r, this));

this.splice(
0,
this.length,
...rates.map((r) => new ExchangeRate(r, this.client)),
);

return this;
}
}
59 changes: 14 additions & 45 deletions src/structures/TipccClient.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { EventEmitter } from 'node:events';
import { RequestHandler } from './RequestHandler';
import { CurrencyCache } from './CurrencyCache';
import { CryptoCurrency, FiatCurrency } from './Currency';
import { ExchangeRate } from './ExchangeRate';

import {
RESTGetAPICurrenciesCryptoCurrenciesResult,
RESTGetAPICurrenciesFiatsResult,
RESTGetAPICurrenciesRatesResult,
Routes,
} from '@tipccjs/tipcc-api-types/v0';
CryptocurrencyCache,
ExchangeRateCache,
FiatCache,
} from './CurrencyCache';
import { WalletManager } from './Manager/WalletManager';
import { TransactionManager } from './Manager/TransactionManager';

Expand All @@ -27,7 +22,7 @@ export class TipccClient extends EventEmitter {
/** The tip.cc API token this client uses */
public token: string;

/** The {@link RequestHandler} this client uses */
/** The {@link RequestHandler} for this client */
public REST: RequestHandler;

/** The {@link WalletManager} for this client */
Expand All @@ -36,14 +31,14 @@ export class TipccClient extends EventEmitter {
/** The {@link TransactionManager} for this client */
public transactions: TransactionManager;

/** The {@link CurrencyCache} for cryptocurrencies */
public cryptos: CurrencyCache<CryptoCurrency>;
/** The {@link CryptocurrencyCache} for this client */
public cryptos: CryptocurrencyCache;

/** The {@link CurrencyCache} for fiat currencies */
public fiats: CurrencyCache<FiatCurrency>;
/** The {@link FiatCache} for this client */
public fiats: FiatCache;

/** The {@link CurrencyCache} for exchange rates */
public exchangeRates: CurrencyCache<ExchangeRate>;
/** The {@link ExchangeRateCache} for this client */
public exchangeRates: ExchangeRateCache;

/** A boolean indicating whether the client is ready */
public isReady = false;
Expand Down Expand Up @@ -86,9 +81,9 @@ export class TipccClient extends EventEmitter {
this.transactions = new TransactionManager({
client: this,
});
this.cryptos = new CurrencyCache(this._refreshCryptos);
this.fiats = new CurrencyCache(this._refreshFiats);
this.exchangeRates = new CurrencyCache(this._refreshExchangeRates);
this.cryptos = new CryptocurrencyCache(this);
this.fiats = new FiatCache(this);
this.exchangeRates = new ExchangeRateCache(this);

this.token = token;
this.REST = new RequestHandler(token, {
Expand Down Expand Up @@ -126,30 +121,4 @@ export class TipccClient extends EventEmitter {
public set emojis(emojis: Emoji[]) {
this._emojis = new Map(emojis.map(({ name, id }) => [name, id]));
}

private async _refreshCryptos(): Promise<CryptoCurrency[]> {
const { cryptocurrencies } = (await this.REST.request(
'GET',
Routes.currenciesCryptocurrencies(),
)) as RESTGetAPICurrenciesCryptoCurrenciesResult;

return cryptocurrencies.map((c) => new CryptoCurrency(c));
}

private async _refreshFiats(): Promise<FiatCurrency[]> {
const { fiats } = (await this.REST.request(
'GET',
Routes.currenciesFiats(),
)) as RESTGetAPICurrenciesFiatsResult;

return fiats.map((c) => new FiatCurrency(c));
}

private async _refreshExchangeRates(): Promise<ExchangeRate[]> {
const { rates } = (await this.REST.request(
'GET',
Routes.currenciesRates(),
)) as RESTGetAPICurrenciesRatesResult;
return rates.map((r) => new ExchangeRate(r, this));
}
}