@alephium/web3 0.5.0-rc.16 → 0.5.0-rc.17
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/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/api/explorer-provider.d.ts +18 -0
- package/dist/src/api/explorer-provider.js +65 -0
- package/dist/src/api/index.d.ts +2 -42
- package/dist/src/api/index.js +6 -117
- package/dist/src/api/node-provider.d.ts +21 -0
- package/dist/src/api/node-provider.js +68 -0
- package/dist/src/api/types.d.ts +8 -0
- package/dist/src/api/types.js +17 -1
- package/dist/src/utils/index.d.ts +1 -0
- package/dist/src/utils/index.js +1 -0
- package/dist/src/utils/number.d.ts +6 -4
- package/dist/src/utils/number.fixture.d.ts +5 -0
- package/dist/src/utils/number.fixture.js +28 -1
- package/dist/src/utils/number.js +16 -2
- package/package.json +1 -1
- package/src/api/explorer-provider.ts +78 -0
- package/src/api/index.ts +2 -148
- package/src/api/node-provider.ts +84 -0
- package/src/api/types.ts +23 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/number.fixture.ts +28 -0
- package/src/utils/number.ts +18 -5
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
|
-
|
|
20
|
-
|
|
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
|
+
}
|
package/src/utils/index.ts
CHANGED
|
@@ -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
|
+
]
|
package/src/utils/number.ts
CHANGED
|
@@ -54,23 +54,23 @@ export const prettifyNumberConfig: Record<string, IPrettifyNumberConfig> = {
|
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
export function prettifyAttoAlphAmount(amount: bigint): string |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
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
|
|
73
|
+
return undefined
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
const numberBN = new BigNumber(number)
|
|
@@ -142,3 +142,16 @@ 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
|
+
}
|