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