Skip to content

Commit cb4a62b

Browse files
authored
Merge pull request #33 from ZeroWave022/bug-fixes
refactor: remove duplicate amounts, bug fixes
2 parents c2a4e8a + bfa7389 commit cb4a62b

File tree

5 files changed

+23
-35
lines changed

5 files changed

+23
-35
lines changed

src/structures/Amount.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class Amount {
3939
this.client?.exchangeRates.get(this.currency.code) ?? null;
4040
if (!exchangeRate) return null;
4141
if (!this.value) return null;
42-
return this.currency.convertByExchangeRate(this.value, exchangeRate);
42+
return this.currency.convertByExchangeRate(this.valueRaw, exchangeRate);
4343
}
4444

4545
/** The currency emoji (Discord Formatted) */
@@ -76,7 +76,7 @@ export class Amount {
7676
currency.format.units
7777
.filter((u) => u.min)
7878
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
79-
.find((u) => BigNumber(u.min!).lte(this.valueRaw)) ??
79+
.find((u) => BigNumber(u.min ?? 0).lte(this.valueRaw)) ??
8080
currency.format.units[0];
8181

8282
const usdValue = this.usdValue;
@@ -86,16 +86,18 @@ export class Amount {
8686
.toFixed(unit.optionalDecimals ?? unit.scale)
8787
.replace(/\.?0+$/, '');
8888

89+
const baseString = `${emoji ? `${emoji} ` : ''}**${preparedValue} ${
90+
unit.singular
91+
}**`;
92+
8993
if (includeUsd && usdValue) {
9094
const displayedUsd = usdValue.lt(0.01)
9195
? usdValue.toFixed(4)
9296
: usdValue.toFixed(2);
9397

94-
return `**${emoji ? `${emoji} ` : ''} ${preparedValue} ${
95-
unit.singular
96-
} **${`(≈ $${displayedUsd})`}`;
98+
return baseString + ` (≈ $${displayedUsd})`;
9799
} else {
98-
return `**${emoji ? `${emoji} ` : ''} ${preparedValue} ${unit.singular}`;
100+
return baseString;
99101
}
100102
}
101103

src/structures/RequestHandler.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ export class RequestHandler {
7070
}
7171
}
7272

73+
private _rejectResponse(response: any, rejectFunc: (e: Error) => void) {
74+
if (response.data && response.data.error) {
75+
console.error('tip.cc API request failed. Response was:');
76+
console.log(response);
77+
rejectFunc(new Error(response.data.error));
78+
} else {
79+
rejectFunc(new Error(response.data.error ?? 'Unknown error'));
80+
}
81+
}
82+
7383
/**
7484
* A shortcut for a GET request.
7585
* @param route The route to request
@@ -130,15 +140,6 @@ export class RequestHandler {
130140
.then((response) => {
131141
this._parseRateLimitHeaders(route, response.headers);
132142

133-
const rejectWithError = () => {
134-
if (response.data && response.data.error) {
135-
console.error(response);
136-
reject(new Error(response.data.error));
137-
} else {
138-
reject(new Error(response.data.error ?? 'Unknown error'));
139-
}
140-
};
141-
142143
const retryRequest = () => {
143144
if (response.headers['retry-after']) {
144145
setTimeout(() => {
@@ -147,7 +148,7 @@ export class RequestHandler {
147148
.catch(reject);
148149
}, +response.headers['retry-after']);
149150
} else {
150-
// Retry immediately if no retry-after header
151+
// Retry immediately if no retry-after header
151152
this.request(method, route, payload, requestOptions)
152153
.then(resolve)
153154
.catch(reject);
@@ -159,7 +160,7 @@ export class RequestHandler {
159160
} else if (response.status === 429) {
160161
retryRequest();
161162
} else {
162-
rejectWithError();
163+
this._rejectResponse(response, reject);
163164
}
164165
});
165166
});

src/structures/Transaction.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ export class Transaction {
2121
/** An instance of {@link Amount} for the fee of this transaction */
2222
public fee: Amount | null = null;
2323

24-
/** An instance of {@link Amount} for the USD value of this transaction */
25-
public usdValue: Amount | null = null;
26-
2724
/** The service in which this transaction took place */
2825
public service = 'discord' as const;
2926

@@ -55,9 +52,6 @@ export class Transaction {
5552
this.type = payload.type;
5653
this.amount = new Amount(payload.amount, this.client);
5754
this.fee = payload.fee ? new Amount(payload.fee, this.client) : null;
58-
this.usdValue = payload.usd_value
59-
? new Amount(payload.usd_value, this.client)
60-
: null;
6155
this.service = payload.service;
6256
this.chatId = payload.chat_id ?? null;
6357
this.subchatId = payload.subchat_id ?? null;

src/structures/Wallet.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ export class Wallet {
3131
/** The balance of this wallet */
3232
public balance: Amount;
3333

34-
/** The USD value of this wallet's balance */
35-
public usdValue: Amount | null = null;
36-
3734
/** The client that instantiated this */
3835
public client: TipccClient | undefined;
3936

@@ -46,8 +43,5 @@ export class Wallet {
4643
this.code = payload.code;
4744
this.name = payload.name;
4845
this.balance = new Amount(payload.balance, this.client);
49-
this.usdValue = payload.usd_value
50-
? new Amount(payload.usd_value, this.client)
51-
: null;
5246
}
5347
}

src/structures/managers/WalletManager.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import BigNumber from 'bignumber.js';
22
import { Wallet } from '../Wallet';
33
import { TipccClient } from '../TipccClient';
44
import { Cache } from '../Cache';
5-
import { FiatCurrency } from '../Currency';
65
import {
76
RESTGetAPIAccountWalletResult,
87
RESTGetAPIAccountWalletsResult,
@@ -67,12 +66,10 @@ export class WalletManager {
6766

6867
let total = BigNumber(0);
6968
for (const wallet of this.cache.values()) {
70-
if (!wallet.usdValue) continue;
71-
total = total.plus(wallet.usdValue?.valueRaw);
69+
if (!wallet.balance.usdValue) continue;
70+
total = total.plus(wallet.balance.usdValue);
7271
}
7372

74-
if (!this.client.fiats.get('USD')) this.client.fiats.refresh();
75-
76-
return (this.client.fiats.get('USD') as FiatCurrency).convertFromRaw(total);
73+
return total;
7774
}
7875
}

0 commit comments

Comments
 (0)