@aztec/bot 0.0.0-test.1 → 0.0.1-commit.001888fc
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 +32 -0
- package/dest/amm_bot.d.ts.map +1 -0
- package/dest/amm_bot.js +108 -0
- package/dest/base_bot.d.ts +21 -0
- package/dest/base_bot.d.ts.map +1 -0
- package/dest/base_bot.js +79 -0
- package/dest/bot.d.ts +13 -18
- package/dest/bot.d.ts.map +1 -1
- package/dest/bot.js +26 -84
- package/dest/config.d.ts +106 -66
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +89 -39
- 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 +43 -29
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +380 -139
- package/dest/index.d.ts +5 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +4 -1
- package/dest/interface.d.ts +12 -1
- package/dest/interface.d.ts.map +1 -1
- package/dest/interface.js +5 -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/rpc.d.ts +1 -7
- package/dest/rpc.d.ts.map +1 -1
- package/dest/rpc.js +0 -11
- package/dest/runner.d.ts +15 -11
- package/dest/runner.d.ts.map +1 -1
- package/dest/runner.js +457 -51
- package/dest/store/bot_store.d.ts +69 -0
- package/dest/store/bot_store.d.ts.map +1 -0
- package/dest/store/bot_store.js +138 -0
- package/dest/store/index.d.ts +2 -0
- package/dest/store/index.d.ts.map +1 -0
- package/dest/store/index.js +1 -0
- package/dest/utils.d.ts +8 -5
- package/dest/utils.d.ts.map +1 -1
- package/dest/utils.js +14 -5
- package/package.json +30 -23
- package/src/amm_bot.ts +129 -0
- package/src/base_bot.ts +82 -0
- package/src/bot.ts +52 -101
- package/src/config.ts +129 -71
- package/src/cross_chain_bot.ts +203 -0
- package/src/factory.ts +476 -152
- package/src/index.ts +4 -1
- package/src/interface.ts +9 -0
- package/src/l1_to_l2_seeding.ts +79 -0
- package/src/rpc.ts +0 -13
- package/src/runner.ts +51 -21
- package/src/store/bot_store.ts +196 -0
- package/src/store/index.ts +1 -0
- package/src/utils.ts +17 -6
package/src/factory.ts
CHANGED
|
@@ -1,72 +1,190 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { getInitialTestAccountsData } from '@aztec/accounts/testing';
|
|
2
|
+
import { AztecAddress } from '@aztec/aztec.js/addresses';
|
|
3
3
|
import {
|
|
4
|
-
type AccountWallet,
|
|
5
|
-
AztecAddress,
|
|
6
|
-
type AztecNode,
|
|
7
4
|
BatchCall,
|
|
5
|
+
ContractBase,
|
|
6
|
+
ContractFunctionInteraction,
|
|
8
7
|
type DeployMethod,
|
|
9
8
|
type DeployOptions,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
} from '@aztec/aztec.js';
|
|
17
|
-
import {
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
9
|
+
NO_WAIT,
|
|
10
|
+
} from '@aztec/aztec.js/contracts';
|
|
11
|
+
import type { L2AmountClaim } from '@aztec/aztec.js/ethereum';
|
|
12
|
+
import { L1FeeJuicePortalManager } from '@aztec/aztec.js/ethereum';
|
|
13
|
+
import { FeeJuicePaymentMethodWithClaim } from '@aztec/aztec.js/fee';
|
|
14
|
+
import { deriveKeys } from '@aztec/aztec.js/keys';
|
|
15
|
+
import { createLogger } from '@aztec/aztec.js/log';
|
|
16
|
+
import { waitForL1ToL2MessageReady } from '@aztec/aztec.js/messaging';
|
|
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';
|
|
24
|
+
import { Timer } from '@aztec/foundation/timer';
|
|
25
|
+
import { AMMContract } from '@aztec/noir-contracts.js/AMM';
|
|
26
|
+
import { PrivateTokenContract } from '@aztec/noir-contracts.js/PrivateToken';
|
|
20
27
|
import { TokenContract } from '@aztec/noir-contracts.js/Token';
|
|
21
|
-
import
|
|
28
|
+
import { TestContract } from '@aztec/noir-test-contracts.js/Test';
|
|
29
|
+
import type { ContractInstanceWithAddress } from '@aztec/stdlib/contract';
|
|
30
|
+
import { GasSettings } from '@aztec/stdlib/gas';
|
|
31
|
+
import type { AztecNode, AztecNodeAdmin } from '@aztec/stdlib/interfaces/client';
|
|
22
32
|
import { deriveSigningKey } from '@aztec/stdlib/keys';
|
|
23
|
-
import {
|
|
33
|
+
import { EmbeddedWallet } from '@aztec/wallets/embedded';
|
|
24
34
|
|
|
25
|
-
import { type BotConfig, SupportedTokenContracts
|
|
35
|
+
import { type BotConfig, SupportedTokenContracts } from './config.js';
|
|
36
|
+
import { seedL1ToL2Message } from './l1_to_l2_seeding.js';
|
|
37
|
+
import type { BotStore } from './store/index.js';
|
|
26
38
|
import { getBalances, getPrivateBalance, isStandardTokenContract } from './utils.js';
|
|
27
39
|
|
|
28
40
|
const MINT_BALANCE = 1e12;
|
|
29
41
|
const MIN_BALANCE = 1e3;
|
|
30
42
|
|
|
31
43
|
export class BotFactory {
|
|
32
|
-
private pxe: PXE;
|
|
33
|
-
private node?: AztecNode;
|
|
34
44
|
private log = createLogger('bot');
|
|
35
45
|
|
|
36
|
-
constructor(
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
46
|
+
constructor(
|
|
47
|
+
private readonly config: BotConfig,
|
|
48
|
+
private readonly wallet: EmbeddedWallet,
|
|
49
|
+
private readonly store: BotStore,
|
|
50
|
+
private readonly aztecNode: AztecNode,
|
|
51
|
+
private readonly aztecNodeAdmin?: AztecNodeAdmin,
|
|
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
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Initializes a new bot by setting up the sender account, registering the recipient,
|
|
60
|
+
* deploying the token contract, and minting tokens if necessary.
|
|
61
|
+
*/
|
|
62
|
+
public async setup(): Promise<{
|
|
63
|
+
wallet: EmbeddedWallet;
|
|
64
|
+
defaultAccountAddress: AztecAddress;
|
|
65
|
+
token: TokenContract | PrivateTokenContract;
|
|
66
|
+
node: AztecNode;
|
|
67
|
+
recipient: AztecAddress;
|
|
68
|
+
}> {
|
|
69
|
+
const defaultAccountAddress = await this.setupAccount();
|
|
70
|
+
const recipient = (await this.wallet.createSchnorrAccount(Fr.random(), Fr.random())).address;
|
|
71
|
+
const token = await this.setupToken(defaultAccountAddress);
|
|
72
|
+
await this.mintTokens(token, defaultAccountAddress);
|
|
73
|
+
return { wallet: this.wallet, defaultAccountAddress, token, node: this.aztecNode, recipient };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
public async setupAmm(): Promise<{
|
|
77
|
+
wallet: EmbeddedWallet;
|
|
78
|
+
defaultAccountAddress: AztecAddress;
|
|
79
|
+
amm: AMMContract;
|
|
80
|
+
token0: TokenContract;
|
|
81
|
+
token1: TokenContract;
|
|
82
|
+
node: AztecNode;
|
|
83
|
+
}> {
|
|
84
|
+
const defaultAccountAddress = await this.setupAccount();
|
|
85
|
+
const token0 = await this.setupTokenContract(defaultAccountAddress, this.config.tokenSalt, 'BotToken0', 'BOT0');
|
|
86
|
+
const token1 = await this.setupTokenContract(defaultAccountAddress, this.config.tokenSalt, 'BotToken1', 'BOT1');
|
|
87
|
+
const liquidityToken = await this.setupTokenContract(
|
|
88
|
+
defaultAccountAddress,
|
|
89
|
+
this.config.tokenSalt,
|
|
90
|
+
'BotLPToken',
|
|
91
|
+
'BOTLP',
|
|
92
|
+
);
|
|
93
|
+
const amm = await this.setupAmmContract(
|
|
94
|
+
defaultAccountAddress,
|
|
95
|
+
this.config.tokenSalt,
|
|
96
|
+
token0,
|
|
97
|
+
token1,
|
|
98
|
+
liquidityToken,
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
await this.fundAmm(defaultAccountAddress, defaultAccountAddress, amm, token0, token1, liquidityToken);
|
|
102
|
+
this.log.info(`AMM initialized and funded`);
|
|
103
|
+
|
|
104
|
+
return { wallet: this.wallet, defaultAccountAddress, amm, token0, token1, node: this.aztecNode };
|
|
105
|
+
}
|
|
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');
|
|
44
125
|
}
|
|
45
|
-
|
|
46
|
-
|
|
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');
|
|
47
129
|
}
|
|
130
|
+
const { l1ChainId, l1ContractAddresses } = await this.aztecNode.getNodeInfo();
|
|
131
|
+
const chain = createEthereumChain(l1RpcUrls, l1ChainId);
|
|
132
|
+
const l1Client = createExtendedL1Client(chain.rpcUrls, mnemonicOrPrivateKey, chain.chainInfo);
|
|
48
133
|
|
|
49
|
-
|
|
134
|
+
// Fetch Rollup version (needed for Inbox L2Actor struct)
|
|
135
|
+
const rollupContract = new RollupContract(l1Client, l1ContractAddresses.rollupAddress.toString());
|
|
136
|
+
const rollupVersion = await rollupContract.getVersion();
|
|
50
137
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
|
+
);
|
|
55
156
|
}
|
|
56
|
-
|
|
57
|
-
|
|
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
|
+
};
|
|
58
177
|
}
|
|
59
178
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
const
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
return { wallet, token, pxe: this.pxe, recipient };
|
|
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);
|
|
70
188
|
}
|
|
71
189
|
|
|
72
190
|
/**
|
|
@@ -74,61 +192,65 @@ export class BotFactory {
|
|
|
74
192
|
* @returns The sender wallet.
|
|
75
193
|
*/
|
|
76
194
|
private async setupAccount() {
|
|
77
|
-
|
|
78
|
-
|
|
195
|
+
const privateKey = this.config.senderPrivateKey?.getValue();
|
|
196
|
+
if (privateKey) {
|
|
197
|
+
this.log.info(`Setting up account with provided private key`);
|
|
198
|
+
return await this.setupAccountWithPrivateKey(privateKey);
|
|
79
199
|
} else {
|
|
200
|
+
this.log.info(`Setting up test account`);
|
|
80
201
|
return await this.setupTestAccount();
|
|
81
202
|
}
|
|
82
203
|
}
|
|
83
204
|
|
|
84
|
-
private async setupAccountWithPrivateKey(
|
|
85
|
-
const salt = Fr.ONE;
|
|
86
|
-
const signingKey = deriveSigningKey(
|
|
87
|
-
const
|
|
88
|
-
const
|
|
89
|
-
if (
|
|
90
|
-
this.log.info(`Account at ${
|
|
91
|
-
const
|
|
92
|
-
|
|
205
|
+
private async setupAccountWithPrivateKey(secret: Fr) {
|
|
206
|
+
const salt = this.config.senderSalt ?? Fr.ONE;
|
|
207
|
+
const signingKey = deriveSigningKey(secret);
|
|
208
|
+
const accountManager = await this.wallet.createSchnorrAccount(secret, salt, signingKey);
|
|
209
|
+
const metadata = await this.wallet.getContractMetadata(accountManager.address);
|
|
210
|
+
if (metadata.isContractInitialized) {
|
|
211
|
+
this.log.info(`Account at ${accountManager.address.toString()} already initialized`);
|
|
212
|
+
const timer = new Timer();
|
|
213
|
+
const address = accountManager.address;
|
|
214
|
+
this.log.info(`Account at ${address} registered. duration=${timer.ms()}`);
|
|
215
|
+
await this.store.deleteBridgeClaim(address);
|
|
216
|
+
return address;
|
|
93
217
|
} else {
|
|
94
|
-
const address =
|
|
218
|
+
const address = accountManager.address;
|
|
95
219
|
this.log.info(`Deploying account at ${address}`);
|
|
96
220
|
|
|
97
|
-
const claim = await this.
|
|
221
|
+
const claim = await this.getOrCreateBridgeClaim(address);
|
|
222
|
+
|
|
223
|
+
const paymentMethod = new FeeJuicePaymentMethodWithClaim(accountManager.address, claim);
|
|
224
|
+
const deployMethod = await accountManager.getDeployMethod();
|
|
225
|
+
const maxFeesPerGas = (await this.aztecNode.getCurrentMinFees()).mul(1 + this.config.minFeePadding);
|
|
226
|
+
const gasSettings = GasSettings.default({ maxFeesPerGas });
|
|
98
227
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
228
|
+
await this.withNoMinTxsPerBlock(async () => {
|
|
229
|
+
const { txHash } = await deployMethod.send({
|
|
230
|
+
from: AztecAddress.ZERO,
|
|
231
|
+
fee: { gasSettings, paymentMethod },
|
|
232
|
+
wait: NO_WAIT,
|
|
233
|
+
});
|
|
234
|
+
this.log.info(`Sent tx for account deployment with hash ${txHash.toString()}`);
|
|
235
|
+
return waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
236
|
+
});
|
|
107
237
|
this.log.info(`Account deployed at ${address}`);
|
|
108
|
-
return wallet;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
238
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
} else {
|
|
117
|
-
this.log.info('Registering funded test account');
|
|
118
|
-
const [account] = await getInitialTestAccounts();
|
|
119
|
-
const manager = await getSchnorrAccount(this.pxe, account.secret, account.signingKey, account.salt);
|
|
120
|
-
wallet = await manager.register();
|
|
121
|
-
this.log.info(`Funded test account registered: ${wallet.getAddress()}`);
|
|
239
|
+
// Clean up the consumed bridge claim
|
|
240
|
+
await this.store.deleteBridgeClaim(address);
|
|
241
|
+
|
|
242
|
+
return accountManager.address;
|
|
122
243
|
}
|
|
123
|
-
return wallet;
|
|
124
244
|
}
|
|
125
245
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
246
|
+
private async setupTestAccount() {
|
|
247
|
+
const [initialAccountData] = await getInitialTestAccountsData();
|
|
248
|
+
const accountManager = await this.wallet.createSchnorrAccount(
|
|
249
|
+
initialAccountData.secret,
|
|
250
|
+
initialAccountData.salt,
|
|
251
|
+
initialAccountData.signingKey,
|
|
252
|
+
);
|
|
253
|
+
return accountManager.address;
|
|
132
254
|
}
|
|
133
255
|
|
|
134
256
|
/**
|
|
@@ -136,124 +258,326 @@ export class BotFactory {
|
|
|
136
258
|
* @param wallet - Wallet to deploy the token contract from.
|
|
137
259
|
* @returns The TokenContract instance.
|
|
138
260
|
*/
|
|
139
|
-
private async setupToken(
|
|
140
|
-
let deploy: DeployMethod<TokenContract |
|
|
141
|
-
|
|
261
|
+
private async setupToken(sender: AztecAddress): Promise<TokenContract | PrivateTokenContract> {
|
|
262
|
+
let deploy: DeployMethod<TokenContract | PrivateTokenContract>;
|
|
263
|
+
let tokenInstance: ContractInstanceWithAddress | undefined;
|
|
264
|
+
const deployOpts: DeployOptions = {
|
|
265
|
+
from: sender,
|
|
266
|
+
contractAddressSalt: this.config.tokenSalt,
|
|
267
|
+
universalDeploy: true,
|
|
268
|
+
};
|
|
269
|
+
let token: TokenContract | PrivateTokenContract;
|
|
142
270
|
if (this.config.contract === SupportedTokenContracts.TokenContract) {
|
|
143
|
-
deploy = TokenContract.deploy(wallet,
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
271
|
+
deploy = TokenContract.deploy(this.wallet, sender, 'BotToken', 'BOT', 18);
|
|
272
|
+
tokenInstance = await deploy.getInstance(deployOpts);
|
|
273
|
+
token = TokenContract.at(tokenInstance.address, this.wallet);
|
|
274
|
+
} else if (this.config.contract === SupportedTokenContracts.PrivateTokenContract) {
|
|
275
|
+
// Generate keys for the contract since PrivateToken uses SinglePrivateMutable which requires keys
|
|
276
|
+
const tokenSecretKey = Fr.random();
|
|
277
|
+
const tokenPublicKeys = (await deriveKeys(tokenSecretKey)).publicKeys;
|
|
278
|
+
deploy = PrivateTokenContract.deployWithPublicKeys(tokenPublicKeys, this.wallet, MINT_BALANCE, sender);
|
|
279
|
+
deployOpts.skipInstancePublication = true;
|
|
280
|
+
deployOpts.skipClassPublication = true;
|
|
148
281
|
deployOpts.skipInitialization = false;
|
|
149
|
-
|
|
282
|
+
|
|
283
|
+
// Register the contract with the secret key before deployment
|
|
284
|
+
tokenInstance = await deploy.getInstance(deployOpts);
|
|
285
|
+
token = PrivateTokenContract.at(tokenInstance.address, this.wallet);
|
|
286
|
+
await this.wallet.registerContract(tokenInstance, PrivateTokenContract.artifact, tokenSecretKey);
|
|
287
|
+
// The contract constructor initializes private storage vars that need the contract's own nullifier key.
|
|
288
|
+
deployOpts.additionalScopes = [tokenInstance.address];
|
|
150
289
|
} else {
|
|
151
290
|
throw new Error(`Unsupported token contract type: ${this.config.contract}`);
|
|
152
291
|
}
|
|
153
292
|
|
|
154
|
-
const address = (await deploy.getInstance(deployOpts)).address;
|
|
155
|
-
|
|
293
|
+
const address = tokenInstance?.address ?? (await deploy.getInstance(deployOpts)).address;
|
|
294
|
+
const metadata = await this.wallet.getContractMetadata(address);
|
|
295
|
+
if (metadata.isContractPublished) {
|
|
156
296
|
this.log.info(`Token at ${address.toString()} already deployed`);
|
|
157
|
-
|
|
297
|
+
await deploy.register();
|
|
158
298
|
} else {
|
|
159
299
|
this.log.info(`Deploying token contract at ${address.toString()}`);
|
|
160
|
-
const
|
|
161
|
-
|
|
162
|
-
this.
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
300
|
+
const { txHash } = await deploy.send({ ...deployOpts, wait: NO_WAIT });
|
|
301
|
+
this.log.info(`Sent tx for token setup with hash ${txHash.toString()}`);
|
|
302
|
+
await this.withNoMinTxsPerBlock(async () => {
|
|
303
|
+
await waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
304
|
+
return token;
|
|
305
|
+
});
|
|
166
306
|
}
|
|
307
|
+
return token;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Checks if the token contract is deployed and deploys it if necessary.
|
|
312
|
+
* @param wallet - Wallet to deploy the token contract from.
|
|
313
|
+
* @returns The TokenContract instance.
|
|
314
|
+
*/
|
|
315
|
+
private async setupTokenContract(
|
|
316
|
+
deployer: AztecAddress,
|
|
317
|
+
contractAddressSalt: Fr,
|
|
318
|
+
name: string,
|
|
319
|
+
ticker: string,
|
|
320
|
+
decimals = 18,
|
|
321
|
+
): Promise<TokenContract> {
|
|
322
|
+
const deployOpts: DeployOptions = { from: deployer, contractAddressSalt, universalDeploy: true };
|
|
323
|
+
const deploy = TokenContract.deploy(this.wallet, deployer, name, ticker, decimals);
|
|
324
|
+
const instance = await this.registerOrDeployContract('Token - ' + name, deploy, deployOpts);
|
|
325
|
+
return TokenContract.at(instance.address, this.wallet);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
private async setupAmmContract(
|
|
329
|
+
deployer: AztecAddress,
|
|
330
|
+
contractAddressSalt: Fr,
|
|
331
|
+
token0: TokenContract,
|
|
332
|
+
token1: TokenContract,
|
|
333
|
+
lpToken: TokenContract,
|
|
334
|
+
): Promise<AMMContract> {
|
|
335
|
+
const deployOpts: DeployOptions = { from: deployer, contractAddressSalt, universalDeploy: true };
|
|
336
|
+
const deploy = AMMContract.deploy(this.wallet, token0.address, token1.address, lpToken.address);
|
|
337
|
+
const instance = await this.registerOrDeployContract('AMM', deploy, deployOpts);
|
|
338
|
+
const amm = AMMContract.at(instance.address, this.wallet);
|
|
339
|
+
|
|
340
|
+
this.log.info(`AMM deployed at ${amm.address}`);
|
|
341
|
+
const { receipt: minterReceipt } = await lpToken.methods
|
|
342
|
+
.set_minter(amm.address, true)
|
|
343
|
+
.send({ from: deployer, wait: { timeout: this.config.txMinedWaitSeconds } });
|
|
344
|
+
this.log.info(`Set LP token minter to AMM txHash=${minterReceipt.txHash.toString()}`);
|
|
345
|
+
this.log.info(`Liquidity token initialized`);
|
|
346
|
+
|
|
347
|
+
return amm;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
private async fundAmm(
|
|
351
|
+
defaultAccountAddress: AztecAddress,
|
|
352
|
+
liquidityProvider: AztecAddress,
|
|
353
|
+
amm: AMMContract,
|
|
354
|
+
token0: TokenContract,
|
|
355
|
+
token1: TokenContract,
|
|
356
|
+
lpToken: TokenContract,
|
|
357
|
+
): Promise<void> {
|
|
358
|
+
const getPrivateBalances = () =>
|
|
359
|
+
Promise.all([
|
|
360
|
+
token0.methods
|
|
361
|
+
.balance_of_private(liquidityProvider)
|
|
362
|
+
.simulate({ from: liquidityProvider })
|
|
363
|
+
.then(r => r.result),
|
|
364
|
+
token1.methods
|
|
365
|
+
.balance_of_private(liquidityProvider)
|
|
366
|
+
.simulate({ from: liquidityProvider })
|
|
367
|
+
.then(r => r.result),
|
|
368
|
+
lpToken.methods
|
|
369
|
+
.balance_of_private(liquidityProvider)
|
|
370
|
+
.simulate({ from: liquidityProvider })
|
|
371
|
+
.then(r => r.result),
|
|
372
|
+
]);
|
|
373
|
+
|
|
374
|
+
const authwitNonce = Fr.random();
|
|
375
|
+
|
|
376
|
+
// keep some tokens for swapping
|
|
377
|
+
const amount0Max = MINT_BALANCE / 2;
|
|
378
|
+
const amount0Min = MINT_BALANCE / 4;
|
|
379
|
+
const amount1Max = MINT_BALANCE / 2;
|
|
380
|
+
const amount1Min = MINT_BALANCE / 4;
|
|
381
|
+
|
|
382
|
+
const [t0Bal, t1Bal, lpBal] = await getPrivateBalances();
|
|
383
|
+
|
|
384
|
+
this.log.info(
|
|
385
|
+
`Minting ${MINT_BALANCE} tokens of each BotToken0 and BotToken1. Current private balances of ${liquidityProvider}: token0=${t0Bal}, token1=${t1Bal}, lp=${lpBal}`,
|
|
386
|
+
);
|
|
387
|
+
|
|
388
|
+
// Add authwitnesses for the transfers in AMM::add_liquidity function
|
|
389
|
+
const token0Authwit = await this.wallet.createAuthWit(defaultAccountAddress, {
|
|
390
|
+
caller: amm.address,
|
|
391
|
+
call: await token0.methods
|
|
392
|
+
.transfer_to_public_and_prepare_private_balance_increase(
|
|
393
|
+
liquidityProvider,
|
|
394
|
+
amm.address,
|
|
395
|
+
amount0Max,
|
|
396
|
+
authwitNonce,
|
|
397
|
+
)
|
|
398
|
+
.getFunctionCall(),
|
|
399
|
+
});
|
|
400
|
+
const token1Authwit = await this.wallet.createAuthWit(defaultAccountAddress, {
|
|
401
|
+
caller: amm.address,
|
|
402
|
+
call: await token1.methods
|
|
403
|
+
.transfer_to_public_and_prepare_private_balance_increase(
|
|
404
|
+
liquidityProvider,
|
|
405
|
+
amm.address,
|
|
406
|
+
amount1Max,
|
|
407
|
+
authwitNonce,
|
|
408
|
+
)
|
|
409
|
+
.getFunctionCall(),
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
const { receipt: mintReceipt } = await new BatchCall(this.wallet, [
|
|
413
|
+
token0.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
|
|
414
|
+
token1.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
|
|
415
|
+
]).send({ from: liquidityProvider, wait: { timeout: this.config.txMinedWaitSeconds } });
|
|
416
|
+
|
|
417
|
+
this.log.info(`Sent mint tx: ${mintReceipt.txHash.toString()}`);
|
|
418
|
+
|
|
419
|
+
const { receipt: addLiquidityReceipt } = await amm.methods
|
|
420
|
+
.add_liquidity(amount0Max, amount1Max, amount0Min, amount1Min, authwitNonce)
|
|
421
|
+
.send({
|
|
422
|
+
from: liquidityProvider,
|
|
423
|
+
authWitnesses: [token0Authwit, token1Authwit],
|
|
424
|
+
wait: { timeout: this.config.txMinedWaitSeconds },
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
this.log.info(`Sent tx to add liquidity to the AMM: ${addLiquidityReceipt.txHash.toString()}`);
|
|
428
|
+
this.log.info(`Liquidity added`);
|
|
429
|
+
|
|
430
|
+
const [newT0Bal, newT1Bal, newLPBal] = await getPrivateBalances();
|
|
431
|
+
this.log.info(
|
|
432
|
+
`Updated private balances of ${defaultAccountAddress} after minting and funding AMM: token0=${newT0Bal}, token1=${newT1Bal}, lp=${newLPBal}`,
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
private async registerOrDeployContract<T extends ContractBase>(
|
|
437
|
+
name: string,
|
|
438
|
+
deploy: DeployMethod<T>,
|
|
439
|
+
deployOpts: DeployOptions,
|
|
440
|
+
): Promise<ContractInstanceWithAddress> {
|
|
441
|
+
const instance = await deploy.getInstance(deployOpts);
|
|
442
|
+
const address = instance.address;
|
|
443
|
+
const metadata = await this.wallet.getContractMetadata(address);
|
|
444
|
+
if (metadata.isContractPublished) {
|
|
445
|
+
this.log.info(`Contract ${name} at ${address.toString()} already deployed`);
|
|
446
|
+
await deploy.register();
|
|
447
|
+
} else {
|
|
448
|
+
this.log.info(`Deploying contract ${name} at ${address.toString()}`);
|
|
449
|
+
await this.withNoMinTxsPerBlock(async () => {
|
|
450
|
+
const { txHash } = await deploy.send({ ...deployOpts, wait: NO_WAIT });
|
|
451
|
+
this.log.info(`Sent contract ${name} setup tx with hash ${txHash.toString()}`);
|
|
452
|
+
return waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
return instance;
|
|
167
456
|
}
|
|
168
457
|
|
|
169
458
|
/**
|
|
170
459
|
* Mints private and public tokens for the sender if their balance is below the minimum.
|
|
171
460
|
* @param token - Token contract.
|
|
172
461
|
*/
|
|
173
|
-
private async mintTokens(token: TokenContract |
|
|
174
|
-
const sender = token.wallet.getAddress();
|
|
462
|
+
private async mintTokens(token: TokenContract | PrivateTokenContract, minter: AztecAddress) {
|
|
175
463
|
const isStandardToken = isStandardTokenContract(token);
|
|
176
464
|
let privateBalance = 0n;
|
|
177
465
|
let publicBalance = 0n;
|
|
178
466
|
|
|
179
467
|
if (isStandardToken) {
|
|
180
|
-
({ privateBalance, publicBalance } = await getBalances(token,
|
|
468
|
+
({ privateBalance, publicBalance } = await getBalances(token, minter));
|
|
181
469
|
} else {
|
|
182
|
-
privateBalance = await getPrivateBalance(token,
|
|
470
|
+
privateBalance = await getPrivateBalance(token, minter);
|
|
183
471
|
}
|
|
184
472
|
|
|
185
|
-
const calls:
|
|
473
|
+
const calls: ContractFunctionInteraction[] = [];
|
|
186
474
|
if (privateBalance < MIN_BALANCE) {
|
|
187
|
-
this.log.info(`Minting private tokens for ${
|
|
475
|
+
this.log.info(`Minting private tokens for ${minter.toString()}`);
|
|
188
476
|
|
|
189
|
-
const from = sender; // we are setting from to sender here because we need a sender to calculate the tag
|
|
190
477
|
calls.push(
|
|
191
478
|
isStandardToken
|
|
192
|
-
?
|
|
193
|
-
:
|
|
479
|
+
? token.methods.mint_to_private(minter, MINT_BALANCE)
|
|
480
|
+
: token.methods.mint(MINT_BALANCE, minter),
|
|
194
481
|
);
|
|
195
482
|
}
|
|
196
483
|
if (isStandardToken && publicBalance < MIN_BALANCE) {
|
|
197
|
-
this.log.info(`Minting public tokens for ${
|
|
198
|
-
calls.push(
|
|
484
|
+
this.log.info(`Minting public tokens for ${minter.toString()}`);
|
|
485
|
+
calls.push(token.methods.mint_to_public(minter, MINT_BALANCE));
|
|
199
486
|
}
|
|
200
487
|
if (calls.length === 0) {
|
|
201
|
-
this.log.info(`Skipping minting as ${
|
|
488
|
+
this.log.info(`Skipping minting as ${minter.toString()} has enough tokens`);
|
|
202
489
|
return;
|
|
203
490
|
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
await this.
|
|
208
|
-
|
|
209
|
-
|
|
491
|
+
|
|
492
|
+
// PrivateToken's mint accesses contract-level private storage vars (admin, total_supply).
|
|
493
|
+
const additionalScopes = isStandardToken ? undefined : [token.address];
|
|
494
|
+
await this.withNoMinTxsPerBlock(async () => {
|
|
495
|
+
const { txHash } = await new BatchCall(token.wallet, calls).send({
|
|
496
|
+
from: minter,
|
|
497
|
+
additionalScopes,
|
|
498
|
+
wait: NO_WAIT,
|
|
499
|
+
});
|
|
500
|
+
this.log.info(`Sent token mint tx with hash ${txHash.toString()}`);
|
|
501
|
+
return waitForTx(this.aztecNode, txHash, { timeout: this.config.txMinedWaitSeconds });
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* Gets or creates a bridge claim for the recipient.
|
|
507
|
+
* Checks if a claim already exists in the store and reuses it if valid.
|
|
508
|
+
* Only creates a new bridge if fee juice balance is below threshold.
|
|
509
|
+
*/
|
|
510
|
+
private async getOrCreateBridgeClaim(recipient: AztecAddress): Promise<L2AmountClaim> {
|
|
511
|
+
// Check if we have an existing claim in the store
|
|
512
|
+
const existingClaim = await this.store.getBridgeClaim(recipient);
|
|
513
|
+
if (existingClaim) {
|
|
514
|
+
this.log.info(`Found existing bridge claim for ${recipient.toString()}, checking validity...`);
|
|
515
|
+
|
|
516
|
+
// Check if the message is ready on L2
|
|
517
|
+
try {
|
|
518
|
+
const messageHash = Fr.fromHexString(existingClaim.claim.messageHash);
|
|
519
|
+
await this.withNoMinTxsPerBlock(() =>
|
|
520
|
+
waitForL1ToL2MessageReady(this.aztecNode, messageHash, {
|
|
521
|
+
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds,
|
|
522
|
+
}),
|
|
523
|
+
);
|
|
524
|
+
return existingClaim.claim;
|
|
525
|
+
} catch (err) {
|
|
526
|
+
this.log.warn(`Failed to verify existing claim, creating new one: ${err}`);
|
|
527
|
+
await this.store.deleteBridgeClaim(recipient);
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
const claim = await this.bridgeL1FeeJuice(recipient);
|
|
532
|
+
await this.store.saveBridgeClaim(recipient, claim);
|
|
533
|
+
|
|
534
|
+
return claim;
|
|
210
535
|
}
|
|
211
536
|
|
|
212
|
-
private async bridgeL1FeeJuice(recipient: AztecAddress
|
|
537
|
+
private async bridgeL1FeeJuice(recipient: AztecAddress): Promise<L2AmountClaim> {
|
|
213
538
|
const l1RpcUrls = this.config.l1RpcUrls;
|
|
214
539
|
if (!l1RpcUrls?.length) {
|
|
215
540
|
throw new Error('L1 Rpc url is required to bridge the fee juice to fund the deployment of the account.');
|
|
216
541
|
}
|
|
217
|
-
const mnemonicOrPrivateKey = this.config.l1PrivateKey
|
|
542
|
+
const mnemonicOrPrivateKey = this.config.l1PrivateKey?.getValue() ?? this.config.l1Mnemonic?.getValue();
|
|
218
543
|
if (!mnemonicOrPrivateKey) {
|
|
219
544
|
throw new Error(
|
|
220
545
|
'Either a mnemonic or private key of an L1 account is required to bridge the fee juice to fund the deployment of the account.',
|
|
221
546
|
);
|
|
222
547
|
}
|
|
223
548
|
|
|
224
|
-
const { l1ChainId } = await this.
|
|
549
|
+
const { l1ChainId } = await this.aztecNode.getNodeInfo();
|
|
225
550
|
const chain = createEthereumChain(l1RpcUrls, l1ChainId);
|
|
226
|
-
const
|
|
551
|
+
const extendedClient = createExtendedL1Client(chain.rpcUrls, mnemonicOrPrivateKey, chain.chainInfo);
|
|
227
552
|
|
|
228
|
-
const portal = await L1FeeJuicePortalManager.new(this.
|
|
229
|
-
const
|
|
553
|
+
const portal = await L1FeeJuicePortalManager.new(this.aztecNode, extendedClient, this.log);
|
|
554
|
+
const mintAmount = await portal.getTokenManager().getMintAmount();
|
|
555
|
+
const claim = await portal.bridgeTokensPublic(recipient, mintAmount, true /* mint */);
|
|
230
556
|
|
|
231
|
-
|
|
232
|
-
|
|
557
|
+
await this.withNoMinTxsPerBlock(() =>
|
|
558
|
+
waitForL1ToL2MessageReady(this.aztecNode, Fr.fromHexString(claim.messageHash), {
|
|
559
|
+
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds,
|
|
560
|
+
}),
|
|
561
|
+
);
|
|
233
562
|
|
|
234
|
-
this.log.info(`Created a claim for ${
|
|
563
|
+
this.log.info(`Created a claim for ${mintAmount} L1 fee juice to ${recipient}.`, claim);
|
|
235
564
|
|
|
236
|
-
|
|
237
|
-
await this.advanceL2Block();
|
|
238
|
-
await this.advanceL2Block();
|
|
239
|
-
|
|
240
|
-
return claim;
|
|
565
|
+
return claim as L2AmountClaim;
|
|
241
566
|
}
|
|
242
567
|
|
|
243
|
-
private async
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
}
|
|
568
|
+
private async withNoMinTxsPerBlock<T>(fn: () => Promise<T>): Promise<T> {
|
|
569
|
+
if (!this.aztecNodeAdmin || !this.config.flushSetupTransactions) {
|
|
570
|
+
this.log.verbose(`No node admin client or flushing not requested (not setting minTxsPerBlock to 0)`);
|
|
571
|
+
return fn();
|
|
572
|
+
}
|
|
573
|
+
const { minTxsPerBlock } = await this.aztecNodeAdmin.getConfig();
|
|
574
|
+
this.log.warn(`Setting sequencer minTxsPerBlock to 0 from ${minTxsPerBlock} to flush setup transactions`);
|
|
575
|
+
await this.aztecNodeAdmin.setConfig({ minTxsPerBlock: 0 });
|
|
576
|
+
try {
|
|
577
|
+
return await fn();
|
|
578
|
+
} finally {
|
|
579
|
+
this.log.warn(`Restoring sequencer minTxsPerBlock to ${minTxsPerBlock}`);
|
|
580
|
+
await this.aztecNodeAdmin.setConfig({ minTxsPerBlock });
|
|
257
581
|
}
|
|
258
582
|
}
|
|
259
583
|
}
|