From 97bfd95f4025a6547892c08d12bd545922fd6c8f Mon Sep 17 00:00:00 2001 From: Walledgarden Date: Sun, 30 Apr 2023 12:30:49 +0200 Subject: [PATCH 1/2] fix: Create transaction --- .gitignore | 3 +- src/structures/CurrencyCache.ts | 15 ++++++++++ src/structures/Manager/TransactionManager.ts | 29 +++++++++++++------- src/structures/RequestHandler.ts | 1 + 4 files changed, 37 insertions(+), 11 deletions(-) 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/Manager/TransactionManager.ts b/src/structures/Manager/TransactionManager.ts index 5c88b93..fefcc79 100644 --- a/src/structures/Manager/TransactionManager.ts +++ b/src/structures/Manager/TransactionManager.ts @@ -12,13 +12,13 @@ import { Transaction } from '../Transaction'; import { Cache } from '../Cache'; import { EventEmitter } from 'stream'; -export interface RawValueTransaction { +export interface ValueTransaction { recipient_s: string | string[]; value: string | number | BigNumber; currencyCode: string; } -export interface ValueTransaction { +export interface RawValueTransaction { recipient_s: string | string[]; valueRaw: string | number | BigNumber; currencyCode: string; @@ -33,9 +33,11 @@ export interface Events { export const isRawValueTransaction = ( payload: any, -): payload is RawValueTransaction => payload && payload.valueRaw; +): payload is RawValueTransaction => + payload && typeof payload.valueRaw !== 'undefined'; + export const isValueTransaction = (payload: any): payload is ValueTransaction => - payload && payload.value; + payload && typeof payload.value !== 'undefined'; export class TransactionManager extends EventEmitter { public client: TipccClient; @@ -99,16 +101,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; 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')); From 64424093d91310928e61c4c38acb072e6925255d Mon Sep 17 00:00:00 2001 From: ZeroWave022 <36341766+ZeroWave022@users.noreply.github.com> Date: Sun, 30 Apr 2023 15:09:03 +0200 Subject: [PATCH 2/2] fix: revert some merge changes --- src/structures/managers/TransactionManager.ts | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) 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;