@aztec/bot 5.0.0-private.20260319 → 5.0.0-rc.1
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/amm_bot.d.ts +4 -3
- package/dest/amm_bot.d.ts.map +1 -1
- package/dest/amm_bot.js +3 -3
- package/dest/base_bot.d.ts +4 -4
- package/dest/base_bot.d.ts.map +1 -1
- package/dest/base_bot.js +12 -22
- package/dest/bot.d.ts +3 -2
- package/dest/bot.d.ts.map +1 -1
- package/dest/bot.js +3 -7
- package/dest/config.d.ts +30 -81
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +2 -2
- package/dest/cross_chain_bot.d.ts +6 -4
- package/dest/cross_chain_bot.d.ts.map +1 -1
- package/dest/cross_chain_bot.js +14 -10
- package/dest/factory.d.ts +7 -3
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +122 -193
- package/dest/interface.d.ts +2 -6
- package/dest/interface.d.ts.map +1 -1
- package/dest/interface.js +30 -7
- package/dest/runner.d.ts +3 -2
- package/dest/runner.d.ts.map +1 -1
- package/dest/runner.js +6 -4
- package/package.json +16 -16
- package/src/amm_bot.ts +5 -2
- package/src/base_bot.ts +10 -17
- package/src/bot.ts +4 -4
- package/src/config.ts +4 -4
- package/src/cross_chain_bot.ts +12 -7
- package/src/factory.ts +118 -134
- package/src/interface.ts +7 -7
- package/src/runner.ts +26 -3
package/src/factory.ts
CHANGED
|
@@ -15,19 +15,19 @@ import { deriveKeys } from '@aztec/aztec.js/keys';
|
|
|
15
15
|
import { createLogger } from '@aztec/aztec.js/log';
|
|
16
16
|
import { waitForL1ToL2MessageReady } from '@aztec/aztec.js/messaging';
|
|
17
17
|
import { waitForTx } from '@aztec/aztec.js/node';
|
|
18
|
+
import { getFeeJuiceBalance } from '@aztec/aztec.js/utils';
|
|
18
19
|
import { createEthereumChain } from '@aztec/ethereum/chain';
|
|
19
20
|
import { createExtendedL1Client } from '@aztec/ethereum/client';
|
|
20
21
|
import { RollupContract } from '@aztec/ethereum/contracts';
|
|
21
22
|
import type { ExtendedViemWalletClient } from '@aztec/ethereum/types';
|
|
22
23
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
23
24
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
24
|
-
import { Timer } from '@aztec/foundation/timer';
|
|
25
25
|
import { AMMContract } from '@aztec/noir-contracts.js/AMM';
|
|
26
26
|
import { PrivateTokenContract } from '@aztec/noir-contracts.js/PrivateToken';
|
|
27
27
|
import { TokenContract } from '@aztec/noir-contracts.js/Token';
|
|
28
28
|
import { TestContract } from '@aztec/noir-test-contracts.js/Test';
|
|
29
|
+
import type { BlockTag } from '@aztec/stdlib/block';
|
|
29
30
|
import type { ContractInstanceWithAddress } from '@aztec/stdlib/contract';
|
|
30
|
-
import { GasFees, GasSettings } from '@aztec/stdlib/gas';
|
|
31
31
|
import type { AztecNode, AztecNodeAdmin } from '@aztec/stdlib/interfaces/client';
|
|
32
32
|
import { deriveSigningKey } from '@aztec/stdlib/keys';
|
|
33
33
|
import { EmbeddedWallet } from '@aztec/wallets/embedded';
|
|
@@ -39,6 +39,7 @@ import { getBalances, getPrivateBalance, isStandardTokenContract } from './utils
|
|
|
39
39
|
|
|
40
40
|
const MINT_BALANCE = 1e12;
|
|
41
41
|
const MIN_BALANCE = 1e3;
|
|
42
|
+
const FEE_JUICE_TOP_UP_THRESHOLD = 100n * 10n ** 18n;
|
|
42
43
|
|
|
43
44
|
export class BotFactory {
|
|
44
45
|
private log = createLogger('bot');
|
|
@@ -49,6 +50,7 @@ export class BotFactory {
|
|
|
49
50
|
private readonly store: BotStore,
|
|
50
51
|
private readonly aztecNode: AztecNode,
|
|
51
52
|
private readonly aztecNodeAdmin?: AztecNodeAdmin,
|
|
53
|
+
private readonly syncChainTip?: BlockTag,
|
|
52
54
|
) {
|
|
53
55
|
// Set fee padding on the wallet so that all transactions during setup
|
|
54
56
|
// (token deploy, minting, etc.) use the configured padding, not the default.
|
|
@@ -68,6 +70,7 @@ export class BotFactory {
|
|
|
68
70
|
}> {
|
|
69
71
|
const defaultAccountAddress = await this.setupAccount();
|
|
70
72
|
const recipient = (await this.wallet.createSchnorrAccount(Fr.random(), Fr.random())).address;
|
|
73
|
+
await this.ensureFeeJuiceBalance(defaultAccountAddress);
|
|
71
74
|
const token = await this.setupToken(defaultAccountAddress);
|
|
72
75
|
await this.mintTokens(token, defaultAccountAddress);
|
|
73
76
|
return { wallet: this.wallet, defaultAccountAddress, token, node: this.aztecNode, recipient };
|
|
@@ -82,6 +85,7 @@ export class BotFactory {
|
|
|
82
85
|
node: AztecNode;
|
|
83
86
|
}> {
|
|
84
87
|
const defaultAccountAddress = await this.setupAccount();
|
|
88
|
+
await this.ensureFeeJuiceBalance(defaultAccountAddress);
|
|
85
89
|
const token0 = await this.setupTokenContract(defaultAccountAddress, this.config.tokenSalt, 'BotToken0', 'BOT0');
|
|
86
90
|
const token1 = await this.setupTokenContract(defaultAccountAddress, this.config.tokenSalt, 'BotToken1', 'BOT1');
|
|
87
91
|
const liquidityToken = await this.setupTokenContract(
|
|
@@ -117,6 +121,7 @@ export class BotFactory {
|
|
|
117
121
|
rollupVersion: bigint;
|
|
118
122
|
}> {
|
|
119
123
|
const defaultAccountAddress = await this.setupAccount();
|
|
124
|
+
await this.ensureFeeJuiceBalance(defaultAccountAddress);
|
|
120
125
|
|
|
121
126
|
// Create L1 client (same pattern as bridgeL1FeeJuice)
|
|
122
127
|
const l1RpcUrls = this.config.l1RpcUrls;
|
|
@@ -135,7 +140,7 @@ export class BotFactory {
|
|
|
135
140
|
const rollupContract = new RollupContract(l1Client, l1ContractAddresses.rollupAddress.toString());
|
|
136
141
|
const rollupVersion = await rollupContract.getVersion();
|
|
137
142
|
|
|
138
|
-
// Deploy TestContract
|
|
143
|
+
// Deploy TestContract (pays from the standing balance funded above).
|
|
139
144
|
const contract = await this.setupTestContract(defaultAccountAddress);
|
|
140
145
|
|
|
141
146
|
// Recover any pending messages from store (clean up stale ones first)
|
|
@@ -162,6 +167,7 @@ export class BotFactory {
|
|
|
162
167
|
const firstMsg = allMessages[0];
|
|
163
168
|
await waitForL1ToL2MessageReady(this.aztecNode, Fr.fromHexString(firstMsg.msgHash), {
|
|
164
169
|
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds,
|
|
170
|
+
chainTip: this.syncChainTip,
|
|
165
171
|
});
|
|
166
172
|
this.log.info(`First L1→L2 message is ready`);
|
|
167
173
|
}
|
|
@@ -177,12 +183,8 @@ export class BotFactory {
|
|
|
177
183
|
}
|
|
178
184
|
|
|
179
185
|
private async setupTestContract(deployer: AztecAddress): Promise<TestContract> {
|
|
180
|
-
const deployOpts: DeployOptions = {
|
|
181
|
-
|
|
182
|
-
contractAddressSalt: this.config.tokenSalt,
|
|
183
|
-
universalDeploy: true,
|
|
184
|
-
};
|
|
185
|
-
const deploy = TestContract.deploy(this.wallet);
|
|
186
|
+
const deployOpts: DeployOptions = { from: deployer };
|
|
187
|
+
const deploy = TestContract.deploy(this.wallet, { salt: this.config.tokenSalt, universalDeploy: true });
|
|
186
188
|
const instance = await this.registerOrDeployContract('TestContract', deploy, deployOpts);
|
|
187
189
|
return TestContract.at(instance.address, this.wallet);
|
|
188
190
|
}
|
|
@@ -202,55 +204,16 @@ export class BotFactory {
|
|
|
202
204
|
}
|
|
203
205
|
}
|
|
204
206
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
const timer = new Timer();
|
|
213
|
-
const address = accountManager.address;
|
|
214
|
-
this.log.info(`Account at ${address} registered. duration=${timer.ms()}`);
|
|
215
|
-
await this.store.deleteBridgeClaim(address);
|
|
216
|
-
return address;
|
|
217
|
-
} else {
|
|
218
|
-
const address = accountManager.address;
|
|
219
|
-
this.log.info(`Deploying account at ${address}`);
|
|
220
|
-
|
|
221
|
-
const claim = await this.getOrCreateBridgeClaim(address);
|
|
222
|
-
|
|
223
|
-
const paymentMethod = new FeeJuicePaymentMethodWithClaim(accountManager.address, claim);
|
|
224
|
-
const deployMethod = await accountManager.getDeployMethod();
|
|
225
|
-
const maxFeesPerGas = (await this.aztecNode.getCurrentMinFees()).mul(1 + this.config.minFeePadding);
|
|
226
|
-
|
|
227
|
-
const { estimatedGas } = await deployMethod.simulate({
|
|
228
|
-
from: AztecAddress.ZERO,
|
|
229
|
-
fee: { estimateGas: true, paymentMethod },
|
|
230
|
-
});
|
|
231
|
-
const gasSettings = GasSettings.from({ ...estimatedGas!, maxFeesPerGas, maxPriorityFeesPerGas: GasFees.empty() });
|
|
232
|
-
|
|
233
|
-
await this.withNoMinTxsPerBlock(async () => {
|
|
234
|
-
const { txHash } = await deployMethod.send({
|
|
235
|
-
from: AztecAddress.ZERO,
|
|
236
|
-
fee: { gasSettings, paymentMethod },
|
|
237
|
-
wait: NO_WAIT,
|
|
238
|
-
});
|
|
239
|
-
this.log.info(`Sent tx for account deployment with hash ${txHash.toString()}`, { gasSettings });
|
|
240
|
-
return waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
241
|
-
});
|
|
242
|
-
this.log.info(`Account deployed at ${address}`);
|
|
243
|
-
|
|
244
|
-
// Clean up the consumed bridge claim
|
|
245
|
-
await this.store.deleteBridgeClaim(address);
|
|
246
|
-
|
|
247
|
-
return accountManager.address;
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
207
|
+
/**
|
|
208
|
+
* Keyless fallback for tests and local dev: reuses the first genesis test account, whose address is
|
|
209
|
+
* pre-funded with fee juice via `initialFundedAccounts`. The test accounts are initializerless, so this
|
|
210
|
+
* must create an initializerless account for the address to match the funded one. Production bots set a
|
|
211
|
+
* sender private key and fund the resulting initializerless account from L1 instead; see
|
|
212
|
+
* setupAccountWithPrivateKey.
|
|
213
|
+
*/
|
|
251
214
|
private async setupTestAccount() {
|
|
252
215
|
const [initialAccountData] = await getInitialTestAccountsData();
|
|
253
|
-
const accountManager = await this.wallet.
|
|
216
|
+
const accountManager = await this.wallet.createSchnorrInitializerlessAccount(
|
|
254
217
|
initialAccountData.secret,
|
|
255
218
|
initialAccountData.salt,
|
|
256
219
|
initialAccountData.signingKey,
|
|
@@ -258,35 +221,44 @@ export class BotFactory {
|
|
|
258
221
|
return accountManager.address;
|
|
259
222
|
}
|
|
260
223
|
|
|
224
|
+
private async setupAccountWithPrivateKey(secret: Fr) {
|
|
225
|
+
const salt = this.config.senderSalt ?? Fr.ONE;
|
|
226
|
+
const signingKey = deriveSigningKey(secret);
|
|
227
|
+
const accountManager = await this.wallet.createSchnorrInitializerlessAccount(secret, salt, signingKey);
|
|
228
|
+
return accountManager.address;
|
|
229
|
+
}
|
|
230
|
+
|
|
261
231
|
/**
|
|
262
232
|
* Checks if the token contract is deployed and deploys it if necessary.
|
|
263
|
-
*
|
|
264
|
-
* @
|
|
233
|
+
* Uses a bridge claim for deploy when balance is below threshold to avoid failing before refuel.
|
|
234
|
+
* @param sender - Aztec address to deploy the token contract from.
|
|
235
|
+
* @param existingToken - Optional token instance when called from setupTokenWithOptionalEarlyRefuel.
|
|
236
|
+
* @returns The TokenContract or PrivateTokenContract instance.
|
|
265
237
|
*/
|
|
266
238
|
private async setupToken(sender: AztecAddress): Promise<TokenContract | PrivateTokenContract> {
|
|
267
239
|
let deploy: DeployMethod<TokenContract | PrivateTokenContract>;
|
|
268
|
-
|
|
269
|
-
const deployOpts: DeployOptions = {
|
|
270
|
-
from: sender,
|
|
271
|
-
contractAddressSalt: this.config.tokenSalt,
|
|
272
|
-
universalDeploy: true,
|
|
273
|
-
};
|
|
240
|
+
const salt = this.config.tokenSalt;
|
|
241
|
+
const deployOpts: DeployOptions = { from: sender };
|
|
274
242
|
let token: TokenContract | PrivateTokenContract;
|
|
275
243
|
if (this.config.contract === SupportedTokenContracts.TokenContract) {
|
|
276
|
-
deploy = TokenContract.deploy(this.wallet, sender, 'BotToken', 'BOT', 18);
|
|
277
|
-
|
|
278
|
-
token = TokenContract.at(
|
|
244
|
+
deploy = TokenContract.deploy(this.wallet, sender, 'BotToken', 'BOT', 18, { salt, universalDeploy: true });
|
|
245
|
+
const instance = await deploy.getInstance();
|
|
246
|
+
token = TokenContract.at(instance.address, this.wallet);
|
|
279
247
|
} else if (this.config.contract === SupportedTokenContracts.PrivateTokenContract) {
|
|
280
248
|
// Generate keys for the contract since PrivateToken uses SinglePrivateMutable which requires keys
|
|
281
249
|
const tokenSecretKey = Fr.random();
|
|
282
250
|
const tokenPublicKeys = (await deriveKeys(tokenSecretKey)).publicKeys;
|
|
283
|
-
deploy = PrivateTokenContract.
|
|
251
|
+
deploy = PrivateTokenContract.deploy(this.wallet, MINT_BALANCE, sender, {
|
|
252
|
+
salt,
|
|
253
|
+
universalDeploy: true,
|
|
254
|
+
publicKeys: tokenPublicKeys,
|
|
255
|
+
});
|
|
284
256
|
deployOpts.skipInstancePublication = true;
|
|
285
257
|
deployOpts.skipClassPublication = true;
|
|
286
258
|
deployOpts.skipInitialization = false;
|
|
287
259
|
|
|
288
260
|
// Register the contract with the secret key before deployment
|
|
289
|
-
tokenInstance = await deploy.getInstance(
|
|
261
|
+
const tokenInstance = await deploy.getInstance();
|
|
290
262
|
token = PrivateTokenContract.at(tokenInstance.address, this.wallet);
|
|
291
263
|
await this.wallet.registerContract(tokenInstance, PrivateTokenContract.artifact, tokenSecretKey);
|
|
292
264
|
// The contract constructor initializes private storage vars that need the contract's own nullifier key.
|
|
@@ -295,21 +267,7 @@ export class BotFactory {
|
|
|
295
267
|
throw new Error(`Unsupported token contract type: ${this.config.contract}`);
|
|
296
268
|
}
|
|
297
269
|
|
|
298
|
-
|
|
299
|
-
const metadata = await this.wallet.getContractMetadata(address);
|
|
300
|
-
if (metadata.isContractPublished) {
|
|
301
|
-
this.log.info(`Token at ${address.toString()} already deployed`);
|
|
302
|
-
await deploy.register();
|
|
303
|
-
} else {
|
|
304
|
-
this.log.info(`Deploying token contract at ${address.toString()}`);
|
|
305
|
-
const { estimatedGas } = await deploy.simulate({ ...deployOpts, fee: { estimateGas: true } });
|
|
306
|
-
const { txHash } = await deploy.send({ ...deployOpts, fee: { gasSettings: estimatedGas }, wait: NO_WAIT });
|
|
307
|
-
this.log.info(`Sent tx for token setup with hash ${txHash.toString()}`, { estimatedGas });
|
|
308
|
-
await this.withNoMinTxsPerBlock(async () => {
|
|
309
|
-
await waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
310
|
-
return token;
|
|
311
|
-
});
|
|
312
|
-
}
|
|
270
|
+
await this.registerOrDeployContract('token', deploy, deployOpts);
|
|
313
271
|
return token;
|
|
314
272
|
}
|
|
315
273
|
|
|
@@ -320,43 +278,39 @@ export class BotFactory {
|
|
|
320
278
|
*/
|
|
321
279
|
private async setupTokenContract(
|
|
322
280
|
deployer: AztecAddress,
|
|
323
|
-
|
|
281
|
+
salt: Fr,
|
|
324
282
|
name: string,
|
|
325
283
|
ticker: string,
|
|
326
284
|
decimals = 18,
|
|
327
285
|
): Promise<TokenContract> {
|
|
328
|
-
const deployOpts: DeployOptions = { from: deployer
|
|
329
|
-
const deploy = TokenContract.deploy(this.wallet, deployer, name, ticker, decimals);
|
|
286
|
+
const deployOpts: DeployOptions = { from: deployer };
|
|
287
|
+
const deploy = TokenContract.deploy(this.wallet, deployer, name, ticker, decimals, { salt, universalDeploy: true });
|
|
330
288
|
const instance = await this.registerOrDeployContract('Token - ' + name, deploy, deployOpts);
|
|
331
289
|
return TokenContract.at(instance.address, this.wallet);
|
|
332
290
|
}
|
|
333
291
|
|
|
334
292
|
private async setupAmmContract(
|
|
335
293
|
deployer: AztecAddress,
|
|
336
|
-
|
|
294
|
+
salt: Fr,
|
|
337
295
|
token0: TokenContract,
|
|
338
296
|
token1: TokenContract,
|
|
339
297
|
lpToken: TokenContract,
|
|
340
298
|
): Promise<AMMContract> {
|
|
341
|
-
const deployOpts: DeployOptions = { from: deployer
|
|
342
|
-
const deploy = AMMContract.deploy(this.wallet, token0.address, token1.address, lpToken.address
|
|
299
|
+
const deployOpts: DeployOptions = { from: deployer };
|
|
300
|
+
const deploy = AMMContract.deploy(this.wallet, token0.address, token1.address, lpToken.address, {
|
|
301
|
+
salt,
|
|
302
|
+
universalDeploy: true,
|
|
303
|
+
});
|
|
343
304
|
const instance = await this.registerOrDeployContract('AMM', deploy, deployOpts);
|
|
344
305
|
const amm = AMMContract.at(instance.address, this.wallet);
|
|
345
306
|
|
|
346
307
|
this.log.info(`AMM deployed at ${amm.address}`);
|
|
347
308
|
const setMinterInteraction = lpToken.methods.set_minter(amm.address, true);
|
|
348
|
-
const { estimatedGas: setMinterGas } = await setMinterInteraction.simulate({
|
|
349
|
-
from: deployer,
|
|
350
|
-
fee: { estimateGas: true },
|
|
351
|
-
});
|
|
352
309
|
const { receipt: minterReceipt } = await setMinterInteraction.send({
|
|
353
310
|
from: deployer,
|
|
354
|
-
fee: { gasSettings: setMinterGas },
|
|
355
311
|
wait: { timeout: this.config.txMinedWaitSeconds },
|
|
356
312
|
});
|
|
357
|
-
this.log.info(`Set LP token minter to AMM txHash=${minterReceipt.txHash.toString()}
|
|
358
|
-
estimatedGas: setMinterGas,
|
|
359
|
-
});
|
|
313
|
+
this.log.info(`Set LP token minter to AMM txHash=${minterReceipt.txHash.toString()}`);
|
|
360
314
|
this.log.info(`Liquidity token initialized`);
|
|
361
315
|
|
|
362
316
|
return amm;
|
|
@@ -428,17 +382,12 @@ export class BotFactory {
|
|
|
428
382
|
token0.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
|
|
429
383
|
token1.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
|
|
430
384
|
]);
|
|
431
|
-
const { estimatedGas: mintGas } = await mintBatch.simulate({
|
|
432
|
-
from: liquidityProvider,
|
|
433
|
-
fee: { estimateGas: true },
|
|
434
|
-
});
|
|
435
385
|
const { receipt: mintReceipt } = await mintBatch.send({
|
|
436
386
|
from: liquidityProvider,
|
|
437
|
-
fee: { gasSettings: mintGas },
|
|
438
387
|
wait: { timeout: this.config.txMinedWaitSeconds },
|
|
439
388
|
});
|
|
440
389
|
|
|
441
|
-
this.log.info(`Sent mint tx: ${mintReceipt.txHash.toString()}
|
|
390
|
+
this.log.info(`Sent mint tx: ${mintReceipt.txHash.toString()}`);
|
|
442
391
|
|
|
443
392
|
const addLiquidityInteraction = amm.methods.add_liquidity(
|
|
444
393
|
amount0Max,
|
|
@@ -447,21 +396,13 @@ export class BotFactory {
|
|
|
447
396
|
amount1Min,
|
|
448
397
|
authwitNonce,
|
|
449
398
|
);
|
|
450
|
-
const { estimatedGas: addLiquidityGas } = await addLiquidityInteraction.simulate({
|
|
451
|
-
from: liquidityProvider,
|
|
452
|
-
fee: { estimateGas: true },
|
|
453
|
-
authWitnesses: [token0Authwit, token1Authwit],
|
|
454
|
-
});
|
|
455
399
|
const { receipt: addLiquidityReceipt } = await addLiquidityInteraction.send({
|
|
456
400
|
from: liquidityProvider,
|
|
457
|
-
fee: { gasSettings: addLiquidityGas },
|
|
458
401
|
authWitnesses: [token0Authwit, token1Authwit],
|
|
459
402
|
wait: { timeout: this.config.txMinedWaitSeconds },
|
|
460
403
|
});
|
|
461
404
|
|
|
462
|
-
this.log.info(`Sent tx to add liquidity to the AMM: ${addLiquidityReceipt.txHash.toString()}
|
|
463
|
-
estimatedGas: addLiquidityGas,
|
|
464
|
-
});
|
|
405
|
+
this.log.info(`Sent tx to add liquidity to the AMM: ${addLiquidityReceipt.txHash.toString()}`);
|
|
465
406
|
this.log.info(`Liquidity added`);
|
|
466
407
|
|
|
467
408
|
const [newT0Bal, newT1Bal, newLPBal] = await getPrivateBalances();
|
|
@@ -475,28 +416,75 @@ export class BotFactory {
|
|
|
475
416
|
deploy: DeployMethod<T>,
|
|
476
417
|
deployOpts: DeployOptions,
|
|
477
418
|
): Promise<ContractInstanceWithAddress> {
|
|
478
|
-
const instance = await deploy.getInstance(
|
|
419
|
+
const instance = await deploy.getInstance();
|
|
479
420
|
const address = instance.address;
|
|
480
421
|
const metadata = await this.wallet.getContractMetadata(address);
|
|
481
422
|
if (metadata.isContractPublished) {
|
|
482
423
|
this.log.info(`Contract ${name} at ${address.toString()} already deployed`);
|
|
483
424
|
await deploy.register();
|
|
484
|
-
|
|
485
|
-
const { estimatedGas } = await deploy.simulate({ ...deployOpts, fee: { estimateGas: true } });
|
|
486
|
-
this.log.info(`Deploying contract ${name} at ${address.toString()}`, { estimatedGas });
|
|
487
|
-
await this.withNoMinTxsPerBlock(async () => {
|
|
488
|
-
const { txHash } = await deploy.send({ ...deployOpts, fee: { gasSettings: estimatedGas }, wait: NO_WAIT });
|
|
489
|
-
this.log.info(`Sent contract ${name} setup tx with hash ${txHash.toString()}`);
|
|
490
|
-
return waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
491
|
-
});
|
|
425
|
+
return instance;
|
|
492
426
|
}
|
|
427
|
+
|
|
428
|
+
// Setup always runs ensureFeeJuiceBalance before any deploy, so the account pays from its standing
|
|
429
|
+
// balance here. No manual gas estimation: the embedded wallet simulates before sending and derives
|
|
430
|
+
// the gas limits and padded maxFeesPerGas itself.
|
|
431
|
+
this.log.info(`Deploying contract ${name} at ${address.toString()}`);
|
|
432
|
+
await this.withNoMinTxsPerBlock(async () => {
|
|
433
|
+
const { txHash } = await deploy.send({ ...deployOpts, wait: NO_WAIT });
|
|
434
|
+
this.log.info(`Sent contract ${name} deploy tx ${txHash.toString()}`);
|
|
435
|
+
return waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
436
|
+
});
|
|
437
|
+
|
|
493
438
|
return instance;
|
|
494
439
|
}
|
|
495
440
|
|
|
441
|
+
/** True when the config allows bridging fee juice from L1 (fee_juice mode, an L1 RPC, and an L1 key). */
|
|
442
|
+
private isL1BridgingConfigured(): boolean {
|
|
443
|
+
const mnemonicOrPrivateKey = this.config.l1PrivateKey?.getValue() ?? this.config.l1Mnemonic?.getValue();
|
|
444
|
+
return this.config.feePaymentMethod === 'fee_juice' && !!this.config.l1RpcUrls?.length && !!mnemonicOrPrivateKey;
|
|
445
|
+
}
|
|
446
|
+
|
|
496
447
|
/**
|
|
497
|
-
*
|
|
498
|
-
*
|
|
448
|
+
* Ensures the account holds enough fee juice before any other setup step. The account starts empty
|
|
449
|
+
* (initializerless accounts have no deployment tx) and the runtime loop pays fees from this balance and
|
|
450
|
+
* never refuels itself, so every flow funds the account up front. Bridges claims from L1 and consumes
|
|
451
|
+
* each with a claim-only tx until the balance clears the threshold, working from a zero (fresh run) or
|
|
452
|
+
* drained (restart) balance. Each bridge mints a fixed amount well above the threshold, so this is a
|
|
453
|
+
* single bridge in practice. No-op when L1 bridging is not configured or the balance is already above
|
|
454
|
+
* the threshold.
|
|
499
455
|
*/
|
|
456
|
+
private async ensureFeeJuiceBalance(account: AztecAddress): Promise<void> {
|
|
457
|
+
if (!this.isL1BridgingConfigured()) {
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
let balance = await getFeeJuiceBalance(account, this.aztecNode);
|
|
462
|
+
if (balance >= FEE_JUICE_TOP_UP_THRESHOLD) {
|
|
463
|
+
this.log.info(`Fee juice balance ${balance} above threshold ${FEE_JUICE_TOP_UP_THRESHOLD}, skipping top-up`);
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
this.log.info(`Fee juice balance ${balance} below threshold ${FEE_JUICE_TOP_UP_THRESHOLD}, bridging from L1`);
|
|
468
|
+
|
|
469
|
+
while (balance < FEE_JUICE_TOP_UP_THRESHOLD) {
|
|
470
|
+
// Persist the claim before consuming it: if the top-up tx fails or the bot crashes mid-loop, the
|
|
471
|
+
// next run reuses the pending claim instead of bridging again (and wasting the bridged funds).
|
|
472
|
+
const claim = await this.getOrCreateBridgeClaim(account);
|
|
473
|
+
const paymentMethod = new FeeJuicePaymentMethodWithClaim(account, claim);
|
|
474
|
+
|
|
475
|
+
await this.withNoMinTxsPerBlock(async () => {
|
|
476
|
+
const executionPayload = await paymentMethod.getExecutionPayload();
|
|
477
|
+
const { txHash } = await this.wallet.sendTx(executionPayload, { from: account, wait: NO_WAIT });
|
|
478
|
+
this.log.info(`Sent fee juice top-up tx ${txHash.toString()}`);
|
|
479
|
+
return waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
480
|
+
});
|
|
481
|
+
await this.store.deleteBridgeClaim(account);
|
|
482
|
+
balance = await getFeeJuiceBalance(account, this.aztecNode);
|
|
483
|
+
this.log.info(`Fee juice balance after top-up: ${balance}`);
|
|
484
|
+
}
|
|
485
|
+
this.log.info(`Fee juice top-up complete for ${account.toString()}`);
|
|
486
|
+
}
|
|
487
|
+
|
|
500
488
|
private async mintTokens(token: TokenContract | PrivateTokenContract, minter: AztecAddress) {
|
|
501
489
|
const isStandardToken = isStandardTokenContract(token);
|
|
502
490
|
let privateBalance = 0n;
|
|
@@ -530,36 +518,32 @@ export class BotFactory {
|
|
|
530
518
|
// PrivateToken's mint accesses contract-level private storage vars (admin, total_supply).
|
|
531
519
|
const additionalScopes = isStandardToken ? undefined : [token.address];
|
|
532
520
|
const mintBatch = new BatchCall(token.wallet, calls);
|
|
533
|
-
const { estimatedGas } = await mintBatch.simulate({ from: minter, fee: { estimateGas: true }, additionalScopes });
|
|
534
521
|
await this.withNoMinTxsPerBlock(async () => {
|
|
535
522
|
const { txHash } = await mintBatch.send({
|
|
536
523
|
from: minter,
|
|
537
524
|
additionalScopes,
|
|
538
|
-
fee: { gasSettings: estimatedGas },
|
|
539
525
|
wait: NO_WAIT,
|
|
540
526
|
});
|
|
541
|
-
this.log.info(`Sent token mint tx with hash ${txHash.toString()}
|
|
527
|
+
this.log.info(`Sent token mint tx with hash ${txHash.toString()}`);
|
|
542
528
|
return waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
543
529
|
});
|
|
544
530
|
}
|
|
545
531
|
|
|
546
532
|
/**
|
|
547
|
-
*
|
|
548
|
-
*
|
|
549
|
-
*
|
|
533
|
+
* Returns a usable bridge claim for the recipient, reusing a persisted one when its L1→L2 message is
|
|
534
|
+
* still available (resuming a top-up that failed or crashed before the claim was consumed) and bridging
|
|
535
|
+
* a fresh claim otherwise. The caller deletes the claim from the store once it has been consumed.
|
|
550
536
|
*/
|
|
551
537
|
private async getOrCreateBridgeClaim(recipient: AztecAddress): Promise<L2AmountClaim> {
|
|
552
|
-
// Check if we have an existing claim in the store
|
|
553
538
|
const existingClaim = await this.store.getBridgeClaim(recipient);
|
|
554
539
|
if (existingClaim) {
|
|
555
540
|
this.log.info(`Found existing bridge claim for ${recipient.toString()}, checking validity...`);
|
|
556
|
-
|
|
557
|
-
// Check if the message is ready on L2
|
|
558
541
|
try {
|
|
559
542
|
const messageHash = Fr.fromHexString(existingClaim.claim.messageHash);
|
|
560
543
|
await this.withNoMinTxsPerBlock(() =>
|
|
561
544
|
waitForL1ToL2MessageReady(this.aztecNode, messageHash, {
|
|
562
545
|
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds,
|
|
546
|
+
chainTip: this.syncChainTip,
|
|
563
547
|
}),
|
|
564
548
|
);
|
|
565
549
|
return existingClaim.claim;
|
|
@@ -571,7 +555,6 @@ export class BotFactory {
|
|
|
571
555
|
|
|
572
556
|
const claim = await this.bridgeL1FeeJuice(recipient);
|
|
573
557
|
await this.store.saveBridgeClaim(recipient, claim);
|
|
574
|
-
|
|
575
558
|
return claim;
|
|
576
559
|
}
|
|
577
560
|
|
|
@@ -598,6 +581,7 @@ export class BotFactory {
|
|
|
598
581
|
await this.withNoMinTxsPerBlock(() =>
|
|
599
582
|
waitForL1ToL2MessageReady(this.aztecNode, Fr.fromHexString(claim.messageHash), {
|
|
600
583
|
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds,
|
|
584
|
+
chainTip: this.syncChainTip,
|
|
601
585
|
}),
|
|
602
586
|
);
|
|
603
587
|
|
package/src/interface.ts
CHANGED
|
@@ -22,11 +22,11 @@ export interface BotRunnerApi {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
export const BotRunnerApiSchema: ApiSchemaFor<BotRunnerApi> = {
|
|
25
|
-
start: z.function(
|
|
26
|
-
stop: z.function(
|
|
27
|
-
run: z.function(
|
|
28
|
-
setup: z.function(
|
|
29
|
-
getInfo: z.function(
|
|
30
|
-
getConfig: z.function(
|
|
31
|
-
update: z.function(
|
|
25
|
+
start: z.function({ input: z.tuple([]), output: z.void() }),
|
|
26
|
+
stop: z.function({ input: z.tuple([]), output: z.void() }),
|
|
27
|
+
run: z.function({ input: z.tuple([]), output: z.void() }),
|
|
28
|
+
setup: z.function({ input: z.tuple([]), output: z.void() }),
|
|
29
|
+
getInfo: z.function({ input: z.tuple([]), output: BotInfoSchema }),
|
|
30
|
+
getConfig: z.function({ input: z.tuple([]), output: BotConfigSchema }),
|
|
31
|
+
update: z.function({ input: z.tuple([BotConfigSchema]), output: z.void() }),
|
|
32
32
|
};
|
package/src/runner.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { createLogger } from '@aztec/aztec.js/log';
|
|
|
2
2
|
import type { AztecNode } from '@aztec/aztec.js/node';
|
|
3
3
|
import { omit } from '@aztec/foundation/collection';
|
|
4
4
|
import { RunningPromise } from '@aztec/foundation/running-promise';
|
|
5
|
+
import type { BlockTag } from '@aztec/stdlib/block';
|
|
5
6
|
import type { AztecNodeAdmin } from '@aztec/stdlib/interfaces/client';
|
|
6
7
|
import { type TelemetryClient, type Traceable, type Tracer, trackSpan } from '@aztec/telemetry-client';
|
|
7
8
|
import type { EmbeddedWallet } from '@aztec/wallets/embedded';
|
|
@@ -30,6 +31,7 @@ export class BotRunner implements BotRunnerApi, Traceable {
|
|
|
30
31
|
private readonly telemetry: TelemetryClient,
|
|
31
32
|
private readonly aztecNodeAdmin: AztecNodeAdmin | undefined,
|
|
32
33
|
private readonly store: BotStore,
|
|
34
|
+
private readonly syncChainTip?: BlockTag,
|
|
33
35
|
) {
|
|
34
36
|
this.tracer = telemetry.getTracer('Bot');
|
|
35
37
|
|
|
@@ -149,13 +151,34 @@ export class BotRunner implements BotRunnerApi, Traceable {
|
|
|
149
151
|
try {
|
|
150
152
|
switch (this.config.botMode) {
|
|
151
153
|
case 'crosschain':
|
|
152
|
-
this.bot = CrossChainBot.create(
|
|
154
|
+
this.bot = CrossChainBot.create(
|
|
155
|
+
this.config,
|
|
156
|
+
this.wallet,
|
|
157
|
+
this.aztecNode,
|
|
158
|
+
this.aztecNodeAdmin,
|
|
159
|
+
this.store,
|
|
160
|
+
this.syncChainTip,
|
|
161
|
+
);
|
|
153
162
|
break;
|
|
154
163
|
case 'amm':
|
|
155
|
-
this.bot = AmmBot.create(
|
|
164
|
+
this.bot = AmmBot.create(
|
|
165
|
+
this.config,
|
|
166
|
+
this.wallet,
|
|
167
|
+
this.aztecNode,
|
|
168
|
+
this.aztecNodeAdmin,
|
|
169
|
+
this.store,
|
|
170
|
+
this.syncChainTip,
|
|
171
|
+
);
|
|
156
172
|
break;
|
|
157
173
|
case 'transfer':
|
|
158
|
-
this.bot = Bot.create(
|
|
174
|
+
this.bot = Bot.create(
|
|
175
|
+
this.config,
|
|
176
|
+
this.wallet,
|
|
177
|
+
this.aztecNode,
|
|
178
|
+
this.aztecNodeAdmin,
|
|
179
|
+
this.store,
|
|
180
|
+
this.syncChainTip,
|
|
181
|
+
);
|
|
159
182
|
break;
|
|
160
183
|
default: {
|
|
161
184
|
const _exhaustive: never = this.config.botMode;
|