@aztec/bot 0.0.1-commit.b655e406 → 0.0.1-commit.c0b82b2
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 +5 -1
- 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 +7 -4
- package/dest/config.d.ts +72 -57
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +43 -15
- 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 +140 -0
- package/dest/factory.d.ts +18 -26
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +175 -75
- package/dest/index.d.ts +2 -1
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +1 -0
- package/dest/interface.d.ts +1 -1
- 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/rpc.d.ts +1 -1
- 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 +30 -5
- 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.d.ts +1 -1
- package/package.json +19 -15
- package/src/amm_bot.ts +7 -7
- package/src/base_bot.ts +13 -27
- package/src/bot.ts +9 -8
- package/src/config.ts +90 -59
- package/src/cross_chain_bot.ts +209 -0
- package/src/factory.ts +198 -60
- 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
|
@@ -0,0 +1,209 @@
|
|
|
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
|
+
return batch.send({ ...opts, wait: NO_WAIT });
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
protected override async onTxMined(receipt: TxReceipt, logCtx: object): Promise<void> {
|
|
147
|
+
// Verify L2→L1 messages appeared in this tx's effects
|
|
148
|
+
const indexed = await this.node.getTxEffect(receipt.txHash);
|
|
149
|
+
if (indexed) {
|
|
150
|
+
const l2ToL1Msgs = indexed.data.l2ToL1Msgs.filter(m => !m.isZero());
|
|
151
|
+
if (l2ToL1Msgs.length >= this.config.l2ToL1MessagesPerTx) {
|
|
152
|
+
this.l2ToL1Sent += l2ToL1Msgs.length;
|
|
153
|
+
} else {
|
|
154
|
+
this.log.error(`Expected ${this.config.l2ToL1MessagesPerTx} L2→L1 messages but found ${l2ToL1Msgs.length}`, {
|
|
155
|
+
...logCtx,
|
|
156
|
+
blockNumber: receipt.blockNumber,
|
|
157
|
+
txHash: receipt.txHash.toString(),
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const pendingCount = (await this.store.getUnconsumedL1ToL2Messages()).length;
|
|
163
|
+
this.log.info(`CrossChainBot txs mined`, {
|
|
164
|
+
...logCtx,
|
|
165
|
+
l2ToL1Sent: this.l2ToL1Sent,
|
|
166
|
+
l1ToL2Consumed: this.l1ToL2Consumed,
|
|
167
|
+
l1ToL2Pending: pendingCount,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Finds the oldest pending message that is ready for consumption. */
|
|
172
|
+
private async getReadyL1ToL2Message(
|
|
173
|
+
pendingMessages: PendingL1ToL2Message[],
|
|
174
|
+
): Promise<PendingL1ToL2Message | undefined> {
|
|
175
|
+
const now = Date.now();
|
|
176
|
+
for (const msg of pendingMessages) {
|
|
177
|
+
const ready = await isL1ToL2MessageReady(this.node, Fr.fromHexString(msg.msgHash), {
|
|
178
|
+
// Use forPublicConsumption: false so we wait until blockNumber >= messageBlockNumber.
|
|
179
|
+
// With forPublicConsumption: true, the check returns true one block early (the sequencer
|
|
180
|
+
// includes L1→L2 messages before executing the block's txs), but gas estimation simulates
|
|
181
|
+
// against the current world state which doesn't yet have the message.
|
|
182
|
+
// See https://linear.app/aztec-labs/issue/A-548 for details.
|
|
183
|
+
forPublicConsumption: false,
|
|
184
|
+
});
|
|
185
|
+
if (ready) {
|
|
186
|
+
return msg;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Time-based stale detection: if the message is old and still not ready, remove it
|
|
190
|
+
if (now - msg.timestamp > STALE_MESSAGE_THRESHOLD_MS) {
|
|
191
|
+
await this.store.deleteL1ToL2Message(msg.msgHash);
|
|
192
|
+
this.log.warn(`Removed stale L1→L2 message ${msg.msgHash}`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return undefined;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Seeds a new L1→L2 message on L1 and stores it. */
|
|
199
|
+
private async seedNewL1ToL2Message(): Promise<void> {
|
|
200
|
+
await seedL1ToL2Message(
|
|
201
|
+
this.l1Client,
|
|
202
|
+
this.inboxAddress,
|
|
203
|
+
this.contract.address,
|
|
204
|
+
this.rollupVersion,
|
|
205
|
+
this.store,
|
|
206
|
+
this.log,
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
}
|
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';
|
|
28
|
+
import { TestContract } from '@aztec/noir-test-contracts.js/Test';
|
|
29
|
+
import type { ContractInstanceWithAddress } from '@aztec/stdlib/contract';
|
|
22
30
|
import { 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,7 +45,7 @@ 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,
|
|
@@ -46,15 +55,28 @@ export class BotFactory {
|
|
|
46
55
|
* Initializes a new bot by setting up the sender account, registering the recipient,
|
|
47
56
|
* deploying the token contract, and minting tokens if necessary.
|
|
48
57
|
*/
|
|
49
|
-
public async setup() {
|
|
50
|
-
|
|
58
|
+
public async setup(): Promise<{
|
|
59
|
+
wallet: EmbeddedWallet;
|
|
60
|
+
defaultAccountAddress: AztecAddress;
|
|
61
|
+
token: TokenContract | PrivateTokenContract;
|
|
62
|
+
node: AztecNode;
|
|
63
|
+
recipient: AztecAddress;
|
|
64
|
+
}> {
|
|
51
65
|
const defaultAccountAddress = await this.setupAccount();
|
|
66
|
+
const recipient = (await this.wallet.createSchnorrAccount(Fr.random(), Fr.random())).address;
|
|
52
67
|
const token = await this.setupToken(defaultAccountAddress);
|
|
53
68
|
await this.mintTokens(token, defaultAccountAddress);
|
|
54
69
|
return { wallet: this.wallet, defaultAccountAddress, token, node: this.aztecNode, recipient };
|
|
55
70
|
}
|
|
56
71
|
|
|
57
|
-
public async setupAmm() {
|
|
72
|
+
public async setupAmm(): Promise<{
|
|
73
|
+
wallet: EmbeddedWallet;
|
|
74
|
+
defaultAccountAddress: AztecAddress;
|
|
75
|
+
amm: AMMContract;
|
|
76
|
+
token0: TokenContract;
|
|
77
|
+
token1: TokenContract;
|
|
78
|
+
node: AztecNode;
|
|
79
|
+
}> {
|
|
58
80
|
const defaultAccountAddress = await this.setupAccount();
|
|
59
81
|
const token0 = await this.setupTokenContract(defaultAccountAddress, this.config.tokenSalt, 'BotToken0', 'BOT0');
|
|
60
82
|
const token1 = await this.setupTokenContract(defaultAccountAddress, this.config.tokenSalt, 'BotToken1', 'BOT1');
|
|
@@ -78,6 +100,94 @@ export class BotFactory {
|
|
|
78
100
|
return { wallet: this.wallet, defaultAccountAddress, amm, token0, token1, node: this.aztecNode };
|
|
79
101
|
}
|
|
80
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
|
+
// Use forPublicConsumption: false so we wait until the message is in the current world
|
|
162
|
+
// state. With true, it returns one block early which causes gas estimation simulation to
|
|
163
|
+
// fail since it runs against the current state.
|
|
164
|
+
// See https://linear.app/aztec-labs/issue/A-548 for details.
|
|
165
|
+
forPublicConsumption: false,
|
|
166
|
+
});
|
|
167
|
+
this.log.info(`First L1→L2 message is ready`);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return {
|
|
171
|
+
wallet: this.wallet,
|
|
172
|
+
defaultAccountAddress,
|
|
173
|
+
contract,
|
|
174
|
+
node: this.aztecNode,
|
|
175
|
+
l1Client,
|
|
176
|
+
rollupVersion,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private async setupTestContract(deployer: AztecAddress): Promise<TestContract> {
|
|
181
|
+
const deployOpts: DeployOptions = {
|
|
182
|
+
from: deployer,
|
|
183
|
+
contractAddressSalt: this.config.tokenSalt,
|
|
184
|
+
universalDeploy: true,
|
|
185
|
+
};
|
|
186
|
+
const deploy = TestContract.deploy(this.wallet);
|
|
187
|
+
const instance = await this.registerOrDeployContract('TestContract', deploy, deployOpts);
|
|
188
|
+
return TestContract.at(instance.address, this.wallet);
|
|
189
|
+
}
|
|
190
|
+
|
|
81
191
|
/**
|
|
82
192
|
* Checks if the sender account contract is initialized, and initializes it if necessary.
|
|
83
193
|
* @returns The sender wallet.
|
|
@@ -96,14 +206,9 @@ export class BotFactory {
|
|
|
96
206
|
private async setupAccountWithPrivateKey(secret: Fr) {
|
|
97
207
|
const salt = this.config.senderSalt ?? Fr.ONE;
|
|
98
208
|
const signingKey = deriveSigningKey(secret);
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
contract: new SchnorrAccountContract(signingKey!),
|
|
103
|
-
};
|
|
104
|
-
const accountManager = await this.wallet.createAccount(accountData);
|
|
105
|
-
const isInit = (await this.wallet.getContractMetadata(accountManager.address)).isContractInitialized;
|
|
106
|
-
if (isInit) {
|
|
209
|
+
const accountManager = await this.wallet.createSchnorrAccount(secret, salt, signingKey);
|
|
210
|
+
const metadata = await this.wallet.getContractMetadata(accountManager.address);
|
|
211
|
+
if (metadata.isContractInitialized) {
|
|
107
212
|
this.log.info(`Account at ${accountManager.address.toString()} already initialized`);
|
|
108
213
|
const timer = new Timer();
|
|
109
214
|
const address = accountManager.address;
|
|
@@ -118,12 +223,18 @@ export class BotFactory {
|
|
|
118
223
|
|
|
119
224
|
const paymentMethod = new FeeJuicePaymentMethodWithClaim(accountManager.address, claim);
|
|
120
225
|
const deployMethod = await accountManager.getDeployMethod();
|
|
121
|
-
const maxFeesPerGas = (await this.aztecNode.
|
|
226
|
+
const maxFeesPerGas = (await this.aztecNode.getCurrentMinFees()).mul(1 + this.config.minFeePadding);
|
|
122
227
|
const gasSettings = GasSettings.default({ maxFeesPerGas });
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
228
|
+
|
|
229
|
+
await this.withNoMinTxsPerBlock(async () => {
|
|
230
|
+
const txHash = await deployMethod.send({
|
|
231
|
+
from: AztecAddress.ZERO,
|
|
232
|
+
fee: { gasSettings, paymentMethod },
|
|
233
|
+
wait: NO_WAIT,
|
|
234
|
+
});
|
|
235
|
+
this.log.info(`Sent tx for account deployment with hash ${txHash.toString()}`);
|
|
236
|
+
return waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
237
|
+
});
|
|
127
238
|
this.log.info(`Account deployed at ${address}`);
|
|
128
239
|
|
|
129
240
|
// Clean up the consumed bridge claim
|
|
@@ -135,12 +246,11 @@ export class BotFactory {
|
|
|
135
246
|
|
|
136
247
|
private async setupTestAccount() {
|
|
137
248
|
const [initialAccountData] = await getInitialTestAccountsData();
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
const accountManager = await this.wallet.createAccount(accountData);
|
|
249
|
+
const accountManager = await this.wallet.createSchnorrAccount(
|
|
250
|
+
initialAccountData.secret,
|
|
251
|
+
initialAccountData.salt,
|
|
252
|
+
initialAccountData.signingKey,
|
|
253
|
+
);
|
|
144
254
|
return accountManager.address;
|
|
145
255
|
}
|
|
146
256
|
|
|
@@ -151,33 +261,51 @@ export class BotFactory {
|
|
|
151
261
|
*/
|
|
152
262
|
private async setupToken(sender: AztecAddress): Promise<TokenContract | PrivateTokenContract> {
|
|
153
263
|
let deploy: DeployMethod<TokenContract | PrivateTokenContract>;
|
|
264
|
+
let tokenInstance: ContractInstanceWithAddress | undefined;
|
|
154
265
|
const deployOpts: DeployOptions = {
|
|
155
266
|
from: sender,
|
|
156
267
|
contractAddressSalt: this.config.tokenSalt,
|
|
157
268
|
universalDeploy: true,
|
|
158
269
|
};
|
|
270
|
+
let token: TokenContract | PrivateTokenContract;
|
|
159
271
|
if (this.config.contract === SupportedTokenContracts.TokenContract) {
|
|
160
272
|
deploy = TokenContract.deploy(this.wallet, sender, 'BotToken', 'BOT', 18);
|
|
273
|
+
tokenInstance = await deploy.getInstance(deployOpts);
|
|
274
|
+
token = TokenContract.at(tokenInstance.address, this.wallet);
|
|
161
275
|
} else if (this.config.contract === SupportedTokenContracts.PrivateTokenContract) {
|
|
162
|
-
|
|
276
|
+
// Generate keys for the contract since PrivateToken uses SinglePrivateMutable which requires keys
|
|
277
|
+
const tokenSecretKey = Fr.random();
|
|
278
|
+
const tokenPublicKeys = (await deriveKeys(tokenSecretKey)).publicKeys;
|
|
279
|
+
deploy = PrivateTokenContract.deployWithPublicKeys(tokenPublicKeys, this.wallet, MINT_BALANCE, sender);
|
|
163
280
|
deployOpts.skipInstancePublication = true;
|
|
164
281
|
deployOpts.skipClassPublication = true;
|
|
165
282
|
deployOpts.skipInitialization = false;
|
|
283
|
+
|
|
284
|
+
// Register the contract with the secret key before deployment
|
|
285
|
+
tokenInstance = await deploy.getInstance(deployOpts);
|
|
286
|
+
token = PrivateTokenContract.at(tokenInstance.address, this.wallet);
|
|
287
|
+
await this.wallet.registerContract(tokenInstance, PrivateTokenContract.artifact, tokenSecretKey);
|
|
288
|
+
// The contract constructor initializes private storage vars that need the contract's own nullifier key.
|
|
289
|
+
deployOpts.additionalScopes = [tokenInstance.address];
|
|
166
290
|
} else {
|
|
167
291
|
throw new Error(`Unsupported token contract type: ${this.config.contract}`);
|
|
168
292
|
}
|
|
169
293
|
|
|
170
|
-
const address = (await deploy.getInstance(deployOpts)).address;
|
|
171
|
-
|
|
294
|
+
const address = tokenInstance?.address ?? (await deploy.getInstance(deployOpts)).address;
|
|
295
|
+
const metadata = await this.wallet.getContractMetadata(address);
|
|
296
|
+
if (metadata.isContractPublished) {
|
|
172
297
|
this.log.info(`Token at ${address.toString()} already deployed`);
|
|
173
|
-
|
|
298
|
+
await deploy.register();
|
|
174
299
|
} else {
|
|
175
300
|
this.log.info(`Deploying token contract at ${address.toString()}`);
|
|
176
|
-
const
|
|
177
|
-
const txHash = await sentTx.getTxHash();
|
|
301
|
+
const txHash = await deploy.send({ ...deployOpts, wait: NO_WAIT });
|
|
178
302
|
this.log.info(`Sent tx for token setup with hash ${txHash.toString()}`);
|
|
179
|
-
|
|
303
|
+
await this.withNoMinTxsPerBlock(async () => {
|
|
304
|
+
await waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
305
|
+
return token;
|
|
306
|
+
});
|
|
180
307
|
}
|
|
308
|
+
return token;
|
|
181
309
|
}
|
|
182
310
|
|
|
183
311
|
/**
|
|
@@ -185,7 +313,7 @@ export class BotFactory {
|
|
|
185
313
|
* @param wallet - Wallet to deploy the token contract from.
|
|
186
314
|
* @returns The TokenContract instance.
|
|
187
315
|
*/
|
|
188
|
-
private setupTokenContract(
|
|
316
|
+
private async setupTokenContract(
|
|
189
317
|
deployer: AztecAddress,
|
|
190
318
|
contractAddressSalt: Fr,
|
|
191
319
|
name: string,
|
|
@@ -194,7 +322,8 @@ export class BotFactory {
|
|
|
194
322
|
): Promise<TokenContract> {
|
|
195
323
|
const deployOpts: DeployOptions = { from: deployer, contractAddressSalt, universalDeploy: true };
|
|
196
324
|
const deploy = TokenContract.deploy(this.wallet, deployer, name, ticker, decimals);
|
|
197
|
-
|
|
325
|
+
const instance = await this.registerOrDeployContract('Token - ' + name, deploy, deployOpts);
|
|
326
|
+
return TokenContract.at(instance.address, this.wallet);
|
|
198
327
|
}
|
|
199
328
|
|
|
200
329
|
private async setupAmmContract(
|
|
@@ -206,12 +335,14 @@ export class BotFactory {
|
|
|
206
335
|
): Promise<AMMContract> {
|
|
207
336
|
const deployOpts: DeployOptions = { from: deployer, contractAddressSalt, universalDeploy: true };
|
|
208
337
|
const deploy = AMMContract.deploy(this.wallet, token0.address, token1.address, lpToken.address);
|
|
209
|
-
const
|
|
338
|
+
const instance = await this.registerOrDeployContract('AMM', deploy, deployOpts);
|
|
339
|
+
const amm = AMMContract.at(instance.address, this.wallet);
|
|
210
340
|
|
|
211
341
|
this.log.info(`AMM deployed at ${amm.address}`);
|
|
212
|
-
const
|
|
213
|
-
|
|
214
|
-
|
|
342
|
+
const minterReceipt = await lpToken.methods
|
|
343
|
+
.set_minter(amm.address, true)
|
|
344
|
+
.send({ from: deployer, wait: { timeout: this.config.txMinedWaitSeconds } });
|
|
345
|
+
this.log.info(`Set LP token minter to AMM txHash=${minterReceipt.txHash.toString()}`);
|
|
215
346
|
this.log.info(`Liquidity token initialized`);
|
|
216
347
|
|
|
217
348
|
return amm;
|
|
@@ -270,23 +401,22 @@ export class BotFactory {
|
|
|
270
401
|
.getFunctionCall(),
|
|
271
402
|
});
|
|
272
403
|
|
|
273
|
-
const
|
|
404
|
+
const mintReceipt = await new BatchCall(this.wallet, [
|
|
274
405
|
token0.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
|
|
275
406
|
token1.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
|
|
276
|
-
]).send({ from: liquidityProvider });
|
|
407
|
+
]).send({ from: liquidityProvider, wait: { timeout: this.config.txMinedWaitSeconds } });
|
|
277
408
|
|
|
278
|
-
this.log.info(`Sent mint tx: ${
|
|
279
|
-
await mintTx.wait({ timeout: this.config.txMinedWaitSeconds });
|
|
409
|
+
this.log.info(`Sent mint tx: ${mintReceipt.txHash.toString()}`);
|
|
280
410
|
|
|
281
|
-
const
|
|
411
|
+
const addLiquidityReceipt = await amm.methods
|
|
282
412
|
.add_liquidity(amount0Max, amount1Max, amount0Min, amount1Min, authwitNonce)
|
|
283
413
|
.send({
|
|
284
414
|
from: liquidityProvider,
|
|
285
415
|
authWitnesses: [token0Authwit, token1Authwit],
|
|
416
|
+
wait: { timeout: this.config.txMinedWaitSeconds },
|
|
286
417
|
});
|
|
287
418
|
|
|
288
|
-
this.log.info(`Sent tx to add liquidity to the AMM: ${
|
|
289
|
-
await addLiquidityTx.wait({ timeout: this.config.txMinedWaitSeconds });
|
|
419
|
+
this.log.info(`Sent tx to add liquidity to the AMM: ${addLiquidityReceipt.txHash.toString()}`);
|
|
290
420
|
this.log.info(`Liquidity added`);
|
|
291
421
|
|
|
292
422
|
const [newT0Bal, newT1Bal, newLPBal] = await getPrivateBalances();
|
|
@@ -299,18 +429,22 @@ export class BotFactory {
|
|
|
299
429
|
name: string,
|
|
300
430
|
deploy: DeployMethod<T>,
|
|
301
431
|
deployOpts: DeployOptions,
|
|
302
|
-
): Promise<
|
|
303
|
-
const
|
|
304
|
-
|
|
432
|
+
): Promise<ContractInstanceWithAddress> {
|
|
433
|
+
const instance = await deploy.getInstance(deployOpts);
|
|
434
|
+
const address = instance.address;
|
|
435
|
+
const metadata = await this.wallet.getContractMetadata(address);
|
|
436
|
+
if (metadata.isContractPublished) {
|
|
305
437
|
this.log.info(`Contract ${name} at ${address.toString()} already deployed`);
|
|
306
|
-
|
|
438
|
+
await deploy.register();
|
|
307
439
|
} else {
|
|
308
440
|
this.log.info(`Deploying contract ${name} at ${address.toString()}`);
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
441
|
+
await this.withNoMinTxsPerBlock(async () => {
|
|
442
|
+
const txHash = await deploy.send({ ...deployOpts, wait: NO_WAIT });
|
|
443
|
+
this.log.info(`Sent contract ${name} setup tx with hash ${txHash.toString()}`);
|
|
444
|
+
return waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
445
|
+
});
|
|
313
446
|
}
|
|
447
|
+
return instance;
|
|
314
448
|
}
|
|
315
449
|
|
|
316
450
|
/**
|
|
@@ -346,10 +480,14 @@ export class BotFactory {
|
|
|
346
480
|
this.log.info(`Skipping minting as ${minter.toString()} has enough tokens`);
|
|
347
481
|
return;
|
|
348
482
|
}
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
await this.withNoMinTxsPerBlock(() =>
|
|
483
|
+
|
|
484
|
+
// PrivateToken's mint accesses contract-level private storage vars (admin, total_supply).
|
|
485
|
+
const additionalScopes = isStandardToken ? undefined : [token.address];
|
|
486
|
+
await this.withNoMinTxsPerBlock(async () => {
|
|
487
|
+
const txHash = await new BatchCall(token.wallet, calls).send({ from: minter, additionalScopes, wait: NO_WAIT });
|
|
488
|
+
this.log.info(`Sent token mint tx with hash ${txHash.toString()}`);
|
|
489
|
+
return waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
490
|
+
});
|
|
353
491
|
}
|
|
354
492
|
|
|
355
493
|
/**
|