@alephium/web3 0.2.0-rc.34 → 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/api/types.ts CHANGED
@@ -19,7 +19,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
19
19
  import { assertType, bs58, Eq } from '../utils'
20
20
  import * as node from './api-alephium'
21
21
 
22
- export type Number256 = number | bigint | string
22
+ export type Number256 = bigint
23
23
  export type Val = Number256 | boolean | string | Val[]
24
24
  export type NamedVals = Record<string, Val>
25
25
 
@@ -68,12 +68,8 @@ export function toApiNumber256Optional(v?: Val): string | undefined {
68
68
  return v === undefined ? undefined : toApiNumber256(v)
69
69
  }
70
70
 
71
- export function fromApiNumber256(n: string): Number256 {
72
- if (Number.isSafeInteger(Number.parseInt(n))) {
73
- return Number(n)
74
- } else {
75
- return BigInt(n)
76
- }
71
+ export function fromApiNumber256(n: string): bigint {
72
+ return BigInt(n)
77
73
  }
78
74
 
79
75
  // TODO: check hex string
@@ -32,7 +32,6 @@ import {
32
32
  toApiVal,
33
33
  Token,
34
34
  Val,
35
- toApiTokens,
36
35
  fromApiTokens,
37
36
  fromApiVals
38
37
  } from '../api'
@@ -816,11 +815,11 @@ export class Contract extends Artifact {
816
815
  const signerParams: SignDeployContractTxParams = {
817
816
  signerAddress: (await signer.getSelectedAccount()).address,
818
817
  bytecode: bytecode,
819
- initialAttoAlphAmount: extractOptionalNumber256(params.initialAttoAlphAmount),
820
- issueTokenAmount: extractOptionalNumber256(params.issueTokenAmount),
821
- initialTokenAmounts: toApiTokens(params.initialTokenAmounts),
818
+ initialAttoAlphAmount: params.initialAttoAlphAmount,
819
+ issueTokenAmount: params.issueTokenAmount,
820
+ initialTokenAmounts: params.initialTokenAmounts,
822
821
  gasAmount: params.gasAmount,
823
- gasPrice: extractOptionalNumber256(params.gasPrice)
822
+ gasPrice: params.gasPrice
824
823
  }
825
824
  return signerParams
826
825
  }
@@ -902,10 +901,10 @@ export class Script extends Artifact {
902
901
  const signerParams: SignExecuteScriptTxParams = {
903
902
  signerAddress: (await signer.getSelectedAccount()).address,
904
903
  bytecode: this.buildByteCodeToDeploy(params.initialFields ? params.initialFields : {}),
905
- attoAlphAmount: extractOptionalNumber256(params.attoAlphAmount),
906
- tokens: toApiTokens(params.tokens),
904
+ attoAlphAmount: params.attoAlphAmount,
905
+ tokens: params.tokens,
907
906
  gasAmount: params.gasAmount,
908
- gasPrice: extractOptionalNumber256(params.gasPrice)
907
+ gasPrice: params.gasPrice
909
908
  }
910
909
  return signerParams
911
910
  }
@@ -923,10 +922,6 @@ export class Script extends Artifact {
923
922
  }
924
923
  }
925
924
 
926
- function extractOptionalNumber256(v?: Val): string | undefined {
927
- return typeof v !== 'undefined' ? toApiNumber256(v) : undefined
928
- }
929
-
930
925
  function fromApiFields(vals: node.Val[], fieldsSig: node.FieldsSig): Fields {
931
926
  return fromApiVals(vals, fieldsSig.names, fieldsSig.types)
932
927
  }
@@ -1102,7 +1097,7 @@ type BuildTxParams<T> = Omit<T, 'bytecode'> & { initialFields?: Val[] }
1102
1097
  export interface BuildDeployContractTx {
1103
1098
  signerAddress: string
1104
1099
  initialFields?: Fields
1105
- initialAttoAlphAmount?: string
1100
+ initialAttoAlphAmount?: Number256
1106
1101
  initialTokenAmounts?: Token[]
1107
1102
  issueTokenAmount?: Number256
1108
1103
  gasAmount?: number
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,
@@ -91,10 +92,10 @@ assertType<Eq<SignDeployContractTxResult, SignResult<node.BuildDeployContractTxR
91
92
  export interface SignExecuteScriptTxParams {
92
93
  signerAddress: string
93
94
  bytecode: string
94
- attoAlphAmount?: string
95
+ attoAlphAmount?: Number256
95
96
  tokens?: Token[]
96
97
  gasAmount?: number
97
- gasPrice?: string
98
+ gasPrice?: Number256
98
99
  }
99
100
  assertType<Eq<keyof SignExecuteScriptTxParams, keyof TxBuildParams<node.BuildExecuteScriptTx>>>()
100
101
  export interface SignExecuteScriptTxResult {
@@ -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 {
@@ -250,7 +253,9 @@ export abstract class SignerProviderSimple implements SignerProvider {
250
253
  async buildScriptTx(params: SignExecuteScriptTxParams): Promise<node.BuildExecuteScriptTxResult> {
251
254
  const data: node.BuildExecuteScriptTx = {
252
255
  ...(await this.usePublicKey(params)),
253
- tokens: toApiTokens(params.tokens)
256
+ attoAlphAmount: toApiNumber256Optional(params.attoAlphAmount),
257
+ tokens: toApiTokens(params.tokens),
258
+ gasPrice: toApiNumber256Optional(params.gasPrice)
254
259
  }
255
260
  return this.getNodeProvider().contracts.postContractsUnsignedTxExecuteScript(data)
256
261
  }