@alephium/web3 0.2.0-rc.10 → 0.2.0-rc.11

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.
@@ -8,3 +8,4 @@ export declare class ExplorerProvider extends ExplorerApi<null> {
8
8
  }
9
9
  export * as node from './api-alephium';
10
10
  export * as explorer from './api-explorer';
11
+ export * from './types';
@@ -39,6 +39,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
39
39
  __setModuleDefault(result, mod);
40
40
  return result;
41
41
  };
42
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
43
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
44
+ };
42
45
  Object.defineProperty(exports, "__esModule", { value: true });
43
46
  exports.explorer = exports.node = exports.ExplorerProvider = exports.NodeProvider = void 0;
44
47
  const api_alephium_1 = require("./api-alephium");
@@ -68,3 +71,4 @@ class ExplorerProvider extends api_explorer_1.Api {
68
71
  exports.ExplorerProvider = ExplorerProvider;
69
72
  exports.node = __importStar(require("./api-alephium"));
70
73
  exports.explorer = __importStar(require("./api-explorer"));
74
+ __exportStar(require("./types"), exports);
@@ -0,0 +1,23 @@
1
+ import * as node from './api-alephium';
2
+ export declare type Number256 = number | bigint | string;
3
+ export declare type Val = Number256 | boolean | string | Val[];
4
+ export declare type NamedVals = Record<string, Val>;
5
+ export interface Token {
6
+ id: string;
7
+ amount: Number256;
8
+ }
9
+ export declare function toApiToken(token: Token): node.Token;
10
+ export declare function toApiTokens(tokens?: Token[]): node.Token[] | undefined;
11
+ export declare function fromApiToken(token: node.Token): Token;
12
+ export declare function fromApiTokens(tokens?: node.Token[]): Token[] | undefined;
13
+ export declare function toApiBoolean(v: Val): boolean;
14
+ export declare function toApiNumber256(v: Val): string;
15
+ export declare function toApiNumber256Optional(v?: Val): string | undefined;
16
+ export declare function fromApiNumber256(n: string): Number256;
17
+ export declare function toApiByteVec(v: Val): string;
18
+ export declare function toApiAddress(v: Val): string;
19
+ export declare function toApiArray(tpe: string, v: Val): node.Val;
20
+ export declare function toApiVal(v: Val, tpe: string): node.Val;
21
+ export declare function fromApiVals(vals: node.Val[], names: string[], types: string[]): NamedVals;
22
+ export declare function fromApiArray(vals: node.Val[], types: string[]): Val[];
23
+ export declare function fromApiVal(v: node.Val, tpe: string): Val;
@@ -0,0 +1,240 @@
1
+ "use strict";
2
+ /*
3
+ Copyright 2018 - 2022 The Alephium Authors
4
+ This file is part of the alephium project.
5
+
6
+ The library is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Lesser General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ The library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public License
17
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.fromApiVal = exports.fromApiArray = exports.fromApiVals = exports.toApiVal = exports.toApiArray = exports.toApiAddress = exports.toApiByteVec = exports.fromApiNumber256 = exports.toApiNumber256Optional = exports.toApiNumber256 = exports.toApiBoolean = exports.fromApiTokens = exports.fromApiToken = exports.toApiTokens = exports.toApiToken = void 0;
21
+ const utils_1 = require("../utils");
22
+ utils_1.assertType;
23
+ function toApiToken(token) {
24
+ return { id: token.id, amount: toApiNumber256(token.amount) };
25
+ }
26
+ exports.toApiToken = toApiToken;
27
+ function toApiTokens(tokens) {
28
+ return tokens?.map(toApiToken);
29
+ }
30
+ exports.toApiTokens = toApiTokens;
31
+ function fromApiToken(token) {
32
+ return { id: token.id, amount: fromApiNumber256(token.amount) };
33
+ }
34
+ exports.fromApiToken = fromApiToken;
35
+ function fromApiTokens(tokens) {
36
+ return tokens?.map(fromApiToken);
37
+ }
38
+ exports.fromApiTokens = fromApiTokens;
39
+ function toApiBoolean(v) {
40
+ if (typeof v === 'boolean') {
41
+ return v;
42
+ }
43
+ else {
44
+ throw new Error(`Invalid boolean value: ${v}`);
45
+ }
46
+ }
47
+ exports.toApiBoolean = toApiBoolean;
48
+ // TODO: check integer bounds
49
+ function toApiNumber256(v) {
50
+ if ((typeof v === 'number' && Number.isInteger(v)) || typeof v === 'bigint') {
51
+ return v.toString();
52
+ }
53
+ else if (typeof v === 'string') {
54
+ return v;
55
+ }
56
+ else {
57
+ throw new Error(`Invalid 256 bit number: ${v}`);
58
+ }
59
+ }
60
+ exports.toApiNumber256 = toApiNumber256;
61
+ function toApiNumber256Optional(v) {
62
+ return v === undefined ? undefined : toApiNumber256(v);
63
+ }
64
+ exports.toApiNumber256Optional = toApiNumber256Optional;
65
+ function fromApiNumber256(n) {
66
+ if (Number.isSafeInteger(Number.parseInt(n))) {
67
+ return Number(n);
68
+ }
69
+ else {
70
+ return BigInt(n);
71
+ }
72
+ }
73
+ exports.fromApiNumber256 = fromApiNumber256;
74
+ // TODO: check hex string
75
+ function toApiByteVec(v) {
76
+ if (typeof v === 'string') {
77
+ // try to convert from address to contract id
78
+ try {
79
+ const address = utils_1.bs58.decode(v);
80
+ if (address.length == 33 && address[0] == 3) {
81
+ return Buffer.from(address.slice(1)).toString('hex');
82
+ }
83
+ }
84
+ catch (_) {
85
+ return v;
86
+ }
87
+ return v;
88
+ }
89
+ else {
90
+ throw new Error(`Invalid string: ${v}`);
91
+ }
92
+ }
93
+ exports.toApiByteVec = toApiByteVec;
94
+ function toApiAddress(v) {
95
+ if (typeof v === 'string') {
96
+ try {
97
+ utils_1.bs58.decode(v);
98
+ return v;
99
+ }
100
+ catch (error) {
101
+ throw new Error(`Invalid base58 string: ${v}`);
102
+ }
103
+ }
104
+ else {
105
+ throw new Error(`Invalid string: ${v}`);
106
+ }
107
+ }
108
+ exports.toApiAddress = toApiAddress;
109
+ function toApiArray(tpe, v) {
110
+ if (!Array.isArray(v)) {
111
+ throw new Error(`Expected array, got ${v}`);
112
+ }
113
+ const semiColonIndex = tpe.lastIndexOf(';');
114
+ if (semiColonIndex == -1) {
115
+ throw new Error(`Invalid Val type: ${tpe}`);
116
+ }
117
+ const subType = tpe.slice(1, semiColonIndex);
118
+ const dim = parseInt(tpe.slice(semiColonIndex + 1, -1));
119
+ if (v.length != dim) {
120
+ throw new Error(`Invalid val dimension: ${v}`);
121
+ }
122
+ else {
123
+ return { value: v.map((v) => toApiVal(v, subType)), type: 'Array' };
124
+ }
125
+ }
126
+ exports.toApiArray = toApiArray;
127
+ function toApiVal(v, tpe) {
128
+ if (tpe === 'Bool') {
129
+ return { value: toApiBoolean(v), type: tpe };
130
+ }
131
+ else if (tpe === 'U256' || tpe === 'I256') {
132
+ return { value: toApiNumber256(v), type: tpe };
133
+ }
134
+ else if (tpe === 'ByteVec') {
135
+ return { value: toApiByteVec(v), type: tpe };
136
+ }
137
+ else if (tpe === 'Address') {
138
+ return { value: toApiAddress(v), type: tpe };
139
+ }
140
+ else {
141
+ return toApiArray(tpe, v);
142
+ }
143
+ }
144
+ exports.toApiVal = toApiVal;
145
+ function _fromApiVal(vals, valIndex, tpe) {
146
+ if (vals.length === 0) {
147
+ throw new Error('Not enough Vals');
148
+ }
149
+ const firstVal = vals[`${valIndex}`];
150
+ if (tpe === 'Bool' && firstVal.type === tpe) {
151
+ return [firstVal.value, valIndex + 1];
152
+ }
153
+ else if ((tpe === 'U256' || tpe === 'I256') && firstVal.type === tpe) {
154
+ return [fromApiNumber256(firstVal.value), valIndex + 1];
155
+ }
156
+ else if ((tpe === 'ByteVec' || tpe === 'Address') && firstVal.type === tpe) {
157
+ return [firstVal.value, valIndex + 1];
158
+ }
159
+ else {
160
+ const [baseType, dims] = decodeArrayType(tpe);
161
+ const arraySize = dims.reduce((a, b) => a * b);
162
+ const nextIndex = valIndex + arraySize;
163
+ const valsToUse = vals.slice(valIndex, nextIndex);
164
+ if (valsToUse.length == arraySize && valsToUse.every((val) => val.type === baseType)) {
165
+ const localVals = valsToUse.map((val) => fromApiVal(val, baseType));
166
+ return [foldVals(localVals, dims), nextIndex];
167
+ }
168
+ else {
169
+ throw new Error(`Invalid array Val type: ${valsToUse}, ${tpe}`);
170
+ }
171
+ }
172
+ }
173
+ function fromApiVals(vals, names, types) {
174
+ let valIndex = 0;
175
+ const result = {};
176
+ types.forEach((currentType, index) => {
177
+ const currentName = names[`${index}`];
178
+ const [val, nextIndex] = _fromApiVal(vals, valIndex, currentType);
179
+ valIndex = nextIndex;
180
+ result[`${currentName}`] = val;
181
+ });
182
+ return result;
183
+ }
184
+ exports.fromApiVals = fromApiVals;
185
+ function fromApiArray(vals, types) {
186
+ let valIndex = 0;
187
+ const result = [];
188
+ for (const currentType of types) {
189
+ const [val, nextIndex] = _fromApiVal(vals, valIndex, currentType);
190
+ result.push(val);
191
+ valIndex = nextIndex;
192
+ }
193
+ return result;
194
+ }
195
+ exports.fromApiArray = fromApiArray;
196
+ function fromApiVal(v, tpe) {
197
+ if (v.type === 'Bool' && v.type === tpe) {
198
+ return v.value;
199
+ }
200
+ else if ((v.type === 'U256' || v.type === 'I256') && v.type === tpe) {
201
+ return fromApiNumber256(v.value);
202
+ }
203
+ else if ((v.type === 'ByteVec' || v.type === 'Address') && v.type === tpe) {
204
+ return v.value;
205
+ }
206
+ else {
207
+ throw new Error(`Invalid node.Val type: ${v}`);
208
+ }
209
+ }
210
+ exports.fromApiVal = fromApiVal;
211
+ function decodeArrayType(tpe) {
212
+ const semiColonIndex = tpe.lastIndexOf(';');
213
+ if (semiColonIndex === -1) {
214
+ throw new Error(`Invalid Val type: ${tpe}`);
215
+ }
216
+ const subType = tpe.slice(1, semiColonIndex);
217
+ const dim = parseInt(tpe.slice(semiColonIndex + 1, -1));
218
+ if (subType[0] == '[') {
219
+ const [baseType, subDim] = decodeArrayType(subType);
220
+ return [baseType, (subDim.unshift(dim), subDim)];
221
+ }
222
+ else {
223
+ return [subType, [dim]];
224
+ }
225
+ }
226
+ function foldVals(vals, dims) {
227
+ if (dims.length == 1) {
228
+ return vals;
229
+ }
230
+ else {
231
+ const result = [];
232
+ const chunkSize = vals.length / dims[0];
233
+ const chunkDims = dims.slice(1);
234
+ for (let i = 0; i < vals.length; i += chunkSize) {
235
+ const chunk = vals.slice(i, i + chunkSize);
236
+ result.push(foldVals(chunk, chunkDims));
237
+ }
238
+ return result;
239
+ }
240
+ }
@@ -1,9 +1,10 @@
1
- import { node, NodeProvider } from '../api';
1
+ import { NamedVals, node, NodeProvider, Number256, Token, Val } from '../api';
2
2
  import { SignDeployContractTxParams, SignExecuteScriptTxParams, SignerWithNodeProvider } from '../signer';
3
- import { CompileContractResult, CompileScriptResult } from '../api/api-alephium';
4
- declare type FieldsSig = node.FieldsSig;
5
- declare type EventSig = node.EventSig;
6
- declare type FunctionSig = node.FunctionSig;
3
+ export declare type FieldsSig = node.FieldsSig;
4
+ export declare type EventSig = node.EventSig;
5
+ export declare type FunctionSig = node.FunctionSig;
6
+ export declare type Fields = NamedVals;
7
+ export declare type Arguments = NamedVals;
7
8
  declare enum SourceType {
8
9
  Contract = 0,
9
10
  Script = 1,
@@ -70,8 +71,8 @@ export declare class Project {
70
71
  private constructor();
71
72
  private getContractPath;
72
73
  static checkCompilerWarnings(warnings: string[], errorOnWarnings: boolean): void;
73
- static contract(path: string): Contract;
74
- static script(path: string): Script;
74
+ static contract(name: string): Contract;
75
+ static script(name: string): Script;
75
76
  private saveArtifactsToFile;
76
77
  contractByCodeHash(codeHash: string): Contract;
77
78
  private static compile;
@@ -81,8 +82,9 @@ export declare class Project {
81
82
  static build(compilerOptionsPartial?: Partial<CompilerOptions>, contractsRootPath?: string, artifactsRootPath?: string): Promise<void>;
82
83
  }
83
84
  export declare abstract class Artifact {
85
+ readonly name: string;
84
86
  readonly functions: FunctionSig[];
85
- constructor(functions: FunctionSig[]);
87
+ constructor(name: string, functions: FunctionSig[]);
86
88
  abstract buildByteCodeToDeploy(initialFields?: Fields): string;
87
89
  publicFunctions(): string[];
88
90
  usingPreapprovedAssetsFunctions(): string[];
@@ -93,9 +95,9 @@ export declare class Contract extends Artifact {
93
95
  readonly codeHash: string;
94
96
  readonly fieldsSig: FieldsSig;
95
97
  readonly eventsSig: EventSig[];
96
- constructor(bytecode: string, codeHash: string, fieldsSig: FieldsSig, eventsSig: EventSig[], functions: FunctionSig[]);
98
+ constructor(name: string, bytecode: string, codeHash: string, fieldsSig: FieldsSig, eventsSig: EventSig[], functions: FunctionSig[]);
97
99
  static fromJson(artifact: any): Contract;
98
- static fromCompileResult(result: CompileContractResult): Contract;
100
+ static fromCompileResult(result: node.CompileContractResult): Contract;
99
101
  static fromArtifactFile(path: string): Promise<Contract>;
100
102
  fetchState(address: string, group: number): Promise<ContractState>;
101
103
  toString(): string;
@@ -121,8 +123,8 @@ export declare class Contract extends Artifact {
121
123
  export declare class Script extends Artifact {
122
124
  readonly bytecodeTemplate: string;
123
125
  readonly fieldsSig: FieldsSig;
124
- constructor(bytecodeTemplate: string, fieldsSig: FieldsSig, functions: FunctionSig[]);
125
- static fromCompileResult(result: CompileScriptResult): Script;
126
+ constructor(name: string, bytecodeTemplate: string, fieldsSig: FieldsSig, functions: FunctionSig[]);
127
+ static fromCompileResult(result: node.CompileScriptResult): Script;
126
128
  static fromJson(artifact: any): Script;
127
129
  static fromArtifactFile(path: string): Promise<Script>;
128
130
  toString(): string;
@@ -130,21 +132,10 @@ export declare class Script extends Artifact {
130
132
  transactionForDeployment(signer: SignerWithNodeProvider, params: Omit<BuildExecuteScriptTx, 'signerAddress'>): Promise<BuildScriptTxResult>;
131
133
  buildByteCodeToDeploy(initialFields: Fields): string;
132
134
  }
133
- export declare type Number256 = number | bigint | string;
134
- export declare type Val = Number256 | boolean | string | Val[];
135
- export declare type NamedVals = Record<string, Val>;
136
- export declare type Fields = NamedVals;
137
- export declare type Arguments = NamedVals;
138
- export declare function extractArray(tpe: string, v: Val): node.Val;
139
- export declare function toApiVal(v: Val, tpe: string): node.Val;
140
135
  export interface Asset {
141
136
  alphAmount: Number256;
142
137
  tokens?: Token[];
143
138
  }
144
- export interface Token {
145
- id: string;
146
- amount: Number256;
147
- }
148
139
  export interface InputAsset {
149
140
  address: string;
150
141
  asset: Asset;
@@ -201,7 +192,7 @@ export interface ContractOutput {
201
192
  type: string;
202
193
  address: string;
203
194
  alphAmount: Number256;
204
- tokens: Token[];
195
+ tokens?: Token[];
205
196
  }
206
197
  export interface DeployContractTransaction {
207
198
  fromGroup: number;