@aztec/bot 0.0.1-commit.dbf9cec → 0.0.1-commit.ddcf04837
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 +1 -1
- package/dest/amm_bot.d.ts.map +1 -1
- package/dest/amm_bot.js +24 -17
- 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 +3 -6
- package/dest/config.d.ts +5 -5
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +3 -4
- package/dest/cross_chain_bot.d.ts +1 -1
- package/dest/cross_chain_bot.d.ts.map +1 -1
- package/dest/cross_chain_bot.js +4 -10
- package/dest/factory.d.ts +6 -1
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +219 -64
- package/dest/utils.js +3 -3
- package/package.json +15 -15
- package/src/amm_bot.ts +21 -16
- package/src/base_bot.ts +8 -16
- package/src/bot.ts +3 -5
- package/src/config.ts +5 -6
- package/src/cross_chain_bot.ts +4 -10
- package/src/factory.ts +246 -59
- package/src/utils.ts +3 -3
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 { 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;
|
|
@@ -38,6 +42,9 @@ export class BotFactory {
|
|
|
38
42
|
this.aztecNode = aztecNode;
|
|
39
43
|
this.aztecNodeAdmin = aztecNodeAdmin;
|
|
40
44
|
this.log = createLogger('bot');
|
|
45
|
+
// Set fee padding on the wallet so that all transactions during setup
|
|
46
|
+
// (token deploy, minting, etc.) use the configured padding, not the default.
|
|
47
|
+
this.wallet.setMinFeePadding(config.minFeePadding);
|
|
41
48
|
}
|
|
42
49
|
/**
|
|
43
50
|
* Initializes a new bot by setting up the sender account, registering the recipient,
|
|
@@ -45,7 +52,8 @@ export class BotFactory {
|
|
|
45
52
|
*/ async setup() {
|
|
46
53
|
const defaultAccountAddress = await this.setupAccount();
|
|
47
54
|
const recipient = (await this.wallet.createSchnorrAccount(Fr.random(), Fr.random())).address;
|
|
48
|
-
const token = await this.
|
|
55
|
+
const token = await this.setupTokenWithOptionalEarlyRefuel(defaultAccountAddress);
|
|
56
|
+
await this.ensureFeeJuiceBalance(defaultAccountAddress, token);
|
|
49
57
|
await this.mintTokens(token, defaultAccountAddress);
|
|
50
58
|
return {
|
|
51
59
|
wallet: this.wallet,
|
|
@@ -57,7 +65,8 @@ export class BotFactory {
|
|
|
57
65
|
}
|
|
58
66
|
async setupAmm() {
|
|
59
67
|
const defaultAccountAddress = await this.setupAccount();
|
|
60
|
-
const token0 = await this.
|
|
68
|
+
const token0 = await this.setupTokenContractWithOptionalEarlyRefuel(defaultAccountAddress, this.config.tokenSalt, 'BotToken0', 'BOT0');
|
|
69
|
+
await this.ensureFeeJuiceBalance(defaultAccountAddress, token0);
|
|
61
70
|
const token1 = await this.setupTokenContract(defaultAccountAddress, this.config.tokenSalt, 'BotToken1', 'BOT1');
|
|
62
71
|
const liquidityToken = await this.setupTokenContract(defaultAccountAddress, this.config.tokenSalt, 'BotLPToken', 'BOTLP');
|
|
63
72
|
const amm = await this.setupAmmContract(defaultAccountAddress, this.config.tokenSalt, token0, token1, liquidityToken);
|
|
@@ -108,12 +117,7 @@ export class BotFactory {
|
|
|
108
117
|
this.log.info(`Waiting for first L1→L2 message to be ready...`);
|
|
109
118
|
const firstMsg = allMessages[0];
|
|
110
119
|
await waitForL1ToL2MessageReady(this.aztecNode, Fr.fromHexString(firstMsg.msgHash), {
|
|
111
|
-
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds
|
|
112
|
-
// Use forPublicConsumption: false so we wait until the message is in the current world
|
|
113
|
-
// state. With true, it returns one block early which causes gas estimation simulation to
|
|
114
|
-
// fail since it runs against the current state.
|
|
115
|
-
// See https://linear.app/aztec-labs/issue/A-548 for details.
|
|
116
|
-
forPublicConsumption: false
|
|
120
|
+
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds
|
|
117
121
|
});
|
|
118
122
|
this.log.info(`First L1→L2 message is ready`);
|
|
119
123
|
}
|
|
@@ -154,7 +158,7 @@ export class BotFactory {
|
|
|
154
158
|
const signingKey = deriveSigningKey(secret);
|
|
155
159
|
const accountManager = await this.wallet.createSchnorrAccount(secret, salt, signingKey);
|
|
156
160
|
const metadata = await this.wallet.getContractMetadata(accountManager.address);
|
|
157
|
-
if (metadata.
|
|
161
|
+
if (metadata.initializationStatus === ContractInitializationStatus.INITIALIZED) {
|
|
158
162
|
this.log.info(`Account at ${accountManager.address.toString()} already initialized`);
|
|
159
163
|
const timer = new Timer();
|
|
160
164
|
const address = accountManager.address;
|
|
@@ -167,15 +171,10 @@ export class BotFactory {
|
|
|
167
171
|
const claim = await this.getOrCreateBridgeClaim(address);
|
|
168
172
|
const paymentMethod = new FeeJuicePaymentMethodWithClaim(accountManager.address, claim);
|
|
169
173
|
const deployMethod = await accountManager.getDeployMethod();
|
|
170
|
-
const maxFeesPerGas = (await this.aztecNode.getCurrentMinFees()).mul(1 + this.config.minFeePadding);
|
|
171
|
-
const gasSettings = GasSettings.default({
|
|
172
|
-
maxFeesPerGas
|
|
173
|
-
});
|
|
174
174
|
await this.withNoMinTxsPerBlock(async ()=>{
|
|
175
|
-
const txHash = await deployMethod.send({
|
|
176
|
-
from:
|
|
175
|
+
const { txHash } = await deployMethod.send({
|
|
176
|
+
from: NO_FROM,
|
|
177
177
|
fee: {
|
|
178
|
-
gasSettings,
|
|
179
178
|
paymentMethod
|
|
180
179
|
},
|
|
181
180
|
wait: NO_WAIT
|
|
@@ -197,12 +196,70 @@ export class BotFactory {
|
|
|
197
196
|
return accountManager.address;
|
|
198
197
|
}
|
|
199
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
|
+
/**
|
|
200
256
|
* Checks if the token contract is deployed and deploys it if necessary.
|
|
201
|
-
*
|
|
202
|
-
* @
|
|
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.
|
|
203
261
|
*/ async setupToken(sender) {
|
|
204
262
|
let deploy;
|
|
205
|
-
let tokenInstance;
|
|
206
263
|
const deployOpts = {
|
|
207
264
|
from: sender,
|
|
208
265
|
contractAddressSalt: this.config.tokenSalt,
|
|
@@ -211,8 +268,8 @@ export class BotFactory {
|
|
|
211
268
|
let token;
|
|
212
269
|
if (this.config.contract === SupportedTokenContracts.TokenContract) {
|
|
213
270
|
deploy = TokenContract.deploy(this.wallet, sender, 'BotToken', 'BOT', 18);
|
|
214
|
-
|
|
215
|
-
token = TokenContract.at(
|
|
271
|
+
const instance = await deploy.getInstance(deployOpts);
|
|
272
|
+
token = TokenContract.at(instance.address, this.wallet);
|
|
216
273
|
} else if (this.config.contract === SupportedTokenContracts.PrivateTokenContract) {
|
|
217
274
|
// Generate keys for the contract since PrivateToken uses SinglePrivateMutable which requires keys
|
|
218
275
|
const tokenSecretKey = Fr.random();
|
|
@@ -222,7 +279,7 @@ export class BotFactory {
|
|
|
222
279
|
deployOpts.skipClassPublication = true;
|
|
223
280
|
deployOpts.skipInitialization = false;
|
|
224
281
|
// Register the contract with the secret key before deployment
|
|
225
|
-
tokenInstance = await deploy.getInstance(deployOpts);
|
|
282
|
+
const tokenInstance = await deploy.getInstance(deployOpts);
|
|
226
283
|
token = PrivateTokenContract.at(tokenInstance.address, this.wallet);
|
|
227
284
|
await this.wallet.registerContract(tokenInstance, PrivateTokenContract.artifact, tokenSecretKey);
|
|
228
285
|
// The contract constructor initializes private storage vars that need the contract's own nullifier key.
|
|
@@ -232,25 +289,7 @@ export class BotFactory {
|
|
|
232
289
|
} else {
|
|
233
290
|
throw new Error(`Unsupported token contract type: ${this.config.contract}`);
|
|
234
291
|
}
|
|
235
|
-
|
|
236
|
-
const metadata = await this.wallet.getContractMetadata(address);
|
|
237
|
-
if (metadata.isContractPublished) {
|
|
238
|
-
this.log.info(`Token at ${address.toString()} already deployed`);
|
|
239
|
-
await deploy.register();
|
|
240
|
-
} else {
|
|
241
|
-
this.log.info(`Deploying token contract at ${address.toString()}`);
|
|
242
|
-
const txHash = await deploy.send({
|
|
243
|
-
...deployOpts,
|
|
244
|
-
wait: NO_WAIT
|
|
245
|
-
});
|
|
246
|
-
this.log.info(`Sent tx for token setup with hash ${txHash.toString()}`);
|
|
247
|
-
await this.withNoMinTxsPerBlock(async ()=>{
|
|
248
|
-
await waitForTx(this.aztecNode, txHash, {
|
|
249
|
-
timeout: this.config.txMinedWaitSeconds
|
|
250
|
-
});
|
|
251
|
-
return token;
|
|
252
|
-
});
|
|
253
|
-
}
|
|
292
|
+
await this.registerOrDeployContract('token', deploy, deployOpts);
|
|
254
293
|
return token;
|
|
255
294
|
}
|
|
256
295
|
/**
|
|
@@ -277,7 +316,8 @@ export class BotFactory {
|
|
|
277
316
|
const instance = await this.registerOrDeployContract('AMM', deploy, deployOpts);
|
|
278
317
|
const amm = AMMContract.at(instance.address, this.wallet);
|
|
279
318
|
this.log.info(`AMM deployed at ${amm.address}`);
|
|
280
|
-
const
|
|
319
|
+
const setMinterInteraction = lpToken.methods.set_minter(amm.address, true);
|
|
320
|
+
const { receipt: minterReceipt } = await setMinterInteraction.send({
|
|
281
321
|
from: deployer,
|
|
282
322
|
wait: {
|
|
283
323
|
timeout: this.config.txMinedWaitSeconds
|
|
@@ -291,13 +331,13 @@ export class BotFactory {
|
|
|
291
331
|
const getPrivateBalances = ()=>Promise.all([
|
|
292
332
|
token0.methods.balance_of_private(liquidityProvider).simulate({
|
|
293
333
|
from: liquidityProvider
|
|
294
|
-
}),
|
|
334
|
+
}).then((r)=>r.result),
|
|
295
335
|
token1.methods.balance_of_private(liquidityProvider).simulate({
|
|
296
336
|
from: liquidityProvider
|
|
297
|
-
}),
|
|
337
|
+
}).then((r)=>r.result),
|
|
298
338
|
lpToken.methods.balance_of_private(liquidityProvider).simulate({
|
|
299
339
|
from: liquidityProvider
|
|
300
|
-
})
|
|
340
|
+
}).then((r)=>r.result)
|
|
301
341
|
]);
|
|
302
342
|
const authwitNonce = Fr.random();
|
|
303
343
|
// keep some tokens for swapping
|
|
@@ -316,17 +356,19 @@ export class BotFactory {
|
|
|
316
356
|
caller: amm.address,
|
|
317
357
|
call: await token1.methods.transfer_to_public_and_prepare_private_balance_increase(liquidityProvider, amm.address, amount1Max, authwitNonce).getFunctionCall()
|
|
318
358
|
});
|
|
319
|
-
const
|
|
359
|
+
const mintBatch = new BatchCall(this.wallet, [
|
|
320
360
|
token0.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
|
|
321
361
|
token1.methods.mint_to_private(liquidityProvider, MINT_BALANCE)
|
|
322
|
-
])
|
|
362
|
+
]);
|
|
363
|
+
const { receipt: mintReceipt } = await mintBatch.send({
|
|
323
364
|
from: liquidityProvider,
|
|
324
365
|
wait: {
|
|
325
366
|
timeout: this.config.txMinedWaitSeconds
|
|
326
367
|
}
|
|
327
368
|
});
|
|
328
369
|
this.log.info(`Sent mint tx: ${mintReceipt.txHash.toString()}`);
|
|
329
|
-
const
|
|
370
|
+
const addLiquidityInteraction = amm.methods.add_liquidity(amount0Max, amount1Max, amount0Min, amount1Min, authwitNonce);
|
|
371
|
+
const { receipt: addLiquidityReceipt } = await addLiquidityInteraction.send({
|
|
330
372
|
from: liquidityProvider,
|
|
331
373
|
authWitnesses: [
|
|
332
374
|
token0Authwit,
|
|
@@ -349,24 +391,127 @@ export class BotFactory {
|
|
|
349
391
|
this.log.info(`Contract ${name} at ${address.toString()} already deployed`);
|
|
350
392
|
await deploy.register();
|
|
351
393
|
} else {
|
|
352
|
-
|
|
353
|
-
await this.
|
|
354
|
-
|
|
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({
|
|
355
402
|
...deployOpts,
|
|
356
|
-
|
|
403
|
+
fee: {
|
|
404
|
+
estimateGas: true,
|
|
405
|
+
paymentMethod
|
|
406
|
+
}
|
|
357
407
|
});
|
|
358
|
-
this.
|
|
359
|
-
|
|
360
|
-
|
|
408
|
+
const maxFeesPerGas = (await this.getMinFees()).mul(1 + this.config.minFeePadding);
|
|
409
|
+
const gasSettings = GasSettings.from({
|
|
410
|
+
...estimatedGas,
|
|
411
|
+
maxFeesPerGas,
|
|
412
|
+
maxPriorityFeesPerGas: GasFees.empty()
|
|
361
413
|
});
|
|
362
|
-
|
|
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
|
+
}
|
|
363
453
|
}
|
|
364
454
|
return instance;
|
|
365
455
|
}
|
|
366
456
|
/**
|
|
367
457
|
* Mints private and public tokens for the sender if their balance is below the minimum.
|
|
368
458
|
* @param token - Token contract.
|
|
369
|
-
*/
|
|
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,
|
|
485
|
+
fee: {
|
|
486
|
+
estimateGas: true,
|
|
487
|
+
paymentMethod
|
|
488
|
+
}
|
|
489
|
+
});
|
|
490
|
+
const gasSettings = GasSettings.from({
|
|
491
|
+
...estimatedGas,
|
|
492
|
+
maxFeesPerGas,
|
|
493
|
+
maxPriorityFeesPerGas: GasFees.empty()
|
|
494
|
+
});
|
|
495
|
+
await this.withNoMinTxsPerBlock(async ()=>{
|
|
496
|
+
const { txHash } = await minimalInteraction.send({
|
|
497
|
+
from: account,
|
|
498
|
+
fee: {
|
|
499
|
+
gasSettings,
|
|
500
|
+
paymentMethod
|
|
501
|
+
},
|
|
502
|
+
wait: NO_WAIT
|
|
503
|
+
});
|
|
504
|
+
this.log.info(`Sent fee juice top-up tx ${txHash.toString()}`);
|
|
505
|
+
return waitForTx(this.aztecNode, txHash, {
|
|
506
|
+
timeout: this.config.txMinedWaitSeconds
|
|
507
|
+
});
|
|
508
|
+
});
|
|
509
|
+
balance = await getFeeJuiceBalance(account, this.aztecNode);
|
|
510
|
+
this.log.info(`Fee juice balance after top-up: ${balance}`);
|
|
511
|
+
}
|
|
512
|
+
this.log.info(`Fee juice top-up complete for ${account.toString()}`);
|
|
513
|
+
}
|
|
514
|
+
async mintTokens(token, minter) {
|
|
370
515
|
const isStandardToken = isStandardTokenContract(token);
|
|
371
516
|
let privateBalance = 0n;
|
|
372
517
|
let publicBalance = 0n;
|
|
@@ -392,8 +537,9 @@ export class BotFactory {
|
|
|
392
537
|
const additionalScopes = isStandardToken ? undefined : [
|
|
393
538
|
token.address
|
|
394
539
|
];
|
|
540
|
+
const mintBatch = new BatchCall(token.wallet, calls);
|
|
395
541
|
await this.withNoMinTxsPerBlock(async ()=>{
|
|
396
|
-
const txHash = await
|
|
542
|
+
const { txHash } = await mintBatch.send({
|
|
397
543
|
from: minter,
|
|
398
544
|
additionalScopes,
|
|
399
545
|
wait: NO_WAIT
|
|
@@ -417,8 +563,7 @@ export class BotFactory {
|
|
|
417
563
|
try {
|
|
418
564
|
const messageHash = Fr.fromHexString(existingClaim.claim.messageHash);
|
|
419
565
|
await this.withNoMinTxsPerBlock(()=>waitForL1ToL2MessageReady(this.aztecNode, messageHash, {
|
|
420
|
-
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds
|
|
421
|
-
forPublicConsumption: false
|
|
566
|
+
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds
|
|
422
567
|
}));
|
|
423
568
|
return existingClaim.claim;
|
|
424
569
|
} catch (err) {
|
|
@@ -446,12 +591,22 @@ export class BotFactory {
|
|
|
446
591
|
const mintAmount = await portal.getTokenManager().getMintAmount();
|
|
447
592
|
const claim = await portal.bridgeTokensPublic(recipient, mintAmount, true);
|
|
448
593
|
await this.withNoMinTxsPerBlock(()=>waitForL1ToL2MessageReady(this.aztecNode, Fr.fromHexString(claim.messageHash), {
|
|
449
|
-
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds
|
|
450
|
-
forPublicConsumption: false
|
|
594
|
+
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds
|
|
451
595
|
}));
|
|
452
596
|
this.log.info(`Created a claim for ${mintAmount} L1 fee juice to ${recipient}.`, claim);
|
|
453
597
|
return claim;
|
|
454
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
|
+
}
|
|
455
610
|
async withNoMinTxsPerBlock(fn) {
|
|
456
611
|
if (!this.aztecNodeAdmin || !this.config.flushSetupTransactions) {
|
|
457
612
|
this.log.verbose(`No node admin client or flushing not requested (not setting minTxsPerBlock to 0)`);
|
package/dest/utils.js
CHANGED
|
@@ -4,10 +4,10 @@
|
|
|
4
4
|
* @param who - Address to get the balance for.
|
|
5
5
|
* @returns - Private and public token balances as bigints.
|
|
6
6
|
*/ export async function getBalances(token, who, from) {
|
|
7
|
-
const privateBalance = await token.methods.balance_of_private(who).simulate({
|
|
7
|
+
const { result: privateBalance } = await token.methods.balance_of_private(who).simulate({
|
|
8
8
|
from: from ?? who
|
|
9
9
|
});
|
|
10
|
-
const publicBalance = await token.methods.balance_of_public(who).simulate({
|
|
10
|
+
const { result: publicBalance } = await token.methods.balance_of_public(who).simulate({
|
|
11
11
|
from: from ?? who
|
|
12
12
|
});
|
|
13
13
|
return {
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
};
|
|
17
17
|
}
|
|
18
18
|
export async function getPrivateBalance(token, who, from) {
|
|
19
|
-
const privateBalance = await token.methods.get_balance(who).simulate({
|
|
19
|
+
const { result: privateBalance } = await token.methods.get_balance(who).simulate({
|
|
20
20
|
from: from ?? who
|
|
21
21
|
});
|
|
22
22
|
return privateBalance;
|
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.ddcf04837",
|
|
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.ddcf04837",
|
|
58
|
+
"@aztec/aztec.js": "0.0.1-commit.ddcf04837",
|
|
59
|
+
"@aztec/entrypoints": "0.0.1-commit.ddcf04837",
|
|
60
|
+
"@aztec/ethereum": "0.0.1-commit.ddcf04837",
|
|
61
|
+
"@aztec/foundation": "0.0.1-commit.ddcf04837",
|
|
62
|
+
"@aztec/kv-store": "0.0.1-commit.ddcf04837",
|
|
63
|
+
"@aztec/l1-artifacts": "0.0.1-commit.ddcf04837",
|
|
64
|
+
"@aztec/noir-contracts.js": "0.0.1-commit.ddcf04837",
|
|
65
|
+
"@aztec/noir-protocol-circuits-types": "0.0.1-commit.ddcf04837",
|
|
66
|
+
"@aztec/noir-test-contracts.js": "0.0.1-commit.ddcf04837",
|
|
67
|
+
"@aztec/protocol-contracts": "0.0.1-commit.ddcf04837",
|
|
68
|
+
"@aztec/stdlib": "0.0.1-commit.ddcf04837",
|
|
69
|
+
"@aztec/telemetry-client": "0.0.1-commit.ddcf04837",
|
|
70
|
+
"@aztec/wallets": "0.0.1-commit.ddcf04837",
|
|
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
|
@@ -71,12 +71,14 @@ export class AmmBot extends BaseBot {
|
|
|
71
71
|
.getFunctionCall(),
|
|
72
72
|
});
|
|
73
73
|
|
|
74
|
-
const
|
|
75
|
-
.
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
)
|
|
74
|
+
const { result: tokenInBalance } = await tokenIn.methods
|
|
75
|
+
.balance_of_public(amm.address)
|
|
76
|
+
.simulate({ from: this.defaultAccountAddress });
|
|
77
|
+
const { result: tokenOutBalance } = await tokenOut.methods
|
|
78
|
+
.balance_of_public(amm.address)
|
|
79
|
+
.simulate({ from: this.defaultAccountAddress });
|
|
80
|
+
const { result: amountOutMin } = await amm.methods
|
|
81
|
+
.get_amount_out_for_exact_in(tokenInBalance, tokenOutBalance, amountIn)
|
|
80
82
|
.simulate({ from: this.defaultAccountAddress });
|
|
81
83
|
|
|
82
84
|
const swapExactTokensInteraction = amm.methods
|
|
@@ -85,11 +87,12 @@ export class AmmBot extends BaseBot {
|
|
|
85
87
|
authWitnesses: [swapAuthwit],
|
|
86
88
|
});
|
|
87
89
|
|
|
88
|
-
const opts =
|
|
90
|
+
const opts = this.getSendMethodOpts();
|
|
89
91
|
|
|
90
92
|
this.log.verbose(`Sending transaction`, logCtx);
|
|
91
93
|
this.log.info(`Tx. Balances: ${jsonStringify(balances)}`, { ...logCtx, balances });
|
|
92
|
-
|
|
94
|
+
const { txHash } = await swapExactTokensInteraction.send({ ...opts, wait: NO_WAIT });
|
|
95
|
+
return txHash;
|
|
93
96
|
}
|
|
94
97
|
|
|
95
98
|
protected override async onTxMined(receipt: TxReceipt, logCtx: object): Promise<void> {
|
|
@@ -110,15 +113,17 @@ export class AmmBot extends BaseBot {
|
|
|
110
113
|
}
|
|
111
114
|
|
|
112
115
|
private async getPublicBalanceFor(address: AztecAddress, from?: AztecAddress): Promise<Balances> {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
};
|
|
116
|
+
const { result: token0 } = await this.token0.methods.balance_of_public(address).simulate({ from: from ?? address });
|
|
117
|
+
const { result: token1 } = await this.token1.methods.balance_of_public(address).simulate({ from: from ?? address });
|
|
118
|
+
return { token0, token1 };
|
|
117
119
|
}
|
|
118
120
|
private async getPrivateBalanceFor(address: AztecAddress, from?: AztecAddress): Promise<Balances> {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
121
|
+
const { result: token0 } = await this.token0.methods
|
|
122
|
+
.balance_of_private(address)
|
|
123
|
+
.simulate({ from: from ?? address });
|
|
124
|
+
const { result: token1 } = await this.token1.methods
|
|
125
|
+
.balance_of_private(address)
|
|
126
|
+
.simulate({ from: from ?? address });
|
|
127
|
+
return { token0, token1 };
|
|
123
128
|
}
|
|
124
129
|
}
|
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,13 +70,11 @@ 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 });
|
|
77
|
+
return txHash;
|
|
80
78
|
}
|
|
81
79
|
|
|
82
80
|
public async getBalances() {
|