@aztec/bot 3.0.3 → 3.9.9-nightly.20260312
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 +39 -24
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +42 -14
- 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 +172 -80
- 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 +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 +17 -14
- package/src/amm_bot.ts +26 -21
- package/src/base_bot.ts +13 -27
- package/src/bot.ts +10 -8
- package/src/config.ts +89 -58
- package/src/cross_chain_bot.ts +203 -0
- package/src/factory.ts +193 -63
- 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
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CrossChainBot exercises L2->L1 and L1->L2 messaging.
|
|
3
|
+
*
|
|
4
|
+
* createAndSendTx onTxMined
|
|
5
|
+
* ────────────────────────────────────── ──────────────────────────────
|
|
6
|
+
*
|
|
7
|
+
* 1. SEED (fire-and-forget) 3. VERIFY L2->L1
|
|
8
|
+
* if store has fewer pending messages Query getTxEffect, confirm
|
|
9
|
+
* than seedCount and no seed is the expected L2->L1 messages
|
|
10
|
+
* in-flight: appeared in tx effects.
|
|
11
|
+
* * kick off L1 inbox tx
|
|
12
|
+
* * store msg on completion
|
|
13
|
+
*
|
|
14
|
+
* 2. BUILD & SEND BATCH
|
|
15
|
+
* Always:
|
|
16
|
+
* N x create_l2_to_l1_message
|
|
17
|
+
* (random content, fixed
|
|
18
|
+
* L1 recipient)
|
|
19
|
+
* If a ready L1->L2 msg exists:
|
|
20
|
+
* 1 x consume_message_from_
|
|
21
|
+
* arbitrary_sender_public
|
|
22
|
+
* delete consumed msg from store
|
|
23
|
+
* Send batch tx (no wait)
|
|
24
|
+
*
|
|
25
|
+
*/
|
|
26
|
+
import { AztecAddress } from '@aztec/aztec.js/addresses';
|
|
27
|
+
import { BatchCall, NO_WAIT } from '@aztec/aztec.js/contracts';
|
|
28
|
+
import { isL1ToL2MessageReady } from '@aztec/aztec.js/messaging';
|
|
29
|
+
import { TxHash, TxReceipt } from '@aztec/aztec.js/tx';
|
|
30
|
+
import type { ExtendedViemWalletClient } from '@aztec/ethereum/types';
|
|
31
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
32
|
+
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
33
|
+
import type { TestContract } from '@aztec/noir-test-contracts.js/Test';
|
|
34
|
+
import type { AztecNode, AztecNodeAdmin } from '@aztec/stdlib/interfaces/client';
|
|
35
|
+
import type { EmbeddedWallet } from '@aztec/wallets/embedded';
|
|
36
|
+
|
|
37
|
+
import { BaseBot } from './base_bot.js';
|
|
38
|
+
import type { BotConfig } from './config.js';
|
|
39
|
+
import { BotFactory } from './factory.js';
|
|
40
|
+
import { seedL1ToL2Message } from './l1_to_l2_seeding.js';
|
|
41
|
+
import type { BotStore, PendingL1ToL2Message } from './store/index.js';
|
|
42
|
+
|
|
43
|
+
/** Stale message threshold: messages older than this are removed. */
|
|
44
|
+
const STALE_MESSAGE_THRESHOLD_MS = 2 * 60 * 60 * 1000; // 2 hours
|
|
45
|
+
|
|
46
|
+
/** Bot that exercises both L2→L1 and L1→L2 cross-chain messaging. */
|
|
47
|
+
export class CrossChainBot extends BaseBot {
|
|
48
|
+
private l2ToL1Sent = 0;
|
|
49
|
+
private l1ToL2Consumed = 0;
|
|
50
|
+
private pendingSeedPromise: Promise<void> | undefined;
|
|
51
|
+
|
|
52
|
+
protected constructor(
|
|
53
|
+
node: AztecNode,
|
|
54
|
+
wallet: EmbeddedWallet,
|
|
55
|
+
defaultAccountAddress: AztecAddress,
|
|
56
|
+
private readonly contract: TestContract,
|
|
57
|
+
private readonly l1Client: ExtendedViemWalletClient,
|
|
58
|
+
private readonly l1Recipient: EthAddress,
|
|
59
|
+
private readonly inboxAddress: EthAddress,
|
|
60
|
+
private readonly rollupVersion: bigint,
|
|
61
|
+
private readonly store: BotStore,
|
|
62
|
+
config: BotConfig,
|
|
63
|
+
) {
|
|
64
|
+
super(node, wallet, defaultAccountAddress, config);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
static async create(
|
|
68
|
+
config: BotConfig,
|
|
69
|
+
wallet: EmbeddedWallet,
|
|
70
|
+
aztecNode: AztecNode,
|
|
71
|
+
aztecNodeAdmin: AztecNodeAdmin | undefined,
|
|
72
|
+
store: BotStore,
|
|
73
|
+
): Promise<CrossChainBot> {
|
|
74
|
+
if (config.followChain === 'NONE') {
|
|
75
|
+
throw new Error(`CrossChainBot requires followChain to be set (got NONE)`);
|
|
76
|
+
}
|
|
77
|
+
const factory = new BotFactory(config, wallet, store, aztecNode, aztecNodeAdmin);
|
|
78
|
+
const { defaultAccountAddress, contract, l1Client, rollupVersion } = await factory.setupCrossChain();
|
|
79
|
+
const l1Recipient = EthAddress.fromString(l1Client.account!.address);
|
|
80
|
+
const { l1ContractAddresses } = await aztecNode.getNodeInfo();
|
|
81
|
+
const inboxAddress = EthAddress.fromString(l1ContractAddresses.inboxAddress.toString());
|
|
82
|
+
return new CrossChainBot(
|
|
83
|
+
aztecNode,
|
|
84
|
+
wallet,
|
|
85
|
+
defaultAccountAddress,
|
|
86
|
+
contract,
|
|
87
|
+
l1Client,
|
|
88
|
+
l1Recipient,
|
|
89
|
+
inboxAddress,
|
|
90
|
+
rollupVersion,
|
|
91
|
+
store,
|
|
92
|
+
config,
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
protected async createAndSendTx(logCtx: object): Promise<TxHash> {
|
|
97
|
+
const pendingMessages = await this.store.getUnconsumedL1ToL2Messages();
|
|
98
|
+
|
|
99
|
+
// Send an L1→L2 message if we're below the threshold and not already seeding one
|
|
100
|
+
if (pendingMessages.length < this.config.l1ToL2SeedCount && !this.pendingSeedPromise) {
|
|
101
|
+
this.pendingSeedPromise = this.seedNewL1ToL2Message()
|
|
102
|
+
.catch(err => this.log.warn(`Failed to seed L1→L2 message: ${err}`, logCtx))
|
|
103
|
+
.finally(() => {
|
|
104
|
+
this.pendingSeedPromise = undefined;
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Build batch: always L2→L1, optionally consume L1→L2
|
|
109
|
+
const calls = [];
|
|
110
|
+
|
|
111
|
+
// L2→L1: create messages with random content
|
|
112
|
+
for (let i = 0; i < this.config.l2ToL1MessagesPerTx; i++) {
|
|
113
|
+
calls.push(
|
|
114
|
+
this.contract.methods.create_l2_to_l1_message_arbitrary_recipient_public(Fr.random(), this.l1Recipient),
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// L1→L2: consume oldest ready message if available
|
|
119
|
+
const readyMsg = await this.getReadyL1ToL2Message(pendingMessages);
|
|
120
|
+
if (readyMsg) {
|
|
121
|
+
calls.push(
|
|
122
|
+
this.contract.methods.consume_message_from_arbitrary_sender_public(
|
|
123
|
+
Fr.fromHexString(readyMsg.content),
|
|
124
|
+
Fr.fromHexString(readyMsg.secret),
|
|
125
|
+
EthAddress.fromString(readyMsg.sender),
|
|
126
|
+
new Fr(BigInt(readyMsg.globalLeafIndex)),
|
|
127
|
+
),
|
|
128
|
+
);
|
|
129
|
+
// Delete consumed message immediately so it works with FOLLOW_CHAIN=NONE
|
|
130
|
+
await this.store.deleteL1ToL2Message(readyMsg.msgHash);
|
|
131
|
+
this.l1ToL2Consumed++;
|
|
132
|
+
} else {
|
|
133
|
+
this.log.warn(`No ready L1→L2 message to consume`, {
|
|
134
|
+
...logCtx,
|
|
135
|
+
pendingCount: pendingMessages.length,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const batch = new BatchCall(this.wallet, calls);
|
|
140
|
+
const opts = await this.getSendMethodOpts(batch);
|
|
141
|
+
|
|
142
|
+
this.log.verbose(`Sending cross-chain batch with ${calls.length} calls`, logCtx);
|
|
143
|
+
const { txHash } = await batch.send({ ...opts, wait: NO_WAIT });
|
|
144
|
+
return txHash;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
protected override async onTxMined(receipt: TxReceipt, logCtx: object): Promise<void> {
|
|
148
|
+
// Verify L2→L1 messages appeared in this tx's effects
|
|
149
|
+
const indexed = await this.node.getTxEffect(receipt.txHash);
|
|
150
|
+
if (indexed) {
|
|
151
|
+
const l2ToL1Msgs = indexed.data.l2ToL1Msgs.filter(m => !m.isZero());
|
|
152
|
+
if (l2ToL1Msgs.length >= this.config.l2ToL1MessagesPerTx) {
|
|
153
|
+
this.l2ToL1Sent += l2ToL1Msgs.length;
|
|
154
|
+
} else {
|
|
155
|
+
this.log.error(`Expected ${this.config.l2ToL1MessagesPerTx} L2→L1 messages but found ${l2ToL1Msgs.length}`, {
|
|
156
|
+
...logCtx,
|
|
157
|
+
blockNumber: receipt.blockNumber,
|
|
158
|
+
txHash: receipt.txHash.toString(),
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const pendingCount = (await this.store.getUnconsumedL1ToL2Messages()).length;
|
|
164
|
+
this.log.info(`CrossChainBot txs mined`, {
|
|
165
|
+
...logCtx,
|
|
166
|
+
l2ToL1Sent: this.l2ToL1Sent,
|
|
167
|
+
l1ToL2Consumed: this.l1ToL2Consumed,
|
|
168
|
+
l1ToL2Pending: pendingCount,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Finds the oldest pending message that is ready for consumption. */
|
|
173
|
+
private async getReadyL1ToL2Message(
|
|
174
|
+
pendingMessages: PendingL1ToL2Message[],
|
|
175
|
+
): Promise<PendingL1ToL2Message | undefined> {
|
|
176
|
+
const now = Date.now();
|
|
177
|
+
for (const msg of pendingMessages) {
|
|
178
|
+
const ready = await isL1ToL2MessageReady(this.node, Fr.fromHexString(msg.msgHash));
|
|
179
|
+
if (ready) {
|
|
180
|
+
return msg;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Time-based stale detection: if the message is old and still not ready, remove it
|
|
184
|
+
if (now - msg.timestamp > STALE_MESSAGE_THRESHOLD_MS) {
|
|
185
|
+
await this.store.deleteL1ToL2Message(msg.msgHash);
|
|
186
|
+
this.log.warn(`Removed stale L1→L2 message ${msg.msgHash}`);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return undefined;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Seeds a new L1→L2 message on L1 and stores it. */
|
|
193
|
+
private async seedNewL1ToL2Message(): Promise<void> {
|
|
194
|
+
await seedL1ToL2Message(
|
|
195
|
+
this.l1Client,
|
|
196
|
+
this.inboxAddress,
|
|
197
|
+
this.contract.address,
|
|
198
|
+
this.rollupVersion,
|
|
199
|
+
this.store,
|
|
200
|
+
this.log,
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
}
|
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,25 +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';
|
|
17
|
+
import { waitForTx } from '@aztec/aztec.js/node';
|
|
16
18
|
import { createEthereumChain } from '@aztec/ethereum/chain';
|
|
17
19
|
import { createExtendedL1Client } from '@aztec/ethereum/client';
|
|
20
|
+
import { RollupContract } from '@aztec/ethereum/contracts';
|
|
21
|
+
import type { ExtendedViemWalletClient } from '@aztec/ethereum/types';
|
|
18
22
|
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
23
|
+
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
19
24
|
import { Timer } from '@aztec/foundation/timer';
|
|
20
25
|
import { AMMContract } from '@aztec/noir-contracts.js/AMM';
|
|
21
26
|
import { PrivateTokenContract } from '@aztec/noir-contracts.js/PrivateToken';
|
|
22
27
|
import { TokenContract } from '@aztec/noir-contracts.js/Token';
|
|
28
|
+
import { TestContract } from '@aztec/noir-test-contracts.js/Test';
|
|
29
|
+
import type { ContractInstanceWithAddress } from '@aztec/stdlib/contract';
|
|
23
30
|
import { GasSettings } from '@aztec/stdlib/gas';
|
|
24
31
|
import type { AztecNode, AztecNodeAdmin } from '@aztec/stdlib/interfaces/client';
|
|
25
32
|
import { deriveSigningKey } from '@aztec/stdlib/keys';
|
|
26
|
-
import {
|
|
33
|
+
import { EmbeddedWallet } from '@aztec/wallets/embedded';
|
|
27
34
|
|
|
28
35
|
import { type BotConfig, SupportedTokenContracts } from './config.js';
|
|
36
|
+
import { seedL1ToL2Message } from './l1_to_l2_seeding.js';
|
|
29
37
|
import type { BotStore } from './store/index.js';
|
|
30
38
|
import { getBalances, getPrivateBalance, isStandardTokenContract } from './utils.js';
|
|
31
39
|
|
|
@@ -37,7 +45,7 @@ export class BotFactory {
|
|
|
37
45
|
|
|
38
46
|
constructor(
|
|
39
47
|
private readonly config: BotConfig,
|
|
40
|
-
private readonly wallet:
|
|
48
|
+
private readonly wallet: EmbeddedWallet,
|
|
41
49
|
private readonly store: BotStore,
|
|
42
50
|
private readonly aztecNode: AztecNode,
|
|
43
51
|
private readonly aztecNodeAdmin?: AztecNodeAdmin,
|
|
@@ -48,21 +56,21 @@ export class BotFactory {
|
|
|
48
56
|
* deploying the token contract, and minting tokens if necessary.
|
|
49
57
|
*/
|
|
50
58
|
public async setup(): Promise<{
|
|
51
|
-
wallet:
|
|
59
|
+
wallet: EmbeddedWallet;
|
|
52
60
|
defaultAccountAddress: AztecAddress;
|
|
53
61
|
token: TokenContract | PrivateTokenContract;
|
|
54
62
|
node: AztecNode;
|
|
55
63
|
recipient: AztecAddress;
|
|
56
64
|
}> {
|
|
57
65
|
const defaultAccountAddress = await this.setupAccount();
|
|
58
|
-
const recipient = (await this.wallet.
|
|
66
|
+
const recipient = (await this.wallet.createSchnorrAccount(Fr.random(), Fr.random())).address;
|
|
59
67
|
const token = await this.setupToken(defaultAccountAddress);
|
|
60
68
|
await this.mintTokens(token, defaultAccountAddress);
|
|
61
69
|
return { wallet: this.wallet, defaultAccountAddress, token, node: this.aztecNode, recipient };
|
|
62
70
|
}
|
|
63
71
|
|
|
64
72
|
public async setupAmm(): Promise<{
|
|
65
|
-
wallet:
|
|
73
|
+
wallet: EmbeddedWallet;
|
|
66
74
|
defaultAccountAddress: AztecAddress;
|
|
67
75
|
amm: AMMContract;
|
|
68
76
|
token0: TokenContract;
|
|
@@ -92,6 +100,89 @@ export class BotFactory {
|
|
|
92
100
|
return { wallet: this.wallet, defaultAccountAddress, amm, token0, token1, node: this.aztecNode };
|
|
93
101
|
}
|
|
94
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
|
+
|
|
95
186
|
/**
|
|
96
187
|
* Checks if the sender account contract is initialized, and initializes it if necessary.
|
|
97
188
|
* @returns The sender wallet.
|
|
@@ -110,14 +201,9 @@ export class BotFactory {
|
|
|
110
201
|
private async setupAccountWithPrivateKey(secret: Fr) {
|
|
111
202
|
const salt = this.config.senderSalt ?? Fr.ONE;
|
|
112
203
|
const signingKey = deriveSigningKey(secret);
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
contract: new SchnorrAccountContract(signingKey!),
|
|
117
|
-
};
|
|
118
|
-
const accountManager = await this.wallet.createAccount(accountData);
|
|
119
|
-
const isInit = (await this.wallet.getContractMetadata(accountManager.address)).isContractInitialized;
|
|
120
|
-
if (isInit) {
|
|
204
|
+
const accountManager = await this.wallet.createSchnorrAccount(secret, salt, signingKey);
|
|
205
|
+
const metadata = await this.wallet.getContractMetadata(accountManager.address);
|
|
206
|
+
if (metadata.isContractInitialized) {
|
|
121
207
|
this.log.info(`Account at ${accountManager.address.toString()} already initialized`);
|
|
122
208
|
const timer = new Timer();
|
|
123
209
|
const address = accountManager.address;
|
|
@@ -132,12 +218,18 @@ export class BotFactory {
|
|
|
132
218
|
|
|
133
219
|
const paymentMethod = new FeeJuicePaymentMethodWithClaim(accountManager.address, claim);
|
|
134
220
|
const deployMethod = await accountManager.getDeployMethod();
|
|
135
|
-
const maxFeesPerGas = (await this.aztecNode.
|
|
221
|
+
const maxFeesPerGas = (await this.aztecNode.getCurrentMinFees()).mul(1 + this.config.minFeePadding);
|
|
136
222
|
const gasSettings = GasSettings.default({ maxFeesPerGas });
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|
+
});
|
|
141
233
|
this.log.info(`Account deployed at ${address}`);
|
|
142
234
|
|
|
143
235
|
// Clean up the consumed bridge claim
|
|
@@ -149,12 +241,11 @@ export class BotFactory {
|
|
|
149
241
|
|
|
150
242
|
private async setupTestAccount() {
|
|
151
243
|
const [initialAccountData] = await getInitialTestAccountsData();
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
const accountManager = await this.wallet.createAccount(accountData);
|
|
244
|
+
const accountManager = await this.wallet.createSchnorrAccount(
|
|
245
|
+
initialAccountData.secret,
|
|
246
|
+
initialAccountData.salt,
|
|
247
|
+
initialAccountData.signingKey,
|
|
248
|
+
);
|
|
158
249
|
return accountManager.address;
|
|
159
250
|
}
|
|
160
251
|
|
|
@@ -165,33 +256,51 @@ export class BotFactory {
|
|
|
165
256
|
*/
|
|
166
257
|
private async setupToken(sender: AztecAddress): Promise<TokenContract | PrivateTokenContract> {
|
|
167
258
|
let deploy: DeployMethod<TokenContract | PrivateTokenContract>;
|
|
259
|
+
let tokenInstance: ContractInstanceWithAddress | undefined;
|
|
168
260
|
const deployOpts: DeployOptions = {
|
|
169
261
|
from: sender,
|
|
170
262
|
contractAddressSalt: this.config.tokenSalt,
|
|
171
263
|
universalDeploy: true,
|
|
172
264
|
};
|
|
265
|
+
let token: TokenContract | PrivateTokenContract;
|
|
173
266
|
if (this.config.contract === SupportedTokenContracts.TokenContract) {
|
|
174
267
|
deploy = TokenContract.deploy(this.wallet, sender, 'BotToken', 'BOT', 18);
|
|
268
|
+
tokenInstance = await deploy.getInstance(deployOpts);
|
|
269
|
+
token = TokenContract.at(tokenInstance.address, this.wallet);
|
|
175
270
|
} else if (this.config.contract === SupportedTokenContracts.PrivateTokenContract) {
|
|
176
|
-
|
|
271
|
+
// Generate keys for the contract since PrivateToken uses SinglePrivateMutable which requires keys
|
|
272
|
+
const tokenSecretKey = Fr.random();
|
|
273
|
+
const tokenPublicKeys = (await deriveKeys(tokenSecretKey)).publicKeys;
|
|
274
|
+
deploy = PrivateTokenContract.deployWithPublicKeys(tokenPublicKeys, this.wallet, MINT_BALANCE, sender);
|
|
177
275
|
deployOpts.skipInstancePublication = true;
|
|
178
276
|
deployOpts.skipClassPublication = true;
|
|
179
277
|
deployOpts.skipInitialization = false;
|
|
278
|
+
|
|
279
|
+
// Register the contract with the secret key before deployment
|
|
280
|
+
tokenInstance = await deploy.getInstance(deployOpts);
|
|
281
|
+
token = PrivateTokenContract.at(tokenInstance.address, this.wallet);
|
|
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];
|
|
180
285
|
} else {
|
|
181
286
|
throw new Error(`Unsupported token contract type: ${this.config.contract}`);
|
|
182
287
|
}
|
|
183
288
|
|
|
184
|
-
const address = (await deploy.getInstance(deployOpts)).address;
|
|
185
|
-
|
|
289
|
+
const address = tokenInstance?.address ?? (await deploy.getInstance(deployOpts)).address;
|
|
290
|
+
const metadata = await this.wallet.getContractMetadata(address);
|
|
291
|
+
if (metadata.isContractPublished) {
|
|
186
292
|
this.log.info(`Token at ${address.toString()} already deployed`);
|
|
187
|
-
|
|
293
|
+
await deploy.register();
|
|
188
294
|
} else {
|
|
189
295
|
this.log.info(`Deploying token contract at ${address.toString()}`);
|
|
190
|
-
const
|
|
191
|
-
const txHash = await sentTx.getTxHash();
|
|
296
|
+
const { txHash } = await deploy.send({ ...deployOpts, wait: NO_WAIT });
|
|
192
297
|
this.log.info(`Sent tx for token setup with hash ${txHash.toString()}`);
|
|
193
|
-
|
|
298
|
+
await this.withNoMinTxsPerBlock(async () => {
|
|
299
|
+
await waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
300
|
+
return token;
|
|
301
|
+
});
|
|
194
302
|
}
|
|
303
|
+
return token;
|
|
195
304
|
}
|
|
196
305
|
|
|
197
306
|
/**
|
|
@@ -199,7 +308,7 @@ export class BotFactory {
|
|
|
199
308
|
* @param wallet - Wallet to deploy the token contract from.
|
|
200
309
|
* @returns The TokenContract instance.
|
|
201
310
|
*/
|
|
202
|
-
private setupTokenContract(
|
|
311
|
+
private async setupTokenContract(
|
|
203
312
|
deployer: AztecAddress,
|
|
204
313
|
contractAddressSalt: Fr,
|
|
205
314
|
name: string,
|
|
@@ -208,7 +317,8 @@ export class BotFactory {
|
|
|
208
317
|
): Promise<TokenContract> {
|
|
209
318
|
const deployOpts: DeployOptions = { from: deployer, contractAddressSalt, universalDeploy: true };
|
|
210
319
|
const deploy = TokenContract.deploy(this.wallet, deployer, name, ticker, decimals);
|
|
211
|
-
|
|
320
|
+
const instance = await this.registerOrDeployContract('Token - ' + name, deploy, deployOpts);
|
|
321
|
+
return TokenContract.at(instance.address, this.wallet);
|
|
212
322
|
}
|
|
213
323
|
|
|
214
324
|
private async setupAmmContract(
|
|
@@ -220,12 +330,14 @@ export class BotFactory {
|
|
|
220
330
|
): Promise<AMMContract> {
|
|
221
331
|
const deployOpts: DeployOptions = { from: deployer, contractAddressSalt, universalDeploy: true };
|
|
222
332
|
const deploy = AMMContract.deploy(this.wallet, token0.address, token1.address, lpToken.address);
|
|
223
|
-
const
|
|
333
|
+
const instance = await this.registerOrDeployContract('AMM', deploy, deployOpts);
|
|
334
|
+
const amm = AMMContract.at(instance.address, this.wallet);
|
|
224
335
|
|
|
225
336
|
this.log.info(`AMM deployed at ${amm.address}`);
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
|
|
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()}`);
|
|
229
341
|
this.log.info(`Liquidity token initialized`);
|
|
230
342
|
|
|
231
343
|
return amm;
|
|
@@ -241,9 +353,18 @@ export class BotFactory {
|
|
|
241
353
|
): Promise<void> {
|
|
242
354
|
const getPrivateBalances = () =>
|
|
243
355
|
Promise.all([
|
|
244
|
-
token0.methods
|
|
245
|
-
|
|
246
|
-
|
|
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),
|
|
247
368
|
]);
|
|
248
369
|
|
|
249
370
|
const authwitNonce = Fr.random();
|
|
@@ -284,23 +405,22 @@ export class BotFactory {
|
|
|
284
405
|
.getFunctionCall(),
|
|
285
406
|
});
|
|
286
407
|
|
|
287
|
-
const
|
|
408
|
+
const { receipt: mintReceipt } = await new BatchCall(this.wallet, [
|
|
288
409
|
token0.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
|
|
289
410
|
token1.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
|
|
290
|
-
]).send({ from: liquidityProvider });
|
|
411
|
+
]).send({ from: liquidityProvider, wait: { timeout: this.config.txMinedWaitSeconds } });
|
|
291
412
|
|
|
292
|
-
this.log.info(`Sent mint tx: ${
|
|
293
|
-
await mintTx.wait({ timeout: this.config.txMinedWaitSeconds });
|
|
413
|
+
this.log.info(`Sent mint tx: ${mintReceipt.txHash.toString()}`);
|
|
294
414
|
|
|
295
|
-
const
|
|
415
|
+
const { receipt: addLiquidityReceipt } = await amm.methods
|
|
296
416
|
.add_liquidity(amount0Max, amount1Max, amount0Min, amount1Min, authwitNonce)
|
|
297
417
|
.send({
|
|
298
418
|
from: liquidityProvider,
|
|
299
419
|
authWitnesses: [token0Authwit, token1Authwit],
|
|
420
|
+
wait: { timeout: this.config.txMinedWaitSeconds },
|
|
300
421
|
});
|
|
301
422
|
|
|
302
|
-
this.log.info(`Sent tx to add liquidity to the AMM: ${
|
|
303
|
-
await addLiquidityTx.wait({ timeout: this.config.txMinedWaitSeconds });
|
|
423
|
+
this.log.info(`Sent tx to add liquidity to the AMM: ${addLiquidityReceipt.txHash.toString()}`);
|
|
304
424
|
this.log.info(`Liquidity added`);
|
|
305
425
|
|
|
306
426
|
const [newT0Bal, newT1Bal, newLPBal] = await getPrivateBalances();
|
|
@@ -313,18 +433,22 @@ export class BotFactory {
|
|
|
313
433
|
name: string,
|
|
314
434
|
deploy: DeployMethod<T>,
|
|
315
435
|
deployOpts: DeployOptions,
|
|
316
|
-
): Promise<
|
|
317
|
-
const
|
|
318
|
-
|
|
436
|
+
): Promise<ContractInstanceWithAddress> {
|
|
437
|
+
const instance = await deploy.getInstance(deployOpts);
|
|
438
|
+
const address = instance.address;
|
|
439
|
+
const metadata = await this.wallet.getContractMetadata(address);
|
|
440
|
+
if (metadata.isContractPublished) {
|
|
319
441
|
this.log.info(`Contract ${name} at ${address.toString()} already deployed`);
|
|
320
|
-
|
|
442
|
+
await deploy.register();
|
|
321
443
|
} else {
|
|
322
444
|
this.log.info(`Deploying contract ${name} at ${address.toString()}`);
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
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
|
+
});
|
|
327
450
|
}
|
|
451
|
+
return instance;
|
|
328
452
|
}
|
|
329
453
|
|
|
330
454
|
/**
|
|
@@ -360,10 +484,18 @@ export class BotFactory {
|
|
|
360
484
|
this.log.info(`Skipping minting as ${minter.toString()} has enough tokens`);
|
|
361
485
|
return;
|
|
362
486
|
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
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
|
+
});
|
|
367
499
|
}
|
|
368
500
|
|
|
369
501
|
/**
|
|
@@ -383,7 +515,6 @@ export class BotFactory {
|
|
|
383
515
|
await this.withNoMinTxsPerBlock(() =>
|
|
384
516
|
waitForL1ToL2MessageReady(this.aztecNode, messageHash, {
|
|
385
517
|
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds,
|
|
386
|
-
forPublicConsumption: false,
|
|
387
518
|
}),
|
|
388
519
|
);
|
|
389
520
|
return existingClaim.claim;
|
|
@@ -422,7 +553,6 @@ export class BotFactory {
|
|
|
422
553
|
await this.withNoMinTxsPerBlock(() =>
|
|
423
554
|
waitForL1ToL2MessageReady(this.aztecNode, Fr.fromHexString(claim.messageHash), {
|
|
424
555
|
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds,
|
|
425
|
-
forPublicConsumption: false,
|
|
426
556
|
}),
|
|
427
557
|
);
|
|
428
558
|
|