@alephium/web3 2.0.0 → 2.0.2

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.
@@ -576,6 +576,7 @@ export interface CompileProjectResult {
576
576
  constants?: Constant[];
577
577
  enums?: Enum[];
578
578
  warnings?: string[];
579
+ testError?: string;
579
580
  }
580
581
  /** CompileScriptResult */
581
582
  export interface CompileScriptResult {
@@ -1443,7 +1444,7 @@ export declare class HttpClient<SecurityDataType = unknown> {
1443
1444
  }
1444
1445
  /**
1445
1446
  * @title Alephium API
1446
- * @version 4.2.0
1447
+ * @version 4.2.3
1447
1448
  * @baseUrl ../
1448
1449
  */
1449
1450
  export declare class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
@@ -1939,7 +1940,9 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
1939
1940
  * @summary Get the UTXOs of an address
1940
1941
  * @request GET:/addresses/{address}/utxos
1941
1942
  */
1942
- getAddressesAddressUtxos: (address: string, params?: RequestParams) => Promise<UTXOs>;
1943
+ getAddressesAddressUtxos: (address: string, query?: {
1944
+ 'error-if-exceed-max-utxos'?: boolean;
1945
+ }, params?: RequestParams) => Promise<UTXOs>;
1943
1946
  /**
1944
1947
  * No description
1945
1948
  *
@@ -151,7 +151,7 @@ class HttpClient {
151
151
  exports.HttpClient = HttpClient;
152
152
  /**
153
153
  * @title Alephium API
154
- * @version 4.2.0
154
+ * @version 4.2.3
155
155
  * @baseUrl ../
156
156
  */
157
157
  class Api extends HttpClient {
@@ -829,9 +829,10 @@ class Api extends HttpClient {
829
829
  * @summary Get the UTXOs of an address
830
830
  * @request GET:/addresses/{address}/utxos
831
831
  */
832
- getAddressesAddressUtxos: (address, params = {}) => this.request({
832
+ getAddressesAddressUtxos: (address, query, params = {}) => this.request({
833
833
  path: `/addresses/${address}/utxos`,
834
834
  method: 'GET',
835
+ query: query,
835
836
  format: 'json',
836
837
  ...params
837
838
  }).then(utils_1.convertHttpResponse),
@@ -1,5 +1,5 @@
1
1
  import { NamedVals, node, NodeProvider, Number256, Token, Val } from '../api';
2
- import { SignDeployContractTxParams, SignDeployContractTxResult, SignExecuteScriptTxParams, SignerProvider, Address, SignExecuteScriptTxResult } from '../signer';
2
+ import { SignDeployContractTxParams, SignDeployContractTxResult, SignExecuteScriptTxParams, SignerProvider, Address, SignExecuteScriptTxResult, Account } from '../signer';
3
3
  import { Optional, HexString } from '../utils';
4
4
  import { EventSubscribeOptions, EventSubscription } from './events';
5
5
  import { contract, Method } from '../codec';
@@ -101,6 +101,7 @@ export declare class Script extends Artifact {
101
101
  static fromJson(artifact: any, bytecodeDebugPatch?: string, structs?: Struct[]): Script;
102
102
  static fromArtifactFile(path: string, bytecodeDebugPatch: string, structs?: Struct[]): Promise<Script>;
103
103
  toString(): string;
104
+ getBytecodeAndGroup<P extends Fields>(account: Account, fields: P): [string, number];
104
105
  txParamsForExecution<P extends Fields>(params: ExecuteScriptParams<P>): Promise<SignExecuteScriptTxParams>;
105
106
  buildByteCodeToDeploy(initialFields: Fields): string;
106
107
  }
@@ -522,10 +522,23 @@ class Script extends Artifact {
522
522
  };
523
523
  return JSON.stringify(object, null, 2);
524
524
  }
525
+ getBytecodeAndGroup(account, fields) {
526
+ const bytecode = this.buildByteCodeToDeploy(fields);
527
+ if ((0, signer_1.isGroupedAccount)(account)) {
528
+ return [bytecode, account.group];
529
+ }
530
+ const group = getGroupFromTxScript(bytecode);
531
+ const defaultGroup = (0, address_1.groupOfAddress)(account.address);
532
+ if (group === undefined || group === defaultGroup) {
533
+ return [bytecode, defaultGroup];
534
+ }
535
+ const newFields = ralph.updateFieldsWithGroup(fields, group);
536
+ const newBytecode = this.buildByteCodeToDeploy(newFields);
537
+ return [newBytecode, group];
538
+ }
525
539
  async txParamsForExecution(params) {
526
540
  const selectedAccount = await params.signer.getSelectedAccount();
527
- const bytecode = this.buildByteCodeToDeploy(params.initialFields ?? {});
528
- const group = getGroupFromTxScript(bytecode, selectedAccount);
541
+ const [bytecode, group] = this.getBytecodeAndGroup(selectedAccount, params.initialFields ?? {});
529
542
  const signerParams = {
530
543
  signerAddress: selectedAccount.address,
531
544
  signerKeyType: selectedAccount.keyType,
@@ -549,9 +562,7 @@ class Script extends Artifact {
549
562
  }
550
563
  }
551
564
  exports.Script = Script;
552
- function getGroupFromTxScript(bytecode, account) {
553
- if ((0, signer_1.isGroupedAccount)(account))
554
- return account.group;
565
+ function getGroupFromTxScript(bytecode) {
555
566
  const script = script_codec_1.scriptCodec.decode((0, utils_1.hexToBinUnsafe)(bytecode));
556
567
  const instrs = script.methods.flatMap((method) => method.instrs);
557
568
  for (let index = 0; index < instrs.length - 1; index += 1) {
@@ -574,7 +585,7 @@ function getGroupFromTxScript(bytecode, account) {
574
585
  }
575
586
  }
576
587
  }
577
- return (0, address_1.groupOfAddress)(account.address);
588
+ return undefined;
578
589
  }
579
590
  function fromApiFields(immFields, mutFields, fieldsSig, structs) {
580
591
  let [immIndex, mutIndex] = [0, 0];
@@ -63,6 +63,7 @@ export declare function flattenFields(fields: Fields, names: string[], types: st
63
63
  value: Val;
64
64
  isMutable: boolean;
65
65
  }[];
66
+ export declare function updateFieldsWithGroup(fields: Fields, group: number): Fields;
66
67
  export declare function buildScriptByteCode(bytecodeTemplate: string, fields: Fields, fieldsSig: FieldsSig, structs: Struct[]): string;
67
68
  export declare function encodeContractFields(fields: Fields, fieldsSig: FieldsSig, structs: Struct[]): {
68
69
  encodedImmFields: Uint8Array;
@@ -17,7 +17,7 @@ You should have received a copy of the GNU Lesser General Public License
17
17
  along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
  */
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.buildDebugBytecode = exports.encodeContractField = exports.buildContractByteCode = exports.encodeContractFields = exports.buildScriptByteCode = exports.flattenFields = exports.typeLength = exports.encodeMapKey = exports.decodePrimitive = exports.tryDecodeMapDebugLog = exports.calcFieldSize = exports.encodeMapPrefix = exports.parseMapType = exports.splitFields = exports.encodeScriptField = exports.encodeScriptFieldAsString = exports.encodePrimitiveValues = exports.addressVal = exports.byteVecVal = exports.u256Val = exports.i256Val = exports.boolVal = exports.encodeVmAddress = exports.encodeVmByteVec = exports.encodeVmU256 = exports.encodeVmI256 = exports.encodeVmBool = exports.VmValType = exports.encodeAddress = exports.encodeByteVec = void 0;
20
+ exports.buildDebugBytecode = exports.encodeContractField = exports.buildContractByteCode = exports.encodeContractFields = exports.buildScriptByteCode = exports.updateFieldsWithGroup = exports.flattenFields = exports.typeLength = exports.encodeMapKey = exports.decodePrimitive = exports.tryDecodeMapDebugLog = exports.calcFieldSize = exports.encodeMapPrefix = exports.parseMapType = exports.splitFields = exports.encodeScriptField = exports.encodeScriptFieldAsString = exports.encodePrimitiveValues = exports.addressVal = exports.byteVecVal = exports.u256Val = exports.i256Val = exports.boolVal = exports.encodeVmAddress = exports.encodeVmByteVec = exports.encodeVmU256 = exports.encodeVmI256 = exports.encodeVmBool = exports.VmValType = exports.encodeAddress = exports.encodeByteVec = void 0;
21
21
  const api_1 = require("../api");
22
22
  const utils_1 = require("../utils");
23
23
  const codec_1 = require("../codec");
@@ -336,6 +336,31 @@ function checkPrimitiveValue(name, ralphType, value) {
336
336
  }
337
337
  throw Error(`Invalid value ${value} for ${name}, expected a value of type ${ralphType}`);
338
338
  }
339
+ function updateFieldsWithGroup(fields, group) {
340
+ const newFields = {};
341
+ for (const key in fields) {
342
+ const value = fields[`${key}`];
343
+ newFields[`${key}`] = updateValWithGroup(value, group);
344
+ }
345
+ return newFields;
346
+ }
347
+ exports.updateFieldsWithGroup = updateFieldsWithGroup;
348
+ function updateValWithGroup(value, group) {
349
+ if (typeof value === 'string') {
350
+ if (!(0, address_1.isValidAddress)(value))
351
+ return value;
352
+ if ((0, address_1.isGrouplessAddressWithoutGroupIndex)(value))
353
+ return `${value}:${group}`;
354
+ return value;
355
+ }
356
+ if (Array.isArray(value)) {
357
+ return value.map((v) => updateValWithGroup(v, group));
358
+ }
359
+ if (typeof value === 'object') {
360
+ return updateFieldsWithGroup(value, group);
361
+ }
362
+ return value;
363
+ }
339
364
  const scriptFieldRegex = /\{([0-9]*)\}/g;
340
365
  function buildScriptByteCode(bytecodeTemplate, fields, fieldsSig, structs) {
341
366
  const allFields = flattenFields(fields, fieldsSig.names, fieldsSig.types, fieldsSig.isMutable, structs);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "A JS/TS library to interact with the Alephium platform",
5
5
  "license": "GPL",
6
6
  "main": "dist/src/index.js",
@@ -33,7 +33,7 @@
33
33
  },
34
34
  "author": "Alephium dev <dev@alephium.org>",
35
35
  "config": {
36
- "alephium_version": "4.2.0",
36
+ "alephium_version": "4.2.3",
37
37
  "explorer_backend_version": "2.3.2"
38
38
  },
39
39
  "type": "commonjs",
@@ -646,6 +646,7 @@ export interface CompileProjectResult {
646
646
  constants?: Constant[]
647
647
  enums?: Enum[]
648
648
  warnings?: string[]
649
+ testError?: string
649
650
  }
650
651
 
651
652
  /** CompileScriptResult */
@@ -1777,7 +1778,7 @@ export class HttpClient<SecurityDataType = unknown> {
1777
1778
 
1778
1779
  /**
1779
1780
  * @title Alephium API
1780
- * @version 4.2.0
1781
+ * @version 4.2.3
1781
1782
  * @baseUrl ../
1782
1783
  */
1783
1784
  export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
@@ -2787,13 +2788,20 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
2787
2788
  * @summary Get the UTXOs of an address
2788
2789
  * @request GET:/addresses/{address}/utxos
2789
2790
  */
2790
- getAddressesAddressUtxos: (address: string, params: RequestParams = {}) =>
2791
+ getAddressesAddressUtxos: (
2792
+ address: string,
2793
+ query?: {
2794
+ 'error-if-exceed-max-utxos'?: boolean
2795
+ },
2796
+ params: RequestParams = {}
2797
+ ) =>
2791
2798
  this.request<
2792
2799
  UTXOs,
2793
2800
  BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable | GatewayTimeout
2794
2801
  >({
2795
2802
  path: `/addresses/${address}/utxos`,
2796
2803
  method: 'GET',
2804
+ query: query,
2797
2805
  format: 'json',
2798
2806
  ...params
2799
2807
  }).then(convertHttpResponse),
@@ -796,10 +796,26 @@ export class Script extends Artifact {
796
796
  return JSON.stringify(object, null, 2)
797
797
  }
798
798
 
799
+ getBytecodeAndGroup<P extends Fields>(account: Account, fields: P): [string, number] {
800
+ const bytecode = this.buildByteCodeToDeploy(fields)
801
+ if (isGroupedAccount(account)) {
802
+ return [bytecode, account.group]
803
+ }
804
+
805
+ const group = getGroupFromTxScript(bytecode)
806
+ const defaultGroup = groupOfAddress(account.address)
807
+ if (group === undefined || group === defaultGroup) {
808
+ return [bytecode, defaultGroup]
809
+ }
810
+
811
+ const newFields = ralph.updateFieldsWithGroup(fields, group) as P
812
+ const newBytecode = this.buildByteCodeToDeploy(newFields)
813
+ return [newBytecode, group]
814
+ }
815
+
799
816
  async txParamsForExecution<P extends Fields>(params: ExecuteScriptParams<P>): Promise<SignExecuteScriptTxParams> {
800
817
  const selectedAccount = await params.signer.getSelectedAccount()
801
- const bytecode = this.buildByteCodeToDeploy(params.initialFields ?? {})
802
- const group = getGroupFromTxScript(bytecode, selectedAccount)
818
+ const [bytecode, group] = this.getBytecodeAndGroup(selectedAccount, params.initialFields ?? {})
803
819
  const signerParams: SignExecuteScriptTxParams = {
804
820
  signerAddress: selectedAccount.address,
805
821
  signerKeyType: selectedAccount.keyType,
@@ -823,9 +839,7 @@ export class Script extends Artifact {
823
839
  }
824
840
  }
825
841
 
826
- function getGroupFromTxScript(bytecode: string, account: Account): number {
827
- if (isGroupedAccount(account)) return account.group
828
-
842
+ function getGroupFromTxScript(bytecode: string): number | undefined {
829
843
  const script = scriptCodec.decode(hexToBinUnsafe(bytecode))
830
844
  const instrs = script.methods.flatMap((method) => method.instrs)
831
845
  for (let index = 0; index < instrs.length - 1; index += 1) {
@@ -850,7 +864,7 @@ function getGroupFromTxScript(bytecode: string, account: Account): number {
850
864
  }
851
865
  }
852
866
  }
853
- return groupOfAddress(account.address)
867
+ return undefined
854
868
  }
855
869
 
856
870
  export function fromApiFields(
@@ -39,13 +39,13 @@ import {
39
39
  instrCodec,
40
40
  u256Codec,
41
41
  toU256,
42
- toI256,
43
- intAs4BytesCodec
42
+ toI256
44
43
  } from '../codec'
45
44
  import { boolCodec } from '../codec/codec'
46
45
  import { TraceableError } from '../error'
47
- import djb2 from '../utils/djb2'
48
- import { addressToBytes, groupFromHint } from '../address'
46
+ import { addressToBytes, isGrouplessAddressWithoutGroupIndex, isValidAddress } from '../address'
47
+ import { scriptCodec } from '../codec/script-codec'
48
+ import { TOTAL_NUMBER_OF_GROUPS } from '../constants'
49
49
 
50
50
  export function encodeByteVec(hex: string): Uint8Array {
51
51
  if (!isHexString(hex)) {
@@ -399,6 +399,32 @@ function checkPrimitiveValue(name: string, ralphType: string, value: Val): strin
399
399
  throw Error(`Invalid value ${value} for ${name}, expected a value of type ${ralphType}`)
400
400
  }
401
401
 
402
+ export function updateFieldsWithGroup(fields: Fields, group: number): Fields {
403
+ const newFields: Fields = {}
404
+ for (const key in fields) {
405
+ const value = fields[`${key}`]
406
+ newFields[`${key}`] = updateValWithGroup(value, group)
407
+ }
408
+ return newFields
409
+ }
410
+
411
+ function updateValWithGroup(value: Val, group: number): Val {
412
+ if (typeof value === 'string') {
413
+ if (!isValidAddress(value)) return value
414
+ if (isGrouplessAddressWithoutGroupIndex(value)) return `${value}:${group}`
415
+ return value
416
+ }
417
+
418
+ if (Array.isArray(value)) {
419
+ return value.map((v) => updateValWithGroup(v, group))
420
+ }
421
+
422
+ if (typeof value === 'object') {
423
+ return updateFieldsWithGroup(value as Fields, group)
424
+ }
425
+ return value
426
+ }
427
+
402
428
  const scriptFieldRegex = /\{([0-9]*)\}/g
403
429
 
404
430
  export function buildScriptByteCode(