@aztec/aztec.js 0.0.0-test.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.
Files changed (299) hide show
  1. package/README.md +44 -0
  2. package/dest/account/contract.d.ts +37 -0
  3. package/dest/account/contract.d.ts.map +1 -0
  4. package/dest/account/contract.js +16 -0
  5. package/dest/account/index.d.ts +15 -0
  6. package/dest/account/index.d.ts.map +1 -0
  7. package/dest/account/index.js +9 -0
  8. package/dest/account/interface.d.ts +31 -0
  9. package/dest/account/interface.d.ts.map +1 -0
  10. package/dest/account/interface.js +4 -0
  11. package/dest/account/wallet.d.ts +11 -0
  12. package/dest/account/wallet.d.ts.map +1 -0
  13. package/dest/account/wallet.js +3 -0
  14. package/dest/account_manager/deploy_account_method.d.ts +15 -0
  15. package/dest/account_manager/deploy_account_method.d.ts.map +1 -0
  16. package/dest/account_manager/deploy_account_method.js +43 -0
  17. package/dest/account_manager/deploy_account_sent_tx.d.ts +30 -0
  18. package/dest/account_manager/deploy_account_sent_tx.d.ts.map +1 -0
  19. package/dest/account_manager/deploy_account_sent_tx.js +29 -0
  20. package/dest/account_manager/index.d.ts +108 -0
  21. package/dest/account_manager/index.d.ts.map +1 -0
  22. package/dest/account_manager/index.js +159 -0
  23. package/dest/api/abi.d.ts +3 -0
  24. package/dest/api/abi.d.ts.map +1 -0
  25. package/dest/api/abi.js +1 -0
  26. package/dest/api/account.d.ts +3 -0
  27. package/dest/api/account.d.ts.map +1 -0
  28. package/dest/api/account.js +2 -0
  29. package/dest/api/addresses.d.ts +3 -0
  30. package/dest/api/addresses.d.ts.map +1 -0
  31. package/dest/api/addresses.js +2 -0
  32. package/dest/api/cheat_codes.d.ts +26 -0
  33. package/dest/api/cheat_codes.d.ts.map +1 -0
  34. package/dest/api/cheat_codes.js +25 -0
  35. package/dest/api/deployment.d.ts +4 -0
  36. package/dest/api/deployment.d.ts.map +1 -0
  37. package/dest/api/deployment.js +3 -0
  38. package/dest/api/entrypoint.d.ts +2 -0
  39. package/dest/api/entrypoint.d.ts.map +1 -0
  40. package/dest/api/entrypoint.js +1 -0
  41. package/dest/api/eth_address.d.ts +2 -0
  42. package/dest/api/eth_address.d.ts.map +1 -0
  43. package/dest/api/eth_address.js +1 -0
  44. package/dest/api/ethereum/anvil_test_watcher.d.ts +32 -0
  45. package/dest/api/ethereum/anvil_test_watcher.d.ts.map +1 -0
  46. package/dest/api/ethereum/anvil_test_watcher.js +133 -0
  47. package/dest/api/ethereum/chain_monitor.d.ts +25 -0
  48. package/dest/api/ethereum/chain_monitor.d.ts.map +1 -0
  49. package/dest/api/ethereum/chain_monitor.js +74 -0
  50. package/dest/api/ethereum/cheat_codes.d.ts +63 -0
  51. package/dest/api/ethereum/cheat_codes.d.ts.map +1 -0
  52. package/dest/api/ethereum/cheat_codes.js +162 -0
  53. package/dest/api/ethereum/index.d.ts +7 -0
  54. package/dest/api/ethereum/index.d.ts.map +1 -0
  55. package/dest/api/ethereum/index.js +6 -0
  56. package/dest/api/ethereum/l1_contracts.d.ts +3 -0
  57. package/dest/api/ethereum/l1_contracts.d.ts.map +1 -0
  58. package/dest/api/ethereum/l1_contracts.js +13 -0
  59. package/dest/api/ethereum/portal_manager.d.ts +136 -0
  60. package/dest/api/ethereum/portal_manager.d.ts.map +1 -0
  61. package/dest/api/ethereum/portal_manager.js +286 -0
  62. package/dest/api/fee.d.ts +6 -0
  63. package/dest/api/fee.d.ts.map +1 -0
  64. package/dest/api/fee.js +4 -0
  65. package/dest/api/fields.d.ts +2 -0
  66. package/dest/api/fields.d.ts.map +1 -0
  67. package/dest/api/fields.js +1 -0
  68. package/dest/api/interfaces/pxe.d.ts +2 -0
  69. package/dest/api/interfaces/pxe.d.ts.map +1 -0
  70. package/dest/api/interfaces/pxe.js +1 -0
  71. package/dest/api/log.d.ts +2 -0
  72. package/dest/api/log.d.ts.map +1 -0
  73. package/dest/api/log.js +1 -0
  74. package/dest/api/log_id.d.ts +2 -0
  75. package/dest/api/log_id.d.ts.map +1 -0
  76. package/dest/api/log_id.js +1 -0
  77. package/dest/api/tx_hash.d.ts +2 -0
  78. package/dest/api/tx_hash.d.ts.map +1 -0
  79. package/dest/api/tx_hash.js +1 -0
  80. package/dest/api/wallet.d.ts +2 -0
  81. package/dest/api/wallet.d.ts.map +1 -0
  82. package/dest/api/wallet.js +1 -0
  83. package/dest/contract/base_contract_interaction.d.ts +98 -0
  84. package/dest/contract/base_contract_interaction.d.ts.map +1 -0
  85. package/dest/contract/base_contract_interaction.js +149 -0
  86. package/dest/contract/batch_call.d.ts +28 -0
  87. package/dest/contract/batch_call.d.ts.map +1 -0
  88. package/dest/contract/batch_call.js +91 -0
  89. package/dest/contract/checker.d.ts +11 -0
  90. package/dest/contract/checker.d.ts.map +1 -0
  91. package/dest/contract/checker.js +107 -0
  92. package/dest/contract/contract.d.ts +41 -0
  93. package/dest/contract/contract.d.ts.map +1 -0
  94. package/dest/contract/contract.js +52 -0
  95. package/dest/contract/contract_base.d.ts +61 -0
  96. package/dest/contract/contract_base.d.ts.map +1 -0
  97. package/dest/contract/contract_base.js +45 -0
  98. package/dest/contract/contract_function_interaction.d.ts +68 -0
  99. package/dest/contract/contract_function_interaction.d.ts.map +1 -0
  100. package/dest/contract/contract_function_interaction.js +122 -0
  101. package/dest/contract/deploy_method.d.ts +113 -0
  102. package/dest/contract/deploy_method.d.ts.map +1 -0
  103. package/dest/contract/deploy_method.js +212 -0
  104. package/dest/contract/deploy_proven_tx.d.ts +21 -0
  105. package/dest/contract/deploy_proven_tx.d.ts.map +1 -0
  106. package/dest/contract/deploy_proven_tx.js +19 -0
  107. package/dest/contract/deploy_sent_tx.d.ts +45 -0
  108. package/dest/contract/deploy_sent_tx.d.ts.map +1 -0
  109. package/dest/contract/deploy_sent_tx.js +43 -0
  110. package/dest/contract/get_gas_limits.d.ts +18 -0
  111. package/dest/contract/get_gas_limits.d.ts.map +1 -0
  112. package/dest/contract/get_gas_limits.js +10 -0
  113. package/dest/contract/index.d.ts +45 -0
  114. package/dest/contract/index.d.ts.map +1 -0
  115. package/dest/contract/index.js +43 -0
  116. package/dest/contract/protocol_contracts.d.ts +9 -0
  117. package/dest/contract/protocol_contracts.d.ts.map +1 -0
  118. package/dest/contract/protocol_contracts.js +26 -0
  119. package/dest/contract/proven_tx.d.ts +17 -0
  120. package/dest/contract/proven_tx.d.ts.map +1 -0
  121. package/dest/contract/proven_tx.js +22 -0
  122. package/dest/contract/sent_tx.d.ts +61 -0
  123. package/dest/contract/sent_tx.d.ts.map +1 -0
  124. package/dest/contract/sent_tx.js +102 -0
  125. package/dest/contract/unsafe_contract.d.ts +15 -0
  126. package/dest/contract/unsafe_contract.d.ts.map +1 -0
  127. package/dest/contract/unsafe_contract.js +6 -0
  128. package/dest/deployment/broadcast_function.d.ts +24 -0
  129. package/dest/deployment/broadcast_function.d.ts.map +1 -0
  130. package/dest/deployment/broadcast_function.js +70 -0
  131. package/dest/deployment/contract_deployer.d.ts +27 -0
  132. package/dest/deployment/contract_deployer.d.ts.map +1 -0
  133. package/dest/deployment/contract_deployer.js +30 -0
  134. package/dest/deployment/deploy_instance.d.ts +10 -0
  135. package/dest/deployment/deploy_instance.d.ts.map +1 -0
  136. package/dest/deployment/deploy_instance.js +14 -0
  137. package/dest/deployment/index.d.ts +2 -0
  138. package/dest/deployment/index.d.ts.map +1 -0
  139. package/dest/deployment/index.js +1 -0
  140. package/dest/deployment/register_class.d.ts +6 -0
  141. package/dest/deployment/register_class.d.ts.map +1 -0
  142. package/dest/deployment/register_class.js +22 -0
  143. package/dest/entrypoint/default_entrypoint.d.ts +12 -0
  144. package/dest/entrypoint/default_entrypoint.d.ts.map +1 -0
  145. package/dest/entrypoint/default_entrypoint.js +28 -0
  146. package/dest/entrypoint/default_multi_call_entrypoint.d.ts +15 -0
  147. package/dest/entrypoint/default_multi_call_entrypoint.d.ts.map +1 -0
  148. package/dest/entrypoint/default_multi_call_entrypoint.js +130 -0
  149. package/dest/entrypoint/entrypoint.d.ts +35 -0
  150. package/dest/entrypoint/entrypoint.d.ts.map +1 -0
  151. package/dest/entrypoint/entrypoint.js +4 -0
  152. package/dest/entrypoint/payload.d.ts +128 -0
  153. package/dest/entrypoint/payload.d.ts.map +1 -0
  154. package/dest/entrypoint/payload.js +143 -0
  155. package/dest/fee/fee_juice_payment_method.d.ts +14 -0
  156. package/dest/fee/fee_juice_payment_method.d.ts.map +1 -0
  157. package/dest/fee/fee_juice_payment_method.js +20 -0
  158. package/dest/fee/fee_juice_payment_method_with_claim.d.ts +18 -0
  159. package/dest/fee/fee_juice_payment_method_with_claim.d.ts.map +1 -0
  160. package/dest/fee/fee_juice_payment_method_with_claim.js +37 -0
  161. package/dest/fee/fee_payment_method.d.ts +22 -0
  162. package/dest/fee/fee_payment_method.d.ts.map +1 -0
  163. package/dest/fee/fee_payment_method.js +3 -0
  164. package/dest/fee/private_fee_payment_method.d.ts +51 -0
  165. package/dest/fee/private_fee_payment_method.d.ts.map +1 -0
  166. package/dest/fee/private_fee_payment_method.js +103 -0
  167. package/dest/fee/public_fee_payment_method.d.ts +41 -0
  168. package/dest/fee/public_fee_payment_method.d.ts.map +1 -0
  169. package/dest/fee/public_fee_payment_method.js +97 -0
  170. package/dest/index.d.ts +66 -0
  171. package/dest/index.d.ts.map +1 -0
  172. package/dest/index.js +69 -0
  173. package/dest/rpc_clients/index.d.ts +3 -0
  174. package/dest/rpc_clients/index.d.ts.map +1 -0
  175. package/dest/rpc_clients/index.js +2 -0
  176. package/dest/rpc_clients/node/index.d.ts +12 -0
  177. package/dest/rpc_clients/node/index.d.ts.map +1 -0
  178. package/dest/rpc_clients/node/index.js +78 -0
  179. package/dest/rpc_clients/pxe_client.d.ts +10 -0
  180. package/dest/rpc_clients/pxe_client.d.ts.map +1 -0
  181. package/dest/rpc_clients/pxe_client.js +23 -0
  182. package/dest/utils/abi_types.d.ts +29 -0
  183. package/dest/utils/abi_types.d.ts.map +1 -0
  184. package/dest/utils/abi_types.js +1 -0
  185. package/dest/utils/authwit.d.ts +60 -0
  186. package/dest/utils/authwit.d.ts.map +1 -0
  187. package/dest/utils/authwit.js +74 -0
  188. package/dest/utils/aztec_cheatcodes.d.ts +59 -0
  189. package/dest/utils/aztec_cheatcodes.d.ts.map +1 -0
  190. package/dest/utils/aztec_cheatcodes.js +62 -0
  191. package/dest/utils/fee_juice.d.ts +7 -0
  192. package/dest/utils/fee_juice.d.ts.map +1 -0
  193. package/dest/utils/fee_juice.js +9 -0
  194. package/dest/utils/field_compressed_string.d.ts +17 -0
  195. package/dest/utils/field_compressed_string.d.ts.map +1 -0
  196. package/dest/utils/field_compressed_string.js +15 -0
  197. package/dest/utils/index.d.ts +8 -0
  198. package/dest/utils/index.d.ts.map +1 -0
  199. package/dest/utils/index.js +6 -0
  200. package/dest/utils/node.d.ts +5 -0
  201. package/dest/utils/node.d.ts.map +1 -0
  202. package/dest/utils/node.js +15 -0
  203. package/dest/utils/pub_key.d.ts +9 -0
  204. package/dest/utils/pub_key.d.ts.map +1 -0
  205. package/dest/utils/pub_key.js +9 -0
  206. package/dest/utils/pxe.d.ts +4 -0
  207. package/dest/utils/pxe.d.ts.map +1 -0
  208. package/dest/utils/pxe.js +14 -0
  209. package/dest/wallet/account_wallet.d.ts +76 -0
  210. package/dest/wallet/account_wallet.d.ts.map +1 -0
  211. package/dest/wallet/account_wallet.js +231 -0
  212. package/dest/wallet/account_wallet_with_private_key.d.ts +26 -0
  213. package/dest/wallet/account_wallet_with_private_key.d.ts.map +1 -0
  214. package/dest/wallet/account_wallet_with_private_key.js +22 -0
  215. package/dest/wallet/base_wallet.d.ts +73 -0
  216. package/dest/wallet/base_wallet.d.ts.map +1 -0
  217. package/dest/wallet/base_wallet.js +123 -0
  218. package/dest/wallet/index.d.ts +17 -0
  219. package/dest/wallet/index.d.ts.map +1 -0
  220. package/dest/wallet/index.js +20 -0
  221. package/dest/wallet/signerless_wallet.d.ts +25 -0
  222. package/dest/wallet/signerless_wallet.d.ts.map +1 -0
  223. package/dest/wallet/signerless_wallet.js +36 -0
  224. package/package.json +116 -0
  225. package/src/account/contract.ts +56 -0
  226. package/src/account/index.ts +16 -0
  227. package/src/account/interface.ts +36 -0
  228. package/src/account/wallet.ts +13 -0
  229. package/src/account_manager/deploy_account_method.ts +86 -0
  230. package/src/account_manager/deploy_account_sent_tx.ts +42 -0
  231. package/src/account_manager/index.ts +229 -0
  232. package/src/api/README.md +7 -0
  233. package/src/api/abi.ts +10 -0
  234. package/src/api/account.ts +10 -0
  235. package/src/api/addresses.ts +2 -0
  236. package/src/api/cheat_codes.ts +35 -0
  237. package/src/api/deployment.ts +3 -0
  238. package/src/api/entrypoint.ts +1 -0
  239. package/src/api/eth_address.ts +1 -0
  240. package/src/api/ethereum/anvil_test_watcher.ts +158 -0
  241. package/src/api/ethereum/chain_monitor.ts +88 -0
  242. package/src/api/ethereum/cheat_codes.ts +184 -0
  243. package/src/api/ethereum/index.ts +19 -0
  244. package/src/api/ethereum/l1_contracts.ts +21 -0
  245. package/src/api/ethereum/portal_manager.ts +410 -0
  246. package/src/api/fee.ts +5 -0
  247. package/src/api/fields.ts +1 -0
  248. package/src/api/interfaces/pxe.ts +1 -0
  249. package/src/api/log.ts +1 -0
  250. package/src/api/log_id.ts +1 -0
  251. package/src/api/tx_hash.ts +1 -0
  252. package/src/api/wallet.ts +7 -0
  253. package/src/contract/base_contract_interaction.ts +192 -0
  254. package/src/contract/batch_call.ts +95 -0
  255. package/src/contract/checker.ts +117 -0
  256. package/src/contract/contract.ts +69 -0
  257. package/src/contract/contract_base.ts +90 -0
  258. package/src/contract/contract_function_interaction.ts +173 -0
  259. package/src/contract/deploy_method.ts +271 -0
  260. package/src/contract/deploy_proven_tx.ts +34 -0
  261. package/src/contract/deploy_sent_tx.ts +74 -0
  262. package/src/contract/get_gas_limits.ts +26 -0
  263. package/src/contract/index.ts +48 -0
  264. package/src/contract/protocol_contracts.ts +35 -0
  265. package/src/contract/proven_tx.ts +42 -0
  266. package/src/contract/sent_tx.ts +141 -0
  267. package/src/contract/unsafe_contract.ts +19 -0
  268. package/src/deployment/broadcast_function.ts +148 -0
  269. package/src/deployment/contract_deployer.ts +41 -0
  270. package/src/deployment/deploy_instance.ts +31 -0
  271. package/src/deployment/index.ts +1 -0
  272. package/src/deployment/register_class.ts +44 -0
  273. package/src/entrypoint/default_entrypoint.ts +39 -0
  274. package/src/entrypoint/default_multi_call_entrypoint.ts +93 -0
  275. package/src/entrypoint/entrypoint.ts +39 -0
  276. package/src/entrypoint/payload.ts +238 -0
  277. package/src/fee/fee_juice_payment_method.ts +26 -0
  278. package/src/fee/fee_juice_payment_method_with_claim.ts +49 -0
  279. package/src/fee/fee_payment_method.ts +22 -0
  280. package/src/fee/private_fee_payment_method.ts +118 -0
  281. package/src/fee/public_fee_payment_method.ts +114 -0
  282. package/src/index.ts +106 -0
  283. package/src/rpc_clients/index.ts +2 -0
  284. package/src/rpc_clients/node/index.ts +77 -0
  285. package/src/rpc_clients/pxe_client.ts +25 -0
  286. package/src/utils/abi_types.ts +25 -0
  287. package/src/utils/authwit.ts +110 -0
  288. package/src/utils/aztec_cheatcodes.ts +77 -0
  289. package/src/utils/fee_juice.ts +14 -0
  290. package/src/utils/field_compressed_string.ts +27 -0
  291. package/src/utils/index.ts +21 -0
  292. package/src/utils/node.ts +19 -0
  293. package/src/utils/pub_key.ts +13 -0
  294. package/src/utils/pxe.ts +17 -0
  295. package/src/wallet/account_wallet.ts +242 -0
  296. package/src/wallet/account_wallet_with_private_key.ts +40 -0
  297. package/src/wallet/base_wallet.ts +198 -0
  298. package/src/wallet/index.ts +33 -0
  299. package/src/wallet/signerless_wallet.ts +52 -0
@@ -0,0 +1,95 @@
1
+ import { type FunctionCall, FunctionType, decodeFromAbi } from '@aztec/stdlib/abi';
2
+ import type { TxExecutionRequest } from '@aztec/stdlib/tx';
3
+
4
+ import type { Wallet } from '../account/index.js';
5
+ import { BaseContractInteraction, type SendMethodOptions } from './base_contract_interaction.js';
6
+ import type { SimulateMethodOptions } from './contract_function_interaction.js';
7
+
8
+ /** A batch of function calls to be sent as a single transaction through a wallet. */
9
+ export class BatchCall extends BaseContractInteraction {
10
+ constructor(wallet: Wallet, protected calls: FunctionCall[]) {
11
+ super(wallet);
12
+ }
13
+
14
+ /**
15
+ * Create a transaction execution request that represents this batch, encoded and authenticated by the
16
+ * user's wallet, ready to be simulated.
17
+ * @param opts - An optional object containing additional configuration for the transaction.
18
+ * @returns A Promise that resolves to a transaction instance.
19
+ */
20
+ public async create(opts?: SendMethodOptions): Promise<TxExecutionRequest> {
21
+ const calls = this.calls;
22
+ const capsules = this.getCapsules();
23
+ const fee = await this.getFeeOptions({ calls, capsules, ...opts });
24
+ return await this.wallet.createTxExecutionRequest({ calls, capsules, ...opts, fee });
25
+ }
26
+
27
+ /**
28
+ * Simulate a transaction and get its return values
29
+ * Differs from prove in a few important ways:
30
+ * 1. It returns the values of the function execution
31
+ * 2. It supports `unconstrained`, `private` and `public` functions
32
+ *
33
+ * @param options - An optional object containing additional configuration for the transaction.
34
+ * @returns The result of the transaction as returned by the contract function.
35
+ */
36
+ public async simulate(options: SimulateMethodOptions = {}): Promise<any> {
37
+ const { indexedCalls, unconstrained } = this.calls.reduce<{
38
+ /** Keep track of the number of private calls to retrieve the return values */
39
+ privateIndex: 0;
40
+ /** Keep track of the number of public calls to retrieve the return values */
41
+ publicIndex: 0;
42
+ /** The public and private function calls in the batch */
43
+ indexedCalls: [FunctionCall, number, number][];
44
+ /** The unconstrained function calls in the batch. */
45
+ unconstrained: [FunctionCall, number][];
46
+ }>(
47
+ (acc, current, index) => {
48
+ if (current.type === FunctionType.UNCONSTRAINED) {
49
+ acc.unconstrained.push([current, index]);
50
+ } else {
51
+ acc.indexedCalls.push([
52
+ current,
53
+ index,
54
+ current.type === FunctionType.PRIVATE ? acc.privateIndex++ : acc.publicIndex++,
55
+ ]);
56
+ }
57
+ return acc;
58
+ },
59
+ { indexedCalls: [], unconstrained: [], publicIndex: 0, privateIndex: 0 },
60
+ );
61
+
62
+ const calls = indexedCalls.map(([call]) => call);
63
+ const capsules = this.getCapsules();
64
+ const fee = await this.getFeeOptions({ calls, capsules, ...options });
65
+ const txRequest = await this.wallet.createTxExecutionRequest({ calls, capsules, ...options, fee });
66
+
67
+ const unconstrainedCalls = unconstrained.map(
68
+ async ([call, index]) =>
69
+ [await this.wallet.simulateUnconstrained(call.name, call.args, call.to, options?.from), index] as const,
70
+ );
71
+
72
+ const [unconstrainedResults, simulatedTx] = await Promise.all([
73
+ Promise.all(unconstrainedCalls),
74
+ this.wallet.simulateTx(txRequest, true, options?.from, options?.skipTxValidation),
75
+ ]);
76
+
77
+ const results: any[] = [];
78
+
79
+ unconstrainedResults.forEach(([result, index]) => {
80
+ results[index] = result;
81
+ });
82
+ indexedCalls.forEach(([call, callIndex, resultIndex]) => {
83
+ // As account entrypoints are private, for private functions we retrieve the return values from the first nested call
84
+ // since we're interested in the first set of values AFTER the account entrypoint
85
+ // For public functions we retrieve the first values directly from the public output.
86
+ const rawReturnValues =
87
+ call.type == FunctionType.PRIVATE
88
+ ? simulatedTx.getPrivateReturnValues()?.nested?.[resultIndex].values
89
+ : simulatedTx.getPublicReturnValues()?.[resultIndex].values;
90
+
91
+ results[callIndex] = rawReturnValues ? decodeFromAbi(call.returnTypes, rawReturnValues) : [];
92
+ });
93
+ return results;
94
+ }
95
+ }
@@ -0,0 +1,117 @@
1
+ import type { AbiType, BasicType, ContractArtifact, StructType } from '@aztec/stdlib/abi';
2
+
3
+ /**
4
+ * Represents a type derived from input type T with the 'kind' property removed.
5
+ * Useful when checking attributes of a specific kind and validating their types.
6
+ */
7
+ type TypeWithoutKind<T> = Omit<{ [key in keyof T]: any }, 'kind'>;
8
+
9
+ /**
10
+ * Validates the given ContractArtifact object by checking its functions and their parameters.
11
+ * Ensures that the ABI has at least one function, a constructor, valid bytecode, and correct parameter types.
12
+ * Throws an error if any inconsistency is detected during the validation process.
13
+ *
14
+ * @param artifact - The ContractArtifact object to be validated.
15
+ * @returns A boolean value indicating whether the artifact is valid or not.
16
+ */
17
+ export function abiChecker(artifact: ContractArtifact) {
18
+ if (!artifact.functions || artifact.functions.length === 0) {
19
+ throw new Error('artifact has no functions');
20
+ }
21
+
22
+ artifact.functions.forEach(func => {
23
+ if (!('name' in func && typeof func.name === 'string' && func.name.length > 0)) {
24
+ throw new Error('ABI function has no name');
25
+ }
26
+
27
+ // TODO: implement a better check for bytecode (right now only checks if it's > 0)
28
+ if (!('bytecode' in func && func.bytecode.length > 0)) {
29
+ throw new Error('ABI function parameter has incorrect bytecode');
30
+ }
31
+
32
+ func.parameters.forEach(param => {
33
+ if (!param.type) {
34
+ throw new Error('ABI function parameter has no type');
35
+ }
36
+
37
+ abiParameterTypeChecker(param.type);
38
+ });
39
+ });
40
+
41
+ // TODO: implement a better check for constructor (right now only checks if it has it or not)
42
+ if (!artifact.functions.find(func => func.name === 'constructor')) {
43
+ throw new Error('ABI has no constructor');
44
+ }
45
+
46
+ return true;
47
+ }
48
+
49
+ /**
50
+ * Validates the ABI function parameter's type by checking its kind and attributes.
51
+ * Throws an error if the type has an unrecognized kind or incorrectly formed attributes.
52
+ * Additionally, checks nested types for array and struct kinds.
53
+ *
54
+ * @param type - The AbiType object representing the type of the ABI function parameter.
55
+ * @returns A boolean value indicating whether the type is valid or not.
56
+ */
57
+ function abiParameterTypeChecker(type: AbiType): boolean {
58
+ switch (type.kind) {
59
+ case 'field':
60
+ case 'boolean':
61
+ return checkAttributes(type, {});
62
+ case 'integer':
63
+ return checkAttributes(type, { sign: 'string', width: 'number' });
64
+ case 'string':
65
+ return checkAttributes(type, { length: 'number' });
66
+ case 'array':
67
+ return checkAttributes(type, { length: 'number', type: 'object' }) && abiParameterTypeChecker(type.type);
68
+ case 'struct':
69
+ return checkAttributes(type, { fields: 'object', path: 'string' }) && checkStruct(type);
70
+ default:
71
+ throw new Error('ABI function parameter has an unrecognized type');
72
+ }
73
+ }
74
+
75
+ /**
76
+ * Check if the structure of the AbiType 'struct' is valid by ensuring field names are strings
77
+ * and their type attribute passes the abiParameterTypeChecker. Returns true on successful validation,
78
+ * otherwise throws an error providing insight into the incorrect formation in the struct.
79
+ *
80
+ * @param type - The StructType object containing an array of fields to validate.
81
+ * @returns A boolean value indicating successful validation of the struct's fields.
82
+ */
83
+ function checkStruct(type: StructType) {
84
+ return type.fields.reduce((acc, field) => {
85
+ if (!('name' in field && typeof field.name === 'string')) {
86
+ throw new Error('ABI function parameter has an incorrectly formed struct');
87
+ }
88
+ return acc && abiParameterTypeChecker(field.type);
89
+ }, true);
90
+ }
91
+
92
+ /**
93
+ * Check if a provided ABI type has the correct attributes and their associated types.
94
+ * This function compares the given 'type' object's keys with the expected attribute types
95
+ * specified in 'incompleteAttributes', as well as the required 'kind' property.
96
+ * Throws an error if there are any unrecognized attributes or incorrect attribute types.
97
+ *
98
+ * @param type - The ABI type object to be checked for correct attributes.
99
+ * @param incompleteAttributes - An object representing the expected attribute types without the 'kind' property.
100
+ * @returns Returns true if the provided ABI type has the correct attributes and their associated types, otherwise throws an error.
101
+ */
102
+ function checkAttributes<T extends BasicType<string>>(type: T, incompleteAttributes: TypeWithoutKind<T>) {
103
+ const typeKeys = Object.keys(type);
104
+ const attributes = { ...incompleteAttributes, kind: 'string' };
105
+
106
+ if (typeKeys.length !== Object.keys(attributes).length) {
107
+ throw new Error(`Unrecognized attribute on type ${type.kind}`);
108
+ }
109
+
110
+ typeKeys.forEach(element => {
111
+ if (!(element in type && typeof (type as any)[element] === (attributes as any)[element])) {
112
+ throw new Error(`ABI function parameter has an incorrectly formed ${type.kind}`);
113
+ }
114
+ });
115
+
116
+ return true;
117
+ }
@@ -0,0 +1,69 @@
1
+ import type { ContractArtifact } from '@aztec/stdlib/abi';
2
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
3
+ import { getContractClassFromArtifact } from '@aztec/stdlib/contract';
4
+ import { PublicKeys } from '@aztec/stdlib/keys';
5
+
6
+ import type { Wallet } from '../account/index.js';
7
+ import { ContractBase } from './contract_base.js';
8
+ import { DeployMethod } from './deploy_method.js';
9
+
10
+ /**
11
+ * The Contract class represents a contract and provides utility methods for interacting with it.
12
+ * It enables the creation of ContractFunctionInteraction instances for each function in the contract's ABI,
13
+ * allowing users to call or send transactions to these functions. Additionally, the Contract class can be used
14
+ * to attach the contract instance to a deployed contract on-chain through the PXE, which facilitates
15
+ * interaction with Aztec's privacy protocol.
16
+ */
17
+ export class Contract extends ContractBase {
18
+ /**
19
+ * Creates a contract instance.
20
+ * @param address - The deployed contract's address.
21
+ * @param artifact - Build artifact of the contract.
22
+ * @param wallet - The wallet to use when interacting with the contract.
23
+ * @returns A promise that resolves to a new Contract instance.
24
+ */
25
+ public static async at(address: AztecAddress, artifact: ContractArtifact, wallet: Wallet): Promise<Contract> {
26
+ const instance = (await wallet.getContractMetadata(address)).contractInstance;
27
+ if (instance === undefined) {
28
+ throw new Error(`Contract instance at ${address.toString()} has not been registered in the wallet's PXE`);
29
+ }
30
+ const thisContractClass = await getContractClassFromArtifact(artifact);
31
+ if (!thisContractClass.id.equals(instance.currentContractClassId)) {
32
+ // wallet holds an outdated version of this contract
33
+ await wallet.updateContract(address, artifact);
34
+ instance.currentContractClassId = thisContractClass.id;
35
+ }
36
+ return new Contract(instance, artifact, wallet);
37
+ }
38
+
39
+ /**
40
+ * Creates a tx to deploy a new instance of a contract.
41
+ * @param wallet - The wallet for executing the deployment.
42
+ * @param artifact - Build artifact of the contract to deploy
43
+ * @param args - Arguments for the constructor.
44
+ * @param constructorName - The name of the constructor function to call.
45
+ */
46
+ public static deploy(wallet: Wallet, artifact: ContractArtifact, args: any[], constructorName?: string) {
47
+ const postDeployCtor = (address: AztecAddress, wallet: Wallet) => Contract.at(address, artifact, wallet);
48
+ return new DeployMethod(PublicKeys.default(), wallet, artifact, postDeployCtor, args, constructorName);
49
+ }
50
+
51
+ /**
52
+ * Creates a tx to deploy a new instance of a contract using the specified public keys hash to derive the address.
53
+ * @param publicKeys - Hash of public keys to use for deriving the address.
54
+ * @param wallet - The wallet for executing the deployment.
55
+ * @param artifact - Build artifact of the contract.
56
+ * @param args - Arguments for the constructor.
57
+ * @param constructorName - The name of the constructor function to call.
58
+ */
59
+ public static deployWithPublicKeys(
60
+ publicKeys: PublicKeys,
61
+ wallet: Wallet,
62
+ artifact: ContractArtifact,
63
+ args: any[],
64
+ constructorName?: string,
65
+ ) {
66
+ const postDeployCtor = (address: AztecAddress, wallet: Wallet) => Contract.at(address, artifact, wallet);
67
+ return new DeployMethod(publicKeys, wallet, artifact, postDeployCtor, args, constructorName);
68
+ }
69
+ }
@@ -0,0 +1,90 @@
1
+ import {
2
+ type ContractArtifact,
3
+ type ContractNote,
4
+ type FieldLayout,
5
+ type FunctionArtifact,
6
+ FunctionSelector,
7
+ } from '@aztec/stdlib/abi';
8
+ import { type ContractInstanceWithAddress, computePartialAddress } from '@aztec/stdlib/contract';
9
+
10
+ import type { Wallet } from '../account/index.js';
11
+ import { ContractFunctionInteraction } from './contract_function_interaction.js';
12
+
13
+ /**
14
+ * Type representing a contract method that returns a ContractFunctionInteraction instance
15
+ * and has a readonly 'selector' property of type Buffer. Takes any number of arguments.
16
+ */
17
+ export type ContractMethod = ((...args: any[]) => ContractFunctionInteraction) & {
18
+ /**
19
+ * The unique identifier for a contract function in bytecode.
20
+ */
21
+ selector: () => Promise<FunctionSelector>;
22
+ };
23
+
24
+ /**
25
+ * Type representing the storage layout of a contract.
26
+ */
27
+ export type ContractStorageLayout<T extends string> = {
28
+ [K in T]: FieldLayout;
29
+ };
30
+
31
+ /**
32
+ * Type representing the notes used in a contract.
33
+ */
34
+ export type ContractNotes<T extends string> = {
35
+ [K in T]: ContractNote;
36
+ };
37
+
38
+ /**
39
+ * Abstract implementation of a contract extended by the Contract class and generated contract types.
40
+ */
41
+ export class ContractBase {
42
+ /**
43
+ * An object containing contract methods mapped to their respective names.
44
+ */
45
+ public methods: { [name: string]: ContractMethod } = {};
46
+
47
+ protected constructor(
48
+ /** The deployed contract instance definition. */
49
+ public readonly instance: ContractInstanceWithAddress,
50
+ /** The Application Binary Interface for the contract. */
51
+ public readonly artifact: ContractArtifact,
52
+ /** The wallet used for interacting with this contract. */
53
+ public wallet: Wallet,
54
+ ) {
55
+ artifact.functions.forEach((f: FunctionArtifact) => {
56
+ const interactionFunction = (...args: any[]) => {
57
+ return new ContractFunctionInteraction(this.wallet, this.instance.address, f, args);
58
+ };
59
+
60
+ this.methods[f.name] = Object.assign(interactionFunction, {
61
+ /**
62
+ * A getter for users to fetch the function selector.
63
+ * @returns Selector of the function.
64
+ */
65
+ selector() {
66
+ return FunctionSelector.fromNameAndParameters(f.name, f.parameters);
67
+ },
68
+ });
69
+ });
70
+ }
71
+
72
+ /** Address of the contract. */
73
+ public get address() {
74
+ return this.instance.address;
75
+ }
76
+
77
+ /** Partial address of the contract. */
78
+ public get partialAddress() {
79
+ return computePartialAddress(this.instance);
80
+ }
81
+
82
+ /**
83
+ * Creates a new instance of the contract wrapper attached to a different wallet.
84
+ * @param wallet - Wallet to use for sending txs.
85
+ * @returns A new contract instance.
86
+ */
87
+ public withWallet(wallet: Wallet): this {
88
+ return new ContractBase(this.instance, this.artifact, wallet) as this;
89
+ }
90
+ }
@@ -0,0 +1,173 @@
1
+ import {
2
+ type FunctionAbi,
3
+ FunctionCall,
4
+ FunctionSelector,
5
+ FunctionType,
6
+ decodeFromAbi,
7
+ encodeArguments,
8
+ } from '@aztec/stdlib/abi';
9
+ import { AztecAddress } from '@aztec/stdlib/aztec-address';
10
+ import type { PrivateKernelProverProfileResult } from '@aztec/stdlib/kernel';
11
+ import type { TxExecutionRequest } from '@aztec/stdlib/tx';
12
+
13
+ import type { Wallet } from '../account/wallet.js';
14
+ import { FeeJuicePaymentMethod } from '../fee/fee_juice_payment_method.js';
15
+ import { BaseContractInteraction, type SendMethodOptions } from './base_contract_interaction.js';
16
+
17
+ export type { SendMethodOptions };
18
+
19
+ /**
20
+ * Represents the options for simulating a contract function interaction.
21
+ * Allows specifying the address from which the view method should be called.
22
+ * Disregarded for simulation of public functions
23
+ */
24
+ export type SimulateMethodOptions = Pick<SendMethodOptions, 'fee'> & {
25
+ /** The sender's Aztec address. */
26
+ from?: AztecAddress;
27
+ /** Simulate without checking for the validity of the resulting transaction, e.g. whether it emits any existing nullifiers. */
28
+ skipTxValidation?: boolean;
29
+ /** Whether to ensure the fee payer is not empty and has enough balance to pay for the fee. */
30
+ skipFeeEnforcement?: boolean;
31
+ };
32
+
33
+ /**
34
+ * The result of a profile() call.
35
+ */
36
+ export type ProfileResult = PrivateKernelProverProfileResult & {
37
+ /** The result of the transaction as returned by the contract function. */
38
+ returnValues: any;
39
+ };
40
+
41
+ /**
42
+ * This is the class that is returned when calling e.g. `contract.methods.myMethod(arg0, arg1)`.
43
+ * It contains available interactions one can call on a method, including view.
44
+ */
45
+ export class ContractFunctionInteraction extends BaseContractInteraction {
46
+ constructor(
47
+ wallet: Wallet,
48
+ protected contractAddress: AztecAddress,
49
+ protected functionDao: FunctionAbi,
50
+ protected args: any[],
51
+ ) {
52
+ super(wallet);
53
+ if (args.some(arg => arg === undefined || arg === null)) {
54
+ throw new Error('All function interaction arguments must be defined and not null. Received: ' + args);
55
+ }
56
+ }
57
+
58
+ // docs:start:create
59
+ /**
60
+ * Create a transaction execution request that represents this call, encoded and authenticated by the
61
+ * user's wallet, ready to be simulated.
62
+ * @param opts - An optional object containing additional configuration for the transaction.
63
+ * @returns A Promise that resolves to a transaction instance.
64
+ */
65
+ public async create(opts: SendMethodOptions = {}): Promise<TxExecutionRequest> {
66
+ // docs:end:create
67
+ if (this.functionDao.functionType === FunctionType.UNCONSTRAINED) {
68
+ throw new Error("Can't call `create` on an unconstrained function.");
69
+ }
70
+ const calls = [await this.request()];
71
+ const capsules = this.getCapsules();
72
+ const fee = await this.getFeeOptions({ calls, capsules, ...opts });
73
+ const { nonce, cancellable } = opts;
74
+ return await this.wallet.createTxExecutionRequest({ calls, fee, nonce, cancellable, capsules });
75
+ }
76
+
77
+ // docs:start:request
78
+ /**
79
+ * Returns an execution request that represents this operation. Useful as a building
80
+ * block for constructing batch requests.
81
+ * @returns An execution request wrapped in promise.
82
+ */
83
+ public async request(): Promise<FunctionCall> {
84
+ // docs:end:request
85
+ const args = encodeArguments(this.functionDao, this.args);
86
+ return {
87
+ name: this.functionDao.name,
88
+ args,
89
+ selector: await FunctionSelector.fromNameAndParameters(this.functionDao.name, this.functionDao.parameters),
90
+ type: this.functionDao.functionType,
91
+ to: this.contractAddress,
92
+ isStatic: this.functionDao.isStatic,
93
+ returnTypes: this.functionDao.returnTypes,
94
+ };
95
+ }
96
+
97
+ // docs:start:simulate
98
+ /**
99
+ * Simulate a transaction and get its return values
100
+ * Differs from prove in a few important ways:
101
+ * 1. It returns the values of the function execution
102
+ * 2. It supports `unconstrained`, `private` and `public` functions
103
+ *
104
+ * @param options - An optional object containing additional configuration for the transaction.
105
+ * @returns The result of the transaction as returned by the contract function.
106
+ */
107
+ public async simulate(options: SimulateMethodOptions = {}): Promise<any> {
108
+ // docs:end:simulate
109
+ if (this.functionDao.functionType == FunctionType.UNCONSTRAINED) {
110
+ return this.wallet.simulateUnconstrained(this.functionDao.name, this.args, this.contractAddress, options?.from);
111
+ }
112
+
113
+ const fee = options.fee ?? { paymentMethod: new FeeJuicePaymentMethod(AztecAddress.ZERO) };
114
+ const txRequest = await this.create({ fee });
115
+ const simulatedTx = await this.wallet.simulateTx(
116
+ txRequest,
117
+ true /* simulatePublic */,
118
+ options.from,
119
+ options.skipTxValidation,
120
+ options.skipFeeEnforcement ?? true,
121
+ );
122
+
123
+ let rawReturnValues;
124
+ if (this.functionDao.functionType == FunctionType.PRIVATE) {
125
+ if (simulatedTx.getPrivateReturnValues().nested.length > 0) {
126
+ // The function invoked is private and it was called via an account contract
127
+ // TODO(#10631): There is a bug here: this branch might be triggered when there is no-account contract as well
128
+ rawReturnValues = simulatedTx.getPrivateReturnValues().nested[0].values;
129
+ } else {
130
+ // The function invoked is private and it was called directly (without account contract)
131
+ rawReturnValues = simulatedTx.getPrivateReturnValues().values;
132
+ }
133
+ } else {
134
+ // For public functions we retrieve the first values directly from the public output.
135
+ rawReturnValues = simulatedTx.getPublicReturnValues()?.[0].values;
136
+ }
137
+
138
+ return rawReturnValues ? decodeFromAbi(this.functionDao.returnTypes, rawReturnValues) : [];
139
+ }
140
+
141
+ /**
142
+ * Simulate a transaction and profile the gate count for each function in the transaction.
143
+ * @param options - Same options as `simulate`.
144
+ *
145
+ * @returns An object containing the function return value and profile result.
146
+ */
147
+ public async simulateWithProfile(options: SimulateMethodOptions = {}): Promise<ProfileResult> {
148
+ if (this.functionDao.functionType == FunctionType.UNCONSTRAINED) {
149
+ throw new Error("Can't profile an unconstrained function.");
150
+ }
151
+
152
+ const txRequest = await this.create({ fee: options.fee });
153
+ const simulatedTx = await this.wallet.simulateTx(
154
+ txRequest,
155
+ true,
156
+ options?.from,
157
+ options?.skipTxValidation,
158
+ undefined,
159
+ true,
160
+ );
161
+
162
+ const rawReturnValues =
163
+ this.functionDao.functionType == FunctionType.PRIVATE
164
+ ? simulatedTx.getPrivateReturnValues().nested?.[0].values
165
+ : simulatedTx.getPublicReturnValues()?.[0].values;
166
+ const rawReturnValuesDecoded = rawReturnValues ? decodeFromAbi(this.functionDao.returnTypes, rawReturnValues) : [];
167
+
168
+ return {
169
+ returnValues: rawReturnValuesDecoded,
170
+ gateCounts: simulatedTx.profileResult!.gateCounts,
171
+ };
172
+ }
173
+ }