@aztec/aztec.js 0.0.1-commit.d431d1c → 0.0.1-commit.db765a8

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 (139) hide show
  1. package/dest/api/abi.d.ts +2 -2
  2. package/dest/api/abi.d.ts.map +1 -1
  3. package/dest/api/block.d.ts +2 -2
  4. package/dest/api/block.d.ts.map +1 -1
  5. package/dest/api/block.js +1 -1
  6. package/dest/api/contract.d.ts +18 -12
  7. package/dest/api/contract.d.ts.map +1 -1
  8. package/dest/api/contract.js +16 -10
  9. package/dest/api/deployment.d.ts +1 -2
  10. package/dest/api/deployment.d.ts.map +1 -1
  11. package/dest/api/deployment.js +0 -1
  12. package/dest/api/events.d.ts +10 -6
  13. package/dest/api/events.d.ts.map +1 -1
  14. package/dest/api/events.js +30 -20
  15. package/dest/api/fields.d.ts +2 -1
  16. package/dest/api/fields.d.ts.map +1 -1
  17. package/dest/api/fields.js +1 -0
  18. package/dest/api/keys.d.ts +1 -1
  19. package/dest/api/keys.js +1 -1
  20. package/dest/api/node.d.ts +8 -4
  21. package/dest/api/node.d.ts.map +1 -1
  22. package/dest/api/node.js +7 -3
  23. package/dest/api/tx.d.ts +2 -2
  24. package/dest/api/tx.d.ts.map +1 -1
  25. package/dest/api/tx.js +1 -1
  26. package/dest/api/wallet.d.ts +3 -2
  27. package/dest/api/wallet.d.ts.map +1 -1
  28. package/dest/api/wallet.js +2 -1
  29. package/dest/contract/base_contract_interaction.d.ts +8 -10
  30. package/dest/contract/base_contract_interaction.d.ts.map +1 -1
  31. package/dest/contract/base_contract_interaction.js +5 -17
  32. package/dest/contract/batch_call.d.ts +3 -3
  33. package/dest/contract/batch_call.d.ts.map +1 -1
  34. package/dest/contract/batch_call.js +17 -8
  35. package/dest/contract/contract_function_interaction.d.ts +7 -16
  36. package/dest/contract/contract_function_interaction.d.ts.map +1 -1
  37. package/dest/contract/contract_function_interaction.js +107 -18
  38. package/dest/contract/deploy_method.d.ts +91 -19
  39. package/dest/contract/deploy_method.d.ts.map +1 -1
  40. package/dest/contract/deploy_method.js +68 -30
  41. package/dest/contract/get_gas_limits.js +3 -3
  42. package/dest/contract/interaction_options.d.ts +99 -21
  43. package/dest/contract/interaction_options.d.ts.map +1 -1
  44. package/dest/contract/interaction_options.js +41 -1
  45. package/dest/contract/protocol_contracts/auth-registry.d.ts +3 -3
  46. package/dest/contract/protocol_contracts/auth-registry.d.ts.map +1 -1
  47. package/dest/contract/protocol_contracts/auth-registry.js +49 -20
  48. package/dest/contract/protocol_contracts/contract-class-registry.d.ts +2 -22
  49. package/dest/contract/protocol_contracts/contract-class-registry.d.ts.map +1 -1
  50. package/dest/contract/protocol_contracts/contract-class-registry.js +13 -671
  51. package/dest/contract/protocol_contracts/contract-instance-registry.d.ts +2 -11
  52. package/dest/contract/protocol_contracts/contract-instance-registry.d.ts.map +1 -1
  53. package/dest/contract/protocol_contracts/contract-instance-registry.js +80 -486
  54. package/dest/contract/protocol_contracts/fee-juice.d.ts +1 -10
  55. package/dest/contract/protocol_contracts/fee-juice.d.ts.map +1 -1
  56. package/dest/contract/protocol_contracts/fee-juice.js +7 -413
  57. package/dest/contract/protocol_contracts/multi-call-entrypoint.d.ts +3 -3
  58. package/dest/contract/protocol_contracts/multi-call-entrypoint.d.ts.map +1 -1
  59. package/dest/contract/protocol_contracts/multi-call-entrypoint.js +40 -15
  60. package/dest/contract/protocol_contracts/public-checks.d.ts +3 -3
  61. package/dest/contract/protocol_contracts/public-checks.d.ts.map +1 -1
  62. package/dest/contract/protocol_contracts/public-checks.js +40 -23
  63. package/dest/contract/wait_for_proven.js +1 -1
  64. package/dest/contract/wait_opts.d.ts +16 -0
  65. package/dest/contract/wait_opts.d.ts.map +1 -0
  66. package/dest/contract/wait_opts.js +5 -0
  67. package/dest/fee/fee_juice_payment_method_with_claim.js +6 -6
  68. package/dest/fee/private_fee_payment_method.js +10 -10
  69. package/dest/fee/public_fee_payment_method.js +10 -10
  70. package/dest/fee/sponsored_fee_payment.js +3 -3
  71. package/dest/utils/abi_types.d.ts +6 -1
  72. package/dest/utils/abi_types.d.ts.map +1 -1
  73. package/dest/utils/abi_types.js +1 -1
  74. package/dest/utils/authwit.d.ts +6 -6
  75. package/dest/utils/authwit.d.ts.map +1 -1
  76. package/dest/utils/authwit.js +12 -12
  77. package/dest/utils/node.d.ts +12 -1
  78. package/dest/utils/node.d.ts.map +1 -1
  79. package/dest/utils/node.js +46 -0
  80. package/dest/wallet/capabilities.d.ts +452 -0
  81. package/dest/wallet/capabilities.d.ts.map +1 -0
  82. package/dest/wallet/capabilities.js +3 -0
  83. package/dest/wallet/deploy_account_method.d.ts +31 -6
  84. package/dest/wallet/deploy_account_method.d.ts.map +1 -1
  85. package/dest/wallet/deploy_account_method.js +26 -0
  86. package/dest/wallet/index.d.ts +2 -1
  87. package/dest/wallet/index.d.ts.map +1 -1
  88. package/dest/wallet/index.js +1 -0
  89. package/dest/wallet/wallet.d.ts +1639 -93
  90. package/dest/wallet/wallet.d.ts.map +1 -1
  91. package/dest/wallet/wallet.js +186 -27
  92. package/package.json +20 -11
  93. package/src/api/abi.ts +1 -0
  94. package/src/api/block.ts +1 -1
  95. package/src/api/contract.ts +31 -9
  96. package/src/api/deployment.ts +0 -1
  97. package/src/api/events.ts +35 -27
  98. package/src/api/fields.ts +1 -0
  99. package/src/api/keys.ts +2 -2
  100. package/src/api/node.ts +7 -3
  101. package/src/api/tx.ts +2 -0
  102. package/src/api/wallet.ts +48 -1
  103. package/src/contract/base_contract_interaction.ts +28 -15
  104. package/src/contract/batch_call.ts +17 -14
  105. package/src/contract/contract_function_interaction.ts +114 -26
  106. package/src/contract/deploy_method.ts +172 -35
  107. package/src/contract/get_gas_limits.ts +3 -3
  108. package/src/contract/interaction_options.ts +141 -23
  109. package/src/contract/protocol_contracts/auth-registry.ts +13 -14
  110. package/src/contract/protocol_contracts/contract-class-registry.ts +4 -347
  111. package/src/contract/protocol_contracts/contract-instance-registry.ts +34 -234
  112. package/src/contract/protocol_contracts/fee-juice.ts +2 -202
  113. package/src/contract/protocol_contracts/multi-call-entrypoint.ts +11 -13
  114. package/src/contract/protocol_contracts/public-checks.ts +11 -15
  115. package/src/contract/wait_for_proven.ts +1 -1
  116. package/src/contract/wait_opts.ts +21 -0
  117. package/src/fee/fee_juice_payment_method_with_claim.ts +5 -5
  118. package/src/fee/private_fee_payment_method.ts +7 -7
  119. package/src/fee/public_fee_payment_method.ts +8 -8
  120. package/src/fee/sponsored_fee_payment.ts +3 -3
  121. package/src/utils/abi_types.ts +7 -0
  122. package/src/utils/authwit.ts +31 -21
  123. package/src/utils/node.ts +62 -0
  124. package/src/wallet/capabilities.ts +500 -0
  125. package/src/wallet/deploy_account_method.ts +56 -4
  126. package/src/wallet/index.ts +1 -0
  127. package/src/wallet/wallet.ts +257 -50
  128. package/dest/contract/deploy_sent_tx.d.ts +0 -48
  129. package/dest/contract/deploy_sent_tx.d.ts.map +0 -1
  130. package/dest/contract/deploy_sent_tx.js +0 -46
  131. package/dest/contract/sent_tx.d.ts +0 -50
  132. package/dest/contract/sent_tx.d.ts.map +0 -1
  133. package/dest/contract/sent_tx.js +0 -90
  134. package/dest/deployment/broadcast_function.d.ts +0 -24
  135. package/dest/deployment/broadcast_function.d.ts.map +0 -1
  136. package/dest/deployment/broadcast_function.js +0 -74
  137. package/src/contract/deploy_sent_tx.ts +0 -75
  138. package/src/contract/sent_tx.ts +0 -129
  139. package/src/deployment/broadcast_function.ts +0 -148
@@ -1,90 +0,0 @@
1
- import { promiseWithResolvers } from '@aztec/foundation/promise';
2
- import { retryUntil } from '@aztec/foundation/retry';
3
- import { TxStatus } from '@aztec/stdlib/tx';
4
- export const DefaultWaitOpts = {
5
- ignoreDroppedReceiptsFor: 5,
6
- timeout: 300,
7
- interval: 1
8
- };
9
- /**
10
- * The SentTx class represents a sent transaction through the PXE (or directly to a node) providing methods to fetch
11
- * its hash, receipt, and mining status.
12
- */ export class SentTx {
13
- walletOrNode;
14
- sendTxPromise;
15
- sendTxError;
16
- txHash;
17
- constructor(walletOrNode, sendTx){
18
- this.walletOrNode = walletOrNode;
19
- const { promise, resolve } = promiseWithResolvers();
20
- this.sendTxPromise = promise;
21
- sendTx().then((txHash)=>{
22
- this.txHash = txHash;
23
- resolve();
24
- }).catch((err)=>{
25
- this.sendTxError = err;
26
- // Calling resolve instead of reject here because we want to throw the error when getTxHash is called.
27
- resolve();
28
- });
29
- }
30
- /**
31
- * Retrieves the transaction hash of the SentTx instance.
32
- * The function internally awaits for the 'txHashPromise' to resolve, and then returns the resolved transaction hash.
33
- *
34
- * @returns A promise that resolves to the transaction hash of the SentTx instance.
35
- * TODO(#7717): Don't throw here.
36
- */ async getTxHash() {
37
- // Make sure sendTx has been resolved, which can be triggered when it returns a txHash or when it throws an error.
38
- await this.sendTxPromise;
39
- // If sendTx threw an error, throw it.
40
- if (this.sendTxError) {
41
- throw this.sendTxError;
42
- }
43
- // sendTx returned a txHash if it's been resolved and no error was set.
44
- return Promise.resolve(this.txHash);
45
- }
46
- /**
47
- * Retrieve the transaction receipt associated with the current SentTx instance.
48
- * The function fetches the transaction hash using 'getTxHash' and then queries
49
- * the PXE to get the corresponding transaction receipt.
50
- *
51
- * @returns A promise that resolves to a TxReceipt object representing the fetched transaction receipt.
52
- */ async getReceipt() {
53
- const txHash = await this.getTxHash();
54
- return await this.walletOrNode.getTxReceipt(txHash);
55
- }
56
- /**
57
- * Awaits for a tx to be mined and returns the receipt. Throws if tx is not mined.
58
- * @param opts - Options for configuring the waiting for the tx to be mined.
59
- * @returns The transaction receipt.
60
- */ async wait(opts) {
61
- const receipt = await this.waitForReceipt(opts);
62
- if (receipt.status !== TxStatus.SUCCESS && !opts?.dontThrowOnRevert) {
63
- throw new Error(`Transaction ${(await this.getTxHash()).toString()} was ${receipt.status}. Reason: ${receipt.error ?? 'unknown'}`);
64
- }
65
- return receipt;
66
- }
67
- async waitForReceipt(opts) {
68
- const txHash = await this.getTxHash();
69
- const startTime = Date.now();
70
- const ignoreDroppedReceiptsFor = opts?.ignoreDroppedReceiptsFor ?? DefaultWaitOpts.ignoreDroppedReceiptsFor;
71
- return await retryUntil(async ()=>{
72
- const txReceipt = await this.walletOrNode.getTxReceipt(txHash);
73
- // If receipt is not yet available, try again
74
- if (txReceipt.status === TxStatus.PENDING) {
75
- return undefined;
76
- }
77
- // If the tx was "dropped", either return it or ignore based on timing.
78
- // We can ignore it at first because the transaction may have been sent to node 1, and now we're asking node 2 for the receipt.
79
- // If we don't allow a short grace period, we could incorrectly return a TxReceipt with status DROPPED.
80
- if (txReceipt.status === TxStatus.DROPPED) {
81
- const elapsedSeconds = (Date.now() - startTime) / 1000;
82
- if (!ignoreDroppedReceiptsFor || elapsedSeconds > ignoreDroppedReceiptsFor) {
83
- return txReceipt;
84
- }
85
- return undefined;
86
- }
87
- return txReceipt;
88
- }, 'isMined', opts?.timeout ?? DefaultWaitOpts.timeout, opts?.interval ?? DefaultWaitOpts.interval);
89
- }
90
- }
@@ -1,24 +0,0 @@
1
- import { type ContractArtifact, FunctionSelector } from '@aztec/stdlib/abi';
2
- import type { ContractFunctionInteraction } from '../contract/contract_function_interaction.js';
3
- import type { Wallet } from '../wallet/index.js';
4
- /**
5
- * Sets up a call to broadcast a private function's bytecode via the ClassRegistry contract.
6
- * Note that this is not required for users to call the function, but is rather a convenience to make
7
- * this code publicly available so dapps or wallets do not need to redistribute it.
8
- * @param wallet - Wallet to send the transaction.
9
- * @param artifact - Contract artifact that contains the function to be broadcast.
10
- * @param selector - Selector of the function to be broadcast.
11
- * @returns A ContractFunctionInteraction object that can be used to send the transaction.
12
- */
13
- export declare function broadcastPrivateFunction(wallet: Wallet, artifact: ContractArtifact, selector: FunctionSelector): Promise<ContractFunctionInteraction>;
14
- /**
15
- * Sets up a call to broadcast a utility function's bytecode via the ClassRegistry contract.
16
- * Note that this is not required for users to call the function, but is rather a convenience to make
17
- * this code publicly available so dapps or wallets do not need to redistribute it.
18
- * @param wallet - Wallet to send the transaction.
19
- * @param artifact - Contract artifact that contains the function to be broadcast.
20
- * @param selector - Selector of the function to be broadcast.
21
- * @returns A ContractFunctionInteraction object that can be used to send the transaction.
22
- */
23
- export declare function broadcastUtilityFunction(wallet: Wallet, artifact: ContractArtifact, selector: FunctionSelector): Promise<ContractFunctionInteraction>;
24
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYnJvYWRjYXN0X2Z1bmN0aW9uLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvZGVwbG95bWVudC9icm9hZGNhc3RfZnVuY3Rpb24udHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBUUEsT0FBTyxFQUFFLEtBQUssZ0JBQWdCLEVBQUUsZ0JBQWdCLEVBQWdDLE1BQU0sbUJBQW1CLENBQUM7QUFTMUcsT0FBTyxLQUFLLEVBQUUsMkJBQTJCLEVBQUUsTUFBTSw4Q0FBOEMsQ0FBQztBQUVoRyxPQUFPLEtBQUssRUFBRSxNQUFNLEVBQUUsTUFBTSxvQkFBb0IsQ0FBQztBQUVqRDs7Ozs7Ozs7R0FRRztBQUNILHdCQUFzQix3QkFBd0IsQ0FDNUMsTUFBTSxFQUFFLE1BQU0sRUFDZCxRQUFRLEVBQUUsZ0JBQWdCLEVBQzFCLFFBQVEsRUFBRSxnQkFBZ0IsR0FDekIsT0FBTyxDQUFDLDJCQUEyQixDQUFDLENBb0R0QztBQUVEOzs7Ozs7OztHQVFHO0FBQ0gsd0JBQXNCLHdCQUF3QixDQUM1QyxNQUFNLEVBQUUsTUFBTSxFQUNkLFFBQVEsRUFBRSxnQkFBZ0IsRUFDMUIsUUFBUSxFQUFFLGdCQUFnQixHQUN6QixPQUFPLENBQUMsMkJBQTJCLENBQUMsQ0E4Q3RDIn0=
@@ -1 +0,0 @@
1
- {"version":3,"file":"broadcast_function.d.ts","sourceRoot":"","sources":["../../src/deployment/broadcast_function.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,KAAK,gBAAgB,EAAE,gBAAgB,EAAgC,MAAM,mBAAmB,CAAC;AAS1G,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,8CAA8C,CAAC;AAEhG,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAEjD;;;;;;;;GAQG;AACH,wBAAsB,wBAAwB,CAC5C,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,gBAAgB,EAC1B,QAAQ,EAAE,gBAAgB,GACzB,OAAO,CAAC,2BAA2B,CAAC,CAoDtC;AAED;;;;;;;;GAQG;AACH,wBAAsB,wBAAwB,CAC5C,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,gBAAgB,EAC1B,QAAQ,EAAE,gBAAgB,GACzB,OAAO,CAAC,2BAA2B,CAAC,CA8CtC"}
@@ -1,74 +0,0 @@
1
- import { ARTIFACT_FUNCTION_TREE_MAX_HEIGHT, CONTRACT_CLASS_REGISTRY_BYTECODE_CAPSULE_SLOT, MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS } from '@aztec/constants';
2
- import { padArrayEnd } from '@aztec/foundation/collection';
3
- import { Fr } from '@aztec/foundation/curves/bn254';
4
- import { ProtocolContractAddress } from '@aztec/protocol-contracts';
5
- import { FunctionSelector, FunctionType, bufferAsFields } from '@aztec/stdlib/abi';
6
- import { computeVerificationKeyHash, createPrivateFunctionMembershipProof, createUtilityFunctionMembershipProof, getContractClassFromArtifact } from '@aztec/stdlib/contract';
7
- import { Capsule } from '@aztec/stdlib/tx';
8
- import { ContractClassRegistryContract } from '../contract/protocol_contracts/contract-class-registry.js';
9
- /**
10
- * Sets up a call to broadcast a private function's bytecode via the ClassRegistry contract.
11
- * Note that this is not required for users to call the function, but is rather a convenience to make
12
- * this code publicly available so dapps or wallets do not need to redistribute it.
13
- * @param wallet - Wallet to send the transaction.
14
- * @param artifact - Contract artifact that contains the function to be broadcast.
15
- * @param selector - Selector of the function to be broadcast.
16
- * @returns A ContractFunctionInteraction object that can be used to send the transaction.
17
- */ export async function broadcastPrivateFunction(wallet, artifact, selector) {
18
- const contractClass = await getContractClassFromArtifact(artifact);
19
- const privateFunctions = artifact.functions.filter((fn)=>fn.functionType === FunctionType.PRIVATE);
20
- const functionsAndSelectors = await Promise.all(privateFunctions.map(async (fn)=>({
21
- f: fn,
22
- selector: await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters)
23
- })));
24
- const privateFunctionArtifact = functionsAndSelectors.find((fn)=>selector.equals(fn.selector))?.f;
25
- if (!privateFunctionArtifact) {
26
- throw new Error(`Private function with selector ${selector.toString()} not found`);
27
- }
28
- const { artifactTreeSiblingPath, artifactTreeLeafIndex, artifactMetadataHash, functionMetadataHash, utilityFunctionsTreeRoot, privateFunctionTreeSiblingPath, privateFunctionTreeLeafIndex } = await createPrivateFunctionMembershipProof(selector, artifact);
29
- const vkHash = await computeVerificationKeyHash(privateFunctionArtifact);
30
- const classRegistry = ContractClassRegistryContract.at(wallet);
31
- const bytecode = bufferAsFields(privateFunctionArtifact.bytecode, MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS);
32
- return classRegistry.methods.broadcast_private_function(contractClass.id, artifactMetadataHash, utilityFunctionsTreeRoot, privateFunctionTreeSiblingPath, privateFunctionTreeLeafIndex, padArrayEnd(artifactTreeSiblingPath, Fr.ZERO, ARTIFACT_FUNCTION_TREE_MAX_HEIGHT), artifactTreeLeafIndex, // eslint-disable-next-line camelcase
33
- {
34
- selector,
35
- metadata_hash: functionMetadataHash,
36
- vk_hash: vkHash
37
- }).with({
38
- capsules: [
39
- new Capsule(ProtocolContractAddress.ContractClassRegistry, new Fr(CONTRACT_CLASS_REGISTRY_BYTECODE_CAPSULE_SLOT), bytecode)
40
- ]
41
- });
42
- }
43
- /**
44
- * Sets up a call to broadcast a utility function's bytecode via the ClassRegistry contract.
45
- * Note that this is not required for users to call the function, but is rather a convenience to make
46
- * this code publicly available so dapps or wallets do not need to redistribute it.
47
- * @param wallet - Wallet to send the transaction.
48
- * @param artifact - Contract artifact that contains the function to be broadcast.
49
- * @param selector - Selector of the function to be broadcast.
50
- * @returns A ContractFunctionInteraction object that can be used to send the transaction.
51
- */ export async function broadcastUtilityFunction(wallet, artifact, selector) {
52
- const contractClass = await getContractClassFromArtifact(artifact);
53
- const utilityFunctions = artifact.functions.filter((fn)=>fn.functionType === FunctionType.UTILITY);
54
- const utilityFunctionsAndSelectors = await Promise.all(utilityFunctions.map(async (fn)=>({
55
- f: fn,
56
- selector: await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters)
57
- })));
58
- const utilityFunctionArtifact = utilityFunctionsAndSelectors.find((fn)=>selector.equals(fn.selector))?.f;
59
- if (!utilityFunctionArtifact) {
60
- throw new Error(`Utility function with selector ${selector.toString()} not found`);
61
- }
62
- const { artifactMetadataHash, artifactTreeLeafIndex, artifactTreeSiblingPath, functionMetadataHash, privateFunctionsArtifactTreeRoot } = await createUtilityFunctionMembershipProof(selector, artifact);
63
- const classRegistry = ContractClassRegistryContract.at(wallet);
64
- const bytecode = bufferAsFields(utilityFunctionArtifact.bytecode, MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS);
65
- return classRegistry.methods.broadcast_utility_function(contractClass.id, artifactMetadataHash, privateFunctionsArtifactTreeRoot, padArrayEnd(artifactTreeSiblingPath, Fr.ZERO, ARTIFACT_FUNCTION_TREE_MAX_HEIGHT), artifactTreeLeafIndex, // eslint-disable-next-line camelcase
66
- {
67
- selector,
68
- metadata_hash: functionMetadataHash
69
- }).with({
70
- capsules: [
71
- new Capsule(ProtocolContractAddress.ContractClassRegistry, new Fr(CONTRACT_CLASS_REGISTRY_BYTECODE_CAPSULE_SLOT), bytecode)
72
- ]
73
- });
74
- }
@@ -1,75 +0,0 @@
1
- import { createLogger } from '@aztec/foundation/log';
2
- import type { FieldsOf } from '@aztec/foundation/types';
3
- import type { ContractInstanceWithAddress } from '@aztec/stdlib/contract';
4
- import type { TxHash, TxReceipt } from '@aztec/stdlib/tx';
5
-
6
- import type { Wallet } from '../wallet/wallet.js';
7
- import type { ContractBase } from './contract_base.js';
8
- import { SentTx, type WaitOpts } from './sent_tx.js';
9
-
10
- /** Options related to waiting for a deployment tx. */
11
- export type DeployedWaitOpts = WaitOpts & {
12
- /** Wallet to use for creating a contract instance. Uses the one set in the deployer constructor if not set. */
13
- wallet?: Wallet;
14
- };
15
-
16
- /** Extends a transaction receipt with a contract instance that represents the newly deployed contract. */
17
- export type DeployTxReceipt<TContract extends ContractBase = ContractBase> = FieldsOf<TxReceipt> & {
18
- /** Instance of the newly deployed contract. */
19
- contract: TContract;
20
- /** The deployed contract instance with address and metadata. */
21
- instance: ContractInstanceWithAddress;
22
- };
23
-
24
- /**
25
- * A contract deployment transaction sent to the network, extending SentTx with methods to publish a contract instance.
26
- */
27
- export class DeploySentTx<TContract extends ContractBase = ContractBase> extends SentTx {
28
- private log = createLogger('aztecjs:deploy_sent_tx');
29
-
30
- constructor(
31
- wallet: Wallet,
32
- sendTx: () => Promise<TxHash>,
33
- private postDeployCtor: (instance: ContractInstanceWithAddress, wallet: Wallet) => TContract,
34
- /** A getter for the deployed contract instance */
35
- private instanceGetter: () => Promise<ContractInstanceWithAddress>,
36
- ) {
37
- super(wallet, sendTx);
38
- }
39
-
40
- /**
41
- * Returns the contract instance for this deployment.
42
- * @returns The deployed contract instance with address and metadata.
43
- */
44
- public async getInstance(): Promise<ContractInstanceWithAddress> {
45
- return await this.instanceGetter();
46
- }
47
-
48
- /**
49
- * Awaits for the tx to be mined and returns the contract instance. Throws if tx is not mined.
50
- * @param opts - Options for configuring the waiting for the tx to be mined.
51
- * @returns The deployed contract instance.
52
- */
53
- public async deployed(opts?: DeployedWaitOpts): Promise<TContract> {
54
- const receipt = await this.wait(opts);
55
- this.log.info(`Contract ${receipt.instance.address.toString()} successfully deployed.`);
56
- return receipt.contract;
57
- }
58
-
59
- /**
60
- * Awaits for the tx to be mined and returns the receipt along with a contract instance. Throws if tx is not mined.
61
- * @param opts - Options for configuring the waiting for the tx to be mined.
62
- * @returns The transaction receipt with the deployed contract instance.
63
- */
64
- public override async wait(opts?: DeployedWaitOpts): Promise<DeployTxReceipt<TContract>> {
65
- const receipt = await super.wait(opts);
66
- // In the case of DeploySentTx we have a guarantee that this.walletOrNode is a Wallet so we can cast it to Wallet.
67
- const contractWallet = opts?.wallet ?? (this.walletOrNode as Wallet);
68
- if (!contractWallet) {
69
- throw new Error(`A wallet is required for creating a contract instance`);
70
- }
71
- const instance = await this.instanceGetter();
72
- const contract = this.postDeployCtor(instance, contractWallet) as TContract;
73
- return { ...receipt, contract, instance };
74
- }
75
- }
@@ -1,129 +0,0 @@
1
- import { promiseWithResolvers } from '@aztec/foundation/promise';
2
- import { retryUntil } from '@aztec/foundation/retry';
3
- import type { AztecNode } from '@aztec/stdlib/interfaces/client';
4
- import { TxHash, type TxReceipt, TxStatus } from '@aztec/stdlib/tx';
5
-
6
- import type { Wallet } from '../wallet/wallet.js';
7
-
8
- /** Options related to waiting for a tx. */
9
- export type WaitOpts = {
10
- /** The amount of time to ignore TxStatus.DROPPED receipts (in seconds) due to the presumption that it is being propagated by the p2p network. Defaults to 5. */
11
- ignoreDroppedReceiptsFor?: number;
12
- /** The maximum time (in seconds) to wait for the transaction to be mined. Defaults to 60. */
13
- timeout?: number;
14
- /** The time interval (in seconds) between retries to fetch the transaction receipt. Defaults to 1. */
15
- interval?: number;
16
- /** Whether to accept a revert as a status code for the tx when waiting for it. If false, will throw if the tx reverts. */
17
- dontThrowOnRevert?: boolean;
18
- };
19
-
20
- export const DefaultWaitOpts: WaitOpts = {
21
- ignoreDroppedReceiptsFor: 5,
22
- timeout: 300,
23
- interval: 1,
24
- };
25
-
26
- /**
27
- * The SentTx class represents a sent transaction through the PXE (or directly to a node) providing methods to fetch
28
- * its hash, receipt, and mining status.
29
- */
30
- export class SentTx {
31
- protected sendTxPromise: Promise<void>;
32
- protected sendTxError?: Error;
33
- protected txHash?: TxHash;
34
-
35
- constructor(
36
- protected walletOrNode: Wallet | AztecNode,
37
- sendTx: () => Promise<TxHash>,
38
- ) {
39
- const { promise, resolve } = promiseWithResolvers<void>();
40
- this.sendTxPromise = promise;
41
- sendTx()
42
- .then(txHash => {
43
- this.txHash = txHash;
44
- resolve();
45
- })
46
- .catch(err => {
47
- this.sendTxError = err;
48
- // Calling resolve instead of reject here because we want to throw the error when getTxHash is called.
49
- resolve();
50
- });
51
- }
52
-
53
- /**
54
- * Retrieves the transaction hash of the SentTx instance.
55
- * The function internally awaits for the 'txHashPromise' to resolve, and then returns the resolved transaction hash.
56
- *
57
- * @returns A promise that resolves to the transaction hash of the SentTx instance.
58
- * TODO(#7717): Don't throw here.
59
- */
60
- public async getTxHash(): Promise<TxHash> {
61
- // Make sure sendTx has been resolved, which can be triggered when it returns a txHash or when it throws an error.
62
- await this.sendTxPromise;
63
-
64
- // If sendTx threw an error, throw it.
65
- if (this.sendTxError) {
66
- throw this.sendTxError;
67
- }
68
-
69
- // sendTx returned a txHash if it's been resolved and no error was set.
70
- return Promise.resolve(this.txHash!);
71
- }
72
-
73
- /**
74
- * Retrieve the transaction receipt associated with the current SentTx instance.
75
- * The function fetches the transaction hash using 'getTxHash' and then queries
76
- * the PXE to get the corresponding transaction receipt.
77
- *
78
- * @returns A promise that resolves to a TxReceipt object representing the fetched transaction receipt.
79
- */
80
- public async getReceipt(): Promise<TxReceipt> {
81
- const txHash = await this.getTxHash();
82
- return await this.walletOrNode.getTxReceipt(txHash);
83
- }
84
-
85
- /**
86
- * Awaits for a tx to be mined and returns the receipt. Throws if tx is not mined.
87
- * @param opts - Options for configuring the waiting for the tx to be mined.
88
- * @returns The transaction receipt.
89
- */
90
- public async wait(opts?: WaitOpts): Promise<TxReceipt> {
91
- const receipt = await this.waitForReceipt(opts);
92
- if (receipt.status !== TxStatus.SUCCESS && !opts?.dontThrowOnRevert) {
93
- throw new Error(
94
- `Transaction ${(await this.getTxHash()).toString()} was ${receipt.status}. Reason: ${receipt.error ?? 'unknown'}`,
95
- );
96
- }
97
- return receipt;
98
- }
99
-
100
- protected async waitForReceipt(opts?: WaitOpts): Promise<TxReceipt> {
101
- const txHash = await this.getTxHash();
102
- const startTime = Date.now();
103
- const ignoreDroppedReceiptsFor = opts?.ignoreDroppedReceiptsFor ?? DefaultWaitOpts.ignoreDroppedReceiptsFor;
104
-
105
- return await retryUntil(
106
- async () => {
107
- const txReceipt = await this.walletOrNode.getTxReceipt(txHash);
108
- // If receipt is not yet available, try again
109
- if (txReceipt.status === TxStatus.PENDING) {
110
- return undefined;
111
- }
112
- // If the tx was "dropped", either return it or ignore based on timing.
113
- // We can ignore it at first because the transaction may have been sent to node 1, and now we're asking node 2 for the receipt.
114
- // If we don't allow a short grace period, we could incorrectly return a TxReceipt with status DROPPED.
115
- if (txReceipt.status === TxStatus.DROPPED) {
116
- const elapsedSeconds = (Date.now() - startTime) / 1000;
117
- if (!ignoreDroppedReceiptsFor || elapsedSeconds > ignoreDroppedReceiptsFor) {
118
- return txReceipt;
119
- }
120
- return undefined;
121
- }
122
- return txReceipt;
123
- },
124
- 'isMined',
125
- opts?.timeout ?? DefaultWaitOpts.timeout,
126
- opts?.interval ?? DefaultWaitOpts.interval,
127
- );
128
- }
129
- }
@@ -1,148 +0,0 @@
1
- import {
2
- ARTIFACT_FUNCTION_TREE_MAX_HEIGHT,
3
- CONTRACT_CLASS_REGISTRY_BYTECODE_CAPSULE_SLOT,
4
- MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS,
5
- } from '@aztec/constants';
6
- import { padArrayEnd } from '@aztec/foundation/collection';
7
- import { Fr } from '@aztec/foundation/curves/bn254';
8
- import { ProtocolContractAddress } from '@aztec/protocol-contracts';
9
- import { type ContractArtifact, FunctionSelector, FunctionType, bufferAsFields } from '@aztec/stdlib/abi';
10
- import {
11
- computeVerificationKeyHash,
12
- createPrivateFunctionMembershipProof,
13
- createUtilityFunctionMembershipProof,
14
- getContractClassFromArtifact,
15
- } from '@aztec/stdlib/contract';
16
- import { Capsule } from '@aztec/stdlib/tx';
17
-
18
- import type { ContractFunctionInteraction } from '../contract/contract_function_interaction.js';
19
- import { ContractClassRegistryContract } from '../contract/protocol_contracts/contract-class-registry.js';
20
- import type { Wallet } from '../wallet/index.js';
21
-
22
- /**
23
- * Sets up a call to broadcast a private function's bytecode via the ClassRegistry contract.
24
- * Note that this is not required for users to call the function, but is rather a convenience to make
25
- * this code publicly available so dapps or wallets do not need to redistribute it.
26
- * @param wallet - Wallet to send the transaction.
27
- * @param artifact - Contract artifact that contains the function to be broadcast.
28
- * @param selector - Selector of the function to be broadcast.
29
- * @returns A ContractFunctionInteraction object that can be used to send the transaction.
30
- */
31
- export async function broadcastPrivateFunction(
32
- wallet: Wallet,
33
- artifact: ContractArtifact,
34
- selector: FunctionSelector,
35
- ): Promise<ContractFunctionInteraction> {
36
- const contractClass = await getContractClassFromArtifact(artifact);
37
- const privateFunctions = artifact.functions.filter(fn => fn.functionType === FunctionType.PRIVATE);
38
- const functionsAndSelectors = await Promise.all(
39
- privateFunctions.map(async fn => ({
40
- f: fn,
41
- selector: await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters),
42
- })),
43
- );
44
- const privateFunctionArtifact = functionsAndSelectors.find(fn => selector.equals(fn.selector))?.f;
45
- if (!privateFunctionArtifact) {
46
- throw new Error(`Private function with selector ${selector.toString()} not found`);
47
- }
48
-
49
- const {
50
- artifactTreeSiblingPath,
51
- artifactTreeLeafIndex,
52
- artifactMetadataHash,
53
- functionMetadataHash,
54
- utilityFunctionsTreeRoot,
55
- privateFunctionTreeSiblingPath,
56
- privateFunctionTreeLeafIndex,
57
- } = await createPrivateFunctionMembershipProof(selector, artifact);
58
-
59
- const vkHash = await computeVerificationKeyHash(privateFunctionArtifact);
60
-
61
- const classRegistry = ContractClassRegistryContract.at(wallet);
62
- const bytecode = bufferAsFields(
63
- privateFunctionArtifact.bytecode,
64
- MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS,
65
- );
66
- return classRegistry.methods
67
- .broadcast_private_function(
68
- contractClass.id,
69
- artifactMetadataHash,
70
- utilityFunctionsTreeRoot,
71
- privateFunctionTreeSiblingPath,
72
- privateFunctionTreeLeafIndex,
73
- padArrayEnd(artifactTreeSiblingPath, Fr.ZERO, ARTIFACT_FUNCTION_TREE_MAX_HEIGHT),
74
- artifactTreeLeafIndex,
75
- // eslint-disable-next-line camelcase
76
- { selector, metadata_hash: functionMetadataHash, vk_hash: vkHash },
77
- )
78
- .with({
79
- capsules: [
80
- new Capsule(
81
- ProtocolContractAddress.ContractClassRegistry,
82
- new Fr(CONTRACT_CLASS_REGISTRY_BYTECODE_CAPSULE_SLOT),
83
- bytecode,
84
- ),
85
- ],
86
- });
87
- }
88
-
89
- /**
90
- * Sets up a call to broadcast a utility function's bytecode via the ClassRegistry contract.
91
- * Note that this is not required for users to call the function, but is rather a convenience to make
92
- * this code publicly available so dapps or wallets do not need to redistribute it.
93
- * @param wallet - Wallet to send the transaction.
94
- * @param artifact - Contract artifact that contains the function to be broadcast.
95
- * @param selector - Selector of the function to be broadcast.
96
- * @returns A ContractFunctionInteraction object that can be used to send the transaction.
97
- */
98
- export async function broadcastUtilityFunction(
99
- wallet: Wallet,
100
- artifact: ContractArtifact,
101
- selector: FunctionSelector,
102
- ): Promise<ContractFunctionInteraction> {
103
- const contractClass = await getContractClassFromArtifact(artifact);
104
- const utilityFunctions = artifact.functions.filter(fn => fn.functionType === FunctionType.UTILITY);
105
- const utilityFunctionsAndSelectors = await Promise.all(
106
- utilityFunctions.map(async fn => ({
107
- f: fn,
108
- selector: await FunctionSelector.fromNameAndParameters(fn.name, fn.parameters),
109
- })),
110
- );
111
- const utilityFunctionArtifact = utilityFunctionsAndSelectors.find(fn => selector.equals(fn.selector))?.f;
112
- if (!utilityFunctionArtifact) {
113
- throw new Error(`Utility function with selector ${selector.toString()} not found`);
114
- }
115
-
116
- const {
117
- artifactMetadataHash,
118
- artifactTreeLeafIndex,
119
- artifactTreeSiblingPath,
120
- functionMetadataHash,
121
- privateFunctionsArtifactTreeRoot,
122
- } = await createUtilityFunctionMembershipProof(selector, artifact);
123
-
124
- const classRegistry = ContractClassRegistryContract.at(wallet);
125
- const bytecode = bufferAsFields(
126
- utilityFunctionArtifact.bytecode,
127
- MAX_PACKED_BYTECODE_SIZE_PER_PRIVATE_FUNCTION_IN_FIELDS,
128
- );
129
- return classRegistry.methods
130
- .broadcast_utility_function(
131
- contractClass.id,
132
- artifactMetadataHash,
133
- privateFunctionsArtifactTreeRoot,
134
- padArrayEnd(artifactTreeSiblingPath, Fr.ZERO, ARTIFACT_FUNCTION_TREE_MAX_HEIGHT),
135
- artifactTreeLeafIndex,
136
- // eslint-disable-next-line camelcase
137
- { selector, metadata_hash: functionMetadataHash },
138
- )
139
- .with({
140
- capsules: [
141
- new Capsule(
142
- ProtocolContractAddress.ContractClassRegistry,
143
- new Fr(CONTRACT_CLASS_REGISTRY_BYTECODE_CAPSULE_SLOT),
144
- bytecode,
145
- ),
146
- ],
147
- });
148
- }