@aztec/bot 0.0.0-test.1 → 0.0.1-fake-ceab37513c
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 +33 -0
- package/dest/amm_bot.d.ts.map +1 -0
- package/dest/amm_bot.js +94 -0
- package/dest/base_bot.d.ts +18 -0
- package/dest/base_bot.d.ts.map +1 -0
- package/dest/base_bot.js +80 -0
- package/dest/bot.d.ts +10 -14
- package/dest/bot.d.ts.map +1 -1
- package/dest/bot.js +21 -82
- package/dest/config.d.ts +51 -30
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +40 -19
- package/dest/factory.d.ts +27 -6
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +200 -75
- package/dest/index.d.ts +2 -1
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +2 -1
- package/dest/interface.d.ts +11 -0
- package/dest/interface.d.ts.map +1 -1
- package/dest/interface.js +5 -0
- package/dest/rpc.d.ts +0 -6
- package/dest/rpc.d.ts.map +1 -1
- package/dest/rpc.js +0 -11
- package/dest/runner.d.ts +6 -1
- package/dest/runner.d.ts.map +1 -1
- package/dest/runner.js +31 -9
- package/dest/utils.d.ts +6 -3
- package/dest/utils.d.ts.map +1 -1
- package/dest/utils.js +12 -3
- package/package.json +22 -21
- package/src/amm_bot.ts +116 -0
- package/src/base_bot.ts +94 -0
- package/src/bot.ts +36 -98
- package/src/config.ts +52 -22
- package/src/factory.ts +256 -78
- package/src/index.ts +2 -1
- package/src/interface.ts +9 -0
- package/src/rpc.ts +0 -13
- package/src/runner.ts +32 -11
- package/src/utils.ts +12 -6
package/src/factory.ts
CHANGED
|
@@ -3,8 +3,9 @@ import { getDeployedTestAccountsWallets, getInitialTestAccounts } from '@aztec/a
|
|
|
3
3
|
import {
|
|
4
4
|
type AccountWallet,
|
|
5
5
|
AztecAddress,
|
|
6
|
-
type AztecNode,
|
|
7
6
|
BatchCall,
|
|
7
|
+
ContractBase,
|
|
8
|
+
ContractFunctionInteraction,
|
|
8
9
|
type DeployMethod,
|
|
9
10
|
type DeployOptions,
|
|
10
11
|
FeeJuicePaymentMethodWithClaim,
|
|
@@ -12,13 +13,15 @@ import {
|
|
|
12
13
|
type PXE,
|
|
13
14
|
createLogger,
|
|
14
15
|
createPXEClient,
|
|
15
|
-
|
|
16
|
+
waitForL1ToL2MessageReady,
|
|
16
17
|
} from '@aztec/aztec.js';
|
|
17
|
-
import { createEthereumChain,
|
|
18
|
+
import { createEthereumChain, createExtendedL1Client } from '@aztec/ethereum';
|
|
18
19
|
import { Fr } from '@aztec/foundation/fields';
|
|
19
|
-
import {
|
|
20
|
+
import { Timer } from '@aztec/foundation/timer';
|
|
21
|
+
import { AMMContract } from '@aztec/noir-contracts.js/AMM';
|
|
22
|
+
import { PrivateTokenContract } from '@aztec/noir-contracts.js/PrivateToken';
|
|
20
23
|
import { TokenContract } from '@aztec/noir-contracts.js/Token';
|
|
21
|
-
import type {
|
|
24
|
+
import type { AztecNode, AztecNodeAdmin } from '@aztec/stdlib/interfaces/client';
|
|
22
25
|
import { deriveSigningKey } from '@aztec/stdlib/keys';
|
|
23
26
|
import { makeTracedFetch } from '@aztec/telemetry-client';
|
|
24
27
|
|
|
@@ -31,13 +34,19 @@ const MIN_BALANCE = 1e3;
|
|
|
31
34
|
export class BotFactory {
|
|
32
35
|
private pxe: PXE;
|
|
33
36
|
private node?: AztecNode;
|
|
37
|
+
private nodeAdmin?: AztecNodeAdmin;
|
|
34
38
|
private log = createLogger('bot');
|
|
35
39
|
|
|
36
|
-
constructor(
|
|
37
|
-
|
|
38
|
-
|
|
40
|
+
constructor(
|
|
41
|
+
private readonly config: BotConfig,
|
|
42
|
+
dependencies: { pxe?: PXE; nodeAdmin?: AztecNodeAdmin; node?: AztecNode },
|
|
43
|
+
) {
|
|
44
|
+
if (config.flushSetupTransactions && !dependencies.nodeAdmin) {
|
|
45
|
+
throw new Error(
|
|
46
|
+
`Either a node admin client or node admin url must be provided if transaction flushing is requested`,
|
|
47
|
+
);
|
|
39
48
|
}
|
|
40
|
-
if (config.senderPrivateKey && !dependencies.node) {
|
|
49
|
+
if (config.senderPrivateKey && config.senderPrivateKey.getValue() && !dependencies.node) {
|
|
41
50
|
throw new Error(
|
|
42
51
|
`Either a node client or node url must be provided for bridging L1 fee juice to deploy an account with private key`,
|
|
43
52
|
);
|
|
@@ -47,6 +56,7 @@ export class BotFactory {
|
|
|
47
56
|
}
|
|
48
57
|
|
|
49
58
|
this.node = dependencies.node;
|
|
59
|
+
this.nodeAdmin = dependencies.nodeAdmin;
|
|
50
60
|
|
|
51
61
|
if (dependencies.pxe) {
|
|
52
62
|
this.log.info(`Using local PXE`);
|
|
@@ -64,9 +74,49 @@ export class BotFactory {
|
|
|
64
74
|
public async setup() {
|
|
65
75
|
const recipient = await this.registerRecipient();
|
|
66
76
|
const wallet = await this.setupAccount();
|
|
67
|
-
const
|
|
68
|
-
await this.
|
|
69
|
-
|
|
77
|
+
const defaultAccountAddress = wallet.getAddress();
|
|
78
|
+
const token = await this.setupToken(wallet, defaultAccountAddress);
|
|
79
|
+
await this.mintTokens(token, defaultAccountAddress);
|
|
80
|
+
return { wallet, defaultAccountAddress, token, pxe: this.pxe, recipient };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public async setupAmm() {
|
|
84
|
+
const wallet = await this.setupAccount();
|
|
85
|
+
const defaultAccountAddress = wallet.getAddress();
|
|
86
|
+
const token0 = await this.setupTokenContract(
|
|
87
|
+
wallet,
|
|
88
|
+
wallet.getAddress(),
|
|
89
|
+
this.config.tokenSalt,
|
|
90
|
+
'BotToken0',
|
|
91
|
+
'BOT0',
|
|
92
|
+
);
|
|
93
|
+
const token1 = await this.setupTokenContract(
|
|
94
|
+
wallet,
|
|
95
|
+
wallet.getAddress(),
|
|
96
|
+
this.config.tokenSalt,
|
|
97
|
+
'BotToken1',
|
|
98
|
+
'BOT1',
|
|
99
|
+
);
|
|
100
|
+
const liquidityToken = await this.setupTokenContract(
|
|
101
|
+
wallet,
|
|
102
|
+
wallet.getAddress(),
|
|
103
|
+
this.config.tokenSalt,
|
|
104
|
+
'BotLPToken',
|
|
105
|
+
'BOTLP',
|
|
106
|
+
);
|
|
107
|
+
const amm = await this.setupAmmContract(
|
|
108
|
+
wallet,
|
|
109
|
+
wallet.getAddress(),
|
|
110
|
+
this.config.tokenSalt,
|
|
111
|
+
token0,
|
|
112
|
+
token1,
|
|
113
|
+
liquidityToken,
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
await this.fundAmm(wallet, wallet.getAddress(), amm, token0, token1, liquidityToken);
|
|
117
|
+
this.log.info(`AMM initialized and funded`);
|
|
118
|
+
|
|
119
|
+
return { wallet, defaultAccountAddress, amm, token0, token1, pxe: this.pxe };
|
|
70
120
|
}
|
|
71
121
|
|
|
72
122
|
/**
|
|
@@ -74,36 +124,39 @@ export class BotFactory {
|
|
|
74
124
|
* @returns The sender wallet.
|
|
75
125
|
*/
|
|
76
126
|
private async setupAccount() {
|
|
77
|
-
|
|
78
|
-
|
|
127
|
+
const privateKey = this.config.senderPrivateKey?.getValue();
|
|
128
|
+
if (privateKey) {
|
|
129
|
+
return await this.setupAccountWithPrivateKey(privateKey);
|
|
79
130
|
} else {
|
|
80
131
|
return await this.setupTestAccount();
|
|
81
132
|
}
|
|
82
133
|
}
|
|
83
134
|
|
|
84
135
|
private async setupAccountWithPrivateKey(privateKey: Fr) {
|
|
85
|
-
const salt = Fr.ONE;
|
|
136
|
+
const salt = this.config.senderSalt ?? Fr.ONE;
|
|
86
137
|
const signingKey = deriveSigningKey(privateKey);
|
|
87
138
|
const account = await getSchnorrAccount(this.pxe, privateKey, signingKey, salt);
|
|
88
139
|
const isInit = (await this.pxe.getContractMetadata(account.getAddress())).isContractInitialized;
|
|
89
140
|
if (isInit) {
|
|
90
141
|
this.log.info(`Account at ${account.getAddress().toString()} already initialized`);
|
|
142
|
+
const timer = new Timer();
|
|
91
143
|
const wallet = await account.register();
|
|
144
|
+
this.log.info(`Account at ${account.getAddress()} registered. duration=${timer.ms()}`);
|
|
92
145
|
return wallet;
|
|
93
146
|
} else {
|
|
94
147
|
const address = account.getAddress();
|
|
95
148
|
this.log.info(`Deploying account at ${address}`);
|
|
96
149
|
|
|
97
|
-
const claim = await this.bridgeL1FeeJuice(address
|
|
150
|
+
const claim = await this.bridgeL1FeeJuice(address);
|
|
98
151
|
|
|
152
|
+
// docs:start:claim_and_deploy
|
|
99
153
|
const wallet = await account.getWallet();
|
|
100
154
|
const paymentMethod = new FeeJuicePaymentMethodWithClaim(wallet, claim);
|
|
101
155
|
const sentTx = account.deploy({ fee: { paymentMethod } });
|
|
102
156
|
const txHash = await sentTx.getTxHash();
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
this.
|
|
106
|
-
await sentTx.wait({ timeout: this.config.txMinedWaitSeconds });
|
|
157
|
+
// docs:end:claim_and_deploy
|
|
158
|
+
this.log.info(`Sent tx for account deployment with hash ${txHash.toString()}`);
|
|
159
|
+
await this.withNoMinTxsPerBlock(() => sentTx.wait({ timeout: this.config.txMinedWaitSeconds }));
|
|
107
160
|
this.log.info(`Account deployed at ${address}`);
|
|
108
161
|
return wallet;
|
|
109
162
|
}
|
|
@@ -127,7 +180,7 @@ export class BotFactory {
|
|
|
127
180
|
* Registers the recipient for txs in the pxe.
|
|
128
181
|
*/
|
|
129
182
|
private async registerRecipient() {
|
|
130
|
-
const recipient = await this.pxe.registerAccount(this.config.recipientEncryptionSecret, Fr.ONE);
|
|
183
|
+
const recipient = await this.pxe.registerAccount(this.config.recipientEncryptionSecret.getValue(), Fr.ONE);
|
|
131
184
|
return recipient.address;
|
|
132
185
|
}
|
|
133
186
|
|
|
@@ -136,33 +189,165 @@ export class BotFactory {
|
|
|
136
189
|
* @param wallet - Wallet to deploy the token contract from.
|
|
137
190
|
* @returns The TokenContract instance.
|
|
138
191
|
*/
|
|
139
|
-
private async setupToken(wallet: AccountWallet): Promise<TokenContract |
|
|
140
|
-
let deploy: DeployMethod<TokenContract |
|
|
141
|
-
const deployOpts: DeployOptions = {
|
|
192
|
+
private async setupToken(wallet: AccountWallet, sender: AztecAddress): Promise<TokenContract | PrivateTokenContract> {
|
|
193
|
+
let deploy: DeployMethod<TokenContract | PrivateTokenContract>;
|
|
194
|
+
const deployOpts: DeployOptions = {
|
|
195
|
+
from: sender,
|
|
196
|
+
contractAddressSalt: this.config.tokenSalt,
|
|
197
|
+
universalDeploy: true,
|
|
198
|
+
};
|
|
142
199
|
if (this.config.contract === SupportedTokenContracts.TokenContract) {
|
|
143
|
-
deploy = TokenContract.deploy(wallet,
|
|
144
|
-
} else if (this.config.contract === SupportedTokenContracts.
|
|
145
|
-
deploy =
|
|
146
|
-
deployOpts.
|
|
147
|
-
deployOpts.
|
|
200
|
+
deploy = TokenContract.deploy(wallet, sender, 'BotToken', 'BOT', 18);
|
|
201
|
+
} else if (this.config.contract === SupportedTokenContracts.PrivateTokenContract) {
|
|
202
|
+
deploy = PrivateTokenContract.deploy(wallet, MINT_BALANCE, sender);
|
|
203
|
+
deployOpts.skipInstancePublication = true;
|
|
204
|
+
deployOpts.skipClassPublication = true;
|
|
148
205
|
deployOpts.skipInitialization = false;
|
|
149
|
-
deployOpts.skipPublicSimulation = true;
|
|
150
206
|
} else {
|
|
151
207
|
throw new Error(`Unsupported token contract type: ${this.config.contract}`);
|
|
152
208
|
}
|
|
153
209
|
|
|
154
210
|
const address = (await deploy.getInstance(deployOpts)).address;
|
|
155
|
-
if ((await this.pxe.getContractMetadata(address)).
|
|
211
|
+
if ((await this.pxe.getContractMetadata(address)).isContractPublished) {
|
|
156
212
|
this.log.info(`Token at ${address.toString()} already deployed`);
|
|
157
213
|
return deploy.register();
|
|
158
214
|
} else {
|
|
159
215
|
this.log.info(`Deploying token contract at ${address.toString()}`);
|
|
160
216
|
const sentTx = deploy.send(deployOpts);
|
|
161
217
|
const txHash = await sentTx.getTxHash();
|
|
162
|
-
this.log.info(`Sent tx with hash ${txHash.toString()}`);
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
218
|
+
this.log.info(`Sent tx for token setup with hash ${txHash.toString()}`);
|
|
219
|
+
return this.withNoMinTxsPerBlock(() => sentTx.deployed({ timeout: this.config.txMinedWaitSeconds }));
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Checks if the token contract is deployed and deploys it if necessary.
|
|
225
|
+
* @param wallet - Wallet to deploy the token contract from.
|
|
226
|
+
* @returns The TokenContract instance.
|
|
227
|
+
*/
|
|
228
|
+
private setupTokenContract(
|
|
229
|
+
wallet: AccountWallet,
|
|
230
|
+
deployer: AztecAddress,
|
|
231
|
+
contractAddressSalt: Fr,
|
|
232
|
+
name: string,
|
|
233
|
+
ticker: string,
|
|
234
|
+
decimals = 18,
|
|
235
|
+
): Promise<TokenContract> {
|
|
236
|
+
const deployOpts: DeployOptions = { from: deployer, contractAddressSalt, universalDeploy: true };
|
|
237
|
+
const deploy = TokenContract.deploy(wallet, deployer, name, ticker, decimals);
|
|
238
|
+
return this.registerOrDeployContract('Token - ' + name, deploy, deployOpts);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
private async setupAmmContract(
|
|
242
|
+
wallet: AccountWallet,
|
|
243
|
+
deployer: AztecAddress,
|
|
244
|
+
contractAddressSalt: Fr,
|
|
245
|
+
token0: TokenContract,
|
|
246
|
+
token1: TokenContract,
|
|
247
|
+
lpToken: TokenContract,
|
|
248
|
+
): Promise<AMMContract> {
|
|
249
|
+
const deployOpts: DeployOptions = { from: deployer, contractAddressSalt, universalDeploy: true };
|
|
250
|
+
const deploy = AMMContract.deploy(wallet, token0.address, token1.address, lpToken.address);
|
|
251
|
+
const amm = await this.registerOrDeployContract('AMM', deploy, deployOpts);
|
|
252
|
+
|
|
253
|
+
this.log.info(`AMM deployed at ${amm.address}`);
|
|
254
|
+
const minterTx = lpToken.methods.set_minter(amm.address, true).send({ from: deployer });
|
|
255
|
+
this.log.info(`Set LP token minter to AMM txHash=${(await minterTx.getTxHash()).toString()}`);
|
|
256
|
+
await minterTx.wait({ timeout: this.config.txMinedWaitSeconds });
|
|
257
|
+
this.log.info(`Liquidity token initialized`);
|
|
258
|
+
|
|
259
|
+
return amm;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
private async fundAmm(
|
|
263
|
+
wallet: AccountWallet,
|
|
264
|
+
liquidityProvider: AztecAddress,
|
|
265
|
+
amm: AMMContract,
|
|
266
|
+
token0: TokenContract,
|
|
267
|
+
token1: TokenContract,
|
|
268
|
+
lpToken: TokenContract,
|
|
269
|
+
): Promise<void> {
|
|
270
|
+
const getPrivateBalances = () =>
|
|
271
|
+
Promise.all([
|
|
272
|
+
token0.methods.balance_of_private(liquidityProvider).simulate({ from: liquidityProvider }),
|
|
273
|
+
token1.methods.balance_of_private(liquidityProvider).simulate({ from: liquidityProvider }),
|
|
274
|
+
lpToken.methods.balance_of_private(liquidityProvider).simulate({ from: liquidityProvider }),
|
|
275
|
+
]);
|
|
276
|
+
|
|
277
|
+
const authwitNonce = Fr.random();
|
|
278
|
+
|
|
279
|
+
// keep some tokens for swapping
|
|
280
|
+
const amount0Max = MINT_BALANCE / 2;
|
|
281
|
+
const amount0Min = MINT_BALANCE / 4;
|
|
282
|
+
const amount1Max = MINT_BALANCE / 2;
|
|
283
|
+
const amount1Min = MINT_BALANCE / 4;
|
|
284
|
+
|
|
285
|
+
const [t0Bal, t1Bal, lpBal] = await getPrivateBalances();
|
|
286
|
+
|
|
287
|
+
this.log.info(
|
|
288
|
+
`Minting ${MINT_BALANCE} tokens of each BotToken0 and BotToken1. Current private balances of ${liquidityProvider}: token0=${t0Bal}, token1=${t1Bal}, lp=${lpBal}`,
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
// Add authwitnesses for the transfers in AMM::add_liquidity function
|
|
292
|
+
const token0Authwit = await wallet.createAuthWit({
|
|
293
|
+
caller: amm.address,
|
|
294
|
+
action: token0.methods.transfer_to_public_and_prepare_private_balance_increase(
|
|
295
|
+
liquidityProvider,
|
|
296
|
+
amm.address,
|
|
297
|
+
amount0Max,
|
|
298
|
+
authwitNonce,
|
|
299
|
+
),
|
|
300
|
+
});
|
|
301
|
+
const token1Authwit = await wallet.createAuthWit({
|
|
302
|
+
caller: amm.address,
|
|
303
|
+
action: token1.methods.transfer_to_public_and_prepare_private_balance_increase(
|
|
304
|
+
liquidityProvider,
|
|
305
|
+
amm.address,
|
|
306
|
+
amount1Max,
|
|
307
|
+
authwitNonce,
|
|
308
|
+
),
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
const mintTx = new BatchCall(wallet, [
|
|
312
|
+
token0.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
|
|
313
|
+
token1.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
|
|
314
|
+
]).send({ from: liquidityProvider });
|
|
315
|
+
|
|
316
|
+
this.log.info(`Sent mint tx: ${(await mintTx.getTxHash()).toString()}`);
|
|
317
|
+
await mintTx.wait({ timeout: this.config.txMinedWaitSeconds });
|
|
318
|
+
|
|
319
|
+
const addLiquidityTx = amm.methods
|
|
320
|
+
.add_liquidity(amount0Max, amount1Max, amount0Min, amount1Min, authwitNonce)
|
|
321
|
+
.send({
|
|
322
|
+
from: liquidityProvider,
|
|
323
|
+
authWitnesses: [token0Authwit, token1Authwit],
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
this.log.info(`Sent tx to add liquidity to the AMM: ${(await addLiquidityTx.getTxHash()).toString()}`);
|
|
327
|
+
await addLiquidityTx.wait({ timeout: this.config.txMinedWaitSeconds });
|
|
328
|
+
this.log.info(`Liquidity added`);
|
|
329
|
+
|
|
330
|
+
const [newT0Bal, newT1Bal, newLPBal] = await getPrivateBalances();
|
|
331
|
+
this.log.info(
|
|
332
|
+
`Updated private balances of ${wallet.getAddress()} after minting and funding AMM: token0=${newT0Bal}, token1=${newT1Bal}, lp=${newLPBal}`,
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
private async registerOrDeployContract<T extends ContractBase>(
|
|
337
|
+
name: string,
|
|
338
|
+
deploy: DeployMethod<T>,
|
|
339
|
+
deployOpts: DeployOptions,
|
|
340
|
+
): Promise<T> {
|
|
341
|
+
const address = (await deploy.getInstance(deployOpts)).address;
|
|
342
|
+
if ((await this.pxe.getContractMetadata(address)).isContractPublished) {
|
|
343
|
+
this.log.info(`Contract ${name} at ${address.toString()} already deployed`);
|
|
344
|
+
return deploy.register();
|
|
345
|
+
} else {
|
|
346
|
+
this.log.info(`Deploying contract ${name} at ${address.toString()}`);
|
|
347
|
+
const sentTx = deploy.send(deployOpts);
|
|
348
|
+
const txHash = await sentTx.getTxHash();
|
|
349
|
+
this.log.info(`Sent contract ${name} setup tx with hash ${txHash.toString()}`);
|
|
350
|
+
return this.withNoMinTxsPerBlock(() => sentTx.deployed({ timeout: this.config.txMinedWaitSeconds }));
|
|
166
351
|
}
|
|
167
352
|
}
|
|
168
353
|
|
|
@@ -170,51 +355,47 @@ export class BotFactory {
|
|
|
170
355
|
* Mints private and public tokens for the sender if their balance is below the minimum.
|
|
171
356
|
* @param token - Token contract.
|
|
172
357
|
*/
|
|
173
|
-
private async mintTokens(token: TokenContract |
|
|
174
|
-
const sender = token.wallet.getAddress();
|
|
358
|
+
private async mintTokens(token: TokenContract | PrivateTokenContract, minter: AztecAddress) {
|
|
175
359
|
const isStandardToken = isStandardTokenContract(token);
|
|
176
360
|
let privateBalance = 0n;
|
|
177
361
|
let publicBalance = 0n;
|
|
178
362
|
|
|
179
363
|
if (isStandardToken) {
|
|
180
|
-
({ privateBalance, publicBalance } = await getBalances(token,
|
|
364
|
+
({ privateBalance, publicBalance } = await getBalances(token, minter));
|
|
181
365
|
} else {
|
|
182
|
-
privateBalance = await getPrivateBalance(token,
|
|
366
|
+
privateBalance = await getPrivateBalance(token, minter);
|
|
183
367
|
}
|
|
184
368
|
|
|
185
|
-
const calls:
|
|
369
|
+
const calls: ContractFunctionInteraction[] = [];
|
|
186
370
|
if (privateBalance < MIN_BALANCE) {
|
|
187
|
-
this.log.info(`Minting private tokens for ${
|
|
371
|
+
this.log.info(`Minting private tokens for ${minter.toString()}`);
|
|
188
372
|
|
|
189
|
-
const from = sender; // we are setting from to sender here because we need a sender to calculate the tag
|
|
190
373
|
calls.push(
|
|
191
374
|
isStandardToken
|
|
192
|
-
?
|
|
193
|
-
:
|
|
375
|
+
? token.methods.mint_to_private(minter, MINT_BALANCE)
|
|
376
|
+
: token.methods.mint(MINT_BALANCE, minter),
|
|
194
377
|
);
|
|
195
378
|
}
|
|
196
379
|
if (isStandardToken && publicBalance < MIN_BALANCE) {
|
|
197
|
-
this.log.info(`Minting public tokens for ${
|
|
198
|
-
calls.push(
|
|
380
|
+
this.log.info(`Minting public tokens for ${minter.toString()}`);
|
|
381
|
+
calls.push(token.methods.mint_to_public(minter, MINT_BALANCE));
|
|
199
382
|
}
|
|
200
383
|
if (calls.length === 0) {
|
|
201
|
-
this.log.info(`Skipping minting as ${
|
|
384
|
+
this.log.info(`Skipping minting as ${minter.toString()} has enough tokens`);
|
|
202
385
|
return;
|
|
203
386
|
}
|
|
204
|
-
const sentTx = new BatchCall(token.wallet, calls).send();
|
|
387
|
+
const sentTx = new BatchCall(token.wallet, calls).send({ from: minter });
|
|
205
388
|
const txHash = await sentTx.getTxHash();
|
|
206
|
-
this.log.info(`Sent tx with hash ${txHash.toString()}`);
|
|
207
|
-
await this.
|
|
208
|
-
this.log.verbose('Waiting for token mint to settle');
|
|
209
|
-
await sentTx.wait({ timeout: this.config.txMinedWaitSeconds });
|
|
389
|
+
this.log.info(`Sent token mint tx with hash ${txHash.toString()}`);
|
|
390
|
+
await this.withNoMinTxsPerBlock(() => sentTx.wait({ timeout: this.config.txMinedWaitSeconds }));
|
|
210
391
|
}
|
|
211
392
|
|
|
212
|
-
private async bridgeL1FeeJuice(recipient: AztecAddress
|
|
393
|
+
private async bridgeL1FeeJuice(recipient: AztecAddress) {
|
|
213
394
|
const l1RpcUrls = this.config.l1RpcUrls;
|
|
214
395
|
if (!l1RpcUrls?.length) {
|
|
215
396
|
throw new Error('L1 Rpc url is required to bridge the fee juice to fund the deployment of the account.');
|
|
216
397
|
}
|
|
217
|
-
const mnemonicOrPrivateKey = this.config.l1PrivateKey
|
|
398
|
+
const mnemonicOrPrivateKey = this.config.l1PrivateKey?.getValue() ?? this.config.l1Mnemonic?.getValue();
|
|
218
399
|
if (!mnemonicOrPrivateKey) {
|
|
219
400
|
throw new Error(
|
|
220
401
|
'Either a mnemonic or private key of an L1 account is required to bridge the fee juice to fund the deployment of the account.',
|
|
@@ -223,37 +404,34 @@ export class BotFactory {
|
|
|
223
404
|
|
|
224
405
|
const { l1ChainId } = await this.pxe.getNodeInfo();
|
|
225
406
|
const chain = createEthereumChain(l1RpcUrls, l1ChainId);
|
|
226
|
-
const
|
|
227
|
-
|
|
228
|
-
const portal = await L1FeeJuicePortalManager.new(this.pxe, publicClient, walletClient, this.log);
|
|
229
|
-
const claim = await portal.bridgeTokensPublic(recipient, amount, true /* mint */);
|
|
407
|
+
const extendedClient = createExtendedL1Client(chain.rpcUrls, mnemonicOrPrivateKey, chain.chainInfo);
|
|
230
408
|
|
|
231
|
-
const
|
|
232
|
-
|
|
409
|
+
const portal = await L1FeeJuicePortalManager.new(this.pxe, extendedClient, this.log);
|
|
410
|
+
const mintAmount = await portal.getTokenManager().getMintAmount();
|
|
411
|
+
const claim = await portal.bridgeTokensPublic(recipient, mintAmount, true /* mint */);
|
|
233
412
|
|
|
234
|
-
this.
|
|
413
|
+
await this.withNoMinTxsPerBlock(() =>
|
|
414
|
+
waitForL1ToL2MessageReady(this.pxe, Fr.fromHexString(claim.messageHash), {
|
|
415
|
+
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds,
|
|
416
|
+
forPublicConsumption: false,
|
|
417
|
+
}),
|
|
418
|
+
);
|
|
235
419
|
|
|
236
|
-
|
|
237
|
-
await this.advanceL2Block();
|
|
238
|
-
await this.advanceL2Block();
|
|
420
|
+
this.log.info(`Created a claim for ${mintAmount} L1 fee juice to ${recipient}.`, claim);
|
|
239
421
|
|
|
240
422
|
return claim;
|
|
241
423
|
}
|
|
242
424
|
|
|
243
|
-
private async
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
await this.node!.flushTxs();
|
|
254
|
-
} catch (err) {
|
|
255
|
-
this.log.error(`Failed to flush transactions: ${err}`);
|
|
256
|
-
}
|
|
425
|
+
private async withNoMinTxsPerBlock<T>(fn: () => Promise<T>): Promise<T> {
|
|
426
|
+
if (!this.nodeAdmin || !this.config.flushSetupTransactions) {
|
|
427
|
+
return fn();
|
|
428
|
+
}
|
|
429
|
+
const { minTxsPerBlock } = await this.nodeAdmin.getConfig();
|
|
430
|
+
await this.nodeAdmin.setConfig({ minTxsPerBlock: 0 });
|
|
431
|
+
try {
|
|
432
|
+
return await fn();
|
|
433
|
+
} finally {
|
|
434
|
+
await this.nodeAdmin.setConfig({ minTxsPerBlock });
|
|
257
435
|
}
|
|
258
436
|
}
|
|
259
437
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { Bot } from './bot.js';
|
|
2
|
+
export { AmmBot } from './amm_bot.js';
|
|
2
3
|
export { BotRunner } from './runner.js';
|
|
3
4
|
export {
|
|
4
5
|
type BotConfig,
|
|
@@ -7,5 +8,5 @@ export {
|
|
|
7
8
|
botConfigMappings,
|
|
8
9
|
SupportedTokenContracts,
|
|
9
10
|
} from './config.js';
|
|
10
|
-
export {
|
|
11
|
+
export { getBotRunnerApiHandler } from './rpc.js';
|
|
11
12
|
export * from './interface.js';
|
package/src/interface.ts
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
|
+
import { AztecAddress } from '@aztec/aztec.js';
|
|
1
2
|
import type { ApiSchemaFor } from '@aztec/stdlib/schemas';
|
|
2
3
|
|
|
3
4
|
import { z } from 'zod';
|
|
4
5
|
|
|
5
6
|
import { type BotConfig, BotConfigSchema } from './config.js';
|
|
6
7
|
|
|
8
|
+
export const BotInfoSchema = z.object({
|
|
9
|
+
botAddress: AztecAddress.schema,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
export type BotInfo = z.infer<typeof BotInfoSchema>;
|
|
13
|
+
|
|
7
14
|
export interface BotRunnerApi {
|
|
8
15
|
start(): Promise<void>;
|
|
9
16
|
stop(): Promise<void>;
|
|
10
17
|
run(): Promise<void>;
|
|
11
18
|
setup(): Promise<void>;
|
|
12
19
|
getConfig(): Promise<BotConfig>;
|
|
20
|
+
getInfo(): Promise<BotInfo>;
|
|
13
21
|
update(config: BotConfig): Promise<void>;
|
|
14
22
|
}
|
|
15
23
|
|
|
@@ -18,6 +26,7 @@ export const BotRunnerApiSchema: ApiSchemaFor<BotRunnerApi> = {
|
|
|
18
26
|
stop: z.function().args().returns(z.void()),
|
|
19
27
|
run: z.function().args().returns(z.void()),
|
|
20
28
|
setup: z.function().args().returns(z.void()),
|
|
29
|
+
getInfo: z.function().args().returns(BotInfoSchema),
|
|
21
30
|
getConfig: z.function().args().returns(BotConfigSchema),
|
|
22
31
|
update: z.function().args(BotConfigSchema).returns(z.void()),
|
|
23
32
|
};
|
package/src/rpc.ts
CHANGED
|
@@ -1,21 +1,8 @@
|
|
|
1
1
|
import type { ApiHandler } from '@aztec/foundation/json-rpc/server';
|
|
2
|
-
import { createTracedJsonRpcServer } from '@aztec/telemetry-client';
|
|
3
2
|
|
|
4
3
|
import { BotRunnerApiSchema } from './interface.js';
|
|
5
4
|
import type { BotRunner } from './runner.js';
|
|
6
5
|
|
|
7
|
-
/**
|
|
8
|
-
* Wraps a bot runner with a JSON RPC HTTP server.
|
|
9
|
-
* @param botRunner - The BotRunner.
|
|
10
|
-
* @returns An JSON-RPC HTTP server
|
|
11
|
-
*/
|
|
12
|
-
export function createBotRunnerRpcServer(botRunner: BotRunner) {
|
|
13
|
-
createTracedJsonRpcServer(botRunner, BotRunnerApiSchema, {
|
|
14
|
-
http200OnError: false,
|
|
15
|
-
healthCheck: botRunner.isHealthy.bind(botRunner),
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
|
|
19
6
|
export function getBotRunnerApiHandler(botRunner: BotRunner): ApiHandler {
|
|
20
7
|
return [botRunner, BotRunnerApiSchema, botRunner.isHealthy.bind(botRunner)];
|
|
21
8
|
}
|
package/src/runner.ts
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
import { type AztecNode, type PXE, createAztecNodeClient, createLogger } from '@aztec/aztec.js';
|
|
2
|
+
import { omit } from '@aztec/foundation/collection';
|
|
2
3
|
import { RunningPromise } from '@aztec/foundation/running-promise';
|
|
4
|
+
import { type AztecNodeAdmin, createAztecNodeAdminClient } from '@aztec/stdlib/interfaces/client';
|
|
3
5
|
import { type TelemetryClient, type Traceable, type Tracer, makeTracedFetch, trackSpan } from '@aztec/telemetry-client';
|
|
4
6
|
|
|
7
|
+
import { AmmBot } from './amm_bot.js';
|
|
8
|
+
import type { BaseBot } from './base_bot.js';
|
|
5
9
|
import { Bot } from './bot.js';
|
|
6
10
|
import { type BotConfig, getVersions } from './config.js';
|
|
7
|
-
import type { BotRunnerApi } from './interface.js';
|
|
11
|
+
import type { BotInfo, BotRunnerApi } from './interface.js';
|
|
8
12
|
|
|
9
13
|
export class BotRunner implements BotRunnerApi, Traceable {
|
|
10
14
|
private log = createLogger('bot');
|
|
11
|
-
private bot?: Promise<
|
|
15
|
+
private bot?: Promise<BaseBot>;
|
|
12
16
|
private pxe?: PXE;
|
|
13
17
|
private node: AztecNode;
|
|
18
|
+
private nodeAdmin?: AztecNodeAdmin;
|
|
14
19
|
private runningPromise: RunningPromise;
|
|
15
20
|
private consecutiveErrors = 0;
|
|
16
21
|
private healthy = true;
|
|
@@ -19,15 +24,19 @@ export class BotRunner implements BotRunnerApi, Traceable {
|
|
|
19
24
|
|
|
20
25
|
public constructor(
|
|
21
26
|
private config: BotConfig,
|
|
22
|
-
dependencies: { pxe?: PXE; node?: AztecNode; telemetry: TelemetryClient },
|
|
27
|
+
dependencies: { pxe?: PXE; node?: AztecNode; nodeAdmin?: AztecNodeAdmin; telemetry: TelemetryClient },
|
|
23
28
|
) {
|
|
24
29
|
this.tracer = dependencies.telemetry.getTracer('Bot');
|
|
25
30
|
this.pxe = dependencies.pxe;
|
|
26
31
|
if (!dependencies.node && !config.nodeUrl) {
|
|
27
32
|
throw new Error(`Missing node URL in config or dependencies`);
|
|
28
33
|
}
|
|
29
|
-
|
|
30
|
-
|
|
34
|
+
const versions = getVersions();
|
|
35
|
+
const fetch = makeTracedFetch([1, 2, 3], true);
|
|
36
|
+
this.node = dependencies.node ?? createAztecNodeClient(config.nodeUrl!, versions, fetch);
|
|
37
|
+
this.nodeAdmin =
|
|
38
|
+
dependencies.nodeAdmin ??
|
|
39
|
+
(config.nodeAdminUrl ? createAztecNodeAdminClient(config.nodeAdminUrl, versions, fetch) : undefined);
|
|
31
40
|
this.runningPromise = new RunningPromise(() => this.#work(), this.log, config.txIntervalSeconds * 1000);
|
|
32
41
|
}
|
|
33
42
|
|
|
@@ -126,12 +135,24 @@ export class BotRunner implements BotRunnerApi, Traceable {
|
|
|
126
135
|
|
|
127
136
|
/** Returns the current configuration for the bot. */
|
|
128
137
|
public getConfig() {
|
|
129
|
-
|
|
138
|
+
const redacted = omit(this.config, 'l1Mnemonic', 'l1PrivateKey', 'senderPrivateKey');
|
|
139
|
+
return Promise.resolve(redacted as BotConfig);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** Returns the bot sender address. */
|
|
143
|
+
public async getInfo(): Promise<BotInfo> {
|
|
144
|
+
if (!this.bot) {
|
|
145
|
+
throw new Error(`Bot is not initialized`);
|
|
146
|
+
}
|
|
147
|
+
const botAddress = await this.bot.then(b => b.wallet.getAddress());
|
|
148
|
+
return { botAddress };
|
|
130
149
|
}
|
|
131
150
|
|
|
132
151
|
async #createBot() {
|
|
133
152
|
try {
|
|
134
|
-
this.bot =
|
|
153
|
+
this.bot = this.config.ammTxs
|
|
154
|
+
? AmmBot.create(this.config, { pxe: this.pxe, node: this.node, nodeAdmin: this.nodeAdmin })
|
|
155
|
+
: Bot.create(this.config, { pxe: this.pxe, node: this.node, nodeAdmin: this.nodeAdmin });
|
|
135
156
|
await this.bot;
|
|
136
157
|
} catch (err) {
|
|
137
158
|
this.log.error(`Error setting up bot: ${err}`);
|
|
@@ -142,16 +163,16 @@ export class BotRunner implements BotRunnerApi, Traceable {
|
|
|
142
163
|
@trackSpan('Bot.work')
|
|
143
164
|
async #work() {
|
|
144
165
|
if (this.config.maxPendingTxs > 0) {
|
|
145
|
-
const
|
|
146
|
-
if (
|
|
147
|
-
this.log.verbose(`Not sending bot tx since node has ${
|
|
166
|
+
const pendingTxCount = await this.node.getPendingTxCount();
|
|
167
|
+
if (pendingTxCount >= this.config.maxPendingTxs) {
|
|
168
|
+
this.log.verbose(`Not sending bot tx since node has ${pendingTxCount} pending txs`);
|
|
148
169
|
return;
|
|
149
170
|
}
|
|
150
171
|
}
|
|
151
172
|
|
|
152
173
|
try {
|
|
153
174
|
await this.run();
|
|
154
|
-
} catch
|
|
175
|
+
} catch {
|
|
155
176
|
// Already logged in run()
|
|
156
177
|
if (this.config.maxConsecutiveErrors > 0 && this.consecutiveErrors >= this.config.maxConsecutiveErrors) {
|
|
157
178
|
this.log.error(`Too many errors bot is unhealthy`);
|