diff --git a/src/structures/Amount.ts b/src/structures/Amount.ts index 3c81bbd..a7e3073 100644 --- a/src/structures/Amount.ts +++ b/src/structures/Amount.ts @@ -39,7 +39,7 @@ export class Amount { this.client?.exchangeRates.get(this.currency.code) ?? null; if (!exchangeRate) return null; if (!this.value) return null; - return this.currency.convertByExchangeRate(this.value, exchangeRate); + return this.currency.convertByExchangeRate(this.valueRaw, exchangeRate); } /** The currency emoji (Discord Formatted) */ @@ -76,7 +76,7 @@ export class Amount { currency.format.units .filter((u) => u.min) // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - .find((u) => BigNumber(u.min!).lte(this.valueRaw)) ?? + .find((u) => BigNumber(u.min ?? 0).lte(this.valueRaw)) ?? currency.format.units[0]; const usdValue = this.usdValue; @@ -86,16 +86,18 @@ export class Amount { .toFixed(unit.optionalDecimals ?? unit.scale) .replace(/\.?0+$/, ''); + const baseString = `${emoji ? `${emoji} ` : ''}**${preparedValue} ${ + unit.singular + }**`; + if (includeUsd && usdValue) { const displayedUsd = usdValue.lt(0.01) ? usdValue.toFixed(4) : usdValue.toFixed(2); - return `**${emoji ? `${emoji} ` : ''} ${preparedValue} ${ - unit.singular - } **${`(≈ $${displayedUsd})`}`; + return baseString + ` (≈ $${displayedUsd})`; } else { - return `**${emoji ? `${emoji} ` : ''} ${preparedValue} ${unit.singular}`; + return baseString; } } diff --git a/src/structures/RequestHandler.ts b/src/structures/RequestHandler.ts index 86ec06b..17be580 100644 --- a/src/structures/RequestHandler.ts +++ b/src/structures/RequestHandler.ts @@ -70,6 +70,16 @@ export class RequestHandler { } } + private _rejectResponse(response: any, rejectFunc: (e: Error) => void) { + if (response.data && response.data.error) { + console.error('tip.cc API request failed. Response was:'); + console.log(response); + rejectFunc(new Error(response.data.error)); + } else { + rejectFunc(new Error(response.data.error ?? 'Unknown error')); + } + } + /** * A shortcut for a GET request. * @param route The route to request @@ -130,15 +140,6 @@ export class RequestHandler { .then((response) => { this._parseRateLimitHeaders(route, response.headers); - const rejectWithError = () => { - if (response.data && response.data.error) { - console.error(response); - reject(new Error(response.data.error)); - } else { - reject(new Error(response.data.error ?? 'Unknown error')); - } - }; - const retryRequest = () => { if (response.headers['retry-after']) { setTimeout(() => { @@ -147,7 +148,7 @@ export class RequestHandler { .catch(reject); }, +response.headers['retry-after']); } else { - // Retry immediately if no retry-after header + // Retry immediately if no retry-after header this.request(method, route, payload, requestOptions) .then(resolve) .catch(reject); @@ -159,7 +160,7 @@ export class RequestHandler { } else if (response.status === 429) { retryRequest(); } else { - rejectWithError(); + this._rejectResponse(response, reject); } }); }); diff --git a/src/structures/Transaction.ts b/src/structures/Transaction.ts index 13f95f1..0fc7e3d 100644 --- a/src/structures/Transaction.ts +++ b/src/structures/Transaction.ts @@ -21,9 +21,6 @@ export class Transaction { /** An instance of {@link Amount} for the fee of this transaction */ public fee: Amount | null = null; - /** An instance of {@link Amount} for the USD value of this transaction */ - public usdValue: Amount | null = null; - /** The service in which this transaction took place */ public service = 'discord' as const; @@ -55,9 +52,6 @@ export class Transaction { this.type = payload.type; this.amount = new Amount(payload.amount, this.client); this.fee = payload.fee ? new Amount(payload.fee, this.client) : null; - this.usdValue = payload.usd_value - ? new Amount(payload.usd_value, this.client) - : null; this.service = payload.service; this.chatId = payload.chat_id ?? null; this.subchatId = payload.subchat_id ?? null; diff --git a/src/structures/Wallet.ts b/src/structures/Wallet.ts index c76c144..c46cb75 100644 --- a/src/structures/Wallet.ts +++ b/src/structures/Wallet.ts @@ -31,9 +31,6 @@ export class Wallet { /** The balance of this wallet */ public balance: Amount; - /** The USD value of this wallet's balance */ - public usdValue: Amount | null = null; - /** The client that instantiated this */ public client: TipccClient | undefined; @@ -46,8 +43,5 @@ export class Wallet { this.code = payload.code; this.name = payload.name; this.balance = new Amount(payload.balance, this.client); - this.usdValue = payload.usd_value - ? new Amount(payload.usd_value, this.client) - : null; } } diff --git a/src/structures/managers/WalletManager.ts b/src/structures/managers/WalletManager.ts index 294e4d6..7e78ca8 100644 --- a/src/structures/managers/WalletManager.ts +++ b/src/structures/managers/WalletManager.ts @@ -2,7 +2,6 @@ import BigNumber from 'bignumber.js'; import { Wallet } from '../Wallet'; import { TipccClient } from '../TipccClient'; import { Cache } from '../Cache'; -import { FiatCurrency } from '../Currency'; import { RESTGetAPIAccountWalletResult, RESTGetAPIAccountWalletsResult, @@ -67,12 +66,10 @@ export class WalletManager { let total = BigNumber(0); for (const wallet of this.cache.values()) { - if (!wallet.usdValue) continue; - total = total.plus(wallet.usdValue?.valueRaw); + if (!wallet.balance.usdValue) continue; + total = total.plus(wallet.balance.usdValue); } - if (!this.client.fiats.get('USD')) this.client.fiats.refresh(); - - return (this.client.fiats.get('USD') as FiatCurrency).convertFromRaw(total); + return total; } }