@aztec/bot 0.0.0-test.1 → 0.0.1-commit.5daedc8
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 +97 -0
- package/dest/base_bot.d.ts +21 -0
- package/dest/base_bot.d.ts.map +1 -0
- package/dest/base_bot.js +80 -0
- package/dest/bot.d.ts +13 -18
- package/dest/bot.d.ts.map +1 -1
- package/dest/bot.js +24 -86
- package/dest/config.d.ts +82 -57
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +55 -32
- package/dest/factory.d.ts +31 -27
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +277 -132
- package/dest/index.d.ts +4 -2
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +3 -1
- package/dest/interface.d.ts +12 -1
- package/dest/interface.d.ts.map +1 -1
- package/dest/interface.js +5 -0
- package/dest/rpc.d.ts +1 -7
- package/dest/rpc.d.ts.map +1 -1
- package/dest/rpc.js +0 -11
- package/dest/runner.d.ts +15 -11
- package/dest/runner.d.ts.map +1 -1
- package/dest/runner.js +33 -25
- package/dest/store/bot_store.d.ts +44 -0
- package/dest/store/bot_store.d.ts.map +1 -0
- package/dest/store/bot_store.js +107 -0
- package/dest/store/index.d.ts +2 -0
- package/dest/store/index.d.ts.map +1 -0
- package/dest/store/index.js +1 -0
- package/dest/utils.d.ts +8 -5
- package/dest/utils.d.ts.map +1 -1
- package/dest/utils.js +14 -5
- package/package.json +27 -23
- package/src/amm_bot.ts +124 -0
- package/src/base_bot.ts +96 -0
- package/src/bot.ts +52 -103
- package/src/config.ts +66 -38
- package/src/factory.ts +335 -146
- package/src/index.ts +3 -1
- package/src/interface.ts +9 -0
- package/src/rpc.ts +0 -13
- package/src/runner.ts +38 -21
- package/src/store/bot_store.ts +141 -0
- package/src/store/index.ts +1 -0
- package/src/utils.ts +17 -6
package/src/factory.ts
CHANGED
|
@@ -1,72 +1,94 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { SchnorrAccountContract } from '@aztec/accounts/schnorr';
|
|
2
|
+
import { getInitialTestAccountsData } from '@aztec/accounts/testing';
|
|
3
|
+
import { AztecAddress } from '@aztec/aztec.js/addresses';
|
|
3
4
|
import {
|
|
4
|
-
type AccountWallet,
|
|
5
|
-
AztecAddress,
|
|
6
|
-
type AztecNode,
|
|
7
5
|
BatchCall,
|
|
6
|
+
ContractBase,
|
|
7
|
+
ContractFunctionInteraction,
|
|
8
8
|
type DeployMethod,
|
|
9
9
|
type DeployOptions,
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
} from '@aztec/
|
|
17
|
-
import { createEthereumChain, createL1Clients } from '@aztec/ethereum';
|
|
10
|
+
} from '@aztec/aztec.js/contracts';
|
|
11
|
+
import { L1FeeJuicePortalManager } from '@aztec/aztec.js/ethereum';
|
|
12
|
+
import type { L2AmountClaim } from '@aztec/aztec.js/ethereum';
|
|
13
|
+
import { FeeJuicePaymentMethodWithClaim } from '@aztec/aztec.js/fee';
|
|
14
|
+
import { createLogger } from '@aztec/aztec.js/log';
|
|
15
|
+
import { waitForL1ToL2MessageReady } from '@aztec/aztec.js/messaging';
|
|
16
|
+
import { createEthereumChain, createExtendedL1Client } from '@aztec/ethereum';
|
|
18
17
|
import { Fr } from '@aztec/foundation/fields';
|
|
19
|
-
import {
|
|
18
|
+
import { Timer } from '@aztec/foundation/timer';
|
|
19
|
+
import { AMMContract } from '@aztec/noir-contracts.js/AMM';
|
|
20
|
+
import { PrivateTokenContract } from '@aztec/noir-contracts.js/PrivateToken';
|
|
20
21
|
import { TokenContract } from '@aztec/noir-contracts.js/Token';
|
|
21
|
-
import
|
|
22
|
+
import { GasSettings } from '@aztec/stdlib/gas';
|
|
23
|
+
import type { AztecNode, AztecNodeAdmin } from '@aztec/stdlib/interfaces/client';
|
|
22
24
|
import { deriveSigningKey } from '@aztec/stdlib/keys';
|
|
23
|
-
import {
|
|
25
|
+
import { TestWallet } from '@aztec/test-wallet/server';
|
|
24
26
|
|
|
25
|
-
import { type BotConfig, SupportedTokenContracts
|
|
27
|
+
import { type BotConfig, SupportedTokenContracts } from './config.js';
|
|
28
|
+
import type { BotStore } from './store/index.js';
|
|
26
29
|
import { getBalances, getPrivateBalance, isStandardTokenContract } from './utils.js';
|
|
27
30
|
|
|
28
31
|
const MINT_BALANCE = 1e12;
|
|
29
32
|
const MIN_BALANCE = 1e3;
|
|
30
33
|
|
|
31
34
|
export class BotFactory {
|
|
32
|
-
private pxe: PXE;
|
|
33
|
-
private node?: AztecNode;
|
|
34
35
|
private log = createLogger('bot');
|
|
35
36
|
|
|
36
|
-
constructor(
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
if (!dependencies.pxe && !config.pxeUrl) {
|
|
46
|
-
throw new Error(`Either a PXE client or a PXE URL must be provided`);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
this.node = dependencies.node;
|
|
50
|
-
|
|
51
|
-
if (dependencies.pxe) {
|
|
52
|
-
this.log.info(`Using local PXE`);
|
|
53
|
-
this.pxe = dependencies.pxe;
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
this.log.info(`Using remote PXE at ${config.pxeUrl!}`);
|
|
57
|
-
this.pxe = createPXEClient(config.pxeUrl!, getVersions(), makeTracedFetch([1, 2, 3], false));
|
|
58
|
-
}
|
|
37
|
+
constructor(
|
|
38
|
+
private readonly config: BotConfig,
|
|
39
|
+
private readonly wallet: TestWallet,
|
|
40
|
+
private readonly store: BotStore,
|
|
41
|
+
private readonly aztecNode: AztecNode,
|
|
42
|
+
private readonly aztecNodeAdmin?: AztecNodeAdmin,
|
|
43
|
+
) {}
|
|
59
44
|
|
|
60
45
|
/**
|
|
61
46
|
* Initializes a new bot by setting up the sender account, registering the recipient,
|
|
62
47
|
* deploying the token contract, and minting tokens if necessary.
|
|
63
48
|
*/
|
|
64
|
-
public async setup() {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
49
|
+
public async setup(): Promise<{
|
|
50
|
+
wallet: TestWallet;
|
|
51
|
+
defaultAccountAddress: AztecAddress;
|
|
52
|
+
token: TokenContract | PrivateTokenContract;
|
|
53
|
+
node: AztecNode;
|
|
54
|
+
recipient: AztecAddress;
|
|
55
|
+
}> {
|
|
56
|
+
const recipient = (await this.wallet.createAccount()).address;
|
|
57
|
+
const defaultAccountAddress = await this.setupAccount();
|
|
58
|
+
const token = await this.setupToken(defaultAccountAddress);
|
|
59
|
+
await this.mintTokens(token, defaultAccountAddress);
|
|
60
|
+
return { wallet: this.wallet, defaultAccountAddress, token, node: this.aztecNode, recipient };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public async setupAmm(): Promise<{
|
|
64
|
+
wallet: TestWallet;
|
|
65
|
+
defaultAccountAddress: AztecAddress;
|
|
66
|
+
amm: AMMContract;
|
|
67
|
+
token0: TokenContract;
|
|
68
|
+
token1: TokenContract;
|
|
69
|
+
node: AztecNode;
|
|
70
|
+
}> {
|
|
71
|
+
const defaultAccountAddress = await this.setupAccount();
|
|
72
|
+
const token0 = await this.setupTokenContract(defaultAccountAddress, this.config.tokenSalt, 'BotToken0', 'BOT0');
|
|
73
|
+
const token1 = await this.setupTokenContract(defaultAccountAddress, this.config.tokenSalt, 'BotToken1', 'BOT1');
|
|
74
|
+
const liquidityToken = await this.setupTokenContract(
|
|
75
|
+
defaultAccountAddress,
|
|
76
|
+
this.config.tokenSalt,
|
|
77
|
+
'BotLPToken',
|
|
78
|
+
'BOTLP',
|
|
79
|
+
);
|
|
80
|
+
const amm = await this.setupAmmContract(
|
|
81
|
+
defaultAccountAddress,
|
|
82
|
+
this.config.tokenSalt,
|
|
83
|
+
token0,
|
|
84
|
+
token1,
|
|
85
|
+
liquidityToken,
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
await this.fundAmm(defaultAccountAddress, defaultAccountAddress, amm, token0, token1, liquidityToken);
|
|
89
|
+
this.log.info(`AMM initialized and funded`);
|
|
90
|
+
|
|
91
|
+
return { wallet: this.wallet, defaultAccountAddress, amm, token0, token1, node: this.aztecNode };
|
|
70
92
|
}
|
|
71
93
|
|
|
72
94
|
/**
|
|
@@ -74,61 +96,65 @@ export class BotFactory {
|
|
|
74
96
|
* @returns The sender wallet.
|
|
75
97
|
*/
|
|
76
98
|
private async setupAccount() {
|
|
77
|
-
|
|
78
|
-
|
|
99
|
+
const privateKey = this.config.senderPrivateKey?.getValue();
|
|
100
|
+
if (privateKey) {
|
|
101
|
+
this.log.info(`Setting up account with provided private key`);
|
|
102
|
+
return await this.setupAccountWithPrivateKey(privateKey);
|
|
79
103
|
} else {
|
|
104
|
+
this.log.info(`Setting up test account`);
|
|
80
105
|
return await this.setupTestAccount();
|
|
81
106
|
}
|
|
82
107
|
}
|
|
83
108
|
|
|
84
|
-
private async setupAccountWithPrivateKey(
|
|
85
|
-
const salt = Fr.ONE;
|
|
86
|
-
const signingKey = deriveSigningKey(
|
|
87
|
-
const
|
|
88
|
-
|
|
109
|
+
private async setupAccountWithPrivateKey(secret: Fr) {
|
|
110
|
+
const salt = this.config.senderSalt ?? Fr.ONE;
|
|
111
|
+
const signingKey = deriveSigningKey(secret);
|
|
112
|
+
const accountData = {
|
|
113
|
+
secret,
|
|
114
|
+
salt,
|
|
115
|
+
contract: new SchnorrAccountContract(signingKey!),
|
|
116
|
+
};
|
|
117
|
+
const accountManager = await this.wallet.createAccount(accountData);
|
|
118
|
+
const isInit = (await this.wallet.getContractMetadata(accountManager.address)).isContractInitialized;
|
|
89
119
|
if (isInit) {
|
|
90
|
-
this.log.info(`Account at ${
|
|
91
|
-
const
|
|
92
|
-
|
|
120
|
+
this.log.info(`Account at ${accountManager.address.toString()} already initialized`);
|
|
121
|
+
const timer = new Timer();
|
|
122
|
+
const address = accountManager.address;
|
|
123
|
+
this.log.info(`Account at ${address} registered. duration=${timer.ms()}`);
|
|
124
|
+
await this.store.deleteBridgeClaim(address);
|
|
125
|
+
return address;
|
|
93
126
|
} else {
|
|
94
|
-
const address =
|
|
127
|
+
const address = accountManager.address;
|
|
95
128
|
this.log.info(`Deploying account at ${address}`);
|
|
96
129
|
|
|
97
|
-
const claim = await this.
|
|
130
|
+
const claim = await this.getOrCreateBridgeClaim(address);
|
|
98
131
|
|
|
99
|
-
const
|
|
100
|
-
const
|
|
101
|
-
const
|
|
132
|
+
const paymentMethod = new FeeJuicePaymentMethodWithClaim(accountManager.address, claim);
|
|
133
|
+
const deployMethod = await accountManager.getDeployMethod();
|
|
134
|
+
const maxFeesPerGas = (await this.aztecNode.getCurrentBaseFees()).mul(1 + this.config.baseFeePadding);
|
|
135
|
+
const gasSettings = GasSettings.default({ maxFeesPerGas });
|
|
136
|
+
const sentTx = deployMethod.send({ from: AztecAddress.ZERO, fee: { gasSettings, paymentMethod } });
|
|
102
137
|
const txHash = await sentTx.getTxHash();
|
|
103
|
-
this.log.info(`Sent tx with hash ${txHash.toString()}`);
|
|
104
|
-
await this.
|
|
105
|
-
this.log.verbose('Waiting for account deployment to settle');
|
|
106
|
-
await sentTx.wait({ timeout: this.config.txMinedWaitSeconds });
|
|
138
|
+
this.log.info(`Sent tx for account deployment with hash ${txHash.toString()}`);
|
|
139
|
+
await this.withNoMinTxsPerBlock(() => sentTx.wait({ timeout: this.config.txMinedWaitSeconds }));
|
|
107
140
|
this.log.info(`Account deployed at ${address}`);
|
|
108
|
-
return wallet;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
141
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
} else {
|
|
117
|
-
this.log.info('Registering funded test account');
|
|
118
|
-
const [account] = await getInitialTestAccounts();
|
|
119
|
-
const manager = await getSchnorrAccount(this.pxe, account.secret, account.signingKey, account.salt);
|
|
120
|
-
wallet = await manager.register();
|
|
121
|
-
this.log.info(`Funded test account registered: ${wallet.getAddress()}`);
|
|
142
|
+
// Clean up the consumed bridge claim
|
|
143
|
+
await this.store.deleteBridgeClaim(address);
|
|
144
|
+
|
|
145
|
+
return accountManager.address;
|
|
122
146
|
}
|
|
123
|
-
return wallet;
|
|
124
147
|
}
|
|
125
148
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
149
|
+
private async setupTestAccount() {
|
|
150
|
+
const [initialAccountData] = await getInitialTestAccountsData();
|
|
151
|
+
const accountData = {
|
|
152
|
+
secret: initialAccountData.secret,
|
|
153
|
+
salt: initialAccountData.salt,
|
|
154
|
+
contract: new SchnorrAccountContract(initialAccountData.signingKey),
|
|
155
|
+
};
|
|
156
|
+
const accountManager = await this.wallet.createAccount(accountData);
|
|
157
|
+
return accountManager.address;
|
|
132
158
|
}
|
|
133
159
|
|
|
134
160
|
/**
|
|
@@ -136,33 +162,167 @@ export class BotFactory {
|
|
|
136
162
|
* @param wallet - Wallet to deploy the token contract from.
|
|
137
163
|
* @returns The TokenContract instance.
|
|
138
164
|
*/
|
|
139
|
-
private async setupToken(
|
|
140
|
-
let deploy: DeployMethod<TokenContract |
|
|
141
|
-
const deployOpts: DeployOptions = {
|
|
165
|
+
private async setupToken(sender: AztecAddress): Promise<TokenContract | PrivateTokenContract> {
|
|
166
|
+
let deploy: DeployMethod<TokenContract | PrivateTokenContract>;
|
|
167
|
+
const deployOpts: DeployOptions = {
|
|
168
|
+
from: sender,
|
|
169
|
+
contractAddressSalt: this.config.tokenSalt,
|
|
170
|
+
universalDeploy: true,
|
|
171
|
+
};
|
|
142
172
|
if (this.config.contract === SupportedTokenContracts.TokenContract) {
|
|
143
|
-
deploy = TokenContract.deploy(wallet,
|
|
144
|
-
} else if (this.config.contract === SupportedTokenContracts.
|
|
145
|
-
deploy =
|
|
146
|
-
deployOpts.
|
|
147
|
-
deployOpts.
|
|
173
|
+
deploy = TokenContract.deploy(this.wallet, sender, 'BotToken', 'BOT', 18);
|
|
174
|
+
} else if (this.config.contract === SupportedTokenContracts.PrivateTokenContract) {
|
|
175
|
+
deploy = PrivateTokenContract.deploy(this.wallet, MINT_BALANCE, sender);
|
|
176
|
+
deployOpts.skipInstancePublication = true;
|
|
177
|
+
deployOpts.skipClassPublication = true;
|
|
148
178
|
deployOpts.skipInitialization = false;
|
|
149
|
-
deployOpts.skipPublicSimulation = true;
|
|
150
179
|
} else {
|
|
151
180
|
throw new Error(`Unsupported token contract type: ${this.config.contract}`);
|
|
152
181
|
}
|
|
153
182
|
|
|
154
183
|
const address = (await deploy.getInstance(deployOpts)).address;
|
|
155
|
-
if ((await this.
|
|
184
|
+
if ((await this.wallet.getContractMetadata(address)).isContractPublished) {
|
|
156
185
|
this.log.info(`Token at ${address.toString()} already deployed`);
|
|
157
186
|
return deploy.register();
|
|
158
187
|
} else {
|
|
159
188
|
this.log.info(`Deploying token contract at ${address.toString()}`);
|
|
160
189
|
const sentTx = deploy.send(deployOpts);
|
|
161
190
|
const txHash = await sentTx.getTxHash();
|
|
162
|
-
this.log.info(`Sent tx with hash ${txHash.toString()}`);
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
191
|
+
this.log.info(`Sent tx for token setup with hash ${txHash.toString()}`);
|
|
192
|
+
return this.withNoMinTxsPerBlock(() => sentTx.deployed({ timeout: this.config.txMinedWaitSeconds }));
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Checks if the token contract is deployed and deploys it if necessary.
|
|
198
|
+
* @param wallet - Wallet to deploy the token contract from.
|
|
199
|
+
* @returns The TokenContract instance.
|
|
200
|
+
*/
|
|
201
|
+
private setupTokenContract(
|
|
202
|
+
deployer: AztecAddress,
|
|
203
|
+
contractAddressSalt: Fr,
|
|
204
|
+
name: string,
|
|
205
|
+
ticker: string,
|
|
206
|
+
decimals = 18,
|
|
207
|
+
): Promise<TokenContract> {
|
|
208
|
+
const deployOpts: DeployOptions = { from: deployer, contractAddressSalt, universalDeploy: true };
|
|
209
|
+
const deploy = TokenContract.deploy(this.wallet, deployer, name, ticker, decimals);
|
|
210
|
+
return this.registerOrDeployContract('Token - ' + name, deploy, deployOpts);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private async setupAmmContract(
|
|
214
|
+
deployer: AztecAddress,
|
|
215
|
+
contractAddressSalt: Fr,
|
|
216
|
+
token0: TokenContract,
|
|
217
|
+
token1: TokenContract,
|
|
218
|
+
lpToken: TokenContract,
|
|
219
|
+
): Promise<AMMContract> {
|
|
220
|
+
const deployOpts: DeployOptions = { from: deployer, contractAddressSalt, universalDeploy: true };
|
|
221
|
+
const deploy = AMMContract.deploy(this.wallet, token0.address, token1.address, lpToken.address);
|
|
222
|
+
const amm = await this.registerOrDeployContract('AMM', deploy, deployOpts);
|
|
223
|
+
|
|
224
|
+
this.log.info(`AMM deployed at ${amm.address}`);
|
|
225
|
+
const minterTx = lpToken.methods.set_minter(amm.address, true).send({ from: deployer });
|
|
226
|
+
this.log.info(`Set LP token minter to AMM txHash=${(await minterTx.getTxHash()).toString()}`);
|
|
227
|
+
await minterTx.wait({ timeout: this.config.txMinedWaitSeconds });
|
|
228
|
+
this.log.info(`Liquidity token initialized`);
|
|
229
|
+
|
|
230
|
+
return amm;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
private async fundAmm(
|
|
234
|
+
defaultAccountAddress: AztecAddress,
|
|
235
|
+
liquidityProvider: AztecAddress,
|
|
236
|
+
amm: AMMContract,
|
|
237
|
+
token0: TokenContract,
|
|
238
|
+
token1: TokenContract,
|
|
239
|
+
lpToken: TokenContract,
|
|
240
|
+
): Promise<void> {
|
|
241
|
+
const getPrivateBalances = () =>
|
|
242
|
+
Promise.all([
|
|
243
|
+
token0.methods.balance_of_private(liquidityProvider).simulate({ from: liquidityProvider }),
|
|
244
|
+
token1.methods.balance_of_private(liquidityProvider).simulate({ from: liquidityProvider }),
|
|
245
|
+
lpToken.methods.balance_of_private(liquidityProvider).simulate({ from: liquidityProvider }),
|
|
246
|
+
]);
|
|
247
|
+
|
|
248
|
+
const authwitNonce = Fr.random();
|
|
249
|
+
|
|
250
|
+
// keep some tokens for swapping
|
|
251
|
+
const amount0Max = MINT_BALANCE / 2;
|
|
252
|
+
const amount0Min = MINT_BALANCE / 4;
|
|
253
|
+
const amount1Max = MINT_BALANCE / 2;
|
|
254
|
+
const amount1Min = MINT_BALANCE / 4;
|
|
255
|
+
|
|
256
|
+
const [t0Bal, t1Bal, lpBal] = await getPrivateBalances();
|
|
257
|
+
|
|
258
|
+
this.log.info(
|
|
259
|
+
`Minting ${MINT_BALANCE} tokens of each BotToken0 and BotToken1. Current private balances of ${liquidityProvider}: token0=${t0Bal}, token1=${t1Bal}, lp=${lpBal}`,
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
// Add authwitnesses for the transfers in AMM::add_liquidity function
|
|
263
|
+
const token0Authwit = await this.wallet.createAuthWit(defaultAccountAddress, {
|
|
264
|
+
caller: amm.address,
|
|
265
|
+
call: await token0.methods
|
|
266
|
+
.transfer_to_public_and_prepare_private_balance_increase(
|
|
267
|
+
liquidityProvider,
|
|
268
|
+
amm.address,
|
|
269
|
+
amount0Max,
|
|
270
|
+
authwitNonce,
|
|
271
|
+
)
|
|
272
|
+
.getFunctionCall(),
|
|
273
|
+
});
|
|
274
|
+
const token1Authwit = await this.wallet.createAuthWit(defaultAccountAddress, {
|
|
275
|
+
caller: amm.address,
|
|
276
|
+
call: await token1.methods
|
|
277
|
+
.transfer_to_public_and_prepare_private_balance_increase(
|
|
278
|
+
liquidityProvider,
|
|
279
|
+
amm.address,
|
|
280
|
+
amount1Max,
|
|
281
|
+
authwitNonce,
|
|
282
|
+
)
|
|
283
|
+
.getFunctionCall(),
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
const mintTx = new BatchCall(this.wallet, [
|
|
287
|
+
token0.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
|
|
288
|
+
token1.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
|
|
289
|
+
]).send({ from: liquidityProvider });
|
|
290
|
+
|
|
291
|
+
this.log.info(`Sent mint tx: ${(await mintTx.getTxHash()).toString()}`);
|
|
292
|
+
await mintTx.wait({ timeout: this.config.txMinedWaitSeconds });
|
|
293
|
+
|
|
294
|
+
const addLiquidityTx = amm.methods
|
|
295
|
+
.add_liquidity(amount0Max, amount1Max, amount0Min, amount1Min, authwitNonce)
|
|
296
|
+
.send({
|
|
297
|
+
from: liquidityProvider,
|
|
298
|
+
authWitnesses: [token0Authwit, token1Authwit],
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
this.log.info(`Sent tx to add liquidity to the AMM: ${(await addLiquidityTx.getTxHash()).toString()}`);
|
|
302
|
+
await addLiquidityTx.wait({ timeout: this.config.txMinedWaitSeconds });
|
|
303
|
+
this.log.info(`Liquidity added`);
|
|
304
|
+
|
|
305
|
+
const [newT0Bal, newT1Bal, newLPBal] = await getPrivateBalances();
|
|
306
|
+
this.log.info(
|
|
307
|
+
`Updated private balances of ${defaultAccountAddress} after minting and funding AMM: token0=${newT0Bal}, token1=${newT1Bal}, lp=${newLPBal}`,
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
private async registerOrDeployContract<T extends ContractBase>(
|
|
312
|
+
name: string,
|
|
313
|
+
deploy: DeployMethod<T>,
|
|
314
|
+
deployOpts: DeployOptions,
|
|
315
|
+
): Promise<T> {
|
|
316
|
+
const address = (await deploy.getInstance(deployOpts)).address;
|
|
317
|
+
if ((await this.wallet.getContractMetadata(address)).isContractPublished) {
|
|
318
|
+
this.log.info(`Contract ${name} at ${address.toString()} already deployed`);
|
|
319
|
+
return deploy.register();
|
|
320
|
+
} else {
|
|
321
|
+
this.log.info(`Deploying contract ${name} at ${address.toString()}`);
|
|
322
|
+
const sentTx = deploy.send(deployOpts);
|
|
323
|
+
const txHash = await sentTx.getTxHash();
|
|
324
|
+
this.log.info(`Sent contract ${name} setup tx with hash ${txHash.toString()}`);
|
|
325
|
+
return this.withNoMinTxsPerBlock(() => sentTx.deployed({ timeout: this.config.txMinedWaitSeconds }));
|
|
166
326
|
}
|
|
167
327
|
}
|
|
168
328
|
|
|
@@ -170,90 +330,119 @@ export class BotFactory {
|
|
|
170
330
|
* Mints private and public tokens for the sender if their balance is below the minimum.
|
|
171
331
|
* @param token - Token contract.
|
|
172
332
|
*/
|
|
173
|
-
private async mintTokens(token: TokenContract |
|
|
174
|
-
const sender = token.wallet.getAddress();
|
|
333
|
+
private async mintTokens(token: TokenContract | PrivateTokenContract, minter: AztecAddress) {
|
|
175
334
|
const isStandardToken = isStandardTokenContract(token);
|
|
176
335
|
let privateBalance = 0n;
|
|
177
336
|
let publicBalance = 0n;
|
|
178
337
|
|
|
179
338
|
if (isStandardToken) {
|
|
180
|
-
({ privateBalance, publicBalance } = await getBalances(token,
|
|
339
|
+
({ privateBalance, publicBalance } = await getBalances(token, minter));
|
|
181
340
|
} else {
|
|
182
|
-
privateBalance = await getPrivateBalance(token,
|
|
341
|
+
privateBalance = await getPrivateBalance(token, minter);
|
|
183
342
|
}
|
|
184
343
|
|
|
185
|
-
const calls:
|
|
344
|
+
const calls: ContractFunctionInteraction[] = [];
|
|
186
345
|
if (privateBalance < MIN_BALANCE) {
|
|
187
|
-
this.log.info(`Minting private tokens for ${
|
|
346
|
+
this.log.info(`Minting private tokens for ${minter.toString()}`);
|
|
188
347
|
|
|
189
|
-
const from = sender; // we are setting from to sender here because we need a sender to calculate the tag
|
|
190
348
|
calls.push(
|
|
191
349
|
isStandardToken
|
|
192
|
-
?
|
|
193
|
-
:
|
|
350
|
+
? token.methods.mint_to_private(minter, MINT_BALANCE)
|
|
351
|
+
: token.methods.mint(MINT_BALANCE, minter),
|
|
194
352
|
);
|
|
195
353
|
}
|
|
196
354
|
if (isStandardToken && publicBalance < MIN_BALANCE) {
|
|
197
|
-
this.log.info(`Minting public tokens for ${
|
|
198
|
-
calls.push(
|
|
355
|
+
this.log.info(`Minting public tokens for ${minter.toString()}`);
|
|
356
|
+
calls.push(token.methods.mint_to_public(minter, MINT_BALANCE));
|
|
199
357
|
}
|
|
200
358
|
if (calls.length === 0) {
|
|
201
|
-
this.log.info(`Skipping minting as ${
|
|
359
|
+
this.log.info(`Skipping minting as ${minter.toString()} has enough tokens`);
|
|
202
360
|
return;
|
|
203
361
|
}
|
|
204
|
-
const sentTx = new BatchCall(token.wallet, calls).send();
|
|
362
|
+
const sentTx = new BatchCall(token.wallet, calls).send({ from: minter });
|
|
205
363
|
const txHash = await sentTx.getTxHash();
|
|
206
|
-
this.log.info(`Sent tx with hash ${txHash.toString()}`);
|
|
207
|
-
await this.
|
|
208
|
-
|
|
209
|
-
|
|
364
|
+
this.log.info(`Sent token mint tx with hash ${txHash.toString()}`);
|
|
365
|
+
await this.withNoMinTxsPerBlock(() => sentTx.wait({ timeout: this.config.txMinedWaitSeconds }));
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Gets or creates a bridge claim for the recipient.
|
|
370
|
+
* Checks if a claim already exists in the store and reuses it if valid.
|
|
371
|
+
* Only creates a new bridge if fee juice balance is below threshold.
|
|
372
|
+
*/
|
|
373
|
+
private async getOrCreateBridgeClaim(recipient: AztecAddress): Promise<L2AmountClaim> {
|
|
374
|
+
// Check if we have an existing claim in the store
|
|
375
|
+
const existingClaim = await this.store.getBridgeClaim(recipient);
|
|
376
|
+
if (existingClaim) {
|
|
377
|
+
this.log.info(`Found existing bridge claim for ${recipient.toString()}, checking validity...`);
|
|
378
|
+
|
|
379
|
+
// Check if the message is ready on L2
|
|
380
|
+
try {
|
|
381
|
+
const messageHash = Fr.fromHexString(existingClaim.claim.messageHash);
|
|
382
|
+
await this.withNoMinTxsPerBlock(() =>
|
|
383
|
+
waitForL1ToL2MessageReady(this.aztecNode, messageHash, {
|
|
384
|
+
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds,
|
|
385
|
+
forPublicConsumption: false,
|
|
386
|
+
}),
|
|
387
|
+
);
|
|
388
|
+
return existingClaim.claim;
|
|
389
|
+
} catch (err) {
|
|
390
|
+
this.log.warn(`Failed to verify existing claim, creating new one: ${err}`);
|
|
391
|
+
await this.store.deleteBridgeClaim(recipient);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
const claim = await this.bridgeL1FeeJuice(recipient);
|
|
396
|
+
await this.store.saveBridgeClaim(recipient, claim);
|
|
397
|
+
|
|
398
|
+
return claim;
|
|
210
399
|
}
|
|
211
400
|
|
|
212
|
-
private async bridgeL1FeeJuice(recipient: AztecAddress
|
|
401
|
+
private async bridgeL1FeeJuice(recipient: AztecAddress): Promise<L2AmountClaim> {
|
|
213
402
|
const l1RpcUrls = this.config.l1RpcUrls;
|
|
214
403
|
if (!l1RpcUrls?.length) {
|
|
215
404
|
throw new Error('L1 Rpc url is required to bridge the fee juice to fund the deployment of the account.');
|
|
216
405
|
}
|
|
217
|
-
const mnemonicOrPrivateKey = this.config.l1PrivateKey
|
|
406
|
+
const mnemonicOrPrivateKey = this.config.l1PrivateKey?.getValue() ?? this.config.l1Mnemonic?.getValue();
|
|
218
407
|
if (!mnemonicOrPrivateKey) {
|
|
219
408
|
throw new Error(
|
|
220
409
|
'Either a mnemonic or private key of an L1 account is required to bridge the fee juice to fund the deployment of the account.',
|
|
221
410
|
);
|
|
222
411
|
}
|
|
223
412
|
|
|
224
|
-
const { l1ChainId } = await this.
|
|
413
|
+
const { l1ChainId } = await this.aztecNode.getNodeInfo();
|
|
225
414
|
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 */);
|
|
230
|
-
|
|
231
|
-
const isSynced = async () => await this.pxe.isL1ToL2MessageSynced(Fr.fromHexString(claim.messageHash));
|
|
232
|
-
await retryUntil(isSynced, `message ${claim.messageHash} sync`, 24, 1);
|
|
415
|
+
const extendedClient = createExtendedL1Client(chain.rpcUrls, mnemonicOrPrivateKey, chain.chainInfo);
|
|
233
416
|
|
|
234
|
-
|
|
417
|
+
const portal = await L1FeeJuicePortalManager.new(this.aztecNode, extendedClient, this.log);
|
|
418
|
+
const mintAmount = await portal.getTokenManager().getMintAmount();
|
|
419
|
+
const claim = await portal.bridgeTokensPublic(recipient, mintAmount, true /* mint */);
|
|
235
420
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
421
|
+
await this.withNoMinTxsPerBlock(() =>
|
|
422
|
+
waitForL1ToL2MessageReady(this.aztecNode, Fr.fromHexString(claim.messageHash), {
|
|
423
|
+
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds,
|
|
424
|
+
forPublicConsumption: false,
|
|
425
|
+
}),
|
|
426
|
+
);
|
|
239
427
|
|
|
240
|
-
|
|
241
|
-
}
|
|
428
|
+
this.log.info(`Created a claim for ${mintAmount} L1 fee juice to ${recipient}.`, claim);
|
|
242
429
|
|
|
243
|
-
|
|
244
|
-
const initialBlockNumber = await this.node!.getBlockNumber();
|
|
245
|
-
await this.tryFlushTxs();
|
|
246
|
-
await retryUntil(async () => (await this.node!.getBlockNumber()) >= initialBlockNumber + 1);
|
|
430
|
+
return claim as L2AmountClaim;
|
|
247
431
|
}
|
|
248
432
|
|
|
249
|
-
private async
|
|
250
|
-
if (this.config.flushSetupTransactions) {
|
|
251
|
-
this.log.verbose(
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
433
|
+
private async withNoMinTxsPerBlock<T>(fn: () => Promise<T>): Promise<T> {
|
|
434
|
+
if (!this.aztecNodeAdmin || !this.config.flushSetupTransactions) {
|
|
435
|
+
this.log.verbose(`No node admin client or flushing not requested (not setting minTxsPerBlock to 0)`);
|
|
436
|
+
return fn();
|
|
437
|
+
}
|
|
438
|
+
const { minTxsPerBlock } = await this.aztecNodeAdmin.getConfig();
|
|
439
|
+
this.log.warn(`Setting sequencer minTxsPerBlock to 0 from ${minTxsPerBlock} to flush setup transactions`);
|
|
440
|
+
await this.aztecNodeAdmin.setConfig({ minTxsPerBlock: 0 });
|
|
441
|
+
try {
|
|
442
|
+
return await fn();
|
|
443
|
+
} finally {
|
|
444
|
+
this.log.warn(`Restoring sequencer minTxsPerBlock to ${minTxsPerBlock}`);
|
|
445
|
+
await this.aztecNodeAdmin.setConfig({ minTxsPerBlock });
|
|
257
446
|
}
|
|
258
447
|
}
|
|
259
448
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export { Bot } from './bot.js';
|
|
2
|
+
export { AmmBot } from './amm_bot.js';
|
|
2
3
|
export { BotRunner } from './runner.js';
|
|
4
|
+
export { BotStore } from './store/bot_store.js';
|
|
3
5
|
export {
|
|
4
6
|
type BotConfig,
|
|
5
7
|
getBotConfigFromEnv,
|
|
@@ -7,5 +9,5 @@ export {
|
|
|
7
9
|
botConfigMappings,
|
|
8
10
|
SupportedTokenContracts,
|
|
9
11
|
} from './config.js';
|
|
10
|
-
export {
|
|
12
|
+
export { getBotRunnerApiHandler } from './rpc.js';
|
|
11
13
|
export * from './interface.js';
|
package/src/interface.ts
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
|
+
import { AztecAddress } from '@aztec/aztec.js/addresses';
|
|
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
|
}
|