@alephium/web3 0.2.0-test.0 → 0.2.0

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 (126) hide show
  1. package/.eslintignore +2 -2
  2. package/README.md +2 -135
  3. package/dist/alephium-web3.min.js +1 -1
  4. package/dist/alephium-web3.min.js.LICENSE.txt +0 -17
  5. package/dist/alephium-web3.min.js.map +1 -1
  6. package/dist/src/api/api-alephium.d.ts +152 -24
  7. package/dist/src/api/api-alephium.js +163 -82
  8. package/dist/src/api/api-explorer.d.ts +192 -49
  9. package/dist/src/api/api-explorer.js +195 -34
  10. package/dist/src/api/index.d.ts +40 -5
  11. package/dist/src/api/index.js +115 -7
  12. package/dist/src/api/types.d.ts +23 -0
  13. package/dist/src/api/types.js +235 -0
  14. package/dist/src/api/utils.d.ts +6 -0
  15. package/dist/{scripts/rename-gitignore.js → src/api/utils.js} +11 -6
  16. package/dist/src/contract/contract.d.ts +127 -80
  17. package/dist/src/contract/contract.js +425 -467
  18. package/dist/src/contract/events.d.ts +4 -4
  19. package/dist/src/contract/events.js +2 -1
  20. package/dist/src/contract/index.js +5 -1
  21. package/dist/src/contract/ralph.d.ts +5 -4
  22. package/dist/src/contract/ralph.js +27 -1
  23. package/dist/src/global.d.ts +7 -0
  24. package/dist/src/global.js +54 -0
  25. package/dist/src/index.d.ts +2 -0
  26. package/dist/src/index.js +23 -1
  27. package/dist/src/signer/index.d.ts +0 -1
  28. package/dist/src/signer/index.js +5 -2
  29. package/dist/src/signer/signer.d.ts +59 -60
  30. package/dist/src/signer/signer.js +99 -69
  31. package/dist/src/transaction/index.d.ts +0 -1
  32. package/dist/src/transaction/index.js +5 -2
  33. package/dist/src/transaction/status.d.ts +2 -1
  34. package/dist/src/transaction/status.js +2 -1
  35. package/dist/src/utils/bs58.d.ts +1 -0
  36. package/dist/src/utils/bs58.js +13 -1
  37. package/dist/src/utils/index.d.ts +0 -1
  38. package/dist/src/utils/index.js +5 -2
  39. package/dist/src/utils/subscription.d.ts +0 -3
  40. package/dist/src/utils/subscription.js +0 -1
  41. package/dist/src/utils/utils.d.ts +6 -11
  42. package/dist/src/utils/utils.js +22 -26
  43. package/jest-config.json +11 -0
  44. package/package.json +11 -47
  45. package/src/api/api-alephium.ts +219 -33
  46. package/src/api/api-explorer.ts +275 -52
  47. package/src/api/index.ts +140 -6
  48. package/src/api/types.ts +229 -0
  49. package/{scripts/rename-gitignore.js → src/api/utils.ts} +7 -6
  50. package/src/contract/contract.ts +663 -581
  51. package/src/contract/events.ts +8 -7
  52. package/src/contract/ralph.ts +29 -4
  53. package/src/global.ts +56 -0
  54. package/src/index.ts +7 -0
  55. package/src/signer/index.ts +0 -1
  56. package/src/signer/signer.ts +165 -134
  57. package/src/transaction/index.ts +0 -1
  58. package/src/transaction/status.ts +6 -3
  59. package/src/utils/bs58.ts +11 -0
  60. package/src/utils/index.ts +0 -1
  61. package/src/utils/subscription.ts +1 -5
  62. package/src/utils/utils.ts +15 -23
  63. package/webpack.config.js +3 -0
  64. package/.eslintrc.json +0 -21
  65. package/LICENSE +0 -165
  66. package/contracts/add/add.ral +0 -16
  67. package/contracts/greeter/greeter.ral +0 -7
  68. package/contracts/greeter/greeter_interface.ral +0 -3
  69. package/contracts/greeter_main.ral +0 -9
  70. package/contracts/main.ral +0 -6
  71. package/contracts/sub/sub.ral +0 -9
  72. package/dev/user.conf +0 -29
  73. package/dist/scripts/create-project.d.ts +0 -2
  74. package/dist/scripts/create-project.js +0 -124
  75. package/dist/scripts/rename-gitignore.d.ts +0 -1
  76. package/dist/scripts/start-devnet.d.ts +0 -1
  77. package/dist/scripts/start-devnet.js +0 -131
  78. package/dist/scripts/stop-devnet.d.ts +0 -1
  79. package/dist/scripts/stop-devnet.js +0 -32
  80. package/dist/src/signer/node-wallet.d.ts +0 -13
  81. package/dist/src/signer/node-wallet.js +0 -60
  82. package/dist/src/test/index.d.ts +0 -7
  83. package/dist/src/test/index.js +0 -41
  84. package/dist/src/test/privatekey-wallet.d.ts +0 -12
  85. package/dist/src/test/privatekey-wallet.js +0 -68
  86. package/dist/src/transaction/sign-verify.d.ts +0 -2
  87. package/dist/src/transaction/sign-verify.js +0 -58
  88. package/dist/src/utils/password-crypto.d.ts +0 -2
  89. package/dist/src/utils/password-crypto.js +0 -69
  90. package/gitignore +0 -10
  91. package/scripts/create-project.ts +0 -136
  92. package/scripts/start-devnet.js +0 -141
  93. package/scripts/stop-devnet.js +0 -32
  94. package/src/contract/ralph.test.ts +0 -178
  95. package/src/fixtures/address.json +0 -36
  96. package/src/fixtures/balance.json +0 -9
  97. package/src/fixtures/self-clique.json +0 -19
  98. package/src/fixtures/transaction.json +0 -13
  99. package/src/fixtures/transactions.json +0 -179
  100. package/src/signer/fixtures/genesis.json +0 -26
  101. package/src/signer/fixtures/wallets.json +0 -26
  102. package/src/signer/node-wallet.ts +0 -74
  103. package/src/test/index.ts +0 -32
  104. package/src/test/privatekey-wallet.ts +0 -58
  105. package/src/transaction/sign-verify.test.ts +0 -50
  106. package/src/transaction/sign-verify.ts +0 -39
  107. package/src/utils/address.test.ts +0 -47
  108. package/src/utils/djb2.test.ts +0 -35
  109. package/src/utils/password-crypto.test.ts +0 -27
  110. package/src/utils/password-crypto.ts +0 -77
  111. package/src/utils/utils.test.ts +0 -161
  112. package/templates/base/README.md +0 -34
  113. package/templates/base/package.json +0 -35
  114. package/templates/base/src/greeter.ts +0 -41
  115. package/templates/base/tsconfig.json +0 -19
  116. package/templates/react/README.md +0 -34
  117. package/templates/react/config-overrides.js +0 -18
  118. package/templates/react/package.json +0 -66
  119. package/templates/react/src/App.tsx +0 -42
  120. package/templates/react/src/artifacts/greeter.ral.json +0 -26
  121. package/templates/react/src/artifacts/greeter_main.ral.json +0 -22
  122. package/templates/shared/.eslintrc.json +0 -12
  123. package/templates/shared/scripts/header.js +0 -0
  124. package/test/contract.test.ts +0 -178
  125. package/test/events.test.ts +0 -138
  126. package/test/transaction.test.ts +0 -72
@@ -28,6 +28,29 @@ export interface AddressInfo {
28
28
  txNumber: number
29
29
  }
30
30
 
31
+ export interface AssetOutput {
32
+ /** @format int32 */
33
+ hint: number
34
+
35
+ /** @format 32-byte-hash */
36
+ key: string
37
+
38
+ /** @format uint256 */
39
+ attoAlphAmount: string
40
+ address: string
41
+ tokens?: Token[]
42
+
43
+ /** @format int64 */
44
+ lockTime?: number
45
+
46
+ /** @format hex-string */
47
+ message?: string
48
+
49
+ /** @format 32-byte-hash */
50
+ spent?: string
51
+ type: string
52
+ }
53
+
31
54
  export interface BadRequest {
32
55
  detail: string
33
56
  }
@@ -76,6 +99,23 @@ export interface ConfirmedTransaction {
76
99
  type: string
77
100
  }
78
101
 
102
+ export interface ContractOutput {
103
+ /** @format int32 */
104
+ hint: number
105
+
106
+ /** @format 32-byte-hash */
107
+ key: string
108
+
109
+ /** @format uint256 */
110
+ attoAlphAmount: string
111
+ address: string
112
+ tokens?: Token[]
113
+
114
+ /** @format 32-byte-hash */
115
+ spent?: string
116
+ type: string
117
+ }
118
+
79
119
  export interface ExplorerInfo {
80
120
  releaseVersion: string
81
121
  commit: string
@@ -90,14 +130,14 @@ export interface Hashrate {
90
130
 
91
131
  export interface Input {
92
132
  outputRef: OutputRef
93
- unlockScript?: string
94
133
 
95
- /** @format 32-byte-hash */
96
- txHashRef: string
97
- address: string
134
+ /** @format hex-string */
135
+ unlockScript?: string
136
+ address?: string
98
137
 
99
138
  /** @format uint256 */
100
- amount: string
139
+ attoAlphAmount?: string
140
+ tokens?: Token[]
101
141
  }
102
142
 
103
143
  export interface InternalServerError {
@@ -110,28 +150,17 @@ export interface ListBlocks {
110
150
  blocks?: BlockEntryLite[]
111
151
  }
112
152
 
153
+ export interface LogbackValue {
154
+ name: string
155
+ level: string
156
+ }
157
+
113
158
  export interface NotFound {
114
159
  detail: string
115
160
  resource: string
116
161
  }
117
162
 
118
- export interface Output {
119
- /** @format int32 */
120
- hint: number
121
-
122
- /** @format 32-byte-hash */
123
- key: string
124
-
125
- /** @format uint256 */
126
- amount: string
127
- address: string
128
-
129
- /** @format int64 */
130
- lockTime?: number
131
-
132
- /** @format 32-byte-hash */
133
- spent?: string
134
- }
163
+ export type Output = AssetOutput | ContractOutput
135
164
 
136
165
  export interface OutputRef {
137
166
  /** @format int32 */
@@ -198,6 +227,14 @@ export interface TimedCount {
198
227
  totalCountAllChains: number
199
228
  }
200
229
 
230
+ export interface Token {
231
+ /** @format 32-byte-hash */
232
+ id: string
233
+
234
+ /** @format uint256 */
235
+ amount: string
236
+ }
237
+
201
238
  export interface TokenSupply {
202
239
  /** @format int64 */
203
240
  timestamp: number
@@ -239,20 +276,6 @@ export interface Transaction {
239
276
 
240
277
  export type TransactionLike = ConfirmedTransaction | UnconfirmedTransaction
241
278
 
242
- export interface UInput {
243
- outputRef: OutputRef
244
- unlockScript?: string
245
- }
246
-
247
- export interface UOutput {
248
- /** @format uint256 */
249
- amount: string
250
- address: string
251
-
252
- /** @format int64 */
253
- lockTime?: number
254
- }
255
-
256
279
  export interface Unauthorized {
257
280
  detail: string
258
281
  }
@@ -266,28 +289,22 @@ export interface UnconfirmedTransaction {
266
289
 
267
290
  /** @format int32 */
268
291
  chainTo: number
269
- inputs?: UInput[]
270
- outputs?: UOutput[]
292
+ inputs?: Input[]
293
+ outputs?: Output[]
271
294
 
272
295
  /** @format int32 */
273
296
  gasAmount: number
274
297
 
275
298
  /** @format uint256 */
276
299
  gasPrice: string
300
+
301
+ /** @format int64 */
302
+ lastSeen: number
277
303
  type: string
278
304
  }
279
305
 
280
306
  import 'cross-fetch/polyfill'
281
-
282
- function convertHttpResponse<T>(
283
- response: HttpResponse<T, { detail: string }> | HttpResponse<T, { detail: string }>
284
- ): T {
285
- if (response.error) {
286
- throw new Error(response.error.detail)
287
- } else {
288
- return response.data
289
- }
290
- }
307
+ import { convertHttpResponse } from './utils'
291
308
 
292
309
  export type QueryParamsType = Record<string | number, any>
293
310
  export type ResponseFormat = keyof Omit<Body, 'body' | 'bodyUsed'>
@@ -491,7 +508,6 @@ export class HttpClient<SecurityDataType = unknown> {
491
508
  this.abortControllers.delete(cancelToken)
492
509
  }
493
510
 
494
- if (!response.ok) throw data
495
511
  return data
496
512
  })
497
513
  }
@@ -524,7 +540,7 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
524
540
  *
525
541
  * @tags Blocks
526
542
  * @name GetBlocksBlockHash
527
- * @request GET:/blocks/{block-hash}
543
+ * @request GET:/blocks/{block_hash}
528
544
  */
529
545
  getBlocksBlockHash: (blockHash: string, params: RequestParams = {}) =>
530
546
  this.request<BlockEntryLite, BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
@@ -539,7 +555,7 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
539
555
  *
540
556
  * @tags Blocks
541
557
  * @name GetBlocksBlockHashTransactions
542
- * @request GET:/blocks/{block-hash}/transactions
558
+ * @request GET:/blocks/{block_hash}/transactions
543
559
  */
544
560
  getBlocksBlockHashTransactions: (
545
561
  blockHash: string,
@@ -560,7 +576,7 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
560
576
  *
561
577
  * @tags Transactions
562
578
  * @name GetTransactionsTransactionHash
563
- * @request GET:/transactions/{transaction-hash}
579
+ * @request GET:/transactions/{transaction_hash}
564
580
  */
565
581
  getTransactionsTransactionHash: (transactionHash: string, params: RequestParams = {}) =>
566
582
  this.request<TransactionLike, BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
@@ -568,6 +584,21 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
568
584
  method: 'GET',
569
585
  format: 'json',
570
586
  ...params
587
+ }).then(convertHttpResponse),
588
+
589
+ /**
590
+ * @description Get a transaction from a output reference key
591
+ *
592
+ * @tags Transactions
593
+ * @name GetTransactionsByOutputRefKeyOutputRefKey
594
+ * @request GET:/transactions/by/output-ref-key/{output_ref_key}
595
+ */
596
+ getTransactionsByOutputRefKeyOutputRefKey: (outputRefKey: string, params: RequestParams = {}) =>
597
+ this.request<Transaction, BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
598
+ path: `/transactions/by/output-ref-key/${outputRefKey}`,
599
+ method: 'GET',
600
+ format: 'json',
601
+ ...params
571
602
  }).then(convertHttpResponse)
572
603
  }
573
604
  addresses = {
@@ -606,6 +637,26 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
606
637
  ...params
607
638
  }).then(convertHttpResponse),
608
639
 
640
+ /**
641
+ * @description List transactions of a given address within a time-range
642
+ *
643
+ * @tags Addresses
644
+ * @name GetAddressesAddressTimerangedTransactions
645
+ * @request GET:/addresses/{address}/timeranged-transactions
646
+ */
647
+ getAddressesAddressTimerangedTransactions: (
648
+ address: string,
649
+ query: { fromTs: number; toTs: number; page?: number; limit?: number; reverse?: boolean },
650
+ params: RequestParams = {}
651
+ ) =>
652
+ this.request<Transaction[], BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
653
+ path: `/addresses/${address}/timeranged-transactions`,
654
+ method: 'GET',
655
+ query: query,
656
+ format: 'json',
657
+ ...params
658
+ }).then(convertHttpResponse),
659
+
609
660
  /**
610
661
  * @description Get total transactions of a given address
611
662
  *
@@ -621,6 +672,21 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
621
672
  ...params
622
673
  }).then(convertHttpResponse),
623
674
 
675
+ /**
676
+ * @description List unconfirmed transactions of a given address
677
+ *
678
+ * @tags Addresses
679
+ * @name GetAddressesAddressUnconfirmedTransactions
680
+ * @request GET:/addresses/{address}/unconfirmed-transactions
681
+ */
682
+ getAddressesAddressUnconfirmedTransactions: (address: string, params: RequestParams = {}) =>
683
+ this.request<TransactionLike[], BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
684
+ path: `/addresses/${address}/unconfirmed-transactions`,
685
+ method: 'GET',
686
+ format: 'json',
687
+ ...params
688
+ }).then(convertHttpResponse),
689
+
624
690
  /**
625
691
  * @description Get address balance
626
692
  *
@@ -634,6 +700,75 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
634
700
  method: 'GET',
635
701
  format: 'json',
636
702
  ...params
703
+ }).then(convertHttpResponse),
704
+
705
+ /**
706
+ * @description List address tokens
707
+ *
708
+ * @tags Addresses
709
+ * @name GetAddressesAddressTokens
710
+ * @request GET:/addresses/{address}/tokens
711
+ */
712
+ getAddressesAddressTokens: (address: string, params: RequestParams = {}) =>
713
+ this.request<string[], BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
714
+ path: `/addresses/${address}/tokens`,
715
+ method: 'GET',
716
+ format: 'json',
717
+ ...params
718
+ }).then(convertHttpResponse),
719
+
720
+ /**
721
+ * @description List address tokens
722
+ *
723
+ * @tags Addresses
724
+ * @name GetAddressesAddressTokensTokenIdTransactions
725
+ * @request GET:/addresses/{address}/tokens/{token_id}/transactions
726
+ */
727
+ getAddressesAddressTokensTokenIdTransactions: (
728
+ address: string,
729
+ tokenId: string,
730
+ query?: { page?: number; limit?: number; reverse?: boolean },
731
+ params: RequestParams = {}
732
+ ) =>
733
+ this.request<Transaction[], BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
734
+ path: `/addresses/${address}/tokens/${tokenId}/transactions`,
735
+ method: 'GET',
736
+ query: query,
737
+ format: 'json',
738
+ ...params
739
+ }).then(convertHttpResponse),
740
+
741
+ /**
742
+ * @description Get address balance of given token
743
+ *
744
+ * @tags Addresses
745
+ * @name GetAddressesAddressTokensTokenIdBalance
746
+ * @request GET:/addresses/{address}/tokens/{token_id}/balance
747
+ */
748
+ getAddressesAddressTokensTokenIdBalance: (address: string, tokenId: string, params: RequestParams = {}) =>
749
+ this.request<AddressBalance, BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
750
+ path: `/addresses/${address}/tokens/${tokenId}/balance`,
751
+ method: 'GET',
752
+ format: 'json',
753
+ ...params
754
+ }).then(convertHttpResponse)
755
+ }
756
+ addressesActive = {
757
+ /**
758
+ * @description Are the addresses active (at least 1 transaction)
759
+ *
760
+ * @tags Addresses
761
+ * @name PostAddressesActive
762
+ * @request POST:/addresses-active
763
+ */
764
+ postAddressesActive: (data?: string[], params: RequestParams = {}) =>
765
+ this.request<boolean[], BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
766
+ path: `/addresses-active`,
767
+ method: 'POST',
768
+ body: data,
769
+ type: ContentType.Json,
770
+ format: 'json',
771
+ ...params
637
772
  }).then(convertHttpResponse)
638
773
  }
639
774
  infos = {
@@ -770,6 +905,63 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
770
905
  }
771
906
  ).then(convertHttpResponse)
772
907
  }
908
+ unconfirmedTransactions = {
909
+ /**
910
+ * @description list unconfirmed transactions
911
+ *
912
+ * @tags Transactions
913
+ * @name GetUnconfirmedTransactions
914
+ * @request GET:/unconfirmed-transactions
915
+ */
916
+ getUnconfirmedTransactions: (
917
+ query?: { page?: number; limit?: number; reverse?: boolean },
918
+ params: RequestParams = {}
919
+ ) =>
920
+ this.request<TransactionLike[], BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
921
+ path: `/unconfirmed-transactions`,
922
+ method: 'GET',
923
+ query: query,
924
+ format: 'json',
925
+ ...params
926
+ }).then(convertHttpResponse)
927
+ }
928
+ tokens = {
929
+ /**
930
+ * @description List tokens
931
+ *
932
+ * @tags Tokens
933
+ * @name GetTokens
934
+ * @request GET:/tokens
935
+ */
936
+ getTokens: (query?: { page?: number; limit?: number; reverse?: boolean }, params: RequestParams = {}) =>
937
+ this.request<string[], BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
938
+ path: `/tokens`,
939
+ method: 'GET',
940
+ query: query,
941
+ format: 'json',
942
+ ...params
943
+ }).then(convertHttpResponse),
944
+
945
+ /**
946
+ * @description List token transactions
947
+ *
948
+ * @tags Tokens
949
+ * @name GetTokensTokenIdTransactions
950
+ * @request GET:/tokens/{token_id}/transactions
951
+ */
952
+ getTokensTokenIdTransactions: (
953
+ tokenId: string,
954
+ query?: { page?: number; limit?: number; reverse?: boolean },
955
+ params: RequestParams = {}
956
+ ) =>
957
+ this.request<Transaction[], BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
958
+ path: `/tokens/${tokenId}/transactions`,
959
+ method: 'GET',
960
+ query: query,
961
+ format: 'json',
962
+ ...params
963
+ }).then(convertHttpResponse)
964
+ }
773
965
  charts = {
774
966
  /**
775
967
  * @description `interval-type` query param: hourly, daily
@@ -847,6 +1039,37 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
847
1039
  path: `/utils/sanity-check`,
848
1040
  method: 'PUT',
849
1041
  ...params
1042
+ }).then(convertHttpResponse),
1043
+
1044
+ /**
1045
+ * @description Update global log level, accepted: TRACE, DEBUG, INFO, WARN, ERROR
1046
+ *
1047
+ * @tags Utils
1048
+ * @name PutUtilsUpdateGlobalLoglevel
1049
+ * @request PUT:/utils/update-global-loglevel
1050
+ */
1051
+ putUtilsUpdateGlobalLoglevel: (data: 'TRACE' | 'DEBUG' | 'INFO' | 'WARN' | 'ERROR', params: RequestParams = {}) =>
1052
+ this.request<void, BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
1053
+ path: `/utils/update-global-loglevel`,
1054
+ method: 'PUT',
1055
+ body: data,
1056
+ ...params
1057
+ }).then(convertHttpResponse),
1058
+
1059
+ /**
1060
+ * @description Update logback values
1061
+ *
1062
+ * @tags Utils
1063
+ * @name PutUtilsUpdateLogConfig
1064
+ * @request PUT:/utils/update-log-config
1065
+ */
1066
+ putUtilsUpdateLogConfig: (data?: LogbackValue[], params: RequestParams = {}) =>
1067
+ this.request<void, BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable>({
1068
+ path: `/utils/update-log-config`,
1069
+ method: 'PUT',
1070
+ body: data,
1071
+ type: ContentType.Json,
1072
+ ...params
850
1073
  }).then(convertHttpResponse)
851
1074
  }
852
1075
  }
package/src/api/index.ts CHANGED
@@ -19,17 +19,151 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
19
19
  import { Api as NodeApi } from './api-alephium'
20
20
  import { Api as ExplorerApi } from './api-explorer'
21
21
 
22
- export class NodeProvider extends NodeApi<null> {
23
- constructor(baseUrl: string) {
24
- super({ baseUrl: baseUrl })
22
+ export interface ApiRequestArguments {
23
+ path: string
24
+ method: string
25
+ params: any[]
26
+ }
27
+ export type ApiRequestHandler = (args: ApiRequestArguments) => Promise<any>
28
+
29
+ function forwardRequests(api: Record<string, any>, handler: ApiRequestHandler): void {
30
+ // Update class properties to forward requests
31
+ for (const [path, pathObject] of Object.entries(api)) {
32
+ for (const method of Object.keys(pathObject)) {
33
+ pathObject[`${method}`] = async (...params: any): Promise<any> => {
34
+ return handler({ path, method, params })
35
+ }
36
+ }
25
37
  }
26
38
  }
27
39
 
28
- export class ExplorerProvider extends ExplorerApi<null> {
29
- constructor(baseUrl: string) {
30
- super({ baseUrl: baseUrl })
40
+ async function request(provider: Record<string, any>, args: ApiRequestArguments): Promise<any> {
41
+ const call = provider[`${args.path}`][`${args.method}`] as (...any) => Promise<any>
42
+ return call(...args.params)
43
+ }
44
+
45
+ function initializeNodeApi(baseUrl: string, apiKey?: string): NodeApi<string> {
46
+ const nodeApi = new NodeApi<string>({
47
+ baseUrl: baseUrl,
48
+ baseApiParams: { secure: true },
49
+ securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {})
50
+ })
51
+ nodeApi.setSecurityData(apiKey ?? null)
52
+ return nodeApi
53
+ }
54
+
55
+ export class NodeProvider {
56
+ readonly wallets: NodeApi<string>['wallets']
57
+ readonly infos: NodeApi<string>['infos']
58
+ readonly blockflow: NodeApi<string>['blockflow']
59
+ readonly addresses: NodeApi<string>['addresses']
60
+ readonly transactions: NodeApi<string>['transactions']
61
+ readonly contracts: NodeApi<string>['contracts']
62
+ readonly multisig: NodeApi<string>['multisig']
63
+ readonly utils: NodeApi<string>['utils']
64
+ readonly miners: NodeApi<string>['miners']
65
+ readonly events: NodeApi<string>['events']
66
+
67
+ constructor(baseUrl: string, apiKey?: string)
68
+ constructor(provider: NodeProvider)
69
+ constructor(handler: ApiRequestHandler)
70
+ constructor(param0: string | NodeProvider | ApiRequestHandler, apiKey?: string) {
71
+ let nodeApi: NodeProvider
72
+ if (typeof param0 === 'string') {
73
+ nodeApi = initializeNodeApi(param0, apiKey)
74
+ } else if (typeof param0 === 'function') {
75
+ nodeApi = new NodeProvider('https://1.2.3.4:0')
76
+ forwardRequests(nodeApi, param0 as ApiRequestHandler)
77
+ } else {
78
+ nodeApi = param0 as NodeProvider
79
+ }
80
+
81
+ this.wallets = { ...nodeApi.wallets }
82
+ this.infos = { ...nodeApi.infos }
83
+ this.blockflow = { ...nodeApi.blockflow }
84
+ this.addresses = { ...nodeApi.addresses }
85
+ this.transactions = { ...nodeApi.transactions }
86
+ this.contracts = { ...nodeApi.contracts }
87
+ this.multisig = { ...nodeApi.multisig }
88
+ this.utils = { ...nodeApi.utils }
89
+ this.miners = { ...nodeApi.miners }
90
+ this.events = { ...nodeApi.events }
91
+ }
92
+
93
+ request = (args: ApiRequestArguments): Promise<any> => {
94
+ return request(this, args)
95
+ }
96
+
97
+ // This can prevent the proxied node provider from being modified
98
+ static Proxy(nodeProvider: NodeProvider): NodeProvider {
99
+ return new NodeProvider(nodeProvider)
100
+ }
101
+
102
+ static Remote(handler: ApiRequestHandler): NodeProvider {
103
+ return new NodeProvider(handler)
104
+ }
105
+ }
106
+
107
+ function initializeExplorerApi(baseUrl: string, apiKey?: string): ExplorerApi<string> {
108
+ const explorerApi = new ExplorerApi<string>({
109
+ baseUrl: baseUrl,
110
+ baseApiParams: { secure: true },
111
+ securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {})
112
+ })
113
+ explorerApi.setSecurityData(apiKey ?? null)
114
+ return explorerApi
115
+ }
116
+
117
+ export class ExplorerProvider {
118
+ readonly blocks = ExplorerApi['blocks']
119
+ readonly transactions = ExplorerApi['transactions']
120
+ readonly addresses = ExplorerApi['addresses']
121
+ readonly addressesActive = ExplorerApi['addressesActive']
122
+ readonly infos = ExplorerApi['infos']
123
+ readonly unconfirmedTransactions = ExplorerApi['unconfirmedTransactions']
124
+ readonly tokens = ExplorerApi['tokens']
125
+ readonly charts = ExplorerApi['charts']
126
+ readonly utils = ExplorerApi['utils']
127
+
128
+ constructor(baseUrl: string, apiKey?: string)
129
+ constructor(provider: ExplorerProvider)
130
+ constructor(handler: ApiRequestHandler)
131
+ constructor(param0: string | ExplorerProvider | ApiRequestHandler, apiKey?: string) {
132
+ let explorerApi: ExplorerProvider
133
+ if (typeof param0 === 'string') {
134
+ explorerApi = initializeExplorerApi(param0, apiKey)
135
+ } else if (typeof param0 === 'function') {
136
+ explorerApi = new ExplorerProvider('https://1.2.3.4:0')
137
+ forwardRequests(explorerApi, param0 as ApiRequestHandler)
138
+ } else {
139
+ explorerApi = param0 as ExplorerProvider
140
+ }
141
+
142
+ this.blocks = { ...explorerApi.blocks }
143
+ this.transactions = { ...explorerApi.transactions }
144
+ this.addresses = { ...explorerApi.addresses }
145
+ this.addressesActive = { ...explorerApi.addressesActive }
146
+ this.infos = { ...explorerApi.infos }
147
+ this.unconfirmedTransactions = { ...explorerApi.unconfirmedTransactions }
148
+ this.tokens = { ...explorerApi.tokens }
149
+ this.charts = { ...explorerApi.charts }
150
+ this.utils = { ...explorerApi.utils }
151
+ }
152
+
153
+ request = (args: ApiRequestArguments): Promise<any> => {
154
+ return request(this, args)
155
+ }
156
+
157
+ // This can prevent the proxied explorer provider from being modified
158
+ static Proxy(explorerProvider: ExplorerProvider): ExplorerProvider {
159
+ return new ExplorerProvider(explorerProvider)
160
+ }
161
+
162
+ static Remote(handler: ApiRequestHandler): ExplorerProvider {
163
+ return new ExplorerProvider(handler)
31
164
  }
32
165
  }
33
166
 
34
167
  export * as node from './api-alephium'
35
168
  export * as explorer from './api-explorer'
169
+ export * from './types'