@aztec/bot 0.0.1-commit.2448fdb → 0.0.1-commit.2606882
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 +2 -2
- package/dest/amm_bot.d.ts.map +1 -1
- package/dest/base_bot.d.ts +2 -2
- package/dest/base_bot.d.ts.map +1 -1
- package/dest/config.d.ts +29 -80
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +1 -2
- package/dest/cross_chain_bot.d.ts +2 -2
- package/dest/cross_chain_bot.d.ts.map +1 -1
- package/dest/cross_chain_bot.js +6 -3
- package/dest/factory.d.ts +6 -1
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +230 -61
- package/dest/interface.d.ts +2 -6
- package/dest/interface.d.ts.map +1 -1
- package/dest/interface.js +30 -7
- package/package.json +16 -16
- package/src/amm_bot.ts +1 -1
- package/src/base_bot.ts +2 -1
- package/src/config.ts +1 -2
- package/src/cross_chain_bot.ts +5 -4
- package/src/factory.ts +233 -61
- package/src/interface.ts +7 -7
package/src/factory.ts
CHANGED
|
@@ -16,6 +16,7 @@ import { deriveKeys } from '@aztec/aztec.js/keys';
|
|
|
16
16
|
import { createLogger } from '@aztec/aztec.js/log';
|
|
17
17
|
import { waitForL1ToL2MessageReady } from '@aztec/aztec.js/messaging';
|
|
18
18
|
import { waitForTx } from '@aztec/aztec.js/node';
|
|
19
|
+
import { getFeeJuiceBalance } from '@aztec/aztec.js/utils';
|
|
19
20
|
import { ContractInitializationStatus } from '@aztec/aztec.js/wallet';
|
|
20
21
|
import { createEthereumChain } from '@aztec/ethereum/chain';
|
|
21
22
|
import { createExtendedL1Client } from '@aztec/ethereum/client';
|
|
@@ -29,6 +30,7 @@ import { PrivateTokenContract } from '@aztec/noir-contracts.js/PrivateToken';
|
|
|
29
30
|
import { TokenContract } from '@aztec/noir-contracts.js/Token';
|
|
30
31
|
import { TestContract } from '@aztec/noir-test-contracts.js/Test';
|
|
31
32
|
import type { ContractInstanceWithAddress } from '@aztec/stdlib/contract';
|
|
33
|
+
import { GasFees, GasSettings, ManaUsageEstimate } from '@aztec/stdlib/gas';
|
|
32
34
|
import type { AztecNode, AztecNodeAdmin } from '@aztec/stdlib/interfaces/client';
|
|
33
35
|
import { deriveSigningKey } from '@aztec/stdlib/keys';
|
|
34
36
|
import { EmbeddedWallet } from '@aztec/wallets/embedded';
|
|
@@ -40,6 +42,8 @@ import { getBalances, getPrivateBalance, isStandardTokenContract } from './utils
|
|
|
40
42
|
|
|
41
43
|
const MINT_BALANCE = 1e12;
|
|
42
44
|
const MIN_BALANCE = 1e3;
|
|
45
|
+
const FEE_JUICE_TOP_UP_THRESHOLD = 100n * 10n ** 18n;
|
|
46
|
+
const FEE_JUICE_TOP_UP_TARGET = 10_000n * 10n ** 18n;
|
|
43
47
|
|
|
44
48
|
export class BotFactory {
|
|
45
49
|
private log = createLogger('bot');
|
|
@@ -69,7 +73,8 @@ export class BotFactory {
|
|
|
69
73
|
}> {
|
|
70
74
|
const defaultAccountAddress = await this.setupAccount();
|
|
71
75
|
const recipient = (await this.wallet.createSchnorrAccount(Fr.random(), Fr.random())).address;
|
|
72
|
-
const token = await this.
|
|
76
|
+
const token = await this.setupTokenWithOptionalEarlyRefuel(defaultAccountAddress);
|
|
77
|
+
await this.ensureFeeJuiceBalance(defaultAccountAddress, token);
|
|
73
78
|
await this.mintTokens(token, defaultAccountAddress);
|
|
74
79
|
return { wallet: this.wallet, defaultAccountAddress, token, node: this.aztecNode, recipient };
|
|
75
80
|
}
|
|
@@ -83,7 +88,13 @@ export class BotFactory {
|
|
|
83
88
|
node: AztecNode;
|
|
84
89
|
}> {
|
|
85
90
|
const defaultAccountAddress = await this.setupAccount();
|
|
86
|
-
const token0 = await this.
|
|
91
|
+
const token0 = await this.setupTokenContractWithOptionalEarlyRefuel(
|
|
92
|
+
defaultAccountAddress,
|
|
93
|
+
this.config.tokenSalt,
|
|
94
|
+
'BotToken0',
|
|
95
|
+
'BOT0',
|
|
96
|
+
);
|
|
97
|
+
await this.ensureFeeJuiceBalance(defaultAccountAddress, token0);
|
|
87
98
|
const token1 = await this.setupTokenContract(defaultAccountAddress, this.config.tokenSalt, 'BotToken1', 'BOT1');
|
|
88
99
|
const liquidityToken = await this.setupTokenContract(
|
|
89
100
|
defaultAccountAddress,
|
|
@@ -178,12 +189,8 @@ export class BotFactory {
|
|
|
178
189
|
}
|
|
179
190
|
|
|
180
191
|
private async setupTestContract(deployer: AztecAddress): Promise<TestContract> {
|
|
181
|
-
const deployOpts: DeployOptions = {
|
|
182
|
-
|
|
183
|
-
contractAddressSalt: this.config.tokenSalt,
|
|
184
|
-
universalDeploy: true,
|
|
185
|
-
};
|
|
186
|
-
const deploy = TestContract.deploy(this.wallet);
|
|
192
|
+
const deployOpts: DeployOptions = { from: deployer };
|
|
193
|
+
const deploy = TestContract.deploy(this.wallet, { salt: this.config.tokenSalt, universalDeploy: true });
|
|
187
194
|
const instance = await this.registerOrDeployContract('TestContract', deploy, deployOpts);
|
|
188
195
|
return TestContract.at(instance.address, this.wallet);
|
|
189
196
|
}
|
|
@@ -252,35 +259,95 @@ export class BotFactory {
|
|
|
252
259
|
return accountManager.address;
|
|
253
260
|
}
|
|
254
261
|
|
|
262
|
+
/**
|
|
263
|
+
* Setup token and refuel first: if the token already exists (restart scenario),
|
|
264
|
+
* run ensureFeeJuiceBalance before any step that might need fee juice. When deploying,
|
|
265
|
+
* use a bridge claim if balance is below threshold.
|
|
266
|
+
*/
|
|
267
|
+
private async setupTokenWithOptionalEarlyRefuel(sender: AztecAddress): Promise<TokenContract | PrivateTokenContract> {
|
|
268
|
+
const token = await this.getTokenInstance(sender);
|
|
269
|
+
const address = token.address;
|
|
270
|
+
const metadata = await this.wallet.getContractMetadata(address);
|
|
271
|
+
if (metadata.isContractPublished) {
|
|
272
|
+
this.log.info(`Token at ${address.toString()} already deployed, refueling before setup`);
|
|
273
|
+
await this.ensureFeeJuiceBalance(sender, token);
|
|
274
|
+
}
|
|
275
|
+
return this.setupToken(sender);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Setup token0 for AMM with refuel-first behaviour when token already exists.
|
|
280
|
+
*/
|
|
281
|
+
private async setupTokenContractWithOptionalEarlyRefuel(
|
|
282
|
+
deployer: AztecAddress,
|
|
283
|
+
salt: Fr,
|
|
284
|
+
name: string,
|
|
285
|
+
ticker: string,
|
|
286
|
+
decimals = 18,
|
|
287
|
+
): Promise<TokenContract> {
|
|
288
|
+
const deploy = TokenContract.deploy(this.wallet, deployer, name, ticker, decimals, { salt, universalDeploy: true });
|
|
289
|
+
const instance = await deploy.getInstance();
|
|
290
|
+
const metadata = await this.wallet.getContractMetadata(instance.address);
|
|
291
|
+
if (metadata.isContractPublished) {
|
|
292
|
+
this.log.info(`Token ${name} at ${instance.address.toString()} already deployed, refueling before setup`);
|
|
293
|
+
const token = TokenContract.at(instance.address, this.wallet);
|
|
294
|
+
await this.ensureFeeJuiceBalance(deployer, token);
|
|
295
|
+
}
|
|
296
|
+
return this.setupTokenContract(deployer, salt, name, ticker, decimals);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
private async getTokenInstance(sender: AztecAddress): Promise<TokenContract | PrivateTokenContract> {
|
|
300
|
+
const salt = this.config.tokenSalt;
|
|
301
|
+
if (this.config.contract === SupportedTokenContracts.TokenContract) {
|
|
302
|
+
const deploy = TokenContract.deploy(this.wallet, sender, 'BotToken', 'BOT', 18, { salt, universalDeploy: true });
|
|
303
|
+
const instance = await deploy.getInstance();
|
|
304
|
+
return TokenContract.at(instance.address, this.wallet);
|
|
305
|
+
}
|
|
306
|
+
if (this.config.contract === SupportedTokenContracts.PrivateTokenContract) {
|
|
307
|
+
const tokenSecretKey = Fr.random();
|
|
308
|
+
const tokenPublicKeys = (await deriveKeys(tokenSecretKey)).publicKeys;
|
|
309
|
+
const deploy = PrivateTokenContract.deploy(this.wallet, MINT_BALANCE, sender, {
|
|
310
|
+
salt,
|
|
311
|
+
universalDeploy: true,
|
|
312
|
+
publicKeys: tokenPublicKeys,
|
|
313
|
+
});
|
|
314
|
+
const instance = await deploy.getInstance();
|
|
315
|
+
return PrivateTokenContract.at(instance.address, this.wallet);
|
|
316
|
+
}
|
|
317
|
+
throw new Error(`Unsupported token contract type: ${this.config.contract}`);
|
|
318
|
+
}
|
|
319
|
+
|
|
255
320
|
/**
|
|
256
321
|
* Checks if the token contract is deployed and deploys it if necessary.
|
|
257
|
-
*
|
|
258
|
-
* @
|
|
322
|
+
* Uses a bridge claim for deploy when balance is below threshold to avoid failing before refuel.
|
|
323
|
+
* @param sender - Aztec address to deploy the token contract from.
|
|
324
|
+
* @param existingToken - Optional token instance when called from setupTokenWithOptionalEarlyRefuel.
|
|
325
|
+
* @returns The TokenContract or PrivateTokenContract instance.
|
|
259
326
|
*/
|
|
260
327
|
private async setupToken(sender: AztecAddress): Promise<TokenContract | PrivateTokenContract> {
|
|
261
328
|
let deploy: DeployMethod<TokenContract | PrivateTokenContract>;
|
|
262
|
-
|
|
263
|
-
const deployOpts: DeployOptions = {
|
|
264
|
-
from: sender,
|
|
265
|
-
contractAddressSalt: this.config.tokenSalt,
|
|
266
|
-
universalDeploy: true,
|
|
267
|
-
};
|
|
329
|
+
const salt = this.config.tokenSalt;
|
|
330
|
+
const deployOpts: DeployOptions = { from: sender };
|
|
268
331
|
let token: TokenContract | PrivateTokenContract;
|
|
269
332
|
if (this.config.contract === SupportedTokenContracts.TokenContract) {
|
|
270
|
-
deploy = TokenContract.deploy(this.wallet, sender, 'BotToken', 'BOT', 18);
|
|
271
|
-
|
|
272
|
-
token = TokenContract.at(
|
|
333
|
+
deploy = TokenContract.deploy(this.wallet, sender, 'BotToken', 'BOT', 18, { salt, universalDeploy: true });
|
|
334
|
+
const instance = await deploy.getInstance();
|
|
335
|
+
token = TokenContract.at(instance.address, this.wallet);
|
|
273
336
|
} else if (this.config.contract === SupportedTokenContracts.PrivateTokenContract) {
|
|
274
337
|
// Generate keys for the contract since PrivateToken uses SinglePrivateMutable which requires keys
|
|
275
338
|
const tokenSecretKey = Fr.random();
|
|
276
339
|
const tokenPublicKeys = (await deriveKeys(tokenSecretKey)).publicKeys;
|
|
277
|
-
deploy = PrivateTokenContract.
|
|
340
|
+
deploy = PrivateTokenContract.deploy(this.wallet, MINT_BALANCE, sender, {
|
|
341
|
+
salt,
|
|
342
|
+
universalDeploy: true,
|
|
343
|
+
publicKeys: tokenPublicKeys,
|
|
344
|
+
});
|
|
278
345
|
deployOpts.skipInstancePublication = true;
|
|
279
346
|
deployOpts.skipClassPublication = true;
|
|
280
347
|
deployOpts.skipInitialization = false;
|
|
281
348
|
|
|
282
349
|
// Register the contract with the secret key before deployment
|
|
283
|
-
tokenInstance = await deploy.getInstance(
|
|
350
|
+
const tokenInstance = await deploy.getInstance();
|
|
284
351
|
token = PrivateTokenContract.at(tokenInstance.address, this.wallet);
|
|
285
352
|
await this.wallet.registerContract(tokenInstance, PrivateTokenContract.artifact, tokenSecretKey);
|
|
286
353
|
// The contract constructor initializes private storage vars that need the contract's own nullifier key.
|
|
@@ -289,20 +356,7 @@ export class BotFactory {
|
|
|
289
356
|
throw new Error(`Unsupported token contract type: ${this.config.contract}`);
|
|
290
357
|
}
|
|
291
358
|
|
|
292
|
-
|
|
293
|
-
const metadata = await this.wallet.getContractMetadata(address);
|
|
294
|
-
if (metadata.isContractPublished) {
|
|
295
|
-
this.log.info(`Token at ${address.toString()} already deployed`);
|
|
296
|
-
await deploy.register();
|
|
297
|
-
} else {
|
|
298
|
-
this.log.info(`Deploying token contract at ${address.toString()}`);
|
|
299
|
-
const { txHash } = await deploy.send({ ...deployOpts, wait: NO_WAIT });
|
|
300
|
-
this.log.info(`Sent tx for token setup with hash ${txHash.toString()}`);
|
|
301
|
-
await this.withNoMinTxsPerBlock(async () => {
|
|
302
|
-
await waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
303
|
-
return token;
|
|
304
|
-
});
|
|
305
|
-
}
|
|
359
|
+
await this.registerOrDeployContract('token', deploy, deployOpts);
|
|
306
360
|
return token;
|
|
307
361
|
}
|
|
308
362
|
|
|
@@ -313,33 +367,38 @@ export class BotFactory {
|
|
|
313
367
|
*/
|
|
314
368
|
private async setupTokenContract(
|
|
315
369
|
deployer: AztecAddress,
|
|
316
|
-
|
|
370
|
+
salt: Fr,
|
|
317
371
|
name: string,
|
|
318
372
|
ticker: string,
|
|
319
373
|
decimals = 18,
|
|
320
374
|
): Promise<TokenContract> {
|
|
321
|
-
const deployOpts: DeployOptions = { from: deployer
|
|
322
|
-
const deploy = TokenContract.deploy(this.wallet, deployer, name, ticker, decimals);
|
|
375
|
+
const deployOpts: DeployOptions = { from: deployer };
|
|
376
|
+
const deploy = TokenContract.deploy(this.wallet, deployer, name, ticker, decimals, { salt, universalDeploy: true });
|
|
323
377
|
const instance = await this.registerOrDeployContract('Token - ' + name, deploy, deployOpts);
|
|
324
378
|
return TokenContract.at(instance.address, this.wallet);
|
|
325
379
|
}
|
|
326
380
|
|
|
327
381
|
private async setupAmmContract(
|
|
328
382
|
deployer: AztecAddress,
|
|
329
|
-
|
|
383
|
+
salt: Fr,
|
|
330
384
|
token0: TokenContract,
|
|
331
385
|
token1: TokenContract,
|
|
332
386
|
lpToken: TokenContract,
|
|
333
387
|
): Promise<AMMContract> {
|
|
334
|
-
const deployOpts: DeployOptions = { from: deployer
|
|
335
|
-
const deploy = AMMContract.deploy(this.wallet, token0.address, token1.address, lpToken.address
|
|
388
|
+
const deployOpts: DeployOptions = { from: deployer };
|
|
389
|
+
const deploy = AMMContract.deploy(this.wallet, token0.address, token1.address, lpToken.address, {
|
|
390
|
+
salt,
|
|
391
|
+
universalDeploy: true,
|
|
392
|
+
});
|
|
336
393
|
const instance = await this.registerOrDeployContract('AMM', deploy, deployOpts);
|
|
337
394
|
const amm = AMMContract.at(instance.address, this.wallet);
|
|
338
395
|
|
|
339
396
|
this.log.info(`AMM deployed at ${amm.address}`);
|
|
340
|
-
const
|
|
341
|
-
|
|
342
|
-
|
|
397
|
+
const setMinterInteraction = lpToken.methods.set_minter(amm.address, true);
|
|
398
|
+
const { receipt: minterReceipt } = await setMinterInteraction.send({
|
|
399
|
+
from: deployer,
|
|
400
|
+
wait: { timeout: this.config.txMinedWaitSeconds },
|
|
401
|
+
});
|
|
343
402
|
this.log.info(`Set LP token minter to AMM txHash=${minterReceipt.txHash.toString()}`);
|
|
344
403
|
this.log.info(`Liquidity token initialized`);
|
|
345
404
|
|
|
@@ -408,20 +467,29 @@ export class BotFactory {
|
|
|
408
467
|
.getFunctionCall(),
|
|
409
468
|
});
|
|
410
469
|
|
|
411
|
-
const
|
|
470
|
+
const mintBatch = new BatchCall(this.wallet, [
|
|
412
471
|
token0.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
|
|
413
472
|
token1.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
|
|
414
|
-
])
|
|
473
|
+
]);
|
|
474
|
+
const { receipt: mintReceipt } = await mintBatch.send({
|
|
475
|
+
from: liquidityProvider,
|
|
476
|
+
wait: { timeout: this.config.txMinedWaitSeconds },
|
|
477
|
+
});
|
|
415
478
|
|
|
416
479
|
this.log.info(`Sent mint tx: ${mintReceipt.txHash.toString()}`);
|
|
417
480
|
|
|
418
|
-
const
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
481
|
+
const addLiquidityInteraction = amm.methods.add_liquidity(
|
|
482
|
+
amount0Max,
|
|
483
|
+
amount1Max,
|
|
484
|
+
amount0Min,
|
|
485
|
+
amount1Min,
|
|
486
|
+
authwitNonce,
|
|
487
|
+
);
|
|
488
|
+
const { receipt: addLiquidityReceipt } = await addLiquidityInteraction.send({
|
|
489
|
+
from: liquidityProvider,
|
|
490
|
+
authWitnesses: [token0Authwit, token1Authwit],
|
|
491
|
+
wait: { timeout: this.config.txMinedWaitSeconds },
|
|
492
|
+
});
|
|
425
493
|
|
|
426
494
|
this.log.info(`Sent tx to add liquidity to the AMM: ${addLiquidityReceipt.txHash.toString()}`);
|
|
427
495
|
this.log.info(`Liquidity added`);
|
|
@@ -437,19 +505,49 @@ export class BotFactory {
|
|
|
437
505
|
deploy: DeployMethod<T>,
|
|
438
506
|
deployOpts: DeployOptions,
|
|
439
507
|
): Promise<ContractInstanceWithAddress> {
|
|
440
|
-
const instance = await deploy.getInstance(
|
|
508
|
+
const instance = await deploy.getInstance();
|
|
441
509
|
const address = instance.address;
|
|
442
510
|
const metadata = await this.wallet.getContractMetadata(address);
|
|
443
511
|
if (metadata.isContractPublished) {
|
|
444
512
|
this.log.info(`Contract ${name} at ${address.toString()} already deployed`);
|
|
445
513
|
await deploy.register();
|
|
446
514
|
} else {
|
|
447
|
-
|
|
448
|
-
await this.
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
515
|
+
const sender = deployOpts.from === NO_FROM ? undefined : deployOpts.from;
|
|
516
|
+
const balance = sender ? await getFeeJuiceBalance(sender, this.aztecNode) : 0n;
|
|
517
|
+
const useClaim =
|
|
518
|
+
sender &&
|
|
519
|
+
balance < FEE_JUICE_TOP_UP_THRESHOLD &&
|
|
520
|
+
this.config.feePaymentMethod === 'fee_juice' &&
|
|
521
|
+
!!this.config.l1RpcUrls?.length;
|
|
522
|
+
const mnemonicOrPrivateKey = this.config.l1PrivateKey?.getValue() ?? this.config.l1Mnemonic?.getValue();
|
|
523
|
+
|
|
524
|
+
if (useClaim && mnemonicOrPrivateKey) {
|
|
525
|
+
const claim = await this.getOrCreateBridgeClaim(sender!);
|
|
526
|
+
const paymentMethod = new FeeJuicePaymentMethodWithClaim(sender!, claim);
|
|
527
|
+
const { estimatedGas } = await deploy.simulate({ ...deployOpts, fee: { estimateGas: true, paymentMethod } });
|
|
528
|
+
const maxFeesPerGas = (await this.getMinFees()).mul(1 + this.config.minFeePadding);
|
|
529
|
+
const gasSettings = GasSettings.from({
|
|
530
|
+
...estimatedGas!,
|
|
531
|
+
maxFeesPerGas,
|
|
532
|
+
maxPriorityFeesPerGas: GasFees.empty(),
|
|
533
|
+
});
|
|
534
|
+
await this.withNoMinTxsPerBlock(async () => {
|
|
535
|
+
const { txHash } = await deploy.send({ ...deployOpts, fee: { gasSettings, paymentMethod }, wait: NO_WAIT });
|
|
536
|
+
this.log.info(
|
|
537
|
+
`Sent contract ${name} deploy tx ${txHash.toString()} (using bridge claim, balance was ${balance})`,
|
|
538
|
+
);
|
|
539
|
+
return waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
540
|
+
});
|
|
541
|
+
await this.store.deleteBridgeClaim(sender!);
|
|
542
|
+
} else {
|
|
543
|
+
const { estimatedGas } = await deploy.simulate({ ...deployOpts, fee: { estimateGas: true } });
|
|
544
|
+
this.log.info(`Deploying contract ${name} at ${address.toString()}`, { estimatedGas });
|
|
545
|
+
await this.withNoMinTxsPerBlock(async () => {
|
|
546
|
+
const { txHash } = await deploy.send({ ...deployOpts, fee: { gasSettings: estimatedGas }, wait: NO_WAIT });
|
|
547
|
+
this.log.info(`Sent contract ${name} setup tx with hash ${txHash.toString()}`);
|
|
548
|
+
return waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
549
|
+
});
|
|
550
|
+
}
|
|
453
551
|
}
|
|
454
552
|
return instance;
|
|
455
553
|
}
|
|
@@ -458,6 +556,66 @@ export class BotFactory {
|
|
|
458
556
|
* Mints private and public tokens for the sender if their balance is below the minimum.
|
|
459
557
|
* @param token - Token contract.
|
|
460
558
|
*/
|
|
559
|
+
/**
|
|
560
|
+
* Ensures the account has sufficient fee juice by bridging from L1 if balance is below threshold.
|
|
561
|
+
* Bridges repeatedly until balance reaches the target (10k FJ).
|
|
562
|
+
* Used on startup/restart to top up when the account has run out after previous runs.
|
|
563
|
+
*/
|
|
564
|
+
private async ensureFeeJuiceBalance(
|
|
565
|
+
account: AztecAddress,
|
|
566
|
+
token: TokenContract | PrivateTokenContract,
|
|
567
|
+
): Promise<void> {
|
|
568
|
+
const { feePaymentMethod, l1RpcUrls } = this.config;
|
|
569
|
+
if (feePaymentMethod !== 'fee_juice' || !l1RpcUrls?.length) {
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
const mnemonicOrPrivateKey = this.config.l1PrivateKey?.getValue() ?? this.config.l1Mnemonic?.getValue();
|
|
573
|
+
if (!mnemonicOrPrivateKey) {
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
let balance = await getFeeJuiceBalance(account, this.aztecNode);
|
|
578
|
+
if (balance >= FEE_JUICE_TOP_UP_THRESHOLD) {
|
|
579
|
+
this.log.info(`Fee juice balance ${balance} above threshold ${FEE_JUICE_TOP_UP_THRESHOLD}, skipping top-up`);
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
this.log.info(
|
|
584
|
+
`Fee juice balance ${balance} below threshold ${FEE_JUICE_TOP_UP_THRESHOLD}, bridging from L1 until ${FEE_JUICE_TOP_UP_TARGET}`,
|
|
585
|
+
);
|
|
586
|
+
const maxFeesPerGas = (await this.getMinFees()).mul(1 + this.config.minFeePadding);
|
|
587
|
+
const minimalInteraction = isStandardTokenContract(token)
|
|
588
|
+
? token.methods.transfer_in_public(account, account, 0n, 0)
|
|
589
|
+
: token.methods.transfer(0n, account, account);
|
|
590
|
+
|
|
591
|
+
while (balance < FEE_JUICE_TOP_UP_TARGET) {
|
|
592
|
+
const claim = await this.bridgeL1FeeJuice(account);
|
|
593
|
+
const paymentMethod = new FeeJuicePaymentMethodWithClaim(account, claim);
|
|
594
|
+
const { estimatedGas } = await minimalInteraction.simulate({
|
|
595
|
+
from: account,
|
|
596
|
+
fee: { estimateGas: true, paymentMethod },
|
|
597
|
+
});
|
|
598
|
+
const gasSettings = GasSettings.from({
|
|
599
|
+
...estimatedGas!,
|
|
600
|
+
maxFeesPerGas,
|
|
601
|
+
maxPriorityFeesPerGas: GasFees.empty(),
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
await this.withNoMinTxsPerBlock(async () => {
|
|
605
|
+
const { txHash } = await minimalInteraction.send({
|
|
606
|
+
from: account,
|
|
607
|
+
fee: { gasSettings, paymentMethod },
|
|
608
|
+
wait: NO_WAIT,
|
|
609
|
+
});
|
|
610
|
+
this.log.info(`Sent fee juice top-up tx ${txHash.toString()}`);
|
|
611
|
+
return waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
612
|
+
});
|
|
613
|
+
balance = await getFeeJuiceBalance(account, this.aztecNode);
|
|
614
|
+
this.log.info(`Fee juice balance after top-up: ${balance}`);
|
|
615
|
+
}
|
|
616
|
+
this.log.info(`Fee juice top-up complete for ${account.toString()}`);
|
|
617
|
+
}
|
|
618
|
+
|
|
461
619
|
private async mintTokens(token: TokenContract | PrivateTokenContract, minter: AztecAddress) {
|
|
462
620
|
const isStandardToken = isStandardTokenContract(token);
|
|
463
621
|
let privateBalance = 0n;
|
|
@@ -490,8 +648,9 @@ export class BotFactory {
|
|
|
490
648
|
|
|
491
649
|
// PrivateToken's mint accesses contract-level private storage vars (admin, total_supply).
|
|
492
650
|
const additionalScopes = isStandardToken ? undefined : [token.address];
|
|
651
|
+
const mintBatch = new BatchCall(token.wallet, calls);
|
|
493
652
|
await this.withNoMinTxsPerBlock(async () => {
|
|
494
|
-
const { txHash } = await
|
|
653
|
+
const { txHash } = await mintBatch.send({
|
|
495
654
|
from: minter,
|
|
496
655
|
additionalScopes,
|
|
497
656
|
wait: NO_WAIT,
|
|
@@ -564,6 +723,19 @@ export class BotFactory {
|
|
|
564
723
|
return claim as L2AmountClaim;
|
|
565
724
|
}
|
|
566
725
|
|
|
726
|
+
/** Returns worst-case min fees across predicted slots, with fallback to current min fees. */
|
|
727
|
+
private async getMinFees(): Promise<GasFees> {
|
|
728
|
+
try {
|
|
729
|
+
const predicted = await this.aztecNode.getPredictedMinFees(ManaUsageEstimate.Limit);
|
|
730
|
+
if (predicted.length === 0) {
|
|
731
|
+
return this.aztecNode.getCurrentMinFees();
|
|
732
|
+
}
|
|
733
|
+
return predicted.reduce((worst, fees) => (fees.feePerL2Gas > worst.feePerL2Gas ? fees : worst));
|
|
734
|
+
} catch {
|
|
735
|
+
return this.aztecNode.getCurrentMinFees();
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
|
|
567
739
|
private async withNoMinTxsPerBlock<T>(fn: () => Promise<T>): Promise<T> {
|
|
568
740
|
if (!this.aztecNodeAdmin || !this.config.flushSetupTransactions) {
|
|
569
741
|
this.log.verbose(`No node admin client or flushing not requested (not setting minTxsPerBlock to 0)`);
|
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
|
};
|