@aztec-labs/aztec.js 6.0.0-nightly.20260829

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.
Files changed (275) hide show
  1. package/README.md +44 -0
  2. package/dest/account/account.d.ts +44 -0
  3. package/dest/account/account.d.ts.map +1 -0
  4. package/dest/account/account.js +31 -0
  5. package/dest/account/account_contract.d.ts +49 -0
  6. package/dest/account/account_contract.d.ts.map +1 -0
  7. package/dest/account/account_contract.js +20 -0
  8. package/dest/account/index.d.ts +11 -0
  9. package/dest/account/index.d.ts.map +1 -0
  10. package/dest/account/index.js +6 -0
  11. package/dest/api/abi.d.ts +4 -0
  12. package/dest/api/abi.d.ts.map +1 -0
  13. package/dest/api/abi.js +1 -0
  14. package/dest/api/account.d.ts +5 -0
  15. package/dest/api/account.d.ts.map +1 -0
  16. package/dest/api/account.js +3 -0
  17. package/dest/api/addresses.d.ts +4 -0
  18. package/dest/api/addresses.d.ts.map +1 -0
  19. package/dest/api/addresses.js +3 -0
  20. package/dest/api/authorization.d.ts +5 -0
  21. package/dest/api/authorization.d.ts.map +1 -0
  22. package/dest/api/authorization.js +4 -0
  23. package/dest/api/block.d.ts +3 -0
  24. package/dest/api/block.d.ts.map +1 -0
  25. package/dest/api/block.js +2 -0
  26. package/dest/api/contract.d.ts +48 -0
  27. package/dest/api/contract.d.ts.map +1 -0
  28. package/dest/api/contract.js +46 -0
  29. package/dest/api/crypto.d.ts +2 -0
  30. package/dest/api/crypto.d.ts.map +1 -0
  31. package/dest/api/crypto.js +1 -0
  32. package/dest/api/deployment.d.ts +4 -0
  33. package/dest/api/deployment.d.ts.map +1 -0
  34. package/dest/api/deployment.js +3 -0
  35. package/dest/api/eth_address.d.ts +2 -0
  36. package/dest/api/eth_address.d.ts.map +1 -0
  37. package/dest/api/eth_address.js +1 -0
  38. package/dest/api/ethereum.d.ts +2 -0
  39. package/dest/api/ethereum.d.ts.map +1 -0
  40. package/dest/api/ethereum.js +1 -0
  41. package/dest/api/event_cursor.d.ts +44 -0
  42. package/dest/api/event_cursor.d.ts.map +1 -0
  43. package/dest/api/event_cursor.js +42 -0
  44. package/dest/api/events.d.ts +36 -0
  45. package/dest/api/events.d.ts.map +1 -0
  46. package/dest/api/events.js +59 -0
  47. package/dest/api/fee.d.ts +6 -0
  48. package/dest/api/fee.d.ts.map +1 -0
  49. package/dest/api/fee.js +4 -0
  50. package/dest/api/fee_testing.d.ts +2 -0
  51. package/dest/api/fee_testing.d.ts.map +1 -0
  52. package/dest/api/fee_testing.js +1 -0
  53. package/dest/api/fields.d.ts +4 -0
  54. package/dest/api/fields.d.ts.map +1 -0
  55. package/dest/api/fields.js +3 -0
  56. package/dest/api/keys.d.ts +4 -0
  57. package/dest/api/keys.d.ts.map +1 -0
  58. package/dest/api/keys.js +3 -0
  59. package/dest/api/log.d.ts +3 -0
  60. package/dest/api/log.d.ts.map +1 -0
  61. package/dest/api/log.js +2 -0
  62. package/dest/api/messaging.d.ts +3 -0
  63. package/dest/api/messaging.d.ts.map +1 -0
  64. package/dest/api/messaging.js +2 -0
  65. package/dest/api/node.d.ts +25 -0
  66. package/dest/api/node.d.ts.map +1 -0
  67. package/dest/api/node.js +22 -0
  68. package/dest/api/note.d.ts +2 -0
  69. package/dest/api/note.d.ts.map +1 -0
  70. package/dest/api/note.js +1 -0
  71. package/dest/api/protocol.d.ts +6 -0
  72. package/dest/api/protocol.d.ts.map +1 -0
  73. package/dest/api/protocol.js +5 -0
  74. package/dest/api/trees.d.ts +3 -0
  75. package/dest/api/trees.d.ts.map +1 -0
  76. package/dest/api/trees.js +2 -0
  77. package/dest/api/tx.d.ts +3 -0
  78. package/dest/api/tx.d.ts.map +1 -0
  79. package/dest/api/tx.js +1 -0
  80. package/dest/api/utils.d.ts +13 -0
  81. package/dest/api/utils.d.ts.map +1 -0
  82. package/dest/api/utils.js +12 -0
  83. package/dest/api/wallet.d.ts +8 -0
  84. package/dest/api/wallet.d.ts.map +1 -0
  85. package/dest/api/wallet.js +7 -0
  86. package/dest/authorization/call_authorization_request.d.ts +46 -0
  87. package/dest/authorization/call_authorization_request.d.ts.map +1 -0
  88. package/dest/authorization/call_authorization_request.js +72 -0
  89. package/dest/contract/base_contract_interaction.d.ts +32 -0
  90. package/dest/contract/base_contract_interaction.d.ts.map +1 -0
  91. package/dest/contract/base_contract_interaction.js +24 -0
  92. package/dest/contract/batch_call.d.ts +27 -0
  93. package/dest/contract/batch_call.d.ts.map +1 -0
  94. package/dest/contract/batch_call.js +152 -0
  95. package/dest/contract/checker.d.ts +11 -0
  96. package/dest/contract/checker.d.ts.map +1 -0
  97. package/dest/contract/checker.js +107 -0
  98. package/dest/contract/contract.d.ts +33 -0
  99. package/dest/contract/contract.d.ts.map +1 -0
  100. package/dest/contract/contract.js +36 -0
  101. package/dest/contract/contract_base.d.ts +51 -0
  102. package/dest/contract/contract_base.d.ts.map +1 -0
  103. package/dest/contract/contract_base.js +38 -0
  104. package/dest/contract/contract_function_interaction.d.ts +67 -0
  105. package/dest/contract/contract_function_interaction.d.ts.map +1 -0
  106. package/dest/contract/contract_function_interaction.js +218 -0
  107. package/dest/contract/deploy_method.d.ts +433 -0
  108. package/dest/contract/deploy_method.d.ts.map +1 -0
  109. package/dest/contract/deploy_method.js +471 -0
  110. package/dest/contract/fastforward_contract_update.d.ts +25 -0
  111. package/dest/contract/fastforward_contract_update.d.ts.map +1 -0
  112. package/dest/contract/fastforward_contract_update.js +57 -0
  113. package/dest/contract/interaction_options.d.ts +213 -0
  114. package/dest/contract/interaction_options.d.ts.map +1 -0
  115. package/dest/contract/interaction_options.js +87 -0
  116. package/dest/contract/protocol_contracts/contract-class-registry.d.ts +18 -0
  117. package/dest/contract/protocol_contracts/contract-class-registry.d.ts.map +1 -0
  118. package/dest/contract/protocol_contracts/contract-class-registry.js +137 -0
  119. package/dest/contract/protocol_contracts/contract-instance-registry.d.ts +25 -0
  120. package/dest/contract/protocol_contracts/contract-instance-registry.d.ts.map +1 -0
  121. package/dest/contract/protocol_contracts/contract-instance-registry.js +366 -0
  122. package/dest/contract/protocol_contracts/fee-juice.d.ts +24 -0
  123. package/dest/contract/protocol_contracts/fee-juice.d.ts.map +1 -0
  124. package/dest/contract/protocol_contracts/fee-juice.js +383 -0
  125. package/dest/contract/wait_for_proven.d.ts +18 -0
  126. package/dest/contract/wait_for_proven.d.ts.map +1 -0
  127. package/dest/contract/wait_for_proven.js +17 -0
  128. package/dest/contract/wait_opts.d.ts +21 -0
  129. package/dest/contract/wait_opts.d.ts.map +1 -0
  130. package/dest/contract/wait_opts.js +5 -0
  131. package/dest/deployment/contract_deployer.d.ts +28 -0
  132. package/dest/deployment/contract_deployer.d.ts.map +1 -0
  133. package/dest/deployment/contract_deployer.js +35 -0
  134. package/dest/deployment/publish_class.d.ts +6 -0
  135. package/dest/deployment/publish_class.d.ts.map +1 -0
  136. package/dest/deployment/publish_class.js +17 -0
  137. package/dest/deployment/publish_instance.d.ts +10 -0
  138. package/dest/deployment/publish_instance.d.ts.map +1 -0
  139. package/dest/deployment/publish_instance.js +11 -0
  140. package/dest/ethereum/portal_manager.d.ts +146 -0
  141. package/dest/ethereum/portal_manager.d.ts.map +1 -0
  142. package/dest/ethereum/portal_manager.js +415 -0
  143. package/dest/fee/fee_juice_payment_method_with_claim.d.ts +23 -0
  144. package/dest/fee/fee_juice_payment_method_with_claim.d.ts.map +1 -0
  145. package/dest/fee/fee_juice_payment_method_with_claim.js +46 -0
  146. package/dest/fee/fee_payment_method.d.ts +26 -0
  147. package/dest/fee/fee_payment_method.d.ts.map +1 -0
  148. package/dest/fee/fee_payment_method.js +3 -0
  149. package/dest/fee/private_fee_payment_method.d.ts +69 -0
  150. package/dest/fee/private_fee_payment_method.d.ts.map +1 -0
  151. package/dest/fee/private_fee_payment_method.js +122 -0
  152. package/dest/fee/public_fee_payment_method.d.ts +59 -0
  153. package/dest/fee/public_fee_payment_method.d.ts.map +1 -0
  154. package/dest/fee/public_fee_payment_method.js +116 -0
  155. package/dest/fee/sponsored_fee_payment.d.ts +17 -0
  156. package/dest/fee/sponsored_fee_payment.d.ts.map +1 -0
  157. package/dest/fee/sponsored_fee_payment.js +33 -0
  158. package/dest/scripts/generate_protocol_contract_types.d.ts +2 -0
  159. package/dest/scripts/generate_protocol_contract_types.d.ts.map +1 -0
  160. package/dest/scripts/generate_protocol_contract_types.js +128 -0
  161. package/dest/test/fixtures.d.ts +13 -0
  162. package/dest/test/fixtures.d.ts.map +1 -0
  163. package/dest/test/fixtures.js +185 -0
  164. package/dest/utils/abi_types.d.ts +32 -0
  165. package/dest/utils/abi_types.d.ts.map +1 -0
  166. package/dest/utils/abi_types.js +1 -0
  167. package/dest/utils/authwit.d.ts +119 -0
  168. package/dest/utils/authwit.d.ts.map +1 -0
  169. package/dest/utils/authwit.js +266 -0
  170. package/dest/utils/cross_chain.d.ts +28 -0
  171. package/dest/utils/cross_chain.d.ts.map +1 -0
  172. package/dest/utils/cross_chain.js +26 -0
  173. package/dest/utils/fee_juice.d.ts +8 -0
  174. package/dest/utils/fee_juice.d.ts.map +1 -0
  175. package/dest/utils/fee_juice.js +10 -0
  176. package/dest/utils/field_compressed_string.d.ts +17 -0
  177. package/dest/utils/field_compressed_string.d.ts.map +1 -0
  178. package/dest/utils/field_compressed_string.js +15 -0
  179. package/dest/utils/node.d.ts +24 -0
  180. package/dest/utils/node.d.ts.map +1 -0
  181. package/dest/utils/node.js +80 -0
  182. package/dest/utils/pub_key.d.ts +9 -0
  183. package/dest/utils/pub_key.d.ts.map +1 -0
  184. package/dest/utils/pub_key.js +8 -0
  185. package/dest/wallet/account_entrypoint_meta_payment_method.d.ts +33 -0
  186. package/dest/wallet/account_entrypoint_meta_payment_method.d.ts.map +1 -0
  187. package/dest/wallet/account_entrypoint_meta_payment_method.js +63 -0
  188. package/dest/wallet/account_manager.d.ts +73 -0
  189. package/dest/wallet/account_manager.d.ts.map +1 -0
  190. package/dest/wallet/account_manager.js +102 -0
  191. package/dest/wallet/capabilities.d.ts +456 -0
  192. package/dest/wallet/capabilities.d.ts.map +1 -0
  193. package/dest/wallet/capabilities.js +3 -0
  194. package/dest/wallet/deploy_account_method.d.ts +97 -0
  195. package/dest/wallet/deploy_account_method.d.ts.map +1 -0
  196. package/dest/wallet/deploy_account_method.js +127 -0
  197. package/dest/wallet/index.d.ts +5 -0
  198. package/dest/wallet/index.d.ts.map +1 -0
  199. package/dest/wallet/index.js +4 -0
  200. package/dest/wallet/tx_simulation_result_with_app_offset.d.ts +40 -0
  201. package/dest/wallet/tx_simulation_result_with_app_offset.d.ts.map +1 -0
  202. package/dest/wallet/tx_simulation_result_with_app_offset.js +65 -0
  203. package/dest/wallet/wallet.d.ts +1451 -0
  204. package/dest/wallet/wallet.d.ts.map +1 -0
  205. package/dest/wallet/wallet.js +430 -0
  206. package/package.json +138 -0
  207. package/src/account/account.ts +72 -0
  208. package/src/account/account_contract.ts +80 -0
  209. package/src/account/index.ts +12 -0
  210. package/src/api/README.md +7 -0
  211. package/src/api/abi.ts +42 -0
  212. package/src/api/account.ts +12 -0
  213. package/src/api/addresses.ts +3 -0
  214. package/src/api/authorization.ts +15 -0
  215. package/src/api/block.ts +2 -0
  216. package/src/api/contract.ts +86 -0
  217. package/src/api/crypto.ts +1 -0
  218. package/src/api/deployment.ts +3 -0
  219. package/src/api/eth_address.ts +1 -0
  220. package/src/api/ethereum.ts +10 -0
  221. package/src/api/event_cursor.ts +60 -0
  222. package/src/api/events.ts +79 -0
  223. package/src/api/fee.ts +5 -0
  224. package/src/api/fee_testing.ts +1 -0
  225. package/src/api/fields.ts +3 -0
  226. package/src/api/keys.ts +8 -0
  227. package/src/api/log.ts +2 -0
  228. package/src/api/messaging.ts +2 -0
  229. package/src/api/node.ts +30 -0
  230. package/src/api/note.ts +1 -0
  231. package/src/api/protocol.ts +6 -0
  232. package/src/api/trees.ts +2 -0
  233. package/src/api/tx.ts +20 -0
  234. package/src/api/utils.ts +14 -0
  235. package/src/api/wallet.ts +83 -0
  236. package/src/authorization/call_authorization_request.ts +93 -0
  237. package/src/contract/base_contract_interaction.ts +62 -0
  238. package/src/contract/batch_call.ts +188 -0
  239. package/src/contract/checker.ts +122 -0
  240. package/src/contract/contract.ts +52 -0
  241. package/src/contract/contract_base.ts +73 -0
  242. package/src/contract/contract_function_interaction.ts +297 -0
  243. package/src/contract/deploy_method.ts +877 -0
  244. package/src/contract/fastforward_contract_update.ts +71 -0
  245. package/src/contract/interaction_options.ts +301 -0
  246. package/src/contract/protocol_contracts/contract-class-registry.ts +104 -0
  247. package/src/contract/protocol_contracts/contract-instance-registry.ts +241 -0
  248. package/src/contract/protocol_contracts/fee-juice.ts +257 -0
  249. package/src/contract/wait_for_proven.ts +43 -0
  250. package/src/contract/wait_opts.ts +26 -0
  251. package/src/deployment/contract_deployer.ts +40 -0
  252. package/src/deployment/publish_class.ts +34 -0
  253. package/src/deployment/publish_instance.ts +24 -0
  254. package/src/ethereum/portal_manager.ts +539 -0
  255. package/src/fee/fee_juice_payment_method_with_claim.ts +63 -0
  256. package/src/fee/fee_payment_method.ts +26 -0
  257. package/src/fee/private_fee_payment_method.ts +145 -0
  258. package/src/fee/public_fee_payment_method.ts +145 -0
  259. package/src/fee/sponsored_fee_payment.ts +45 -0
  260. package/src/scripts/generate_protocol_contract_types.ts +160 -0
  261. package/src/test/fixtures.ts +126 -0
  262. package/src/utils/abi_types.ts +32 -0
  263. package/src/utils/authwit.ts +327 -0
  264. package/src/utils/cross_chain.ts +55 -0
  265. package/src/utils/fee_juice.ts +14 -0
  266. package/src/utils/field_compressed_string.ts +27 -0
  267. package/src/utils/node.ts +110 -0
  268. package/src/utils/pub_key.ts +12 -0
  269. package/src/wallet/account_entrypoint_meta_payment_method.ts +76 -0
  270. package/src/wallet/account_manager.ts +164 -0
  271. package/src/wallet/capabilities.ts +505 -0
  272. package/src/wallet/deploy_account_method.ts +234 -0
  273. package/src/wallet/index.ts +4 -0
  274. package/src/wallet/tx_simulation_result_with_app_offset.ts +100 -0
  275. package/src/wallet/wallet.ts +668 -0
@@ -0,0 +1,297 @@
1
+ import {
2
+ type ABIParameter,
3
+ type AbiType,
4
+ type FunctionAbi,
5
+ FunctionCall,
6
+ FunctionSelector,
7
+ FunctionType,
8
+ canBeMappedFromNullOrUndefined,
9
+ decodeFromAbi,
10
+ encodeArguments,
11
+ getFunctionReturnType,
12
+ isOptionStruct,
13
+ } from '@aztec-labs/stdlib/abi';
14
+ import type { AuthWitness } from '@aztec-labs/stdlib/auth-witness';
15
+ import { AztecAddress } from '@aztec-labs/stdlib/aztec-address';
16
+ import type { Capsule, HashedValues, TxProfileResult } from '@aztec-labs/stdlib/tx';
17
+ import { ExecutionPayload, mergeExecutionPayloads } from '@aztec-labs/stdlib/tx';
18
+
19
+ import type { Wallet } from '../wallet/wallet.js';
20
+ import { BaseContractInteraction } from './base_contract_interaction.js';
21
+ import {
22
+ NO_FROM,
23
+ type ProfileInteractionOptions,
24
+ type RequestInteractionOptions,
25
+ type SimulateInteractionOptions,
26
+ type SimulationResult,
27
+ extractOffchainOutput,
28
+ toProfileOptions,
29
+ toSimulateOptions,
30
+ } from './interaction_options.js';
31
+
32
+ /**
33
+ * This is the class that is returned when calling e.g. `contract.methods.myMethod(arg0, arg1)`.
34
+ * It contains available interactions one can call on a method, including view.
35
+ */
36
+ export class ContractFunctionInteraction extends BaseContractInteraction {
37
+ constructor(
38
+ wallet: Wallet,
39
+ protected contractAddress: AztecAddress,
40
+ protected functionDao: FunctionAbi,
41
+ protected args: any[],
42
+ authWitnesses: AuthWitness[] = [],
43
+ capsules: Capsule[] = [],
44
+ private extraHashedArgs: HashedValues[] = [],
45
+ ) {
46
+ super(wallet, authWitnesses, capsules);
47
+ // This may feel a bit ad-hoc here, so it warrants a comment. We accept Noir Option<T> parameters, and it's natural
48
+ // to map JS's null/undefined to Noir Option's None. One possible way to deal with null/undefined arguments at this
49
+ // point in the codebase is to conclude that they are accepted since at least one Noir type (ie: Option) can be
50
+ // encoded from them. Then we would let `encode` deal with potential mismatches. I chose not to do that because of
51
+ // the pervasiveness of null/undefined in JS, and how easy it is to inadvertently pass it around. Having this check
52
+ // here allows us to fail at a point where the boundaries and intent are clear.
53
+ if (this.hasInvalidNullOrUndefinedArguments(args)) {
54
+ const signature = formatFunctionSignature(this.functionDao.name, this.functionDao.parameters);
55
+ const received = args.map(formatArg).join(', ');
56
+ throw new Error(
57
+ `Null or undefined arguments are only allowed for Option<T> parameters in ${signature}. Received: (${received}).`,
58
+ );
59
+ }
60
+ }
61
+
62
+ private hasInvalidNullOrUndefinedArguments(args: any[]) {
63
+ return args.some((arg, index) => {
64
+ if (arg !== undefined && arg !== null) {
65
+ return false;
66
+ }
67
+
68
+ const parameterType = this.functionDao.parameters[index]?.type;
69
+ return !parameterType || !canBeMappedFromNullOrUndefined(parameterType);
70
+ });
71
+ }
72
+
73
+ /**
74
+ * Returns the encoded function call wrapped by this interaction
75
+ * Useful when generating authwits
76
+ * @returns An encoded function call
77
+ */
78
+ public async getFunctionCall(): Promise<FunctionCall> {
79
+ const args = encodeArguments(this.functionDao, this.args);
80
+ return FunctionCall.from({
81
+ name: this.functionDao.name,
82
+ to: this.contractAddress,
83
+ selector: await FunctionSelector.fromNameAndParameters(this.functionDao.name, this.functionDao.parameters),
84
+ type: this.functionDao.functionType,
85
+ hideMsgSender: false /** Only set to `true` for enqueued public function calls */,
86
+ isStatic: this.functionDao.isStatic,
87
+ args,
88
+ returnType: getFunctionReturnType(this.functionDao),
89
+ });
90
+ }
91
+
92
+ /**
93
+ * Returns the execution payload that allows this operation to happen on chain.
94
+ * @param options - Configuration options.
95
+ * @returns The execution payload for this operation
96
+ */
97
+ public override async request(options: RequestInteractionOptions = {}): Promise<ExecutionPayload> {
98
+ const calls = [await this.getFunctionCall()];
99
+ const { authWitnesses, capsules } = options;
100
+ const feeExecutionPayload = options.fee?.paymentMethod
101
+ ? await options.fee.paymentMethod.getExecutionPayload()
102
+ : undefined;
103
+ const functionExecutionPayload = new ExecutionPayload(
104
+ calls,
105
+ this.authWitnesses.concat(authWitnesses ?? []),
106
+ this.capsules.concat(capsules ?? []),
107
+ this.extraHashedArgs,
108
+ );
109
+ const finalExecutionPayload = feeExecutionPayload
110
+ ? mergeExecutionPayloads([feeExecutionPayload, functionExecutionPayload])
111
+ : functionExecutionPayload;
112
+ return finalExecutionPayload;
113
+ }
114
+
115
+ // docs:start:simulate
116
+ /**
117
+ * Simulate a transaction and get information from its execution.
118
+ * Differs from prove in a few important ways:
119
+ * 1. It returns the values of the function execution, plus additional metadata if requested
120
+ * 2. It supports `utility`, `private` and `public` functions
121
+ *
122
+ * @param options - An optional object containing additional configuration for the simulation.
123
+ * @returns Depending on the simulation options, this method directly returns the result value of the executed
124
+ * function or a rich object containing extra metadata, such as estimated gas costs (if requested via options),
125
+ * execution statistics and emitted offchain effects
126
+ */
127
+ public async simulate(
128
+ options: SimulateInteractionOptions = {} as SimulateInteractionOptions,
129
+ ): Promise<SimulationResult> {
130
+ // docs:end:simulate
131
+ if (this.functionDao.functionType == FunctionType.UTILITY) {
132
+ if (options.overrides?.publicStorage?.length || options.overrides?.contracts) {
133
+ throw new Error('overrides are not supported for utility function simulation.');
134
+ }
135
+ const call = await this.getFunctionCall();
136
+ const scopes = [...(options.additionalScopes ?? [])];
137
+ const utilityResult = await this.wallet.executeUtility(call, {
138
+ scopes: options.from === NO_FROM ? scopes : [options.from, ...scopes],
139
+ authWitnesses: options.authWitnesses,
140
+ });
141
+
142
+ // Decode the raw field elements to the actual return type
143
+ const returnValue = utilityResult.result
144
+ ? decodeFromAbi(getFunctionReturnType(this.functionDao), utilityResult.result)
145
+ : undefined;
146
+ const offchainOutput = extractOffchainOutput(utilityResult.offchainEffects, utilityResult.anchorBlockTimestamp);
147
+
148
+ if (options.includeMetadata) {
149
+ return {
150
+ stats: utilityResult.stats,
151
+ ...offchainOutput,
152
+ result: returnValue,
153
+ };
154
+ }
155
+ return { result: returnValue, ...offchainOutput };
156
+ }
157
+
158
+ const executionPayload = await this.request(options);
159
+ const simulatedTx = await this.wallet.simulateTx(executionPayload, toSimulateOptions(options));
160
+
161
+ let rawReturnValues;
162
+ if (this.functionDao.functionType == FunctionType.PRIVATE) {
163
+ // request() prepends the fee payment method's calls (if any) before this interaction's single call, so the app
164
+ // call is the last call of its type in the payload. Its position among the private return values is the number
165
+ // of private calls that precede it.
166
+ const appCallIndex = executionPayload.calls.filter(c => c.type === FunctionType.PRIVATE).length - 1;
167
+ rawReturnValues = simulatedTx.getPrivateReturnValuesOfAppCall(appCallIndex)?.values;
168
+ } else {
169
+ // For public functions we retrieve the values directly from the public output, offset by any public fee calls
170
+ // that request() prepended ahead of the app call.
171
+ const appCallIndex = executionPayload.calls.filter(c => c.type === FunctionType.PUBLIC).length - 1;
172
+ rawReturnValues = simulatedTx.getPublicReturnValues()?.[appCallIndex]?.values;
173
+ }
174
+
175
+ const returnValue = rawReturnValues
176
+ ? decodeFromAbi(getFunctionReturnType(this.functionDao), rawReturnValues)
177
+ : undefined;
178
+ const offchainOutput = extractOffchainOutput(
179
+ simulatedTx.offchainEffects,
180
+ simulatedTx.publicInputs.constants.anchorBlockHeader.globalVariables.timestamp,
181
+ );
182
+
183
+ if (options.includeMetadata) {
184
+ return {
185
+ stats: simulatedTx.stats,
186
+ ...offchainOutput,
187
+ result: returnValue,
188
+ gasUsed: simulatedTx.gasUsed,
189
+ };
190
+ }
191
+ return { result: returnValue, ...offchainOutput };
192
+ }
193
+
194
+ /**
195
+ * Simulate a transaction and profile the gate count for each function in the transaction.
196
+ * @param options - Same options as `simulate`, plus profiling method
197
+ *
198
+ * @returns An object containing the function return value and profile result.
199
+ */
200
+ public async profile(options: ProfileInteractionOptions): Promise<TxProfileResult> {
201
+ if (this.functionDao.functionType == FunctionType.UTILITY) {
202
+ throw new Error("Can't profile a utility function.");
203
+ }
204
+
205
+ const executionPayload = await this.request(options);
206
+ return await this.wallet.profileTx(executionPayload, toProfileOptions(options));
207
+ }
208
+
209
+ /**
210
+ * Augments this ContractFunctionInteraction with additional metadata, such as authWitnesses, capsules, and extraHashedArgs.
211
+ * This is useful when creating a "batteries included" interaction, such as registering a contract class with its associated
212
+ * capsule instead of having the user provide them externally.
213
+ * @param options - An object containing the metadata to add to the interaction
214
+ * @returns A new ContractFunctionInteraction with the added metadata, but calling the same original function in the same manner
215
+ */
216
+ public with({
217
+ authWitnesses = [],
218
+ capsules = [],
219
+ extraHashedArgs = [],
220
+ }: {
221
+ /** The authWitnesses to add to the interaction */
222
+ authWitnesses?: AuthWitness[];
223
+ /** The capsules to add to the interaction */
224
+ capsules?: Capsule[];
225
+ /** The extra hashed args to add to the interaction */
226
+ extraHashedArgs?: HashedValues[];
227
+ }): ContractFunctionInteraction {
228
+ return new ContractFunctionInteraction(
229
+ this.wallet,
230
+ this.contractAddress,
231
+ this.functionDao,
232
+ this.args,
233
+ this.authWitnesses.concat(authWitnesses),
234
+ this.capsules.concat(capsules),
235
+ this.extraHashedArgs.concat(extraHashedArgs),
236
+ );
237
+ }
238
+ }
239
+
240
+ /**
241
+ * Render an AbiType as a human readable string
242
+ * */
243
+ function formatAbiType(abiType: AbiType): string {
244
+ switch (abiType.kind) {
245
+ case 'field':
246
+ return 'Field';
247
+ case 'boolean':
248
+ return 'bool';
249
+ case 'integer':
250
+ return `${abiType.sign === 'signed' ? 'i' : 'u'}${abiType.width}`;
251
+ case 'string':
252
+ return `str<${abiType.length}>`;
253
+ case 'array':
254
+ return `[${formatAbiType(abiType.type)}; ${abiType.length}]`;
255
+ case 'struct': {
256
+ if (isOptionStruct(abiType)) {
257
+ const innerType = abiType.fields.find(f => f.name === '_value')!.type;
258
+ return `Option<${formatAbiType(innerType)}>`;
259
+ }
260
+ return `(${abiType.fields.map(f => `${f.name}: ${formatAbiType(f.type)}`).join(', ')})`;
261
+ }
262
+ case 'tuple':
263
+ return `(${abiType.fields.map(formatAbiType).join(', ')})`;
264
+ }
265
+ }
266
+
267
+ /**
268
+ * Pretty print a function signature
269
+ */
270
+ function formatFunctionSignature(name: string, parameters: ABIParameter[]): string {
271
+ const params = parameters.map(p => `${p.name}: ${formatAbiType(p.type)}`).join(', ');
272
+ return `${name}(${params})`;
273
+ }
274
+
275
+ /**
276
+ * Non-exhaustive pretty print of JS args to display in error messages in this module
277
+ */
278
+ function formatArg(arg: unknown): string {
279
+ if (arg === undefined) {
280
+ return 'undefined';
281
+ }
282
+ if (arg === null) {
283
+ return 'null';
284
+ }
285
+ if (typeof arg === 'bigint') {
286
+ return `${arg}n`;
287
+ }
288
+ if (Array.isArray(arg)) {
289
+ return `[${arg.map(formatArg).join(', ')}]`;
290
+ }
291
+ if (typeof arg === 'object') {
292
+ const entries = Object.entries(arg).map(([k, v]) => `${k}: ${formatArg(v)}`);
293
+ return `{ ${entries.join(', ')} }`;
294
+ }
295
+ // eslint-disable-next-line @typescript-eslint/no-base-to-string
296
+ return String(arg);
297
+ }