@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,410 @@
1
+ import type { ViemPublicClient, ViemWalletClient } from '@aztec/ethereum';
2
+ import { extractEvent } from '@aztec/ethereum/utils';
3
+ import { sha256ToField } from '@aztec/foundation/crypto';
4
+ import { EthAddress } from '@aztec/foundation/eth-address';
5
+ import { Fr } from '@aztec/foundation/fields';
6
+ import type { Logger } from '@aztec/foundation/log';
7
+ import type { SiblingPath } from '@aztec/foundation/trees';
8
+ import { FeeJuicePortalAbi, OutboxAbi, TestERC20Abi, TokenPortalAbi } from '@aztec/l1-artifacts';
9
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
10
+ import { computeSecretHash } from '@aztec/stdlib/hash';
11
+ import type { PXE } from '@aztec/stdlib/interfaces/client';
12
+
13
+ import { type GetContractReturnType, type Hex, getContract, toFunctionSelector } from 'viem';
14
+
15
+ // docs:start:claim_type
16
+ // docs:start:claim_type_amount
17
+ /** L1 to L2 message info to claim it on L2. */
18
+ export type L2Claim = {
19
+ /** Secret for claiming. */
20
+ claimSecret: Fr;
21
+ /** Hash of the secret for claiming. */
22
+ claimSecretHash: Fr;
23
+ /** Hash of the message. */
24
+ messageHash: Hex;
25
+ /** Leaf index in the L1 to L2 message tree. */
26
+ messageLeafIndex: bigint;
27
+ };
28
+ // docs:end:claim_type
29
+
30
+ /** L1 to L2 message info that corresponds to an amount to claim. */
31
+ export type L2AmountClaim = L2Claim & { /** Amount to claim */ claimAmount: bigint };
32
+ // docs:end:claim_type_amount
33
+
34
+ /** L1 to L2 message info that corresponds to an amount to claim with associated recipient. */
35
+ export type L2AmountClaimWithRecipient = L2AmountClaim & {
36
+ /** Address that will receive the newly minted notes. */ recipient: AztecAddress;
37
+ };
38
+
39
+ /** Stringifies an eth address for logging. */
40
+ function stringifyEthAddress(address: EthAddress | Hex, name?: string) {
41
+ return name ? `${name} (${address.toString()})` : address.toString();
42
+ }
43
+
44
+ /** Generates a pair secret and secret hash */
45
+ export async function generateClaimSecret(logger?: Logger): Promise<[Fr, Fr]> {
46
+ const secret = Fr.random();
47
+ const secretHash = await computeSecretHash(secret);
48
+ logger?.verbose(`Generated claim secret=${secret.toString()} hash=${secretHash.toString()}`);
49
+ return [secret, secretHash];
50
+ }
51
+
52
+ /** Helper for managing an ERC20 on L1. */
53
+ export class L1TokenManager {
54
+ private contract: GetContractReturnType<typeof TestERC20Abi, ViemWalletClient>;
55
+
56
+ public constructor(
57
+ /** Address of the ERC20 contract. */
58
+ public readonly address: EthAddress,
59
+ private publicClient: ViemPublicClient,
60
+ private walletClient: ViemWalletClient,
61
+ private logger: Logger,
62
+ ) {
63
+ this.contract = getContract({
64
+ address: this.address.toString(),
65
+ abi: TestERC20Abi,
66
+ client: this.walletClient,
67
+ });
68
+ }
69
+
70
+ /**
71
+ * Returns the balance of the given address.
72
+ * @param address - Address to get the balance of.
73
+ */
74
+ public async getL1TokenBalance(address: Hex) {
75
+ return await this.contract.read.balanceOf([address]);
76
+ }
77
+
78
+ /**
79
+ * Mints tokens for the given address. Returns once the tx has been mined.
80
+ * @param amount - Amount to mint.
81
+ * @param address - Address to mint the tokens for.
82
+ * @param addressName - Optional name of the address for logging.
83
+ */
84
+ public async mint(amount: bigint, address: Hex, addressName?: string) {
85
+ this.logger.info(`Minting ${amount} tokens for ${stringifyEthAddress(address, addressName)}`);
86
+ await this.publicClient.waitForTransactionReceipt({
87
+ hash: await this.contract.write.mint([address, amount]),
88
+ });
89
+ }
90
+
91
+ /**
92
+ * Approves tokens for the given address. Returns once the tx has been mined.
93
+ * @param amount - Amount to approve.
94
+ * @param address - Address to approve the tokens for.
95
+ * @param addressName - Optional name of the address for logging.
96
+ */
97
+ public async approve(amount: bigint, address: Hex, addressName = '') {
98
+ this.logger.info(`Approving ${amount} tokens for ${stringifyEthAddress(address, addressName)}`);
99
+ await this.publicClient.waitForTransactionReceipt({
100
+ hash: await this.contract.write.approve([address, amount]),
101
+ });
102
+ }
103
+ }
104
+
105
+ /** Helper for interacting with the FeeJuicePortal on L1. */
106
+ export class L1FeeJuicePortalManager {
107
+ private readonly tokenManager: L1TokenManager;
108
+ private readonly contract: GetContractReturnType<typeof FeeJuicePortalAbi, ViemWalletClient>;
109
+
110
+ constructor(
111
+ portalAddress: EthAddress,
112
+ tokenAddress: EthAddress,
113
+ private readonly publicClient: ViemPublicClient,
114
+ private readonly walletClient: ViemWalletClient,
115
+ private readonly logger: Logger,
116
+ ) {
117
+ this.tokenManager = new L1TokenManager(tokenAddress, publicClient, walletClient, logger);
118
+ this.contract = getContract({
119
+ address: portalAddress.toString(),
120
+ abi: FeeJuicePortalAbi,
121
+ client: this.walletClient,
122
+ });
123
+ }
124
+
125
+ /** Returns the associated token manager for the L1 ERC20. */
126
+ public getTokenManager() {
127
+ return this.tokenManager;
128
+ }
129
+
130
+ /**
131
+ * Bridges fee juice from L1 to L2 publicly. Handles L1 ERC20 approvals. Returns once the tx has been mined.
132
+ * @param to - Address to send the tokens to on L2.
133
+ * @param amount - Amount of tokens to send.
134
+ * @param mint - Whether to mint the tokens before sending (only during testing).
135
+ */
136
+ public async bridgeTokensPublic(to: AztecAddress, amount: bigint, mint = false): Promise<L2AmountClaim> {
137
+ const [claimSecret, claimSecretHash] = await generateClaimSecret();
138
+ if (mint) {
139
+ await this.tokenManager.mint(amount, this.walletClient.account.address);
140
+ }
141
+
142
+ await this.tokenManager.approve(amount, this.contract.address, 'FeeJuice Portal');
143
+
144
+ this.logger.info('Sending L1 Fee Juice to L2 to be claimed publicly');
145
+ const args = [to.toString(), amount, claimSecretHash.toString()] as const;
146
+
147
+ await this.contract.simulate.depositToAztecPublic(args);
148
+
149
+ const txReceipt = await this.publicClient.waitForTransactionReceipt({
150
+ hash: await this.contract.write.depositToAztecPublic(args),
151
+ });
152
+
153
+ this.logger.info('Deposited to Aztec public successfully');
154
+
155
+ const log = extractEvent(
156
+ txReceipt.logs,
157
+ this.contract.address,
158
+ this.contract.abi,
159
+ 'DepositToAztecPublic',
160
+ log =>
161
+ log.args.secretHash === claimSecretHash.toString() &&
162
+ log.args.amount === amount &&
163
+ log.args.to === to.toString(),
164
+ this.logger,
165
+ );
166
+
167
+ return {
168
+ claimAmount: amount,
169
+ claimSecret,
170
+ claimSecretHash,
171
+ messageHash: log.args.key,
172
+ messageLeafIndex: log.args.index,
173
+ };
174
+ }
175
+
176
+ /**
177
+ * Creates a new instance
178
+ * @param pxe - PXE client used for retrieving the L1 contract addresses.
179
+ * @param publicClient - L1 public client.
180
+ * @param walletClient - L1 wallet client.
181
+ * @param logger - Logger.
182
+ */
183
+ public static async new(
184
+ pxe: PXE,
185
+ publicClient: ViemPublicClient,
186
+ walletClient: ViemWalletClient,
187
+ logger: Logger,
188
+ ): Promise<L1FeeJuicePortalManager> {
189
+ const {
190
+ l1ContractAddresses: { feeJuiceAddress, feeJuicePortalAddress },
191
+ } = await pxe.getNodeInfo();
192
+
193
+ if (feeJuiceAddress.isZero() || feeJuicePortalAddress.isZero()) {
194
+ throw new Error('Portal or token not deployed on L1');
195
+ }
196
+
197
+ return new L1FeeJuicePortalManager(feeJuicePortalAddress, feeJuiceAddress, publicClient, walletClient, logger);
198
+ }
199
+ }
200
+
201
+ /** Helper for interacting with a test TokenPortal on L1 for sending tokens to L2. */
202
+ export class L1ToL2TokenPortalManager {
203
+ protected readonly portal: GetContractReturnType<typeof TokenPortalAbi, ViemWalletClient>;
204
+ protected readonly tokenManager: L1TokenManager;
205
+
206
+ constructor(
207
+ portalAddress: EthAddress,
208
+ tokenAddress: EthAddress,
209
+ protected publicClient: ViemPublicClient,
210
+ protected walletClient: ViemWalletClient,
211
+ protected logger: Logger,
212
+ ) {
213
+ this.tokenManager = new L1TokenManager(tokenAddress, publicClient, walletClient, logger);
214
+ this.portal = getContract({
215
+ address: portalAddress.toString(),
216
+ abi: TokenPortalAbi,
217
+ client: this.walletClient,
218
+ });
219
+ }
220
+
221
+ /** Returns the token manager for the underlying L1 token. */
222
+ public getTokenManager() {
223
+ return this.tokenManager;
224
+ }
225
+
226
+ /**
227
+ * Bridges tokens from L1 to L2. Handles token approvals. Returns once the tx has been mined.
228
+ * @param to - Address to send the tokens to on L2.
229
+ * @param amount - Amount of tokens to send.
230
+ * @param mint - Whether to mint the tokens before sending (only during testing).
231
+ */
232
+ public async bridgeTokensPublic(to: AztecAddress, amount: bigint, mint = false): Promise<L2AmountClaim> {
233
+ const [claimSecret, claimSecretHash] = await this.bridgeSetup(amount, mint);
234
+
235
+ this.logger.info('Sending L1 tokens to L2 to be claimed publicly');
236
+ const { request } = await this.portal.simulate.depositToAztecPublic([
237
+ to.toString(),
238
+ amount,
239
+ claimSecretHash.toString(),
240
+ ]);
241
+
242
+ const txReceipt = await this.publicClient.waitForTransactionReceipt({
243
+ hash: await this.walletClient.writeContract(request),
244
+ });
245
+
246
+ const log = extractEvent(
247
+ txReceipt.logs,
248
+ this.portal.address,
249
+ this.portal.abi,
250
+ 'DepositToAztecPublic',
251
+ log =>
252
+ log.args.secretHash === claimSecretHash.toString() &&
253
+ log.args.amount === amount &&
254
+ log.args.to === to.toString(),
255
+ this.logger,
256
+ );
257
+
258
+ return {
259
+ claimAmount: amount,
260
+ claimSecret,
261
+ claimSecretHash,
262
+ messageHash: log.args.key,
263
+ messageLeafIndex: log.args.index,
264
+ };
265
+ }
266
+
267
+ /**
268
+ * Bridges tokens from L1 to L2 privately. Handles token approvals. Returns once the tx has been mined.
269
+ * @param to - Address to send the tokens to on L2.
270
+ * @param amount - Amount of tokens to send.
271
+ * @param mint - Whether to mint the tokens before sending (only during testing).
272
+ */
273
+ public async bridgeTokensPrivate(
274
+ to: AztecAddress,
275
+ amount: bigint,
276
+ mint = false,
277
+ ): Promise<L2AmountClaimWithRecipient> {
278
+ const [claimSecret, claimSecretHash] = await this.bridgeSetup(amount, mint);
279
+
280
+ this.logger.info('Sending L1 tokens to L2 to be claimed privately');
281
+ const { request } = await this.portal.simulate.depositToAztecPrivate([amount, claimSecretHash.toString()]);
282
+
283
+ const txReceipt = await this.publicClient.waitForTransactionReceipt({
284
+ hash: await this.walletClient.writeContract(request),
285
+ });
286
+
287
+ const log = extractEvent(
288
+ txReceipt.logs,
289
+ this.portal.address,
290
+ this.portal.abi,
291
+ 'DepositToAztecPrivate',
292
+ log => log.args.amount === amount && log.args.secretHashForL2MessageConsumption === claimSecretHash.toString(),
293
+ this.logger,
294
+ );
295
+
296
+ this.logger.info(
297
+ `Claim message secret: ${claimSecret.toString()}, claim message secret hash: ${claimSecretHash.toString()}`,
298
+ );
299
+
300
+ return {
301
+ claimAmount: amount,
302
+ claimSecret,
303
+ claimSecretHash,
304
+ recipient: to,
305
+ messageHash: log.args.key,
306
+ messageLeafIndex: log.args.index,
307
+ };
308
+ }
309
+
310
+ private async bridgeSetup(amount: bigint, mint: boolean) {
311
+ if (mint) {
312
+ await this.tokenManager.mint(amount, this.walletClient.account.address);
313
+ }
314
+ await this.tokenManager.approve(amount, this.portal.address, 'TokenPortal');
315
+ return generateClaimSecret();
316
+ }
317
+ }
318
+
319
+ /** Helper for interacting with a test TokenPortal on L1 for both withdrawing from and bridging to L2. */
320
+ export class L1TokenPortalManager extends L1ToL2TokenPortalManager {
321
+ private readonly outbox: GetContractReturnType<typeof OutboxAbi, ViemWalletClient>;
322
+
323
+ constructor(
324
+ portalAddress: EthAddress,
325
+ tokenAddress: EthAddress,
326
+ outboxAddress: EthAddress,
327
+ publicClient: ViemPublicClient,
328
+ walletClient: ViemWalletClient,
329
+ logger: Logger,
330
+ ) {
331
+ super(portalAddress, tokenAddress, publicClient, walletClient, logger);
332
+ this.outbox = getContract({
333
+ address: outboxAddress.toString(),
334
+ abi: OutboxAbi,
335
+ client: walletClient,
336
+ });
337
+ }
338
+
339
+ /**
340
+ * Withdraws funds from the portal by consuming an L2 to L1 message. Returns once the tx is mined on L1.
341
+ * @param amount - Amount to withdraw.
342
+ * @param recipient - Who will receive the funds.
343
+ * @param blockNumber - L2 block number of the message.
344
+ * @param messageIndex - Index of the message.
345
+ * @param siblingPath - Sibling path of the message.
346
+ */
347
+ public async withdrawFunds(
348
+ amount: bigint,
349
+ recipient: EthAddress,
350
+ blockNumber: bigint,
351
+ messageIndex: bigint,
352
+ siblingPath: SiblingPath<number>,
353
+ ) {
354
+ this.logger.info(
355
+ `Sending L1 tx to consume message at block ${blockNumber} index ${messageIndex} to withdraw ${amount}`,
356
+ );
357
+
358
+ const isConsumedBefore = await this.outbox.read.hasMessageBeenConsumedAtBlockAndIndex([blockNumber, messageIndex]);
359
+ if (isConsumedBefore) {
360
+ throw new Error(`L1 to L2 message at block ${blockNumber} index ${messageIndex} has already been consumed`);
361
+ }
362
+
363
+ // Call function on L1 contract to consume the message
364
+ const { request: withdrawRequest } = await this.portal.simulate.withdraw([
365
+ recipient.toString(),
366
+ amount,
367
+ false,
368
+ BigInt(blockNumber),
369
+ messageIndex,
370
+ siblingPath.toBufferArray().map((buf: Buffer): Hex => `0x${buf.toString('hex')}`),
371
+ ]);
372
+
373
+ await this.publicClient.waitForTransactionReceipt({ hash: await this.walletClient.writeContract(withdrawRequest) });
374
+
375
+ const isConsumedAfter = await this.outbox.read.hasMessageBeenConsumedAtBlockAndIndex([blockNumber, messageIndex]);
376
+ if (!isConsumedAfter) {
377
+ throw new Error(`L1 to L2 message at block ${blockNumber} index ${messageIndex} not consumed after withdrawal`);
378
+ }
379
+ }
380
+
381
+ /**
382
+ * Computes the L2 to L1 message leaf for the given parameters.
383
+ * @param amount - Amount to bridge.
384
+ * @param recipient - Recipient on L1.
385
+ * @param l2Bridge - Address of the L2 bridge.
386
+ * @param callerOnL1 - Caller address on L1.
387
+ */
388
+ public getL2ToL1MessageLeaf(
389
+ amount: bigint,
390
+ recipient: EthAddress,
391
+ l2Bridge: AztecAddress,
392
+ callerOnL1: EthAddress = EthAddress.ZERO,
393
+ ): Fr {
394
+ const content = sha256ToField([
395
+ Buffer.from(toFunctionSelector('withdraw(address,uint256,address)').substring(2), 'hex'),
396
+ recipient.toBuffer32(),
397
+ new Fr(amount).toBuffer(),
398
+ callerOnL1.toBuffer32(),
399
+ ]);
400
+ const leaf = sha256ToField([
401
+ l2Bridge.toBuffer(),
402
+ new Fr(1).toBuffer(), // aztec version
403
+ EthAddress.fromString(this.portal.address).toBuffer32() ?? Buffer.alloc(32, 0),
404
+ new Fr(this.publicClient.chain.id).toBuffer(), // chain id
405
+ content.toBuffer(),
406
+ ]);
407
+
408
+ return leaf;
409
+ }
410
+ }
package/src/api/fee.ts ADDED
@@ -0,0 +1,5 @@
1
+ export type { FeePaymentMethod } from '../fee/fee_payment_method.js';
2
+ export { FeeJuicePaymentMethod } from '../fee/fee_juice_payment_method.js';
3
+ export { PrivateFeePaymentMethod } from '../fee/private_fee_payment_method.js';
4
+ export { PublicFeePaymentMethod } from '../fee/public_fee_payment_method.js';
5
+ export { FeeJuicePaymentMethodWithClaim } from '../fee/fee_juice_payment_method_with_claim.js';
@@ -0,0 +1 @@
1
+ export { Point, Fr, Fq, GrumpkinScalar } from '@aztec/foundation/fields';
@@ -0,0 +1 @@
1
+ export type { PXE } from '@aztec/stdlib/interfaces/client';
package/src/api/log.ts ADDED
@@ -0,0 +1 @@
1
+ export { createLogger, type Logger } from '@aztec/foundation/log';
@@ -0,0 +1 @@
1
+ export { LogId } from '@aztec/stdlib/logs';
@@ -0,0 +1 @@
1
+ export { TxHash } from '@aztec/stdlib/tx';
@@ -0,0 +1,7 @@
1
+ export {
2
+ AccountWallet,
3
+ AccountWalletWithSecretKey as AccountWalletWithSecretKey,
4
+ SignerlessWallet,
5
+ type Wallet,
6
+ getWallet,
7
+ } from '../wallet/index.js';
@@ -0,0 +1,192 @@
1
+ import type { Fr } from '@aztec/foundation/fields';
2
+ import { createLogger } from '@aztec/foundation/log';
3
+ import { GasSettings } from '@aztec/stdlib/gas';
4
+ import type { Capsule, TxExecutionRequest, TxProvingResult } from '@aztec/stdlib/tx';
5
+
6
+ import type { Wallet } from '../account/wallet.js';
7
+ import type { ExecutionRequestInit } from '../entrypoint/entrypoint.js';
8
+ import type { FeeOptions, UserFeeOptions } from '../entrypoint/payload.js';
9
+ import { FeeJuicePaymentMethod } from '../fee/fee_juice_payment_method.js';
10
+ import { getGasLimits } from './get_gas_limits.js';
11
+ import { ProvenTx } from './proven_tx.js';
12
+ import { SentTx } from './sent_tx.js';
13
+
14
+ /**
15
+ * Represents options for calling a (constrained) function in a contract.
16
+ * Allows the user to specify the sender address and nonce for a transaction.
17
+ */
18
+ export type SendMethodOptions = {
19
+ /** Wether to skip the simulation of the public part of the transaction. */
20
+ skipPublicSimulation?: boolean;
21
+ /** The fee options for the transaction. */
22
+ fee?: UserFeeOptions;
23
+ /** Custom nonce to inject into the app payload of the transaction. Useful when trying to cancel an ongoing transaction by creating a new one with a higher fee */
24
+ nonce?: Fr;
25
+ /** Whether the transaction can be cancelled. If true, an extra nullifier will be emitted: H(nonce, GENERATOR_INDEX__TX_NULLIFIER) */
26
+ cancellable?: boolean;
27
+ };
28
+
29
+ /**
30
+ * Base class for an interaction with a contract, be it a deployment, a function call, or a batch.
31
+ * Implements the sequence create/simulate/send.
32
+ */
33
+ export abstract class BaseContractInteraction {
34
+ protected log = createLogger('aztecjs:contract_interaction');
35
+
36
+ private capsules: Capsule[] = [];
37
+
38
+ constructor(protected wallet: Wallet) {}
39
+
40
+ /**
41
+ * Create a transaction execution request ready to be simulated.
42
+ * @param options - An optional object containing additional configuration for the transaction.
43
+ * @returns A transaction execution request.
44
+ */
45
+ public abstract create(options?: SendMethodOptions): Promise<TxExecutionRequest>;
46
+
47
+ /**
48
+ * Creates a transaction execution request, simulates and proves it. Differs from .prove in
49
+ * that its result does not include the wallet nor the composed tx object, but only the proving result.
50
+ * This object can then be used to either create a ProvenTx ready to be sent, or directly send the transaction.
51
+ * @param options - optional arguments to be used in the creation of the transaction
52
+ * @returns The proving result.
53
+ */
54
+ protected async proveInternal(options: SendMethodOptions = {}): Promise<TxProvingResult> {
55
+ const txRequest = await this.create(options);
56
+ const txSimulationResult = await this.wallet.simulateTx(txRequest, !options.skipPublicSimulation, undefined, true);
57
+ return await this.wallet.proveTx(txRequest, txSimulationResult.privateExecutionResult);
58
+ }
59
+
60
+ // docs:start:prove
61
+ /**
62
+ * Proves a transaction execution request and returns a tx object ready to be sent.
63
+ * @param options - optional arguments to be used in the creation of the transaction
64
+ * @returns The resulting transaction
65
+ */
66
+ public async prove(options: SendMethodOptions = {}): Promise<ProvenTx> {
67
+ // docs:end:prove
68
+ const txProvingResult = await this.proveInternal(options);
69
+ return new ProvenTx(this.wallet, txProvingResult.toTx());
70
+ }
71
+
72
+ // docs:start:send
73
+ /**
74
+ * Sends a transaction to the contract function with the specified options.
75
+ * This function throws an error if called on an unconstrained function.
76
+ * It creates and signs the transaction if necessary, and returns a SentTx instance,
77
+ * which can be used to track the transaction status, receipt, and events.
78
+ * @param options - An optional object containing 'from' property representing
79
+ * the AztecAddress of the sender. If not provided, the default address is used.
80
+ * @returns A SentTx instance for tracking the transaction status and information.
81
+ */
82
+ public send(options: SendMethodOptions = {}): SentTx {
83
+ // docs:end:send
84
+ const promise = (async () => {
85
+ const txProvingResult = await this.proveInternal(options);
86
+ return this.wallet.sendTx(txProvingResult.toTx());
87
+ })();
88
+ return new SentTx(this.wallet, promise);
89
+ }
90
+
91
+ // docs:start:estimateGas
92
+ /**
93
+ * Estimates gas for a given tx request and returns gas limits for it.
94
+ * @param opts - Options.
95
+ * @param pad - Percentage to pad the suggested gas limits by, if empty, defaults to 10%.
96
+ * @returns Gas limits.
97
+ */
98
+ public async estimateGas(
99
+ opts?: Omit<SendMethodOptions, 'estimateGas' | 'skipPublicSimulation'>,
100
+ ): Promise<Pick<GasSettings, 'gasLimits' | 'teardownGasLimits'>> {
101
+ // docs:end:estimateGas
102
+ const txRequest = await this.create({ ...opts, fee: { ...opts?.fee, estimateGas: false } });
103
+ const simulationResult = await this.wallet.simulateTx(
104
+ txRequest,
105
+ true /*simulatePublic*/,
106
+ undefined /* msgSender */,
107
+ undefined /* skipTxValidation */,
108
+ true /* skipFeeEnforcement */,
109
+ );
110
+ const { totalGas: gasLimits, teardownGas: teardownGasLimits } = getGasLimits(
111
+ simulationResult,
112
+ opts?.fee?.estimatedGasPadding,
113
+ );
114
+ return { gasLimits, teardownGasLimits };
115
+ }
116
+
117
+ /**
118
+ * Returns default fee options based on the user opts without running a simulation for gas estimation.
119
+ * @param fee - User-provided fee options.
120
+ */
121
+ protected async getDefaultFeeOptions(fee: UserFeeOptions | undefined): Promise<FeeOptions> {
122
+ const maxFeesPerGas =
123
+ fee?.gasSettings?.maxFeesPerGas ?? (await this.wallet.getCurrentBaseFees()).mul(1 + (fee?.baseFeePadding ?? 0.5));
124
+ const paymentMethod = fee?.paymentMethod ?? new FeeJuicePaymentMethod(this.wallet.getAddress());
125
+ const gasSettings: GasSettings = GasSettings.default({ ...fee?.gasSettings, maxFeesPerGas });
126
+ this.log.debug(`Using L2 gas settings`, gasSettings);
127
+ return { gasSettings, paymentMethod };
128
+ }
129
+
130
+ // docs:start:getFeeOptions
131
+ /**
132
+ * Return fee options based on the user opts, estimating tx gas if needed.
133
+ * @param request - Request to execute for this interaction.
134
+ * @param pad - Percentage to pad the suggested gas limits by, as decimal (e.g., 0.10 for 10%).
135
+ * @returns Fee options for the actual transaction.
136
+ */
137
+ protected async getFeeOptions(
138
+ request: Omit<ExecutionRequestInit, 'fee'> & { /** User-provided fee options */ fee?: UserFeeOptions },
139
+ ): Promise<FeeOptions> {
140
+ // docs:end:getFeeOptions
141
+ const defaultFeeOptions = await this.getDefaultFeeOptions(request.fee);
142
+ const paymentMethod = defaultFeeOptions.paymentMethod;
143
+ const maxFeesPerGas = defaultFeeOptions.gasSettings.maxFeesPerGas;
144
+ const maxPriorityFeesPerGas = defaultFeeOptions.gasSettings.maxPriorityFeesPerGas;
145
+
146
+ let gasSettings = defaultFeeOptions.gasSettings;
147
+ if (request.fee?.estimateGas) {
148
+ const feeForEstimation: FeeOptions = { paymentMethod, gasSettings };
149
+ const txRequest = await this.wallet.createTxExecutionRequest({ ...request, fee: feeForEstimation });
150
+ const simulationResult = await this.wallet.simulateTx(
151
+ txRequest,
152
+ true /*simulatePublic*/,
153
+ undefined /* msgSender */,
154
+ undefined /* skipTxValidation */,
155
+ true /* skipFeeEnforcement */,
156
+ );
157
+ const { totalGas: gasLimits, teardownGas: teardownGasLimits } = getGasLimits(
158
+ simulationResult,
159
+ request.fee?.estimatedGasPadding,
160
+ );
161
+ gasSettings = GasSettings.from({ maxFeesPerGas, maxPriorityFeesPerGas, gasLimits, teardownGasLimits });
162
+ this.log.verbose(
163
+ `Estimated gas limits for tx: DA=${gasLimits.daGas} L2=${gasLimits.l2Gas} teardownDA=${teardownGasLimits.daGas} teardownL2=${teardownGasLimits.l2Gas}`,
164
+ );
165
+ }
166
+
167
+ return { gasSettings, paymentMethod };
168
+ }
169
+
170
+ /**
171
+ * Add data passed to the oracle calls during this contract interaction.
172
+ * @param capsule - Data passed to oracle calls.
173
+ */
174
+ public addCapsule(capsule: Capsule) {
175
+ this.capsules.push(capsule);
176
+ }
177
+
178
+ /**
179
+ * Add data passed to the oracle calls during this contract interaction.
180
+ * @param capsules - Data passed to oracle calls.
181
+ */
182
+ public addCapsules(capsules: Capsule[]) {
183
+ this.capsules.push(...capsules);
184
+ }
185
+
186
+ /**
187
+ * Return all capsules added for this function interaction.
188
+ */
189
+ public getCapsules() {
190
+ return this.capsules;
191
+ }
192
+ }