@alephium/web3 1.5.2 → 1.7.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.
@@ -162,7 +162,7 @@ export abstract class Artifact {
162
162
  this.functions = functions
163
163
  }
164
164
 
165
- abstract buildByteCodeToDeploy(initialFields: Fields, isDevnet: boolean): string
165
+ abstract buildByteCodeToDeploy(initialFields: Fields, isDevnet: boolean, exposePrivateFunctions: boolean): string
166
166
 
167
167
  async isDevnet(signer: SignerProvider): Promise<boolean> {
168
168
  if (!signer.nodeProvider) {
@@ -197,7 +197,10 @@ export class Contract extends Artifact {
197
197
 
198
198
  readonly bytecodeDebug: string
199
199
  readonly codeHashDebug: string
200
- readonly decodedMethods: Method[]
200
+ readonly decodedContract: contract.Contract
201
+
202
+ private bytecodeForTesting: string | undefined
203
+ private codeHashForTesting: string | undefined
201
204
 
202
205
  constructor(
203
206
  version: string,
@@ -230,19 +233,54 @@ export class Contract extends Artifact {
230
233
  this.bytecodeDebug = ralph.buildDebugBytecode(this.bytecode, this.bytecodeDebugPatch)
231
234
  this.codeHashDebug = codeHashDebug
232
235
 
233
- this.decodedMethods = contract.contractCodec.decodeContract(hexToBinUnsafe(bytecode)).methods
236
+ this.decodedContract = contract.contractCodec.decodeContract(hexToBinUnsafe(this.bytecode))
237
+ this.bytecodeForTesting = undefined
238
+ this.codeHashForTesting = undefined
239
+ }
240
+
241
+ getByteCodeForTesting(): string {
242
+ if (this.bytecodeForTesting !== undefined) return this.bytecodeForTesting
243
+
244
+ if (this.publicFunctions().length == this.functions.length) {
245
+ this.bytecodeForTesting = this.bytecodeDebug
246
+ this.codeHashForTesting = this.codeHashDebug
247
+ return this.bytecodeForTesting
248
+ }
249
+
250
+ const decodedDebugContract = contract.contractCodec.decodeContract(hexToBinUnsafe(this.bytecodeDebug))
251
+ const methods = decodedDebugContract.methods.map((method) => ({ ...method, isPublic: true }))
252
+ const bytecodeForTesting = contract.contractCodec.encodeContract({
253
+ fieldLength: decodedDebugContract.fieldLength,
254
+ methods: methods
255
+ })
256
+ const codeHashForTesting = blake.blake2b(bytecodeForTesting, undefined, 32)
257
+ this.bytecodeForTesting = binToHex(bytecodeForTesting)
258
+ this.codeHashForTesting = binToHex(codeHashForTesting)
259
+ return this.bytecodeForTesting
260
+ }
261
+
262
+ hasCodeHash(hash: string): boolean {
263
+ return this.codeHash === hash || this.codeHashDebug === hash || this.codeHashForTesting === hash
264
+ }
265
+
266
+ getDecodedMethod(methodIndex: number): Method {
267
+ return this.decodedContract.methods[`${methodIndex}`]
234
268
  }
235
269
 
236
270
  publicFunctions(): FunctionSig[] {
237
- return this.functions.filter((_, index) => this.decodedMethods[`${index}`].isPublic)
271
+ return this.functions.filter((_, index) => this.getDecodedMethod(index).isPublic)
238
272
  }
239
273
 
240
274
  usingPreapprovedAssetsFunctions(): FunctionSig[] {
241
- return this.functions.filter((_, index) => this.decodedMethods[`${index}`].usePreapprovedAssets)
275
+ return this.functions.filter((_, index) => this.getDecodedMethod(index).usePreapprovedAssets)
242
276
  }
243
277
 
244
278
  usingAssetsInContractFunctions(): FunctionSig[] {
245
- return this.functions.filter((_, index) => this.decodedMethods[`${index}`].useContractAssets)
279
+ return this.functions.filter((_, index) => this.getDecodedMethod(index).useContractAssets)
280
+ }
281
+
282
+ isMethodUsePreapprovedAssets(methodIndex: number): boolean {
283
+ return this.getDecodedMethod(methodIndex).usePreapprovedAssets
246
284
  }
247
285
 
248
286
  // TODO: safely parse json
@@ -530,7 +568,11 @@ export class Contract extends Artifact {
530
568
  ): Promise<SignDeployContractTxParams> {
531
569
  const isDevnet = await this.isDevnet(signer)
532
570
  const initialFields: Fields = params.initialFields ?? {}
533
- const bytecode = this.buildByteCodeToDeploy(addStdIdToFields(this, initialFields), isDevnet)
571
+ const bytecode = this.buildByteCodeToDeploy(
572
+ addStdIdToFields(this, initialFields),
573
+ isDevnet,
574
+ params.exposePrivateFunctions ?? false
575
+ )
534
576
  const selectedAccount = await signer.getSelectedAccount()
535
577
  const signerParams: SignDeployContractTxParams = {
536
578
  signerAddress: selectedAccount.address,
@@ -546,14 +588,15 @@ export class Contract extends Artifact {
546
588
  return signerParams
547
589
  }
548
590
 
549
- buildByteCodeToDeploy(initialFields: Fields, isDevnet: boolean): string {
591
+ buildByteCodeToDeploy(initialFields: Fields, isDevnet: boolean, exposePrivateFunctions = false): string {
550
592
  try {
551
- return ralph.buildContractByteCode(
552
- isDevnet ? this.bytecodeDebug : this.bytecode,
553
- initialFields,
554
- this.fieldsSig,
555
- this.structs
556
- )
593
+ const bytecode =
594
+ exposePrivateFunctions && isDevnet
595
+ ? this.getByteCodeForTesting()
596
+ : isDevnet
597
+ ? this.bytecodeDebug
598
+ : this.bytecode
599
+ return ralph.buildContractByteCode(bytecode, initialFields, this.fieldsSig, this.structs)
557
600
  } catch (error) {
558
601
  throw new Error(`Failed to build bytecode for contract ${this.name}, error: ${error}`)
559
602
  }
@@ -975,10 +1018,11 @@ export interface DeployContractParams<P extends Fields = Fields> {
975
1018
  issueTokenTo?: string
976
1019
  gasAmount?: number
977
1020
  gasPrice?: Number256
1021
+ exposePrivateFunctions?: boolean
978
1022
  }
979
1023
  assertType<
980
1024
  Eq<
981
- Omit<DeployContractParams<undefined>, 'initialFields'>,
1025
+ Omit<DeployContractParams<undefined>, 'initialFields' | 'exposePrivateFunctions'>,
982
1026
  Omit<SignDeployContractTxParams, 'signerAddress' | 'signerKeyType' | 'bytecode'>
983
1027
  >
984
1028
  >
@@ -1687,7 +1731,7 @@ export async function signExecuteMethod<I extends ContractInstance, F extends Fi
1687
1731
  ): Promise<SignExecuteScriptTxResult> {
1688
1732
  const methodIndex = contract.contract.getMethodIndex(methodName)
1689
1733
  const functionSig = contract.contract.functions[methodIndex]
1690
- const methodUsePreapprovedAssets = contract.contract.decodedMethods[methodIndex].usePreapprovedAssets
1734
+ const methodUsePreapprovedAssets = contract.contract.isMethodUsePreapprovedAssets(methodIndex)
1691
1735
  const bytecodeTemplate = getBytecodeTemplate(
1692
1736
  methodIndex,
1693
1737
  methodUsePreapprovedAssets,
@@ -1944,7 +1988,7 @@ export async function multicallMethods<I extends ContractInstance, F extends Fie
1944
1988
  })
1945
1989
  const result = await getCurrentNodeProvider().contracts.postContractsMulticallContract({ calls: callsParams.flat() })
1946
1990
  let callResultIndex = 0
1947
- const results = callsParams.map((calls, index0) => {
1991
+ return callsParams.map((calls, index0) => {
1948
1992
  const callsResult: Record<string, CallContractResult<any>> = {}
1949
1993
  const entries = callEntries[`${index0}`]
1950
1994
  calls.forEach((call, index1) => {
@@ -1961,7 +2005,6 @@ export async function multicallMethods<I extends ContractInstance, F extends Fie
1961
2005
  })
1962
2006
  return callsResult
1963
2007
  })
1964
- return results.length === 1 ? results[0] : results
1965
2008
  }
1966
2009
 
1967
2010
  export async function getContractEventsCurrentCount(contractAddress: Address): Promise<number> {