@aztec/bot 0.0.0-test.1 → 0.0.1-commit.001888fc

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