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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,5 @@ dist
.pnp.*

/docs
/lib
/lib
test.*
15 changes: 15 additions & 0 deletions src/structures/CurrencyCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ export class CryptocurrencyCache extends CurrencyCache<CryptoCurrency> {

return this;
}

public async fetch(
code: string,
cache = true,
): Promise<CryptoCurrency | null> {
if (cache && this.get(code)) return this.get(code);
await this.refresh();
return this.get(code);
}
}

/**
Expand All @@ -71,6 +80,12 @@ export class FiatCache extends CurrencyCache<FiatCurrency> {

return this;
}

public async fetch(code: string, cache = true): Promise<FiatCurrency | null> {
if (cache && this.get(code)) return this.get(code);
await this.refresh();
return this.get(code);
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/structures/RequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
25 changes: 16 additions & 9 deletions src/structures/managers/TransactionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down