mainnet-js 3.1.7 → 4.0.0-next.10

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 (267) hide show
  1. package/dist/module/cache/IndexedDbCache.d.ts +1 -1
  2. package/dist/module/cache/IndexedDbCache.d.ts.map +1 -1
  3. package/dist/module/cache/MemoryCache.d.ts +1 -1
  4. package/dist/module/cache/MemoryCache.d.ts.map +1 -1
  5. package/dist/module/cache/WebStorageCache.d.ts +1 -1
  6. package/dist/module/cache/WebStorageCache.d.ts.map +1 -1
  7. package/dist/module/cache/walletCache.d.ts +16 -6
  8. package/dist/module/cache/walletCache.d.ts.map +1 -1
  9. package/dist/module/cache/walletCache.js +92 -34
  10. package/dist/module/cache/walletCache.js.map +1 -1
  11. package/dist/module/cli.js +1 -2
  12. package/dist/module/cli.js.map +1 -1
  13. package/dist/module/db/index.d.ts +1 -1
  14. package/dist/module/db/index.d.ts.map +1 -1
  15. package/dist/module/db/index.js +1 -1
  16. package/dist/module/db/index.js.map +1 -1
  17. package/dist/module/enum.d.ts +1 -1
  18. package/dist/module/enum.d.ts.map +1 -1
  19. package/dist/module/history/getHistory.d.ts +1 -1
  20. package/dist/module/history/getHistory.d.ts.map +1 -1
  21. package/dist/module/history/getHistory.js +3 -3
  22. package/dist/module/history/getHistory.js.map +1 -1
  23. package/dist/module/index.d.ts +25 -22
  24. package/dist/module/index.d.ts.map +1 -1
  25. package/dist/module/index.js +33 -26
  26. package/dist/module/index.js.map +1 -1
  27. package/dist/module/interface.d.ts +12 -2
  28. package/dist/module/interface.d.ts.map +1 -1
  29. package/dist/module/interface.js.map +1 -1
  30. package/dist/module/libauth.d.ts +1 -1
  31. package/dist/module/libauth.d.ts.map +1 -1
  32. package/dist/module/libauth.js +1 -1
  33. package/dist/module/libauth.js.map +1 -1
  34. package/dist/module/mine/mine.d.ts +2 -7
  35. package/dist/module/mine/mine.d.ts.map +1 -1
  36. package/dist/module/mine/mine.js +6 -27
  37. package/dist/module/mine/mine.js.map +1 -1
  38. package/dist/module/network/Connection.d.ts +1 -12
  39. package/dist/module/network/Connection.d.ts.map +1 -1
  40. package/dist/module/network/Connection.js +24 -33
  41. package/dist/module/network/Connection.js.map +1 -1
  42. package/dist/module/network/ElectrumNetworkProvider.d.ts +16 -17
  43. package/dist/module/network/ElectrumNetworkProvider.d.ts.map +1 -1
  44. package/dist/module/network/ElectrumNetworkProvider.js +91 -93
  45. package/dist/module/network/ElectrumNetworkProvider.js.map +1 -1
  46. package/dist/module/network/MockNetworkProvider.d.ts +28 -0
  47. package/dist/module/network/MockNetworkProvider.d.ts.map +1 -0
  48. package/dist/module/network/MockNetworkProvider.js +74 -0
  49. package/dist/module/network/MockNetworkProvider.js.map +1 -0
  50. package/dist/module/network/NetworkProvider.d.ts +9 -2
  51. package/dist/module/network/NetworkProvider.d.ts.map +1 -1
  52. package/dist/module/network/configuration.d.ts +2 -4
  53. package/dist/module/network/configuration.d.ts.map +1 -1
  54. package/dist/module/network/configuration.js +25 -50
  55. package/dist/module/network/configuration.js.map +1 -1
  56. package/dist/module/network/constant.d.ts +7 -7
  57. package/dist/module/network/constant.d.ts.map +1 -1
  58. package/dist/module/network/constant.js +21 -24
  59. package/dist/module/network/constant.js.map +1 -1
  60. package/dist/module/network/default.d.ts +5 -3
  61. package/dist/module/network/default.d.ts.map +1 -1
  62. package/dist/module/network/default.js +29 -51
  63. package/dist/module/network/default.js.map +1 -1
  64. package/dist/module/network/index.d.ts +4 -4
  65. package/dist/module/network/index.d.ts.map +1 -1
  66. package/dist/module/network/index.js +2 -2
  67. package/dist/module/network/index.js.map +1 -1
  68. package/dist/module/network/interface.d.ts +0 -6
  69. package/dist/module/network/interface.d.ts.map +1 -1
  70. package/dist/module/rate/ExchangeRate.d.ts +1 -1
  71. package/dist/module/rate/ExchangeRate.d.ts.map +1 -1
  72. package/dist/module/rate/ExchangeRate.js +13 -7
  73. package/dist/module/rate/ExchangeRate.js.map +1 -1
  74. package/dist/module/transaction/Wif.d.ts +3 -3
  75. package/dist/module/transaction/Wif.d.ts.map +1 -1
  76. package/dist/module/transaction/Wif.js +4 -4
  77. package/dist/module/transaction/Wif.js.map +1 -1
  78. package/dist/module/transaction/allocateFee.d.ts +1 -1
  79. package/dist/module/transaction/allocateFee.d.ts.map +1 -1
  80. package/dist/module/transaction/allocateFee.js +2 -2
  81. package/dist/module/transaction/allocateFee.js.map +1 -1
  82. package/dist/module/util/deriveCashaddr.js.map +1 -1
  83. package/dist/module/util/deriveNetwork.js +1 -1
  84. package/dist/module/util/deriveNetwork.js.map +1 -1
  85. package/dist/module/util/getAddrsByXpubKey.js +1 -1
  86. package/dist/module/util/getAddrsByXpubKey.js.map +1 -1
  87. package/dist/module/util/getXPubKey.js +1 -1
  88. package/dist/module/util/getXPubKey.js.map +1 -1
  89. package/dist/module/util/hd.d.ts +2 -0
  90. package/dist/module/util/hd.d.ts.map +1 -1
  91. package/dist/module/util/hd.js +41 -0
  92. package/dist/module/util/hd.js.map +1 -1
  93. package/dist/module/util/index.d.ts +7 -9
  94. package/dist/module/util/index.d.ts.map +1 -1
  95. package/dist/module/util/index.js +7 -9
  96. package/dist/module/util/index.js.map +1 -1
  97. package/dist/module/util/transaction.js +1 -1
  98. package/dist/module/util/transaction.js.map +1 -1
  99. package/dist/module/wallet/Base.d.ts +13 -9
  100. package/dist/module/wallet/Base.d.ts.map +1 -1
  101. package/dist/module/wallet/Base.js +54 -58
  102. package/dist/module/wallet/Base.js.map +1 -1
  103. package/dist/module/wallet/HDWallet.d.ts.map +1 -1
  104. package/dist/module/wallet/HDWallet.js +19 -38
  105. package/dist/module/wallet/HDWallet.js.map +1 -1
  106. package/dist/module/wallet/Util.d.ts +4 -22
  107. package/dist/module/wallet/Util.d.ts.map +1 -1
  108. package/dist/module/wallet/Util.js +75 -102
  109. package/dist/module/wallet/Util.js.map +1 -1
  110. package/dist/module/wallet/Watch.d.ts +24 -5
  111. package/dist/module/wallet/Watch.d.ts.map +1 -1
  112. package/dist/module/wallet/Watch.js +141 -17
  113. package/dist/module/wallet/Watch.js.map +1 -1
  114. package/dist/module/wallet/Wif.d.ts +2 -6
  115. package/dist/module/wallet/Wif.d.ts.map +1 -1
  116. package/dist/module/wallet/Wif.js +3 -69
  117. package/dist/module/wallet/Wif.js.map +1 -1
  118. package/dist/module/wallet/createWallet.d.ts +1 -1
  119. package/dist/module/wallet/createWallet.d.ts.map +1 -1
  120. package/dist/module/wallet/createWallet.js +2 -1
  121. package/dist/module/wallet/createWallet.js.map +1 -1
  122. package/dist/module/wallet/interface.d.ts +3 -2
  123. package/dist/module/wallet/interface.d.ts.map +1 -1
  124. package/dist/module/wallet/model.d.ts +2 -1
  125. package/dist/module/wallet/model.d.ts.map +1 -1
  126. package/dist/module/wallet/model.js +1 -1
  127. package/dist/module/wallet/model.js.map +1 -1
  128. package/package.json +17 -29
  129. package/dist/index.html +0 -9
  130. package/dist/mainnet-3.1.7.js +0 -2066
  131. package/dist/module/network/util.d.ts +0 -3
  132. package/dist/module/network/util.d.ts.map +0 -1
  133. package/dist/module/network/util.js +0 -27
  134. package/dist/module/network/util.js.map +0 -1
  135. package/dist/module/test/expect.d.ts +0 -12
  136. package/dist/module/test/expect.d.ts.map +0 -1
  137. package/dist/module/test/expect.js +0 -47
  138. package/dist/module/test/expect.js.map +0 -1
  139. package/dist/module/test/fetch.d.ts +0 -3
  140. package/dist/module/test/fetch.d.ts.map +0 -1
  141. package/dist/module/test/fetch.js +0 -32
  142. package/dist/module/test/fetch.js.map +0 -1
  143. package/dist/module/util/randomBytes.d.ts +0 -2
  144. package/dist/module/util/randomBytes.d.ts.map +0 -1
  145. package/dist/module/util/randomBytes.js +0 -13
  146. package/dist/module/util/randomBytes.js.map +0 -1
  147. package/dist/tsconfig.tsbuildinfo +0 -1
  148. package/src/cache/IndexedDbCache.test.ts +0 -15
  149. package/src/cache/IndexedDbCache.ts +0 -172
  150. package/src/cache/MemoryCache.test.ts +0 -15
  151. package/src/cache/MemoryCache.ts +0 -32
  152. package/src/cache/WebStorageCache.test.ts +0 -15
  153. package/src/cache/WebStorageCache.ts +0 -38
  154. package/src/cache/index.ts +0 -2
  155. package/src/cache/interface.ts +0 -9
  156. package/src/cache/walletCache.ts +0 -254
  157. package/src/chain.ts +0 -3
  158. package/src/cli.ts +0 -32
  159. package/src/config.ts +0 -23
  160. package/src/constant.ts +0 -27
  161. package/src/db/ExchangeRateProvider.ts +0 -28
  162. package/src/db/StorageProvider.ts +0 -64
  163. package/src/db/index.ts +0 -2
  164. package/src/db/interface.ts +0 -11
  165. package/src/enum.ts +0 -34
  166. package/src/history/getHistory.test.ts +0 -290
  167. package/src/history/getHistory.ts +0 -411
  168. package/src/history/interface.ts +0 -24
  169. package/src/index.ts +0 -48
  170. package/src/interface.ts +0 -72
  171. package/src/libauth.ts +0 -11
  172. package/src/message/index.ts +0 -2
  173. package/src/message/interface.ts +0 -40
  174. package/src/message/signed.test.ts +0 -309
  175. package/src/message/signed.ts +0 -201
  176. package/src/mine/index.ts +0 -1
  177. package/src/mine/mine.test.ts +0 -10
  178. package/src/mine/mine.ts +0 -42
  179. package/src/network/Connection.test.ts +0 -51
  180. package/src/network/Connection.ts +0 -73
  181. package/src/network/ElectrumNetworkProvider.ts +0 -657
  182. package/src/network/NetworkProvider.ts +0 -180
  183. package/src/network/Rpc.test.ts +0 -130
  184. package/src/network/configuration.test.ts +0 -59
  185. package/src/network/configuration.ts +0 -72
  186. package/src/network/constant.ts +0 -43
  187. package/src/network/default.ts +0 -120
  188. package/src/network/electrum.test.ts +0 -28
  189. package/src/network/getRelayFeeCache.test.ts +0 -15
  190. package/src/network/getRelayFeeCache.ts +0 -23
  191. package/src/network/index.ts +0 -14
  192. package/src/network/interface.ts +0 -80
  193. package/src/network/util.test.ts +0 -24
  194. package/src/network/util.ts +0 -30
  195. package/src/rate/ExchangeRate.test.headless.js +0 -35
  196. package/src/rate/ExchangeRate.test.ts +0 -51
  197. package/src/rate/ExchangeRate.ts +0 -142
  198. package/src/test/expect.ts +0 -59
  199. package/src/test/fetch.ts +0 -39
  200. package/src/test/json.test.ts +0 -13
  201. package/src/transaction/Wif.ts +0 -680
  202. package/src/transaction/allocateFee.test.ts +0 -298
  203. package/src/transaction/allocateFee.ts +0 -149
  204. package/src/util/amountInSatoshi.test.ts +0 -27
  205. package/src/util/amountInSatoshi.ts +0 -33
  206. package/src/util/asSendRequestObject.ts +0 -81
  207. package/src/util/base64.test.ts +0 -39
  208. package/src/util/base64.ts +0 -12
  209. package/src/util/browserNotSupported.ts +0 -7
  210. package/src/util/checkForEmptySeed.ts +0 -9
  211. package/src/util/checkUtxos.ts +0 -29
  212. package/src/util/checkWifNetwork.ts +0 -24
  213. package/src/util/convert.test.ts +0 -46
  214. package/src/util/convert.ts +0 -50
  215. package/src/util/delay.ts +0 -3
  216. package/src/util/deriveCashaddr.test.ts +0 -164
  217. package/src/util/deriveCashaddr.ts +0 -143
  218. package/src/util/deriveLockscript.ts +0 -16
  219. package/src/util/deriveNetwork.ts +0 -19
  220. package/src/util/derivePublicKeyHash.test.ts +0 -55
  221. package/src/util/derivePublicKeyHash.ts +0 -64
  222. package/src/util/floor.test.ts +0 -21
  223. package/src/util/floor.ts +0 -4
  224. package/src/util/getAddrsByXpubKey.test.ts +0 -115
  225. package/src/util/getAddrsByXpubKey.ts +0 -86
  226. package/src/util/getRuntimePlatform.test.headless.js +0 -40
  227. package/src/util/getRuntimePlatform.test.ts +0 -5
  228. package/src/util/getRuntimePlatform.ts +0 -31
  229. package/src/util/getUsdRate.ts +0 -5
  230. package/src/util/getXPubKey.ts +0 -39
  231. package/src/util/hash160.test.ts +0 -18
  232. package/src/util/hash160.ts +0 -12
  233. package/src/util/hd.ts +0 -16
  234. package/src/util/header.test.ts +0 -34
  235. package/src/util/header.ts +0 -26
  236. package/src/util/index.ts +0 -33
  237. package/src/util/randomBytes.ts +0 -13
  238. package/src/util/randomInt.test.ts +0 -15
  239. package/src/util/randomInt.ts +0 -4
  240. package/src/util/sanitizeAddress.ts +0 -10
  241. package/src/util/sanitizeUnit.ts +0 -11
  242. package/src/util/satoshiToAmount.test.ts +0 -6
  243. package/src/util/satoshiToAmount.ts +0 -33
  244. package/src/util/sumSendRequestAmounts.ts +0 -34
  245. package/src/util/sumUtxoValue.ts +0 -27
  246. package/src/util/transaction.ts +0 -10
  247. package/src/wallet/Base.ts +0 -1563
  248. package/src/wallet/Cashtokens.test.headless.js +0 -730
  249. package/src/wallet/Cashtokens.test.ts +0 -1411
  250. package/src/wallet/HDWallet.test.ts +0 -1086
  251. package/src/wallet/HDWallet.ts +0 -992
  252. package/src/wallet/Util.test.ts +0 -134
  253. package/src/wallet/Util.ts +0 -191
  254. package/src/wallet/WalletCache.test.ts +0 -45
  255. package/src/wallet/Watch.ts +0 -441
  256. package/src/wallet/Wif.bip39.test.ts +0 -48
  257. package/src/wallet/Wif.test.ts +0 -1189
  258. package/src/wallet/Wif.ts +0 -687
  259. package/src/wallet/Wif.watchOnly.test.ts +0 -58
  260. package/src/wallet/createWallet.ts +0 -238
  261. package/src/wallet/enum.ts +0 -18
  262. package/src/wallet/interface.ts +0 -102
  263. package/src/wallet/model.test.ts +0 -24
  264. package/src/wallet/model.ts +0 -352
  265. package/tsconfig.browser.json +0 -11
  266. package/tsconfig.json +0 -33
  267. package/webpack.config.cjs +0 -132
@@ -1,80 +0,0 @@
1
- import { NFTCapability } from "../interface.js";
2
-
3
- export interface BlockHeader {
4
- height: number;
5
- hex: string;
6
- }
7
-
8
- export interface ElectrumHostParams {
9
- host: string;
10
- port: number;
11
- timeout?: number;
12
- scheme: "tcp" | "tcp_tls" | "ws" | "wss";
13
- }
14
-
15
- export interface ElectrumTokenData {
16
- amount: string;
17
- category: string;
18
- nft?: {
19
- capability: NFTCapability;
20
- commitment: string;
21
- };
22
- }
23
-
24
- export interface ElectrumUtxo {
25
- tx_pos: number;
26
- value: number;
27
- tx_hash: string;
28
- height: number;
29
- token_data?: ElectrumTokenData;
30
- }
31
-
32
- export interface ElectrumRawTransaction {
33
- blockhash: string;
34
- blocktime: number;
35
- confirmations: number;
36
- hash: string;
37
- hex: string;
38
- locktime: number;
39
- size: number;
40
- time: number;
41
- txid: string;
42
- version: number;
43
- vin: ElectrumRawTransactionVin[];
44
- vout: ElectrumRawTransactionVout[];
45
- }
46
-
47
- export interface ElectrumRawTransactionVinScriptSig {
48
- asm: string;
49
- hex: string;
50
- }
51
-
52
- export interface ElectrumRawTransactionVin {
53
- scriptSig: ElectrumRawTransactionVinScriptSig;
54
- sequence: number;
55
- txid: string;
56
- vout: number;
57
- }
58
-
59
- export type ElectrumRawTransactionVinWithValues = ElectrumRawTransactionVin &
60
- ElectrumRawTransactionVout;
61
-
62
- export interface ElectrumRawTransactionWithInputValues
63
- extends Omit<ElectrumRawTransaction, "vin"> {
64
- vin: ElectrumRawTransactionVinWithValues[];
65
- }
66
-
67
- export interface ElectrumRawTransactionVout {
68
- n: number;
69
- scriptPubKey: ElectrumRawTransactionVoutScriptPubKey;
70
- value: number;
71
- tokenData?: ElectrumTokenData;
72
- }
73
-
74
- export interface ElectrumRawTransactionVoutScriptPubKey {
75
- addresses: string[];
76
- asm: string;
77
- hex: string;
78
- reqSigs: number;
79
- type: string;
80
- }
@@ -1,24 +0,0 @@
1
- import { parseElectrumUrl } from "./util";
2
-
3
- test("Should parse various electrum urls", async () => {
4
- expect(parseElectrumUrl("https://localhost:1")).toEqual({
5
- host: "localhost",
6
- port: 1,
7
- scheme: "tcp_tls",
8
- });
9
- expect(parseElectrumUrl("http://127.0.0.1:1")).toEqual({
10
- host: "127.0.0.1",
11
- port: 1,
12
- scheme: "tcp",
13
- });
14
- expect(parseElectrumUrl("ws://1.2.3.4:10001")).toEqual({
15
- host: "1.2.3.4",
16
- port: 10001,
17
- scheme: "ws",
18
- });
19
- expect(parseElectrumUrl("wss://example.space:1")).toEqual({
20
- host: "example.space",
21
- port: 1,
22
- scheme: "wss",
23
- });
24
- });
@@ -1,30 +0,0 @@
1
- import { ElectrumHostParams } from "./interface.js";
2
-
3
- export function parseElectrumUrl(givenUrl: string): ElectrumHostParams {
4
- let url = new URL(givenUrl);
5
- let port = parseInt(url.port || "443");
6
- let scheme = getElectrumScheme(url.protocol);
7
-
8
- return { host: url.hostname, port: port, scheme: scheme };
9
- }
10
-
11
- function getElectrumScheme(protocol: string) {
12
- let transport: string;
13
- switch (protocol) {
14
- case "http:":
15
- transport = "tcp";
16
- break;
17
- case "https:":
18
- transport = "tcp_tls";
19
- break;
20
- case "ws:":
21
- transport = "ws";
22
- break;
23
- case "wss:":
24
- transport = "wss";
25
- break;
26
- default:
27
- throw Error("Electrum transport protocol not understood.");
28
- }
29
- return transport as "tcp" | "tcp_tls" | "ws" | "wss";
30
- }
@@ -1,35 +0,0 @@
1
- const playwright = require("playwright");
2
- const PAGE_URL = "http://localhost:8080";
3
-
4
- describe("Exchange rate tests", () => {
5
- let browser;
6
- let page;
7
-
8
- /**
9
- * Create the browser and page context
10
- */
11
- beforeAll(async () => {
12
- browser = await playwright["chromium"].launch();
13
- page = await browser.newPage();
14
-
15
- if (!page) {
16
- throw new Error("Connection wasn't established");
17
- }
18
-
19
- // Open the page
20
- await page.goto(PAGE_URL, {
21
- waitUntil: "networkidle0",
22
- });
23
- });
24
-
25
- afterAll(async () => {
26
- await browser.close();
27
- });
28
-
29
- test("Get price in usd", async () => {
30
- await page.evaluate(async () => {
31
- let rate = await ExchangeRate.get("usd");
32
- expect(rate).toBeGreaterThan(0);
33
- }, []);
34
- });
35
- });
@@ -1,51 +0,0 @@
1
- import { setupFetchMock } from "../test/fetch";
2
- import { ExchangeRate } from "./ExchangeRate";
3
- import { initProviders, disconnectProviders } from "../network";
4
- import { Config } from "../config";
5
-
6
- beforeAll(async () => {
7
- await initProviders();
8
- });
9
- afterAll(async () => {
10
- await disconnectProviders();
11
- });
12
-
13
- describe("Exchange rate tests", () => {
14
- test("Get price in usd", async () => {
15
- setupFetchMock("https://markets.api.bitcoin.com/live/bitcoin", {
16
- data: {
17
- BCH: 1337.42,
18
- },
19
- });
20
-
21
- let rate = await ExchangeRate.get("usd");
22
- expect(rate).toBe(1337.42);
23
- });
24
-
25
- test("Test other currencies", async () => {
26
- const eurRate = await ExchangeRate.get("eur");
27
- expect(eurRate).toBeGreaterThan(0);
28
- });
29
-
30
- test("Test non-existing currency", async () => {
31
- await expect(ExchangeRate.get("xyz")).rejects.toThrow();
32
- });
33
-
34
- test("Test custom exchange rate function", async () => {
35
- Config.GetExchangeRateFn = async (symbol: string): Promise<number> => {
36
- if (symbol === "usd") {
37
- return 5555.55;
38
- }
39
- throw new Error("Unsupported currency");
40
- };
41
-
42
- const usdRate = await ExchangeRate.get("usd", false);
43
- expect(usdRate).toBe(5555.55);
44
-
45
- await expect(ExchangeRate.get("eur", false)).rejects.toThrow(
46
- "Unsupported currency"
47
- );
48
-
49
- Config.GetExchangeRateFn = undefined;
50
- });
51
- });
@@ -1,142 +0,0 @@
1
- import { EXCHANGE_RATE_TTL } from "../constant.js";
2
- import {
3
- RuntimePlatform,
4
- getRuntimePlatform,
5
- } from "../util/getRuntimePlatform.js";
6
- import ExchangeRateProvider from "../db/ExchangeRateProvider.js";
7
- import { Config } from "../config.js";
8
-
9
- export class ExchangeRate {
10
- symbol: string;
11
- rate: number;
12
- ttl: number;
13
- constructor({
14
- symbol,
15
- rate,
16
- ttl,
17
- }: {
18
- symbol: string;
19
- rate: number;
20
- ttl: number;
21
- }) {
22
- this.symbol = symbol;
23
- this.rate = rate;
24
- this.ttl = ttl;
25
- }
26
-
27
- toString() {
28
- this.rate.toFixed(2);
29
- }
30
-
31
- static async get(symbol: string, useCache = true) {
32
- const platform = getRuntimePlatform();
33
- if (platform === RuntimePlatform.browser) {
34
- try {
35
- return await this.getRateFromLocalStorage(symbol, useCache);
36
- } catch {
37
- return await this.getRateFromGlobalScope(symbol, useCache);
38
- }
39
- } else {
40
- return await this.getRateFromGlobalScope(symbol, useCache);
41
- }
42
- }
43
-
44
- static async getRateFromLocalStorage(
45
- symbol: string,
46
- useCache = true
47
- ): Promise<number> {
48
- if (!useCache) {
49
- return await getRateFromExchange(symbol);
50
- }
51
-
52
- let cache = new ExchangeRateProvider();
53
- let cacheRate = await cache.getRate(symbol);
54
- if (cacheRate) {
55
- // if the cache is still good, return it
56
- if (cacheRate.ttl > Date.now()) {
57
- return cacheRate.rate;
58
- }
59
- // else fall through
60
- }
61
- let freshRate = await getRateFromExchange(symbol);
62
- cache.setRate(symbol, freshRate, getTtl());
63
- return freshRate;
64
- }
65
-
66
- static async getRateFromGlobalScope(symbol, useCache = true) {
67
- if (!useCache) {
68
- return await getRateFromExchange(symbol);
69
- }
70
-
71
- if (globalThis.RATE) {
72
- let rateObject = globalThis.RATE;
73
- if (symbol in rateObject) {
74
- let cachedRate = rateObject[symbol] as ExchangeRate;
75
- // If the cache is still good return ie
76
- if (cachedRate.ttl > Date.now()) {
77
- return cachedRate.rate;
78
- }
79
- }
80
- }
81
- let freshRate = await getRateFromExchange(symbol);
82
- this.cacheRateInGlobalScope(symbol, freshRate);
83
- return freshRate;
84
- }
85
-
86
- static cacheRateInGlobalScope(symbol, rate) {
87
- if (!globalThis.RATE) {
88
- globalThis.RATE = {};
89
- } else {
90
- globalThis.RATE[symbol] = {
91
- symbol: symbol,
92
- rate: rate,
93
- ttl: getTtl(),
94
- };
95
- }
96
- }
97
- }
98
-
99
- export function getTtl() {
100
- return Math.trunc(Date.now() + EXCHANGE_RATE_TTL);
101
- }
102
-
103
- // Attempt to get the usd rate from some web app
104
- export async function getRateFromExchange(symbol: string): Promise<number> {
105
- if (Config.GetExchangeRateFn) {
106
- return await Config.GetExchangeRateFn(symbol);
107
- }
108
-
109
- if (symbol.length > 0) {
110
- symbol = symbol.toLocaleLowerCase();
111
- }
112
-
113
- if (symbol === "usd") {
114
- try {
115
- const response = await fetch(
116
- "https://markets.api.bitcoin.com/live/bitcoin"
117
- );
118
- const data = await response.json();
119
- return data["data"]["BCH"];
120
- } catch {}
121
- }
122
-
123
- const response = await fetch("https://bitpay.com/rates/BCH");
124
- const data = (await response.json()) as {
125
- data: { code: string; rate: number }[];
126
- };
127
- const rates = data.data.reduce(
128
- (acc, rate) => ({ ...acc, [rate.code.toLocaleLowerCase()]: rate.rate }),
129
- {}
130
- );
131
- if (symbol in rates) {
132
- return rates[symbol];
133
- }
134
-
135
- throw Error(`Currency '${symbol}' is not supported.`);
136
- }
137
-
138
- // do not await and do not throw in case we are offline
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);
@@ -1,59 +0,0 @@
1
- export function expect(expected) {
2
- return {
3
- toBe: function (received) {
4
- if (!(expected === received)) {
5
- throw new Error(
6
- `assertion failed: expected ${expected} is not equal to received ${received}`
7
- );
8
- }
9
- },
10
- toEqual: function (received) {
11
- if (!(expected === received)) {
12
- throw new Error(
13
- `assertion failed: expected ${expected} is not equal to received ${received}`
14
- );
15
- }
16
- },
17
- toBeGreaterThan: function (received) {
18
- if (!(expected > received)) {
19
- throw new Error(
20
- `assertion failed: expected ${expected} is not GreaterThan received ${received}`
21
- );
22
- }
23
- },
24
- toBeGreaterThanOrEqual: function (received) {
25
- if (!(expected >= received)) {
26
- throw new Error(
27
- `assertion failed: expected ${expected} is not GreaterThanOrEqual received ${received}`
28
- );
29
- }
30
- },
31
- toStrictEqual: function (received) {
32
- if (!(JSON.stringify(expected) === JSON.stringify(received))) {
33
- throw new Error(
34
- `assertion failed: expected ${expected} is not StrictEqual to received ${received}`
35
- );
36
- }
37
- },
38
- toContain: function (received) {
39
- if (!(expected as String).includes(received)) {
40
- throw new Error(
41
- `assertion failed: expected ${expected} does not Contain received ${received}`
42
- );
43
- }
44
- },
45
- rejects: {
46
- toThrow: async function (message: string): Promise<void> {
47
- try {
48
- await expected;
49
- } catch (e: any) {
50
- if (!(e.message as String).includes(message)) {
51
- throw new Error(
52
- `assertion failed: expected function to throw with message ${message}, but it did not.`
53
- );
54
- }
55
- }
56
- },
57
- },
58
- };
59
- }
package/src/test/fetch.ts DELETED
@@ -1,39 +0,0 @@
1
- const map: Record<string, any> = {};
2
-
3
- const _fetch = globalThis.fetch;
4
-
5
- Object.defineProperty(globalThis, "fetch", {
6
- writable: true,
7
- });
8
-
9
- globalThis.fetch = (async (uri: any, ...rest: any) => {
10
- if (!map[uri]) {
11
- // allow for execution of the original fetch taking some time
12
- const response = await _fetch(uri, ...rest);
13
- // upon arrival of the response, check if it was mocked in the meanwhile
14
- if (!map[uri]) {
15
- return response;
16
- }
17
- }
18
-
19
- return new Promise((resolve) =>
20
- resolve({
21
- json: () => {
22
- return typeof map[uri] === "string" ? JSON.parse(map[uri]) : map[uri];
23
- },
24
- text: () => {
25
- return typeof map[uri] === "string"
26
- ? map[uri]
27
- : JSON.stringify(map[uri], null, 2);
28
- },
29
- })
30
- );
31
- }) as any;
32
-
33
- export function setupFetchMock(mockUrl: string, responseData: any) {
34
- map[mockUrl] = responseData;
35
- }
36
-
37
- export function removeFetchMock(mockUrl: string) {
38
- delete map[mockUrl];
39
- }
@@ -1,13 +0,0 @@
1
- export const json = (param: any) => {
2
- return JSON.stringify(
3
- param,
4
- (key, value) => (typeof value === "bigint" ? value.toString() : value) // return everything else unchanged
5
- );
6
- };
7
- export default json;
8
-
9
- describe("json tests", () => {
10
- test("Should serialize bigint", async () => {
11
- expect(json(1n)).toBe('"1"');
12
- });
13
- });