@alephium/web3 0.5.0-rc.16 → 0.5.0-rc.18

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.
@@ -0,0 +1,78 @@
1
+ /*
2
+ Copyright 2018 - 2022 The Alephium Authors
3
+ This file is part of the alephium project.
4
+
5
+ The library is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ The library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+ import { ApiRequestArguments, ApiRequestHandler, forwardRequests, request } from './types'
20
+ import { Api as ExplorerApi } from './api-explorer'
21
+
22
+ function initializeExplorerApi(baseUrl: string, apiKey?: string): ExplorerApi<string> {
23
+ const explorerApi = new ExplorerApi<string>({
24
+ baseUrl: baseUrl,
25
+ baseApiParams: { secure: true },
26
+ securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {})
27
+ })
28
+ explorerApi.setSecurityData(apiKey ?? null)
29
+ return explorerApi
30
+ }
31
+
32
+ export class ExplorerProvider {
33
+ readonly blocks: ExplorerApi<string>['blocks']
34
+ readonly transactions: ExplorerApi<string>['transactions']
35
+ readonly addresses: ExplorerApi<string>['addresses']
36
+ readonly infos: ExplorerApi<string>['infos']
37
+ readonly mempool: ExplorerApi<string>['mempool']
38
+ readonly tokens: ExplorerApi<string>['tokens']
39
+ readonly charts: ExplorerApi<string>['charts']
40
+ readonly utils: ExplorerApi<string>['utils']
41
+
42
+ constructor(baseUrl: string, apiKey?: string)
43
+ constructor(provider: ExplorerProvider)
44
+ constructor(handler: ApiRequestHandler)
45
+ constructor(param0: string | ExplorerProvider | ApiRequestHandler, apiKey?: string) {
46
+ let explorerApi: ExplorerProvider
47
+ if (typeof param0 === 'string') {
48
+ explorerApi = initializeExplorerApi(param0, apiKey)
49
+ } else if (typeof param0 === 'function') {
50
+ explorerApi = new ExplorerProvider('https://1.2.3.4:0')
51
+ forwardRequests(explorerApi, param0 as ApiRequestHandler)
52
+ } else {
53
+ explorerApi = param0 as ExplorerProvider
54
+ }
55
+
56
+ this.blocks = { ...explorerApi.blocks }
57
+ this.transactions = { ...explorerApi.transactions }
58
+ this.addresses = { ...explorerApi.addresses }
59
+ this.infos = { ...explorerApi.infos }
60
+ this.mempool = { ...explorerApi.mempool }
61
+ this.tokens = { ...explorerApi.tokens }
62
+ this.charts = { ...explorerApi.charts }
63
+ this.utils = { ...explorerApi.utils }
64
+ }
65
+
66
+ request = (args: ApiRequestArguments): Promise<any> => {
67
+ return request(this, args)
68
+ }
69
+
70
+ // This can prevent the proxied explorer provider from being modified
71
+ static Proxy(explorerProvider: ExplorerProvider): ExplorerProvider {
72
+ return new ExplorerProvider(explorerProvider)
73
+ }
74
+
75
+ static Remote(handler: ApiRequestHandler): ExplorerProvider {
76
+ return new ExplorerProvider(handler)
77
+ }
78
+ }
package/src/api/index.ts CHANGED
@@ -16,154 +16,8 @@ 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 { Api as NodeApi } from './api-alephium'
20
- import { Api as ExplorerApi } from './api-explorer'
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
-
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 mempool: NodeApi<string>['mempool']
62
- readonly contracts: NodeApi<string>['contracts']
63
- readonly multisig: NodeApi<string>['multisig']
64
- readonly utils: NodeApi<string>['utils']
65
- readonly miners: NodeApi<string>['miners']
66
- readonly events: NodeApi<string>['events']
67
-
68
- constructor(baseUrl: string, apiKey?: string)
69
- constructor(provider: NodeProvider)
70
- constructor(handler: ApiRequestHandler)
71
- constructor(param0: string | NodeProvider | ApiRequestHandler, apiKey?: string) {
72
- let nodeApi: NodeProvider
73
- if (typeof param0 === 'string') {
74
- nodeApi = initializeNodeApi(param0, apiKey)
75
- } else if (typeof param0 === 'function') {
76
- nodeApi = new NodeProvider('https://1.2.3.4:0')
77
- forwardRequests(nodeApi, param0 as ApiRequestHandler)
78
- } else {
79
- nodeApi = param0 as NodeProvider
80
- }
81
-
82
- this.wallets = { ...nodeApi.wallets }
83
- this.infos = { ...nodeApi.infos }
84
- this.blockflow = { ...nodeApi.blockflow }
85
- this.addresses = { ...nodeApi.addresses }
86
- this.transactions = { ...nodeApi.transactions }
87
- this.mempool = { ...nodeApi.mempool }
88
- this.contracts = { ...nodeApi.contracts }
89
- this.multisig = { ...nodeApi.multisig }
90
- this.utils = { ...nodeApi.utils }
91
- this.miners = { ...nodeApi.miners }
92
- this.events = { ...nodeApi.events }
93
- }
94
-
95
- request = (args: ApiRequestArguments): Promise<any> => {
96
- return request(this, args)
97
- }
98
-
99
- // This can prevent the proxied node provider from being modified
100
- static Proxy(nodeProvider: NodeProvider): NodeProvider {
101
- return new NodeProvider(nodeProvider)
102
- }
103
-
104
- static Remote(handler: ApiRequestHandler): NodeProvider {
105
- return new NodeProvider(handler)
106
- }
107
- }
108
-
109
- function initializeExplorerApi(baseUrl: string, apiKey?: string): ExplorerApi<string> {
110
- const explorerApi = new ExplorerApi<string>({
111
- baseUrl: baseUrl,
112
- baseApiParams: { secure: true },
113
- securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {})
114
- })
115
- explorerApi.setSecurityData(apiKey ?? null)
116
- return explorerApi
117
- }
118
-
119
- export class ExplorerProvider {
120
- readonly blocks = ExplorerApi['blocks']
121
- readonly transactions = ExplorerApi['transactions']
122
- readonly addresses = ExplorerApi['addresses']
123
- readonly infos = ExplorerApi['infos']
124
- readonly mempool = ExplorerApi['mempool']
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
141
- }
142
-
143
- this.blocks = { ...explorerApi.blocks }
144
- this.transactions = { ...explorerApi.transactions }
145
- this.addresses = { ...explorerApi.addresses }
146
- this.infos = { ...explorerApi.infos }
147
- this.mempool = { ...explorerApi.mempool }
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)
164
- }
165
- }
166
-
19
+ export * from './node-provider'
20
+ export * from './explorer-provider'
167
21
  export * as node from './api-alephium'
168
22
  export * as explorer from './api-explorer'
169
23
  export * from './types'
@@ -0,0 +1,84 @@
1
+ /*
2
+ Copyright 2018 - 2022 The Alephium Authors
3
+ This file is part of the alephium project.
4
+
5
+ The library is free software: you can redistribute it and/or modify
6
+ it under the terms of the GNU Lesser General Public License as published by
7
+ the Free Software Foundation, either version 3 of the License, or
8
+ (at your option) any later version.
9
+
10
+ The library is distributed in the hope that it will be useful,
11
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ GNU Lesser General Public License for more details.
14
+
15
+ You should have received a copy of the GNU Lesser General Public License
16
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
17
+ */
18
+
19
+ import { ApiRequestArguments, ApiRequestHandler, forwardRequests, request } from './types'
20
+ import { Api as NodeApi } from './api-alephium'
21
+
22
+ function initializeNodeApi(baseUrl: string, apiKey?: string): NodeApi<string> {
23
+ const nodeApi = new NodeApi<string>({
24
+ baseUrl: baseUrl,
25
+ baseApiParams: { secure: true },
26
+ securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {})
27
+ })
28
+ nodeApi.setSecurityData(apiKey ?? null)
29
+ return nodeApi
30
+ }
31
+
32
+ export class NodeProvider {
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 mempool: NodeApi<string>['mempool']
39
+ readonly contracts: NodeApi<string>['contracts']
40
+ readonly multisig: NodeApi<string>['multisig']
41
+ readonly utils: NodeApi<string>['utils']
42
+ readonly miners: NodeApi<string>['miners']
43
+ readonly events: NodeApi<string>['events']
44
+
45
+ constructor(baseUrl: string, apiKey?: string)
46
+ constructor(provider: NodeProvider)
47
+ constructor(handler: ApiRequestHandler)
48
+ constructor(param0: string | NodeProvider | ApiRequestHandler, apiKey?: string) {
49
+ let nodeApi: NodeProvider
50
+ if (typeof param0 === 'string') {
51
+ nodeApi = initializeNodeApi(param0, apiKey)
52
+ } else if (typeof param0 === 'function') {
53
+ nodeApi = new NodeProvider('https://1.2.3.4:0')
54
+ forwardRequests(nodeApi, param0 as ApiRequestHandler)
55
+ } else {
56
+ nodeApi = param0 as NodeProvider
57
+ }
58
+
59
+ this.wallets = { ...nodeApi.wallets }
60
+ this.infos = { ...nodeApi.infos }
61
+ this.blockflow = { ...nodeApi.blockflow }
62
+ this.addresses = { ...nodeApi.addresses }
63
+ this.transactions = { ...nodeApi.transactions }
64
+ this.mempool = { ...nodeApi.mempool }
65
+ this.contracts = { ...nodeApi.contracts }
66
+ this.multisig = { ...nodeApi.multisig }
67
+ this.utils = { ...nodeApi.utils }
68
+ this.miners = { ...nodeApi.miners }
69
+ this.events = { ...nodeApi.events }
70
+ }
71
+
72
+ request = (args: ApiRequestArguments): Promise<any> => {
73
+ return request(this, args)
74
+ }
75
+
76
+ // This can prevent the proxied node provider from being modified
77
+ static Proxy(nodeProvider: NodeProvider): NodeProvider {
78
+ return new NodeProvider(nodeProvider)
79
+ }
80
+
81
+ static Remote(handler: ApiRequestHandler): NodeProvider {
82
+ return new NodeProvider(handler)
83
+ }
84
+ }
package/src/api/types.ts CHANGED
@@ -245,3 +245,26 @@ export function typeLength(tpe: string): number {
245
245
  const [, dims] = decodeArrayType(tpe)
246
246
  return dims.reduce((a, b) => a * b)
247
247
  }
248
+
249
+ export interface ApiRequestArguments {
250
+ path: string
251
+ method: string
252
+ params: any[]
253
+ }
254
+ export type ApiRequestHandler = (args: ApiRequestArguments) => Promise<any>
255
+
256
+ export function forwardRequests(api: Record<string, any>, handler: ApiRequestHandler): void {
257
+ // Update class properties to forward requests
258
+ for (const [path, pathObject] of Object.entries(api)) {
259
+ for (const method of Object.keys(pathObject)) {
260
+ pathObject[`${method}`] = async (...params: any): Promise<any> => {
261
+ return handler({ path, method, params })
262
+ }
263
+ }
264
+ }
265
+ }
266
+
267
+ export async function request(provider: Record<string, any>, args: ApiRequestArguments): Promise<any> {
268
+ const call = provider[`${args.path}`][`${args.method}`] as (...any) => Promise<any>
269
+ return call(...args.params)
270
+ }
@@ -54,7 +54,9 @@ import {
54
54
  Eq,
55
55
  Optional,
56
56
  groupOfAddress,
57
- addressFromContractId
57
+ addressFromContractId,
58
+ stringifyJsonWithBigint,
59
+ parseJsonWithBigint
58
60
  } from '../utils'
59
61
  import { getCurrentNodeProvider } from '../global'
60
62
  import * as path from 'path'
@@ -207,7 +209,7 @@ class ProjectArtifact {
207
209
  compilerOptionsUsed: this.compilerOptionsUsed,
208
210
  infos: Object.fromEntries(new Map([...this.infos].sort()))
209
211
  }
210
- const content = JSON.stringify(artifact, null, 2)
212
+ const content = stringifyJsonWithBigint(artifact, 2)
211
213
  return fsPromises.writeFile(filepath, content)
212
214
  }
213
215
 
@@ -241,7 +243,7 @@ class ProjectArtifact {
241
243
  return undefined
242
244
  }
243
245
  const content = await fsPromises.readFile(filepath)
244
- const json = JSON.parse(content.toString())
246
+ const json = parseJsonWithBigint(content.toString())
245
247
  const compilerOptionsUsed = json.compilerOptionsUsed as node.CompilerOptions
246
248
  const files = new Map(Object.entries<CodeInfo>(json.infos))
247
249
  return new ProjectArtifact(compilerOptionsUsed, files)
@@ -737,7 +739,7 @@ export class Contract extends Artifact {
737
739
  // support both 'code.ral' and 'code.ral.json'
738
740
  static async fromArtifactFile(path: string, bytecodeDebugPatch: string, codeHashDebug: string): Promise<Contract> {
739
741
  const content = await fsPromises.readFile(path)
740
- const artifact = JSON.parse(content.toString())
742
+ const artifact = parseJsonWithBigint(content.toString())
741
743
  return Contract.fromJson(artifact, bytecodeDebugPatch, codeHashDebug)
742
744
  }
743
745
 
@@ -751,7 +753,7 @@ export class Contract extends Artifact {
751
753
  eventsSig: this.eventsSig,
752
754
  functions: this.functions
753
755
  }
754
- return JSON.stringify(object, null, 2)
756
+ return stringifyJsonWithBigint(object, 2)
755
757
  }
756
758
 
757
759
  toState<T extends Fields>(fields: T, asset: Asset, address?: string): ContractState<T> {
@@ -1047,7 +1049,7 @@ export class Script extends Artifact {
1047
1049
 
1048
1050
  static async fromArtifactFile(path: string, bytecodeDebugPatch: string): Promise<Script> {
1049
1051
  const content = await fsPromises.readFile(path)
1050
- const artifact = JSON.parse(content.toString())
1052
+ const artifact = parseJsonWithBigint(content.toString())
1051
1053
  return this.fromJson(artifact, bytecodeDebugPatch)
1052
1054
  }
1053
1055
 
@@ -1059,7 +1061,7 @@ export class Script extends Artifact {
1059
1061
  fieldsSig: this.fieldsSig,
1060
1062
  functions: this.functions
1061
1063
  }
1062
- return JSON.stringify(object, null, 2)
1064
+ return stringifyJsonWithBigint(object, 2)
1063
1065
  }
1064
1066
 
1065
1067
  async txParamsForExecution<P extends Fields>(
package/src/index.ts CHANGED
@@ -16,10 +16,6 @@ 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
- BigInt.prototype['toJSON'] = function () {
20
- return this.toString()
21
- }
22
-
23
19
  export * from './api'
24
20
  export * from './contract'
25
21
  export * from './signer'
@@ -61,7 +61,7 @@ export abstract class SignerProvider {
61
61
  const derivedAddress = addressFromPublicKey(account.publicKey, account.keyType)
62
62
  const derivedGroup = groupOfAddress(derivedAddress)
63
63
  if (derivedAddress !== account.address || derivedGroup !== account.group) {
64
- throw Error(`Invalid accounot data: ${JSON.stringify(account)}`)
64
+ throw Error(`Invalid account data: ${JSON.stringify(account)}`)
65
65
  }
66
66
  }
67
67
 
@@ -22,3 +22,4 @@ export * from './djb2'
22
22
  export * from './utils'
23
23
  export * from './subscription'
24
24
  export * from './sign'
25
+ export * from './number'
@@ -157,3 +157,31 @@ export const tests = [
157
157
  tokenFormat: '0.0'
158
158
  }
159
159
  ]
160
+
161
+ export const tests1 = [
162
+ {
163
+ raw: '0',
164
+ decimals: 18,
165
+ amount: 0n
166
+ },
167
+ {
168
+ raw: '1.23',
169
+ decimals: 2,
170
+ amount: 123n
171
+ },
172
+ {
173
+ raw: '1',
174
+ decimals: 5,
175
+ amount: 100000n
176
+ },
177
+ {
178
+ raw: '1',
179
+ decimals: 18,
180
+ amount: 1000000000000000000n
181
+ },
182
+ {
183
+ raw: '1.23456789',
184
+ decimals: 18,
185
+ amount: 1234567890000000000n
186
+ }
187
+ ]
@@ -54,23 +54,23 @@ export const prettifyNumberConfig: Record<string, IPrettifyNumberConfig> = {
54
54
  }
55
55
  }
56
56
 
57
- export function prettifyAttoAlphAmount(amount: bigint): string | null {
57
+ export function prettifyAttoAlphAmount(amount: bigint): string | undefined {
58
58
  return prettifyNumber(amount, 18, prettifyNumberConfig.ALPH)
59
59
  }
60
60
 
61
- export function prettifyTokenAmount(amount: bigint, decimals: number): string | null {
61
+ export function prettifyTokenAmount(amount: bigint, decimals: number): string | undefined {
62
62
  return prettifyNumber(amount, decimals, prettifyNumberConfig.TOKEN)
63
63
  }
64
64
 
65
- export function prettifyExactAmount(amount: bigint, decimals: number): string | null {
65
+ export function prettifyExactAmount(amount: bigint, decimals: number): string | undefined {
66
66
  return prettifyNumber(amount, decimals, prettifyNumberConfig.Exact)
67
67
  }
68
68
 
69
- export function prettifyNumber(amount: bigint, decimals: number, config: IPrettifyNumberConfig): string | null {
69
+ export function prettifyNumber(amount: bigint, decimals: number, config: IPrettifyNumberConfig): string | undefined {
70
70
  const number = toFixedNumber(amount, decimals)
71
71
 
72
72
  if (!isNumeric(number)) {
73
- return null
73
+ return undefined
74
74
  }
75
75
 
76
76
  const numberBN = new BigNumber(number)
@@ -142,3 +142,50 @@ function toFixedNumber(val: bigint, decimals: number): string {
142
142
  }
143
143
  return negative + str
144
144
  }
145
+
146
+ export function convertAmountWithDecimals(amount: string | number, decimals: number): bigint | undefined {
147
+ try {
148
+ const result = new BigNumber(amount).multipliedBy(Math.pow(10, decimals))
149
+ return BigInt(result.toFormat(0, { groupSeparator: '' }))
150
+ } catch (e) {
151
+ return undefined
152
+ }
153
+ }
154
+
155
+ export function convertAlphAmount(amount: string | number): bigint | undefined {
156
+ return convertAmountWithDecimals(amount, 18)
157
+ }
158
+
159
+ function replacer(key: string, value: any): any {
160
+ if (typeof value === 'bigint') {
161
+ return { __bigintval__: value.toString() }
162
+ }
163
+ return value
164
+ }
165
+
166
+ function reviver(key: string, value: any): any {
167
+ if (value != null && typeof value === 'object' && '__bigintval__' in value) {
168
+ return BigInt(value['__bigintval__'])
169
+ }
170
+ return value
171
+ }
172
+
173
+ export function stringifyJsonWithBigint(obj: any, space?: string | number): string {
174
+ const bigintToString = BigInt.prototype['toJSON']
175
+ BigInt.prototype['toJSON'] = undefined
176
+
177
+ const result = JSON.stringify(obj, replacer, space)
178
+
179
+ BigInt.prototype['toJSON'] = bigintToString
180
+ return result
181
+ }
182
+
183
+ export function parseJsonWithBigint(text: string): any {
184
+ const bigintToString = BigInt.prototype['toJSON']
185
+ BigInt.prototype['toJSON'] = undefined
186
+
187
+ const result = JSON.parse(text, reviver)
188
+
189
+ BigInt.prototype['toJSON'] = bigintToString
190
+ return result
191
+ }