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
14 changes: 8 additions & 6 deletions src/structures/Amount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) */
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}

Expand Down
23 changes: 12 additions & 11 deletions src/structures/RequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(() => {
Expand All @@ -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);
Expand All @@ -159,7 +160,7 @@ export class RequestHandler {
} else if (response.status === 429) {
retryRequest();
} else {
rejectWithError();
this._rejectResponse(response, reject);
}
});
});
Expand Down
6 changes: 0 additions & 6 deletions src/structures/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
6 changes: 0 additions & 6 deletions src/structures/Wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
}
9 changes: 3 additions & 6 deletions src/structures/managers/WalletManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
}