@aztec/bot 0.0.1-commit.f2ce05ee → 0.0.1-commit.f5a9928

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 (50) hide show
  1. package/dest/amm_bot.d.ts +6 -5
  2. package/dest/amm_bot.d.ts.map +1 -1
  3. package/dest/amm_bot.js +26 -19
  4. package/dest/base_bot.d.ts +7 -7
  5. package/dest/base_bot.d.ts.map +1 -1
  6. package/dest/base_bot.js +21 -32
  7. package/dest/bot.d.ts +5 -4
  8. package/dest/bot.d.ts.map +1 -1
  9. package/dest/bot.js +7 -10
  10. package/dest/config.d.ts +47 -82
  11. package/dest/config.d.ts.map +1 -1
  12. package/dest/config.js +42 -15
  13. package/dest/cross_chain_bot.d.ts +56 -0
  14. package/dest/cross_chain_bot.d.ts.map +1 -0
  15. package/dest/cross_chain_bot.js +138 -0
  16. package/dest/factory.d.ts +34 -9
  17. package/dest/factory.d.ts.map +1 -1
  18. package/dest/factory.js +293 -169
  19. package/dest/index.d.ts +2 -1
  20. package/dest/index.d.ts.map +1 -1
  21. package/dest/index.js +1 -0
  22. package/dest/interface.d.ts +2 -6
  23. package/dest/interface.d.ts.map +1 -1
  24. package/dest/interface.js +30 -7
  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/runner.d.ts +4 -3
  29. package/dest/runner.d.ts.map +1 -1
  30. package/dest/runner.js +20 -2
  31. package/dest/store/bot_store.d.ts +30 -5
  32. package/dest/store/bot_store.d.ts.map +1 -1
  33. package/dest/store/bot_store.js +37 -6
  34. package/dest/store/index.d.ts +2 -2
  35. package/dest/store/index.d.ts.map +1 -1
  36. package/dest/utils.js +3 -3
  37. package/package.json +17 -14
  38. package/src/amm_bot.ts +28 -20
  39. package/src/base_bot.ts +16 -33
  40. package/src/bot.ts +11 -10
  41. package/src/config.ts +47 -18
  42. package/src/cross_chain_bot.ts +208 -0
  43. package/src/factory.ts +322 -177
  44. package/src/index.ts +1 -0
  45. package/src/interface.ts +7 -7
  46. package/src/l1_to_l2_seeding.ts +79 -0
  47. package/src/runner.ts +41 -5
  48. package/src/store/bot_store.ts +60 -5
  49. package/src/store/index.ts +1 -1
  50. package/src/utils.ts +3 -3
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { Bot } from './bot.js';
2
2
  export { AmmBot } from './amm_bot.js';
3
+ export { CrossChainBot } from './cross_chain_bot.js';
3
4
  export { BotRunner } from './runner.js';
4
5
  export { BotStore } from './store/bot_store.js';
5
6
  export {
package/src/interface.ts CHANGED
@@ -22,11 +22,11 @@ export interface BotRunnerApi {
22
22
  }
23
23
 
24
24
  export const BotRunnerApiSchema: ApiSchemaFor<BotRunnerApi> = {
25
- start: z.function().args().returns(z.void()),
26
- stop: z.function().args().returns(z.void()),
27
- run: z.function().args().returns(z.void()),
28
- setup: z.function().args().returns(z.void()),
29
- getInfo: z.function().args().returns(BotInfoSchema),
30
- getConfig: z.function().args().returns(BotConfigSchema),
31
- update: z.function().args(BotConfigSchema).returns(z.void()),
25
+ start: z.function({ input: z.tuple([]), output: z.void() }),
26
+ stop: z.function({ input: z.tuple([]), output: z.void() }),
27
+ run: z.function({ input: z.tuple([]), output: z.void() }),
28
+ setup: z.function({ input: z.tuple([]), output: z.void() }),
29
+ getInfo: z.function({ input: z.tuple([]), output: BotInfoSchema }),
30
+ getConfig: z.function({ input: z.tuple([]), output: BotConfigSchema }),
31
+ update: z.function({ input: z.tuple([BotConfigSchema]), output: z.void() }),
32
32
  };
@@ -0,0 +1,79 @@
1
+ import { generateClaimSecret } from '@aztec/aztec.js/ethereum';
2
+ import type { ExtendedViemWalletClient } from '@aztec/ethereum/types';
3
+ import { compactArray } from '@aztec/foundation/collection';
4
+ import { Fr } from '@aztec/foundation/curves/bn254';
5
+ import { EthAddress } from '@aztec/foundation/eth-address';
6
+ import type { Logger } from '@aztec/foundation/log';
7
+ import { InboxAbi } from '@aztec/l1-artifacts';
8
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
9
+
10
+ import { decodeEventLog, getContract } from 'viem';
11
+
12
+ import type { BotStore, PendingL1ToL2Message } from './store/index.js';
13
+
14
+ /** Sends an L1→L2 message via the Inbox contract and stores it. */
15
+ export async function seedL1ToL2Message(
16
+ l1Client: ExtendedViemWalletClient,
17
+ inboxAddress: EthAddress,
18
+ l2Recipient: AztecAddress,
19
+ rollupVersion: bigint,
20
+ store: BotStore,
21
+ log: Logger,
22
+ ): Promise<PendingL1ToL2Message> {
23
+ log.info('Seeding L1→L2 message');
24
+ const [secret, secretHash] = await generateClaimSecret(log);
25
+ const content = Fr.random();
26
+
27
+ const inbox = getContract({
28
+ address: inboxAddress.toString(),
29
+ abi: InboxAbi,
30
+ client: l1Client,
31
+ });
32
+
33
+ const txHash = await inbox.write.sendL2Message(
34
+ [{ actor: l2Recipient.toString(), version: rollupVersion }, content.toString(), secretHash.toString()],
35
+ { gas: 1_000_000n },
36
+ );
37
+ log.info(`L1→L2 message sent in tx ${txHash}`);
38
+
39
+ const txReceipt = await l1Client.waitForTransactionReceipt({ hash: txHash });
40
+ if (txReceipt.status !== 'success') {
41
+ throw new Error(`L1→L2 message tx failed: ${txHash}`);
42
+ }
43
+
44
+ // Extract MessageSent event
45
+ const messageSentLogs = compactArray(
46
+ txReceipt.logs
47
+ .filter(l => l.address.toLowerCase() === inboxAddress.toString().toLowerCase())
48
+ .map(l => {
49
+ try {
50
+ return decodeEventLog({ abi: InboxAbi, eventName: 'MessageSent', data: l.data, topics: l.topics });
51
+ } catch {
52
+ return undefined;
53
+ }
54
+ }),
55
+ );
56
+
57
+ if (messageSentLogs.length !== 1) {
58
+ throw new Error(`Expected 1 MessageSent event, got ${messageSentLogs.length}`);
59
+ }
60
+
61
+ const event = messageSentLogs[0];
62
+
63
+ const msgHash = event.args.hash;
64
+ const globalLeafIndex = event.args.index;
65
+
66
+ const msg: PendingL1ToL2Message = {
67
+ content: content.toString(),
68
+ secret: secret.toString(),
69
+ secretHash: secretHash.toString(),
70
+ msgHash,
71
+ sender: l1Client.account!.address,
72
+ globalLeafIndex: globalLeafIndex.toString(),
73
+ timestamp: Date.now(),
74
+ };
75
+
76
+ await store.savePendingL1ToL2Message(msg);
77
+ log.info(`Seeded L1→L2 message msgHash=${msg.msgHash}`);
78
+ return msg;
79
+ }
package/src/runner.ts CHANGED
@@ -2,14 +2,16 @@ import { createLogger } from '@aztec/aztec.js/log';
2
2
  import type { AztecNode } from '@aztec/aztec.js/node';
3
3
  import { omit } from '@aztec/foundation/collection';
4
4
  import { RunningPromise } from '@aztec/foundation/running-promise';
5
+ import type { BlockTag } from '@aztec/stdlib/block';
5
6
  import type { AztecNodeAdmin } from '@aztec/stdlib/interfaces/client';
6
7
  import { type TelemetryClient, type Traceable, type Tracer, trackSpan } from '@aztec/telemetry-client';
7
- import type { TestWallet } from '@aztec/test-wallet/server';
8
+ import type { EmbeddedWallet } from '@aztec/wallets/embedded';
8
9
 
9
10
  import { AmmBot } from './amm_bot.js';
10
11
  import type { BaseBot } from './base_bot.js';
11
12
  import { Bot } from './bot.js';
12
13
  import type { BotConfig } from './config.js';
14
+ import { CrossChainBot } from './cross_chain_bot.js';
13
15
  import type { BotInfo, BotRunnerApi } from './interface.js';
14
16
  import { BotStore } from './store/index.js';
15
17
 
@@ -24,11 +26,12 @@ export class BotRunner implements BotRunnerApi, Traceable {
24
26
 
25
27
  public constructor(
26
28
  private config: BotConfig,
27
- private readonly wallet: TestWallet,
29
+ private readonly wallet: EmbeddedWallet,
28
30
  private readonly aztecNode: AztecNode,
29
31
  private readonly telemetry: TelemetryClient,
30
32
  private readonly aztecNodeAdmin: AztecNodeAdmin | undefined,
31
33
  private readonly store: BotStore,
34
+ private readonly syncChainTip?: BlockTag,
32
35
  ) {
33
36
  this.tracer = telemetry.getTracer('Bot');
34
37
 
@@ -146,9 +149,42 @@ export class BotRunner implements BotRunnerApi, Traceable {
146
149
 
147
150
  async #createBot() {
148
151
  try {
149
- this.bot = this.config.ammTxs
150
- ? AmmBot.create(this.config, this.wallet, this.aztecNode, this.aztecNodeAdmin, this.store)
151
- : Bot.create(this.config, this.wallet, this.aztecNode, this.aztecNodeAdmin, this.store);
152
+ switch (this.config.botMode) {
153
+ case 'crosschain':
154
+ this.bot = CrossChainBot.create(
155
+ this.config,
156
+ this.wallet,
157
+ this.aztecNode,
158
+ this.aztecNodeAdmin,
159
+ this.store,
160
+ this.syncChainTip,
161
+ );
162
+ break;
163
+ case 'amm':
164
+ this.bot = AmmBot.create(
165
+ this.config,
166
+ this.wallet,
167
+ this.aztecNode,
168
+ this.aztecNodeAdmin,
169
+ this.store,
170
+ this.syncChainTip,
171
+ );
172
+ break;
173
+ case 'transfer':
174
+ this.bot = Bot.create(
175
+ this.config,
176
+ this.wallet,
177
+ this.aztecNode,
178
+ this.aztecNodeAdmin,
179
+ this.store,
180
+ this.syncChainTip,
181
+ );
182
+ break;
183
+ default: {
184
+ const _exhaustive: never = this.config.botMode;
185
+ throw new Error(`Unsupported bot mode: [${_exhaustive}]`);
186
+ }
187
+ }
152
188
  await this.bot;
153
189
  } catch (err) {
154
190
  this.log.error(`Error setting up bot: ${err}`);
@@ -2,6 +2,7 @@ import { AztecAddress } from '@aztec/aztec.js/addresses';
2
2
  import type { L2AmountClaim } from '@aztec/aztec.js/ethereum';
3
3
  import { Fr } from '@aztec/foundation/curves/bn254';
4
4
  import { type Logger, createLogger } from '@aztec/foundation/log';
5
+ import { DateProvider } from '@aztec/foundation/timer';
5
6
  import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
6
7
 
7
8
  export interface BridgeClaimData {
@@ -10,18 +11,38 @@ export interface BridgeClaimData {
10
11
  recipient: string;
11
12
  }
12
13
 
14
+ export interface PendingL1ToL2Message {
15
+ /** Random content field sent in the message. */
16
+ content: string;
17
+ /** Secret for consuming the message. */
18
+ secret: string;
19
+ /** Hash of the secret. */
20
+ secretHash: string;
21
+ /** Hash of the L1→L2 message. */
22
+ msgHash: string;
23
+ /** L1 sender address (hex). */
24
+ sender: string;
25
+ /** Global leaf index in the L1→L2 message tree. */
26
+ globalLeafIndex: string;
27
+ /** Timestamp when the message was seeded. */
28
+ timestamp: number;
29
+ }
30
+
13
31
  /**
14
32
  * Simple data store for the bot to persist L1 bridge claims.
15
33
  */
16
34
  export class BotStore {
17
35
  public static readonly SCHEMA_VERSION = 1;
18
36
  private readonly bridgeClaims: AztecAsyncMap<string, string>;
37
+ private readonly pendingL1ToL2: AztecAsyncMap<string, string>;
19
38
 
20
39
  constructor(
21
40
  private readonly store: AztecAsyncKVStore,
22
41
  private readonly log: Logger = createLogger('bot:store'),
42
+ private readonly dateProvider: DateProvider = new DateProvider(),
23
43
  ) {
24
44
  this.bridgeClaims = store.openMap<string, string>('bridge_claims');
45
+ this.pendingL1ToL2 = store.openMap<string, string>('pending_l1_to_l2');
25
46
  }
26
47
 
27
48
  /**
@@ -39,7 +60,7 @@ export class BotStore {
39
60
 
40
61
  const data = {
41
62
  claim: serializableClaim,
42
- timestamp: Date.now(),
63
+ timestamp: this.dateProvider.now(),
43
64
  recipient: recipient.toString(),
44
65
  };
45
66
 
@@ -115,7 +136,7 @@ export class BotStore {
115
136
  * Cleans up old bridge claims (older than 24 hours).
116
137
  */
117
138
  public async cleanupOldClaims(maxAgeMs: number = 24 * 60 * 60 * 1000): Promise<number> {
118
- const now = Date.now();
139
+ const now = this.dateProvider.now();
119
140
  let cleanedCount = 0;
120
141
  const entries = this.bridgeClaims.entriesAsync();
121
142
 
@@ -131,9 +152,43 @@ export class BotStore {
131
152
  return cleanedCount;
132
153
  }
133
154
 
134
- /**
135
- * Closes the store.
136
- */
155
+ /** Saves a pending L1→L2 message keyed by msgHash. */
156
+ public async savePendingL1ToL2Message(msg: PendingL1ToL2Message): Promise<void> {
157
+ await this.pendingL1ToL2.set(msg.msgHash, JSON.stringify(msg));
158
+ this.log.info(`Saved pending L1→L2 message ${msg.msgHash}`);
159
+ }
160
+
161
+ /** Returns all unconsumed pending L1→L2 messages. */
162
+ public async getUnconsumedL1ToL2Messages(): Promise<PendingL1ToL2Message[]> {
163
+ const messages: PendingL1ToL2Message[] = [];
164
+ for await (const [_, data] of this.pendingL1ToL2.entriesAsync()) {
165
+ messages.push(JSON.parse(data));
166
+ }
167
+ return messages;
168
+ }
169
+
170
+ /** Deletes a consumed L1→L2 message from the store. */
171
+ public async deleteL1ToL2Message(msgHash: string): Promise<void> {
172
+ await this.pendingL1ToL2.delete(msgHash);
173
+ this.log.info(`Deleted consumed L1→L2 message ${msgHash}`);
174
+ }
175
+
176
+ /** Cleans up pending L1→L2 messages older than maxAgeMs. */
177
+ public async cleanupOldPendingMessages(maxAgeMs: number = 24 * 60 * 60 * 1000): Promise<number> {
178
+ const now = this.dateProvider.now();
179
+ let cleanedCount = 0;
180
+ for await (const [key, data] of this.pendingL1ToL2.entriesAsync()) {
181
+ const parsed = JSON.parse(data);
182
+ if (now - parsed.timestamp > maxAgeMs) {
183
+ await this.pendingL1ToL2.delete(key);
184
+ cleanedCount++;
185
+ this.log.info(`Cleaned up old pending L1→L2 message ${key}`);
186
+ }
187
+ }
188
+ return cleanedCount;
189
+ }
190
+
191
+ /** Closes the store. */
137
192
  public async close(): Promise<void> {
138
193
  await this.store.close();
139
194
  this.log.info('Closed bot data store');
@@ -1 +1 @@
1
- export { BotStore, type BridgeClaimData } from './bot_store.js';
1
+ export { BotStore, type BridgeClaimData, type PendingL1ToL2Message } from './bot_store.js';
package/src/utils.ts CHANGED
@@ -15,8 +15,8 @@ export async function getBalances(
15
15
  who: AztecAddress,
16
16
  from?: AztecAddress,
17
17
  ): Promise<{ privateBalance: bigint; publicBalance: bigint }> {
18
- const privateBalance = await token.methods.balance_of_private(who).simulate({ from: from ?? who });
19
- const publicBalance = await token.methods.balance_of_public(who).simulate({ from: from ?? who });
18
+ const { result: privateBalance } = await token.methods.balance_of_private(who).simulate({ from: from ?? who });
19
+ const { result: publicBalance } = await token.methods.balance_of_public(who).simulate({ from: from ?? who });
20
20
  return { privateBalance, publicBalance };
21
21
  }
22
22
 
@@ -25,7 +25,7 @@ export async function getPrivateBalance(
25
25
  who: AztecAddress,
26
26
  from?: AztecAddress,
27
27
  ): Promise<bigint> {
28
- const privateBalance = await token.methods.get_balance(who).simulate({ from: from ?? who });
28
+ const { result: privateBalance } = await token.methods.get_balance(who).simulate({ from: from ?? who });
29
29
  return privateBalance;
30
30
  }
31
31