@aztec/bot 0.0.1-commit.d3ec352c → 0.0.1-commit.e3c1de76
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 +3 -4
- package/dest/amm_bot.d.ts.map +1 -1
- package/dest/amm_bot.js +5 -1
- package/dest/base_bot.d.ts +3 -3
- package/dest/base_bot.d.ts.map +1 -1
- package/dest/base_bot.js +5 -5
- package/dest/bot.d.ts +3 -3
- package/dest/bot.d.ts.map +1 -1
- package/dest/bot.js +5 -2
- package/dest/config.d.ts +11 -12
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +7 -7
- package/dest/factory.d.ts +1 -6
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +96 -62
- package/dest/runner.js +412 -30
- package/dest/store/bot_store.d.ts +2 -2
- package/dest/store/bot_store.d.ts.map +1 -1
- package/dest/store/bot_store.js +1 -1
- package/package.json +14 -14
- package/src/amm_bot.ts +4 -4
- package/src/base_bot.ts +6 -10
- package/src/bot.ts +4 -3
- package/src/config.ts +52 -50
- package/src/factory.ts +77 -42
- package/src/store/bot_store.ts +1 -1
package/dest/factory.js
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import { SchnorrAccountContract } from '@aztec/accounts/schnorr';
|
|
2
2
|
import { getInitialTestAccountsData } from '@aztec/accounts/testing';
|
|
3
3
|
import { AztecAddress } from '@aztec/aztec.js/addresses';
|
|
4
|
-
import { BatchCall } from '@aztec/aztec.js/contracts';
|
|
4
|
+
import { BatchCall, NO_WAIT } from '@aztec/aztec.js/contracts';
|
|
5
5
|
import { L1FeeJuicePortalManager } from '@aztec/aztec.js/ethereum';
|
|
6
6
|
import { FeeJuicePaymentMethodWithClaim } from '@aztec/aztec.js/fee';
|
|
7
|
+
import { deriveKeys } from '@aztec/aztec.js/keys';
|
|
7
8
|
import { createLogger } from '@aztec/aztec.js/log';
|
|
8
9
|
import { waitForL1ToL2MessageReady } from '@aztec/aztec.js/messaging';
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
10
|
+
import { waitForTx } from '@aztec/aztec.js/node';
|
|
11
|
+
import { createEthereumChain } from '@aztec/ethereum/chain';
|
|
12
|
+
import { createExtendedL1Client } from '@aztec/ethereum/client';
|
|
13
|
+
import { Fr } from '@aztec/foundation/curves/bn254';
|
|
11
14
|
import { Timer } from '@aztec/foundation/timer';
|
|
12
15
|
import { AMMContract } from '@aztec/noir-contracts.js/AMM';
|
|
13
16
|
import { PrivateTokenContract } from '@aztec/noir-contracts.js/PrivateToken';
|
|
@@ -37,8 +40,8 @@ export class BotFactory {
|
|
|
37
40
|
* Initializes a new bot by setting up the sender account, registering the recipient,
|
|
38
41
|
* deploying the token contract, and minting tokens if necessary.
|
|
39
42
|
*/ async setup() {
|
|
40
|
-
const recipient = (await this.wallet.createAccount()).address;
|
|
41
43
|
const defaultAccountAddress = await this.setupAccount();
|
|
44
|
+
const recipient = (await this.wallet.createAccount()).address;
|
|
42
45
|
const token = await this.setupToken(defaultAccountAddress);
|
|
43
46
|
await this.mintTokens(token, defaultAccountAddress);
|
|
44
47
|
return {
|
|
@@ -88,8 +91,8 @@ export class BotFactory {
|
|
|
88
91
|
contract: new SchnorrAccountContract(signingKey)
|
|
89
92
|
};
|
|
90
93
|
const accountManager = await this.wallet.createAccount(accountData);
|
|
91
|
-
const
|
|
92
|
-
if (
|
|
94
|
+
const metadata = await this.wallet.getContractMetadata(accountManager.address);
|
|
95
|
+
if (metadata.isContractInitialized) {
|
|
93
96
|
this.log.info(`Account at ${accountManager.address.toString()} already initialized`);
|
|
94
97
|
const timer = new Timer();
|
|
95
98
|
const address = accountManager.address;
|
|
@@ -102,22 +105,24 @@ export class BotFactory {
|
|
|
102
105
|
const claim = await this.getOrCreateBridgeClaim(address);
|
|
103
106
|
const paymentMethod = new FeeJuicePaymentMethodWithClaim(accountManager.address, claim);
|
|
104
107
|
const deployMethod = await accountManager.getDeployMethod();
|
|
105
|
-
const maxFeesPerGas = (await this.aztecNode.
|
|
108
|
+
const maxFeesPerGas = (await this.aztecNode.getCurrentMinFees()).mul(1 + this.config.minFeePadding);
|
|
106
109
|
const gasSettings = GasSettings.default({
|
|
107
110
|
maxFeesPerGas
|
|
108
111
|
});
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
112
|
+
await this.withNoMinTxsPerBlock(async ()=>{
|
|
113
|
+
const txHash = await deployMethod.send({
|
|
114
|
+
from: AztecAddress.ZERO,
|
|
115
|
+
fee: {
|
|
116
|
+
gasSettings,
|
|
117
|
+
paymentMethod
|
|
118
|
+
},
|
|
119
|
+
wait: NO_WAIT
|
|
120
|
+
});
|
|
121
|
+
this.log.info(`Sent tx for account deployment with hash ${txHash.toString()}`);
|
|
122
|
+
return waitForTx(this.aztecNode, txHash, {
|
|
119
123
|
timeout: this.config.txMinedWaitSeconds
|
|
120
|
-
})
|
|
124
|
+
});
|
|
125
|
+
});
|
|
121
126
|
this.log.info(`Account deployed at ${address}`);
|
|
122
127
|
// Clean up the consumed bridge claim
|
|
123
128
|
await this.store.deleteBridgeClaim(address);
|
|
@@ -140,47 +145,66 @@ export class BotFactory {
|
|
|
140
145
|
* @returns The TokenContract instance.
|
|
141
146
|
*/ async setupToken(sender) {
|
|
142
147
|
let deploy;
|
|
148
|
+
let tokenInstance;
|
|
143
149
|
const deployOpts = {
|
|
144
150
|
from: sender,
|
|
145
151
|
contractAddressSalt: this.config.tokenSalt,
|
|
146
152
|
universalDeploy: true
|
|
147
153
|
};
|
|
154
|
+
let token;
|
|
148
155
|
if (this.config.contract === SupportedTokenContracts.TokenContract) {
|
|
149
156
|
deploy = TokenContract.deploy(this.wallet, sender, 'BotToken', 'BOT', 18);
|
|
157
|
+
tokenInstance = await deploy.getInstance(deployOpts);
|
|
158
|
+
token = TokenContract.at(tokenInstance.address, this.wallet);
|
|
150
159
|
} else if (this.config.contract === SupportedTokenContracts.PrivateTokenContract) {
|
|
151
|
-
|
|
160
|
+
// Generate keys for the contract since PrivateToken uses SinglePrivateMutable which requires keys
|
|
161
|
+
const tokenSecretKey = Fr.random();
|
|
162
|
+
const tokenPublicKeys = (await deriveKeys(tokenSecretKey)).publicKeys;
|
|
163
|
+
deploy = PrivateTokenContract.deployWithPublicKeys(tokenPublicKeys, this.wallet, MINT_BALANCE, sender);
|
|
152
164
|
deployOpts.skipInstancePublication = true;
|
|
153
165
|
deployOpts.skipClassPublication = true;
|
|
154
166
|
deployOpts.skipInitialization = false;
|
|
167
|
+
// Register the contract with the secret key before deployment
|
|
168
|
+
tokenInstance = await deploy.getInstance(deployOpts);
|
|
169
|
+
token = PrivateTokenContract.at(tokenInstance.address, this.wallet);
|
|
170
|
+
await this.wallet.registerContract(tokenInstance, PrivateTokenContract.artifact, tokenSecretKey);
|
|
155
171
|
} else {
|
|
156
172
|
throw new Error(`Unsupported token contract type: ${this.config.contract}`);
|
|
157
173
|
}
|
|
158
|
-
const address = (await deploy.getInstance(deployOpts)).address;
|
|
159
|
-
|
|
174
|
+
const address = tokenInstance?.address ?? (await deploy.getInstance(deployOpts)).address;
|
|
175
|
+
const metadata = await this.wallet.getContractMetadata(address);
|
|
176
|
+
if (metadata.isContractPublished) {
|
|
160
177
|
this.log.info(`Token at ${address.toString()} already deployed`);
|
|
161
|
-
|
|
178
|
+
await deploy.register();
|
|
162
179
|
} else {
|
|
163
180
|
this.log.info(`Deploying token contract at ${address.toString()}`);
|
|
164
|
-
const
|
|
165
|
-
|
|
181
|
+
const txHash = await deploy.send({
|
|
182
|
+
...deployOpts,
|
|
183
|
+
wait: NO_WAIT
|
|
184
|
+
});
|
|
166
185
|
this.log.info(`Sent tx for token setup with hash ${txHash.toString()}`);
|
|
167
|
-
|
|
186
|
+
await this.withNoMinTxsPerBlock(async ()=>{
|
|
187
|
+
await waitForTx(this.aztecNode, txHash, {
|
|
168
188
|
timeout: this.config.txMinedWaitSeconds
|
|
169
|
-
})
|
|
189
|
+
});
|
|
190
|
+
return token;
|
|
191
|
+
});
|
|
170
192
|
}
|
|
193
|
+
return token;
|
|
171
194
|
}
|
|
172
195
|
/**
|
|
173
196
|
* Checks if the token contract is deployed and deploys it if necessary.
|
|
174
197
|
* @param wallet - Wallet to deploy the token contract from.
|
|
175
198
|
* @returns The TokenContract instance.
|
|
176
|
-
*/ setupTokenContract(deployer, contractAddressSalt, name, ticker, decimals = 18) {
|
|
199
|
+
*/ async setupTokenContract(deployer, contractAddressSalt, name, ticker, decimals = 18) {
|
|
177
200
|
const deployOpts = {
|
|
178
201
|
from: deployer,
|
|
179
202
|
contractAddressSalt,
|
|
180
203
|
universalDeploy: true
|
|
181
204
|
};
|
|
182
205
|
const deploy = TokenContract.deploy(this.wallet, deployer, name, ticker, decimals);
|
|
183
|
-
|
|
206
|
+
const instance = await this.registerOrDeployContract('Token - ' + name, deploy, deployOpts);
|
|
207
|
+
return TokenContract.at(instance.address, this.wallet);
|
|
184
208
|
}
|
|
185
209
|
async setupAmmContract(deployer, contractAddressSalt, token0, token1, lpToken) {
|
|
186
210
|
const deployOpts = {
|
|
@@ -189,15 +213,16 @@ export class BotFactory {
|
|
|
189
213
|
universalDeploy: true
|
|
190
214
|
};
|
|
191
215
|
const deploy = AMMContract.deploy(this.wallet, token0.address, token1.address, lpToken.address);
|
|
192
|
-
const
|
|
216
|
+
const instance = await this.registerOrDeployContract('AMM', deploy, deployOpts);
|
|
217
|
+
const amm = AMMContract.at(instance.address, this.wallet);
|
|
193
218
|
this.log.info(`AMM deployed at ${amm.address}`);
|
|
194
|
-
const
|
|
195
|
-
from: deployer
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
timeout: this.config.txMinedWaitSeconds
|
|
219
|
+
const minterReceipt = await lpToken.methods.set_minter(amm.address, true).send({
|
|
220
|
+
from: deployer,
|
|
221
|
+
wait: {
|
|
222
|
+
timeout: this.config.txMinedWaitSeconds
|
|
223
|
+
}
|
|
200
224
|
});
|
|
225
|
+
this.log.info(`Set LP token minter to AMM txHash=${minterReceipt.txHash.toString()}`);
|
|
201
226
|
this.log.info(`Liquidity token initialized`);
|
|
202
227
|
return amm;
|
|
203
228
|
}
|
|
@@ -230,45 +255,52 @@ export class BotFactory {
|
|
|
230
255
|
caller: amm.address,
|
|
231
256
|
call: await token1.methods.transfer_to_public_and_prepare_private_balance_increase(liquidityProvider, amm.address, amount1Max, authwitNonce).getFunctionCall()
|
|
232
257
|
});
|
|
233
|
-
const
|
|
258
|
+
const mintReceipt = await new BatchCall(this.wallet, [
|
|
234
259
|
token0.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
|
|
235
260
|
token1.methods.mint_to_private(liquidityProvider, MINT_BALANCE)
|
|
236
261
|
]).send({
|
|
237
|
-
from: liquidityProvider
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
timeout: this.config.txMinedWaitSeconds
|
|
262
|
+
from: liquidityProvider,
|
|
263
|
+
wait: {
|
|
264
|
+
timeout: this.config.txMinedWaitSeconds
|
|
265
|
+
}
|
|
242
266
|
});
|
|
243
|
-
|
|
267
|
+
this.log.info(`Sent mint tx: ${mintReceipt.txHash.toString()}`);
|
|
268
|
+
const addLiquidityReceipt = await amm.methods.add_liquidity(amount0Max, amount1Max, amount0Min, amount1Min, authwitNonce).send({
|
|
244
269
|
from: liquidityProvider,
|
|
245
270
|
authWitnesses: [
|
|
246
271
|
token0Authwit,
|
|
247
272
|
token1Authwit
|
|
248
|
-
]
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
timeout: this.config.txMinedWaitSeconds
|
|
273
|
+
],
|
|
274
|
+
wait: {
|
|
275
|
+
timeout: this.config.txMinedWaitSeconds
|
|
276
|
+
}
|
|
253
277
|
});
|
|
278
|
+
this.log.info(`Sent tx to add liquidity to the AMM: ${addLiquidityReceipt.txHash.toString()}`);
|
|
254
279
|
this.log.info(`Liquidity added`);
|
|
255
280
|
const [newT0Bal, newT1Bal, newLPBal] = await getPrivateBalances();
|
|
256
281
|
this.log.info(`Updated private balances of ${defaultAccountAddress} after minting and funding AMM: token0=${newT0Bal}, token1=${newT1Bal}, lp=${newLPBal}`);
|
|
257
282
|
}
|
|
258
283
|
async registerOrDeployContract(name, deploy, deployOpts) {
|
|
259
|
-
const
|
|
260
|
-
|
|
284
|
+
const instance = await deploy.getInstance(deployOpts);
|
|
285
|
+
const address = instance.address;
|
|
286
|
+
const metadata = await this.wallet.getContractMetadata(address);
|
|
287
|
+
if (metadata.isContractPublished) {
|
|
261
288
|
this.log.info(`Contract ${name} at ${address.toString()} already deployed`);
|
|
262
|
-
|
|
289
|
+
await deploy.register();
|
|
263
290
|
} else {
|
|
264
291
|
this.log.info(`Deploying contract ${name} at ${address.toString()}`);
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
292
|
+
await this.withNoMinTxsPerBlock(async ()=>{
|
|
293
|
+
const txHash = await deploy.send({
|
|
294
|
+
...deployOpts,
|
|
295
|
+
wait: NO_WAIT
|
|
296
|
+
});
|
|
297
|
+
this.log.info(`Sent contract ${name} setup tx with hash ${txHash.toString()}`);
|
|
298
|
+
return waitForTx(this.aztecNode, txHash, {
|
|
269
299
|
timeout: this.config.txMinedWaitSeconds
|
|
270
|
-
})
|
|
300
|
+
});
|
|
301
|
+
});
|
|
271
302
|
}
|
|
303
|
+
return instance;
|
|
272
304
|
}
|
|
273
305
|
/**
|
|
274
306
|
* Mints private and public tokens for the sender if their balance is below the minimum.
|
|
@@ -295,14 +327,16 @@ export class BotFactory {
|
|
|
295
327
|
this.log.info(`Skipping minting as ${minter.toString()} has enough tokens`);
|
|
296
328
|
return;
|
|
297
329
|
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
330
|
+
await this.withNoMinTxsPerBlock(async ()=>{
|
|
331
|
+
const txHash = await new BatchCall(token.wallet, calls).send({
|
|
332
|
+
from: minter,
|
|
333
|
+
wait: NO_WAIT
|
|
334
|
+
});
|
|
335
|
+
this.log.info(`Sent token mint tx with hash ${txHash.toString()}`);
|
|
336
|
+
return waitForTx(this.aztecNode, txHash, {
|
|
304
337
|
timeout: this.config.txMinedWaitSeconds
|
|
305
|
-
})
|
|
338
|
+
});
|
|
339
|
+
});
|
|
306
340
|
}
|
|
307
341
|
/**
|
|
308
342
|
* Gets or creates a bridge claim for the recipient.
|