@alephium/web3 1.3.0 → 1.4.0

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.
@@ -214,6 +214,12 @@ function subContractId(parentContractId, pathInHex, group) {
214
214
  if (group < 0 || group >= constants_1.TOTAL_NUMBER_OF_GROUPS) {
215
215
  throw new Error(`Invalid group ${group}`);
216
216
  }
217
+ if (!(0, utils_1.isHexString)(parentContractId)) {
218
+ throw new Error(`Invalid parent contract ID: ${parentContractId}, expected hex string`);
219
+ }
220
+ if (!(0, utils_1.isHexString)(pathInHex)) {
221
+ throw new Error(`Invalid path: ${pathInHex}, expected hex string`);
222
+ }
217
223
  const data = (0, utils_1.concatBytes)([(0, utils_1.hexToBinUnsafe)(parentContractId), (0, utils_1.hexToBinUnsafe)(pathInHex)]);
218
224
  const bytes = new Uint8Array([
219
225
  ...blakejs_1.default.blake2b(blakejs_1.default.blake2b(data, undefined, 32), undefined, 32).slice(0, -1),
@@ -10,7 +10,6 @@ export interface AddressBalance {
10
10
  lockedBalance: string;
11
11
  /** @format x.x ALPH */
12
12
  lockedBalanceHint: string;
13
- warning?: string;
14
13
  }
15
14
  /** AddressInfo */
16
15
  export interface AddressInfo {
@@ -75,7 +74,6 @@ export interface Balance {
75
74
  lockedTokenBalances?: Token[];
76
75
  /** @format int32 */
77
76
  utxoNum: number;
78
- warning?: string;
79
77
  }
80
78
  /** Balances */
81
79
  export interface Balances {
@@ -473,6 +471,7 @@ export interface CompilerOptions {
473
471
  ignoreUnusedPrivateFunctionsWarnings?: boolean;
474
472
  ignoreUpdateFieldsCheckWarnings?: boolean;
475
473
  ignoreCheckExternalCallerWarnings?: boolean;
474
+ ignoreUnusedFunctionReturnWarnings?: boolean;
476
475
  }
477
476
  /** Confirmed */
478
477
  export interface Confirmed {
@@ -1015,7 +1014,6 @@ export interface UTXO {
1015
1014
  /** UTXOs */
1016
1015
  export interface UTXOs {
1017
1016
  utxos: UTXO[];
1018
- warning?: string;
1019
1017
  }
1020
1018
  /** Unauthorized */
1021
1019
  export interface Unauthorized {
@@ -1191,7 +1189,7 @@ export declare class HttpClient<SecurityDataType = unknown> {
1191
1189
  }
1192
1190
  /**
1193
1191
  * @title Alephium API
1194
- * @version 3.4.0
1192
+ * @version 3.5.0
1195
1193
  * @baseUrl ../
1196
1194
  */
1197
1195
  export declare class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
@@ -151,7 +151,7 @@ class HttpClient {
151
151
  exports.HttpClient = HttpClient;
152
152
  /**
153
153
  * @title Alephium API
154
- * @version 3.4.0
154
+ * @version 3.5.0
155
155
  * @baseUrl ../
156
156
  */
157
157
  class Api extends HttpClient {
@@ -182,6 +182,7 @@ export declare abstract class ContractFactory<I extends ContractInstance, F exte
182
182
  constructor(contract: Contract);
183
183
  abstract at(address: string): I;
184
184
  deploy(signer: SignerProvider, deployParams: DeployContractParams<F>): Promise<DeployContractResult<I>>;
185
+ deployTemplate(signer: SignerProvider): Promise<DeployContractResult<I>>;
185
186
  stateForTest(initFields: F, asset?: Asset, address?: string): ContractState<F>;
186
187
  }
187
188
  export declare class ExecutableScript<P extends Fields = Fields, R extends Val | null = null> {
@@ -60,7 +60,8 @@ exports.DEFAULT_NODE_COMPILER_OPTIONS = {
60
60
  ignoreUnusedFieldsWarnings: false,
61
61
  ignoreUnusedPrivateFunctionsWarnings: false,
62
62
  ignoreUpdateFieldsCheckWarnings: false,
63
- ignoreCheckExternalCallerWarnings: false
63
+ ignoreCheckExternalCallerWarnings: false,
64
+ ignoreUnusedFunctionReturnWarnings: false
64
65
  };
65
66
  exports.DEFAULT_COMPILER_OPTIONS = { errorOnWarnings: true, ...exports.DEFAULT_NODE_COMPILER_OPTIONS };
66
67
  class Struct {
@@ -627,6 +628,11 @@ class ContractFactory {
627
628
  contractInstance: this.at(result.contractAddress)
628
629
  };
629
630
  }
631
+ async deployTemplate(signer) {
632
+ return this.deploy(signer, {
633
+ initialFields: this.contract.getInitialFieldsWithDefaultValues()
634
+ });
635
+ }
630
636
  // This is used for testing contract functions
631
637
  stateForTest(initFields, asset, address) {
632
638
  const newAsset = {
@@ -1068,8 +1074,8 @@ exports.callMethod = callMethod;
1068
1074
  async function signExecuteMethod(contract, instance, methodName, params) {
1069
1075
  const methodIndex = contract.contract.getMethodIndex(methodName);
1070
1076
  const functionSig = contract.contract.functions[methodIndex];
1071
- const usePreapprovedAssets = contract.contract.decodedMethods[methodIndex].usePreapprovedAssets;
1072
- const bytecodeTemplate = getBytecodeTemplate(methodIndex, usePreapprovedAssets, functionSig, contract.contract.structs, params.attoAlphAmount, params.tokens);
1077
+ const methodUsePreapprovedAssets = contract.contract.decodedMethods[methodIndex].usePreapprovedAssets;
1078
+ const bytecodeTemplate = getBytecodeTemplate(methodIndex, methodUsePreapprovedAssets, functionSig, contract.contract.structs, params.attoAlphAmount, params.tokens);
1073
1079
  const fieldsSig = toFieldsSig(contract.contract.name, functionSig);
1074
1080
  const bytecode = ralph.buildScriptByteCode(bytecodeTemplate, { __contract__: instance.contractId, ...params.args }, fieldsSig, contract.contract.structs);
1075
1081
  const signer = params.signer;
@@ -1086,16 +1092,17 @@ async function signExecuteMethod(contract, instance, methodName, params) {
1086
1092
  return await signer.signAndSubmitExecuteScriptTx(signerParams);
1087
1093
  }
1088
1094
  exports.signExecuteMethod = signExecuteMethod;
1089
- function getBytecodeTemplate(methodIndex, usePreapprovedAssets, functionSig, structs, attoAlphAmount, tokens) {
1095
+ function getBytecodeTemplate(methodIndex, methodUsePreapprovedAssets, functionSig, structs, attoAlphAmount, tokens) {
1090
1096
  // For the default TxScript main function
1091
1097
  const numberOfMethods = '01';
1092
1098
  const isPublic = '01';
1093
- const modifier = usePreapprovedAssets ? '03' : '00';
1099
+ const scriptUseApprovedAssets = attoAlphAmount !== undefined || tokens !== undefined;
1100
+ const modifier = scriptUseApprovedAssets ? '03' : '00';
1094
1101
  const argsLength = '00';
1095
1102
  const returnsLength = '00';
1096
1103
  const [templateVarStoreLocalInstrs, templateVarsLength] = getTemplateVarStoreLocalInstrs(functionSig, structs);
1097
- const approveAlphInstrs = getApproveAlphInstrs(usePreapprovedAssets ? attoAlphAmount : undefined);
1098
- const approveTokensInstrs = getApproveTokensInstrs(usePreapprovedAssets ? tokens : undefined);
1104
+ const approveAlphInstrs = getApproveAlphInstrs(methodUsePreapprovedAssets ? attoAlphAmount : undefined);
1105
+ const approveTokensInstrs = getApproveTokensInstrs(methodUsePreapprovedAssets ? tokens : undefined);
1099
1106
  const callerInstrs = getCallAddressInstrs(approveAlphInstrs.length / 2 + approveTokensInstrs.length / 3);
1100
1107
  // First template var is the contract
1101
1108
  const functionArgsNum = encodeU256Const(BigInt(templateVarsLength - 1));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
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": "3.4.0",
36
+ "alephium_version": "3.5.0",
37
37
  "explorer_backend_version": "2.0.0"
38
38
  },
39
39
  "type": "commonjs",
@@ -22,7 +22,7 @@ import { TOTAL_NUMBER_OF_GROUPS } from '../constants'
22
22
  import blake from 'blakejs'
23
23
  import bs58 from '../utils/bs58'
24
24
  import djb2 from '../utils/djb2'
25
- import { binToHex, concatBytes, hexToBinUnsafe } from '../utils'
25
+ import { binToHex, concatBytes, hexToBinUnsafe, isHexString } from '../utils'
26
26
  import { KeyType } from '../signer'
27
27
  import { MultiSig, lockupScriptCodec } from '../codec/lockup-script-codec'
28
28
  import { compactSignedIntCodec } from '../codec'
@@ -211,6 +211,12 @@ export function subContractId(parentContractId: string, pathInHex: string, group
211
211
  if (group < 0 || group >= TOTAL_NUMBER_OF_GROUPS) {
212
212
  throw new Error(`Invalid group ${group}`)
213
213
  }
214
+ if (!isHexString(parentContractId)) {
215
+ throw new Error(`Invalid parent contract ID: ${parentContractId}, expected hex string`)
216
+ }
217
+ if (!isHexString(pathInHex)) {
218
+ throw new Error(`Invalid path: ${pathInHex}, expected hex string`)
219
+ }
214
220
  const data = concatBytes([hexToBinUnsafe(parentContractId), hexToBinUnsafe(pathInHex)])
215
221
  const bytes = new Uint8Array([
216
222
  ...blake.blake2b(blake.blake2b(data, undefined, 32), undefined, 32).slice(0, -1),
@@ -21,7 +21,6 @@ export interface AddressBalance {
21
21
  lockedBalance: string
22
22
  /** @format x.x ALPH */
23
23
  lockedBalanceHint: string
24
- warning?: string
25
24
  }
26
25
 
27
26
  /** AddressInfo */
@@ -93,7 +92,6 @@ export interface Balance {
93
92
  lockedTokenBalances?: Token[]
94
93
  /** @format int32 */
95
94
  utxoNum: number
96
- warning?: string
97
95
  }
98
96
 
99
97
  /** Balances */
@@ -527,6 +525,7 @@ export interface CompilerOptions {
527
525
  ignoreUnusedPrivateFunctionsWarnings?: boolean
528
526
  ignoreUpdateFieldsCheckWarnings?: boolean
529
527
  ignoreCheckExternalCallerWarnings?: boolean
528
+ ignoreUnusedFunctionReturnWarnings?: boolean
530
529
  }
531
530
 
532
531
  /** Confirmed */
@@ -1146,7 +1145,6 @@ export interface UTXO {
1146
1145
  /** UTXOs */
1147
1146
  export interface UTXOs {
1148
1147
  utxos: UTXO[]
1149
- warning?: string
1150
1148
  }
1151
1149
 
1152
1150
  /** Unauthorized */
@@ -1495,7 +1493,7 @@ export class HttpClient<SecurityDataType = unknown> {
1495
1493
 
1496
1494
  /**
1497
1495
  * @title Alephium API
1498
- * @version 3.4.0
1496
+ * @version 3.5.0
1499
1497
  * @baseUrl ../
1500
1498
  */
1501
1499
  export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDataType> {
@@ -112,7 +112,8 @@ export const DEFAULT_NODE_COMPILER_OPTIONS: node.CompilerOptions = {
112
112
  ignoreUnusedFieldsWarnings: false,
113
113
  ignoreUnusedPrivateFunctionsWarnings: false,
114
114
  ignoreUpdateFieldsCheckWarnings: false,
115
- ignoreCheckExternalCallerWarnings: false
115
+ ignoreCheckExternalCallerWarnings: false,
116
+ ignoreUnusedFunctionReturnWarnings: false
116
117
  }
117
118
 
118
119
  export const DEFAULT_COMPILER_OPTIONS: CompilerOptions = { errorOnWarnings: true, ...DEFAULT_NODE_COMPILER_OPTIONS }
@@ -1010,6 +1011,12 @@ export abstract class ContractFactory<I extends ContractInstance, F extends Fiel
1010
1011
  }
1011
1012
  }
1012
1013
 
1014
+ async deployTemplate(signer: SignerProvider): Promise<DeployContractResult<I>> {
1015
+ return this.deploy(signer, {
1016
+ initialFields: this.contract.getInitialFieldsWithDefaultValues() as F
1017
+ })
1018
+ }
1019
+
1013
1020
  // This is used for testing contract functions
1014
1021
  stateForTest(initFields: F, asset?: Asset, address?: string): ContractState<F> {
1015
1022
  const newAsset = {
@@ -1681,10 +1688,10 @@ export async function signExecuteMethod<I extends ContractInstance, F extends Fi
1681
1688
  ): Promise<SignExecuteScriptTxResult> {
1682
1689
  const methodIndex = contract.contract.getMethodIndex(methodName)
1683
1690
  const functionSig = contract.contract.functions[methodIndex]
1684
- const usePreapprovedAssets = contract.contract.decodedMethods[methodIndex].usePreapprovedAssets
1691
+ const methodUsePreapprovedAssets = contract.contract.decodedMethods[methodIndex].usePreapprovedAssets
1685
1692
  const bytecodeTemplate = getBytecodeTemplate(
1686
1693
  methodIndex,
1687
- usePreapprovedAssets,
1694
+ methodUsePreapprovedAssets,
1688
1695
  functionSig,
1689
1696
  contract.contract.structs,
1690
1697
  params.attoAlphAmount,
@@ -1716,7 +1723,7 @@ export async function signExecuteMethod<I extends ContractInstance, F extends Fi
1716
1723
 
1717
1724
  function getBytecodeTemplate(
1718
1725
  methodIndex: number,
1719
- usePreapprovedAssets: boolean,
1726
+ methodUsePreapprovedAssets: boolean,
1720
1727
  functionSig: FunctionSig,
1721
1728
  structs: Struct[],
1722
1729
  attoAlphAmount?: Number256,
@@ -1725,14 +1732,15 @@ function getBytecodeTemplate(
1725
1732
  // For the default TxScript main function
1726
1733
  const numberOfMethods = '01'
1727
1734
  const isPublic = '01'
1728
- const modifier = usePreapprovedAssets ? '03' : '00'
1735
+ const scriptUseApprovedAssets = attoAlphAmount !== undefined || tokens !== undefined
1736
+ const modifier = scriptUseApprovedAssets ? '03' : '00'
1729
1737
  const argsLength = '00'
1730
1738
  const returnsLength = '00'
1731
1739
 
1732
1740
  const [templateVarStoreLocalInstrs, templateVarsLength] = getTemplateVarStoreLocalInstrs(functionSig, structs)
1733
1741
 
1734
- const approveAlphInstrs: string[] = getApproveAlphInstrs(usePreapprovedAssets ? attoAlphAmount : undefined)
1735
- const approveTokensInstrs: string[] = getApproveTokensInstrs(usePreapprovedAssets ? tokens : undefined)
1742
+ const approveAlphInstrs: string[] = getApproveAlphInstrs(methodUsePreapprovedAssets ? attoAlphAmount : undefined)
1743
+ const approveTokensInstrs: string[] = getApproveTokensInstrs(methodUsePreapprovedAssets ? tokens : undefined)
1736
1744
  const callerInstrs: string[] = getCallAddressInstrs(approveAlphInstrs.length / 2 + approveTokensInstrs.length / 3)
1737
1745
 
1738
1746
  // First template var is the contract