@aztec/bot 2.1.0-rc.9 → 3.0.0-devnet.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dest/amm_bot.d.ts +8 -8
- package/dest/amm_bot.d.ts.map +1 -1
- package/dest/amm_bot.js +23 -20
- package/dest/base_bot.d.ts +10 -7
- package/dest/base_bot.d.ts.map +1 -1
- package/dest/base_bot.js +18 -18
- package/dest/bot.d.ts +7 -8
- package/dest/bot.d.ts.map +1 -1
- package/dest/bot.js +11 -12
- package/dest/config.d.ts +19 -15
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +22 -20
- package/dest/factory.d.ts +18 -17
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +122 -102
- package/dest/index.d.ts +1 -0
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -0
- package/dest/interface.d.ts +1 -1
- package/dest/interface.d.ts.map +1 -1
- package/dest/interface.js +1 -1
- package/dest/runner.d.ts +11 -12
- package/dest/runner.d.ts.map +1 -1
- package/dest/runner.js +18 -32
- package/dest/store/bot_store.d.ts +44 -0
- package/dest/store/bot_store.d.ts.map +1 -0
- package/dest/store/bot_store.js +107 -0
- package/dest/store/index.d.ts +2 -0
- package/dest/store/index.d.ts.map +1 -0
- package/dest/store/index.js +1 -0
- package/dest/utils.d.ts +3 -3
- package/dest/utils.d.ts.map +1 -1
- package/dest/utils.js +5 -5
- package/package.json +13 -11
- package/src/amm_bot.ts +39 -31
- package/src/base_bot.ts +24 -22
- package/src/bot.ts +25 -14
- package/src/config.ts +20 -22
- package/src/factory.ts +138 -140
- package/src/index.ts +1 -0
- package/src/interface.ts +1 -1
- package/src/runner.ts +19 -23
- package/src/store/bot_store.ts +141 -0
- package/src/store/index.ts +1 -0
- package/src/utils.ts +10 -5
package/src/runner.ts
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { createLogger } from '@aztec/aztec.js/log';
|
|
2
|
+
import type { AztecNode } from '@aztec/aztec.js/node';
|
|
2
3
|
import { omit } from '@aztec/foundation/collection';
|
|
3
4
|
import { RunningPromise } from '@aztec/foundation/running-promise';
|
|
4
|
-
import {
|
|
5
|
-
import { type TelemetryClient, type Traceable, type Tracer,
|
|
5
|
+
import type { AztecNodeAdmin } from '@aztec/stdlib/interfaces/client';
|
|
6
|
+
import { type TelemetryClient, type Traceable, type Tracer, trackSpan } from '@aztec/telemetry-client';
|
|
7
|
+
import type { TestWallet } from '@aztec/test-wallet/server';
|
|
6
8
|
|
|
7
9
|
import { AmmBot } from './amm_bot.js';
|
|
8
10
|
import type { BaseBot } from './base_bot.js';
|
|
9
11
|
import { Bot } from './bot.js';
|
|
10
|
-
import {
|
|
12
|
+
import type { BotConfig } from './config.js';
|
|
11
13
|
import type { BotInfo, BotRunnerApi } from './interface.js';
|
|
14
|
+
import { BotStore } from './store/index.js';
|
|
12
15
|
|
|
13
16
|
export class BotRunner implements BotRunnerApi, Traceable {
|
|
14
17
|
private log = createLogger('bot');
|
|
15
18
|
private bot?: Promise<BaseBot>;
|
|
16
|
-
private pxe?: PXE;
|
|
17
|
-
private node: AztecNode;
|
|
18
|
-
private nodeAdmin?: AztecNodeAdmin;
|
|
19
19
|
private runningPromise: RunningPromise;
|
|
20
20
|
private consecutiveErrors = 0;
|
|
21
21
|
private healthy = true;
|
|
@@ -24,19 +24,14 @@ export class BotRunner implements BotRunnerApi, Traceable {
|
|
|
24
24
|
|
|
25
25
|
public constructor(
|
|
26
26
|
private config: BotConfig,
|
|
27
|
-
|
|
27
|
+
private readonly wallet: TestWallet,
|
|
28
|
+
private readonly aztecNode: AztecNode,
|
|
29
|
+
private readonly telemetry: TelemetryClient,
|
|
30
|
+
private readonly aztecNodeAdmin: AztecNodeAdmin | undefined,
|
|
31
|
+
private readonly store: BotStore,
|
|
28
32
|
) {
|
|
29
|
-
this.tracer =
|
|
30
|
-
|
|
31
|
-
if (!dependencies.node && !config.nodeUrl) {
|
|
32
|
-
throw new Error(`Missing node URL in config or dependencies`);
|
|
33
|
-
}
|
|
34
|
-
const versions = getVersions();
|
|
35
|
-
const fetch = makeTracedFetch([1, 2, 3], true);
|
|
36
|
-
this.node = dependencies.node ?? createAztecNodeClient(config.nodeUrl!, versions, fetch);
|
|
37
|
-
this.nodeAdmin =
|
|
38
|
-
dependencies.nodeAdmin ??
|
|
39
|
-
(config.nodeAdminUrl ? createAztecNodeAdminClient(config.nodeAdminUrl, versions, fetch) : undefined);
|
|
33
|
+
this.tracer = telemetry.getTracer('Bot');
|
|
34
|
+
|
|
40
35
|
this.runningPromise = new RunningPromise(() => this.#work(), this.log, config.txIntervalSeconds * 1000);
|
|
41
36
|
}
|
|
42
37
|
|
|
@@ -74,6 +69,7 @@ export class BotRunner implements BotRunnerApi, Traceable {
|
|
|
74
69
|
this.log.verbose(`Stopping bot`);
|
|
75
70
|
await this.runningPromise.stop();
|
|
76
71
|
}
|
|
72
|
+
await this.store.close();
|
|
77
73
|
this.log.info(`Stopped bot`);
|
|
78
74
|
}
|
|
79
75
|
|
|
@@ -144,15 +140,15 @@ export class BotRunner implements BotRunnerApi, Traceable {
|
|
|
144
140
|
if (!this.bot) {
|
|
145
141
|
throw new Error(`Bot is not initialized`);
|
|
146
142
|
}
|
|
147
|
-
const botAddress = await this.bot.then(b => b.
|
|
143
|
+
const botAddress = await this.bot.then(b => b.defaultAccountAddress);
|
|
148
144
|
return { botAddress };
|
|
149
145
|
}
|
|
150
146
|
|
|
151
147
|
async #createBot() {
|
|
152
148
|
try {
|
|
153
149
|
this.bot = this.config.ammTxs
|
|
154
|
-
? AmmBot.create(this.config,
|
|
155
|
-
: Bot.create(this.config,
|
|
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);
|
|
156
152
|
await this.bot;
|
|
157
153
|
} catch (err) {
|
|
158
154
|
this.log.error(`Error setting up bot: ${err}`);
|
|
@@ -163,7 +159,7 @@ export class BotRunner implements BotRunnerApi, Traceable {
|
|
|
163
159
|
@trackSpan('Bot.work')
|
|
164
160
|
async #work() {
|
|
165
161
|
if (this.config.maxPendingTxs > 0) {
|
|
166
|
-
const pendingTxCount = await this.
|
|
162
|
+
const pendingTxCount = await this.aztecNode.getPendingTxCount();
|
|
167
163
|
if (pendingTxCount >= this.config.maxPendingTxs) {
|
|
168
164
|
this.log.verbose(`Not sending bot tx since node has ${pendingTxCount} pending txs`);
|
|
169
165
|
return;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { AztecAddress } from '@aztec/aztec.js/addresses';
|
|
2
|
+
import type { L2AmountClaim } from '@aztec/aztec.js/ethereum';
|
|
3
|
+
import { Fr } from '@aztec/foundation/fields';
|
|
4
|
+
import { type Logger, createLogger } from '@aztec/foundation/log';
|
|
5
|
+
import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
|
|
6
|
+
|
|
7
|
+
export interface BridgeClaimData {
|
|
8
|
+
claim: L2AmountClaim;
|
|
9
|
+
timestamp: number;
|
|
10
|
+
recipient: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Simple data store for the bot to persist L1 bridge claims.
|
|
15
|
+
*/
|
|
16
|
+
export class BotStore {
|
|
17
|
+
public static readonly SCHEMA_VERSION = 1;
|
|
18
|
+
private readonly bridgeClaims: AztecAsyncMap<string, string>;
|
|
19
|
+
|
|
20
|
+
constructor(
|
|
21
|
+
private readonly store: AztecAsyncKVStore,
|
|
22
|
+
private readonly log: Logger = createLogger('bot:store'),
|
|
23
|
+
) {
|
|
24
|
+
this.bridgeClaims = store.openMap<string, string>('bridge_claims');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Saves a bridge claim for a recipient.
|
|
29
|
+
*/
|
|
30
|
+
public async saveBridgeClaim(recipient: AztecAddress, claim: L2AmountClaim): Promise<void> {
|
|
31
|
+
// Convert Fr fields and BigInts to strings for JSON serialization
|
|
32
|
+
const serializableClaim = {
|
|
33
|
+
claimAmount: claim.claimAmount.toString(),
|
|
34
|
+
claimSecret: claim.claimSecret.toString(),
|
|
35
|
+
claimSecretHash: claim.claimSecretHash.toString(),
|
|
36
|
+
messageHash: claim.messageHash,
|
|
37
|
+
messageLeafIndex: claim.messageLeafIndex.toString(),
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const data = {
|
|
41
|
+
claim: serializableClaim,
|
|
42
|
+
timestamp: Date.now(),
|
|
43
|
+
recipient: recipient.toString(),
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
await this.bridgeClaims.set(recipient.toString(), JSON.stringify(data));
|
|
47
|
+
this.log.info(`Saved bridge claim for ${recipient.toString()}`);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Gets a bridge claim for a recipient if it exists.
|
|
52
|
+
*/
|
|
53
|
+
public async getBridgeClaim(recipient: AztecAddress): Promise<BridgeClaimData | undefined> {
|
|
54
|
+
const data = await this.bridgeClaims.getAsync(recipient.toString());
|
|
55
|
+
if (!data) {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const parsed = JSON.parse(data);
|
|
60
|
+
|
|
61
|
+
// Reconstruct L2AmountClaim from serialized data
|
|
62
|
+
const claim: L2AmountClaim = {
|
|
63
|
+
claimAmount: BigInt(parsed.claim.claimAmount),
|
|
64
|
+
claimSecret: Fr.fromString(parsed.claim.claimSecret),
|
|
65
|
+
claimSecretHash: Fr.fromString(parsed.claim.claimSecretHash),
|
|
66
|
+
messageHash: parsed.claim.messageHash,
|
|
67
|
+
messageLeafIndex: BigInt(parsed.claim.messageLeafIndex),
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
claim,
|
|
72
|
+
timestamp: parsed.timestamp,
|
|
73
|
+
recipient: parsed.recipient,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Deletes a bridge claim for a recipient.
|
|
79
|
+
*/
|
|
80
|
+
public async deleteBridgeClaim(recipient: AztecAddress): Promise<void> {
|
|
81
|
+
await this.bridgeClaims.delete(recipient.toString());
|
|
82
|
+
this.log.info(`Deleted bridge claim for ${recipient.toString()}`);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Gets all stored bridge claims.
|
|
87
|
+
*/
|
|
88
|
+
public async getAllBridgeClaims(): Promise<BridgeClaimData[]> {
|
|
89
|
+
const claims: BridgeClaimData[] = [];
|
|
90
|
+
const entries = this.bridgeClaims.entriesAsync();
|
|
91
|
+
|
|
92
|
+
for await (const [_, data] of entries) {
|
|
93
|
+
const parsed = JSON.parse(data);
|
|
94
|
+
|
|
95
|
+
// Reconstruct L2AmountClaim from serialized data
|
|
96
|
+
const claim: L2AmountClaim = {
|
|
97
|
+
claimAmount: BigInt(parsed.claim.claimAmount),
|
|
98
|
+
claimSecret: Fr.fromString(parsed.claim.claimSecret),
|
|
99
|
+
claimSecretHash: Fr.fromString(parsed.claim.claimSecretHash),
|
|
100
|
+
messageHash: parsed.claim.messageHash,
|
|
101
|
+
messageLeafIndex: BigInt(parsed.claim.messageLeafIndex),
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
claims.push({
|
|
105
|
+
claim,
|
|
106
|
+
timestamp: parsed.timestamp,
|
|
107
|
+
recipient: parsed.recipient,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return claims;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Cleans up old bridge claims (older than 24 hours).
|
|
116
|
+
*/
|
|
117
|
+
public async cleanupOldClaims(maxAgeMs: number = 24 * 60 * 60 * 1000): Promise<number> {
|
|
118
|
+
const now = Date.now();
|
|
119
|
+
let cleanedCount = 0;
|
|
120
|
+
const entries = this.bridgeClaims.entriesAsync();
|
|
121
|
+
|
|
122
|
+
for await (const [key, data] of entries) {
|
|
123
|
+
const parsed = JSON.parse(data);
|
|
124
|
+
if (now - parsed.timestamp > maxAgeMs) {
|
|
125
|
+
await this.bridgeClaims.delete(key);
|
|
126
|
+
cleanedCount++;
|
|
127
|
+
this.log.info(`Cleaned up old bridge claim for ${parsed.recipient}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return cleanedCount;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Closes the store.
|
|
136
|
+
*/
|
|
137
|
+
public async close(): Promise<void> {
|
|
138
|
+
await this.store.close();
|
|
139
|
+
this.log.info('Closed bot data store');
|
|
140
|
+
}
|
|
141
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { BotStore, type BridgeClaimData } from './bot_store.js';
|
package/src/utils.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ContractBase } from '@aztec/aztec.js/contracts';
|
|
2
2
|
import type { AMMContract } from '@aztec/noir-contracts.js/AMM';
|
|
3
3
|
import type { PrivateTokenContract } from '@aztec/noir-contracts.js/PrivateToken';
|
|
4
4
|
import type { TokenContract } from '@aztec/noir-contracts.js/Token';
|
|
@@ -13,14 +13,19 @@ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
|
|
|
13
13
|
export async function getBalances(
|
|
14
14
|
token: TokenContract,
|
|
15
15
|
who: AztecAddress,
|
|
16
|
+
from?: AztecAddress,
|
|
16
17
|
): Promise<{ privateBalance: bigint; publicBalance: bigint }> {
|
|
17
|
-
const privateBalance = await token.methods.balance_of_private(who).simulate({ from: who });
|
|
18
|
-
const publicBalance = await token.methods.balance_of_public(who).simulate({ from: who });
|
|
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 });
|
|
19
20
|
return { privateBalance, publicBalance };
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
export async function getPrivateBalance(
|
|
23
|
-
|
|
23
|
+
export async function getPrivateBalance(
|
|
24
|
+
token: PrivateTokenContract,
|
|
25
|
+
who: AztecAddress,
|
|
26
|
+
from?: AztecAddress,
|
|
27
|
+
): Promise<bigint> {
|
|
28
|
+
const privateBalance = await token.methods.get_balance(who).simulate({ from: from ?? who });
|
|
24
29
|
return privateBalance;
|
|
25
30
|
}
|
|
26
31
|
|