diff --git a/.gitignore b/.gitignore index 3d54c58..162cf4c 100644 --- a/.gitignore +++ b/.gitignore @@ -130,4 +130,5 @@ dist .pnp.* /docs -/lib \ No newline at end of file +/lib +test.* \ No newline at end of file diff --git a/src/structures/CurrencyCache.ts b/src/structures/CurrencyCache.ts index bd90d98..e73f722 100644 --- a/src/structures/CurrencyCache.ts +++ b/src/structures/CurrencyCache.ts @@ -54,6 +54,15 @@ export class CryptocurrencyCache extends CurrencyCache { return this; } + + public async fetch( + code: string, + cache = true, + ): Promise { + if (cache && this.get(code)) return this.get(code); + await this.refresh(); + return this.get(code); + } } /** @@ -71,6 +80,12 @@ export class FiatCache extends CurrencyCache { return this; } + + public async fetch(code: string, cache = true): Promise { + if (cache && this.get(code)) return this.get(code); + await this.refresh(); + return this.get(code); + } } /** diff --git a/src/structures/RequestHandler.ts b/src/structures/RequestHandler.ts index b4a6228..86ec06b 100644 --- a/src/structures/RequestHandler.ts +++ b/src/structures/RequestHandler.ts @@ -132,6 +132,7 @@ export class RequestHandler { 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')); diff --git a/src/structures/managers/TransactionManager.ts b/src/structures/managers/TransactionManager.ts index a600d80..f5b44da 100644 --- a/src/structures/managers/TransactionManager.ts +++ b/src/structures/managers/TransactionManager.ts @@ -12,13 +12,13 @@ import { Transaction } from '../Transaction'; import { Cache } from '../Cache'; import { EventEmitter } from 'stream'; -interface RawValueTransaction { +interface ValueTransaction { recipient_s: string | string[]; value: string | number | BigNumber; currencyCode: string; } -interface ValueTransaction { +interface RawValueTransaction { recipient_s: string | string[]; valueRaw: string | number | BigNumber; currencyCode: string; @@ -32,7 +32,7 @@ interface Events { } const isRawValueTransaction = (payload: any): payload is RawValueTransaction => - payload && payload.valueRaw; + payload && typeof payload.valueRaw !== 'undefined'; export class TransactionManager extends EventEmitter { public client: TipccClient; @@ -96,16 +96,23 @@ export class TransactionManager extends EventEmitter { const recipients = Array.isArray(payload.recipient_s) ? payload.recipient_s : [payload.recipient_s]; - const value = BigNumber( - isRawValueTransaction(payload) ? payload.value : payload.valueRaw, - ).toFixed(40); - const currencyCode = payload.currencyCode; + + const currency = await this.client.cryptos.fetch(payload.currencyCode); + if (!currency) throw new Error('Invalid currency code.'); + + const value = ( + isRawValueTransaction(payload) + ? new BigNumber(payload.valueRaw) + : new BigNumber(payload.value).shiftedBy(currency.format.scale) + ).toFixed(0); const tx = (await this.client.REST.post(Routes.tips(), { - recipients: recipients, + ...(Array.isArray(payload.recipient_s) + ? { recipients } + : { recipient: recipients[0] }), amount: { value, - currency: currencyCode, + currency: currency.code, }, service: 'discord', } as RESTPostAPITipBody)) as RESTPostAPITipResult;