@alephium/web3 0.2.0-rc.35 → 0.2.0-rc.36

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.
package/src/api/index.ts CHANGED
@@ -19,6 +19,29 @@ 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 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
+ }
37
+ }
38
+ }
39
+
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
+
22
45
  function initializeNodeApi(baseUrl: string, apiKey?: string): NodeApi<string> {
23
46
  const nodeApi = new NodeApi<string>({
24
47
  baseUrl: baseUrl,
@@ -29,20 +52,7 @@ function initializeNodeApi(baseUrl: string, apiKey?: string): NodeApi<string> {
29
52
  return nodeApi
30
53
  }
31
54
 
32
- interface INodeProvider {
33
- readonly wallets: NodeApi<string>['wallets']
34
- readonly infos: NodeApi<string>['infos']
35
- readonly blockflow: NodeApi<string>['blockflow']
36
- readonly addresses: NodeApi<string>['addresses']
37
- readonly transactions: NodeApi<string>['transactions']
38
- readonly contracts: NodeApi<string>['contracts']
39
- readonly multisig: NodeApi<string>['multisig']
40
- readonly utils: NodeApi<string>['utils']
41
- readonly miners: NodeApi<string>['miners']
42
- readonly events: NodeApi<string>['events']
43
- }
44
-
45
- export class NodeProvider implements INodeProvider {
55
+ export class NodeProvider {
46
56
  readonly wallets: NodeApi<string>['wallets']
47
57
  readonly infos: NodeApi<string>['infos']
48
58
  readonly blockflow: NodeApi<string>['blockflow']
@@ -54,14 +64,18 @@ export class NodeProvider implements INodeProvider {
54
64
  readonly miners: NodeApi<string>['miners']
55
65
  readonly events: NodeApi<string>['events']
56
66
 
57
- constructor(provider: INodeProvider)
58
67
  constructor(baseUrl: string, apiKey?: string)
59
- constructor(param0: string | INodeProvider, apiKey?: string) {
60
- let nodeApi: INodeProvider
68
+ constructor(provider: NodeProvider)
69
+ constructor(handler: ApiRequestHandler)
70
+ constructor(param0: string | NodeProvider | ApiRequestHandler, apiKey?: string) {
71
+ let nodeApi: NodeProvider
61
72
  if (typeof param0 === 'string') {
62
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)
63
77
  } else {
64
- nodeApi = param0 as INodeProvider
78
+ nodeApi = param0 as NodeProvider
65
79
  }
66
80
 
67
81
  this.wallets = nodeApi.wallets
@@ -76,49 +90,79 @@ export class NodeProvider implements INodeProvider {
76
90
  this.events = nodeApi.events
77
91
  }
78
92
 
93
+ request = (args: ApiRequestArguments): Promise<any> => {
94
+ return request(this, args)
95
+ }
96
+
79
97
  // This can prevent the proxied node provider from being modified
80
98
  static Proxy(nodeProvider: NodeProvider): NodeProvider {
81
99
  return new NodeProvider(nodeProvider)
82
100
  }
101
+
102
+ static Remote(handler: ApiRequestHandler): NodeProvider {
103
+ return new NodeProvider(handler)
104
+ }
83
105
  }
84
106
 
85
- export interface RequestArguments {
86
- path: string
87
- method: string
88
- params?: any
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
89
115
  }
90
116
 
91
- export class RemoteNodeProvider implements INodeProvider {
92
- readonly wallets!: NodeApi<string>['wallets']
93
- readonly infos!: NodeApi<string>['infos']
94
- readonly blockflow!: NodeApi<string>['blockflow']
95
- readonly addresses!: NodeApi<string>['addresses']
96
- readonly transactions!: NodeApi<string>['transactions']
97
- readonly contracts!: NodeApi<string>['contracts']
98
- readonly multisig!: NodeApi<string>['multisig']
99
- readonly utils!: NodeApi<string>['utils']
100
- readonly miners!: NodeApi<string>['miners']
101
- readonly events!: NodeApi<string>['events']
102
-
103
- constructor(request: (request: RequestArguments) => Promise<any>) {
104
- const fakeNodeProvide = new NodeProvider('https://1.2.3.4:12973')
105
- Object.assign(this, fakeNodeProvide) // Initialize the class
106
-
107
- // Update class properties to forward requests
108
- for (const [path, pathObject] of Object.entries(this)) {
109
- for (const method of Object.keys(pathObject)) {
110
- pathObject[`${method}`] = async (params: any): Promise<any> => {
111
- return request({ path, method, params })
112
- }
113
- }
117
+ export class ExplorerProvider {
118
+ readonly blocks = ExplorerApi['blocks']
119
+ readonly transactions = ExplorerApi['transactions']
120
+ readonly transactionByOutputRefKey = ExplorerApi['transactionByOutputRefKey']
121
+ readonly addresses = ExplorerApi['addresses']
122
+ readonly addressesActive = ExplorerApi['addressesActive']
123
+ readonly infos = ExplorerApi['infos']
124
+ readonly unconfirmedTransactions = ExplorerApi['unconfirmedTransactions']
125
+ readonly tokens = ExplorerApi['tokens']
126
+ readonly charts = ExplorerApi['charts']
127
+ readonly utils = ExplorerApi['utils']
128
+
129
+ constructor(baseUrl: string, apiKey?: string)
130
+ constructor(provider: ExplorerProvider)
131
+ constructor(handler: ApiRequestHandler)
132
+ constructor(param0: string | ExplorerProvider | ApiRequestHandler, apiKey?: string) {
133
+ let explorerApi: ExplorerProvider
134
+ if (typeof param0 === 'string') {
135
+ explorerApi = initializeExplorerApi(param0, apiKey)
136
+ } else if (typeof param0 === 'function') {
137
+ explorerApi = new ExplorerProvider('https://1.2.3.4:0')
138
+ forwardRequests(explorerApi, param0 as ApiRequestHandler)
139
+ } else {
140
+ explorerApi = param0 as ExplorerProvider
114
141
  }
142
+
143
+ this.blocks = explorerApi.blocks
144
+ this.transactions = explorerApi.transactions
145
+ this.transactionByOutputRefKey = explorerApi.transactionByOutputRefKey
146
+ this.addresses = explorerApi.addresses
147
+ this.addressesActive = explorerApi.addressesActive
148
+ this.infos = explorerApi.infos
149
+ this.unconfirmedTransactions = explorerApi.unconfirmedTransactions
150
+ this.tokens = explorerApi.tokens
151
+ this.charts = explorerApi.charts
152
+ this.utils = explorerApi.utils
153
+ }
154
+
155
+ request = (args: ApiRequestArguments): Promise<any> => {
156
+ return request(this, args)
157
+ }
158
+
159
+ // This can prevent the proxied explorer provider from being modified
160
+ static Proxy(explorerProvider: ExplorerProvider): ExplorerProvider {
161
+ return new ExplorerProvider(explorerProvider)
115
162
  }
116
- }
117
163
 
118
- // TODO: use proxy provider once the endpoints are refined.
119
- export class ExplorerProvider extends ExplorerApi<null> {
120
- constructor(baseUrl: string) {
121
- super({ baseUrl: baseUrl })
164
+ static Remote(handler: ApiRequestHandler): ExplorerProvider {
165
+ return new ExplorerProvider(handler)
122
166
  }
123
167
  }
124
168
 
package/src/global.ts CHANGED
@@ -16,7 +16,7 @@ You should have received a copy of the GNU Lesser General Public License
16
16
  along with the library. If not, see <http://www.gnu.org/licenses/>.
17
17
  */
18
18
 
19
- import { NodeProvider } from './api'
19
+ import { ExplorerProvider, NodeProvider } from './api'
20
20
 
21
21
  let _currentNodeProvider: NodeProvider | undefined = undefined
22
22
 
@@ -36,3 +36,21 @@ export function getCurrentNodeProvider(): NodeProvider {
36
36
  }
37
37
  return _currentNodeProvider
38
38
  }
39
+
40
+ let _currentExplorerProvider: ExplorerProvider | undefined = undefined
41
+
42
+ export function setCurrentExplorerProvider(provider: ExplorerProvider): void
43
+ export function setCurrentExplorerProvider(baseUrl: string, apiKey?: string): void
44
+ export function setCurrentExplorerProvider(provider: ExplorerProvider | string, apiKey?: string): void {
45
+ if (typeof provider == 'string') {
46
+ _currentExplorerProvider = new ExplorerProvider(provider, apiKey)
47
+ } else {
48
+ _currentExplorerProvider = provider
49
+ }
50
+ }
51
+
52
+ // Different from `NodeProvider`, this may return `undefined`
53
+ // as ExplorerProvider is not necessary for all applications
54
+ export function getCurrentExplorerProvider(): ExplorerProvider | undefined {
55
+ return _currentExplorerProvider
56
+ }
@@ -18,6 +18,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
 
19
19
  import { ec as EC } from 'elliptic'
20
20
  import {
21
+ ExplorerProvider,
21
22
  fromApiNumber256,
22
23
  fromApiTokens,
23
24
  NodeProvider,
@@ -145,6 +146,7 @@ export interface SubmissionResult {
145
146
 
146
147
  export interface SignerProvider {
147
148
  get nodeProvider(): NodeProvider | undefined
149
+ get explorerProvider(): ExplorerProvider | undefined
148
150
 
149
151
  getSelectedAccount(): Promise<Account>
150
152
 
@@ -161,6 +163,7 @@ export interface SignerProvider {
161
163
 
162
164
  export abstract class SignerProviderSimple implements SignerProvider {
163
165
  abstract get nodeProvider(): NodeProvider | undefined
166
+ abstract get explorerProvider(): ExplorerProvider | undefined
164
167
  abstract getSelectedAccount(): Promise<Account>
165
168
 
166
169
  private getNodeProvider(): NodeProvider {