@aztec/wallet-sdk 0.0.1-commit.d1f2d6c → 0.0.1-commit.d20b825a7
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.
- package/dest/base-wallet/base_wallet.d.ts +93 -35
- package/dest/base-wallet/base_wallet.d.ts.map +1 -1
- package/dest/base-wallet/base_wallet.js +253 -69
- package/dest/base-wallet/index.d.ts +3 -2
- package/dest/base-wallet/index.d.ts.map +1 -1
- package/dest/base-wallet/index.js +1 -0
- package/dest/base-wallet/utils.d.ts +50 -0
- package/dest/base-wallet/utils.d.ts.map +1 -0
- package/dest/base-wallet/utils.js +133 -0
- package/dest/crypto.d.ts +39 -1
- package/dest/crypto.d.ts.map +1 -1
- package/dest/crypto.js +88 -0
- package/dest/extension/provider/extension_wallet.d.ts +4 -6
- package/dest/extension/provider/extension_wallet.d.ts.map +1 -1
- package/dest/extension/provider/extension_wallet.js +9 -2
- package/dest/extension/provider/index.d.ts +2 -2
- package/dest/extension/provider/index.d.ts.map +1 -1
- package/dest/iframe/handlers/iframe_connection_handler.d.ts +118 -0
- package/dest/iframe/handlers/iframe_connection_handler.d.ts.map +1 -0
- package/dest/iframe/handlers/iframe_connection_handler.js +228 -0
- package/dest/iframe/handlers/index.d.ts +2 -0
- package/dest/iframe/handlers/index.d.ts.map +1 -0
- package/dest/iframe/handlers/index.js +1 -0
- package/dest/iframe/provider/iframe_discovery.d.ts +25 -0
- package/dest/iframe/provider/iframe_discovery.d.ts.map +1 -0
- package/dest/iframe/provider/iframe_discovery.js +167 -0
- package/dest/iframe/provider/iframe_provider.d.ts +65 -0
- package/dest/iframe/provider/iframe_provider.d.ts.map +1 -0
- package/dest/iframe/provider/iframe_provider.js +257 -0
- package/dest/iframe/provider/iframe_wallet.d.ts +68 -0
- package/dest/iframe/provider/iframe_wallet.d.ts.map +1 -0
- package/dest/iframe/provider/iframe_wallet.js +200 -0
- package/dest/iframe/provider/index.d.ts +4 -0
- package/dest/iframe/provider/index.d.ts.map +1 -0
- package/dest/iframe/provider/index.js +3 -0
- package/dest/manager/types.d.ts +6 -5
- package/dest/manager/types.d.ts.map +1 -1
- package/dest/manager/wallet_manager.d.ts +1 -1
- package/dest/manager/wallet_manager.d.ts.map +1 -1
- package/dest/manager/wallet_manager.js +48 -18
- package/dest/types.d.ts +14 -2
- package/dest/types.d.ts.map +1 -1
- package/dest/types.js +4 -0
- package/package.json +19 -9
- package/src/base-wallet/base_wallet.ts +347 -122
- package/src/base-wallet/index.ts +7 -1
- package/src/base-wallet/utils.ts +240 -0
- package/src/crypto.ts +104 -0
- package/src/extension/provider/extension_wallet.ts +13 -10
- package/src/extension/provider/index.ts +1 -1
- package/src/iframe/handlers/iframe_connection_handler.ts +328 -0
- package/src/iframe/handlers/index.ts +7 -0
- package/src/iframe/provider/iframe_discovery.ts +185 -0
- package/src/iframe/provider/iframe_provider.ts +331 -0
- package/src/iframe/provider/iframe_wallet.ts +229 -0
- package/src/iframe/provider/index.ts +3 -0
- package/src/manager/types.ts +5 -4
- package/src/manager/wallet_manager.ts +55 -23
- package/src/types.ts +13 -0
|
@@ -1,16 +1,21 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { NO_FROM } from '@aztec/aztec.js/account';
|
|
2
|
+
import { NO_WAIT, extractOffchainOutput } from '@aztec/aztec.js/contracts';
|
|
2
3
|
import { waitForTx } from '@aztec/aztec.js/node';
|
|
3
|
-
import {
|
|
4
|
+
import { ContractInitializationStatus, TxSimulationResultWithAppOffset } from '@aztec/aztec.js/wallet';
|
|
4
5
|
import { AccountFeePaymentMethodOptions } from '@aztec/entrypoints/account';
|
|
6
|
+
import { DefaultEntrypoint } from '@aztec/entrypoints/default';
|
|
5
7
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
6
8
|
import { createLogger } from '@aztec/foundation/log';
|
|
9
|
+
import { displayDebugLogs } from '@aztec/pxe/client/lazy';
|
|
7
10
|
import { decodeFromAbi } from '@aztec/stdlib/abi';
|
|
11
|
+
import { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
8
12
|
import { computePartialAddress, getContractClassFromArtifact } from '@aztec/stdlib/contract';
|
|
9
13
|
import { SimulationError } from '@aztec/stdlib/errors';
|
|
10
|
-
import { Gas, GasSettings } from '@aztec/stdlib/gas';
|
|
11
|
-
import {
|
|
14
|
+
import { Gas, GasFees, GasSettings, ManaUsageEstimate } from '@aztec/stdlib/gas';
|
|
15
|
+
import { computeSiloedPrivateInitializationNullifier, computeSiloedPublicInitializationNullifier } from '@aztec/stdlib/hash';
|
|
12
16
|
import { mergeExecutionPayloads } from '@aztec/stdlib/tx';
|
|
13
17
|
import { inspect } from 'util';
|
|
18
|
+
import { buildMergedSimulationResult, extractOptimizablePublicStaticCalls, simulateViaNode } from './utils.js';
|
|
14
19
|
/**
|
|
15
20
|
* A base class for Wallet implementations
|
|
16
21
|
*/ export class BaseWallet {
|
|
@@ -19,14 +24,36 @@ import { inspect } from 'util';
|
|
|
19
24
|
log;
|
|
20
25
|
minFeePadding;
|
|
21
26
|
cancellableTransactions;
|
|
27
|
+
// A wallet is instantiated for a particular chain, so chain info never changes during its lifetime.
|
|
28
|
+
// We cache it here because getChainInfo is called frequently (every tx simulation, send, auth wit, etc.).
|
|
29
|
+
nodeInfoPromise;
|
|
22
30
|
// Protected because we want to force wallets to instantiate their own PXE.
|
|
23
|
-
constructor(pxe, aztecNode){
|
|
31
|
+
constructor(pxe, aztecNode, log = createLogger('wallet-sdk:base_wallet')){
|
|
24
32
|
this.pxe = pxe;
|
|
25
33
|
this.aztecNode = aztecNode;
|
|
26
|
-
this.log =
|
|
34
|
+
this.log = log;
|
|
27
35
|
this.minFeePadding = 0.5;
|
|
28
36
|
this.cancellableTransactions = false;
|
|
29
37
|
}
|
|
38
|
+
scopesFrom(from, additionalScopes = []) {
|
|
39
|
+
const allScopes = from === NO_FROM ? additionalScopes : [
|
|
40
|
+
from,
|
|
41
|
+
...additionalScopes
|
|
42
|
+
];
|
|
43
|
+
const scopeSet = new Set(allScopes.map((address)=>address.toString()));
|
|
44
|
+
return [
|
|
45
|
+
...scopeSet
|
|
46
|
+
].map(AztecAddress.fromString);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Picks the sender address PXE should tag private messages with. Returns `undefined` when there is no signing
|
|
50
|
+
* account (`from === NO_FROM`) and no explicit override; in that case any private log emitted by the tx will fail
|
|
51
|
+
* the contract-side `Sender for tags is not set` assertion unless `set_sender_for_tags` is called first.
|
|
52
|
+
* @param from - Tx sender, or `NO_FROM`.
|
|
53
|
+
* @param sendMessagesAs - Explicit override.
|
|
54
|
+
*/ senderForTagsFrom(from, sendMessagesAs) {
|
|
55
|
+
return sendMessagesAs ?? (from === NO_FROM ? undefined : from);
|
|
56
|
+
}
|
|
30
57
|
/**
|
|
31
58
|
* Returns the list of aliased contacts associated with the wallet.
|
|
32
59
|
* This base implementation directly returns PXE's senders, but note that in general contacts are a superset of senders.
|
|
@@ -41,7 +68,10 @@ import { inspect } from 'util';
|
|
|
41
68
|
}));
|
|
42
69
|
}
|
|
43
70
|
async getChainInfo() {
|
|
44
|
-
|
|
71
|
+
if (!this.nodeInfoPromise) {
|
|
72
|
+
this.nodeInfoPromise = this.aztecNode.getNodeInfo();
|
|
73
|
+
}
|
|
74
|
+
const { l1ChainId, rollupVersion } = await this.nodeInfoPromise;
|
|
45
75
|
return {
|
|
46
76
|
chainId: new Fr(l1ChainId),
|
|
47
77
|
version: new Fr(rollupVersion)
|
|
@@ -49,24 +79,45 @@ import { inspect } from 'util';
|
|
|
49
79
|
}
|
|
50
80
|
async createTxExecutionRequestFromPayloadAndFee(executionPayload, from, feeOptions) {
|
|
51
81
|
const feeExecutionPayload = await feeOptions.walletFeePaymentMethod?.getExecutionPayload();
|
|
52
|
-
const executionOptions = {
|
|
53
|
-
txNonce: Fr.random(),
|
|
54
|
-
cancellable: this.cancellableTransactions,
|
|
55
|
-
feePaymentMethodOptions: feeOptions.accountFeePaymentMethodOptions
|
|
56
|
-
};
|
|
57
82
|
const finalExecutionPayload = feeExecutionPayload ? mergeExecutionPayloads([
|
|
58
83
|
feeExecutionPayload,
|
|
59
84
|
executionPayload
|
|
60
85
|
]) : executionPayload;
|
|
61
|
-
const fromAccount = await this.getAccountFromAddress(from);
|
|
62
86
|
const chainInfo = await this.getChainInfo();
|
|
63
|
-
|
|
87
|
+
if (from === NO_FROM) {
|
|
88
|
+
const entrypoint = new DefaultEntrypoint();
|
|
89
|
+
return entrypoint.createTxExecutionRequest(finalExecutionPayload, feeOptions.gasSettings, chainInfo);
|
|
90
|
+
} else {
|
|
91
|
+
const fromAccount = await this.getAccountFromAddress(from);
|
|
92
|
+
const executionOptions = {
|
|
93
|
+
txNonce: Fr.random(),
|
|
94
|
+
cancellable: this.cancellableTransactions,
|
|
95
|
+
// If from is an address, feeOptions include the way the account contract should handle the fee payment
|
|
96
|
+
feePaymentMethodOptions: feeOptions.accountFeePaymentMethodOptions
|
|
97
|
+
};
|
|
98
|
+
return fromAccount.createTxExecutionRequest(finalExecutionPayload, feeOptions.gasSettings, chainInfo, executionOptions);
|
|
99
|
+
}
|
|
64
100
|
}
|
|
65
101
|
async createAuthWit(from, messageHashOrIntent) {
|
|
66
102
|
const account = await this.getAccountFromAddress(from);
|
|
67
103
|
const chainInfo = await this.getChainInfo();
|
|
68
104
|
return account.createAuthWit(messageHashOrIntent, chainInfo);
|
|
69
105
|
}
|
|
106
|
+
/**
|
|
107
|
+
* Request capabilities from the wallet.
|
|
108
|
+
*
|
|
109
|
+
* This method is wallet-implementation-dependent and must be provided by classes extending BaseWallet.
|
|
110
|
+
* Embedded wallets typically don't support capability-based authorization (no user authorization flow),
|
|
111
|
+
* while external wallets (browser extensions, hardware wallets) implement this to reduce authorization
|
|
112
|
+
* friction by allowing apps to request permissions upfront.
|
|
113
|
+
*
|
|
114
|
+
* TODO: Consider making it abstract so implementing it is a conscious decision. Leaving it as-is
|
|
115
|
+
* while the feature stabilizes.
|
|
116
|
+
*
|
|
117
|
+
* @param _manifest - Application capability manifest declaring what operations the app needs
|
|
118
|
+
*/ requestCapabilities(_manifest) {
|
|
119
|
+
throw new Error('Not implemented');
|
|
120
|
+
}
|
|
70
121
|
async batch(methods) {
|
|
71
122
|
const results = [];
|
|
72
123
|
for (const method of methods){
|
|
@@ -88,26 +139,33 @@ import { inspect } from 'util';
|
|
|
88
139
|
}
|
|
89
140
|
/**
|
|
90
141
|
* Completes partial user-provided fee options with wallet defaults.
|
|
91
|
-
* @param
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
*/ async completeFeeOptions(from, feePayer, gasSettings) {
|
|
96
|
-
const maxFeesPerGas = gasSettings?.maxFeesPerGas ?? (await this.aztecNode.getCurrentMinFees()).mul(1 + this.minFeePadding);
|
|
142
|
+
* @param config - Fee completion config.
|
|
143
|
+
*/ async completeFeeOptions(config) {
|
|
144
|
+
const { from, feePayer, gasSettings, forEstimation, congestionEstimate } = config;
|
|
145
|
+
const maxFeesPerGas = gasSettings?.maxFeesPerGas ?? (await this.getMinFees(congestionEstimate)).mul(1 + this.minFeePadding);
|
|
97
146
|
let accountFeePaymentMethodOptions;
|
|
98
|
-
//
|
|
99
|
-
//
|
|
100
|
-
if (
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
147
|
+
// If from is an address, we need to determine the appropriate fee payment method options for the
|
|
148
|
+
// account contract entrypoint to use
|
|
149
|
+
if (from !== NO_FROM) {
|
|
150
|
+
if (!feePayer) {
|
|
151
|
+
// The transaction does not include a fee payment method, so we set the flag
|
|
152
|
+
// for the account to use its fee juice balance
|
|
153
|
+
accountFeePaymentMethodOptions = AccountFeePaymentMethodOptions.PREEXISTING_FEE_JUICE;
|
|
154
|
+
} else {
|
|
155
|
+
// The transaction includes fee payment method, so we check if we are the fee payer for it
|
|
156
|
+
// (this can only happen if the embedded payment method is FeeJuiceWithClaim)
|
|
157
|
+
accountFeePaymentMethodOptions = from.equals(feePayer) ? AccountFeePaymentMethodOptions.FEE_JUICE_WITH_CLAIM : AccountFeePaymentMethodOptions.EXTERNAL;
|
|
158
|
+
}
|
|
106
159
|
}
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
160
|
+
const gasSettingsOverrides = {
|
|
161
|
+
gasLimits: gasSettings?.gasLimits ? Gas.from(gasSettings.gasLimits) : undefined,
|
|
162
|
+
teardownGasLimits: gasSettings?.teardownGasLimits ? Gas.from(gasSettings.teardownGasLimits) : undefined,
|
|
163
|
+
maxFeesPerGas,
|
|
164
|
+
maxPriorityFeesPerGas: gasSettings?.maxPriorityFeesPerGas ?? GasFees.empty()
|
|
165
|
+
};
|
|
166
|
+
// When estimating gas (simulation), use high limits so the simulation doesn't run out of gas.
|
|
167
|
+
// When sending for real, use protocol max limits that the network will actually accept.
|
|
168
|
+
const fullGasSettings = forEstimation ? GasSettings.forEstimation(gasSettingsOverrides) : GasSettings.fallback(gasSettingsOverrides);
|
|
111
169
|
this.log.debug(`Using L2 gas settings`, fullGasSettings);
|
|
112
170
|
return {
|
|
113
171
|
gasSettings: fullGasSettings,
|
|
@@ -116,22 +174,24 @@ import { inspect } from 'util';
|
|
|
116
174
|
};
|
|
117
175
|
}
|
|
118
176
|
/**
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
* to
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
177
|
+
* Returns the worst-case min fee across predicted future slots.
|
|
178
|
+
* Falls back to getCurrentMinFees if the node doesn't support getPredictedMinFees.
|
|
179
|
+
* @param estimate - The mana usage estimate to use for fee prediction. Defaults to Limit for conservative estimation.
|
|
180
|
+
*/ async getMinFees(estimate = ManaUsageEstimate.Limit) {
|
|
181
|
+
try {
|
|
182
|
+
const predicted = await this.aztecNode.getPredictedMinFees(estimate);
|
|
183
|
+
if (predicted.length === 0) {
|
|
184
|
+
return this.aztecNode.getCurrentMinFees();
|
|
185
|
+
}
|
|
186
|
+
return predicted.reduce((worst, fees)=>fees.feePerL2Gas > worst.feePerL2Gas ? fees : worst);
|
|
187
|
+
} catch (err) {
|
|
188
|
+
// Fallback for old nodes that don't support getPredictedMinFees.
|
|
189
|
+
// Only fall back on method-not-found errors (JSON-RPC code -32601); rethrow others.
|
|
190
|
+
if (err?.cause?.code === -32601 || err?.message?.includes('Method not found')) {
|
|
191
|
+
return this.aztecNode.getCurrentMinFees();
|
|
192
|
+
}
|
|
193
|
+
throw err;
|
|
194
|
+
}
|
|
135
195
|
}
|
|
136
196
|
registerSender(address, _alias = '') {
|
|
137
197
|
return this.pxe.registerSender(address);
|
|
@@ -168,20 +228,105 @@ import { inspect } from 'util';
|
|
|
168
228
|
}
|
|
169
229
|
return instance;
|
|
170
230
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
231
|
+
/**
|
|
232
|
+
* Simulates calls through the standard PXE path (account entrypoint).
|
|
233
|
+
* @param executionPayload - The execution payload to simulate.
|
|
234
|
+
* @param opts - Simulation options.
|
|
235
|
+
*/ async simulateViaEntrypoint(executionPayload, opts) {
|
|
236
|
+
const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(executionPayload, opts.from, opts.feeOptions);
|
|
237
|
+
const result = await this.pxe.simulateTx(txRequest, {
|
|
238
|
+
simulatePublic: true,
|
|
239
|
+
skipTxValidation: opts.skipTxValidation,
|
|
240
|
+
skipFeeEnforcement: opts.skipFeeEnforcement,
|
|
241
|
+
scopes: this.scopesFrom(opts.from, opts.additionalScopes),
|
|
242
|
+
senderForTags: this.senderForTagsFrom(opts.from, opts.sendMessagesAs)
|
|
243
|
+
});
|
|
244
|
+
const appCallOffset = await this.computeAppCallOffset(opts.from, opts.feeOptions);
|
|
245
|
+
return TxSimulationResultWithAppOffset.fromResultAndOffset(result, appCallOffset);
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Computes the index where the app's calls begin in the flattened array of calls (0 = entrypoint/root, 1..N = fee
|
|
249
|
+
* calls, N+1 = app).
|
|
250
|
+
* @param from - The sender address, or NO_FROM for the default entrypoint.
|
|
251
|
+
* @param feeOptions - Fee options containing the wallet fee payment method.
|
|
252
|
+
*/ async computeAppCallOffset(from, feeOptions) {
|
|
253
|
+
if (from === NO_FROM) {
|
|
254
|
+
return 0;
|
|
255
|
+
}
|
|
256
|
+
const feeExecutionPayload = await feeOptions.walletFeePaymentMethod?.getExecutionPayload();
|
|
257
|
+
return (feeExecutionPayload?.calls.length ?? 0) + 1; // +1 for entrypoint
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Simulates a transaction, optimizing leading public static calls by running them directly
|
|
261
|
+
* on the node while sending the remaining calls through the standard PXE path.
|
|
262
|
+
* Return values from both paths are merged back in original call order.
|
|
263
|
+
* @param executionPayload - The execution payload to simulate.
|
|
264
|
+
* @param opts - Simulation options (from address, fee settings, etc.).
|
|
265
|
+
* @returns The merged simulation result.
|
|
266
|
+
*/ async simulateTx(executionPayload, opts) {
|
|
267
|
+
const feeOptions = await this.completeFeeOptions({
|
|
268
|
+
from: opts.from,
|
|
269
|
+
feePayer: executionPayload.feePayer,
|
|
270
|
+
gasSettings: opts.fee?.gasSettings,
|
|
271
|
+
forEstimation: true,
|
|
272
|
+
congestionEstimate: opts.fee?.congestionEstimate
|
|
273
|
+
});
|
|
274
|
+
const { optimizableCalls, remainingCalls } = extractOptimizablePublicStaticCalls(executionPayload);
|
|
275
|
+
const remainingPayload = {
|
|
276
|
+
...executionPayload,
|
|
277
|
+
calls: remainingCalls
|
|
278
|
+
};
|
|
279
|
+
const chainInfo = await this.getChainInfo();
|
|
280
|
+
let blockHeader;
|
|
281
|
+
// PXE might not be synced yet, so we pull the latest header from the node
|
|
282
|
+
// To keep things consistent, we'll always try with PXE first
|
|
283
|
+
try {
|
|
284
|
+
blockHeader = await this.pxe.getSyncedBlockHeader();
|
|
285
|
+
} catch {
|
|
286
|
+
blockHeader = await this.aztecNode.getBlockHeader();
|
|
287
|
+
}
|
|
288
|
+
const simulationOrigin = opts.from === NO_FROM ? AztecAddress.ZERO : opts.from;
|
|
289
|
+
const [optimizedResults, normalResult] = await Promise.all([
|
|
290
|
+
optimizableCalls.length > 0 ? simulateViaNode(this.aztecNode, optimizableCalls, simulationOrigin, chainInfo, feeOptions.gasSettings, blockHeader, opts.skipFeeEnforcement ?? true, this.getContractName.bind(this)) : Promise.resolve([]),
|
|
291
|
+
remainingCalls.length > 0 ? this.simulateViaEntrypoint(remainingPayload, {
|
|
292
|
+
from: opts.from,
|
|
293
|
+
feeOptions,
|
|
294
|
+
additionalScopes: opts.additionalScopes,
|
|
295
|
+
skipTxValidation: opts.skipTxValidation,
|
|
296
|
+
skipFeeEnforcement: opts.skipFeeEnforcement ?? true,
|
|
297
|
+
sendMessagesAs: opts.sendMessagesAs
|
|
298
|
+
}) : Promise.resolve(null)
|
|
299
|
+
]);
|
|
300
|
+
return buildMergedSimulationResult(optimizedResults, normalResult);
|
|
175
301
|
}
|
|
176
302
|
async profileTx(executionPayload, opts) {
|
|
177
|
-
const feeOptions = await this.completeFeeOptions(
|
|
303
|
+
const feeOptions = await this.completeFeeOptions({
|
|
304
|
+
from: opts.from,
|
|
305
|
+
feePayer: executionPayload.feePayer,
|
|
306
|
+
gasSettings: opts.fee?.gasSettings,
|
|
307
|
+
congestionEstimate: opts.fee?.congestionEstimate
|
|
308
|
+
});
|
|
178
309
|
const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(executionPayload, opts.from, feeOptions);
|
|
179
|
-
return this.pxe.profileTx(txRequest,
|
|
310
|
+
return this.pxe.profileTx(txRequest, {
|
|
311
|
+
profileMode: opts.profileMode,
|
|
312
|
+
skipProofGeneration: opts.skipProofGeneration ?? true,
|
|
313
|
+
scopes: this.scopesFrom(opts.from, opts.additionalScopes),
|
|
314
|
+
senderForTags: this.senderForTagsFrom(opts.from, opts.sendMessagesAs)
|
|
315
|
+
});
|
|
180
316
|
}
|
|
181
317
|
async sendTx(executionPayload, opts) {
|
|
182
|
-
const feeOptions = await this.completeFeeOptions(
|
|
318
|
+
const feeOptions = await this.completeFeeOptions({
|
|
319
|
+
from: opts.from,
|
|
320
|
+
feePayer: executionPayload.feePayer,
|
|
321
|
+
gasSettings: opts.fee?.gasSettings,
|
|
322
|
+
congestionEstimate: opts.fee?.congestionEstimate
|
|
323
|
+
});
|
|
183
324
|
const txRequest = await this.createTxExecutionRequestFromPayloadAndFee(executionPayload, opts.from, feeOptions);
|
|
184
|
-
const provenTx = await this.pxe.proveTx(txRequest
|
|
325
|
+
const provenTx = await this.pxe.proveTx(txRequest, {
|
|
326
|
+
scopes: this.scopesFrom(opts.from, opts.additionalScopes),
|
|
327
|
+
senderForTags: this.senderForTagsFrom(opts.from, opts.sendMessagesAs)
|
|
328
|
+
});
|
|
329
|
+
const offchainOutput = extractOffchainOutput(provenTx.getOffchainEffects(), provenTx.publicInputs.constants.anchorBlockHeader.globalVariables.timestamp);
|
|
185
330
|
const tx = await provenTx.toTx();
|
|
186
331
|
const txHash = tx.getTxHash();
|
|
187
332
|
if (await this.aztecNode.getTxEffect(txHash)) {
|
|
@@ -194,11 +339,33 @@ import { inspect } from 'util';
|
|
|
194
339
|
this.log.info(`Sent transaction ${txHash}`);
|
|
195
340
|
// If wait is NO_WAIT, return txHash immediately
|
|
196
341
|
if (opts.wait === NO_WAIT) {
|
|
197
|
-
return
|
|
342
|
+
return {
|
|
343
|
+
txHash,
|
|
344
|
+
...offchainOutput
|
|
345
|
+
};
|
|
198
346
|
}
|
|
199
347
|
// Otherwise, wait for the full receipt (default behavior on wait: undefined)
|
|
200
348
|
const waitOpts = typeof opts.wait === 'object' ? opts.wait : undefined;
|
|
201
|
-
|
|
349
|
+
const receipt = await waitForTx(this.aztecNode, txHash, waitOpts);
|
|
350
|
+
// Display debug logs from public execution if present (served in test mode only)
|
|
351
|
+
if (receipt.debugLogs?.length) {
|
|
352
|
+
await displayDebugLogs(receipt.debugLogs, this.getContractName.bind(this));
|
|
353
|
+
}
|
|
354
|
+
return {
|
|
355
|
+
receipt,
|
|
356
|
+
...offchainOutput
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Resolves a contract address to a human-readable name via PXE, if available.
|
|
361
|
+
* @param address - The contract address to resolve.
|
|
362
|
+
*/ async getContractName(address) {
|
|
363
|
+
const instance = await this.pxe.getContractInstance(address);
|
|
364
|
+
if (!instance) {
|
|
365
|
+
return undefined;
|
|
366
|
+
}
|
|
367
|
+
const artifact = await this.pxe.getContractArtifact(instance.currentContractClassId);
|
|
368
|
+
return artifact?.name;
|
|
202
369
|
}
|
|
203
370
|
contextualizeError(err, ...context) {
|
|
204
371
|
let contextStr = '';
|
|
@@ -213,8 +380,11 @@ import { inspect } from 'util';
|
|
|
213
380
|
}
|
|
214
381
|
return err;
|
|
215
382
|
}
|
|
216
|
-
|
|
217
|
-
return this.pxe.
|
|
383
|
+
executeUtility(call, opts) {
|
|
384
|
+
return this.pxe.executeUtility(call, {
|
|
385
|
+
authwits: opts.authWitnesses,
|
|
386
|
+
scopes: opts.scopes
|
|
387
|
+
});
|
|
218
388
|
}
|
|
219
389
|
async getPrivateEvents(eventDef, eventFilter) {
|
|
220
390
|
const pxeEvents = await this.pxe.getPrivateEvents(eventDef.eventSelector, eventFilter);
|
|
@@ -232,20 +402,34 @@ import { inspect } from 'util';
|
|
|
232
402
|
});
|
|
233
403
|
return decodedEvents;
|
|
234
404
|
}
|
|
235
|
-
|
|
405
|
+
/**
|
|
406
|
+
* Returns metadata about a contract, including whether it has been initialized, published, and updated.
|
|
407
|
+
* @param address - The contract address to query.
|
|
408
|
+
*/ async getContractMetadata(address) {
|
|
236
409
|
const instance = await this.pxe.getContractInstance(address);
|
|
237
|
-
const
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
410
|
+
const publiclyRegisteredContractPromise = this.aztecNode.getContract(address);
|
|
411
|
+
let initializationStatus;
|
|
412
|
+
if (instance) {
|
|
413
|
+
// We have the instance, so we can compute the private initialization nullifier (which includes init_hash and is
|
|
414
|
+
// emitted by both private and public initializers) and get a definitive INITIALIZED/UNINITIALIZED answer.
|
|
415
|
+
const initNullifier = await computeSiloedPrivateInitializationNullifier(address, instance.initializationHash);
|
|
416
|
+
const witness = await this.aztecNode.getNullifierMembershipWitness('latest', initNullifier);
|
|
417
|
+
initializationStatus = witness ? ContractInitializationStatus.INITIALIZED : ContractInitializationStatus.UNINITIALIZED;
|
|
418
|
+
} else {
|
|
419
|
+
// Without the instance we lack the init_hash needed for the private nullifier. We fall back to checking the
|
|
420
|
+
// public initialization nullifier (computed from address alone). Not all contracts emit it (only those with
|
|
421
|
+
// public functions that require initialization checks), so its absence doesn't mean the contract is
|
|
422
|
+
// uninitialized.
|
|
423
|
+
const publicNullifier = await computeSiloedPublicInitializationNullifier(address);
|
|
424
|
+
const witness = await this.aztecNode.getNullifierMembershipWitness('latest', publicNullifier);
|
|
425
|
+
initializationStatus = witness ? ContractInitializationStatus.INITIALIZED : ContractInitializationStatus.UNKNOWN;
|
|
426
|
+
}
|
|
427
|
+
const publiclyRegisteredContract = await publiclyRegisteredContractPromise;
|
|
243
428
|
const isContractUpdated = publiclyRegisteredContract && !publiclyRegisteredContract.currentContractClassId.equals(publiclyRegisteredContract.originalContractClassId);
|
|
244
429
|
return {
|
|
245
430
|
instance: instance ?? undefined,
|
|
246
|
-
|
|
431
|
+
initializationStatus,
|
|
247
432
|
isContractPublished: !!publiclyRegisteredContract,
|
|
248
|
-
isContractClassPubliclyRegistered: !!publiclyRegisteredContractClass,
|
|
249
433
|
isContractUpdated: !!isContractUpdated,
|
|
250
434
|
updatedContractClassId: isContractUpdated ? publiclyRegisteredContract.currentContractClassId : undefined
|
|
251
435
|
};
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export { BaseWallet, type FeeOptions } from './base_wallet.js';
|
|
2
|
-
|
|
1
|
+
export { BaseWallet, type CompleteFeeOptionsConfig, type FeeOptions, type SimulateViaEntrypointOptions, } from './base_wallet.js';
|
|
2
|
+
export { simulateViaNode, buildMergedSimulationResult, extractOptimizablePublicStaticCalls } from './utils.js';
|
|
3
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9iYXNlLXdhbGxldC9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQ0wsVUFBVSxFQUNWLEtBQUssd0JBQXdCLEVBQzdCLEtBQUssVUFBVSxFQUNmLEtBQUssNEJBQTRCLEdBQ2xDLE1BQU0sa0JBQWtCLENBQUM7QUFDMUIsT0FBTyxFQUFFLGVBQWUsRUFBRSwyQkFBMkIsRUFBRSxtQ0FBbUMsRUFBRSxNQUFNLFlBQVksQ0FBQyJ9
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/base-wallet/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/base-wallet/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,KAAK,wBAAwB,EAC7B,KAAK,UAAU,EACf,KAAK,4BAA4B,GAClC,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,eAAe,EAAE,2BAA2B,EAAE,mCAAmC,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { AztecNode } from '@aztec/aztec.js/node';
|
|
2
|
+
import { TxSimulationResultWithAppOffset } from '@aztec/aztec.js/wallet';
|
|
3
|
+
import type { ChainInfo } from '@aztec/entrypoints/interfaces';
|
|
4
|
+
import type { ContractNameResolver } from '@aztec/pxe/client/lazy';
|
|
5
|
+
import { type FunctionCall } from '@aztec/stdlib/abi';
|
|
6
|
+
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
7
|
+
import type { GasSettings } from '@aztec/stdlib/gas';
|
|
8
|
+
import { type BlockHeader, type ExecutionPayload, TxSimulationResult } from '@aztec/stdlib/tx';
|
|
9
|
+
/**
|
|
10
|
+
* Splits an execution payload into a leading prefix of public static calls
|
|
11
|
+
* (eligible for direct node simulation) and the remaining calls.
|
|
12
|
+
*
|
|
13
|
+
* Only a leading run of public static calls is eligible for optimization.
|
|
14
|
+
* Any non-public-static call may enqueue public state mutations
|
|
15
|
+
* (e.g. private calls can enqueue public calls), so all calls that follow
|
|
16
|
+
* must go through the normal simulation path to see the correct state.
|
|
17
|
+
*
|
|
18
|
+
*/
|
|
19
|
+
export declare function extractOptimizablePublicStaticCalls(payload: ExecutionPayload): {
|
|
20
|
+
/** Leading public static calls eligible for direct node simulation. */
|
|
21
|
+
optimizableCalls: FunctionCall[];
|
|
22
|
+
/** All remaining calls. */
|
|
23
|
+
remainingCalls: FunctionCall[];
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Simulates public static calls by splitting them into batches of MAX_ENQUEUED_CALLS_PER_CALL
|
|
27
|
+
* and sending each batch directly to the node.
|
|
28
|
+
*
|
|
29
|
+
* @param node - The Aztec node to simulate on.
|
|
30
|
+
* @param publicStaticCalls - Array of public static function calls to optimize.
|
|
31
|
+
* @param from - The account address making the calls.
|
|
32
|
+
* @param chainInfo - Chain information (chainId and version).
|
|
33
|
+
* @param gasSettings - Gas settings for the transaction.
|
|
34
|
+
* @param blockHeader - Block header to use as anchor.
|
|
35
|
+
* @param skipFeeEnforcement - Whether to skip fee enforcement during simulation.
|
|
36
|
+
* @returns Array of TxSimulationResult, one per batch.
|
|
37
|
+
*/
|
|
38
|
+
export declare function simulateViaNode(node: AztecNode, publicStaticCalls: FunctionCall[], from: AztecAddress, chainInfo: ChainInfo, gasSettings: GasSettings, blockHeader: BlockHeader, skipFeeEnforcement: boolean | undefined, getContractName: ContractNameResolver): Promise<TxSimulationResult[]>;
|
|
39
|
+
/**
|
|
40
|
+
* Merges simulation results from the optimized (public static) and normal paths.
|
|
41
|
+
* Since optimized calls are always a leading prefix, return values are simply
|
|
42
|
+
* concatenated: optimized first, then normal.
|
|
43
|
+
* Stats are taken from the normal result only (the optimized path doesn't produce them).
|
|
44
|
+
*
|
|
45
|
+
* @param optimizedResults - Results from optimized public static call batches.
|
|
46
|
+
* @param normalResult - Result from normal simulation (null if all calls were optimized).
|
|
47
|
+
* @returns A single TxSimulationResult with return values in original call order.
|
|
48
|
+
*/
|
|
49
|
+
export declare function buildMergedSimulationResult(optimizedResults: TxSimulationResult[], normalResult: TxSimulationResultWithAppOffset | null): TxSimulationResultWithAppOffset;
|
|
50
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9iYXNlLXdhbGxldC91dGlscy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssRUFBRSxTQUFTLEVBQUUsTUFBTSxzQkFBc0IsQ0FBQztBQUN0RCxPQUFPLEVBQUUsK0JBQStCLEVBQUUsTUFBTSx3QkFBd0IsQ0FBQztBQUV6RSxPQUFPLEtBQUssRUFBRSxTQUFTLEVBQUUsTUFBTSwrQkFBK0IsQ0FBQztBQUkvRCxPQUFPLEtBQUssRUFBRSxvQkFBb0IsRUFBRSxNQUFNLHdCQUF3QixDQUFDO0FBR25FLE9BQU8sRUFBRSxLQUFLLFlBQVksRUFBb0IsTUFBTSxtQkFBbUIsQ0FBQztBQUN4RSxPQUFPLEtBQUssRUFBRSxZQUFZLEVBQUUsTUFBTSw2QkFBNkIsQ0FBQztBQUNoRSxPQUFPLEtBQUssRUFBRSxXQUFXLEVBQUUsTUFBTSxtQkFBbUIsQ0FBQztBQVFyRCxPQUFPLEVBQ0wsS0FBSyxXQUFXLEVBQ2hCLEtBQUssZ0JBQWdCLEVBT3JCLGtCQUFrQixFQUNuQixNQUFNLGtCQUFrQixDQUFDO0FBRTFCOzs7Ozs7Ozs7R0FTRztBQUNILHdCQUFnQixtQ0FBbUMsQ0FBQyxPQUFPLEVBQUUsZ0JBQWdCLEdBQUc7SUFDOUUsdUVBQXVFO0lBQ3ZFLGdCQUFnQixFQUFFLFlBQVksRUFBRSxDQUFDO0lBQ2pDLDJCQUEyQjtJQUMzQixjQUFjLEVBQUUsWUFBWSxFQUFFLENBQUM7Q0FDaEMsQ0FPQTtBQXVHRDs7Ozs7Ozs7Ozs7O0dBWUc7QUFDSCx3QkFBc0IsZUFBZSxDQUNuQyxJQUFJLEVBQUUsU0FBUyxFQUNmLGlCQUFpQixFQUFFLFlBQVksRUFBRSxFQUNqQyxJQUFJLEVBQUUsWUFBWSxFQUNsQixTQUFTLEVBQUUsU0FBUyxFQUNwQixXQUFXLEVBQUUsV0FBVyxFQUN4QixXQUFXLEVBQUUsV0FBVyxFQUN4QixrQkFBa0IscUJBQWdCLEVBQ2xDLGVBQWUsRUFBRSxvQkFBb0IsR0FDcEMsT0FBTyxDQUFDLGtCQUFrQixFQUFFLENBQUMsQ0F3Qi9CO0FBRUQ7Ozs7Ozs7OztHQVNHO0FBQ0gsd0JBQWdCLDJCQUEyQixDQUN6QyxnQkFBZ0IsRUFBRSxrQkFBa0IsRUFBRSxFQUN0QyxZQUFZLEVBQUUsK0JBQStCLEdBQUcsSUFBSSxHQUNuRCwrQkFBK0IsQ0FxQmpDIn0=
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/base-wallet/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,+BAA+B,EAAE,MAAM,wBAAwB,CAAC;AAEzE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,+BAA+B,CAAC;AAI/D,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAGnE,OAAO,EAAE,KAAK,YAAY,EAAoB,MAAM,mBAAmB,CAAC;AACxE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAQrD,OAAO,EACL,KAAK,WAAW,EAChB,KAAK,gBAAgB,EAOrB,kBAAkB,EACnB,MAAM,kBAAkB,CAAC;AAE1B;;;;;;;;;GASG;AACH,wBAAgB,mCAAmC,CAAC,OAAO,EAAE,gBAAgB,GAAG;IAC9E,uEAAuE;IACvE,gBAAgB,EAAE,YAAY,EAAE,CAAC;IACjC,2BAA2B;IAC3B,cAAc,EAAE,YAAY,EAAE,CAAC;CAChC,CAOA;AAuGD;;;;;;;;;;;;GAYG;AACH,wBAAsB,eAAe,CACnC,IAAI,EAAE,SAAS,EACf,iBAAiB,EAAE,YAAY,EAAE,EACjC,IAAI,EAAE,YAAY,EAClB,SAAS,EAAE,SAAS,EACpB,WAAW,EAAE,WAAW,EACxB,WAAW,EAAE,WAAW,EACxB,kBAAkB,qBAAgB,EAClC,eAAe,EAAE,oBAAoB,GACpC,OAAO,CAAC,kBAAkB,EAAE,CAAC,CAwB/B;AAED;;;;;;;;;GASG;AACH,wBAAgB,2BAA2B,CACzC,gBAAgB,EAAE,kBAAkB,EAAE,EACtC,YAAY,EAAE,+BAA+B,GAAG,IAAI,GACnD,+BAA+B,CAqBjC"}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { TxSimulationResultWithAppOffset } from '@aztec/aztec.js/wallet';
|
|
2
|
+
import { MAX_ENQUEUED_CALLS_PER_CALL } from '@aztec/constants';
|
|
3
|
+
import { makeTuple } from '@aztec/foundation/array';
|
|
4
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
5
|
+
import { displayDebugLogs } from '@aztec/pxe/client/lazy';
|
|
6
|
+
import { generateSimulatedProvingResult } from '@aztec/pxe/simulator';
|
|
7
|
+
import { ClaimedLengthArray, CountedPublicCallRequest, PrivateCircuitPublicInputs, PublicCallRequest } from '@aztec/stdlib/kernel';
|
|
8
|
+
import { ChonkProof } from '@aztec/stdlib/proofs';
|
|
9
|
+
import { HashedValues, PrivateCallExecutionResult, PrivateExecutionResult, Tx, TxContext, TxSimulationResult } from '@aztec/stdlib/tx';
|
|
10
|
+
/**
|
|
11
|
+
* Splits an execution payload into a leading prefix of public static calls
|
|
12
|
+
* (eligible for direct node simulation) and the remaining calls.
|
|
13
|
+
*
|
|
14
|
+
* Only a leading run of public static calls is eligible for optimization.
|
|
15
|
+
* Any non-public-static call may enqueue public state mutations
|
|
16
|
+
* (e.g. private calls can enqueue public calls), so all calls that follow
|
|
17
|
+
* must go through the normal simulation path to see the correct state.
|
|
18
|
+
*
|
|
19
|
+
*/ export function extractOptimizablePublicStaticCalls(payload) {
|
|
20
|
+
const splitIndex = payload.calls.findIndex((call)=>!call.isPublicStatic());
|
|
21
|
+
const boundary = splitIndex === -1 ? payload.calls.length : splitIndex;
|
|
22
|
+
return {
|
|
23
|
+
optimizableCalls: payload.calls.slice(0, boundary),
|
|
24
|
+
remainingCalls: payload.calls.slice(boundary)
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Simulates a batch of public static calls by bypassing account entrypoint and private execution,
|
|
29
|
+
* directly constructing a minimal Tx and calling node.simulatePublicCalls.
|
|
30
|
+
*
|
|
31
|
+
* @param node - The Aztec node to simulate on.
|
|
32
|
+
* @param publicStaticCalls - Array of public static function calls (max MAX_ENQUEUED_CALLS_PER_CALL).
|
|
33
|
+
* @param from - The account address making the calls.
|
|
34
|
+
* @param chainInfo - Chain information (chainId and version).
|
|
35
|
+
* @param gasSettings - Gas settings for the transaction.
|
|
36
|
+
* @param blockHeader - Block header to use as anchor.
|
|
37
|
+
* @param skipFeeEnforcement - Whether to skip fee enforcement during simulation.
|
|
38
|
+
* @returns TxSimulationResult with public return values.
|
|
39
|
+
*/ async function simulateBatchViaNode(node, publicStaticCalls, from, chainInfo, gasSettings, blockHeader, skipFeeEnforcement, getContractName) {
|
|
40
|
+
const txContext = new TxContext(chainInfo.chainId, chainInfo.version, gasSettings);
|
|
41
|
+
const publicFunctionCalldata = [];
|
|
42
|
+
for (const call of publicStaticCalls){
|
|
43
|
+
const calldata = await HashedValues.fromCalldata([
|
|
44
|
+
call.selector.toField(),
|
|
45
|
+
...call.args
|
|
46
|
+
]);
|
|
47
|
+
publicFunctionCalldata.push(calldata);
|
|
48
|
+
}
|
|
49
|
+
const publicCallRequests = makeTuple(MAX_ENQUEUED_CALLS_PER_CALL, (i)=>{
|
|
50
|
+
const call = publicStaticCalls[i];
|
|
51
|
+
if (!call) {
|
|
52
|
+
return CountedPublicCallRequest.empty();
|
|
53
|
+
}
|
|
54
|
+
const publicCallRequest = new PublicCallRequest(from, call.to, call.isStatic, publicFunctionCalldata[i].hash);
|
|
55
|
+
// Counter starts at 1 (minRevertibleSideEffectCounter) so all calls are revertible
|
|
56
|
+
return new CountedPublicCallRequest(publicCallRequest, i + 1);
|
|
57
|
+
});
|
|
58
|
+
const publicCallRequestsArray = new ClaimedLengthArray(publicCallRequests, publicStaticCalls.length);
|
|
59
|
+
const publicInputs = PrivateCircuitPublicInputs.from({
|
|
60
|
+
...PrivateCircuitPublicInputs.empty(),
|
|
61
|
+
anchorBlockHeader: blockHeader,
|
|
62
|
+
txContext: txContext,
|
|
63
|
+
publicCallRequests: publicCallRequestsArray,
|
|
64
|
+
startSideEffectCounter: new Fr(0),
|
|
65
|
+
endSideEffectCounter: new Fr(publicStaticCalls.length + 1)
|
|
66
|
+
});
|
|
67
|
+
// Minimal entrypoint structure — no real private execution, just public call requests
|
|
68
|
+
const emptyEntrypoint = new PrivateCallExecutionResult(Buffer.alloc(0), Buffer.alloc(0), new Map(), publicInputs, [], new Map(), [], [], [], [], []);
|
|
69
|
+
const privateResult = new PrivateExecutionResult(emptyEntrypoint, Fr.random(), publicFunctionCalldata);
|
|
70
|
+
const provingResult = await generateSimulatedProvingResult(privateResult, (_contractAddress, _functionSelector)=>Promise.resolve(''), node, 1);
|
|
71
|
+
provingResult.publicInputs.feePayer = from;
|
|
72
|
+
const tx = await Tx.create({
|
|
73
|
+
data: provingResult.publicInputs,
|
|
74
|
+
chonkProof: ChonkProof.empty(),
|
|
75
|
+
contractClassLogFields: [],
|
|
76
|
+
publicFunctionCalldata: publicFunctionCalldata
|
|
77
|
+
});
|
|
78
|
+
const publicOutput = await node.simulatePublicCalls(tx, skipFeeEnforcement);
|
|
79
|
+
if (publicOutput.revertReason) {
|
|
80
|
+
throw publicOutput.revertReason;
|
|
81
|
+
}
|
|
82
|
+
// Display debug logs from the public simulation.
|
|
83
|
+
await displayDebugLogs(publicOutput.debugLogs, getContractName);
|
|
84
|
+
return new TxSimulationResult(privateResult, provingResult.publicInputs, publicOutput, undefined);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Simulates public static calls by splitting them into batches of MAX_ENQUEUED_CALLS_PER_CALL
|
|
88
|
+
* and sending each batch directly to the node.
|
|
89
|
+
*
|
|
90
|
+
* @param node - The Aztec node to simulate on.
|
|
91
|
+
* @param publicStaticCalls - Array of public static function calls to optimize.
|
|
92
|
+
* @param from - The account address making the calls.
|
|
93
|
+
* @param chainInfo - Chain information (chainId and version).
|
|
94
|
+
* @param gasSettings - Gas settings for the transaction.
|
|
95
|
+
* @param blockHeader - Block header to use as anchor.
|
|
96
|
+
* @param skipFeeEnforcement - Whether to skip fee enforcement during simulation.
|
|
97
|
+
* @returns Array of TxSimulationResult, one per batch.
|
|
98
|
+
*/ export async function simulateViaNode(node, publicStaticCalls, from, chainInfo, gasSettings, blockHeader, skipFeeEnforcement = true, getContractName) {
|
|
99
|
+
const batches = [];
|
|
100
|
+
for(let i = 0; i < publicStaticCalls.length; i += MAX_ENQUEUED_CALLS_PER_CALL){
|
|
101
|
+
batches.push(publicStaticCalls.slice(i, i + MAX_ENQUEUED_CALLS_PER_CALL));
|
|
102
|
+
}
|
|
103
|
+
const results = [];
|
|
104
|
+
for (const batch of batches){
|
|
105
|
+
const result = await simulateBatchViaNode(node, batch, from, chainInfo, gasSettings, blockHeader, skipFeeEnforcement, getContractName);
|
|
106
|
+
results.push(result);
|
|
107
|
+
}
|
|
108
|
+
return results;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Merges simulation results from the optimized (public static) and normal paths.
|
|
112
|
+
* Since optimized calls are always a leading prefix, return values are simply
|
|
113
|
+
* concatenated: optimized first, then normal.
|
|
114
|
+
* Stats are taken from the normal result only (the optimized path doesn't produce them).
|
|
115
|
+
*
|
|
116
|
+
* @param optimizedResults - Results from optimized public static call batches.
|
|
117
|
+
* @param normalResult - Result from normal simulation (null if all calls were optimized).
|
|
118
|
+
* @returns A single TxSimulationResult with return values in original call order.
|
|
119
|
+
*/ export function buildMergedSimulationResult(optimizedResults, normalResult) {
|
|
120
|
+
const optimizedReturnValues = optimizedResults.flatMap((r)=>r.publicOutput?.publicReturnValues ?? []);
|
|
121
|
+
const normalReturnValues = normalResult?.publicOutput?.publicReturnValues ?? [];
|
|
122
|
+
const allReturnValues = [
|
|
123
|
+
...optimizedReturnValues,
|
|
124
|
+
...normalReturnValues
|
|
125
|
+
];
|
|
126
|
+
const baseResult = normalResult ?? optimizedResults[0];
|
|
127
|
+
const mergedPublicOutput = baseResult.publicOutput ? {
|
|
128
|
+
...baseResult.publicOutput,
|
|
129
|
+
publicReturnValues: allReturnValues
|
|
130
|
+
} : undefined;
|
|
131
|
+
const merged = new TxSimulationResult(baseResult.privateExecutionResult, baseResult.publicInputs, mergedPublicOutput, normalResult?.stats);
|
|
132
|
+
return TxSimulationResultWithAppOffset.fromResultAndOffset(merged, normalResult?.appCallOffset ?? 0);
|
|
133
|
+
}
|