@aztec/bot 0.0.0-test.1 → 0.0.1-commit.1142ef1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/dest/amm_bot.d.ts +33 -0
  2. package/dest/amm_bot.d.ts.map +1 -0
  3. package/dest/amm_bot.js +97 -0
  4. package/dest/base_bot.d.ts +21 -0
  5. package/dest/base_bot.d.ts.map +1 -0
  6. package/dest/base_bot.js +80 -0
  7. package/dest/bot.d.ts +13 -18
  8. package/dest/bot.d.ts.map +1 -1
  9. package/dest/bot.js +24 -86
  10. package/dest/config.d.ts +84 -60
  11. package/dest/config.d.ts.map +1 -1
  12. package/dest/config.js +59 -36
  13. package/dest/factory.d.ts +31 -27
  14. package/dest/factory.d.ts.map +1 -1
  15. package/dest/factory.js +288 -134
  16. package/dest/index.d.ts +4 -2
  17. package/dest/index.d.ts.map +1 -1
  18. package/dest/index.js +3 -1
  19. package/dest/interface.d.ts +12 -1
  20. package/dest/interface.d.ts.map +1 -1
  21. package/dest/interface.js +5 -0
  22. package/dest/rpc.d.ts +1 -7
  23. package/dest/rpc.d.ts.map +1 -1
  24. package/dest/rpc.js +0 -11
  25. package/dest/runner.d.ts +15 -11
  26. package/dest/runner.d.ts.map +1 -1
  27. package/dest/runner.js +441 -51
  28. package/dest/store/bot_store.d.ts +44 -0
  29. package/dest/store/bot_store.d.ts.map +1 -0
  30. package/dest/store/bot_store.js +107 -0
  31. package/dest/store/index.d.ts +2 -0
  32. package/dest/store/index.d.ts.map +1 -0
  33. package/dest/store/index.js +1 -0
  34. package/dest/utils.d.ts +8 -5
  35. package/dest/utils.d.ts.map +1 -1
  36. package/dest/utils.js +14 -5
  37. package/package.json +27 -23
  38. package/src/amm_bot.ts +124 -0
  39. package/src/base_bot.ts +96 -0
  40. package/src/bot.ts +52 -103
  41. package/src/config.ts +98 -68
  42. package/src/factory.ts +348 -148
  43. package/src/index.ts +3 -1
  44. package/src/interface.ts +9 -0
  45. package/src/rpc.ts +0 -13
  46. package/src/runner.ts +38 -21
  47. package/src/store/bot_store.ts +141 -0
  48. package/src/store/index.ts +1 -0
  49. package/src/utils.ts +17 -6
package/src/factory.ts CHANGED
@@ -1,72 +1,97 @@
1
- import { getSchnorrAccount } from '@aztec/accounts/schnorr';
2
- import { getDeployedTestAccountsWallets, getInitialTestAccounts } from '@aztec/accounts/testing';
1
+ import { SchnorrAccountContract } from '@aztec/accounts/schnorr';
2
+ import { getInitialTestAccountsData } from '@aztec/accounts/testing';
3
+ import { AztecAddress } from '@aztec/aztec.js/addresses';
3
4
  import {
4
- type AccountWallet,
5
- AztecAddress,
6
- type AztecNode,
7
5
  BatchCall,
6
+ ContractBase,
7
+ ContractFunctionInteraction,
8
8
  type DeployMethod,
9
9
  type DeployOptions,
10
- FeeJuicePaymentMethodWithClaim,
11
- L1FeeJuicePortalManager,
12
- type PXE,
13
- createLogger,
14
- createPXEClient,
15
- retryUntil,
16
- } from '@aztec/aztec.js';
17
- import { createEthereumChain, createL1Clients } from '@aztec/ethereum';
18
- import { Fr } from '@aztec/foundation/fields';
19
- import { EasyPrivateTokenContract } from '@aztec/noir-contracts.js/EasyPrivateToken';
10
+ } from '@aztec/aztec.js/contracts';
11
+ import { L1FeeJuicePortalManager } from '@aztec/aztec.js/ethereum';
12
+ import type { L2AmountClaim } from '@aztec/aztec.js/ethereum';
13
+ import { FeeJuicePaymentMethodWithClaim } from '@aztec/aztec.js/fee';
14
+ import { deriveKeys } from '@aztec/aztec.js/keys';
15
+ import { createLogger } from '@aztec/aztec.js/log';
16
+ import { waitForL1ToL2MessageReady } from '@aztec/aztec.js/messaging';
17
+ import { createEthereumChain } from '@aztec/ethereum/chain';
18
+ import { createExtendedL1Client } from '@aztec/ethereum/client';
19
+ import { Fr } from '@aztec/foundation/curves/bn254';
20
+ import { Timer } from '@aztec/foundation/timer';
21
+ import { AMMContract } from '@aztec/noir-contracts.js/AMM';
22
+ import { PrivateTokenContract } from '@aztec/noir-contracts.js/PrivateToken';
20
23
  import { TokenContract } from '@aztec/noir-contracts.js/Token';
21
- import type { FunctionCall } from '@aztec/stdlib/abi';
24
+ import type { ContractInstanceWithAddress } from '@aztec/stdlib/contract';
25
+ import { GasSettings } from '@aztec/stdlib/gas';
26
+ import type { AztecNode, AztecNodeAdmin } from '@aztec/stdlib/interfaces/client';
22
27
  import { deriveSigningKey } from '@aztec/stdlib/keys';
23
- import { makeTracedFetch } from '@aztec/telemetry-client';
28
+ import { TestWallet } from '@aztec/test-wallet/server';
24
29
 
25
- import { type BotConfig, SupportedTokenContracts, getVersions } from './config.js';
30
+ import { type BotConfig, SupportedTokenContracts } from './config.js';
31
+ import type { BotStore } from './store/index.js';
26
32
  import { getBalances, getPrivateBalance, isStandardTokenContract } from './utils.js';
27
33
 
28
34
  const MINT_BALANCE = 1e12;
29
35
  const MIN_BALANCE = 1e3;
30
36
 
31
37
  export class BotFactory {
32
- private pxe: PXE;
33
- private node?: AztecNode;
34
38
  private log = createLogger('bot');
35
39
 
36
- constructor(private readonly config: BotConfig, dependencies: { pxe?: PXE; node?: AztecNode } = {}) {
37
- if (config.flushSetupTransactions && !dependencies.node) {
38
- throw new Error(`Either a node client or node url must be provided if transaction flushing is requested`);
39
- }
40
- if (config.senderPrivateKey && !dependencies.node) {
41
- throw new Error(
42
- `Either a node client or node url must be provided for bridging L1 fee juice to deploy an account with private key`,
43
- );
44
- }
45
- if (!dependencies.pxe && !config.pxeUrl) {
46
- throw new Error(`Either a PXE client or a PXE URL must be provided`);
47
- }
48
-
49
- this.node = dependencies.node;
50
-
51
- if (dependencies.pxe) {
52
- this.log.info(`Using local PXE`);
53
- this.pxe = dependencies.pxe;
54
- return;
55
- }
56
- this.log.info(`Using remote PXE at ${config.pxeUrl!}`);
57
- this.pxe = createPXEClient(config.pxeUrl!, getVersions(), makeTracedFetch([1, 2, 3], false));
58
- }
40
+ constructor(
41
+ private readonly config: BotConfig,
42
+ private readonly wallet: TestWallet,
43
+ private readonly store: BotStore,
44
+ private readonly aztecNode: AztecNode,
45
+ private readonly aztecNodeAdmin?: AztecNodeAdmin,
46
+ ) {}
59
47
 
60
48
  /**
61
49
  * Initializes a new bot by setting up the sender account, registering the recipient,
62
50
  * deploying the token contract, and minting tokens if necessary.
63
51
  */
64
- public async setup() {
65
- const recipient = await this.registerRecipient();
66
- const wallet = await this.setupAccount();
67
- const token = await this.setupToken(wallet);
68
- await this.mintTokens(token);
69
- return { wallet, token, pxe: this.pxe, recipient };
52
+ public async setup(): Promise<{
53
+ wallet: TestWallet;
54
+ defaultAccountAddress: AztecAddress;
55
+ token: TokenContract | PrivateTokenContract;
56
+ node: AztecNode;
57
+ recipient: AztecAddress;
58
+ }> {
59
+ const recipient = (await this.wallet.createAccount()).address;
60
+ const defaultAccountAddress = await this.setupAccount();
61
+ const token = await this.setupToken(defaultAccountAddress);
62
+ await this.mintTokens(token, defaultAccountAddress);
63
+ return { wallet: this.wallet, defaultAccountAddress, token, node: this.aztecNode, recipient };
64
+ }
65
+
66
+ public async setupAmm(): Promise<{
67
+ wallet: TestWallet;
68
+ defaultAccountAddress: AztecAddress;
69
+ amm: AMMContract;
70
+ token0: TokenContract;
71
+ token1: TokenContract;
72
+ node: AztecNode;
73
+ }> {
74
+ const defaultAccountAddress = await this.setupAccount();
75
+ const token0 = await this.setupTokenContract(defaultAccountAddress, this.config.tokenSalt, 'BotToken0', 'BOT0');
76
+ const token1 = await this.setupTokenContract(defaultAccountAddress, this.config.tokenSalt, 'BotToken1', 'BOT1');
77
+ const liquidityToken = await this.setupTokenContract(
78
+ defaultAccountAddress,
79
+ this.config.tokenSalt,
80
+ 'BotLPToken',
81
+ 'BOTLP',
82
+ );
83
+ const amm = await this.setupAmmContract(
84
+ defaultAccountAddress,
85
+ this.config.tokenSalt,
86
+ token0,
87
+ token1,
88
+ liquidityToken,
89
+ );
90
+
91
+ await this.fundAmm(defaultAccountAddress, defaultAccountAddress, amm, token0, token1, liquidityToken);
92
+ this.log.info(`AMM initialized and funded`);
93
+
94
+ return { wallet: this.wallet, defaultAccountAddress, amm, token0, token1, node: this.aztecNode };
70
95
  }
71
96
 
72
97
  /**
@@ -74,61 +99,65 @@ export class BotFactory {
74
99
  * @returns The sender wallet.
75
100
  */
76
101
  private async setupAccount() {
77
- if (this.config.senderPrivateKey) {
78
- return await this.setupAccountWithPrivateKey(this.config.senderPrivateKey);
102
+ const privateKey = this.config.senderPrivateKey?.getValue();
103
+ if (privateKey) {
104
+ this.log.info(`Setting up account with provided private key`);
105
+ return await this.setupAccountWithPrivateKey(privateKey);
79
106
  } else {
107
+ this.log.info(`Setting up test account`);
80
108
  return await this.setupTestAccount();
81
109
  }
82
110
  }
83
111
 
84
- private async setupAccountWithPrivateKey(privateKey: Fr) {
85
- const salt = Fr.ONE;
86
- const signingKey = deriveSigningKey(privateKey);
87
- const account = await getSchnorrAccount(this.pxe, privateKey, signingKey, salt);
88
- const isInit = (await this.pxe.getContractMetadata(account.getAddress())).isContractInitialized;
112
+ private async setupAccountWithPrivateKey(secret: Fr) {
113
+ const salt = this.config.senderSalt ?? Fr.ONE;
114
+ const signingKey = deriveSigningKey(secret);
115
+ const accountData = {
116
+ secret,
117
+ salt,
118
+ contract: new SchnorrAccountContract(signingKey!),
119
+ };
120
+ const accountManager = await this.wallet.createAccount(accountData);
121
+ const isInit = (await this.wallet.getContractMetadata(accountManager.address)).isContractInitialized;
89
122
  if (isInit) {
90
- this.log.info(`Account at ${account.getAddress().toString()} already initialized`);
91
- const wallet = await account.register();
92
- return wallet;
123
+ this.log.info(`Account at ${accountManager.address.toString()} already initialized`);
124
+ const timer = new Timer();
125
+ const address = accountManager.address;
126
+ this.log.info(`Account at ${address} registered. duration=${timer.ms()}`);
127
+ await this.store.deleteBridgeClaim(address);
128
+ return address;
93
129
  } else {
94
- const address = account.getAddress();
130
+ const address = accountManager.address;
95
131
  this.log.info(`Deploying account at ${address}`);
96
132
 
97
- const claim = await this.bridgeL1FeeJuice(address, 10n ** 22n);
133
+ const claim = await this.getOrCreateBridgeClaim(address);
98
134
 
99
- const wallet = await account.getWallet();
100
- const paymentMethod = new FeeJuicePaymentMethodWithClaim(wallet, claim);
101
- const sentTx = account.deploy({ fee: { paymentMethod } });
135
+ const paymentMethod = new FeeJuicePaymentMethodWithClaim(accountManager.address, claim);
136
+ const deployMethod = await accountManager.getDeployMethod();
137
+ const maxFeesPerGas = (await this.aztecNode.getCurrentMinFees()).mul(1 + this.config.minFeePadding);
138
+ const gasSettings = GasSettings.default({ maxFeesPerGas });
139
+ const sentTx = deployMethod.send({ from: AztecAddress.ZERO, fee: { gasSettings, paymentMethod } });
102
140
  const txHash = await sentTx.getTxHash();
103
- this.log.info(`Sent tx with hash ${txHash.toString()}`);
104
- await this.tryFlushTxs();
105
- this.log.verbose('Waiting for account deployment to settle');
106
- await sentTx.wait({ timeout: this.config.txMinedWaitSeconds });
141
+ this.log.info(`Sent tx for account deployment with hash ${txHash.toString()}`);
142
+ await this.withNoMinTxsPerBlock(() => sentTx.wait({ timeout: this.config.txMinedWaitSeconds }));
107
143
  this.log.info(`Account deployed at ${address}`);
108
- return wallet;
109
- }
110
- }
111
144
 
112
- private async setupTestAccount() {
113
- let [wallet] = await getDeployedTestAccountsWallets(this.pxe);
114
- if (wallet) {
115
- this.log.info(`Using funded test account: ${wallet.getAddress()}`);
116
- } else {
117
- this.log.info('Registering funded test account');
118
- const [account] = await getInitialTestAccounts();
119
- const manager = await getSchnorrAccount(this.pxe, account.secret, account.signingKey, account.salt);
120
- wallet = await manager.register();
121
- this.log.info(`Funded test account registered: ${wallet.getAddress()}`);
145
+ // Clean up the consumed bridge claim
146
+ await this.store.deleteBridgeClaim(address);
147
+
148
+ return accountManager.address;
122
149
  }
123
- return wallet;
124
150
  }
125
151
 
126
- /**
127
- * Registers the recipient for txs in the pxe.
128
- */
129
- private async registerRecipient() {
130
- const recipient = await this.pxe.registerAccount(this.config.recipientEncryptionSecret, Fr.ONE);
131
- return recipient.address;
152
+ private async setupTestAccount() {
153
+ const [initialAccountData] = await getInitialTestAccountsData();
154
+ const accountData = {
155
+ secret: initialAccountData.secret,
156
+ salt: initialAccountData.salt,
157
+ contract: new SchnorrAccountContract(initialAccountData.signingKey),
158
+ };
159
+ const accountManager = await this.wallet.createAccount(accountData);
160
+ return accountManager.address;
132
161
  }
133
162
 
134
163
  /**
@@ -136,33 +165,175 @@ export class BotFactory {
136
165
  * @param wallet - Wallet to deploy the token contract from.
137
166
  * @returns The TokenContract instance.
138
167
  */
139
- private async setupToken(wallet: AccountWallet): Promise<TokenContract | EasyPrivateTokenContract> {
140
- let deploy: DeployMethod<TokenContract | EasyPrivateTokenContract>;
141
- const deployOpts: DeployOptions = { contractAddressSalt: this.config.tokenSalt, universalDeploy: true };
168
+ private async setupToken(sender: AztecAddress): Promise<TokenContract | PrivateTokenContract> {
169
+ let deploy: DeployMethod<TokenContract | PrivateTokenContract>;
170
+ let tokenInstance: ContractInstanceWithAddress | undefined;
171
+ const deployOpts: DeployOptions = {
172
+ from: sender,
173
+ contractAddressSalt: this.config.tokenSalt,
174
+ universalDeploy: true,
175
+ };
142
176
  if (this.config.contract === SupportedTokenContracts.TokenContract) {
143
- deploy = TokenContract.deploy(wallet, wallet.getAddress(), 'BotToken', 'BOT', 18);
144
- } else if (this.config.contract === SupportedTokenContracts.EasyPrivateTokenContract) {
145
- deploy = EasyPrivateTokenContract.deploy(wallet, MINT_BALANCE, wallet.getAddress());
146
- deployOpts.skipPublicDeployment = true;
147
- deployOpts.skipClassRegistration = true;
177
+ deploy = TokenContract.deploy(this.wallet, sender, 'BotToken', 'BOT', 18);
178
+ } else if (this.config.contract === SupportedTokenContracts.PrivateTokenContract) {
179
+ // Generate keys for the contract since PrivateToken uses SinglePrivateMutable which requires keys
180
+ const tokenSecretKey = Fr.random();
181
+ const tokenPublicKeys = (await deriveKeys(tokenSecretKey)).publicKeys;
182
+ deploy = PrivateTokenContract.deployWithPublicKeys(tokenPublicKeys, this.wallet, MINT_BALANCE, sender);
183
+ deployOpts.skipInstancePublication = true;
184
+ deployOpts.skipClassPublication = true;
148
185
  deployOpts.skipInitialization = false;
149
- deployOpts.skipPublicSimulation = true;
186
+
187
+ // Register the contract with the secret key before deployment
188
+ tokenInstance = await deploy.getInstance(deployOpts);
189
+ await this.wallet.registerContract(tokenInstance, PrivateTokenContract.artifact, tokenSecretKey);
150
190
  } else {
151
191
  throw new Error(`Unsupported token contract type: ${this.config.contract}`);
152
192
  }
153
193
 
154
- const address = (await deploy.getInstance(deployOpts)).address;
155
- if ((await this.pxe.getContractMetadata(address)).isContractPubliclyDeployed) {
194
+ const address = tokenInstance?.address ?? (await deploy.getInstance(deployOpts)).address;
195
+ if ((await this.wallet.getContractMetadata(address)).isContractPublished) {
156
196
  this.log.info(`Token at ${address.toString()} already deployed`);
157
197
  return deploy.register();
158
198
  } else {
159
199
  this.log.info(`Deploying token contract at ${address.toString()}`);
160
200
  const sentTx = deploy.send(deployOpts);
161
201
  const txHash = await sentTx.getTxHash();
162
- this.log.info(`Sent tx with hash ${txHash.toString()}`);
163
- await this.tryFlushTxs();
164
- this.log.verbose('Waiting for token setup to settle');
165
- return sentTx.deployed({ timeout: this.config.txMinedWaitSeconds });
202
+ this.log.info(`Sent tx for token setup with hash ${txHash.toString()}`);
203
+ return this.withNoMinTxsPerBlock(() => sentTx.deployed({ timeout: this.config.txMinedWaitSeconds }));
204
+ }
205
+ }
206
+
207
+ /**
208
+ * Checks if the token contract is deployed and deploys it if necessary.
209
+ * @param wallet - Wallet to deploy the token contract from.
210
+ * @returns The TokenContract instance.
211
+ */
212
+ private setupTokenContract(
213
+ deployer: AztecAddress,
214
+ contractAddressSalt: Fr,
215
+ name: string,
216
+ ticker: string,
217
+ decimals = 18,
218
+ ): Promise<TokenContract> {
219
+ const deployOpts: DeployOptions = { from: deployer, contractAddressSalt, universalDeploy: true };
220
+ const deploy = TokenContract.deploy(this.wallet, deployer, name, ticker, decimals);
221
+ return this.registerOrDeployContract('Token - ' + name, deploy, deployOpts);
222
+ }
223
+
224
+ private async setupAmmContract(
225
+ deployer: AztecAddress,
226
+ contractAddressSalt: Fr,
227
+ token0: TokenContract,
228
+ token1: TokenContract,
229
+ lpToken: TokenContract,
230
+ ): Promise<AMMContract> {
231
+ const deployOpts: DeployOptions = { from: deployer, contractAddressSalt, universalDeploy: true };
232
+ const deploy = AMMContract.deploy(this.wallet, token0.address, token1.address, lpToken.address);
233
+ const amm = await this.registerOrDeployContract('AMM', deploy, deployOpts);
234
+
235
+ this.log.info(`AMM deployed at ${amm.address}`);
236
+ const minterTx = lpToken.methods.set_minter(amm.address, true).send({ from: deployer });
237
+ this.log.info(`Set LP token minter to AMM txHash=${(await minterTx.getTxHash()).toString()}`);
238
+ await minterTx.wait({ timeout: this.config.txMinedWaitSeconds });
239
+ this.log.info(`Liquidity token initialized`);
240
+
241
+ return amm;
242
+ }
243
+
244
+ private async fundAmm(
245
+ defaultAccountAddress: AztecAddress,
246
+ liquidityProvider: AztecAddress,
247
+ amm: AMMContract,
248
+ token0: TokenContract,
249
+ token1: TokenContract,
250
+ lpToken: TokenContract,
251
+ ): Promise<void> {
252
+ const getPrivateBalances = () =>
253
+ Promise.all([
254
+ token0.methods.balance_of_private(liquidityProvider).simulate({ from: liquidityProvider }),
255
+ token1.methods.balance_of_private(liquidityProvider).simulate({ from: liquidityProvider }),
256
+ lpToken.methods.balance_of_private(liquidityProvider).simulate({ from: liquidityProvider }),
257
+ ]);
258
+
259
+ const authwitNonce = Fr.random();
260
+
261
+ // keep some tokens for swapping
262
+ const amount0Max = MINT_BALANCE / 2;
263
+ const amount0Min = MINT_BALANCE / 4;
264
+ const amount1Max = MINT_BALANCE / 2;
265
+ const amount1Min = MINT_BALANCE / 4;
266
+
267
+ const [t0Bal, t1Bal, lpBal] = await getPrivateBalances();
268
+
269
+ this.log.info(
270
+ `Minting ${MINT_BALANCE} tokens of each BotToken0 and BotToken1. Current private balances of ${liquidityProvider}: token0=${t0Bal}, token1=${t1Bal}, lp=${lpBal}`,
271
+ );
272
+
273
+ // Add authwitnesses for the transfers in AMM::add_liquidity function
274
+ const token0Authwit = await this.wallet.createAuthWit(defaultAccountAddress, {
275
+ caller: amm.address,
276
+ call: await token0.methods
277
+ .transfer_to_public_and_prepare_private_balance_increase(
278
+ liquidityProvider,
279
+ amm.address,
280
+ amount0Max,
281
+ authwitNonce,
282
+ )
283
+ .getFunctionCall(),
284
+ });
285
+ const token1Authwit = await this.wallet.createAuthWit(defaultAccountAddress, {
286
+ caller: amm.address,
287
+ call: await token1.methods
288
+ .transfer_to_public_and_prepare_private_balance_increase(
289
+ liquidityProvider,
290
+ amm.address,
291
+ amount1Max,
292
+ authwitNonce,
293
+ )
294
+ .getFunctionCall(),
295
+ });
296
+
297
+ const mintTx = new BatchCall(this.wallet, [
298
+ token0.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
299
+ token1.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
300
+ ]).send({ from: liquidityProvider });
301
+
302
+ this.log.info(`Sent mint tx: ${(await mintTx.getTxHash()).toString()}`);
303
+ await mintTx.wait({ timeout: this.config.txMinedWaitSeconds });
304
+
305
+ const addLiquidityTx = amm.methods
306
+ .add_liquidity(amount0Max, amount1Max, amount0Min, amount1Min, authwitNonce)
307
+ .send({
308
+ from: liquidityProvider,
309
+ authWitnesses: [token0Authwit, token1Authwit],
310
+ });
311
+
312
+ this.log.info(`Sent tx to add liquidity to the AMM: ${(await addLiquidityTx.getTxHash()).toString()}`);
313
+ await addLiquidityTx.wait({ timeout: this.config.txMinedWaitSeconds });
314
+ this.log.info(`Liquidity added`);
315
+
316
+ const [newT0Bal, newT1Bal, newLPBal] = await getPrivateBalances();
317
+ this.log.info(
318
+ `Updated private balances of ${defaultAccountAddress} after minting and funding AMM: token0=${newT0Bal}, token1=${newT1Bal}, lp=${newLPBal}`,
319
+ );
320
+ }
321
+
322
+ private async registerOrDeployContract<T extends ContractBase>(
323
+ name: string,
324
+ deploy: DeployMethod<T>,
325
+ deployOpts: DeployOptions,
326
+ ): Promise<T> {
327
+ const address = (await deploy.getInstance(deployOpts)).address;
328
+ if ((await this.wallet.getContractMetadata(address)).isContractPublished) {
329
+ this.log.info(`Contract ${name} at ${address.toString()} already deployed`);
330
+ return deploy.register();
331
+ } else {
332
+ this.log.info(`Deploying contract ${name} at ${address.toString()}`);
333
+ const sentTx = deploy.send(deployOpts);
334
+ const txHash = await sentTx.getTxHash();
335
+ this.log.info(`Sent contract ${name} setup tx with hash ${txHash.toString()}`);
336
+ return this.withNoMinTxsPerBlock(() => sentTx.deployed({ timeout: this.config.txMinedWaitSeconds }));
166
337
  }
167
338
  }
168
339
 
@@ -170,90 +341,119 @@ export class BotFactory {
170
341
  * Mints private and public tokens for the sender if their balance is below the minimum.
171
342
  * @param token - Token contract.
172
343
  */
173
- private async mintTokens(token: TokenContract | EasyPrivateTokenContract) {
174
- const sender = token.wallet.getAddress();
344
+ private async mintTokens(token: TokenContract | PrivateTokenContract, minter: AztecAddress) {
175
345
  const isStandardToken = isStandardTokenContract(token);
176
346
  let privateBalance = 0n;
177
347
  let publicBalance = 0n;
178
348
 
179
349
  if (isStandardToken) {
180
- ({ privateBalance, publicBalance } = await getBalances(token, sender));
350
+ ({ privateBalance, publicBalance } = await getBalances(token, minter));
181
351
  } else {
182
- privateBalance = await getPrivateBalance(token, sender);
352
+ privateBalance = await getPrivateBalance(token, minter);
183
353
  }
184
354
 
185
- const calls: FunctionCall[] = [];
355
+ const calls: ContractFunctionInteraction[] = [];
186
356
  if (privateBalance < MIN_BALANCE) {
187
- this.log.info(`Minting private tokens for ${sender.toString()}`);
357
+ this.log.info(`Minting private tokens for ${minter.toString()}`);
188
358
 
189
- const from = sender; // we are setting from to sender here because we need a sender to calculate the tag
190
359
  calls.push(
191
360
  isStandardToken
192
- ? await token.methods.mint_to_private(from, sender, MINT_BALANCE).request()
193
- : await token.methods.mint(MINT_BALANCE, sender).request(),
361
+ ? token.methods.mint_to_private(minter, MINT_BALANCE)
362
+ : token.methods.mint(MINT_BALANCE, minter),
194
363
  );
195
364
  }
196
365
  if (isStandardToken && publicBalance < MIN_BALANCE) {
197
- this.log.info(`Minting public tokens for ${sender.toString()}`);
198
- calls.push(await token.methods.mint_to_public(sender, MINT_BALANCE).request());
366
+ this.log.info(`Minting public tokens for ${minter.toString()}`);
367
+ calls.push(token.methods.mint_to_public(minter, MINT_BALANCE));
199
368
  }
200
369
  if (calls.length === 0) {
201
- this.log.info(`Skipping minting as ${sender.toString()} has enough tokens`);
370
+ this.log.info(`Skipping minting as ${minter.toString()} has enough tokens`);
202
371
  return;
203
372
  }
204
- const sentTx = new BatchCall(token.wallet, calls).send();
373
+ const sentTx = new BatchCall(token.wallet, calls).send({ from: minter });
205
374
  const txHash = await sentTx.getTxHash();
206
- this.log.info(`Sent tx with hash ${txHash.toString()}`);
207
- await this.tryFlushTxs();
208
- this.log.verbose('Waiting for token mint to settle');
209
- await sentTx.wait({ timeout: this.config.txMinedWaitSeconds });
375
+ this.log.info(`Sent token mint tx with hash ${txHash.toString()}`);
376
+ await this.withNoMinTxsPerBlock(() => sentTx.wait({ timeout: this.config.txMinedWaitSeconds }));
210
377
  }
211
378
 
212
- private async bridgeL1FeeJuice(recipient: AztecAddress, amount: bigint) {
379
+ /**
380
+ * Gets or creates a bridge claim for the recipient.
381
+ * Checks if a claim already exists in the store and reuses it if valid.
382
+ * Only creates a new bridge if fee juice balance is below threshold.
383
+ */
384
+ private async getOrCreateBridgeClaim(recipient: AztecAddress): Promise<L2AmountClaim> {
385
+ // Check if we have an existing claim in the store
386
+ const existingClaim = await this.store.getBridgeClaim(recipient);
387
+ if (existingClaim) {
388
+ this.log.info(`Found existing bridge claim for ${recipient.toString()}, checking validity...`);
389
+
390
+ // Check if the message is ready on L2
391
+ try {
392
+ const messageHash = Fr.fromHexString(existingClaim.claim.messageHash);
393
+ await this.withNoMinTxsPerBlock(() =>
394
+ waitForL1ToL2MessageReady(this.aztecNode, messageHash, {
395
+ timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds,
396
+ forPublicConsumption: false,
397
+ }),
398
+ );
399
+ return existingClaim.claim;
400
+ } catch (err) {
401
+ this.log.warn(`Failed to verify existing claim, creating new one: ${err}`);
402
+ await this.store.deleteBridgeClaim(recipient);
403
+ }
404
+ }
405
+
406
+ const claim = await this.bridgeL1FeeJuice(recipient);
407
+ await this.store.saveBridgeClaim(recipient, claim);
408
+
409
+ return claim;
410
+ }
411
+
412
+ private async bridgeL1FeeJuice(recipient: AztecAddress): Promise<L2AmountClaim> {
213
413
  const l1RpcUrls = this.config.l1RpcUrls;
214
414
  if (!l1RpcUrls?.length) {
215
415
  throw new Error('L1 Rpc url is required to bridge the fee juice to fund the deployment of the account.');
216
416
  }
217
- const mnemonicOrPrivateKey = this.config.l1PrivateKey || this.config.l1Mnemonic;
417
+ const mnemonicOrPrivateKey = this.config.l1PrivateKey?.getValue() ?? this.config.l1Mnemonic?.getValue();
218
418
  if (!mnemonicOrPrivateKey) {
219
419
  throw new Error(
220
420
  'Either a mnemonic or private key of an L1 account is required to bridge the fee juice to fund the deployment of the account.',
221
421
  );
222
422
  }
223
423
 
224
- const { l1ChainId } = await this.pxe.getNodeInfo();
424
+ const { l1ChainId } = await this.aztecNode.getNodeInfo();
225
425
  const chain = createEthereumChain(l1RpcUrls, l1ChainId);
226
- const { publicClient, walletClient } = createL1Clients(chain.rpcUrls, mnemonicOrPrivateKey, chain.chainInfo);
227
-
228
- const portal = await L1FeeJuicePortalManager.new(this.pxe, publicClient, walletClient, this.log);
229
- const claim = await portal.bridgeTokensPublic(recipient, amount, true /* mint */);
230
-
231
- const isSynced = async () => await this.pxe.isL1ToL2MessageSynced(Fr.fromHexString(claim.messageHash));
232
- await retryUntil(isSynced, `message ${claim.messageHash} sync`, 24, 1);
426
+ const extendedClient = createExtendedL1Client(chain.rpcUrls, mnemonicOrPrivateKey, chain.chainInfo);
233
427
 
234
- this.log.info(`Created a claim for ${amount} L1 fee juice to ${recipient}.`, claim);
428
+ const portal = await L1FeeJuicePortalManager.new(this.aztecNode, extendedClient, this.log);
429
+ const mintAmount = await portal.getTokenManager().getMintAmount();
430
+ const claim = await portal.bridgeTokensPublic(recipient, mintAmount, true /* mint */);
235
431
 
236
- // Progress by 2 L2 blocks so that the l1ToL2Message added above will be available to use on L2.
237
- await this.advanceL2Block();
238
- await this.advanceL2Block();
432
+ await this.withNoMinTxsPerBlock(() =>
433
+ waitForL1ToL2MessageReady(this.aztecNode, Fr.fromHexString(claim.messageHash), {
434
+ timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds,
435
+ forPublicConsumption: false,
436
+ }),
437
+ );
239
438
 
240
- return claim;
241
- }
439
+ this.log.info(`Created a claim for ${mintAmount} L1 fee juice to ${recipient}.`, claim);
242
440
 
243
- private async advanceL2Block() {
244
- const initialBlockNumber = await this.node!.getBlockNumber();
245
- await this.tryFlushTxs();
246
- await retryUntil(async () => (await this.node!.getBlockNumber()) >= initialBlockNumber + 1);
441
+ return claim as L2AmountClaim;
247
442
  }
248
443
 
249
- private async tryFlushTxs() {
250
- if (this.config.flushSetupTransactions) {
251
- this.log.verbose('Flushing transactions');
252
- try {
253
- await this.node!.flushTxs();
254
- } catch (err) {
255
- this.log.error(`Failed to flush transactions: ${err}`);
256
- }
444
+ private async withNoMinTxsPerBlock<T>(fn: () => Promise<T>): Promise<T> {
445
+ if (!this.aztecNodeAdmin || !this.config.flushSetupTransactions) {
446
+ this.log.verbose(`No node admin client or flushing not requested (not setting minTxsPerBlock to 0)`);
447
+ return fn();
448
+ }
449
+ const { minTxsPerBlock } = await this.aztecNodeAdmin.getConfig();
450
+ this.log.warn(`Setting sequencer minTxsPerBlock to 0 from ${minTxsPerBlock} to flush setup transactions`);
451
+ await this.aztecNodeAdmin.setConfig({ minTxsPerBlock: 0 });
452
+ try {
453
+ return await fn();
454
+ } finally {
455
+ this.log.warn(`Restoring sequencer minTxsPerBlock to ${minTxsPerBlock}`);
456
+ await this.aztecNodeAdmin.setConfig({ minTxsPerBlock });
257
457
  }
258
458
  }
259
459
  }
package/src/index.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  export { Bot } from './bot.js';
2
+ export { AmmBot } from './amm_bot.js';
2
3
  export { BotRunner } from './runner.js';
4
+ export { BotStore } from './store/bot_store.js';
3
5
  export {
4
6
  type BotConfig,
5
7
  getBotConfigFromEnv,
@@ -7,5 +9,5 @@ export {
7
9
  botConfigMappings,
8
10
  SupportedTokenContracts,
9
11
  } from './config.js';
10
- export { createBotRunnerRpcServer, getBotRunnerApiHandler } from './rpc.js';
12
+ export { getBotRunnerApiHandler } from './rpc.js';
11
13
  export * from './interface.js';
package/src/interface.ts CHANGED
@@ -1,15 +1,23 @@
1
+ import { AztecAddress } from '@aztec/aztec.js/addresses';
1
2
  import type { ApiSchemaFor } from '@aztec/stdlib/schemas';
2
3
 
3
4
  import { z } from 'zod';
4
5
 
5
6
  import { type BotConfig, BotConfigSchema } from './config.js';
6
7
 
8
+ export const BotInfoSchema = z.object({
9
+ botAddress: AztecAddress.schema,
10
+ });
11
+
12
+ export type BotInfo = z.infer<typeof BotInfoSchema>;
13
+
7
14
  export interface BotRunnerApi {
8
15
  start(): Promise<void>;
9
16
  stop(): Promise<void>;
10
17
  run(): Promise<void>;
11
18
  setup(): Promise<void>;
12
19
  getConfig(): Promise<BotConfig>;
20
+ getInfo(): Promise<BotInfo>;
13
21
  update(config: BotConfig): Promise<void>;
14
22
  }
15
23
 
@@ -18,6 +26,7 @@ export const BotRunnerApiSchema: ApiSchemaFor<BotRunnerApi> = {
18
26
  stop: z.function().args().returns(z.void()),
19
27
  run: z.function().args().returns(z.void()),
20
28
  setup: z.function().args().returns(z.void()),
29
+ getInfo: z.function().args().returns(BotInfoSchema),
21
30
  getConfig: z.function().args().returns(BotConfigSchema),
22
31
  update: z.function().args(BotConfigSchema).returns(z.void()),
23
32
  };