@alephium/web3 0.4.0 → 0.5.0-rc.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.
@@ -249,8 +249,12 @@ export function buildScriptByteCode(bytecodeTemplate: string, fields: Fields, fi
249
249
  })
250
250
  }
251
251
 
252
- export function buildContractByteCode(bytecode: string, fields: Fields, fieldsSig: FieldsSig): string {
253
- const fieldsEncoded = fieldsSig.names.flatMap((fieldName, fieldIndex) => {
252
+ function encodeFields(fields: Fields, fieldsSig: FieldsSig, mutable: boolean) {
253
+ const fieldIndexes = fieldsSig.isMutable
254
+ .map((_, index) => index)
255
+ .filter((index) => fieldsSig.isMutable[`${index}`] === mutable)
256
+ const fieldsEncoded = fieldIndexes.flatMap((fieldIndex) => {
257
+ const fieldName = fieldsSig.names[`${fieldIndex}`]
254
258
  const fieldType = fieldsSig.types[`${fieldIndex}`]
255
259
  if (fieldName in fields) {
256
260
  const fieldValue = fields[`${fieldName}`]
@@ -260,7 +264,13 @@ export function buildContractByteCode(bytecode: string, fields: Fields, fieldsSi
260
264
  }
261
265
  })
262
266
  const fieldsLength = Buffer.from(encodeI256(BigInt(fieldsEncoded.length))).toString('hex')
263
- return bytecode + fieldsLength + fieldsEncoded.map((f) => Buffer.from(f).toString('hex')).join('')
267
+ return fieldsLength + fieldsEncoded.map((f) => Buffer.from(f).toString('hex')).join('')
268
+ }
269
+
270
+ export function buildContractByteCode(bytecode: string, fields: Fields, fieldsSig: FieldsSig): string {
271
+ const encodedImmFields = encodeFields(fields, fieldsSig, false)
272
+ const encodedMutFields = encodeFields(fields, fieldsSig, true)
273
+ return bytecode + encodedImmFields + encodedMutFields
264
274
  }
265
275
 
266
276
  enum ApiValType {
@@ -85,7 +85,7 @@ export abstract class TransactionBuilder {
85
85
  }
86
86
  const response = await this.nodeProvider.contracts.postContractsUnsignedTxDeployContract(data)
87
87
  const contractId = utils.binToHex(utils.contractIdFromAddress(response.contractAddress))
88
- return { ...response, contractId, gasPrice: fromApiNumber256(response.gasPrice) }
88
+ return { ...response, groupIndex: response.fromGroup, contractId, gasPrice: fromApiNumber256(response.gasPrice) }
89
89
  }
90
90
 
91
91
  async buildExecuteScriptTx(
@@ -103,7 +103,7 @@ export abstract class TransactionBuilder {
103
103
  ...rest
104
104
  }
105
105
  const response = await this.nodeProvider.contracts.postContractsUnsignedTxExecuteScript(data)
106
- return { ...response, gasPrice: fromApiNumber256(response.gasPrice) }
106
+ return { ...response, groupIndex: response.fromGroup, gasPrice: fromApiNumber256(response.gasPrice) }
107
107
  }
108
108
 
109
109
  async buildUnsignedTx(params: SignUnsignedTxParams): Promise<Omit<SignUnsignedTxResult, 'signature'>> {
@@ -73,8 +73,7 @@ export interface SignDeployContractTxParams {
73
73
  }
74
74
  assertType<Eq<keyof SignDeployContractTxParams, keyof TxBuildParams<node.BuildDeployContractTx>>>()
75
75
  export interface SignDeployContractTxResult {
76
- fromGroup: number
77
- toGroup: number
76
+ groupIndex: number
78
77
  unsignedTx: string
79
78
  txId: string
80
79
  signature: string
@@ -83,7 +82,12 @@ export interface SignDeployContractTxResult {
83
82
  gasAmount: number
84
83
  gasPrice: Number256
85
84
  }
86
- assertType<Eq<SignDeployContractTxResult, SignResult<node.BuildDeployContractTxResult> & { contractId: string }>>()
85
+ assertType<
86
+ Eq<
87
+ Omit<SignDeployContractTxResult, 'groupIndex'>,
88
+ Omit<SignResult<node.BuildDeployContractTxResult> & { contractId: string }, 'fromGroup' | 'toGroup'>
89
+ >
90
+ >()
87
91
 
88
92
  export interface SignExecuteScriptTxParams {
89
93
  signerAddress: string
@@ -95,15 +99,19 @@ export interface SignExecuteScriptTxParams {
95
99
  }
96
100
  assertType<Eq<keyof SignExecuteScriptTxParams, keyof TxBuildParams<node.BuildExecuteScriptTx>>>()
97
101
  export interface SignExecuteScriptTxResult {
98
- fromGroup: number
99
- toGroup: number
102
+ groupIndex: number
100
103
  unsignedTx: string
101
104
  txId: string
102
105
  signature: string
103
106
  gasAmount: number
104
107
  gasPrice: Number256
105
108
  }
106
- assertType<Eq<SignExecuteScriptTxResult, SignResult<node.BuildExecuteScriptTxResult>>>()
109
+ assertType<
110
+ Eq<
111
+ Omit<SignExecuteScriptTxResult, 'groupIndex'>,
112
+ Omit<SignResult<node.BuildExecuteScriptTxResult>, 'fromGroup' | 'toGroup'>
113
+ >
114
+ >()
107
115
 
108
116
  export interface SignUnsignedTxParams {
109
117
  signerAddress: string
@@ -91,7 +91,9 @@ export function groupOfAddress(address: string): number {
91
91
  } else if (addressType == AddressType.P2SH) {
92
92
  return groupOfP2shAddress(addressBody)
93
93
  } else {
94
- throw new Error(`Invalid asset address type: ${addressType}`)
94
+ // Contract Address
95
+ const id = contractIdFromAddress(address)
96
+ return id[`${id.length - 1}`]
95
97
  }
96
98
  }
97
99