@aztec/bot 0.0.1-commit.9b94fc1 → 0.0.1-commit.9ee6fcc6
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 +14 -15
- 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 +42 -27
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +44 -17
- 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 +261 -79
- 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 +429 -31
- package/dest/store/bot_store.d.ts +31 -6
- package/dest/store/bot_store.d.ts.map +1 -1
- package/dest/store/bot_store.js +38 -7
- 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 +19 -16
- package/src/amm_bot.ts +26 -21
- package/src/base_bot.ts +13 -27
- package/src/bot.ts +10 -8
- package/src/config.ts +90 -60
- package/src/cross_chain_bot.ts +203 -0
- package/src/factory.ts +251 -75
- 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 +61 -6
- 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,24 +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
|
+
import { deriveKeys } from '@aztec/aztec.js/keys';
|
|
14
15
|
import { createLogger } from '@aztec/aztec.js/log';
|
|
15
16
|
import { waitForL1ToL2MessageReady } from '@aztec/aztec.js/messaging';
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
17
|
+
import { waitForTx } from '@aztec/aztec.js/node';
|
|
18
|
+
import { createEthereumChain } from '@aztec/ethereum/chain';
|
|
19
|
+
import { createExtendedL1Client } from '@aztec/ethereum/client';
|
|
20
|
+
import { RollupContract } from '@aztec/ethereum/contracts';
|
|
21
|
+
import type { ExtendedViemWalletClient } from '@aztec/ethereum/types';
|
|
22
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
23
|
+
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
18
24
|
import { Timer } from '@aztec/foundation/timer';
|
|
19
25
|
import { AMMContract } from '@aztec/noir-contracts.js/AMM';
|
|
20
26
|
import { PrivateTokenContract } from '@aztec/noir-contracts.js/PrivateToken';
|
|
21
27
|
import { TokenContract } from '@aztec/noir-contracts.js/Token';
|
|
22
|
-
import {
|
|
28
|
+
import { TestContract } from '@aztec/noir-test-contracts.js/Test';
|
|
29
|
+
import type { ContractInstanceWithAddress } from '@aztec/stdlib/contract';
|
|
30
|
+
import { GasFees, GasSettings } from '@aztec/stdlib/gas';
|
|
23
31
|
import type { AztecNode, AztecNodeAdmin } from '@aztec/stdlib/interfaces/client';
|
|
24
32
|
import { deriveSigningKey } from '@aztec/stdlib/keys';
|
|
25
|
-
import {
|
|
33
|
+
import { EmbeddedWallet } from '@aztec/wallets/embedded';
|
|
26
34
|
|
|
27
35
|
import { type BotConfig, SupportedTokenContracts } from './config.js';
|
|
36
|
+
import { seedL1ToL2Message } from './l1_to_l2_seeding.js';
|
|
28
37
|
import type { BotStore } from './store/index.js';
|
|
29
38
|
import { getBalances, getPrivateBalance, isStandardTokenContract } from './utils.js';
|
|
30
39
|
|
|
@@ -36,32 +45,36 @@ export class BotFactory {
|
|
|
36
45
|
|
|
37
46
|
constructor(
|
|
38
47
|
private readonly config: BotConfig,
|
|
39
|
-
private readonly wallet:
|
|
48
|
+
private readonly wallet: EmbeddedWallet,
|
|
40
49
|
private readonly store: BotStore,
|
|
41
50
|
private readonly aztecNode: AztecNode,
|
|
42
51
|
private readonly aztecNodeAdmin?: AztecNodeAdmin,
|
|
43
|
-
) {
|
|
52
|
+
) {
|
|
53
|
+
// Set fee padding on the wallet so that all transactions during setup
|
|
54
|
+
// (token deploy, minting, etc.) use the configured padding, not the default.
|
|
55
|
+
this.wallet.setMinFeePadding(config.minFeePadding);
|
|
56
|
+
}
|
|
44
57
|
|
|
45
58
|
/**
|
|
46
59
|
* Initializes a new bot by setting up the sender account, registering the recipient,
|
|
47
60
|
* deploying the token contract, and minting tokens if necessary.
|
|
48
61
|
*/
|
|
49
62
|
public async setup(): Promise<{
|
|
50
|
-
wallet:
|
|
63
|
+
wallet: EmbeddedWallet;
|
|
51
64
|
defaultAccountAddress: AztecAddress;
|
|
52
65
|
token: TokenContract | PrivateTokenContract;
|
|
53
66
|
node: AztecNode;
|
|
54
67
|
recipient: AztecAddress;
|
|
55
68
|
}> {
|
|
56
|
-
const recipient = (await this.wallet.createAccount()).address;
|
|
57
69
|
const defaultAccountAddress = await this.setupAccount();
|
|
70
|
+
const recipient = (await this.wallet.createSchnorrAccount(Fr.random(), Fr.random())).address;
|
|
58
71
|
const token = await this.setupToken(defaultAccountAddress);
|
|
59
72
|
await this.mintTokens(token, defaultAccountAddress);
|
|
60
73
|
return { wallet: this.wallet, defaultAccountAddress, token, node: this.aztecNode, recipient };
|
|
61
74
|
}
|
|
62
75
|
|
|
63
76
|
public async setupAmm(): Promise<{
|
|
64
|
-
wallet:
|
|
77
|
+
wallet: EmbeddedWallet;
|
|
65
78
|
defaultAccountAddress: AztecAddress;
|
|
66
79
|
amm: AMMContract;
|
|
67
80
|
token0: TokenContract;
|
|
@@ -91,6 +104,89 @@ export class BotFactory {
|
|
|
91
104
|
return { wallet: this.wallet, defaultAccountAddress, amm, token0, token1, node: this.aztecNode };
|
|
92
105
|
}
|
|
93
106
|
|
|
107
|
+
/**
|
|
108
|
+
* Initializes the cross-chain bot by deploying TestContract, creating an L1 client,
|
|
109
|
+
* seeding initial L1→L2 messages, and waiting for the first to be ready.
|
|
110
|
+
*/
|
|
111
|
+
public async setupCrossChain(): Promise<{
|
|
112
|
+
wallet: EmbeddedWallet;
|
|
113
|
+
defaultAccountAddress: AztecAddress;
|
|
114
|
+
contract: TestContract;
|
|
115
|
+
node: AztecNode;
|
|
116
|
+
l1Client: ExtendedViemWalletClient;
|
|
117
|
+
rollupVersion: bigint;
|
|
118
|
+
}> {
|
|
119
|
+
const defaultAccountAddress = await this.setupAccount();
|
|
120
|
+
|
|
121
|
+
// Create L1 client (same pattern as bridgeL1FeeJuice)
|
|
122
|
+
const l1RpcUrls = this.config.l1RpcUrls;
|
|
123
|
+
if (!l1RpcUrls?.length) {
|
|
124
|
+
throw new Error('L1 RPC URLs required for cross-chain bot');
|
|
125
|
+
}
|
|
126
|
+
const mnemonicOrPrivateKey = this.config.l1PrivateKey?.getValue() ?? this.config.l1Mnemonic?.getValue();
|
|
127
|
+
if (!mnemonicOrPrivateKey) {
|
|
128
|
+
throw new Error('L1 mnemonic or private key required for cross-chain bot');
|
|
129
|
+
}
|
|
130
|
+
const { l1ChainId, l1ContractAddresses } = await this.aztecNode.getNodeInfo();
|
|
131
|
+
const chain = createEthereumChain(l1RpcUrls, l1ChainId);
|
|
132
|
+
const l1Client = createExtendedL1Client(chain.rpcUrls, mnemonicOrPrivateKey, chain.chainInfo);
|
|
133
|
+
|
|
134
|
+
// Fetch Rollup version (needed for Inbox L2Actor struct)
|
|
135
|
+
const rollupContract = new RollupContract(l1Client, l1ContractAddresses.rollupAddress.toString());
|
|
136
|
+
const rollupVersion = await rollupContract.getVersion();
|
|
137
|
+
|
|
138
|
+
// Deploy TestContract
|
|
139
|
+
const contract = await this.setupTestContract(defaultAccountAddress);
|
|
140
|
+
|
|
141
|
+
// Recover any pending messages from store (clean up stale ones first)
|
|
142
|
+
await this.store.cleanupOldPendingMessages();
|
|
143
|
+
const pendingMessages = await this.store.getUnconsumedL1ToL2Messages();
|
|
144
|
+
|
|
145
|
+
// Seed initial L1→L2 messages if pipeline is empty
|
|
146
|
+
const seedCount = Math.max(0, this.config.l1ToL2SeedCount - pendingMessages.length);
|
|
147
|
+
for (let i = 0; i < seedCount; i++) {
|
|
148
|
+
await seedL1ToL2Message(
|
|
149
|
+
l1Client,
|
|
150
|
+
EthAddress.fromString(l1ContractAddresses.inboxAddress.toString()),
|
|
151
|
+
contract.address,
|
|
152
|
+
rollupVersion,
|
|
153
|
+
this.store,
|
|
154
|
+
this.log,
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Block until at least one message is ready
|
|
159
|
+
const allMessages = await this.store.getUnconsumedL1ToL2Messages();
|
|
160
|
+
if (allMessages.length > 0) {
|
|
161
|
+
this.log.info(`Waiting for first L1→L2 message to be ready...`);
|
|
162
|
+
const firstMsg = allMessages[0];
|
|
163
|
+
await waitForL1ToL2MessageReady(this.aztecNode, Fr.fromHexString(firstMsg.msgHash), {
|
|
164
|
+
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds,
|
|
165
|
+
});
|
|
166
|
+
this.log.info(`First L1→L2 message is ready`);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return {
|
|
170
|
+
wallet: this.wallet,
|
|
171
|
+
defaultAccountAddress,
|
|
172
|
+
contract,
|
|
173
|
+
node: this.aztecNode,
|
|
174
|
+
l1Client,
|
|
175
|
+
rollupVersion,
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
private async setupTestContract(deployer: AztecAddress): Promise<TestContract> {
|
|
180
|
+
const deployOpts: DeployOptions = {
|
|
181
|
+
from: deployer,
|
|
182
|
+
contractAddressSalt: this.config.tokenSalt,
|
|
183
|
+
universalDeploy: true,
|
|
184
|
+
};
|
|
185
|
+
const deploy = TestContract.deploy(this.wallet);
|
|
186
|
+
const instance = await this.registerOrDeployContract('TestContract', deploy, deployOpts);
|
|
187
|
+
return TestContract.at(instance.address, this.wallet);
|
|
188
|
+
}
|
|
189
|
+
|
|
94
190
|
/**
|
|
95
191
|
* Checks if the sender account contract is initialized, and initializes it if necessary.
|
|
96
192
|
* @returns The sender wallet.
|
|
@@ -109,14 +205,9 @@ export class BotFactory {
|
|
|
109
205
|
private async setupAccountWithPrivateKey(secret: Fr) {
|
|
110
206
|
const salt = this.config.senderSalt ?? Fr.ONE;
|
|
111
207
|
const signingKey = deriveSigningKey(secret);
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
contract: new SchnorrAccountContract(signingKey!),
|
|
116
|
-
};
|
|
117
|
-
const accountManager = await this.wallet.createAccount(accountData);
|
|
118
|
-
const isInit = (await this.wallet.getContractMetadata(accountManager.address)).isContractInitialized;
|
|
119
|
-
if (isInit) {
|
|
208
|
+
const accountManager = await this.wallet.createSchnorrAccount(secret, salt, signingKey);
|
|
209
|
+
const metadata = await this.wallet.getContractMetadata(accountManager.address);
|
|
210
|
+
if (metadata.isContractInitialized) {
|
|
120
211
|
this.log.info(`Account at ${accountManager.address.toString()} already initialized`);
|
|
121
212
|
const timer = new Timer();
|
|
122
213
|
const address = accountManager.address;
|
|
@@ -131,12 +222,23 @@ export class BotFactory {
|
|
|
131
222
|
|
|
132
223
|
const paymentMethod = new FeeJuicePaymentMethodWithClaim(accountManager.address, claim);
|
|
133
224
|
const deployMethod = await accountManager.getDeployMethod();
|
|
134
|
-
const maxFeesPerGas = (await this.aztecNode.
|
|
135
|
-
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
225
|
+
const maxFeesPerGas = (await this.aztecNode.getCurrentMinFees()).mul(1 + this.config.minFeePadding);
|
|
226
|
+
|
|
227
|
+
const { estimatedGas } = await deployMethod.simulate({
|
|
228
|
+
from: AztecAddress.ZERO,
|
|
229
|
+
fee: { estimateGas: true, paymentMethod },
|
|
230
|
+
});
|
|
231
|
+
const gasSettings = GasSettings.from({ ...estimatedGas!, maxFeesPerGas, maxPriorityFeesPerGas: GasFees.empty() });
|
|
232
|
+
|
|
233
|
+
await this.withNoMinTxsPerBlock(async () => {
|
|
234
|
+
const { txHash } = await deployMethod.send({
|
|
235
|
+
from: AztecAddress.ZERO,
|
|
236
|
+
fee: { gasSettings, paymentMethod },
|
|
237
|
+
wait: NO_WAIT,
|
|
238
|
+
});
|
|
239
|
+
this.log.info(`Sent tx for account deployment with hash ${txHash.toString()}`, { gasSettings });
|
|
240
|
+
return waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
241
|
+
});
|
|
140
242
|
this.log.info(`Account deployed at ${address}`);
|
|
141
243
|
|
|
142
244
|
// Clean up the consumed bridge claim
|
|
@@ -148,12 +250,11 @@ export class BotFactory {
|
|
|
148
250
|
|
|
149
251
|
private async setupTestAccount() {
|
|
150
252
|
const [initialAccountData] = await getInitialTestAccountsData();
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
const accountManager = await this.wallet.createAccount(accountData);
|
|
253
|
+
const accountManager = await this.wallet.createSchnorrAccount(
|
|
254
|
+
initialAccountData.secret,
|
|
255
|
+
initialAccountData.salt,
|
|
256
|
+
initialAccountData.signingKey,
|
|
257
|
+
);
|
|
157
258
|
return accountManager.address;
|
|
158
259
|
}
|
|
159
260
|
|
|
@@ -164,33 +265,52 @@ export class BotFactory {
|
|
|
164
265
|
*/
|
|
165
266
|
private async setupToken(sender: AztecAddress): Promise<TokenContract | PrivateTokenContract> {
|
|
166
267
|
let deploy: DeployMethod<TokenContract | PrivateTokenContract>;
|
|
268
|
+
let tokenInstance: ContractInstanceWithAddress | undefined;
|
|
167
269
|
const deployOpts: DeployOptions = {
|
|
168
270
|
from: sender,
|
|
169
271
|
contractAddressSalt: this.config.tokenSalt,
|
|
170
272
|
universalDeploy: true,
|
|
171
273
|
};
|
|
274
|
+
let token: TokenContract | PrivateTokenContract;
|
|
172
275
|
if (this.config.contract === SupportedTokenContracts.TokenContract) {
|
|
173
276
|
deploy = TokenContract.deploy(this.wallet, sender, 'BotToken', 'BOT', 18);
|
|
277
|
+
tokenInstance = await deploy.getInstance(deployOpts);
|
|
278
|
+
token = TokenContract.at(tokenInstance.address, this.wallet);
|
|
174
279
|
} else if (this.config.contract === SupportedTokenContracts.PrivateTokenContract) {
|
|
175
|
-
|
|
280
|
+
// Generate keys for the contract since PrivateToken uses SinglePrivateMutable which requires keys
|
|
281
|
+
const tokenSecretKey = Fr.random();
|
|
282
|
+
const tokenPublicKeys = (await deriveKeys(tokenSecretKey)).publicKeys;
|
|
283
|
+
deploy = PrivateTokenContract.deployWithPublicKeys(tokenPublicKeys, this.wallet, MINT_BALANCE, sender);
|
|
176
284
|
deployOpts.skipInstancePublication = true;
|
|
177
285
|
deployOpts.skipClassPublication = true;
|
|
178
286
|
deployOpts.skipInitialization = false;
|
|
287
|
+
|
|
288
|
+
// Register the contract with the secret key before deployment
|
|
289
|
+
tokenInstance = await deploy.getInstance(deployOpts);
|
|
290
|
+
token = PrivateTokenContract.at(tokenInstance.address, this.wallet);
|
|
291
|
+
await this.wallet.registerContract(tokenInstance, PrivateTokenContract.artifact, tokenSecretKey);
|
|
292
|
+
// The contract constructor initializes private storage vars that need the contract's own nullifier key.
|
|
293
|
+
deployOpts.additionalScopes = [tokenInstance.address];
|
|
179
294
|
} else {
|
|
180
295
|
throw new Error(`Unsupported token contract type: ${this.config.contract}`);
|
|
181
296
|
}
|
|
182
297
|
|
|
183
|
-
const address = (await deploy.getInstance(deployOpts)).address;
|
|
184
|
-
|
|
298
|
+
const address = tokenInstance?.address ?? (await deploy.getInstance(deployOpts)).address;
|
|
299
|
+
const metadata = await this.wallet.getContractMetadata(address);
|
|
300
|
+
if (metadata.isContractPublished) {
|
|
185
301
|
this.log.info(`Token at ${address.toString()} already deployed`);
|
|
186
|
-
|
|
302
|
+
await deploy.register();
|
|
187
303
|
} else {
|
|
188
304
|
this.log.info(`Deploying token contract at ${address.toString()}`);
|
|
189
|
-
const
|
|
190
|
-
const txHash = await
|
|
191
|
-
this.log.info(`Sent tx for token setup with hash ${txHash.toString()}
|
|
192
|
-
|
|
305
|
+
const { estimatedGas } = await deploy.simulate({ ...deployOpts, fee: { estimateGas: true } });
|
|
306
|
+
const { txHash } = await deploy.send({ ...deployOpts, fee: { gasSettings: estimatedGas }, wait: NO_WAIT });
|
|
307
|
+
this.log.info(`Sent tx for token setup with hash ${txHash.toString()}`, { estimatedGas });
|
|
308
|
+
await this.withNoMinTxsPerBlock(async () => {
|
|
309
|
+
await waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
310
|
+
return token;
|
|
311
|
+
});
|
|
193
312
|
}
|
|
313
|
+
return token;
|
|
194
314
|
}
|
|
195
315
|
|
|
196
316
|
/**
|
|
@@ -198,7 +318,7 @@ export class BotFactory {
|
|
|
198
318
|
* @param wallet - Wallet to deploy the token contract from.
|
|
199
319
|
* @returns The TokenContract instance.
|
|
200
320
|
*/
|
|
201
|
-
private setupTokenContract(
|
|
321
|
+
private async setupTokenContract(
|
|
202
322
|
deployer: AztecAddress,
|
|
203
323
|
contractAddressSalt: Fr,
|
|
204
324
|
name: string,
|
|
@@ -207,7 +327,8 @@ export class BotFactory {
|
|
|
207
327
|
): Promise<TokenContract> {
|
|
208
328
|
const deployOpts: DeployOptions = { from: deployer, contractAddressSalt, universalDeploy: true };
|
|
209
329
|
const deploy = TokenContract.deploy(this.wallet, deployer, name, ticker, decimals);
|
|
210
|
-
|
|
330
|
+
const instance = await this.registerOrDeployContract('Token - ' + name, deploy, deployOpts);
|
|
331
|
+
return TokenContract.at(instance.address, this.wallet);
|
|
211
332
|
}
|
|
212
333
|
|
|
213
334
|
private async setupAmmContract(
|
|
@@ -219,12 +340,23 @@ export class BotFactory {
|
|
|
219
340
|
): Promise<AMMContract> {
|
|
220
341
|
const deployOpts: DeployOptions = { from: deployer, contractAddressSalt, universalDeploy: true };
|
|
221
342
|
const deploy = AMMContract.deploy(this.wallet, token0.address, token1.address, lpToken.address);
|
|
222
|
-
const
|
|
343
|
+
const instance = await this.registerOrDeployContract('AMM', deploy, deployOpts);
|
|
344
|
+
const amm = AMMContract.at(instance.address, this.wallet);
|
|
223
345
|
|
|
224
346
|
this.log.info(`AMM deployed at ${amm.address}`);
|
|
225
|
-
const
|
|
226
|
-
|
|
227
|
-
|
|
347
|
+
const setMinterInteraction = lpToken.methods.set_minter(amm.address, true);
|
|
348
|
+
const { estimatedGas: setMinterGas } = await setMinterInteraction.simulate({
|
|
349
|
+
from: deployer,
|
|
350
|
+
fee: { estimateGas: true },
|
|
351
|
+
});
|
|
352
|
+
const { receipt: minterReceipt } = await setMinterInteraction.send({
|
|
353
|
+
from: deployer,
|
|
354
|
+
fee: { gasSettings: setMinterGas },
|
|
355
|
+
wait: { timeout: this.config.txMinedWaitSeconds },
|
|
356
|
+
});
|
|
357
|
+
this.log.info(`Set LP token minter to AMM txHash=${minterReceipt.txHash.toString()}`, {
|
|
358
|
+
estimatedGas: setMinterGas,
|
|
359
|
+
});
|
|
228
360
|
this.log.info(`Liquidity token initialized`);
|
|
229
361
|
|
|
230
362
|
return amm;
|
|
@@ -240,9 +372,18 @@ export class BotFactory {
|
|
|
240
372
|
): Promise<void> {
|
|
241
373
|
const getPrivateBalances = () =>
|
|
242
374
|
Promise.all([
|
|
243
|
-
token0.methods
|
|
244
|
-
|
|
245
|
-
|
|
375
|
+
token0.methods
|
|
376
|
+
.balance_of_private(liquidityProvider)
|
|
377
|
+
.simulate({ from: liquidityProvider })
|
|
378
|
+
.then(r => r.result),
|
|
379
|
+
token1.methods
|
|
380
|
+
.balance_of_private(liquidityProvider)
|
|
381
|
+
.simulate({ from: liquidityProvider })
|
|
382
|
+
.then(r => r.result),
|
|
383
|
+
lpToken.methods
|
|
384
|
+
.balance_of_private(liquidityProvider)
|
|
385
|
+
.simulate({ from: liquidityProvider })
|
|
386
|
+
.then(r => r.result),
|
|
246
387
|
]);
|
|
247
388
|
|
|
248
389
|
const authwitNonce = Fr.random();
|
|
@@ -283,23 +424,44 @@ export class BotFactory {
|
|
|
283
424
|
.getFunctionCall(),
|
|
284
425
|
});
|
|
285
426
|
|
|
286
|
-
const
|
|
427
|
+
const mintBatch = new BatchCall(this.wallet, [
|
|
287
428
|
token0.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
|
|
288
429
|
token1.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
|
|
289
|
-
])
|
|
430
|
+
]);
|
|
431
|
+
const { estimatedGas: mintGas } = await mintBatch.simulate({
|
|
432
|
+
from: liquidityProvider,
|
|
433
|
+
fee: { estimateGas: true },
|
|
434
|
+
});
|
|
435
|
+
const { receipt: mintReceipt } = await mintBatch.send({
|
|
436
|
+
from: liquidityProvider,
|
|
437
|
+
fee: { gasSettings: mintGas },
|
|
438
|
+
wait: { timeout: this.config.txMinedWaitSeconds },
|
|
439
|
+
});
|
|
290
440
|
|
|
291
|
-
this.log.info(`Sent mint tx: ${
|
|
292
|
-
await mintTx.wait({ timeout: this.config.txMinedWaitSeconds });
|
|
441
|
+
this.log.info(`Sent mint tx: ${mintReceipt.txHash.toString()}`, { estimatedGas: mintGas });
|
|
293
442
|
|
|
294
|
-
const
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
443
|
+
const addLiquidityInteraction = amm.methods.add_liquidity(
|
|
444
|
+
amount0Max,
|
|
445
|
+
amount1Max,
|
|
446
|
+
amount0Min,
|
|
447
|
+
amount1Min,
|
|
448
|
+
authwitNonce,
|
|
449
|
+
);
|
|
450
|
+
const { estimatedGas: addLiquidityGas } = await addLiquidityInteraction.simulate({
|
|
451
|
+
from: liquidityProvider,
|
|
452
|
+
fee: { estimateGas: true },
|
|
453
|
+
authWitnesses: [token0Authwit, token1Authwit],
|
|
454
|
+
});
|
|
455
|
+
const { receipt: addLiquidityReceipt } = await addLiquidityInteraction.send({
|
|
456
|
+
from: liquidityProvider,
|
|
457
|
+
fee: { gasSettings: addLiquidityGas },
|
|
458
|
+
authWitnesses: [token0Authwit, token1Authwit],
|
|
459
|
+
wait: { timeout: this.config.txMinedWaitSeconds },
|
|
460
|
+
});
|
|
300
461
|
|
|
301
|
-
this.log.info(`Sent tx to add liquidity to the AMM: ${
|
|
302
|
-
|
|
462
|
+
this.log.info(`Sent tx to add liquidity to the AMM: ${addLiquidityReceipt.txHash.toString()}`, {
|
|
463
|
+
estimatedGas: addLiquidityGas,
|
|
464
|
+
});
|
|
303
465
|
this.log.info(`Liquidity added`);
|
|
304
466
|
|
|
305
467
|
const [newT0Bal, newT1Bal, newLPBal] = await getPrivateBalances();
|
|
@@ -312,18 +474,23 @@ export class BotFactory {
|
|
|
312
474
|
name: string,
|
|
313
475
|
deploy: DeployMethod<T>,
|
|
314
476
|
deployOpts: DeployOptions,
|
|
315
|
-
): Promise<
|
|
316
|
-
const
|
|
317
|
-
|
|
477
|
+
): Promise<ContractInstanceWithAddress> {
|
|
478
|
+
const instance = await deploy.getInstance(deployOpts);
|
|
479
|
+
const address = instance.address;
|
|
480
|
+
const metadata = await this.wallet.getContractMetadata(address);
|
|
481
|
+
if (metadata.isContractPublished) {
|
|
318
482
|
this.log.info(`Contract ${name} at ${address.toString()} already deployed`);
|
|
319
|
-
|
|
483
|
+
await deploy.register();
|
|
320
484
|
} else {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
485
|
+
const { estimatedGas } = await deploy.simulate({ ...deployOpts, fee: { estimateGas: true } });
|
|
486
|
+
this.log.info(`Deploying contract ${name} at ${address.toString()}`, { estimatedGas });
|
|
487
|
+
await this.withNoMinTxsPerBlock(async () => {
|
|
488
|
+
const { txHash } = await deploy.send({ ...deployOpts, fee: { gasSettings: estimatedGas }, wait: NO_WAIT });
|
|
489
|
+
this.log.info(`Sent contract ${name} setup tx with hash ${txHash.toString()}`);
|
|
490
|
+
return waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
491
|
+
});
|
|
326
492
|
}
|
|
493
|
+
return instance;
|
|
327
494
|
}
|
|
328
495
|
|
|
329
496
|
/**
|
|
@@ -359,10 +526,21 @@ export class BotFactory {
|
|
|
359
526
|
this.log.info(`Skipping minting as ${minter.toString()} has enough tokens`);
|
|
360
527
|
return;
|
|
361
528
|
}
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
529
|
+
|
|
530
|
+
// PrivateToken's mint accesses contract-level private storage vars (admin, total_supply).
|
|
531
|
+
const additionalScopes = isStandardToken ? undefined : [token.address];
|
|
532
|
+
const mintBatch = new BatchCall(token.wallet, calls);
|
|
533
|
+
const { estimatedGas } = await mintBatch.simulate({ from: minter, fee: { estimateGas: true }, additionalScopes });
|
|
534
|
+
await this.withNoMinTxsPerBlock(async () => {
|
|
535
|
+
const { txHash } = await mintBatch.send({
|
|
536
|
+
from: minter,
|
|
537
|
+
additionalScopes,
|
|
538
|
+
fee: { gasSettings: estimatedGas },
|
|
539
|
+
wait: NO_WAIT,
|
|
540
|
+
});
|
|
541
|
+
this.log.info(`Sent token mint tx with hash ${txHash.toString()}`, { estimatedGas });
|
|
542
|
+
return waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
543
|
+
});
|
|
366
544
|
}
|
|
367
545
|
|
|
368
546
|
/**
|
|
@@ -382,7 +560,6 @@ export class BotFactory {
|
|
|
382
560
|
await this.withNoMinTxsPerBlock(() =>
|
|
383
561
|
waitForL1ToL2MessageReady(this.aztecNode, messageHash, {
|
|
384
562
|
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds,
|
|
385
|
-
forPublicConsumption: false,
|
|
386
563
|
}),
|
|
387
564
|
);
|
|
388
565
|
return existingClaim.claim;
|
|
@@ -421,7 +598,6 @@ export class BotFactory {
|
|
|
421
598
|
await this.withNoMinTxsPerBlock(() =>
|
|
422
599
|
waitForL1ToL2MessageReady(this.aztecNode, Fr.fromHexString(claim.messageHash), {
|
|
423
600
|
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds,
|
|
424
|
-
forPublicConsumption: false,
|
|
425
601
|
}),
|
|
426
602
|
);
|
|
427
603
|
|
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}`);
|