@aztec/bot 0.0.1-commit.3469e52 → 0.0.1-commit.381b1a9
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 +6 -7
- package/dest/amm_bot.d.ts.map +1 -1
- package/dest/amm_bot.js +27 -16
- package/dest/base_bot.d.ts +6 -6
- package/dest/base_bot.d.ts.map +1 -1
- package/dest/base_bot.js +12 -13
- package/dest/bot.d.ts +6 -6
- package/dest/bot.d.ts.map +1 -1
- package/dest/bot.js +8 -4
- package/dest/config.d.ts +30 -14
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +36 -8
- package/dest/cross_chain_bot.d.ts +54 -0
- package/dest/cross_chain_bot.d.ts.map +1 -0
- package/dest/cross_chain_bot.js +134 -0
- package/dest/factory.d.ts +20 -10
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +155 -73
- package/dest/index.d.ts +2 -1
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -0
- package/dest/l1_to_l2_seeding.d.ts +8 -0
- package/dest/l1_to_l2_seeding.d.ts.map +1 -0
- package/dest/l1_to_l2_seeding.js +63 -0
- package/dest/runner.d.ts +3 -3
- package/dest/runner.d.ts.map +1 -1
- package/dest/runner.js +17 -1
- package/dest/store/bot_store.d.ts +30 -5
- package/dest/store/bot_store.d.ts.map +1 -1
- package/dest/store/bot_store.js +37 -6
- package/dest/store/index.d.ts +2 -2
- package/dest/store/index.d.ts.map +1 -1
- package/dest/utils.js +3 -3
- package/package.json +16 -13
- package/src/amm_bot.ts +26 -21
- package/src/base_bot.ts +11 -25
- package/src/bot.ts +10 -8
- package/src/config.ts +39 -10
- package/src/cross_chain_bot.ts +203 -0
- package/src/factory.ts +174 -56
- package/src/index.ts +1 -0
- package/src/l1_to_l2_seeding.ts +79 -0
- package/src/runner.ts +18 -5
- package/src/store/bot_store.ts +60 -5
- package/src/store/index.ts +1 -1
- package/src/utils.ts +3 -3
package/src/factory.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { SchnorrAccountContract } from '@aztec/accounts/schnorr';
|
|
2
1
|
import { getInitialTestAccountsData } from '@aztec/accounts/testing';
|
|
3
2
|
import { AztecAddress } from '@aztec/aztec.js/addresses';
|
|
4
3
|
import {
|
|
@@ -7,27 +6,34 @@ import {
|
|
|
7
6
|
ContractFunctionInteraction,
|
|
8
7
|
type DeployMethod,
|
|
9
8
|
type DeployOptions,
|
|
9
|
+
NO_WAIT,
|
|
10
10
|
} from '@aztec/aztec.js/contracts';
|
|
11
|
-
import { L1FeeJuicePortalManager } from '@aztec/aztec.js/ethereum';
|
|
12
11
|
import type { L2AmountClaim } from '@aztec/aztec.js/ethereum';
|
|
12
|
+
import { L1FeeJuicePortalManager } from '@aztec/aztec.js/ethereum';
|
|
13
13
|
import { FeeJuicePaymentMethodWithClaim } from '@aztec/aztec.js/fee';
|
|
14
14
|
import { deriveKeys } from '@aztec/aztec.js/keys';
|
|
15
15
|
import { createLogger } from '@aztec/aztec.js/log';
|
|
16
16
|
import { waitForL1ToL2MessageReady } from '@aztec/aztec.js/messaging';
|
|
17
|
+
import { waitForTx } from '@aztec/aztec.js/node';
|
|
17
18
|
import { createEthereumChain } from '@aztec/ethereum/chain';
|
|
18
19
|
import { createExtendedL1Client } from '@aztec/ethereum/client';
|
|
20
|
+
import { RollupContract } from '@aztec/ethereum/contracts';
|
|
21
|
+
import type { ExtendedViemWalletClient } from '@aztec/ethereum/types';
|
|
19
22
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
23
|
+
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
20
24
|
import { Timer } from '@aztec/foundation/timer';
|
|
21
25
|
import { AMMContract } from '@aztec/noir-contracts.js/AMM';
|
|
22
26
|
import { PrivateTokenContract } from '@aztec/noir-contracts.js/PrivateToken';
|
|
23
27
|
import { TokenContract } from '@aztec/noir-contracts.js/Token';
|
|
28
|
+
import { TestContract } from '@aztec/noir-test-contracts.js/Test';
|
|
24
29
|
import type { ContractInstanceWithAddress } from '@aztec/stdlib/contract';
|
|
25
30
|
import { GasSettings } from '@aztec/stdlib/gas';
|
|
26
31
|
import type { AztecNode, AztecNodeAdmin } from '@aztec/stdlib/interfaces/client';
|
|
27
32
|
import { deriveSigningKey } from '@aztec/stdlib/keys';
|
|
28
|
-
import {
|
|
33
|
+
import { EmbeddedWallet } from '@aztec/wallets/embedded';
|
|
29
34
|
|
|
30
35
|
import { type BotConfig, SupportedTokenContracts } from './config.js';
|
|
36
|
+
import { seedL1ToL2Message } from './l1_to_l2_seeding.js';
|
|
31
37
|
import type { BotStore } from './store/index.js';
|
|
32
38
|
import { getBalances, getPrivateBalance, isStandardTokenContract } from './utils.js';
|
|
33
39
|
|
|
@@ -39,7 +45,7 @@ export class BotFactory {
|
|
|
39
45
|
|
|
40
46
|
constructor(
|
|
41
47
|
private readonly config: BotConfig,
|
|
42
|
-
private readonly wallet:
|
|
48
|
+
private readonly wallet: EmbeddedWallet,
|
|
43
49
|
private readonly store: BotStore,
|
|
44
50
|
private readonly aztecNode: AztecNode,
|
|
45
51
|
private readonly aztecNodeAdmin?: AztecNodeAdmin,
|
|
@@ -50,21 +56,21 @@ export class BotFactory {
|
|
|
50
56
|
* deploying the token contract, and minting tokens if necessary.
|
|
51
57
|
*/
|
|
52
58
|
public async setup(): Promise<{
|
|
53
|
-
wallet:
|
|
59
|
+
wallet: EmbeddedWallet;
|
|
54
60
|
defaultAccountAddress: AztecAddress;
|
|
55
61
|
token: TokenContract | PrivateTokenContract;
|
|
56
62
|
node: AztecNode;
|
|
57
63
|
recipient: AztecAddress;
|
|
58
64
|
}> {
|
|
59
65
|
const defaultAccountAddress = await this.setupAccount();
|
|
60
|
-
const recipient = (await this.wallet.
|
|
66
|
+
const recipient = (await this.wallet.createSchnorrAccount(Fr.random(), Fr.random())).address;
|
|
61
67
|
const token = await this.setupToken(defaultAccountAddress);
|
|
62
68
|
await this.mintTokens(token, defaultAccountAddress);
|
|
63
69
|
return { wallet: this.wallet, defaultAccountAddress, token, node: this.aztecNode, recipient };
|
|
64
70
|
}
|
|
65
71
|
|
|
66
72
|
public async setupAmm(): Promise<{
|
|
67
|
-
wallet:
|
|
73
|
+
wallet: EmbeddedWallet;
|
|
68
74
|
defaultAccountAddress: AztecAddress;
|
|
69
75
|
amm: AMMContract;
|
|
70
76
|
token0: TokenContract;
|
|
@@ -94,6 +100,89 @@ export class BotFactory {
|
|
|
94
100
|
return { wallet: this.wallet, defaultAccountAddress, amm, token0, token1, node: this.aztecNode };
|
|
95
101
|
}
|
|
96
102
|
|
|
103
|
+
/**
|
|
104
|
+
* Initializes the cross-chain bot by deploying TestContract, creating an L1 client,
|
|
105
|
+
* seeding initial L1→L2 messages, and waiting for the first to be ready.
|
|
106
|
+
*/
|
|
107
|
+
public async setupCrossChain(): Promise<{
|
|
108
|
+
wallet: EmbeddedWallet;
|
|
109
|
+
defaultAccountAddress: AztecAddress;
|
|
110
|
+
contract: TestContract;
|
|
111
|
+
node: AztecNode;
|
|
112
|
+
l1Client: ExtendedViemWalletClient;
|
|
113
|
+
rollupVersion: bigint;
|
|
114
|
+
}> {
|
|
115
|
+
const defaultAccountAddress = await this.setupAccount();
|
|
116
|
+
|
|
117
|
+
// Create L1 client (same pattern as bridgeL1FeeJuice)
|
|
118
|
+
const l1RpcUrls = this.config.l1RpcUrls;
|
|
119
|
+
if (!l1RpcUrls?.length) {
|
|
120
|
+
throw new Error('L1 RPC URLs required for cross-chain bot');
|
|
121
|
+
}
|
|
122
|
+
const mnemonicOrPrivateKey = this.config.l1PrivateKey?.getValue() ?? this.config.l1Mnemonic?.getValue();
|
|
123
|
+
if (!mnemonicOrPrivateKey) {
|
|
124
|
+
throw new Error('L1 mnemonic or private key required for cross-chain bot');
|
|
125
|
+
}
|
|
126
|
+
const { l1ChainId, l1ContractAddresses } = await this.aztecNode.getNodeInfo();
|
|
127
|
+
const chain = createEthereumChain(l1RpcUrls, l1ChainId);
|
|
128
|
+
const l1Client = createExtendedL1Client(chain.rpcUrls, mnemonicOrPrivateKey, chain.chainInfo);
|
|
129
|
+
|
|
130
|
+
// Fetch Rollup version (needed for Inbox L2Actor struct)
|
|
131
|
+
const rollupContract = new RollupContract(l1Client, l1ContractAddresses.rollupAddress.toString());
|
|
132
|
+
const rollupVersion = await rollupContract.getVersion();
|
|
133
|
+
|
|
134
|
+
// Deploy TestContract
|
|
135
|
+
const contract = await this.setupTestContract(defaultAccountAddress);
|
|
136
|
+
|
|
137
|
+
// Recover any pending messages from store (clean up stale ones first)
|
|
138
|
+
await this.store.cleanupOldPendingMessages();
|
|
139
|
+
const pendingMessages = await this.store.getUnconsumedL1ToL2Messages();
|
|
140
|
+
|
|
141
|
+
// Seed initial L1→L2 messages if pipeline is empty
|
|
142
|
+
const seedCount = Math.max(0, this.config.l1ToL2SeedCount - pendingMessages.length);
|
|
143
|
+
for (let i = 0; i < seedCount; i++) {
|
|
144
|
+
await seedL1ToL2Message(
|
|
145
|
+
l1Client,
|
|
146
|
+
EthAddress.fromString(l1ContractAddresses.inboxAddress.toString()),
|
|
147
|
+
contract.address,
|
|
148
|
+
rollupVersion,
|
|
149
|
+
this.store,
|
|
150
|
+
this.log,
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Block until at least one message is ready
|
|
155
|
+
const allMessages = await this.store.getUnconsumedL1ToL2Messages();
|
|
156
|
+
if (allMessages.length > 0) {
|
|
157
|
+
this.log.info(`Waiting for first L1→L2 message to be ready...`);
|
|
158
|
+
const firstMsg = allMessages[0];
|
|
159
|
+
await waitForL1ToL2MessageReady(this.aztecNode, Fr.fromHexString(firstMsg.msgHash), {
|
|
160
|
+
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds,
|
|
161
|
+
});
|
|
162
|
+
this.log.info(`First L1→L2 message is ready`);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
wallet: this.wallet,
|
|
167
|
+
defaultAccountAddress,
|
|
168
|
+
contract,
|
|
169
|
+
node: this.aztecNode,
|
|
170
|
+
l1Client,
|
|
171
|
+
rollupVersion,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private async setupTestContract(deployer: AztecAddress): Promise<TestContract> {
|
|
176
|
+
const deployOpts: DeployOptions = {
|
|
177
|
+
from: deployer,
|
|
178
|
+
contractAddressSalt: this.config.tokenSalt,
|
|
179
|
+
universalDeploy: true,
|
|
180
|
+
};
|
|
181
|
+
const deploy = TestContract.deploy(this.wallet);
|
|
182
|
+
const instance = await this.registerOrDeployContract('TestContract', deploy, deployOpts);
|
|
183
|
+
return TestContract.at(instance.address, this.wallet);
|
|
184
|
+
}
|
|
185
|
+
|
|
97
186
|
/**
|
|
98
187
|
* Checks if the sender account contract is initialized, and initializes it if necessary.
|
|
99
188
|
* @returns The sender wallet.
|
|
@@ -112,12 +201,7 @@ export class BotFactory {
|
|
|
112
201
|
private async setupAccountWithPrivateKey(secret: Fr) {
|
|
113
202
|
const salt = this.config.senderSalt ?? Fr.ONE;
|
|
114
203
|
const signingKey = deriveSigningKey(secret);
|
|
115
|
-
const
|
|
116
|
-
secret,
|
|
117
|
-
salt,
|
|
118
|
-
contract: new SchnorrAccountContract(signingKey!),
|
|
119
|
-
};
|
|
120
|
-
const accountManager = await this.wallet.createAccount(accountData);
|
|
204
|
+
const accountManager = await this.wallet.createSchnorrAccount(secret, salt, signingKey);
|
|
121
205
|
const metadata = await this.wallet.getContractMetadata(accountManager.address);
|
|
122
206
|
if (metadata.isContractInitialized) {
|
|
123
207
|
this.log.info(`Account at ${accountManager.address.toString()} already initialized`);
|
|
@@ -136,10 +220,16 @@ export class BotFactory {
|
|
|
136
220
|
const deployMethod = await accountManager.getDeployMethod();
|
|
137
221
|
const maxFeesPerGas = (await this.aztecNode.getCurrentMinFees()).mul(1 + this.config.minFeePadding);
|
|
138
222
|
const gasSettings = GasSettings.default({ maxFeesPerGas });
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
223
|
+
|
|
224
|
+
await this.withNoMinTxsPerBlock(async () => {
|
|
225
|
+
const { txHash } = await deployMethod.send({
|
|
226
|
+
from: AztecAddress.ZERO,
|
|
227
|
+
fee: { gasSettings, paymentMethod },
|
|
228
|
+
wait: NO_WAIT,
|
|
229
|
+
});
|
|
230
|
+
this.log.info(`Sent tx for account deployment with hash ${txHash.toString()}`);
|
|
231
|
+
return waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
232
|
+
});
|
|
143
233
|
this.log.info(`Account deployed at ${address}`);
|
|
144
234
|
|
|
145
235
|
// Clean up the consumed bridge claim
|
|
@@ -151,12 +241,11 @@ export class BotFactory {
|
|
|
151
241
|
|
|
152
242
|
private async setupTestAccount() {
|
|
153
243
|
const [initialAccountData] = await getInitialTestAccountsData();
|
|
154
|
-
const
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
const accountManager = await this.wallet.createAccount(accountData);
|
|
244
|
+
const accountManager = await this.wallet.createSchnorrAccount(
|
|
245
|
+
initialAccountData.secret,
|
|
246
|
+
initialAccountData.salt,
|
|
247
|
+
initialAccountData.signingKey,
|
|
248
|
+
);
|
|
160
249
|
return accountManager.address;
|
|
161
250
|
}
|
|
162
251
|
|
|
@@ -173,8 +262,11 @@ export class BotFactory {
|
|
|
173
262
|
contractAddressSalt: this.config.tokenSalt,
|
|
174
263
|
universalDeploy: true,
|
|
175
264
|
};
|
|
265
|
+
let token: TokenContract | PrivateTokenContract;
|
|
176
266
|
if (this.config.contract === SupportedTokenContracts.TokenContract) {
|
|
177
267
|
deploy = TokenContract.deploy(this.wallet, sender, 'BotToken', 'BOT', 18);
|
|
268
|
+
tokenInstance = await deploy.getInstance(deployOpts);
|
|
269
|
+
token = TokenContract.at(tokenInstance.address, this.wallet);
|
|
178
270
|
} else if (this.config.contract === SupportedTokenContracts.PrivateTokenContract) {
|
|
179
271
|
// Generate keys for the contract since PrivateToken uses SinglePrivateMutable which requires keys
|
|
180
272
|
const tokenSecretKey = Fr.random();
|
|
@@ -186,7 +278,10 @@ export class BotFactory {
|
|
|
186
278
|
|
|
187
279
|
// Register the contract with the secret key before deployment
|
|
188
280
|
tokenInstance = await deploy.getInstance(deployOpts);
|
|
281
|
+
token = PrivateTokenContract.at(tokenInstance.address, this.wallet);
|
|
189
282
|
await this.wallet.registerContract(tokenInstance, PrivateTokenContract.artifact, tokenSecretKey);
|
|
283
|
+
// The contract constructor initializes private storage vars that need the contract's own nullifier key.
|
|
284
|
+
deployOpts.additionalScopes = [tokenInstance.address];
|
|
190
285
|
} else {
|
|
191
286
|
throw new Error(`Unsupported token contract type: ${this.config.contract}`);
|
|
192
287
|
}
|
|
@@ -195,14 +290,17 @@ export class BotFactory {
|
|
|
195
290
|
const metadata = await this.wallet.getContractMetadata(address);
|
|
196
291
|
if (metadata.isContractPublished) {
|
|
197
292
|
this.log.info(`Token at ${address.toString()} already deployed`);
|
|
198
|
-
|
|
293
|
+
await deploy.register();
|
|
199
294
|
} else {
|
|
200
295
|
this.log.info(`Deploying token contract at ${address.toString()}`);
|
|
201
|
-
const
|
|
202
|
-
const txHash = await sentTx.getTxHash();
|
|
296
|
+
const { txHash } = await deploy.send({ ...deployOpts, wait: NO_WAIT });
|
|
203
297
|
this.log.info(`Sent tx for token setup with hash ${txHash.toString()}`);
|
|
204
|
-
|
|
298
|
+
await this.withNoMinTxsPerBlock(async () => {
|
|
299
|
+
await waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
300
|
+
return token;
|
|
301
|
+
});
|
|
205
302
|
}
|
|
303
|
+
return token;
|
|
206
304
|
}
|
|
207
305
|
|
|
208
306
|
/**
|
|
@@ -210,7 +308,7 @@ export class BotFactory {
|
|
|
210
308
|
* @param wallet - Wallet to deploy the token contract from.
|
|
211
309
|
* @returns The TokenContract instance.
|
|
212
310
|
*/
|
|
213
|
-
private setupTokenContract(
|
|
311
|
+
private async setupTokenContract(
|
|
214
312
|
deployer: AztecAddress,
|
|
215
313
|
contractAddressSalt: Fr,
|
|
216
314
|
name: string,
|
|
@@ -219,7 +317,8 @@ export class BotFactory {
|
|
|
219
317
|
): Promise<TokenContract> {
|
|
220
318
|
const deployOpts: DeployOptions = { from: deployer, contractAddressSalt, universalDeploy: true };
|
|
221
319
|
const deploy = TokenContract.deploy(this.wallet, deployer, name, ticker, decimals);
|
|
222
|
-
|
|
320
|
+
const instance = await this.registerOrDeployContract('Token - ' + name, deploy, deployOpts);
|
|
321
|
+
return TokenContract.at(instance.address, this.wallet);
|
|
223
322
|
}
|
|
224
323
|
|
|
225
324
|
private async setupAmmContract(
|
|
@@ -231,12 +330,14 @@ export class BotFactory {
|
|
|
231
330
|
): Promise<AMMContract> {
|
|
232
331
|
const deployOpts: DeployOptions = { from: deployer, contractAddressSalt, universalDeploy: true };
|
|
233
332
|
const deploy = AMMContract.deploy(this.wallet, token0.address, token1.address, lpToken.address);
|
|
234
|
-
const
|
|
333
|
+
const instance = await this.registerOrDeployContract('AMM', deploy, deployOpts);
|
|
334
|
+
const amm = AMMContract.at(instance.address, this.wallet);
|
|
235
335
|
|
|
236
336
|
this.log.info(`AMM deployed at ${amm.address}`);
|
|
237
|
-
const
|
|
238
|
-
|
|
239
|
-
|
|
337
|
+
const { receipt: minterReceipt } = await lpToken.methods
|
|
338
|
+
.set_minter(amm.address, true)
|
|
339
|
+
.send({ from: deployer, wait: { timeout: this.config.txMinedWaitSeconds } });
|
|
340
|
+
this.log.info(`Set LP token minter to AMM txHash=${minterReceipt.txHash.toString()}`);
|
|
240
341
|
this.log.info(`Liquidity token initialized`);
|
|
241
342
|
|
|
242
343
|
return amm;
|
|
@@ -252,9 +353,18 @@ export class BotFactory {
|
|
|
252
353
|
): Promise<void> {
|
|
253
354
|
const getPrivateBalances = () =>
|
|
254
355
|
Promise.all([
|
|
255
|
-
token0.methods
|
|
256
|
-
|
|
257
|
-
|
|
356
|
+
token0.methods
|
|
357
|
+
.balance_of_private(liquidityProvider)
|
|
358
|
+
.simulate({ from: liquidityProvider })
|
|
359
|
+
.then(r => r.result),
|
|
360
|
+
token1.methods
|
|
361
|
+
.balance_of_private(liquidityProvider)
|
|
362
|
+
.simulate({ from: liquidityProvider })
|
|
363
|
+
.then(r => r.result),
|
|
364
|
+
lpToken.methods
|
|
365
|
+
.balance_of_private(liquidityProvider)
|
|
366
|
+
.simulate({ from: liquidityProvider })
|
|
367
|
+
.then(r => r.result),
|
|
258
368
|
]);
|
|
259
369
|
|
|
260
370
|
const authwitNonce = Fr.random();
|
|
@@ -295,23 +405,22 @@ export class BotFactory {
|
|
|
295
405
|
.getFunctionCall(),
|
|
296
406
|
});
|
|
297
407
|
|
|
298
|
-
const
|
|
408
|
+
const { receipt: mintReceipt } = await new BatchCall(this.wallet, [
|
|
299
409
|
token0.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
|
|
300
410
|
token1.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
|
|
301
|
-
]).send({ from: liquidityProvider });
|
|
411
|
+
]).send({ from: liquidityProvider, wait: { timeout: this.config.txMinedWaitSeconds } });
|
|
302
412
|
|
|
303
|
-
this.log.info(`Sent mint tx: ${
|
|
304
|
-
await mintTx.wait({ timeout: this.config.txMinedWaitSeconds });
|
|
413
|
+
this.log.info(`Sent mint tx: ${mintReceipt.txHash.toString()}`);
|
|
305
414
|
|
|
306
|
-
const
|
|
415
|
+
const { receipt: addLiquidityReceipt } = await amm.methods
|
|
307
416
|
.add_liquidity(amount0Max, amount1Max, amount0Min, amount1Min, authwitNonce)
|
|
308
417
|
.send({
|
|
309
418
|
from: liquidityProvider,
|
|
310
419
|
authWitnesses: [token0Authwit, token1Authwit],
|
|
420
|
+
wait: { timeout: this.config.txMinedWaitSeconds },
|
|
311
421
|
});
|
|
312
422
|
|
|
313
|
-
this.log.info(`Sent tx to add liquidity to the AMM: ${
|
|
314
|
-
await addLiquidityTx.wait({ timeout: this.config.txMinedWaitSeconds });
|
|
423
|
+
this.log.info(`Sent tx to add liquidity to the AMM: ${addLiquidityReceipt.txHash.toString()}`);
|
|
315
424
|
this.log.info(`Liquidity added`);
|
|
316
425
|
|
|
317
426
|
const [newT0Bal, newT1Bal, newLPBal] = await getPrivateBalances();
|
|
@@ -324,19 +433,22 @@ export class BotFactory {
|
|
|
324
433
|
name: string,
|
|
325
434
|
deploy: DeployMethod<T>,
|
|
326
435
|
deployOpts: DeployOptions,
|
|
327
|
-
): Promise<
|
|
328
|
-
const
|
|
436
|
+
): Promise<ContractInstanceWithAddress> {
|
|
437
|
+
const instance = await deploy.getInstance(deployOpts);
|
|
438
|
+
const address = instance.address;
|
|
329
439
|
const metadata = await this.wallet.getContractMetadata(address);
|
|
330
440
|
if (metadata.isContractPublished) {
|
|
331
441
|
this.log.info(`Contract ${name} at ${address.toString()} already deployed`);
|
|
332
|
-
|
|
442
|
+
await deploy.register();
|
|
333
443
|
} else {
|
|
334
444
|
this.log.info(`Deploying contract ${name} at ${address.toString()}`);
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
445
|
+
await this.withNoMinTxsPerBlock(async () => {
|
|
446
|
+
const { txHash } = await deploy.send({ ...deployOpts, wait: NO_WAIT });
|
|
447
|
+
this.log.info(`Sent contract ${name} setup tx with hash ${txHash.toString()}`);
|
|
448
|
+
return waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
449
|
+
});
|
|
339
450
|
}
|
|
451
|
+
return instance;
|
|
340
452
|
}
|
|
341
453
|
|
|
342
454
|
/**
|
|
@@ -372,10 +484,18 @@ export class BotFactory {
|
|
|
372
484
|
this.log.info(`Skipping minting as ${minter.toString()} has enough tokens`);
|
|
373
485
|
return;
|
|
374
486
|
}
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
await this.withNoMinTxsPerBlock(() =>
|
|
487
|
+
|
|
488
|
+
// PrivateToken's mint accesses contract-level private storage vars (admin, total_supply).
|
|
489
|
+
const additionalScopes = isStandardToken ? undefined : [token.address];
|
|
490
|
+
await this.withNoMinTxsPerBlock(async () => {
|
|
491
|
+
const { txHash } = await new BatchCall(token.wallet, calls).send({
|
|
492
|
+
from: minter,
|
|
493
|
+
additionalScopes,
|
|
494
|
+
wait: NO_WAIT,
|
|
495
|
+
});
|
|
496
|
+
this.log.info(`Sent token mint tx with hash ${txHash.toString()}`);
|
|
497
|
+
return waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
498
|
+
});
|
|
379
499
|
}
|
|
380
500
|
|
|
381
501
|
/**
|
|
@@ -395,7 +515,6 @@ export class BotFactory {
|
|
|
395
515
|
await this.withNoMinTxsPerBlock(() =>
|
|
396
516
|
waitForL1ToL2MessageReady(this.aztecNode, messageHash, {
|
|
397
517
|
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds,
|
|
398
|
-
forPublicConsumption: false,
|
|
399
518
|
}),
|
|
400
519
|
);
|
|
401
520
|
return existingClaim.claim;
|
|
@@ -434,7 +553,6 @@ export class BotFactory {
|
|
|
434
553
|
await this.withNoMinTxsPerBlock(() =>
|
|
435
554
|
waitForL1ToL2MessageReady(this.aztecNode, Fr.fromHexString(claim.messageHash), {
|
|
436
555
|
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds,
|
|
437
|
-
forPublicConsumption: false,
|
|
438
556
|
}),
|
|
439
557
|
);
|
|
440
558
|
|
package/src/index.ts
CHANGED
|
@@ -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
|
@@ -4,12 +4,13 @@ import { omit } from '@aztec/foundation/collection';
|
|
|
4
4
|
import { RunningPromise } from '@aztec/foundation/running-promise';
|
|
5
5
|
import type { AztecNodeAdmin } from '@aztec/stdlib/interfaces/client';
|
|
6
6
|
import { type TelemetryClient, type Traceable, type Tracer, trackSpan } from '@aztec/telemetry-client';
|
|
7
|
-
import type {
|
|
7
|
+
import type { EmbeddedWallet } from '@aztec/wallets/embedded';
|
|
8
8
|
|
|
9
9
|
import { AmmBot } from './amm_bot.js';
|
|
10
10
|
import type { BaseBot } from './base_bot.js';
|
|
11
11
|
import { Bot } from './bot.js';
|
|
12
12
|
import type { BotConfig } from './config.js';
|
|
13
|
+
import { CrossChainBot } from './cross_chain_bot.js';
|
|
13
14
|
import type { BotInfo, BotRunnerApi } from './interface.js';
|
|
14
15
|
import { BotStore } from './store/index.js';
|
|
15
16
|
|
|
@@ -24,7 +25,7 @@ export class BotRunner implements BotRunnerApi, Traceable {
|
|
|
24
25
|
|
|
25
26
|
public constructor(
|
|
26
27
|
private config: BotConfig,
|
|
27
|
-
private readonly wallet:
|
|
28
|
+
private readonly wallet: EmbeddedWallet,
|
|
28
29
|
private readonly aztecNode: AztecNode,
|
|
29
30
|
private readonly telemetry: TelemetryClient,
|
|
30
31
|
private readonly aztecNodeAdmin: AztecNodeAdmin | undefined,
|
|
@@ -146,9 +147,21 @@ export class BotRunner implements BotRunnerApi, Traceable {
|
|
|
146
147
|
|
|
147
148
|
async #createBot() {
|
|
148
149
|
try {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
150
|
+
switch (this.config.botMode) {
|
|
151
|
+
case 'crosschain':
|
|
152
|
+
this.bot = CrossChainBot.create(this.config, this.wallet, this.aztecNode, this.aztecNodeAdmin, this.store);
|
|
153
|
+
break;
|
|
154
|
+
case 'amm':
|
|
155
|
+
this.bot = AmmBot.create(this.config, this.wallet, this.aztecNode, this.aztecNodeAdmin, this.store);
|
|
156
|
+
break;
|
|
157
|
+
case 'transfer':
|
|
158
|
+
this.bot = Bot.create(this.config, this.wallet, this.aztecNode, this.aztecNodeAdmin, this.store);
|
|
159
|
+
break;
|
|
160
|
+
default: {
|
|
161
|
+
const _exhaustive: never = this.config.botMode;
|
|
162
|
+
throw new Error(`Unsupported bot mode: [${_exhaustive}]`);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
152
165
|
await this.bot;
|
|
153
166
|
} catch (err) {
|
|
154
167
|
this.log.error(`Error setting up bot: ${err}`);
|
package/src/store/bot_store.ts
CHANGED
|
@@ -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:
|
|
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 =
|
|
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
|
-
|
|
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');
|
package/src/store/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { BotStore, type BridgeClaimData } from './bot_store.js';
|
|
1
|
+
export { BotStore, type BridgeClaimData, type PendingL1ToL2Message } from './bot_store.js';
|