@aztec/aztec.js 0.1.0-alpha29 → 0.1.0-alpha31

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 (68) hide show
  1. package/.tsbuildinfo +1 -1
  2. package/dest/account_impl/account_collection.d.ts +4 -4
  3. package/dest/account_impl/account_collection.d.ts.map +1 -1
  4. package/dest/account_impl/account_collection.js +4 -5
  5. package/dest/account_impl/entrypoint_payload.d.ts +2 -2
  6. package/dest/account_impl/entrypoint_payload.d.ts.map +1 -1
  7. package/dest/account_impl/entrypoint_payload.js +4 -4
  8. package/dest/account_impl/index.d.ts +11 -6
  9. package/dest/account_impl/index.d.ts.map +1 -1
  10. package/dest/account_impl/index.js +2 -2
  11. package/dest/account_impl/single_key_account_contract.d.ts +7 -7
  12. package/dest/account_impl/single_key_account_contract.d.ts.map +1 -1
  13. package/dest/account_impl/single_key_account_contract.js +11 -18
  14. package/dest/account_impl/stored_key_account_contract.d.ts +7 -7
  15. package/dest/account_impl/stored_key_account_contract.d.ts.map +1 -1
  16. package/dest/account_impl/stored_key_account_contract.js +11 -18
  17. package/dest/aztec_rpc_client/wallet.d.ts +6 -5
  18. package/dest/aztec_rpc_client/wallet.d.ts.map +1 -1
  19. package/dest/aztec_rpc_client/wallet.js +6 -3
  20. package/dest/contract/base_contract_interaction.d.ts +46 -0
  21. package/dest/contract/base_contract_interaction.d.ts.map +1 -0
  22. package/dest/contract/base_contract_interaction.js +37 -0
  23. package/dest/contract/batch_call.d.ts +16 -0
  24. package/dest/contract/batch_call.d.ts.map +1 -0
  25. package/dest/contract/batch_call.js +22 -0
  26. package/dest/contract/contract.test.js +6 -7
  27. package/dest/contract/contract_function_interaction.d.ts +10 -47
  28. package/dest/contract/contract_function_interaction.d.ts.map +1 -1
  29. package/dest/contract/contract_function_interaction.js +14 -59
  30. package/dest/contract/index.d.ts +1 -0
  31. package/dest/contract/index.d.ts.map +1 -1
  32. package/dest/contract/index.js +2 -1
  33. package/dest/contract/sent_tx.d.ts +7 -2
  34. package/dest/contract/sent_tx.d.ts.map +1 -1
  35. package/dest/contract/sent_tx.js +19 -3
  36. package/dest/contract/sent_tx.test.d.ts +2 -0
  37. package/dest/contract/sent_tx.test.d.ts.map +1 -0
  38. package/dest/contract/sent_tx.test.js +42 -0
  39. package/dest/contract_deployer/deploy_method.d.ts +8 -15
  40. package/dest/contract_deployer/deploy_method.d.ts.map +1 -1
  41. package/dest/contract_deployer/deploy_method.js +28 -53
  42. package/dest/contract_deployer/deploy_sent_tx.js +2 -2
  43. package/dest/index.d.ts +1 -1
  44. package/dest/index.d.ts.map +1 -1
  45. package/dest/index.js +2 -2
  46. package/dest/main.js +1 -1
  47. package/dest/main.js.LICENSE.txt +0 -2
  48. package/dest/utils/defaults.d.ts +5 -0
  49. package/dest/utils/defaults.d.ts.map +1 -0
  50. package/dest/utils/defaults.js +5 -0
  51. package/package.json +5 -4
  52. package/src/account_impl/account_collection.ts +8 -9
  53. package/src/account_impl/entrypoint_payload.ts +5 -5
  54. package/src/account_impl/index.ts +12 -6
  55. package/src/account_impl/single_key_account_contract.ts +12 -23
  56. package/src/account_impl/stored_key_account_contract.ts +17 -24
  57. package/src/aztec_rpc_client/wallet.ts +12 -9
  58. package/src/contract/base_contract_interaction.ts +62 -0
  59. package/src/contract/batch_call.ts +22 -0
  60. package/src/contract/contract.test.ts +5 -8
  61. package/src/contract/contract_function_interaction.ts +15 -82
  62. package/src/contract/index.ts +1 -0
  63. package/src/contract/sent_tx.test.ts +52 -0
  64. package/src/contract/sent_tx.ts +23 -4
  65. package/src/contract_deployer/deploy_method.ts +31 -62
  66. package/src/contract_deployer/deploy_sent_tx.ts +1 -1
  67. package/src/index.ts +2 -2
  68. package/src/utils/defaults.ts +4 -0
@@ -7,6 +7,4 @@
7
7
 
8
8
  /*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
9
9
 
10
- /*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) */
11
-
12
10
  /*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
@@ -0,0 +1,5 @@
1
+ /** Default L1 chain ID to use when constructing txs (matches hardhat and anvil's default). */
2
+ export declare const DEFAULT_CHAIN_ID = 31337;
3
+ /** Default protocol version to use. */
4
+ export declare const DEFAULT_VERSION = 1;
5
+ //# sourceMappingURL=defaults.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"defaults.d.ts","sourceRoot":"","sources":["../../src/utils/defaults.ts"],"names":[],"mappings":"AAAA,8FAA8F;AAC9F,eAAO,MAAM,gBAAgB,QAAQ,CAAC;AACtC,uCAAuC;AACvC,eAAO,MAAM,eAAe,IAAI,CAAC"}
@@ -0,0 +1,5 @@
1
+ /** Default L1 chain ID to use when constructing txs (matches hardhat and anvil's default). */
2
+ export const DEFAULT_CHAIN_ID = 31337;
3
+ /** Default protocol version to use. */
4
+ export const DEFAULT_VERSION = 1;
5
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGVmYXVsdHMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvdXRpbHMvZGVmYXVsdHMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsOEZBQThGO0FBQzlGLE1BQU0sQ0FBQyxNQUFNLGdCQUFnQixHQUFHLEtBQUssQ0FBQztBQUN0Qyx1Q0FBdUM7QUFDdkMsTUFBTSxDQUFDLE1BQU0sZUFBZSxHQUFHLENBQUMsQ0FBQyJ9
package/package.json CHANGED
@@ -1,15 +1,16 @@
1
1
  {
2
2
  "name": "@aztec/aztec.js",
3
- "version": "0.1.0-alpha29",
3
+ "version": "0.1.0-alpha31",
4
4
  "exports": {
5
5
  "node": "./dest/index.js",
6
6
  "default": "./dest/main.js"
7
7
  },
8
8
  "type": "module",
9
9
  "dependencies": {
10
- "@aztec/circuits.js": "0.1.0-alpha29",
11
- "@aztec/foundation": "0.1.0-alpha29",
12
- "@aztec/types": "0.1.0-alpha29",
10
+ "@aztec/circuits.js": "0.1.0-alpha31",
11
+ "@aztec/foundation": "0.1.0-alpha31",
12
+ "@aztec/types": "0.1.0-alpha31",
13
+ "lodash.every": "^4.6.0",
13
14
  "lodash.partition": "^4.6.0",
14
15
  "tslib": "^2.4.0"
15
16
  }
@@ -1,7 +1,7 @@
1
- import { AztecAddress, TxContext } from '@aztec/circuits.js';
2
- import { ExecutionRequest, TxExecutionRequest } from '@aztec/types';
1
+ import { AztecAddress } from '@aztec/circuits.js';
2
+ import { FunctionCall, TxExecutionRequest } from '@aztec/types';
3
3
 
4
- import { AccountImplementation } from './index.js';
4
+ import { AccountImplementation, CreateTxRequestOpts } from './index.js';
5
5
 
6
6
  /**
7
7
  * A concrete account implementation that manages multiple accounts.
@@ -23,14 +23,13 @@ export class AccountCollection implements AccountImplementation {
23
23
  return AztecAddress.fromString(this.accounts.keys().next().value as string);
24
24
  }
25
25
 
26
- public createAuthenticatedTxRequest(
27
- executions: ExecutionRequest[],
28
- txContext: TxContext,
26
+ public createTxExecutionRequest(
27
+ executions: FunctionCall[],
28
+ opts: CreateTxRequestOpts = {},
29
29
  ): Promise<TxExecutionRequest> {
30
- // TODO: Check all executions have the same origin
31
- const sender = executions[0].from;
30
+ const sender = opts.origin ?? this.getAddress();
32
31
  const impl = this.accounts.get(sender.toString());
33
32
  if (!impl) throw new Error(`No account implementation registered for ${sender}`);
34
- return impl.createAuthenticatedTxRequest(executions, txContext);
33
+ return impl.createTxExecutionRequest(executions, opts);
35
34
  }
36
35
  }
@@ -1,7 +1,7 @@
1
1
  import { CircuitsWasm, Fr } from '@aztec/circuits.js';
2
2
  import { padArrayEnd } from '@aztec/foundation/collection';
3
3
  import { sha256 } from '@aztec/foundation/crypto';
4
- import { ExecutionRequest, PackedArguments, emptyExecutionRequest } from '@aztec/types';
4
+ import { FunctionCall, PackedArguments, emptyFunctionCall } from '@aztec/types';
5
5
 
6
6
  // These must match the values defined in yarn-project/noir-libs/noir-aztec/src/entrypoint.nr
7
7
  const ACCOUNT_MAX_PRIVATE_CALLS = 2;
@@ -24,8 +24,8 @@ export type EntrypointPayload = {
24
24
 
25
25
  /** Assembles an entrypoint payload from a set of private and public function calls */
26
26
  export async function buildPayload(
27
- privateCalls: ExecutionRequest[],
28
- publicCalls: ExecutionRequest[],
27
+ privateCalls: FunctionCall[],
28
+ publicCalls: FunctionCall[],
29
29
  ): Promise<{
30
30
  /** The payload for the entrypoint function */
31
31
  payload: EntrypointPayload;
@@ -35,8 +35,8 @@ export async function buildPayload(
35
35
  const nonce = Fr.random();
36
36
 
37
37
  const calls = [
38
- ...padArrayEnd(privateCalls, emptyExecutionRequest(), ACCOUNT_MAX_PRIVATE_CALLS),
39
- ...padArrayEnd(publicCalls, emptyExecutionRequest(), ACCOUNT_MAX_PUBLIC_CALLS),
38
+ ...padArrayEnd(privateCalls, emptyFunctionCall(), ACCOUNT_MAX_PRIVATE_CALLS),
39
+ ...padArrayEnd(publicCalls, emptyFunctionCall(), ACCOUNT_MAX_PUBLIC_CALLS),
40
40
  ];
41
41
 
42
42
  const packedArguments = [];
@@ -1,9 +1,15 @@
1
- import { AztecAddress, TxContext } from '@aztec/circuits.js';
2
- import { ExecutionRequest, TxExecutionRequest } from '@aztec/types';
1
+ import { AztecAddress } from '@aztec/circuits.js';
2
+ import { FunctionCall, TxExecutionRequest } from '@aztec/types';
3
3
 
4
+ export * from './account_collection.js';
4
5
  export * from './single_key_account_contract.js';
5
6
  export * from './stored_key_account_contract.js';
6
- export * from './account_collection.js';
7
+
8
+ /** Options for creating a tx request out of a set of function calls. */
9
+ export type CreateTxRequestOpts = {
10
+ /** Origin of the tx. Needs to be an address managed by this account. */
11
+ origin?: AztecAddress;
12
+ };
7
13
 
8
14
  /** Represents an implementation for a user account contract. Knows how to encode and sign a tx for that particular implementation. */
9
15
  export interface AccountImplementation {
@@ -15,9 +21,9 @@ export interface AccountImplementation {
15
21
 
16
22
  /**
17
23
  * Generates an authenticated request out of set of intents
18
- * @param executions - The execution intent to be authenticated.
19
- * @param txContext - The tx context under with the execution is to be made.
24
+ * @param executions - The execution intents to be run.
25
+ * @param opts - Options.
20
26
  * @returns The authenticated transaction execution request.
21
27
  */
22
- createAuthenticatedTxRequest(executions: ExecutionRequest[], txContext: TxContext): Promise<TxExecutionRequest>;
28
+ createTxExecutionRequest(executions: FunctionCall[], opts?: CreateTxRequestOpts): Promise<TxExecutionRequest>;
23
29
  }
@@ -8,14 +8,15 @@ import {
8
8
  } from '@aztec/circuits.js';
9
9
  import { Signer } from '@aztec/circuits.js/barretenberg';
10
10
  import { ContractAbi, encodeArguments } from '@aztec/foundation/abi';
11
- import { ExecutionRequest, PackedArguments, TxExecutionRequest } from '@aztec/types';
11
+ import { FunctionCall, PackedArguments, TxExecutionRequest } from '@aztec/types';
12
12
 
13
13
  import partition from 'lodash.partition';
14
14
 
15
15
  import SchnorrSingleKeyAccountContractAbi from '../abis/schnorr_single_key_account_contract.json' assert { type: 'json' };
16
16
  import { generatePublicKey } from '../index.js';
17
+ import { DEFAULT_CHAIN_ID, DEFAULT_VERSION } from '../utils/defaults.js';
17
18
  import { buildPayload, hashPayload } from './entrypoint_payload.js';
18
- import { AccountImplementation } from './index.js';
19
+ import { AccountImplementation, CreateTxRequestOpts } from './index.js';
19
20
 
20
21
  /**
21
22
  * Account contract implementation that uses a single key for signing and encryption. This public key is not
@@ -28,18 +29,21 @@ export class SingleKeyAccountContract implements AccountImplementation {
28
29
  private partialContractAddress: PartialContractAddress,
29
30
  private privateKey: PrivateKey,
30
31
  private signer: Signer,
32
+ private chainId: number = DEFAULT_CHAIN_ID,
33
+ private version: number = DEFAULT_VERSION,
31
34
  ) {}
32
35
 
33
36
  getAddress(): AztecAddress {
34
37
  return this.address;
35
38
  }
36
39
 
37
- async createAuthenticatedTxRequest(
38
- executions: ExecutionRequest[],
39
- txContext: TxContext,
40
+ async createTxExecutionRequest(
41
+ executions: FunctionCall[],
42
+ opts: CreateTxRequestOpts = {},
40
43
  ): Promise<TxExecutionRequest> {
41
- this.checkSender(executions);
42
- this.checkIsNotDeployment(txContext);
44
+ if (opts.origin && !opts.origin.equals(this.address)) {
45
+ throw new Error(`Sender ${opts.origin.toString()} does not match account address ${this.address.toString()}`);
46
+ }
43
47
 
44
48
  const [privateCalls, publicCalls] = partition(executions, exec => exec.functionData.isPrivate);
45
49
  const wasm = await CircuitsWasm.get();
@@ -55,7 +59,7 @@ export class SingleKeyAccountContract implements AccountImplementation {
55
59
  argsHash: packedArgs.hash,
56
60
  origin: this.address,
57
61
  functionData: FunctionData.fromAbi(abi),
58
- txContext,
62
+ txContext: TxContext.empty(this.chainId, this.version),
59
63
  packedArguments: [...callsPackedArguments, packedArgs],
60
64
  });
61
65
 
@@ -70,19 +74,4 @@ export class SingleKeyAccountContract implements AccountImplementation {
70
74
  if (!abi) throw new Error(`Entrypoint abi for account contract not found`);
71
75
  return abi;
72
76
  }
73
-
74
- private checkIsNotDeployment(txContext: TxContext) {
75
- if (txContext.isContractDeploymentTx) {
76
- throw new Error(`Cannot yet deploy contracts from an account contract`);
77
- }
78
- }
79
-
80
- private checkSender(executions: ExecutionRequest[]) {
81
- const wrongSender = executions.find(e => !e.from.equals(this.address));
82
- if (wrongSender) {
83
- throw new Error(
84
- `Sender ${wrongSender.from.toString()} does not match account address ${this.address.toString()}`,
85
- );
86
- }
87
- }
88
77
  }
@@ -2,13 +2,14 @@ import { AztecAddress, CircuitsWasm, FunctionData, PrivateKey, TxContext } from
2
2
  import { Signer } from '@aztec/circuits.js/barretenberg';
3
3
  import { ContractAbi, encodeArguments } from '@aztec/foundation/abi';
4
4
  import { DebugLogger, createDebugLogger } from '@aztec/foundation/log';
5
- import { ExecutionRequest, PackedArguments, TxExecutionRequest } from '@aztec/types';
5
+ import { FunctionCall, PackedArguments, TxExecutionRequest } from '@aztec/types';
6
6
 
7
7
  import partition from 'lodash.partition';
8
8
 
9
9
  import EcdsaAccountContractAbi from '../abis/ecdsa_account_contract.json' assert { type: 'json' };
10
+ import { DEFAULT_CHAIN_ID, DEFAULT_VERSION } from '../utils/defaults.js';
10
11
  import { buildPayload, hashPayload } from './entrypoint_payload.js';
11
- import { AccountImplementation } from './index.js';
12
+ import { AccountImplementation, CreateTxRequestOpts } from './index.js';
12
13
 
13
14
  /**
14
15
  * Account contract implementation that keeps a signing public key in storage, and is retrieved on
@@ -17,7 +18,13 @@ import { AccountImplementation } from './index.js';
17
18
  export class StoredKeyAccountContract implements AccountImplementation {
18
19
  private log: DebugLogger;
19
20
 
20
- constructor(private address: AztecAddress, private privateKey: PrivateKey, private signer: Signer) {
21
+ constructor(
22
+ private address: AztecAddress,
23
+ private privateKey: PrivateKey,
24
+ private signer: Signer,
25
+ private chainId: number = DEFAULT_CHAIN_ID,
26
+ private version: number = DEFAULT_VERSION,
27
+ ) {
21
28
  this.log = createDebugLogger('aztec:client:accounts:stored_key');
22
29
  }
23
30
 
@@ -25,12 +32,13 @@ export class StoredKeyAccountContract implements AccountImplementation {
25
32
  return this.address;
26
33
  }
27
34
 
28
- async createAuthenticatedTxRequest(
29
- executions: ExecutionRequest[],
30
- txContext: TxContext,
35
+ async createTxExecutionRequest(
36
+ executions: FunctionCall[],
37
+ opts: CreateTxRequestOpts = {},
31
38
  ): Promise<TxExecutionRequest> {
32
- this.checkSender(executions);
33
- this.checkIsNotDeployment(txContext);
39
+ if (opts.origin && !opts.origin.equals(this.address)) {
40
+ throw new Error(`Sender ${opts.origin.toString()} does not match account address ${this.address.toString()}`);
41
+ }
34
42
 
35
43
  const [privateCalls, publicCalls] = partition(executions, exec => exec.functionData.isPrivate);
36
44
  const wasm = await CircuitsWasm.get();
@@ -46,7 +54,7 @@ export class StoredKeyAccountContract implements AccountImplementation {
46
54
  argsHash: packedArgs.hash,
47
55
  origin: this.address,
48
56
  functionData: FunctionData.fromAbi(abi),
49
- txContext,
57
+ txContext: TxContext.empty(this.chainId, this.version),
50
58
  packedArguments: [...callsPackedArguments, packedArgs],
51
59
  });
52
60
 
@@ -61,19 +69,4 @@ export class StoredKeyAccountContract implements AccountImplementation {
61
69
  if (!abi) throw new Error(`Entrypoint abi for account contract not found`);
62
70
  return abi;
63
71
  }
64
-
65
- private checkIsNotDeployment(txContext: TxContext) {
66
- if (txContext.isContractDeploymentTx) {
67
- throw new Error(`Cannot yet deploy contracts from an account contract`);
68
- }
69
- }
70
-
71
- private checkSender(executions: ExecutionRequest[]) {
72
- const wrongSender = executions.find(e => !e.from.equals(this.address));
73
- if (wrongSender) {
74
- throw new Error(
75
- `Sender ${wrongSender.from.toString()} does not match account address ${this.address.toString()}`,
76
- );
77
- }
78
- }
79
72
  }
@@ -1,19 +1,20 @@
1
- import { AztecAddress, Fr, PartialContractAddress, PrivateKey, PublicKey, TxContext } from '@aztec/circuits.js';
1
+ import { AztecAddress, Fr, PartialContractAddress, PrivateKey, PublicKey } from '@aztec/circuits.js';
2
2
  import {
3
3
  AztecRPC,
4
4
  ContractData,
5
5
  ContractPublicData,
6
6
  DeployedContract,
7
- ExecutionRequest,
7
+ FunctionCall,
8
8
  L2BlockL2Logs,
9
9
  NodeInfo,
10
+ SyncStatus,
10
11
  Tx,
11
12
  TxExecutionRequest,
12
13
  TxHash,
13
14
  TxReceipt,
14
15
  } from '@aztec/types';
15
16
 
16
- import { AccountImplementation } from '../account_impl/index.js';
17
+ import { AccountImplementation, CreateTxRequestOpts } from '../account_impl/index.js';
17
18
 
18
19
  /**
19
20
  * The wallet interface.
@@ -25,11 +26,10 @@ export type Wallet = AccountImplementation & AztecRPC;
25
26
  */
26
27
  export abstract class BaseWallet implements Wallet {
27
28
  constructor(protected readonly rpc: AztecRPC) {}
29
+
28
30
  abstract getAddress(): AztecAddress;
29
- abstract createAuthenticatedTxRequest(
30
- executions: ExecutionRequest[],
31
- txContext: TxContext,
32
- ): Promise<TxExecutionRequest>;
31
+ abstract createTxExecutionRequest(execs: FunctionCall[], opts?: CreateTxRequestOpts): Promise<TxExecutionRequest>;
32
+
33
33
  addAccount(privKey: PrivateKey, address: AztecAddress, partialContractAddress: Fr): Promise<AztecAddress> {
34
34
  return this.rpc.addAccount(privKey, address, partialContractAddress);
35
35
  }
@@ -94,6 +94,9 @@ export abstract class BaseWallet implements Wallet {
94
94
  isAccountSynchronised(account: AztecAddress) {
95
95
  return this.rpc.isAccountSynchronised(account);
96
96
  }
97
+ getSyncStatus(): Promise<SyncStatus> {
98
+ return this.rpc.getSyncStatus();
99
+ }
97
100
  }
98
101
 
99
102
  /**
@@ -106,7 +109,7 @@ export class AccountWallet extends BaseWallet {
106
109
  getAddress(): AztecAddress {
107
110
  return this.accountImpl.getAddress();
108
111
  }
109
- createAuthenticatedTxRequest(executions: ExecutionRequest[], txContext: TxContext): Promise<TxExecutionRequest> {
110
- return this.accountImpl.createAuthenticatedTxRequest(executions, txContext);
112
+ createTxExecutionRequest(executions: FunctionCall[], opts: CreateTxRequestOpts = {}): Promise<TxExecutionRequest> {
113
+ return this.accountImpl.createTxExecutionRequest(executions, opts);
111
114
  }
112
115
  }
@@ -0,0 +1,62 @@
1
+ import { AztecAddress } from '@aztec/circuits.js';
2
+ import { AztecRPC, Tx, TxExecutionRequest } from '@aztec/types';
3
+
4
+ import { SentTx } from './sent_tx.js';
5
+
6
+ /**
7
+ * Represents options for calling a (constrained) function in a contract.
8
+ * Allows the user to specify the sender address and nonce for a transaction.
9
+ */
10
+ export interface SendMethodOptions {
11
+ /**
12
+ * Sender's address initiating the transaction.
13
+ */
14
+ origin?: AztecAddress;
15
+ }
16
+
17
+ /**
18
+ * Base class for an interaction with a contract, be it a deployment, a function call, or a batch.
19
+ * Implements the sequence create/simulate/send.
20
+ */
21
+ export abstract class BaseContractInteraction {
22
+ protected tx?: Tx;
23
+ protected txRequest?: TxExecutionRequest;
24
+
25
+ constructor(protected rpc: AztecRPC) {}
26
+
27
+ /**
28
+ * Create a transaction execution request ready to be simulated.
29
+ * @param options - An optional object containing additional configuration for the transaction.
30
+ * @returns A transaction execution request.
31
+ */
32
+ public abstract create(options?: SendMethodOptions): Promise<TxExecutionRequest>;
33
+
34
+ /**
35
+ * Simulates a transaction execution request and returns a tx object ready to be sent.
36
+ * @param options - optional arguments to be used in the creation of the transaction
37
+ * @returns The resulting transaction
38
+ */
39
+ public async simulate(options: SendMethodOptions = {}): Promise<Tx> {
40
+ const txRequest = this.txRequest ?? (await this.create(options));
41
+ this.tx = await this.rpc.simulateTx(txRequest);
42
+ return this.tx;
43
+ }
44
+
45
+ /**
46
+ * Sends a transaction to the contract function with the specified options.
47
+ * This function throws an error if called on an unconstrained function.
48
+ * It creates and signs the transaction if necessary, and returns a SentTx instance,
49
+ * which can be used to track the transaction status, receipt, and events.
50
+ * @param options - An optional object containing 'from' property representing
51
+ * the AztecAddress of the sender. If not provided, the default address is used.
52
+ * @returns A SentTx instance for tracking the transaction status and information.
53
+ */
54
+ public send(options: SendMethodOptions = {}) {
55
+ const promise = (async () => {
56
+ const tx = this.tx ?? (await this.simulate(options));
57
+ return this.rpc.sendTx(tx);
58
+ })();
59
+
60
+ return new SentTx(this.rpc, promise);
61
+ }
62
+ }
@@ -0,0 +1,22 @@
1
+ import { FunctionCall, TxExecutionRequest, Wallet } from '../index.js';
2
+ import { BaseContractInteraction, SendMethodOptions } from './base_contract_interaction.js';
3
+
4
+ /** A batch of function calls to be sent as a single transaction through a wallet. */
5
+ export class BatchCall extends BaseContractInteraction {
6
+ constructor(protected wallet: Wallet, protected calls: FunctionCall[]) {
7
+ super(wallet);
8
+ }
9
+
10
+ /**
11
+ * Create a transaction execution request that represents this batch, encoded and authenticated by the
12
+ * user's wallet, ready to be simulated.
13
+ * @param options - An optional object containing additional configuration for the transaction.
14
+ * @returns A Promise that resolves to a transaction instance.
15
+ */
16
+ public async create(options?: SendMethodOptions | undefined): Promise<TxExecutionRequest> {
17
+ if (!this.txRequest) {
18
+ this.txRequest = await this.wallet.createTxExecutionRequest(this.calls, options);
19
+ }
20
+ return this.txRequest;
21
+ }
22
+ }
@@ -93,13 +93,14 @@ describe('Contract Class', () => {
93
93
 
94
94
  beforeEach(() => {
95
95
  wallet = mock<Wallet>();
96
- wallet.createAuthenticatedTxRequest.mockResolvedValue(mockTxRequest);
96
+ wallet.createTxExecutionRequest.mockResolvedValue(mockTxRequest);
97
97
  wallet.isContractDeployed.mockResolvedValue(true);
98
98
  wallet.sendTx.mockResolvedValue(mockTxHash);
99
99
  wallet.viewTx.mockResolvedValue(mockViewResultValue);
100
100
  wallet.getTxReceipt.mockResolvedValue(mockTxReceipt);
101
101
  wallet.getNodeInfo.mockResolvedValue(mockNodeInfo);
102
102
  wallet.simulateTx.mockResolvedValue(mockTx);
103
+ wallet.getAccounts.mockResolvedValue([account]);
103
104
  });
104
105
 
105
106
  it('should create and send a contract method tx', async () => {
@@ -114,7 +115,7 @@ describe('Contract Class', () => {
114
115
 
115
116
  expect(txHash).toBe(mockTxHash);
116
117
  expect(receipt).toBe(mockTxReceipt);
117
- expect(wallet.createAuthenticatedTxRequest).toHaveBeenCalledTimes(1);
118
+ expect(wallet.createTxExecutionRequest).toHaveBeenCalledTimes(1);
118
119
  expect(wallet.sendTx).toHaveBeenCalledTimes(1);
119
120
  expect(wallet.sendTx).toHaveBeenCalledWith(mockTx);
120
121
  });
@@ -129,13 +130,9 @@ describe('Contract Class', () => {
129
130
  expect(result).toBe(mockViewResultValue);
130
131
  });
131
132
 
132
- it('should not call send on an unconstrained function', async () => {
133
+ it('should not call create on an unconstrained function', async () => {
133
134
  const fooContract = await Contract.create(contractAddress, defaultAbi, wallet);
134
- expect(() =>
135
- fooContract.methods.qux().send({
136
- origin: account,
137
- }),
138
- ).toThrow();
135
+ await expect(fooContract.methods.qux().create({ origin: account })).rejects.toThrow();
139
136
  });
140
137
 
141
138
  it('should not call view on a secret or open function', async () => {
@@ -1,24 +1,11 @@
1
- import { AztecAddress, Fr, FunctionData, TxContext } from '@aztec/circuits.js';
1
+ import { AztecAddress, FunctionData } from '@aztec/circuits.js';
2
2
  import { FunctionAbi, FunctionType, encodeArguments } from '@aztec/foundation/abi';
3
- import { ExecutionRequest, Tx, TxExecutionRequest } from '@aztec/types';
3
+ import { FunctionCall, TxExecutionRequest } from '@aztec/types';
4
4
 
5
5
  import { Wallet } from '../aztec_rpc_client/wallet.js';
6
- import { SentTx } from './sent_tx.js';
6
+ import { BaseContractInteraction, SendMethodOptions } from './base_contract_interaction.js';
7
7
 
8
- /**
9
- * Represents options for calling a (constrained) function in a contract.
10
- * Allows the user to specify the sender address and nonce for a transaction.
11
- */
12
- export interface SendMethodOptions {
13
- /**
14
- * Sender's address initiating the transaction.
15
- */
16
- origin?: AztecAddress;
17
- /**
18
- * The nonce representing the order of transactions sent by the address.
19
- */
20
- nonce?: Fr;
21
- }
8
+ export { SendMethodOptions };
22
9
 
23
10
  /**
24
11
  * Represents the options for a view method in a contract function interaction.
@@ -33,29 +20,24 @@ export interface ViewMethodOptions {
33
20
 
34
21
  /**
35
22
  * This is the class that is returned when calling e.g. `contract.methods.myMethod(arg0, arg1)`.
36
- * It contains available interactions one can call on a method.
23
+ * It contains available interactions one can call on a method, including view.
37
24
  */
38
- export class ContractFunctionInteraction {
39
- protected tx?: Tx;
40
- protected txRequest?: TxExecutionRequest;
41
-
25
+ export class ContractFunctionInteraction extends BaseContractInteraction {
42
26
  constructor(
43
27
  protected wallet: Wallet,
44
28
  protected contractAddress: AztecAddress,
45
29
  protected functionDao: FunctionAbi,
46
30
  protected args: any[],
47
31
  ) {
32
+ super(wallet);
48
33
  if (args.some(arg => arg === undefined || arg === null)) {
49
34
  throw new Error('All function interaction arguments must be defined and not null. Received: ' + args);
50
35
  }
51
36
  }
52
37
 
53
38
  /**
54
- * Create an Aztec transaction instance by combining the transaction request and its signature.
55
- * This function will first check if a signature exists, and if not, it will call the `sign` method
56
- * to obtain the signature before creating the transaction. Throws an error if the function is
57
- * of unconstrained type or if the transaction request and signature are missing.
58
- *
39
+ * Create a transaction execution request that represents this call, encoded and authenticated by the
40
+ * user's wallet, ready to be simulated.
59
41
  * @param options - An optional object containing additional configuration for the transaction.
60
42
  * @returns A Promise that resolves to a transaction instance.
61
43
  */
@@ -64,76 +46,27 @@ export class ContractFunctionInteraction {
64
46
  throw new Error("Can't call `create` on an unconstrained function.");
65
47
  }
66
48
  if (!this.txRequest) {
67
- const executionRequest = this.getExecutionRequest(this.contractAddress, options.origin);
68
- const nodeInfo = await this.wallet.getNodeInfo();
69
- const txContext = TxContext.empty(new Fr(nodeInfo.chainId), new Fr(nodeInfo.version));
70
- const txRequest = await this.wallet.createAuthenticatedTxRequest([executionRequest], txContext);
71
- this.txRequest = txRequest;
49
+ this.txRequest = await this.wallet.createTxExecutionRequest([this.request()], options);
72
50
  }
73
51
  return this.txRequest;
74
52
  }
75
53
 
76
- /**
77
- * Simulates a transaction's execution.
78
- * @param options - optional arguments to be used in the creation of the transaction
79
- * @returns The resulting transaction
80
- */
81
- public async simulate(options: SendMethodOptions = {}): Promise<Tx> {
82
- const txRequest = this.txRequest ?? (await this.create(options));
83
- this.tx = await this.wallet.simulateTx(txRequest);
84
- return this.tx;
85
- }
86
-
87
54
  /**
88
55
  * Returns an execution request that represents this operation. Useful as a building
89
56
  * block for constructing batch requests.
90
57
  * @param options - An optional object containing additional configuration for the transaction.
91
- * @returns An execution request.
58
+ * @returns An execution request wrapped in promise.
92
59
  */
93
- public request(options: SendMethodOptions = {}): ExecutionRequest {
94
- return this.getExecutionRequest(this.contractAddress, options.origin);
95
- }
96
-
97
- protected getExecutionRequest(to: AztecAddress, from?: AztecAddress): ExecutionRequest {
98
- const flatArgs = encodeArguments(this.functionDao, this.args);
99
- from = from ?? this.wallet.getAddress();
100
-
101
- return {
102
- args: flatArgs,
103
- functionData: FunctionData.fromAbi(this.functionDao),
104
- to,
105
- from,
106
- };
107
- }
108
-
109
- /**
110
- * Sends a transaction to the contract function with the specified options.
111
- * This function throws an error if called on an unconstrained function.
112
- * It creates and signs the transaction if necessary, and returns a SentTx instance,
113
- * which can be used to track the transaction status, receipt, and events.
114
- *
115
- * @param options - An optional object containing 'from' property representing
116
- * the AztecAddress of the sender. If not provided, the default address is used.
117
- * @returns A SentTx instance for tracking the transaction status and information.
118
- */
119
- public send(options: SendMethodOptions = {}) {
120
- if (this.functionDao.functionType === FunctionType.UNCONSTRAINED) {
121
- throw new Error("Can't call `send` on an unconstrained function.");
122
- }
123
-
124
- const promise = (async () => {
125
- const tx = this.tx ?? (await this.simulate(options));
126
- return this.wallet.sendTx(tx);
127
- })();
128
-
129
- return new SentTx(this.wallet, promise);
60
+ public request(): FunctionCall {
61
+ const args = encodeArguments(this.functionDao, this.args);
62
+ const functionData = FunctionData.fromAbi(this.functionDao);
63
+ return { args, functionData, to: this.contractAddress };
130
64
  }
131
65
 
132
66
  /**
133
67
  * Execute a view (read-only) transaction on an unconstrained function.
134
68
  * This method is used to call functions that do not modify the contract state and only return data.
135
69
  * Throws an error if called on a non-unconstrained function.
136
- *
137
70
  * @param options - An optional object containing additional configuration for the transaction.
138
71
  * @returns The result of the view transaction as returned by the contract function.
139
72
  */
@@ -2,3 +2,4 @@ export * from './contract.js';
2
2
  export * from './contract_function_interaction.js';
3
3
  export * from './sent_tx.js';
4
4
  export * from './contract_base.js';
5
+ export * from './batch_call.js';