@aztec/bot 0.0.0-test.1 → 0.0.1-commit.03f7ef2

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 +33 -25
  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/dest/factory.js CHANGED
@@ -1,229 +1,383 @@
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 { SchnorrAccountContract } from '@aztec/accounts/schnorr';
2
+ import { getInitialTestAccountsData } from '@aztec/accounts/testing';
3
+ import { AztecAddress } from '@aztec/aztec.js/addresses';
4
+ import { BatchCall } from '@aztec/aztec.js/contracts';
5
+ import { L1FeeJuicePortalManager } from '@aztec/aztec.js/ethereum';
6
+ import { FeeJuicePaymentMethodWithClaim } from '@aztec/aztec.js/fee';
7
+ import { deriveKeys } from '@aztec/aztec.js/keys';
8
+ import { createLogger } from '@aztec/aztec.js/log';
9
+ import { waitForL1ToL2MessageReady } from '@aztec/aztec.js/messaging';
10
+ import { createEthereumChain } from '@aztec/ethereum/chain';
11
+ import { createExtendedL1Client } from '@aztec/ethereum/client';
12
+ import { Fr } from '@aztec/foundation/curves/bn254';
13
+ import { Timer } from '@aztec/foundation/timer';
14
+ import { AMMContract } from '@aztec/noir-contracts.js/AMM';
15
+ import { PrivateTokenContract } from '@aztec/noir-contracts.js/PrivateToken';
7
16
  import { TokenContract } from '@aztec/noir-contracts.js/Token';
17
+ import { GasSettings } from '@aztec/stdlib/gas';
8
18
  import { deriveSigningKey } from '@aztec/stdlib/keys';
9
- import { makeTracedFetch } from '@aztec/telemetry-client';
10
- import { SupportedTokenContracts, getVersions } from './config.js';
19
+ import { SupportedTokenContracts } from './config.js';
11
20
  import { getBalances, getPrivateBalance, isStandardTokenContract } from './utils.js';
12
21
  const MINT_BALANCE = 1e12;
13
22
  const MIN_BALANCE = 1e3;
14
23
  export class BotFactory {
15
24
  config;
16
- pxe;
17
- node;
25
+ wallet;
26
+ store;
27
+ aztecNode;
28
+ aztecNodeAdmin;
18
29
  log;
19
- constructor(config, dependencies = {}){
30
+ constructor(config, wallet, store, aztecNode, aztecNodeAdmin){
20
31
  this.config = config;
32
+ this.wallet = wallet;
33
+ this.store = store;
34
+ this.aztecNode = aztecNode;
35
+ this.aztecNodeAdmin = aztecNodeAdmin;
21
36
  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));
43
37
  }
44
38
  /**
45
39
  * Initializes a new bot by setting up the sender account, registering the recipient,
46
40
  * deploying the token contract, and minting tokens if necessary.
47
41
  */ 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);
42
+ const recipient = (await this.wallet.createAccount()).address;
43
+ const defaultAccountAddress = await this.setupAccount();
44
+ const token = await this.setupToken(defaultAccountAddress);
45
+ await this.mintTokens(token, defaultAccountAddress);
52
46
  return {
53
- wallet,
47
+ wallet: this.wallet,
48
+ defaultAccountAddress,
54
49
  token,
55
- pxe: this.pxe,
50
+ node: this.aztecNode,
56
51
  recipient
57
52
  };
58
53
  }
54
+ async setupAmm() {
55
+ const defaultAccountAddress = await this.setupAccount();
56
+ const token0 = await this.setupTokenContract(defaultAccountAddress, this.config.tokenSalt, 'BotToken0', 'BOT0');
57
+ const token1 = await this.setupTokenContract(defaultAccountAddress, this.config.tokenSalt, 'BotToken1', 'BOT1');
58
+ const liquidityToken = await this.setupTokenContract(defaultAccountAddress, this.config.tokenSalt, 'BotLPToken', 'BOTLP');
59
+ const amm = await this.setupAmmContract(defaultAccountAddress, this.config.tokenSalt, token0, token1, liquidityToken);
60
+ await this.fundAmm(defaultAccountAddress, defaultAccountAddress, amm, token0, token1, liquidityToken);
61
+ this.log.info(`AMM initialized and funded`);
62
+ return {
63
+ wallet: this.wallet,
64
+ defaultAccountAddress,
65
+ amm,
66
+ token0,
67
+ token1,
68
+ node: this.aztecNode
69
+ };
70
+ }
59
71
  /**
60
72
  * Checks if the sender account contract is initialized, and initializes it if necessary.
61
73
  * @returns The sender wallet.
62
74
  */ async setupAccount() {
63
- if (this.config.senderPrivateKey) {
64
- return await this.setupAccountWithPrivateKey(this.config.senderPrivateKey);
75
+ const privateKey = this.config.senderPrivateKey?.getValue();
76
+ if (privateKey) {
77
+ this.log.info(`Setting up account with provided private key`);
78
+ return await this.setupAccountWithPrivateKey(privateKey);
65
79
  } else {
80
+ this.log.info(`Setting up test account`);
66
81
  return await this.setupTestAccount();
67
82
  }
68
83
  }
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;
84
+ async setupAccountWithPrivateKey(secret) {
85
+ const salt = this.config.senderSalt ?? Fr.ONE;
86
+ const signingKey = deriveSigningKey(secret);
87
+ const accountData = {
88
+ secret,
89
+ salt,
90
+ contract: new SchnorrAccountContract(signingKey)
91
+ };
92
+ const accountManager = await this.wallet.createAccount(accountData);
93
+ const isInit = (await this.wallet.getContractMetadata(accountManager.address)).isContractInitialized;
74
94
  if (isInit) {
75
- this.log.info(`Account at ${account.getAddress().toString()} already initialized`);
76
- const wallet = await account.register();
77
- return wallet;
95
+ this.log.info(`Account at ${accountManager.address.toString()} already initialized`);
96
+ const timer = new Timer();
97
+ const address = accountManager.address;
98
+ this.log.info(`Account at ${address} registered. duration=${timer.ms()}`);
99
+ await this.store.deleteBridgeClaim(address);
100
+ return address;
78
101
  } else {
79
- const address = account.getAddress();
102
+ const address = accountManager.address;
80
103
  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({
104
+ const claim = await this.getOrCreateBridgeClaim(address);
105
+ const paymentMethod = new FeeJuicePaymentMethodWithClaim(accountManager.address, claim);
106
+ const deployMethod = await accountManager.getDeployMethod();
107
+ const maxFeesPerGas = (await this.aztecNode.getCurrentBaseFees()).mul(1 + this.config.baseFeePadding);
108
+ const gasSettings = GasSettings.default({
109
+ maxFeesPerGas
110
+ });
111
+ const sentTx = deployMethod.send({
112
+ from: AztecAddress.ZERO,
85
113
  fee: {
114
+ gasSettings,
86
115
  paymentMethod
87
116
  }
88
117
  });
89
118
  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
95
- });
119
+ this.log.info(`Sent tx for account deployment with hash ${txHash.toString()}`);
120
+ await this.withNoMinTxsPerBlock(()=>sentTx.wait({
121
+ timeout: this.config.txMinedWaitSeconds
122
+ }));
96
123
  this.log.info(`Account deployed at ${address}`);
97
- return wallet;
124
+ // Clean up the consumed bridge claim
125
+ await this.store.deleteBridgeClaim(address);
126
+ return accountManager.address;
98
127
  }
99
128
  }
100
129
  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;
130
+ const [initialAccountData] = await getInitialTestAccountsData();
131
+ const accountData = {
132
+ secret: initialAccountData.secret,
133
+ salt: initialAccountData.salt,
134
+ contract: new SchnorrAccountContract(initialAccountData.signingKey)
135
+ };
136
+ const accountManager = await this.wallet.createAccount(accountData);
137
+ return accountManager.address;
118
138
  }
119
139
  /**
120
140
  * Checks if the token contract is deployed and deploys it if necessary.
121
141
  * @param wallet - Wallet to deploy the token contract from.
122
142
  * @returns The TokenContract instance.
123
- */ async setupToken(wallet) {
143
+ */ async setupToken(sender) {
124
144
  let deploy;
145
+ let tokenInstance;
125
146
  const deployOpts = {
147
+ from: sender,
126
148
  contractAddressSalt: this.config.tokenSalt,
127
149
  universalDeploy: true
128
150
  };
129
151
  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;
152
+ deploy = TokenContract.deploy(this.wallet, sender, 'BotToken', 'BOT', 18);
153
+ } else if (this.config.contract === SupportedTokenContracts.PrivateTokenContract) {
154
+ // Generate keys for the contract since PrivateToken uses SinglePrivateMutable which requires keys
155
+ const tokenSecretKey = Fr.random();
156
+ const tokenPublicKeys = (await deriveKeys(tokenSecretKey)).publicKeys;
157
+ deploy = PrivateTokenContract.deployWithPublicKeys(tokenPublicKeys, this.wallet, MINT_BALANCE, sender);
158
+ deployOpts.skipInstancePublication = true;
159
+ deployOpts.skipClassPublication = true;
135
160
  deployOpts.skipInitialization = false;
136
- deployOpts.skipPublicSimulation = true;
161
+ // Register the contract with the secret key before deployment
162
+ tokenInstance = await deploy.getInstance(deployOpts);
163
+ await this.wallet.registerContract(tokenInstance, PrivateTokenContract.artifact, tokenSecretKey);
137
164
  } else {
138
165
  throw new Error(`Unsupported token contract type: ${this.config.contract}`);
139
166
  }
140
- const address = (await deploy.getInstance(deployOpts)).address;
141
- if ((await this.pxe.getContractMetadata(address)).isContractPubliclyDeployed) {
167
+ const address = tokenInstance?.address ?? (await deploy.getInstance(deployOpts)).address;
168
+ if ((await this.wallet.getContractMetadata(address)).isContractPublished) {
142
169
  this.log.info(`Token at ${address.toString()} already deployed`);
143
170
  return deploy.register();
144
171
  } else {
145
172
  this.log.info(`Deploying token contract at ${address.toString()}`);
146
173
  const sentTx = deploy.send(deployOpts);
147
174
  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({
152
- timeout: this.config.txMinedWaitSeconds
153
- });
175
+ this.log.info(`Sent tx for token setup with hash ${txHash.toString()}`);
176
+ return this.withNoMinTxsPerBlock(()=>sentTx.deployed({
177
+ timeout: this.config.txMinedWaitSeconds
178
+ }));
179
+ }
180
+ }
181
+ /**
182
+ * Checks if the token contract is deployed and deploys it if necessary.
183
+ * @param wallet - Wallet to deploy the token contract from.
184
+ * @returns The TokenContract instance.
185
+ */ setupTokenContract(deployer, contractAddressSalt, name, ticker, decimals = 18) {
186
+ const deployOpts = {
187
+ from: deployer,
188
+ contractAddressSalt,
189
+ universalDeploy: true
190
+ };
191
+ const deploy = TokenContract.deploy(this.wallet, deployer, name, ticker, decimals);
192
+ return this.registerOrDeployContract('Token - ' + name, deploy, deployOpts);
193
+ }
194
+ async setupAmmContract(deployer, contractAddressSalt, token0, token1, lpToken) {
195
+ const deployOpts = {
196
+ from: deployer,
197
+ contractAddressSalt,
198
+ universalDeploy: true
199
+ };
200
+ const deploy = AMMContract.deploy(this.wallet, token0.address, token1.address, lpToken.address);
201
+ const amm = await this.registerOrDeployContract('AMM', deploy, deployOpts);
202
+ this.log.info(`AMM deployed at ${amm.address}`);
203
+ const minterTx = lpToken.methods.set_minter(amm.address, true).send({
204
+ from: deployer
205
+ });
206
+ this.log.info(`Set LP token minter to AMM txHash=${(await minterTx.getTxHash()).toString()}`);
207
+ await minterTx.wait({
208
+ timeout: this.config.txMinedWaitSeconds
209
+ });
210
+ this.log.info(`Liquidity token initialized`);
211
+ return amm;
212
+ }
213
+ async fundAmm(defaultAccountAddress, liquidityProvider, amm, token0, token1, lpToken) {
214
+ const getPrivateBalances = ()=>Promise.all([
215
+ token0.methods.balance_of_private(liquidityProvider).simulate({
216
+ from: liquidityProvider
217
+ }),
218
+ token1.methods.balance_of_private(liquidityProvider).simulate({
219
+ from: liquidityProvider
220
+ }),
221
+ lpToken.methods.balance_of_private(liquidityProvider).simulate({
222
+ from: liquidityProvider
223
+ })
224
+ ]);
225
+ const authwitNonce = Fr.random();
226
+ // keep some tokens for swapping
227
+ const amount0Max = MINT_BALANCE / 2;
228
+ const amount0Min = MINT_BALANCE / 4;
229
+ const amount1Max = MINT_BALANCE / 2;
230
+ const amount1Min = MINT_BALANCE / 4;
231
+ const [t0Bal, t1Bal, lpBal] = await getPrivateBalances();
232
+ this.log.info(`Minting ${MINT_BALANCE} tokens of each BotToken0 and BotToken1. Current private balances of ${liquidityProvider}: token0=${t0Bal}, token1=${t1Bal}, lp=${lpBal}`);
233
+ // Add authwitnesses for the transfers in AMM::add_liquidity function
234
+ const token0Authwit = await this.wallet.createAuthWit(defaultAccountAddress, {
235
+ caller: amm.address,
236
+ call: await token0.methods.transfer_to_public_and_prepare_private_balance_increase(liquidityProvider, amm.address, amount0Max, authwitNonce).getFunctionCall()
237
+ });
238
+ const token1Authwit = await this.wallet.createAuthWit(defaultAccountAddress, {
239
+ caller: amm.address,
240
+ call: await token1.methods.transfer_to_public_and_prepare_private_balance_increase(liquidityProvider, amm.address, amount1Max, authwitNonce).getFunctionCall()
241
+ });
242
+ const mintTx = new BatchCall(this.wallet, [
243
+ token0.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
244
+ token1.methods.mint_to_private(liquidityProvider, MINT_BALANCE)
245
+ ]).send({
246
+ from: liquidityProvider
247
+ });
248
+ this.log.info(`Sent mint tx: ${(await mintTx.getTxHash()).toString()}`);
249
+ await mintTx.wait({
250
+ timeout: this.config.txMinedWaitSeconds
251
+ });
252
+ const addLiquidityTx = amm.methods.add_liquidity(amount0Max, amount1Max, amount0Min, amount1Min, authwitNonce).send({
253
+ from: liquidityProvider,
254
+ authWitnesses: [
255
+ token0Authwit,
256
+ token1Authwit
257
+ ]
258
+ });
259
+ this.log.info(`Sent tx to add liquidity to the AMM: ${(await addLiquidityTx.getTxHash()).toString()}`);
260
+ await addLiquidityTx.wait({
261
+ timeout: this.config.txMinedWaitSeconds
262
+ });
263
+ this.log.info(`Liquidity added`);
264
+ const [newT0Bal, newT1Bal, newLPBal] = await getPrivateBalances();
265
+ this.log.info(`Updated private balances of ${defaultAccountAddress} after minting and funding AMM: token0=${newT0Bal}, token1=${newT1Bal}, lp=${newLPBal}`);
266
+ }
267
+ async registerOrDeployContract(name, deploy, deployOpts) {
268
+ const address = (await deploy.getInstance(deployOpts)).address;
269
+ if ((await this.wallet.getContractMetadata(address)).isContractPublished) {
270
+ this.log.info(`Contract ${name} at ${address.toString()} already deployed`);
271
+ return deploy.register();
272
+ } else {
273
+ this.log.info(`Deploying contract ${name} at ${address.toString()}`);
274
+ const sentTx = deploy.send(deployOpts);
275
+ const txHash = await sentTx.getTxHash();
276
+ this.log.info(`Sent contract ${name} setup tx with hash ${txHash.toString()}`);
277
+ return this.withNoMinTxsPerBlock(()=>sentTx.deployed({
278
+ timeout: this.config.txMinedWaitSeconds
279
+ }));
154
280
  }
155
281
  }
156
282
  /**
157
283
  * Mints private and public tokens for the sender if their balance is below the minimum.
158
284
  * @param token - Token contract.
159
- */ async mintTokens(token) {
160
- const sender = token.wallet.getAddress();
285
+ */ async mintTokens(token, minter) {
161
286
  const isStandardToken = isStandardTokenContract(token);
162
287
  let privateBalance = 0n;
163
288
  let publicBalance = 0n;
164
289
  if (isStandardToken) {
165
- ({ privateBalance, publicBalance } = await getBalances(token, sender));
290
+ ({ privateBalance, publicBalance } = await getBalances(token, minter));
166
291
  } else {
167
- privateBalance = await getPrivateBalance(token, sender);
292
+ privateBalance = await getPrivateBalance(token, minter);
168
293
  }
169
294
  const calls = [];
170
295
  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());
296
+ this.log.info(`Minting private tokens for ${minter.toString()}`);
297
+ calls.push(isStandardToken ? token.methods.mint_to_private(minter, MINT_BALANCE) : token.methods.mint(MINT_BALANCE, minter));
174
298
  }
175
299
  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());
300
+ this.log.info(`Minting public tokens for ${minter.toString()}`);
301
+ calls.push(token.methods.mint_to_public(minter, MINT_BALANCE));
178
302
  }
179
303
  if (calls.length === 0) {
180
- this.log.info(`Skipping minting as ${sender.toString()} has enough tokens`);
304
+ this.log.info(`Skipping minting as ${minter.toString()} has enough tokens`);
181
305
  return;
182
306
  }
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
307
+ const sentTx = new BatchCall(token.wallet, calls).send({
308
+ from: minter
190
309
  });
310
+ const txHash = await sentTx.getTxHash();
311
+ this.log.info(`Sent token mint tx with hash ${txHash.toString()}`);
312
+ await this.withNoMinTxsPerBlock(()=>sentTx.wait({
313
+ timeout: this.config.txMinedWaitSeconds
314
+ }));
191
315
  }
192
- async bridgeL1FeeJuice(recipient, amount) {
316
+ /**
317
+ * Gets or creates a bridge claim for the recipient.
318
+ * Checks if a claim already exists in the store and reuses it if valid.
319
+ * Only creates a new bridge if fee juice balance is below threshold.
320
+ */ async getOrCreateBridgeClaim(recipient) {
321
+ // Check if we have an existing claim in the store
322
+ const existingClaim = await this.store.getBridgeClaim(recipient);
323
+ if (existingClaim) {
324
+ this.log.info(`Found existing bridge claim for ${recipient.toString()}, checking validity...`);
325
+ // Check if the message is ready on L2
326
+ try {
327
+ const messageHash = Fr.fromHexString(existingClaim.claim.messageHash);
328
+ await this.withNoMinTxsPerBlock(()=>waitForL1ToL2MessageReady(this.aztecNode, messageHash, {
329
+ timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds,
330
+ forPublicConsumption: false
331
+ }));
332
+ return existingClaim.claim;
333
+ } catch (err) {
334
+ this.log.warn(`Failed to verify existing claim, creating new one: ${err}`);
335
+ await this.store.deleteBridgeClaim(recipient);
336
+ }
337
+ }
338
+ const claim = await this.bridgeL1FeeJuice(recipient);
339
+ await this.store.saveBridgeClaim(recipient, claim);
340
+ return claim;
341
+ }
342
+ async bridgeL1FeeJuice(recipient) {
193
343
  const l1RpcUrls = this.config.l1RpcUrls;
194
344
  if (!l1RpcUrls?.length) {
195
345
  throw new Error('L1 Rpc url is required to bridge the fee juice to fund the deployment of the account.');
196
346
  }
197
- const mnemonicOrPrivateKey = this.config.l1PrivateKey || this.config.l1Mnemonic;
347
+ const mnemonicOrPrivateKey = this.config.l1PrivateKey?.getValue() ?? this.config.l1Mnemonic?.getValue();
198
348
  if (!mnemonicOrPrivateKey) {
199
349
  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
350
  }
201
- const { l1ChainId } = await this.pxe.getNodeInfo();
351
+ const { l1ChainId } = await this.aztecNode.getNodeInfo();
202
352
  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();
353
+ const extendedClient = createExtendedL1Client(chain.rpcUrls, mnemonicOrPrivateKey, chain.chainInfo);
354
+ const portal = await L1FeeJuicePortalManager.new(this.aztecNode, extendedClient, this.log);
355
+ const mintAmount = await portal.getTokenManager().getMintAmount();
356
+ const claim = await portal.bridgeTokensPublic(recipient, mintAmount, true);
357
+ await this.withNoMinTxsPerBlock(()=>waitForL1ToL2MessageReady(this.aztecNode, Fr.fromHexString(claim.messageHash), {
358
+ timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds,
359
+ forPublicConsumption: false
360
+ }));
361
+ this.log.info(`Created a claim for ${mintAmount} L1 fee juice to ${recipient}.`, claim);
212
362
  return claim;
213
363
  }
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
- }
364
+ async withNoMinTxsPerBlock(fn) {
365
+ if (!this.aztecNodeAdmin || !this.config.flushSetupTransactions) {
366
+ this.log.verbose(`No node admin client or flushing not requested (not setting minTxsPerBlock to 0)`);
367
+ return fn();
368
+ }
369
+ const { minTxsPerBlock } = await this.aztecNodeAdmin.getConfig();
370
+ this.log.warn(`Setting sequencer minTxsPerBlock to 0 from ${minTxsPerBlock} to flush setup transactions`);
371
+ await this.aztecNodeAdmin.setConfig({
372
+ minTxsPerBlock: 0
373
+ });
374
+ try {
375
+ return await fn();
376
+ } finally{
377
+ this.log.warn(`Restoring sequencer minTxsPerBlock to ${minTxsPerBlock}`);
378
+ await this.aztecNodeAdmin.setConfig({
379
+ minTxsPerBlock
380
+ });
227
381
  }
228
382
  }
229
383
  }
package/dest/index.d.ts CHANGED
@@ -1,6 +1,8 @@
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 { type BotConfig, getBotConfigFromEnv, getBotDefaultConfig, botConfigMappings, SupportedTokenContracts, } from './config.js';
4
- export { createBotRunnerRpcServer, getBotRunnerApiHandler } from './rpc.js';
6
+ export { getBotRunnerApiHandler } from './rpc.js';
5
7
  export * from './interface.js';
6
- //# sourceMappingURL=index.d.ts.map
8
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQUUsR0FBRyxFQUFFLE1BQU0sVUFBVSxDQUFDO0FBQy9CLE9BQU8sRUFBRSxNQUFNLEVBQUUsTUFBTSxjQUFjLENBQUM7QUFDdEMsT0FBTyxFQUFFLFNBQVMsRUFBRSxNQUFNLGFBQWEsQ0FBQztBQUN4QyxPQUFPLEVBQUUsUUFBUSxFQUFFLE1BQU0sc0JBQXNCLENBQUM7QUFDaEQsT0FBTyxFQUNMLEtBQUssU0FBUyxFQUNkLG1CQUFtQixFQUNuQixtQkFBbUIsRUFDbkIsaUJBQWlCLEVBQ2pCLHVCQUF1QixHQUN4QixNQUFNLGFBQWEsQ0FBQztBQUNyQixPQUFPLEVBQUUsc0JBQXNCLEVBQUUsTUFBTSxVQUFVLENBQUM7QUFDbEQsY0FBYyxnQkFBZ0IsQ0FBQyJ9
@@ -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,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,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 { getBotConfigFromEnv, getBotDefaultConfig, botConfigMappings, SupportedTokenContracts } from './config.js';
4
- export { createBotRunnerRpcServer, getBotRunnerApiHandler } from './rpc.js';
6
+ export { getBotRunnerApiHandler } from './rpc.js';
5
7
  export * from './interface.js';
@@ -1,12 +1,23 @@
1
+ import { AztecAddress } from '@aztec/aztec.js/addresses';
1
2
  import type { ApiSchemaFor } from '@aztec/stdlib/schemas';
3
+ import { z } from 'zod';
2
4
  import { type BotConfig } from './config.js';
5
+ export declare const BotInfoSchema: z.ZodObject<{
6
+ botAddress: import("@aztec/stdlib/schemas").ZodFor<AztecAddress>;
7
+ }, "strip", z.ZodTypeAny, {
8
+ botAddress: AztecAddress;
9
+ }, {
10
+ botAddress?: any;
11
+ }>;
12
+ export type BotInfo = z.infer<typeof BotInfoSchema>;
3
13
  export interface BotRunnerApi {
4
14
  start(): Promise<void>;
5
15
  stop(): Promise<void>;
6
16
  run(): Promise<void>;
7
17
  setup(): Promise<void>;
8
18
  getConfig(): Promise<BotConfig>;
19
+ getInfo(): Promise<BotInfo>;
9
20
  update(config: BotConfig): Promise<void>;
10
21
  }
11
22
  export declare const BotRunnerApiSchema: ApiSchemaFor<BotRunnerApi>;
12
- //# sourceMappingURL=interface.d.ts.map
23
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW50ZXJmYWNlLmQudHMiLCJz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AAO,CAAC,CAAC;IAC5B,MAAM,CAAC,MAAM,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1C;AAED,eAAO,MAAM,kBAAkB,EAAE,YAAY,CAAC,YAAY,CAQzD,CAAC"}
package/dest/interface.js CHANGED
@@ -1,10 +1,15 @@
1
+ import { AztecAddress } from '@aztec/aztec.js/addresses';
1
2
  import { z } from 'zod';
2
3
  import { BotConfigSchema } from './config.js';
4
+ export const BotInfoSchema = z.object({
5
+ botAddress: AztecAddress.schema
6
+ });
3
7
  export const BotRunnerApiSchema = {
4
8
  start: z.function().args().returns(z.void()),
5
9
  stop: z.function().args().returns(z.void()),
6
10
  run: z.function().args().returns(z.void()),
7
11
  setup: z.function().args().returns(z.void()),
12
+ getInfo: z.function().args().returns(BotInfoSchema),
8
13
  getConfig: z.function().args().returns(BotConfigSchema),
9
14
  update: z.function().args(BotConfigSchema).returns(z.void())
10
15
  };
package/dest/rpc.d.ts CHANGED
@@ -1,10 +1,4 @@
1
1
  import type { ApiHandler } from '@aztec/foundation/json-rpc/server';
2
2
  import type { BotRunner } from './runner.js';
3
- /**
4
- * Wraps a bot runner with a JSON RPC HTTP server.
5
- * @param botRunner - The BotRunner.
6
- * @returns An JSON-RPC HTTP server
7
- */
8
- export declare function createBotRunnerRpcServer(botRunner: BotRunner): void;
9
3
  export declare function getBotRunnerApiHandler(botRunner: BotRunner): ApiHandler;
10
- //# sourceMappingURL=rpc.d.ts.map
4
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicnBjLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvcnBjLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sS0FBSyxFQUFFLFVBQVUsRUFBRSxNQUFNLG1DQUFtQyxDQUFDO0FBR3BFLE9BQU8sS0FBSyxFQUFFLFNBQVMsRUFBRSxNQUFNLGFBQWEsQ0FBQztBQUU3Qyx3QkFBZ0Isc0JBQXNCLENBQUMsU0FBUyxFQUFFLFNBQVMsR0FBRyxVQUFVLENBRXZFIn0=
package/dest/rpc.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"rpc.d.ts","sourceRoot":"","sources":["../src/rpc.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mCAAmC,CAAC;AAIpE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,SAAS,EAAE,SAAS,QAK5D;AAED,wBAAgB,sBAAsB,CAAC,SAAS,EAAE,SAAS,GAAG,UAAU,CAEvE"}
1
+ {"version":3,"file":"rpc.d.ts","sourceRoot":"","sources":["../src/rpc.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mCAAmC,CAAC;AAGpE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C,wBAAgB,sBAAsB,CAAC,SAAS,EAAE,SAAS,GAAG,UAAU,CAEvE"}
package/dest/rpc.js CHANGED
@@ -1,15 +1,4 @@
1
- import { createTracedJsonRpcServer } from '@aztec/telemetry-client';
2
1
  import { BotRunnerApiSchema } from './interface.js';
3
- /**
4
- * Wraps a bot runner with a JSON RPC HTTP server.
5
- * @param botRunner - The BotRunner.
6
- * @returns An JSON-RPC HTTP server
7
- */ export function createBotRunnerRpcServer(botRunner) {
8
- createTracedJsonRpcServer(botRunner, BotRunnerApiSchema, {
9
- http200OnError: false,
10
- healthCheck: botRunner.isHealthy.bind(botRunner)
11
- });
12
- }
13
2
  export function getBotRunnerApiHandler(botRunner) {
14
3
  return [
15
4
  botRunner,