@aztec/bot 0.0.1-commit.7035c9bd6 → 0.0.1-commit.71324e566
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.js +1 -1
- package/dest/base_bot.d.ts +3 -3
- package/dest/base_bot.d.ts.map +1 -1
- package/dest/base_bot.js +12 -22
- package/dest/bot.d.ts +1 -1
- package/dest/bot.d.ts.map +1 -1
- package/dest/bot.js +1 -5
- package/dest/config.d.ts +3 -3
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +2 -2
- package/dest/cross_chain_bot.js +1 -1
- package/dest/factory.d.ts +6 -1
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +199 -126
- package/package.json +15 -15
- package/src/amm_bot.ts +1 -1
- package/src/base_bot.ts +8 -16
- package/src/bot.ts +1 -4
- package/src/config.ts +4 -4
- package/src/cross_chain_bot.ts +1 -1
- package/src/factory.ts +204 -68
package/dest/factory.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { getInitialTestAccountsData } from '@aztec/accounts/testing';
|
|
2
|
-
import {
|
|
2
|
+
import { NO_FROM } from '@aztec/aztec.js/account';
|
|
3
3
|
import { BatchCall, NO_WAIT } from '@aztec/aztec.js/contracts';
|
|
4
4
|
import { L1FeeJuicePortalManager } from '@aztec/aztec.js/ethereum';
|
|
5
5
|
import { FeeJuicePaymentMethodWithClaim } from '@aztec/aztec.js/fee';
|
|
@@ -7,6 +7,8 @@ import { deriveKeys } from '@aztec/aztec.js/keys';
|
|
|
7
7
|
import { createLogger } from '@aztec/aztec.js/log';
|
|
8
8
|
import { waitForL1ToL2MessageReady } from '@aztec/aztec.js/messaging';
|
|
9
9
|
import { waitForTx } from '@aztec/aztec.js/node';
|
|
10
|
+
import { getFeeJuiceBalance } from '@aztec/aztec.js/utils';
|
|
11
|
+
import { ContractInitializationStatus } from '@aztec/aztec.js/wallet';
|
|
10
12
|
import { createEthereumChain } from '@aztec/ethereum/chain';
|
|
11
13
|
import { createExtendedL1Client } from '@aztec/ethereum/client';
|
|
12
14
|
import { RollupContract } from '@aztec/ethereum/contracts';
|
|
@@ -17,13 +19,15 @@ import { AMMContract } from '@aztec/noir-contracts.js/AMM';
|
|
|
17
19
|
import { PrivateTokenContract } from '@aztec/noir-contracts.js/PrivateToken';
|
|
18
20
|
import { TokenContract } from '@aztec/noir-contracts.js/Token';
|
|
19
21
|
import { TestContract } from '@aztec/noir-test-contracts.js/Test';
|
|
20
|
-
import { GasFees, GasSettings } from '@aztec/stdlib/gas';
|
|
22
|
+
import { GasFees, GasSettings, ManaUsageEstimate } from '@aztec/stdlib/gas';
|
|
21
23
|
import { deriveSigningKey } from '@aztec/stdlib/keys';
|
|
22
24
|
import { SupportedTokenContracts } from './config.js';
|
|
23
25
|
import { seedL1ToL2Message } from './l1_to_l2_seeding.js';
|
|
24
26
|
import { getBalances, getPrivateBalance, isStandardTokenContract } from './utils.js';
|
|
25
27
|
const MINT_BALANCE = 1e12;
|
|
26
28
|
const MIN_BALANCE = 1e3;
|
|
29
|
+
const FEE_JUICE_TOP_UP_THRESHOLD = 100n * 10n ** 18n;
|
|
30
|
+
const FEE_JUICE_TOP_UP_TARGET = 10_000n * 10n ** 18n;
|
|
27
31
|
export class BotFactory {
|
|
28
32
|
config;
|
|
29
33
|
wallet;
|
|
@@ -48,7 +52,8 @@ export class BotFactory {
|
|
|
48
52
|
*/ async setup() {
|
|
49
53
|
const defaultAccountAddress = await this.setupAccount();
|
|
50
54
|
const recipient = (await this.wallet.createSchnorrAccount(Fr.random(), Fr.random())).address;
|
|
51
|
-
const token = await this.
|
|
55
|
+
const token = await this.setupTokenWithOptionalEarlyRefuel(defaultAccountAddress);
|
|
56
|
+
await this.ensureFeeJuiceBalance(defaultAccountAddress, token);
|
|
52
57
|
await this.mintTokens(token, defaultAccountAddress);
|
|
53
58
|
return {
|
|
54
59
|
wallet: this.wallet,
|
|
@@ -60,7 +65,8 @@ export class BotFactory {
|
|
|
60
65
|
}
|
|
61
66
|
async setupAmm() {
|
|
62
67
|
const defaultAccountAddress = await this.setupAccount();
|
|
63
|
-
const token0 = await this.
|
|
68
|
+
const token0 = await this.setupTokenContractWithOptionalEarlyRefuel(defaultAccountAddress, this.config.tokenSalt, 'BotToken0', 'BOT0');
|
|
69
|
+
await this.ensureFeeJuiceBalance(defaultAccountAddress, token0);
|
|
64
70
|
const token1 = await this.setupTokenContract(defaultAccountAddress, this.config.tokenSalt, 'BotToken1', 'BOT1');
|
|
65
71
|
const liquidityToken = await this.setupTokenContract(defaultAccountAddress, this.config.tokenSalt, 'BotLPToken', 'BOTLP');
|
|
66
72
|
const amm = await this.setupAmmContract(defaultAccountAddress, this.config.tokenSalt, token0, token1, liquidityToken);
|
|
@@ -152,7 +158,7 @@ export class BotFactory {
|
|
|
152
158
|
const signingKey = deriveSigningKey(secret);
|
|
153
159
|
const accountManager = await this.wallet.createSchnorrAccount(secret, salt, signingKey);
|
|
154
160
|
const metadata = await this.wallet.getContractMetadata(accountManager.address);
|
|
155
|
-
if (metadata.
|
|
161
|
+
if (metadata.initializationStatus === ContractInitializationStatus.INITIALIZED) {
|
|
156
162
|
this.log.info(`Account at ${accountManager.address.toString()} already initialized`);
|
|
157
163
|
const timer = new Timer();
|
|
158
164
|
const address = accountManager.address;
|
|
@@ -165,31 +171,15 @@ export class BotFactory {
|
|
|
165
171
|
const claim = await this.getOrCreateBridgeClaim(address);
|
|
166
172
|
const paymentMethod = new FeeJuicePaymentMethodWithClaim(accountManager.address, claim);
|
|
167
173
|
const deployMethod = await accountManager.getDeployMethod();
|
|
168
|
-
const maxFeesPerGas = (await this.aztecNode.getCurrentMinFees()).mul(1 + this.config.minFeePadding);
|
|
169
|
-
const { estimatedGas } = await deployMethod.simulate({
|
|
170
|
-
from: AztecAddress.ZERO,
|
|
171
|
-
fee: {
|
|
172
|
-
estimateGas: true,
|
|
173
|
-
paymentMethod
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
const gasSettings = GasSettings.from({
|
|
177
|
-
...estimatedGas,
|
|
178
|
-
maxFeesPerGas,
|
|
179
|
-
maxPriorityFeesPerGas: GasFees.empty()
|
|
180
|
-
});
|
|
181
174
|
await this.withNoMinTxsPerBlock(async ()=>{
|
|
182
175
|
const { txHash } = await deployMethod.send({
|
|
183
|
-
from:
|
|
176
|
+
from: NO_FROM,
|
|
184
177
|
fee: {
|
|
185
|
-
gasSettings,
|
|
186
178
|
paymentMethod
|
|
187
179
|
},
|
|
188
180
|
wait: NO_WAIT
|
|
189
181
|
});
|
|
190
|
-
this.log.info(`Sent tx for account deployment with hash ${txHash.toString()}
|
|
191
|
-
gasSettings
|
|
192
|
-
});
|
|
182
|
+
this.log.info(`Sent tx for account deployment with hash ${txHash.toString()}`);
|
|
193
183
|
return waitForTx(this.aztecNode, txHash, {
|
|
194
184
|
timeout: this.config.txMinedWaitSeconds
|
|
195
185
|
});
|
|
@@ -206,12 +196,70 @@ export class BotFactory {
|
|
|
206
196
|
return accountManager.address;
|
|
207
197
|
}
|
|
208
198
|
/**
|
|
199
|
+
* Setup token and refuel first: if the token already exists (restart scenario),
|
|
200
|
+
* run ensureFeeJuiceBalance before any step that might need fee juice. When deploying,
|
|
201
|
+
* use a bridge claim if balance is below threshold.
|
|
202
|
+
*/ async setupTokenWithOptionalEarlyRefuel(sender) {
|
|
203
|
+
const token = await this.getTokenInstance(sender);
|
|
204
|
+
const address = token.address;
|
|
205
|
+
const metadata = await this.wallet.getContractMetadata(address);
|
|
206
|
+
if (metadata.isContractPublished) {
|
|
207
|
+
this.log.info(`Token at ${address.toString()} already deployed, refueling before setup`);
|
|
208
|
+
await this.ensureFeeJuiceBalance(sender, token);
|
|
209
|
+
}
|
|
210
|
+
return this.setupToken(sender);
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Setup token0 for AMM with refuel-first behaviour when token already exists.
|
|
214
|
+
*/ async setupTokenContractWithOptionalEarlyRefuel(deployer, contractAddressSalt, name, ticker, decimals = 18) {
|
|
215
|
+
const deployOpts = {
|
|
216
|
+
from: deployer,
|
|
217
|
+
contractAddressSalt,
|
|
218
|
+
universalDeploy: true
|
|
219
|
+
};
|
|
220
|
+
const deploy = TokenContract.deploy(this.wallet, deployer, name, ticker, decimals);
|
|
221
|
+
const instance = await deploy.getInstance(deployOpts);
|
|
222
|
+
const metadata = await this.wallet.getContractMetadata(instance.address);
|
|
223
|
+
if (metadata.isContractPublished) {
|
|
224
|
+
this.log.info(`Token ${name} at ${instance.address.toString()} already deployed, refueling before setup`);
|
|
225
|
+
const token = TokenContract.at(instance.address, this.wallet);
|
|
226
|
+
await this.ensureFeeJuiceBalance(deployer, token);
|
|
227
|
+
}
|
|
228
|
+
return this.setupTokenContract(deployer, contractAddressSalt, name, ticker, decimals);
|
|
229
|
+
}
|
|
230
|
+
async getTokenInstance(sender) {
|
|
231
|
+
const deployOpts = {
|
|
232
|
+
from: sender,
|
|
233
|
+
contractAddressSalt: this.config.tokenSalt,
|
|
234
|
+
universalDeploy: true
|
|
235
|
+
};
|
|
236
|
+
if (this.config.contract === SupportedTokenContracts.TokenContract) {
|
|
237
|
+
const deploy = TokenContract.deploy(this.wallet, sender, 'BotToken', 'BOT', 18);
|
|
238
|
+
const instance = await deploy.getInstance(deployOpts);
|
|
239
|
+
return TokenContract.at(instance.address, this.wallet);
|
|
240
|
+
}
|
|
241
|
+
if (this.config.contract === SupportedTokenContracts.PrivateTokenContract) {
|
|
242
|
+
const tokenSecretKey = Fr.random();
|
|
243
|
+
const tokenPublicKeys = (await deriveKeys(tokenSecretKey)).publicKeys;
|
|
244
|
+
const deploy = PrivateTokenContract.deployWithPublicKeys(tokenPublicKeys, this.wallet, MINT_BALANCE, sender);
|
|
245
|
+
const instance = await deploy.getInstance({
|
|
246
|
+
...deployOpts,
|
|
247
|
+
skipInstancePublication: true,
|
|
248
|
+
skipClassPublication: true,
|
|
249
|
+
skipInitialization: false
|
|
250
|
+
});
|
|
251
|
+
return PrivateTokenContract.at(instance.address, this.wallet);
|
|
252
|
+
}
|
|
253
|
+
throw new Error(`Unsupported token contract type: ${this.config.contract}`);
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
209
256
|
* Checks if the token contract is deployed and deploys it if necessary.
|
|
210
|
-
*
|
|
211
|
-
* @
|
|
257
|
+
* Uses a bridge claim for deploy when balance is below threshold to avoid failing before refuel.
|
|
258
|
+
* @param sender - Aztec address to deploy the token contract from.
|
|
259
|
+
* @param existingToken - Optional token instance when called from setupTokenWithOptionalEarlyRefuel.
|
|
260
|
+
* @returns The TokenContract or PrivateTokenContract instance.
|
|
212
261
|
*/ async setupToken(sender) {
|
|
213
262
|
let deploy;
|
|
214
|
-
let tokenInstance;
|
|
215
263
|
const deployOpts = {
|
|
216
264
|
from: sender,
|
|
217
265
|
contractAddressSalt: this.config.tokenSalt,
|
|
@@ -220,8 +268,8 @@ export class BotFactory {
|
|
|
220
268
|
let token;
|
|
221
269
|
if (this.config.contract === SupportedTokenContracts.TokenContract) {
|
|
222
270
|
deploy = TokenContract.deploy(this.wallet, sender, 'BotToken', 'BOT', 18);
|
|
223
|
-
|
|
224
|
-
token = TokenContract.at(
|
|
271
|
+
const instance = await deploy.getInstance(deployOpts);
|
|
272
|
+
token = TokenContract.at(instance.address, this.wallet);
|
|
225
273
|
} else if (this.config.contract === SupportedTokenContracts.PrivateTokenContract) {
|
|
226
274
|
// Generate keys for the contract since PrivateToken uses SinglePrivateMutable which requires keys
|
|
227
275
|
const tokenSecretKey = Fr.random();
|
|
@@ -231,7 +279,7 @@ export class BotFactory {
|
|
|
231
279
|
deployOpts.skipClassPublication = true;
|
|
232
280
|
deployOpts.skipInitialization = false;
|
|
233
281
|
// Register the contract with the secret key before deployment
|
|
234
|
-
tokenInstance = await deploy.getInstance(deployOpts);
|
|
282
|
+
const tokenInstance = await deploy.getInstance(deployOpts);
|
|
235
283
|
token = PrivateTokenContract.at(tokenInstance.address, this.wallet);
|
|
236
284
|
await this.wallet.registerContract(tokenInstance, PrivateTokenContract.artifact, tokenSecretKey);
|
|
237
285
|
// The contract constructor initializes private storage vars that need the contract's own nullifier key.
|
|
@@ -241,36 +289,7 @@ export class BotFactory {
|
|
|
241
289
|
} else {
|
|
242
290
|
throw new Error(`Unsupported token contract type: ${this.config.contract}`);
|
|
243
291
|
}
|
|
244
|
-
|
|
245
|
-
const metadata = await this.wallet.getContractMetadata(address);
|
|
246
|
-
if (metadata.isContractPublished) {
|
|
247
|
-
this.log.info(`Token at ${address.toString()} already deployed`);
|
|
248
|
-
await deploy.register();
|
|
249
|
-
} else {
|
|
250
|
-
this.log.info(`Deploying token contract at ${address.toString()}`);
|
|
251
|
-
const { estimatedGas } = await deploy.simulate({
|
|
252
|
-
...deployOpts,
|
|
253
|
-
fee: {
|
|
254
|
-
estimateGas: true
|
|
255
|
-
}
|
|
256
|
-
});
|
|
257
|
-
const { txHash } = await deploy.send({
|
|
258
|
-
...deployOpts,
|
|
259
|
-
fee: {
|
|
260
|
-
gasSettings: estimatedGas
|
|
261
|
-
},
|
|
262
|
-
wait: NO_WAIT
|
|
263
|
-
});
|
|
264
|
-
this.log.info(`Sent tx for token setup with hash ${txHash.toString()}`, {
|
|
265
|
-
estimatedGas
|
|
266
|
-
});
|
|
267
|
-
await this.withNoMinTxsPerBlock(async ()=>{
|
|
268
|
-
await waitForTx(this.aztecNode, txHash, {
|
|
269
|
-
timeout: this.config.txMinedWaitSeconds
|
|
270
|
-
});
|
|
271
|
-
return token;
|
|
272
|
-
});
|
|
273
|
-
}
|
|
292
|
+
await this.registerOrDeployContract('token', deploy, deployOpts);
|
|
274
293
|
return token;
|
|
275
294
|
}
|
|
276
295
|
/**
|
|
@@ -298,24 +317,13 @@ export class BotFactory {
|
|
|
298
317
|
const amm = AMMContract.at(instance.address, this.wallet);
|
|
299
318
|
this.log.info(`AMM deployed at ${amm.address}`);
|
|
300
319
|
const setMinterInteraction = lpToken.methods.set_minter(amm.address, true);
|
|
301
|
-
const { estimatedGas: setMinterGas } = await setMinterInteraction.simulate({
|
|
302
|
-
from: deployer,
|
|
303
|
-
fee: {
|
|
304
|
-
estimateGas: true
|
|
305
|
-
}
|
|
306
|
-
});
|
|
307
320
|
const { receipt: minterReceipt } = await setMinterInteraction.send({
|
|
308
321
|
from: deployer,
|
|
309
|
-
fee: {
|
|
310
|
-
gasSettings: setMinterGas
|
|
311
|
-
},
|
|
312
322
|
wait: {
|
|
313
323
|
timeout: this.config.txMinedWaitSeconds
|
|
314
324
|
}
|
|
315
325
|
});
|
|
316
|
-
this.log.info(`Set LP token minter to AMM txHash=${minterReceipt.txHash.toString()}
|
|
317
|
-
estimatedGas: setMinterGas
|
|
318
|
-
});
|
|
326
|
+
this.log.info(`Set LP token minter to AMM txHash=${minterReceipt.txHash.toString()}`);
|
|
319
327
|
this.log.info(`Liquidity token initialized`);
|
|
320
328
|
return amm;
|
|
321
329
|
}
|
|
@@ -352,40 +360,16 @@ export class BotFactory {
|
|
|
352
360
|
token0.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
|
|
353
361
|
token1.methods.mint_to_private(liquidityProvider, MINT_BALANCE)
|
|
354
362
|
]);
|
|
355
|
-
const { estimatedGas: mintGas } = await mintBatch.simulate({
|
|
356
|
-
from: liquidityProvider,
|
|
357
|
-
fee: {
|
|
358
|
-
estimateGas: true
|
|
359
|
-
}
|
|
360
|
-
});
|
|
361
363
|
const { receipt: mintReceipt } = await mintBatch.send({
|
|
362
364
|
from: liquidityProvider,
|
|
363
|
-
fee: {
|
|
364
|
-
gasSettings: mintGas
|
|
365
|
-
},
|
|
366
365
|
wait: {
|
|
367
366
|
timeout: this.config.txMinedWaitSeconds
|
|
368
367
|
}
|
|
369
368
|
});
|
|
370
|
-
this.log.info(`Sent mint tx: ${mintReceipt.txHash.toString()}
|
|
371
|
-
estimatedGas: mintGas
|
|
372
|
-
});
|
|
369
|
+
this.log.info(`Sent mint tx: ${mintReceipt.txHash.toString()}`);
|
|
373
370
|
const addLiquidityInteraction = amm.methods.add_liquidity(amount0Max, amount1Max, amount0Min, amount1Min, authwitNonce);
|
|
374
|
-
const { estimatedGas: addLiquidityGas } = await addLiquidityInteraction.simulate({
|
|
375
|
-
from: liquidityProvider,
|
|
376
|
-
fee: {
|
|
377
|
-
estimateGas: true
|
|
378
|
-
},
|
|
379
|
-
authWitnesses: [
|
|
380
|
-
token0Authwit,
|
|
381
|
-
token1Authwit
|
|
382
|
-
]
|
|
383
|
-
});
|
|
384
371
|
const { receipt: addLiquidityReceipt } = await addLiquidityInteraction.send({
|
|
385
372
|
from: liquidityProvider,
|
|
386
|
-
fee: {
|
|
387
|
-
gasSettings: addLiquidityGas
|
|
388
|
-
},
|
|
389
373
|
authWitnesses: [
|
|
390
374
|
token0Authwit,
|
|
391
375
|
token1Authwit
|
|
@@ -394,9 +378,7 @@ export class BotFactory {
|
|
|
394
378
|
timeout: this.config.txMinedWaitSeconds
|
|
395
379
|
}
|
|
396
380
|
});
|
|
397
|
-
this.log.info(`Sent tx to add liquidity to the AMM: ${addLiquidityReceipt.txHash.toString()}
|
|
398
|
-
estimatedGas: addLiquidityGas
|
|
399
|
-
});
|
|
381
|
+
this.log.info(`Sent tx to add liquidity to the AMM: ${addLiquidityReceipt.txHash.toString()}`);
|
|
400
382
|
this.log.info(`Liquidity added`);
|
|
401
383
|
const [newT0Bal, newT1Bal, newLPBal] = await getPrivateBalances();
|
|
402
384
|
this.log.info(`Updated private balances of ${defaultAccountAddress} after minting and funding AMM: token0=${newT0Bal}, token1=${newT1Bal}, lp=${newLPBal}`);
|
|
@@ -409,35 +391,127 @@ export class BotFactory {
|
|
|
409
391
|
this.log.info(`Contract ${name} at ${address.toString()} already deployed`);
|
|
410
392
|
await deploy.register();
|
|
411
393
|
} else {
|
|
412
|
-
const
|
|
413
|
-
|
|
394
|
+
const sender = deployOpts.from === NO_FROM ? undefined : deployOpts.from;
|
|
395
|
+
const balance = sender ? await getFeeJuiceBalance(sender, this.aztecNode) : 0n;
|
|
396
|
+
const useClaim = sender && balance < FEE_JUICE_TOP_UP_THRESHOLD && this.config.feePaymentMethod === 'fee_juice' && !!this.config.l1RpcUrls?.length;
|
|
397
|
+
const mnemonicOrPrivateKey = this.config.l1PrivateKey?.getValue() ?? this.config.l1Mnemonic?.getValue();
|
|
398
|
+
if (useClaim && mnemonicOrPrivateKey) {
|
|
399
|
+
const claim = await this.getOrCreateBridgeClaim(sender);
|
|
400
|
+
const paymentMethod = new FeeJuicePaymentMethodWithClaim(sender, claim);
|
|
401
|
+
const { estimatedGas } = await deploy.simulate({
|
|
402
|
+
...deployOpts,
|
|
403
|
+
fee: {
|
|
404
|
+
estimateGas: true,
|
|
405
|
+
paymentMethod
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
const maxFeesPerGas = (await this.getMinFees()).mul(1 + this.config.minFeePadding);
|
|
409
|
+
const gasSettings = GasSettings.from({
|
|
410
|
+
...estimatedGas,
|
|
411
|
+
maxFeesPerGas,
|
|
412
|
+
maxPriorityFeesPerGas: GasFees.empty()
|
|
413
|
+
});
|
|
414
|
+
await this.withNoMinTxsPerBlock(async ()=>{
|
|
415
|
+
const { txHash } = await deploy.send({
|
|
416
|
+
...deployOpts,
|
|
417
|
+
fee: {
|
|
418
|
+
gasSettings,
|
|
419
|
+
paymentMethod
|
|
420
|
+
},
|
|
421
|
+
wait: NO_WAIT
|
|
422
|
+
});
|
|
423
|
+
this.log.info(`Sent contract ${name} deploy tx ${txHash.toString()} (using bridge claim, balance was ${balance})`);
|
|
424
|
+
return waitForTx(this.aztecNode, txHash, {
|
|
425
|
+
timeout: this.config.txMinedWaitSeconds
|
|
426
|
+
});
|
|
427
|
+
});
|
|
428
|
+
await this.store.deleteBridgeClaim(sender);
|
|
429
|
+
} else {
|
|
430
|
+
const { estimatedGas } = await deploy.simulate({
|
|
431
|
+
...deployOpts,
|
|
432
|
+
fee: {
|
|
433
|
+
estimateGas: true
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
this.log.info(`Deploying contract ${name} at ${address.toString()}`, {
|
|
437
|
+
estimatedGas
|
|
438
|
+
});
|
|
439
|
+
await this.withNoMinTxsPerBlock(async ()=>{
|
|
440
|
+
const { txHash } = await deploy.send({
|
|
441
|
+
...deployOpts,
|
|
442
|
+
fee: {
|
|
443
|
+
gasSettings: estimatedGas
|
|
444
|
+
},
|
|
445
|
+
wait: NO_WAIT
|
|
446
|
+
});
|
|
447
|
+
this.log.info(`Sent contract ${name} setup tx with hash ${txHash.toString()}`);
|
|
448
|
+
return waitForTx(this.aztecNode, txHash, {
|
|
449
|
+
timeout: this.config.txMinedWaitSeconds
|
|
450
|
+
});
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
return instance;
|
|
455
|
+
}
|
|
456
|
+
/**
|
|
457
|
+
* Mints private and public tokens for the sender if their balance is below the minimum.
|
|
458
|
+
* @param token - Token contract.
|
|
459
|
+
*/ /**
|
|
460
|
+
* Ensures the account has sufficient fee juice by bridging from L1 if balance is below threshold.
|
|
461
|
+
* Bridges repeatedly until balance reaches the target (10k FJ).
|
|
462
|
+
* Used on startup/restart to top up when the account has run out after previous runs.
|
|
463
|
+
*/ async ensureFeeJuiceBalance(account, token) {
|
|
464
|
+
const { feePaymentMethod, l1RpcUrls } = this.config;
|
|
465
|
+
if (feePaymentMethod !== 'fee_juice' || !l1RpcUrls?.length) {
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
const mnemonicOrPrivateKey = this.config.l1PrivateKey?.getValue() ?? this.config.l1Mnemonic?.getValue();
|
|
469
|
+
if (!mnemonicOrPrivateKey) {
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
let balance = await getFeeJuiceBalance(account, this.aztecNode);
|
|
473
|
+
if (balance >= FEE_JUICE_TOP_UP_THRESHOLD) {
|
|
474
|
+
this.log.info(`Fee juice balance ${balance} above threshold ${FEE_JUICE_TOP_UP_THRESHOLD}, skipping top-up`);
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
this.log.info(`Fee juice balance ${balance} below threshold ${FEE_JUICE_TOP_UP_THRESHOLD}, bridging from L1 until ${FEE_JUICE_TOP_UP_TARGET}`);
|
|
478
|
+
const maxFeesPerGas = (await this.getMinFees()).mul(1 + this.config.minFeePadding);
|
|
479
|
+
const minimalInteraction = isStandardTokenContract(token) ? token.methods.transfer_in_public(account, account, 0n, 0) : token.methods.transfer(0n, account, account);
|
|
480
|
+
while(balance < FEE_JUICE_TOP_UP_TARGET){
|
|
481
|
+
const claim = await this.bridgeL1FeeJuice(account);
|
|
482
|
+
const paymentMethod = new FeeJuicePaymentMethodWithClaim(account, claim);
|
|
483
|
+
const { estimatedGas } = await minimalInteraction.simulate({
|
|
484
|
+
from: account,
|
|
414
485
|
fee: {
|
|
415
|
-
estimateGas: true
|
|
486
|
+
estimateGas: true,
|
|
487
|
+
paymentMethod
|
|
416
488
|
}
|
|
417
489
|
});
|
|
418
|
-
|
|
419
|
-
estimatedGas
|
|
490
|
+
const gasSettings = GasSettings.from({
|
|
491
|
+
...estimatedGas,
|
|
492
|
+
maxFeesPerGas,
|
|
493
|
+
maxPriorityFeesPerGas: GasFees.empty()
|
|
420
494
|
});
|
|
421
495
|
await this.withNoMinTxsPerBlock(async ()=>{
|
|
422
|
-
const { txHash } = await
|
|
423
|
-
|
|
496
|
+
const { txHash } = await minimalInteraction.send({
|
|
497
|
+
from: account,
|
|
424
498
|
fee: {
|
|
425
|
-
gasSettings
|
|
499
|
+
gasSettings,
|
|
500
|
+
paymentMethod
|
|
426
501
|
},
|
|
427
502
|
wait: NO_WAIT
|
|
428
503
|
});
|
|
429
|
-
this.log.info(`Sent
|
|
504
|
+
this.log.info(`Sent fee juice top-up tx ${txHash.toString()}`);
|
|
430
505
|
return waitForTx(this.aztecNode, txHash, {
|
|
431
506
|
timeout: this.config.txMinedWaitSeconds
|
|
432
507
|
});
|
|
433
508
|
});
|
|
509
|
+
balance = await getFeeJuiceBalance(account, this.aztecNode);
|
|
510
|
+
this.log.info(`Fee juice balance after top-up: ${balance}`);
|
|
434
511
|
}
|
|
435
|
-
|
|
512
|
+
this.log.info(`Fee juice top-up complete for ${account.toString()}`);
|
|
436
513
|
}
|
|
437
|
-
|
|
438
|
-
* Mints private and public tokens for the sender if their balance is below the minimum.
|
|
439
|
-
* @param token - Token contract.
|
|
440
|
-
*/ async mintTokens(token, minter) {
|
|
514
|
+
async mintTokens(token, minter) {
|
|
441
515
|
const isStandardToken = isStandardTokenContract(token);
|
|
442
516
|
let privateBalance = 0n;
|
|
443
517
|
let publicBalance = 0n;
|
|
@@ -464,25 +538,13 @@ export class BotFactory {
|
|
|
464
538
|
token.address
|
|
465
539
|
];
|
|
466
540
|
const mintBatch = new BatchCall(token.wallet, calls);
|
|
467
|
-
const { estimatedGas } = await mintBatch.simulate({
|
|
468
|
-
from: minter,
|
|
469
|
-
fee: {
|
|
470
|
-
estimateGas: true
|
|
471
|
-
},
|
|
472
|
-
additionalScopes
|
|
473
|
-
});
|
|
474
541
|
await this.withNoMinTxsPerBlock(async ()=>{
|
|
475
542
|
const { txHash } = await mintBatch.send({
|
|
476
543
|
from: minter,
|
|
477
544
|
additionalScopes,
|
|
478
|
-
fee: {
|
|
479
|
-
gasSettings: estimatedGas
|
|
480
|
-
},
|
|
481
545
|
wait: NO_WAIT
|
|
482
546
|
});
|
|
483
|
-
this.log.info(`Sent token mint tx with hash ${txHash.toString()}
|
|
484
|
-
estimatedGas
|
|
485
|
-
});
|
|
547
|
+
this.log.info(`Sent token mint tx with hash ${txHash.toString()}`);
|
|
486
548
|
return waitForTx(this.aztecNode, txHash, {
|
|
487
549
|
timeout: this.config.txMinedWaitSeconds
|
|
488
550
|
});
|
|
@@ -534,6 +596,17 @@ export class BotFactory {
|
|
|
534
596
|
this.log.info(`Created a claim for ${mintAmount} L1 fee juice to ${recipient}.`, claim);
|
|
535
597
|
return claim;
|
|
536
598
|
}
|
|
599
|
+
/** Returns worst-case min fees across predicted slots, with fallback to current min fees. */ async getMinFees() {
|
|
600
|
+
try {
|
|
601
|
+
const predicted = await this.aztecNode.getPredictedMinFees(ManaUsageEstimate.Limit);
|
|
602
|
+
if (predicted.length === 0) {
|
|
603
|
+
return this.aztecNode.getCurrentMinFees();
|
|
604
|
+
}
|
|
605
|
+
return predicted.reduce((worst, fees)=>fees.feePerL2Gas > worst.feePerL2Gas ? fees : worst);
|
|
606
|
+
} catch {
|
|
607
|
+
return this.aztecNode.getCurrentMinFees();
|
|
608
|
+
}
|
|
609
|
+
}
|
|
537
610
|
async withNoMinTxsPerBlock(fn) {
|
|
538
611
|
if (!this.aztecNodeAdmin || !this.config.flushSetupTransactions) {
|
|
539
612
|
this.log.verbose(`No node admin client or flushing not requested (not setting minTxsPerBlock to 0)`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aztec/bot",
|
|
3
|
-
"version": "0.0.1-commit.
|
|
3
|
+
"version": "0.0.1-commit.71324e566",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dest/index.js",
|
|
@@ -54,20 +54,20 @@
|
|
|
54
54
|
]
|
|
55
55
|
},
|
|
56
56
|
"dependencies": {
|
|
57
|
-
"@aztec/accounts": "0.0.1-commit.
|
|
58
|
-
"@aztec/aztec.js": "0.0.1-commit.
|
|
59
|
-
"@aztec/entrypoints": "0.0.1-commit.
|
|
60
|
-
"@aztec/ethereum": "0.0.1-commit.
|
|
61
|
-
"@aztec/foundation": "0.0.1-commit.
|
|
62
|
-
"@aztec/kv-store": "0.0.1-commit.
|
|
63
|
-
"@aztec/l1-artifacts": "0.0.1-commit.
|
|
64
|
-
"@aztec/noir-contracts.js": "0.0.1-commit.
|
|
65
|
-
"@aztec/noir-protocol-circuits-types": "0.0.1-commit.
|
|
66
|
-
"@aztec/noir-test-contracts.js": "0.0.1-commit.
|
|
67
|
-
"@aztec/protocol-contracts": "0.0.1-commit.
|
|
68
|
-
"@aztec/stdlib": "0.0.1-commit.
|
|
69
|
-
"@aztec/telemetry-client": "0.0.1-commit.
|
|
70
|
-
"@aztec/wallets": "0.0.1-commit.
|
|
57
|
+
"@aztec/accounts": "0.0.1-commit.71324e566",
|
|
58
|
+
"@aztec/aztec.js": "0.0.1-commit.71324e566",
|
|
59
|
+
"@aztec/entrypoints": "0.0.1-commit.71324e566",
|
|
60
|
+
"@aztec/ethereum": "0.0.1-commit.71324e566",
|
|
61
|
+
"@aztec/foundation": "0.0.1-commit.71324e566",
|
|
62
|
+
"@aztec/kv-store": "0.0.1-commit.71324e566",
|
|
63
|
+
"@aztec/l1-artifacts": "0.0.1-commit.71324e566",
|
|
64
|
+
"@aztec/noir-contracts.js": "0.0.1-commit.71324e566",
|
|
65
|
+
"@aztec/noir-protocol-circuits-types": "0.0.1-commit.71324e566",
|
|
66
|
+
"@aztec/noir-test-contracts.js": "0.0.1-commit.71324e566",
|
|
67
|
+
"@aztec/protocol-contracts": "0.0.1-commit.71324e566",
|
|
68
|
+
"@aztec/stdlib": "0.0.1-commit.71324e566",
|
|
69
|
+
"@aztec/telemetry-client": "0.0.1-commit.71324e566",
|
|
70
|
+
"@aztec/wallets": "0.0.1-commit.71324e566",
|
|
71
71
|
"source-map-support": "^0.5.21",
|
|
72
72
|
"tslib": "^2.4.0",
|
|
73
73
|
"viem": "npm:@aztec/viem@2.38.2",
|
package/src/amm_bot.ts
CHANGED
|
@@ -87,7 +87,7 @@ export class AmmBot extends BaseBot {
|
|
|
87
87
|
authWitnesses: [swapAuthwit],
|
|
88
88
|
});
|
|
89
89
|
|
|
90
|
-
const opts =
|
|
90
|
+
const opts = this.getSendMethodOpts();
|
|
91
91
|
|
|
92
92
|
this.log.verbose(`Sending transaction`, logCtx);
|
|
93
93
|
this.log.info(`Tx. Balances: ${jsonStringify(balances)}`, { ...logCtx, balances });
|
package/src/base_bot.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AztecAddress } from '@aztec/aztec.js/addresses';
|
|
2
|
-
import {
|
|
2
|
+
import type { SendInteractionOptions } from '@aztec/aztec.js/contracts';
|
|
3
3
|
import { createLogger } from '@aztec/aztec.js/log';
|
|
4
4
|
import { waitForTx } from '@aztec/aztec.js/node';
|
|
5
5
|
import { TxHash, TxReceipt, TxStatus } from '@aztec/aztec.js/tx';
|
|
@@ -56,27 +56,19 @@ export abstract class BaseBot {
|
|
|
56
56
|
return Promise.resolve();
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
protected
|
|
60
|
-
interaction: ContractFunctionInteraction | BatchCall,
|
|
61
|
-
): Promise<SendInteractionOptions> {
|
|
59
|
+
protected getSendMethodOpts(): SendInteractionOptions {
|
|
62
60
|
const { l2GasLimit, daGasLimit, minFeePadding } = this.config;
|
|
63
61
|
|
|
64
62
|
this.wallet.setMinFeePadding(minFeePadding);
|
|
65
63
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
this.log.verbose(`Estimating gas for transaction`);
|
|
72
|
-
({ estimatedGas: gasSettings } = await interaction.simulate({
|
|
73
|
-
fee: { estimateGas: true },
|
|
74
|
-
from: this.defaultAccountAddress,
|
|
75
|
-
}));
|
|
76
|
-
}
|
|
64
|
+
const gasSettings =
|
|
65
|
+
l2GasLimit !== undefined && l2GasLimit > 0 && daGasLimit !== undefined && daGasLimit > 0
|
|
66
|
+
? { gasLimits: Gas.from({ l2Gas: l2GasLimit, daGas: daGasLimit }) }
|
|
67
|
+
: undefined;
|
|
68
|
+
|
|
77
69
|
return {
|
|
78
70
|
from: this.defaultAccountAddress,
|
|
79
|
-
fee: { gasSettings },
|
|
71
|
+
...(gasSettings ? { fee: { gasSettings } } : {}),
|
|
80
72
|
};
|
|
81
73
|
}
|
|
82
74
|
}
|
package/src/bot.ts
CHANGED
|
@@ -70,10 +70,7 @@ export class Bot extends BaseBot {
|
|
|
70
70
|
);
|
|
71
71
|
|
|
72
72
|
const batch = new BatchCall(wallet, calls);
|
|
73
|
-
const opts =
|
|
74
|
-
|
|
75
|
-
this.log.verbose(`Simulating transaction with ${calls.length}`, logCtx);
|
|
76
|
-
await batch.simulate({ from: this.defaultAccountAddress });
|
|
73
|
+
const opts = this.getSendMethodOpts();
|
|
77
74
|
|
|
78
75
|
this.log.verbose(`Sending transaction`, logCtx);
|
|
79
76
|
const { txHash } = await batch.send({ ...opts, wait: NO_WAIT });
|
package/src/config.ts
CHANGED
|
@@ -69,9 +69,9 @@ export type BotConfig = {
|
|
|
69
69
|
maxPendingTxs: number;
|
|
70
70
|
/** Whether to flush after sending each 'setup' transaction */
|
|
71
71
|
flushSetupTransactions: boolean;
|
|
72
|
-
/** L2 gas limit for the tx (empty to
|
|
72
|
+
/** L2 gas limit for the tx (empty to let the bot's wallet estimate). */
|
|
73
73
|
l2GasLimit: number | undefined;
|
|
74
|
-
/** DA gas limit for the tx (empty to
|
|
74
|
+
/** DA gas limit for the tx (empty to let the bot's wallet estimate). */
|
|
75
75
|
daGasLimit: number | undefined;
|
|
76
76
|
/** Token contract to use */
|
|
77
77
|
contract: SupportedTokenContracts;
|
|
@@ -243,12 +243,12 @@ export const botConfigMappings: ConfigMappingsType<BotConfig> = {
|
|
|
243
243
|
},
|
|
244
244
|
l2GasLimit: {
|
|
245
245
|
env: 'BOT_L2_GAS_LIMIT',
|
|
246
|
-
description:
|
|
246
|
+
description: "L2 gas limit for the tx (empty to let the bot's wallet estimate).",
|
|
247
247
|
...optionalNumberConfigHelper(),
|
|
248
248
|
},
|
|
249
249
|
daGasLimit: {
|
|
250
250
|
env: 'BOT_DA_GAS_LIMIT',
|
|
251
|
-
description:
|
|
251
|
+
description: "DA gas limit for the tx (empty to let the bot's wallet estimate).",
|
|
252
252
|
...optionalNumberConfigHelper(),
|
|
253
253
|
},
|
|
254
254
|
contract: {
|
package/src/cross_chain_bot.ts
CHANGED
|
@@ -137,7 +137,7 @@ export class CrossChainBot extends BaseBot {
|
|
|
137
137
|
}
|
|
138
138
|
|
|
139
139
|
const batch = new BatchCall(this.wallet, calls);
|
|
140
|
-
const opts =
|
|
140
|
+
const opts = this.getSendMethodOpts();
|
|
141
141
|
|
|
142
142
|
this.log.verbose(`Sending cross-chain batch with ${calls.length} calls`, logCtx);
|
|
143
143
|
const { txHash } = await batch.send({ ...opts, wait: NO_WAIT });
|