mainnet-js 3.0.0-next.0 → 3.0.0-next.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. package/dist/index.html +1 -1
  2. package/dist/{mainnet-3.0.0-next.0.js → mainnet-3.0.0-next.2.js} +21 -41
  3. package/dist/module/cache/walletCache.d.ts +10 -4
  4. package/dist/module/cache/walletCache.d.ts.map +1 -1
  5. package/dist/module/cache/walletCache.js +12 -13
  6. package/dist/module/cache/walletCache.js.map +1 -1
  7. package/dist/module/chain.d.ts +1 -1
  8. package/dist/module/chain.js +1 -1
  9. package/dist/module/chain.js.map +1 -1
  10. package/dist/module/constant.d.ts +1 -1
  11. package/dist/module/constant.d.ts.map +1 -1
  12. package/dist/module/constant.js +1 -1
  13. package/dist/module/constant.js.map +1 -1
  14. package/dist/module/enum.d.ts +1 -7
  15. package/dist/module/enum.d.ts.map +1 -1
  16. package/dist/module/enum.js +0 -6
  17. package/dist/module/enum.js.map +1 -1
  18. package/dist/module/history/getHistory.d.ts.map +1 -1
  19. package/dist/module/history/getHistory.js +17 -47
  20. package/dist/module/history/getHistory.js.map +1 -1
  21. package/dist/module/history/interface.d.ts +1 -1
  22. package/dist/module/history/interface.d.ts.map +1 -1
  23. package/dist/module/interface.d.ts +6 -5
  24. package/dist/module/interface.d.ts.map +1 -1
  25. package/dist/module/interface.js.map +1 -1
  26. package/dist/module/network/ElectrumNetworkProvider.d.ts.map +1 -1
  27. package/dist/module/network/ElectrumNetworkProvider.js +2 -4
  28. package/dist/module/network/ElectrumNetworkProvider.js.map +1 -1
  29. package/dist/module/network/getRelayFeeCache.js +2 -2
  30. package/dist/module/network/getRelayFeeCache.js.map +1 -1
  31. package/dist/module/network/interface.d.ts +2 -2
  32. package/dist/module/network/interface.d.ts.map +1 -1
  33. package/dist/module/rate/ExchangeRate.d.ts +2 -1
  34. package/dist/module/rate/ExchangeRate.d.ts.map +1 -1
  35. package/dist/module/rate/ExchangeRate.js +4 -1
  36. package/dist/module/rate/ExchangeRate.js.map +1 -1
  37. package/dist/module/transaction/Wif.d.ts +9 -9
  38. package/dist/module/transaction/Wif.d.ts.map +1 -1
  39. package/dist/module/transaction/Wif.js +35 -35
  40. package/dist/module/transaction/Wif.js.map +1 -1
  41. package/dist/module/transaction/allocateFee.d.ts +3 -3
  42. package/dist/module/transaction/allocateFee.d.ts.map +1 -1
  43. package/dist/module/transaction/allocateFee.js +5 -6
  44. package/dist/module/transaction/allocateFee.js.map +1 -1
  45. package/dist/module/util/amountInSatoshi.d.ts +1 -1
  46. package/dist/module/util/amountInSatoshi.d.ts.map +1 -1
  47. package/dist/module/util/amountInSatoshi.js +3 -9
  48. package/dist/module/util/amountInSatoshi.js.map +1 -1
  49. package/dist/module/util/asSendRequestObject.d.ts.map +1 -1
  50. package/dist/module/util/asSendRequestObject.js +10 -7
  51. package/dist/module/util/asSendRequestObject.js.map +1 -1
  52. package/dist/module/util/convert.d.ts +3 -0
  53. package/dist/module/util/convert.d.ts.map +1 -1
  54. package/dist/module/util/convert.js +12 -0
  55. package/dist/module/util/convert.js.map +1 -1
  56. package/dist/module/util/index.d.ts +2 -3
  57. package/dist/module/util/index.d.ts.map +1 -1
  58. package/dist/module/util/index.js +2 -3
  59. package/dist/module/util/index.js.map +1 -1
  60. package/dist/module/util/satoshiToAmount.d.ts +1 -1
  61. package/dist/module/util/satoshiToAmount.d.ts.map +1 -1
  62. package/dist/module/util/satoshiToAmount.js +3 -9
  63. package/dist/module/util/satoshiToAmount.js.map +1 -1
  64. package/dist/module/util/sumSendRequestAmounts.d.ts.map +1 -1
  65. package/dist/module/util/sumSendRequestAmounts.js +3 -4
  66. package/dist/module/util/sumSendRequestAmounts.js.map +1 -1
  67. package/dist/module/util/sumUtxoValue.d.ts +1 -1
  68. package/dist/module/util/sumUtxoValue.js +3 -3
  69. package/dist/module/util/sumUtxoValue.js.map +1 -1
  70. package/dist/module/wallet/Base.d.ts +28 -30
  71. package/dist/module/wallet/Base.d.ts.map +1 -1
  72. package/dist/module/wallet/Base.js +82 -119
  73. package/dist/module/wallet/Base.js.map +1 -1
  74. package/dist/module/wallet/HDWallet.d.ts +1 -1
  75. package/dist/module/wallet/Util.js +1 -1
  76. package/dist/module/wallet/Util.js.map +1 -1
  77. package/dist/module/wallet/Wif.d.ts +1 -1
  78. package/dist/module/wallet/interface.d.ts +2 -3
  79. package/dist/module/wallet/interface.d.ts.map +1 -1
  80. package/dist/module/wallet/model.d.ts +53 -41
  81. package/dist/module/wallet/model.d.ts.map +1 -1
  82. package/dist/module/wallet/model.js +14 -22
  83. package/dist/module/wallet/model.js.map +1 -1
  84. package/dist/tsconfig.tsbuildinfo +1 -1
  85. package/package.json +1 -1
  86. package/src/cache/walletCache.ts +23 -36
  87. package/src/chain.ts +1 -1
  88. package/src/constant.ts +1 -1
  89. package/src/enum.ts +0 -6
  90. package/src/history/getHistory.test.ts +20 -34
  91. package/src/history/getHistory.ts +17 -49
  92. package/src/history/interface.ts +1 -1
  93. package/src/interface.ts +6 -5
  94. package/src/network/Connection.test.ts +3 -3
  95. package/src/network/ElectrumNetworkProvider.ts +2 -4
  96. package/src/network/Rpc.test.ts +2 -3
  97. package/src/network/getRelayFeeCache.ts +2 -2
  98. package/src/network/interface.ts +2 -2
  99. package/src/rate/ExchangeRate.test.ts +2 -44
  100. package/src/rate/ExchangeRate.ts +5 -2
  101. package/src/transaction/Wif.ts +50 -45
  102. package/src/transaction/allocateFee.test.ts +110 -131
  103. package/src/transaction/allocateFee.ts +14 -15
  104. package/src/util/amountInSatoshi.test.ts +1 -9
  105. package/src/util/amountInSatoshi.ts +6 -10
  106. package/src/util/asSendRequestObject.ts +12 -7
  107. package/src/util/convert.ts +18 -0
  108. package/src/util/index.ts +2 -7
  109. package/src/util/satoshiToAmount.test.ts +1 -1
  110. package/src/util/satoshiToAmount.ts +4 -10
  111. package/src/util/sumSendRequestAmounts.ts +3 -4
  112. package/src/util/sumUtxoValue.ts +4 -4
  113. package/src/wallet/Base.ts +95 -164
  114. package/src/wallet/Cashtokens.test.headless.js +228 -173
  115. package/src/wallet/Cashtokens.test.ts +506 -406
  116. package/src/wallet/HDWallet.test.ts +223 -68
  117. package/src/wallet/Util.ts +1 -1
  118. package/src/wallet/Wif.test.ts +108 -133
  119. package/src/wallet/interface.ts +2 -3
  120. package/src/wallet/model.test.ts +2 -5
  121. package/src/wallet/model.ts +64 -71
  122. package/dist/module/util/balanceObjectFromSatoshi.d.ts +0 -8
  123. package/dist/module/util/balanceObjectFromSatoshi.d.ts.map +0 -1
  124. package/dist/module/util/balanceObjectFromSatoshi.js +0 -35
  125. package/dist/module/util/balanceObjectFromSatoshi.js.map +0 -1
  126. package/src/util/balanceObjectFromSatoshi.test.ts +0 -58
  127. package/src/util/balanceObjectFromSatoshi.ts +0 -52
@@ -3,6 +3,7 @@ import { WalletTypeEnum } from "../wallet/enum";
3
3
  import { createWallet } from "../wallet/createWallet";
4
4
  import { mine } from "../mine";
5
5
  import { encodeCashAddress } from "@bitauth/libauth";
6
+ import { toBch } from "../util";
6
7
 
7
8
  test("Should get miner history", async () => {
8
9
  const alice = await RegTestWallet.fromWIF(process.env.PRIVATE_WIF!);
@@ -28,33 +29,29 @@ test("Should get an address history", async () => {
28
29
  let sendResponse = await alice.send([
29
30
  {
30
31
  cashaddr: bob.cashaddr!,
31
- value: 31000,
32
- unit: "satoshis",
32
+ value: 31000n,
33
33
  },
34
34
  {
35
35
  cashaddr: charlie.cashaddr!,
36
- value: 41000,
37
- unit: "satoshis",
36
+ value: 41000n,
38
37
  },
39
38
  ]);
40
39
  await mine({ cashaddr: alice.getDepositAddress(), blocks: 10 });
41
40
  await bob.send([
42
41
  {
43
42
  cashaddr: charlie.cashaddr!,
44
- value: 2100,
45
- unit: "satoshis",
43
+ value: 2100n,
46
44
  },
47
45
  ]);
48
46
  await mine({ cashaddr: alice.getDepositAddress(), blocks: 1 });
49
47
  await bob.send([
50
48
  {
51
49
  cashaddr: alice.cashaddr!,
52
- value: 2100,
53
- unit: "satoshis",
50
+ value: 2100n,
54
51
  },
55
52
  ]);
56
53
  expect(sendResponse!.txId!.length).toBe(64);
57
- expect(sendResponse.balance!.bch).toBeGreaterThan(0.01);
54
+ expect(toBch(sendResponse.balance!)).toBeGreaterThan(0.01);
58
55
  await mine({ cashaddr: alice.getDepositAddress(), blocks: 10 });
59
56
 
60
57
  // Build Bob's wallet from a public address, check his balance.
@@ -110,28 +107,25 @@ test("Should get a history with multi-party sends", async () => {
110
107
  let sendResponse = await alice.send([
111
108
  {
112
109
  cashaddr: bob.cashaddr!,
113
- value: 31000,
114
- unit: "satoshis",
110
+ value: 31000n,
115
111
  },
116
112
  ]);
117
113
  await mine({ cashaddr: alice.getDepositAddress(), blocks: 10 });
118
114
  await bob.send([
119
115
  {
120
116
  cashaddr: charlie.cashaddr!,
121
- value: 2100,
122
- unit: "satoshis",
117
+ value: 2100n,
123
118
  },
124
119
  ]);
125
120
  await mine({ cashaddr: alice.getDepositAddress(), blocks: 1 });
126
121
  await bob.send([
127
122
  {
128
123
  cashaddr: alice.cashaddr!,
129
- value: 2100,
130
- unit: "satoshis",
124
+ value: 2100n,
131
125
  },
132
126
  ]);
133
127
  expect(sendResponse!.txId!.length).toBe(64);
134
- expect(sendResponse.balance!.bch).toBeGreaterThan(0.01);
128
+ expect(toBch(sendResponse.balance!)).toBeGreaterThan(0.01);
135
129
  await mine({ cashaddr: alice.getDepositAddress(), blocks: 1 });
136
130
 
137
131
  // Build Bob's wallet from a public address, check his balance.
@@ -190,31 +184,27 @@ test("Should cut results with a longer history to given count", async () => {
190
184
  let sendResponse = await alice.send([
191
185
  {
192
186
  cashaddr: bob.cashaddr!,
193
- value: 31000,
194
- unit: "satoshis",
187
+ value: 31000n,
195
188
  },
196
189
  ]);
197
190
  await mine({ cashaddr: alice.getDepositAddress(), blocks: 10 });
198
191
  await bob.send([
199
192
  {
200
193
  cashaddr: charlie.cashaddr!,
201
- value: 2100,
202
- unit: "satoshis",
194
+ value: 2100n,
203
195
  },
204
196
  {
205
197
  cashaddr: alice.cashaddr!,
206
- value: 2100,
207
- unit: "satoshis",
198
+ value: 2100n,
208
199
  },
209
200
  {
210
201
  cashaddr: alice.cashaddr!,
211
- value: 2100,
212
- unit: "satoshis",
202
+ value: 2100n,
213
203
  },
214
204
  ]);
215
205
  await mine({ cashaddr: alice.getDepositAddress(), blocks: 1 });
216
206
  expect(sendResponse!.txId!.length).toBe(64);
217
- expect(sendResponse.balance!.bch).toBeGreaterThan(0.01);
207
+ expect(toBch(sendResponse.balance!)).toBeGreaterThan(0.01);
218
208
  await mine({ cashaddr: alice.getDepositAddress(), blocks: 10 });
219
209
 
220
210
  // Build Bob's wallet from a public address, check his balance.
@@ -258,30 +248,26 @@ test("Should handle input and fee from many utxos", async () => {
258
248
  let sendResponse = await alice.send([
259
249
  {
260
250
  cashaddr: bob.cashaddr!,
261
- value: 600,
262
- unit: "satoshis",
251
+ value: 600n,
263
252
  },
264
253
  {
265
254
  cashaddr: bob.cashaddr!,
266
- value: 600,
267
- unit: "satoshis",
255
+ value: 600n,
268
256
  },
269
257
  {
270
258
  cashaddr: bob.cashaddr!,
271
- value: 600,
272
- unit: "satoshis",
259
+ value: 600n,
273
260
  },
274
261
  {
275
262
  cashaddr: bob.cashaddr!,
276
- value: 600,
277
- unit: "satoshis",
263
+ value: 600n,
278
264
  },
279
265
  ]);
280
266
  await mine({ cashaddr: alice.getDepositAddress(), blocks: 10 });
281
267
  await bob.sendMax(charlie.cashaddr!);
282
268
  await mine({ cashaddr: alice.getDepositAddress(), blocks: 1 });
283
269
  expect(sendResponse!.txId!.length).toBe(64);
284
- expect(sendResponse.balance!.bch).toBeGreaterThan(0.01);
270
+ expect(toBch(sendResponse.balance!)).toBeGreaterThan(0.01);
285
271
  await mine({ cashaddr: alice.getDepositAddress(), blocks: 10 });
286
272
 
287
273
  // Build Bob's wallet from a public address, check his balance.
@@ -22,38 +22,6 @@ type Transaction = TransactionCommon & {
22
22
  hash: string;
23
23
  };
24
24
 
25
- // export type AddressTxI = TxI & {
26
- // address: string;
27
- // }
28
-
29
- // export const getAddressHistory = async ({
30
- // address,
31
- // provider,
32
- // unit = "sat",
33
- // fromHeight = 0,
34
- // toHeight = -1,
35
- // start = 0,
36
- // count = -1,
37
- // }: {
38
- // address: string;
39
- // provider: NetworkProvider;
40
- // unit?: UnitEnum;
41
- // fromHeight?: number;
42
- // toHeight?: number;
43
- // start?: number;
44
- // count?: number;
45
- // }): Promise<TransactionHistoryItem[]> => {
46
- // return getHistory({
47
- // addresses: [address],
48
- // provider,
49
- // unit,
50
- // fromHeight,
51
- // toHeight,
52
- // start,
53
- // count,
54
- // });
55
- // }
56
-
57
25
  export const getHistory = async ({
58
26
  addresses,
59
27
  provider,
@@ -225,7 +193,7 @@ export const getHistory = async ({
225
193
  value: Number(prevoutOutput.valueSatoshis),
226
194
  token: prevoutOutput.token
227
195
  ? {
228
- tokenId: binToHex(prevoutOutput.token.category),
196
+ category: binToHex(prevoutOutput.token.category),
229
197
  amount: prevoutOutput.token.amount,
230
198
  capability: prevoutOutput.token.nft?.capability
231
199
  ? prevoutOutput.token.nft.capability
@@ -264,7 +232,7 @@ export const getHistory = async ({
264
232
  value: Number(output.valueSatoshis),
265
233
  token: output.token
266
234
  ? {
267
- tokenId: binToHex(output.token.category),
235
+ category: binToHex(output.token.category),
268
236
  amount: output.token.amount,
269
237
  capability: output.token.nft?.capability
270
238
  ? output.token.nft.capability
@@ -302,14 +270,14 @@ export const getHistory = async ({
302
270
  satoshiBalance -= input.value;
303
271
 
304
272
  if (input.token?.amount) {
305
- ftTokenBalances[input.token.tokenId] =
306
- (ftTokenBalances[input.token.tokenId] || BigInt(0)) -
273
+ ftTokenBalances[input.token.category] =
274
+ (ftTokenBalances[input.token.category] || BigInt(0)) -
307
275
  input.token.amount;
308
276
  }
309
277
 
310
- if (input.token?.capability) {
311
- nftTokenBalances[input.token.tokenId] =
312
- (nftTokenBalances[input.token.tokenId] || BigInt(0)) - 1n;
278
+ if (input.token?.nft?.capability) {
279
+ nftTokenBalances[input.token.category] =
280
+ (nftTokenBalances[input.token.category] || BigInt(0)) - 1n;
313
281
  }
314
282
  }
315
283
  });
@@ -318,36 +286,36 @@ export const getHistory = async ({
318
286
  satoshiBalance += Number(output.value);
319
287
 
320
288
  if (output.token?.amount) {
321
- ftTokenBalances[output.token.tokenId] =
322
- (ftTokenBalances[output.token.tokenId] || BigInt(0)) +
289
+ ftTokenBalances[output.token.category] =
290
+ (ftTokenBalances[output.token.category] || BigInt(0)) +
323
291
  output.token.amount;
324
292
  }
325
293
 
326
- if (output.token?.capability) {
327
- nftTokenBalances[output.token.tokenId] =
328
- (nftTokenBalances[output.token.tokenId] || BigInt(0)) + 1n;
294
+ if (output.token?.nft?.capability) {
295
+ nftTokenBalances[output.token.category] =
296
+ (nftTokenBalances[output.token.category] || BigInt(0)) + 1n;
329
297
  }
330
298
  }
331
299
  });
332
300
 
333
301
  tx.valueChange = satoshiBalance;
334
302
  tx.tokenAmountChanges = Object.entries(ftTokenBalances).map(
335
- ([tokenId, amount]) => ({
336
- tokenId,
303
+ ([category, amount]) => ({
304
+ category,
337
305
  amount,
338
306
  nftAmount: BigInt(0),
339
307
  })
340
308
  );
341
309
 
342
- for (const [tokenId, nftAmount] of Object.entries(nftTokenBalances)) {
310
+ for (const [category, nftAmount] of Object.entries(nftTokenBalances)) {
343
311
  const tokenChange = tx.tokenAmountChanges.find(
344
- (tokenChange) => tokenChange.tokenId === tokenId
312
+ (tokenChange) => tokenChange.category === category
345
313
  );
346
314
  if (tokenChange) {
347
315
  tokenChange.nftAmount = nftAmount;
348
316
  } else {
349
317
  tx.tokenAmountChanges.push({
350
- tokenId,
318
+ category: category,
351
319
  amount: BigInt(0),
352
320
  nftAmount,
353
321
  });
@@ -17,7 +17,7 @@ export interface TransactionHistoryItem {
17
17
  balance: number;
18
18
  valueChange: number;
19
19
  tokenAmountChanges: {
20
- tokenId: string;
20
+ category: string;
21
21
  amount: bigint;
22
22
  nftAmount: bigint;
23
23
  }[];
package/src/interface.ts CHANGED
@@ -18,7 +18,7 @@ export type Network = typeof Network[keyof typeof Network];
18
18
  export interface Utxo {
19
19
  txid: string;
20
20
  vout: number;
21
- satoshis: number;
21
+ satoshis: bigint;
22
22
  height?: number;
23
23
  coinbase?: boolean;
24
24
  token?: TokenI;
@@ -28,7 +28,6 @@ export interface Utxo {
28
28
  export interface UtxoId {
29
29
  txid: string;
30
30
  vout: number;
31
- satoshis: number;
32
31
  }
33
32
 
34
33
  export interface ElectrumBalanceI {
@@ -59,9 +58,11 @@ export interface HeaderI {
59
58
 
60
59
  export interface TokenI {
61
60
  amount: bigint;
62
- tokenId: string;
63
- capability?: NFTCapability;
64
- commitment?: string;
61
+ category: string;
62
+ nft?: {
63
+ capability: NFTCapability;
64
+ commitment: string;
65
+ };
65
66
  }
66
67
 
67
68
  export const NFTCapability = {
@@ -14,7 +14,7 @@ test("Should connect to mainnet", async () => {
14
14
 
15
15
  let wallet = await Wallet.newRandom();
16
16
  expect(wallet.provider == globalThis.BCH).toBeTruthy();
17
- expect(await wallet.getBalance("sat")).toBe(0);
17
+ expect(await wallet.getBalance()).toBe(0n);
18
18
 
19
19
  let height = await globalThis.BCH.getBlockHeight();
20
20
  expect(height).toBeGreaterThan(5000);
@@ -23,7 +23,7 @@ test("Should connect to mainnet", async () => {
23
23
  test("Should use global provider when creating testnet wallet", async () => {
24
24
  let wallet = await TestNetWallet.newRandom();
25
25
  expect(wallet.provider == globalThis.tBCH).toBeTruthy();
26
- expect(await wallet.getBalance("sat")).toBe(0);
26
+ expect(await wallet.getBalance()).toBe(0n);
27
27
 
28
28
  let height = await globalThis.tBCH.getBlockHeight();
29
29
  expect(height).toBeGreaterThan(114);
@@ -34,7 +34,7 @@ test.skip("Should lower overhead in creating wallets", async () => {
34
34
  for (let i = 0; i < 100; i++) {
35
35
  let wallet = await RegTestWallet.newRandom();
36
36
  expect(wallet.provider == globalThis.rBCH).toBeTruthy();
37
- expect(await wallet.getBalance("sat")).toBe(0);
37
+ expect(await wallet.getBalance()).toBe(0n);
38
38
  }
39
39
 
40
40
  let height = await globalThis.rBCH.getBlockHeight();
@@ -86,14 +86,12 @@ export default class ElectrumNetworkProvider implements NetworkProvider {
86
86
  address: cashaddr,
87
87
  txid: utxo.tx_hash,
88
88
  vout: utxo.tx_pos,
89
- satoshis: utxo.value,
89
+ satoshis: BigInt(utxo.value),
90
90
  height: utxo.height,
91
91
  token: utxo.token_data
92
92
  ? {
93
+ ...utxo.token_data,
93
94
  amount: BigInt(utxo.token_data.amount),
94
- tokenId: utxo.token_data.category,
95
- capability: utxo.token_data.nft?.capability,
96
- commitment: utxo.token_data.nft?.commitment,
97
95
  }
98
96
  : undefined,
99
97
  }));
@@ -73,7 +73,7 @@ describe("Rpc tests", () => {
73
73
 
74
74
  let result = false;
75
75
  aliceWallet.watchBalance((balance) => {
76
- expect(balance.bch).toBeGreaterThan(0);
76
+ expect(balance).toBeGreaterThan(0);
77
77
  result = true;
78
78
  // stop watching
79
79
  return true;
@@ -105,8 +105,7 @@ describe("Rpc tests", () => {
105
105
  await aliceWallet.send([
106
106
  {
107
107
  cashaddr: bobWallet.cashaddr!,
108
- value: 1000,
109
- unit: "satoshis",
108
+ value: 1000n,
110
109
  },
111
110
  ]);
112
111
 
@@ -15,9 +15,9 @@ export async function getRelayFeeCache(provider: NetworkProvider) {
15
15
  relayFeePerKbInCoins;
16
16
  }
17
17
  if (typeof relayFeePerKbInCoins === "number") {
18
- return Math.round(relayFeePerKbInCoins * bchParam.subUnits) / 1000;
18
+ return Math.round(relayFeePerKbInCoins * Number(bchParam.subUnits)) / 1000;
19
19
  } else {
20
20
  console.warn("Couldn't get min relay fee, using default instead");
21
- return Math.round(0.00001 * bchParam.subUnits) / 1000;
21
+ return Math.round(0.00001 * Number(bchParam.subUnits)) / 1000;
22
22
  }
23
23
  }
@@ -16,8 +16,8 @@ export interface ElectrumTokenData {
16
16
  amount: string;
17
17
  category: string;
18
18
  nft?: {
19
- capability?: NFTCapability;
20
- commitment?: string;
19
+ capability: NFTCapability;
20
+ commitment: string;
21
21
  };
22
22
  }
23
23
 
@@ -1,8 +1,5 @@
1
- import { removeFetchMock, setupFetchMock } from "../test/fetch";
2
- import { BalanceResponse } from "../util/balanceObjectFromSatoshi";
3
- import { RegTestWallet } from "../wallet/Wif";
4
- import { ExchangeRate, getRateFromExchange } from "./ExchangeRate";
5
- import { delay } from "../util/delay";
1
+ import { setupFetchMock } from "../test/fetch";
2
+ import { ExchangeRate } from "./ExchangeRate";
6
3
  import { initProviders, disconnectProviders } from "../network";
7
4
  import { Config } from "../config";
8
5
 
@@ -25,45 +22,6 @@ describe("Exchange rate tests", () => {
25
22
  expect(rate).toBe(1337.42);
26
23
  });
27
24
 
28
- test("Test watchBalanceUsd", async () => {
29
- setupFetchMock("https://markets.api.bitcoin.com/live/bitcoin", {
30
- data: {
31
- BCH: 1337.42,
32
- },
33
- });
34
-
35
- const alice = await RegTestWallet.fromId(process.env.ALICE_ID!);
36
- const bob = await RegTestWallet.newRandom();
37
- const balance = (await alice.getBalance()) as BalanceResponse;
38
- let cbCounter = 0;
39
- const cancelWatchFn = await alice.watchBalanceUsd(async (newBalance) => {
40
- cbCounter++;
41
- if (cbCounter === 1) {
42
- expect(newBalance.usd!).toBeGreaterThan(balance.usd!);
43
- }
44
- }, 3000);
45
-
46
- setupFetchMock("https://markets.api.bitcoin.com/live/bitcoin", {
47
- data: {
48
- BCH: 31337.42,
49
- },
50
- });
51
-
52
- await delay(3000);
53
-
54
- await alice.send({
55
- cashaddr: bob.getDepositAddress(),
56
- value: 10000,
57
- unit: "sat",
58
- });
59
-
60
- await delay(3000);
61
-
62
- removeFetchMock("https://markets.api.bitcoin.com/live/bitcoin");
63
- expect(cbCounter).toBe(2);
64
- await cancelWatchFn();
65
- });
66
-
67
25
  test("Test other currencies", async () => {
68
26
  const eurRate = await ExchangeRate.get("eur");
69
27
  expect(eurRate).toBeGreaterThan(0);
@@ -42,7 +42,7 @@ export class ExchangeRate {
42
42
  }
43
43
 
44
44
  static async getRateFromLocalStorage(
45
- symbol,
45
+ symbol: string,
46
46
  useCache = true
47
47
  ): Promise<number> {
48
48
  if (!useCache) {
@@ -136,4 +136,7 @@ export async function getRateFromExchange(symbol: string): Promise<number> {
136
136
  }
137
137
 
138
138
  // do not await and do not throw in case we are offline
139
- ExchangeRate.get("usd").catch(() => {});
139
+ // this promise can be used to warm up the cache
140
+ export const ExchageRatePromise = ExchangeRate.get(Config.DefaultCurrency)
141
+ .then((result) => result)
142
+ .catch((error: Error) => error);