Skip to content

Commit 678b47a

Browse files
authored
Merge pull request #28 from tipccjs/bug-fixes-walled
fix: Create transaction
2 parents 8c14cb9 + 6442409 commit 678b47a

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,5 @@ dist
130130
.pnp.*
131131

132132
/docs
133-
/lib
133+
/lib
134+
test.*

src/structures/CurrencyCache.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ export class CryptocurrencyCache extends CurrencyCache<CryptoCurrency> {
5454

5555
return this;
5656
}
57+
58+
public async fetch(
59+
code: string,
60+
cache = true,
61+
): Promise<CryptoCurrency | null> {
62+
if (cache && this.get(code)) return this.get(code);
63+
await this.refresh();
64+
return this.get(code);
65+
}
5766
}
5867

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

7281
return this;
7382
}
83+
84+
public async fetch(code: string, cache = true): Promise<FiatCurrency | null> {
85+
if (cache && this.get(code)) return this.get(code);
86+
await this.refresh();
87+
return this.get(code);
88+
}
7489
}
7590

7691
/**

src/structures/RequestHandler.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export class RequestHandler {
132132

133133
const rejectWithError = () => {
134134
if (response.data && response.data.error) {
135+
console.error(response);
135136
reject(new Error(response.data.error));
136137
} else {
137138
reject(new Error(response.data.error ?? 'Unknown error'));

src/structures/managers/TransactionManager.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import { Transaction } from '../Transaction';
1212
import { Cache } from '../Cache';
1313
import { EventEmitter } from 'stream';
1414

15-
interface RawValueTransaction {
15+
interface ValueTransaction {
1616
recipient_s: string | string[];
1717
value: string | number | BigNumber;
1818
currencyCode: string;
1919
}
2020

21-
interface ValueTransaction {
21+
interface RawValueTransaction {
2222
recipient_s: string | string[];
2323
valueRaw: string | number | BigNumber;
2424
currencyCode: string;
@@ -32,7 +32,7 @@ interface Events {
3232
}
3333

3434
const isRawValueTransaction = (payload: any): payload is RawValueTransaction =>
35-
payload && payload.valueRaw;
35+
payload && typeof payload.valueRaw !== 'undefined';
3636

3737
export class TransactionManager extends EventEmitter {
3838
public client: TipccClient;
@@ -96,16 +96,23 @@ export class TransactionManager extends EventEmitter {
9696
const recipients = Array.isArray(payload.recipient_s)
9797
? payload.recipient_s
9898
: [payload.recipient_s];
99-
const value = BigNumber(
100-
isRawValueTransaction(payload) ? payload.value : payload.valueRaw,
101-
).toFixed(40);
102-
const currencyCode = payload.currencyCode;
99+
100+
const currency = await this.client.cryptos.fetch(payload.currencyCode);
101+
if (!currency) throw new Error('Invalid currency code.');
102+
103+
const value = (
104+
isRawValueTransaction(payload)
105+
? new BigNumber(payload.valueRaw)
106+
: new BigNumber(payload.value).shiftedBy(currency.format.scale)
107+
).toFixed(0);
103108

104109
const tx = (await this.client.REST.post(Routes.tips(), {
105-
recipients: recipients,
110+
...(Array.isArray(payload.recipient_s)
111+
? { recipients }
112+
: { recipient: recipients[0] }),
106113
amount: {
107114
value,
108-
currency: currencyCode,
115+
currency: currency.code,
109116
},
110117
service: 'discord',
111118
} as RESTPostAPITipBody)) as RESTPostAPITipResult;

0 commit comments

Comments
 (0)