@aztec/bot 0.0.0-test.1 → 0.0.1-commit.b655e406
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 +12 -17
- package/dest/bot.d.ts.map +1 -1
- package/dest/bot.js +24 -86
- package/dest/config.d.ts +66 -41
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +55 -32
- package/dest/factory.d.ts +38 -16
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +277 -132
- package/dest/index.d.ts +3 -1
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +3 -1
- package/dest/interface.d.ts +11 -0
- package/dest/interface.d.ts.map +1 -1
- package/dest/interface.js +5 -0
- package/dest/rpc.d.ts +0 -6
- package/dest/rpc.d.ts.map +1 -1
- package/dest/rpc.js +0 -11
- package/dest/runner.d.ts +14 -10
- 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 +7 -4
- package/dest/utils.d.ts.map +1 -1
- package/dest/utils.js +14 -5
- package/package.json +24 -21
- 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 +321 -145
- 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/dest/factory.js
CHANGED
|
@@ -1,229 +1,374 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { SchnorrAccountContract } from '@aztec/accounts/schnorr';
|
|
2
|
+
import { getInitialTestAccountsData } from '@aztec/accounts/testing';
|
|
3
|
+
import { AztecAddress } from '@aztec/aztec.js/addresses';
|
|
4
|
+
import { BatchCall } from '@aztec/aztec.js/contracts';
|
|
5
|
+
import { L1FeeJuicePortalManager } from '@aztec/aztec.js/ethereum';
|
|
6
|
+
import { FeeJuicePaymentMethodWithClaim } from '@aztec/aztec.js/fee';
|
|
7
|
+
import { createLogger } from '@aztec/aztec.js/log';
|
|
8
|
+
import { waitForL1ToL2MessageReady } from '@aztec/aztec.js/messaging';
|
|
9
|
+
import { createEthereumChain, createExtendedL1Client } from '@aztec/ethereum';
|
|
5
10
|
import { Fr } from '@aztec/foundation/fields';
|
|
6
|
-
import {
|
|
11
|
+
import { Timer } from '@aztec/foundation/timer';
|
|
12
|
+
import { AMMContract } from '@aztec/noir-contracts.js/AMM';
|
|
13
|
+
import { PrivateTokenContract } from '@aztec/noir-contracts.js/PrivateToken';
|
|
7
14
|
import { TokenContract } from '@aztec/noir-contracts.js/Token';
|
|
15
|
+
import { GasSettings } from '@aztec/stdlib/gas';
|
|
8
16
|
import { deriveSigningKey } from '@aztec/stdlib/keys';
|
|
9
|
-
import {
|
|
10
|
-
import { SupportedTokenContracts, getVersions } from './config.js';
|
|
17
|
+
import { SupportedTokenContracts } from './config.js';
|
|
11
18
|
import { getBalances, getPrivateBalance, isStandardTokenContract } from './utils.js';
|
|
12
19
|
const MINT_BALANCE = 1e12;
|
|
13
20
|
const MIN_BALANCE = 1e3;
|
|
14
21
|
export class BotFactory {
|
|
15
22
|
config;
|
|
16
|
-
|
|
17
|
-
|
|
23
|
+
wallet;
|
|
24
|
+
store;
|
|
25
|
+
aztecNode;
|
|
26
|
+
aztecNodeAdmin;
|
|
18
27
|
log;
|
|
19
|
-
constructor(config,
|
|
28
|
+
constructor(config, wallet, store, aztecNode, aztecNodeAdmin){
|
|
20
29
|
this.config = config;
|
|
30
|
+
this.wallet = wallet;
|
|
31
|
+
this.store = store;
|
|
32
|
+
this.aztecNode = aztecNode;
|
|
33
|
+
this.aztecNodeAdmin = aztecNodeAdmin;
|
|
21
34
|
this.log = createLogger('bot');
|
|
22
|
-
if (config.flushSetupTransactions && !dependencies.node) {
|
|
23
|
-
throw new Error(`Either a node client or node url must be provided if transaction flushing is requested`);
|
|
24
|
-
}
|
|
25
|
-
if (config.senderPrivateKey && !dependencies.node) {
|
|
26
|
-
throw new Error(`Either a node client or node url must be provided for bridging L1 fee juice to deploy an account with private key`);
|
|
27
|
-
}
|
|
28
|
-
if (!dependencies.pxe && !config.pxeUrl) {
|
|
29
|
-
throw new Error(`Either a PXE client or a PXE URL must be provided`);
|
|
30
|
-
}
|
|
31
|
-
this.node = dependencies.node;
|
|
32
|
-
if (dependencies.pxe) {
|
|
33
|
-
this.log.info(`Using local PXE`);
|
|
34
|
-
this.pxe = dependencies.pxe;
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
this.log.info(`Using remote PXE at ${config.pxeUrl}`);
|
|
38
|
-
this.pxe = createPXEClient(config.pxeUrl, getVersions(), makeTracedFetch([
|
|
39
|
-
1,
|
|
40
|
-
2,
|
|
41
|
-
3
|
|
42
|
-
], false));
|
|
43
35
|
}
|
|
44
36
|
/**
|
|
45
37
|
* Initializes a new bot by setting up the sender account, registering the recipient,
|
|
46
38
|
* deploying the token contract, and minting tokens if necessary.
|
|
47
39
|
*/ async setup() {
|
|
48
|
-
const recipient = await this.
|
|
49
|
-
const
|
|
50
|
-
const token = await this.setupToken(
|
|
51
|
-
await this.mintTokens(token);
|
|
40
|
+
const recipient = (await this.wallet.createAccount()).address;
|
|
41
|
+
const defaultAccountAddress = await this.setupAccount();
|
|
42
|
+
const token = await this.setupToken(defaultAccountAddress);
|
|
43
|
+
await this.mintTokens(token, defaultAccountAddress);
|
|
52
44
|
return {
|
|
53
|
-
wallet,
|
|
45
|
+
wallet: this.wallet,
|
|
46
|
+
defaultAccountAddress,
|
|
54
47
|
token,
|
|
55
|
-
|
|
48
|
+
node: this.aztecNode,
|
|
56
49
|
recipient
|
|
57
50
|
};
|
|
58
51
|
}
|
|
52
|
+
async setupAmm() {
|
|
53
|
+
const defaultAccountAddress = await this.setupAccount();
|
|
54
|
+
const token0 = await this.setupTokenContract(defaultAccountAddress, this.config.tokenSalt, 'BotToken0', 'BOT0');
|
|
55
|
+
const token1 = await this.setupTokenContract(defaultAccountAddress, this.config.tokenSalt, 'BotToken1', 'BOT1');
|
|
56
|
+
const liquidityToken = await this.setupTokenContract(defaultAccountAddress, this.config.tokenSalt, 'BotLPToken', 'BOTLP');
|
|
57
|
+
const amm = await this.setupAmmContract(defaultAccountAddress, this.config.tokenSalt, token0, token1, liquidityToken);
|
|
58
|
+
await this.fundAmm(defaultAccountAddress, defaultAccountAddress, amm, token0, token1, liquidityToken);
|
|
59
|
+
this.log.info(`AMM initialized and funded`);
|
|
60
|
+
return {
|
|
61
|
+
wallet: this.wallet,
|
|
62
|
+
defaultAccountAddress,
|
|
63
|
+
amm,
|
|
64
|
+
token0,
|
|
65
|
+
token1,
|
|
66
|
+
node: this.aztecNode
|
|
67
|
+
};
|
|
68
|
+
}
|
|
59
69
|
/**
|
|
60
70
|
* Checks if the sender account contract is initialized, and initializes it if necessary.
|
|
61
71
|
* @returns The sender wallet.
|
|
62
72
|
*/ async setupAccount() {
|
|
63
|
-
|
|
64
|
-
|
|
73
|
+
const privateKey = this.config.senderPrivateKey?.getValue();
|
|
74
|
+
if (privateKey) {
|
|
75
|
+
this.log.info(`Setting up account with provided private key`);
|
|
76
|
+
return await this.setupAccountWithPrivateKey(privateKey);
|
|
65
77
|
} else {
|
|
78
|
+
this.log.info(`Setting up test account`);
|
|
66
79
|
return await this.setupTestAccount();
|
|
67
80
|
}
|
|
68
81
|
}
|
|
69
|
-
async setupAccountWithPrivateKey(
|
|
70
|
-
const salt = Fr.ONE;
|
|
71
|
-
const signingKey = deriveSigningKey(
|
|
72
|
-
const
|
|
73
|
-
|
|
82
|
+
async setupAccountWithPrivateKey(secret) {
|
|
83
|
+
const salt = this.config.senderSalt ?? Fr.ONE;
|
|
84
|
+
const signingKey = deriveSigningKey(secret);
|
|
85
|
+
const accountData = {
|
|
86
|
+
secret,
|
|
87
|
+
salt,
|
|
88
|
+
contract: new SchnorrAccountContract(signingKey)
|
|
89
|
+
};
|
|
90
|
+
const accountManager = await this.wallet.createAccount(accountData);
|
|
91
|
+
const isInit = (await this.wallet.getContractMetadata(accountManager.address)).isContractInitialized;
|
|
74
92
|
if (isInit) {
|
|
75
|
-
this.log.info(`Account at ${
|
|
76
|
-
const
|
|
77
|
-
|
|
93
|
+
this.log.info(`Account at ${accountManager.address.toString()} already initialized`);
|
|
94
|
+
const timer = new Timer();
|
|
95
|
+
const address = accountManager.address;
|
|
96
|
+
this.log.info(`Account at ${address} registered. duration=${timer.ms()}`);
|
|
97
|
+
await this.store.deleteBridgeClaim(address);
|
|
98
|
+
return address;
|
|
78
99
|
} else {
|
|
79
|
-
const address =
|
|
100
|
+
const address = accountManager.address;
|
|
80
101
|
this.log.info(`Deploying account at ${address}`);
|
|
81
|
-
const claim = await this.
|
|
82
|
-
const
|
|
83
|
-
const
|
|
84
|
-
const
|
|
102
|
+
const claim = await this.getOrCreateBridgeClaim(address);
|
|
103
|
+
const paymentMethod = new FeeJuicePaymentMethodWithClaim(accountManager.address, claim);
|
|
104
|
+
const deployMethod = await accountManager.getDeployMethod();
|
|
105
|
+
const maxFeesPerGas = (await this.aztecNode.getCurrentBaseFees()).mul(1 + this.config.baseFeePadding);
|
|
106
|
+
const gasSettings = GasSettings.default({
|
|
107
|
+
maxFeesPerGas
|
|
108
|
+
});
|
|
109
|
+
const sentTx = deployMethod.send({
|
|
110
|
+
from: AztecAddress.ZERO,
|
|
85
111
|
fee: {
|
|
112
|
+
gasSettings,
|
|
86
113
|
paymentMethod
|
|
87
114
|
}
|
|
88
115
|
});
|
|
89
116
|
const txHash = await sentTx.getTxHash();
|
|
90
|
-
this.log.info(`Sent tx with hash ${txHash.toString()}`);
|
|
91
|
-
await this.
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
timeout: this.config.txMinedWaitSeconds
|
|
95
|
-
});
|
|
117
|
+
this.log.info(`Sent tx for account deployment with hash ${txHash.toString()}`);
|
|
118
|
+
await this.withNoMinTxsPerBlock(()=>sentTx.wait({
|
|
119
|
+
timeout: this.config.txMinedWaitSeconds
|
|
120
|
+
}));
|
|
96
121
|
this.log.info(`Account deployed at ${address}`);
|
|
97
|
-
|
|
122
|
+
// Clean up the consumed bridge claim
|
|
123
|
+
await this.store.deleteBridgeClaim(address);
|
|
124
|
+
return accountManager.address;
|
|
98
125
|
}
|
|
99
126
|
}
|
|
100
127
|
async setupTestAccount() {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
this.log.info(`Funded test account registered: ${wallet.getAddress()}`);
|
|
110
|
-
}
|
|
111
|
-
return wallet;
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Registers the recipient for txs in the pxe.
|
|
115
|
-
*/ async registerRecipient() {
|
|
116
|
-
const recipient = await this.pxe.registerAccount(this.config.recipientEncryptionSecret, Fr.ONE);
|
|
117
|
-
return recipient.address;
|
|
128
|
+
const [initialAccountData] = await getInitialTestAccountsData();
|
|
129
|
+
const accountData = {
|
|
130
|
+
secret: initialAccountData.secret,
|
|
131
|
+
salt: initialAccountData.salt,
|
|
132
|
+
contract: new SchnorrAccountContract(initialAccountData.signingKey)
|
|
133
|
+
};
|
|
134
|
+
const accountManager = await this.wallet.createAccount(accountData);
|
|
135
|
+
return accountManager.address;
|
|
118
136
|
}
|
|
119
137
|
/**
|
|
120
138
|
* Checks if the token contract is deployed and deploys it if necessary.
|
|
121
139
|
* @param wallet - Wallet to deploy the token contract from.
|
|
122
140
|
* @returns The TokenContract instance.
|
|
123
|
-
*/ async setupToken(
|
|
141
|
+
*/ async setupToken(sender) {
|
|
124
142
|
let deploy;
|
|
125
143
|
const deployOpts = {
|
|
144
|
+
from: sender,
|
|
126
145
|
contractAddressSalt: this.config.tokenSalt,
|
|
127
146
|
universalDeploy: true
|
|
128
147
|
};
|
|
129
148
|
if (this.config.contract === SupportedTokenContracts.TokenContract) {
|
|
130
|
-
deploy = TokenContract.deploy(wallet,
|
|
131
|
-
} else if (this.config.contract === SupportedTokenContracts.
|
|
132
|
-
deploy =
|
|
133
|
-
deployOpts.
|
|
134
|
-
deployOpts.
|
|
149
|
+
deploy = TokenContract.deploy(this.wallet, sender, 'BotToken', 'BOT', 18);
|
|
150
|
+
} else if (this.config.contract === SupportedTokenContracts.PrivateTokenContract) {
|
|
151
|
+
deploy = PrivateTokenContract.deploy(this.wallet, MINT_BALANCE, sender);
|
|
152
|
+
deployOpts.skipInstancePublication = true;
|
|
153
|
+
deployOpts.skipClassPublication = true;
|
|
135
154
|
deployOpts.skipInitialization = false;
|
|
136
|
-
deployOpts.skipPublicSimulation = true;
|
|
137
155
|
} else {
|
|
138
156
|
throw new Error(`Unsupported token contract type: ${this.config.contract}`);
|
|
139
157
|
}
|
|
140
158
|
const address = (await deploy.getInstance(deployOpts)).address;
|
|
141
|
-
if ((await this.
|
|
159
|
+
if ((await this.wallet.getContractMetadata(address)).isContractPublished) {
|
|
142
160
|
this.log.info(`Token at ${address.toString()} already deployed`);
|
|
143
161
|
return deploy.register();
|
|
144
162
|
} else {
|
|
145
163
|
this.log.info(`Deploying token contract at ${address.toString()}`);
|
|
146
164
|
const sentTx = deploy.send(deployOpts);
|
|
147
165
|
const txHash = await sentTx.getTxHash();
|
|
148
|
-
this.log.info(`Sent tx with hash ${txHash.toString()}`);
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
166
|
+
this.log.info(`Sent tx for token setup with hash ${txHash.toString()}`);
|
|
167
|
+
return this.withNoMinTxsPerBlock(()=>sentTx.deployed({
|
|
168
|
+
timeout: this.config.txMinedWaitSeconds
|
|
169
|
+
}));
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Checks if the token contract is deployed and deploys it if necessary.
|
|
174
|
+
* @param wallet - Wallet to deploy the token contract from.
|
|
175
|
+
* @returns The TokenContract instance.
|
|
176
|
+
*/ setupTokenContract(deployer, contractAddressSalt, name, ticker, decimals = 18) {
|
|
177
|
+
const deployOpts = {
|
|
178
|
+
from: deployer,
|
|
179
|
+
contractAddressSalt,
|
|
180
|
+
universalDeploy: true
|
|
181
|
+
};
|
|
182
|
+
const deploy = TokenContract.deploy(this.wallet, deployer, name, ticker, decimals);
|
|
183
|
+
return this.registerOrDeployContract('Token - ' + name, deploy, deployOpts);
|
|
184
|
+
}
|
|
185
|
+
async setupAmmContract(deployer, contractAddressSalt, token0, token1, lpToken) {
|
|
186
|
+
const deployOpts = {
|
|
187
|
+
from: deployer,
|
|
188
|
+
contractAddressSalt,
|
|
189
|
+
universalDeploy: true
|
|
190
|
+
};
|
|
191
|
+
const deploy = AMMContract.deploy(this.wallet, token0.address, token1.address, lpToken.address);
|
|
192
|
+
const amm = await this.registerOrDeployContract('AMM', deploy, deployOpts);
|
|
193
|
+
this.log.info(`AMM deployed at ${amm.address}`);
|
|
194
|
+
const minterTx = lpToken.methods.set_minter(amm.address, true).send({
|
|
195
|
+
from: deployer
|
|
196
|
+
});
|
|
197
|
+
this.log.info(`Set LP token minter to AMM txHash=${(await minterTx.getTxHash()).toString()}`);
|
|
198
|
+
await minterTx.wait({
|
|
199
|
+
timeout: this.config.txMinedWaitSeconds
|
|
200
|
+
});
|
|
201
|
+
this.log.info(`Liquidity token initialized`);
|
|
202
|
+
return amm;
|
|
203
|
+
}
|
|
204
|
+
async fundAmm(defaultAccountAddress, liquidityProvider, amm, token0, token1, lpToken) {
|
|
205
|
+
const getPrivateBalances = ()=>Promise.all([
|
|
206
|
+
token0.methods.balance_of_private(liquidityProvider).simulate({
|
|
207
|
+
from: liquidityProvider
|
|
208
|
+
}),
|
|
209
|
+
token1.methods.balance_of_private(liquidityProvider).simulate({
|
|
210
|
+
from: liquidityProvider
|
|
211
|
+
}),
|
|
212
|
+
lpToken.methods.balance_of_private(liquidityProvider).simulate({
|
|
213
|
+
from: liquidityProvider
|
|
214
|
+
})
|
|
215
|
+
]);
|
|
216
|
+
const authwitNonce = Fr.random();
|
|
217
|
+
// keep some tokens for swapping
|
|
218
|
+
const amount0Max = MINT_BALANCE / 2;
|
|
219
|
+
const amount0Min = MINT_BALANCE / 4;
|
|
220
|
+
const amount1Max = MINT_BALANCE / 2;
|
|
221
|
+
const amount1Min = MINT_BALANCE / 4;
|
|
222
|
+
const [t0Bal, t1Bal, lpBal] = await getPrivateBalances();
|
|
223
|
+
this.log.info(`Minting ${MINT_BALANCE} tokens of each BotToken0 and BotToken1. Current private balances of ${liquidityProvider}: token0=${t0Bal}, token1=${t1Bal}, lp=${lpBal}`);
|
|
224
|
+
// Add authwitnesses for the transfers in AMM::add_liquidity function
|
|
225
|
+
const token0Authwit = await this.wallet.createAuthWit(defaultAccountAddress, {
|
|
226
|
+
caller: amm.address,
|
|
227
|
+
call: await token0.methods.transfer_to_public_and_prepare_private_balance_increase(liquidityProvider, amm.address, amount0Max, authwitNonce).getFunctionCall()
|
|
228
|
+
});
|
|
229
|
+
const token1Authwit = await this.wallet.createAuthWit(defaultAccountAddress, {
|
|
230
|
+
caller: amm.address,
|
|
231
|
+
call: await token1.methods.transfer_to_public_and_prepare_private_balance_increase(liquidityProvider, amm.address, amount1Max, authwitNonce).getFunctionCall()
|
|
232
|
+
});
|
|
233
|
+
const mintTx = new BatchCall(this.wallet, [
|
|
234
|
+
token0.methods.mint_to_private(liquidityProvider, MINT_BALANCE),
|
|
235
|
+
token1.methods.mint_to_private(liquidityProvider, MINT_BALANCE)
|
|
236
|
+
]).send({
|
|
237
|
+
from: liquidityProvider
|
|
238
|
+
});
|
|
239
|
+
this.log.info(`Sent mint tx: ${(await mintTx.getTxHash()).toString()}`);
|
|
240
|
+
await mintTx.wait({
|
|
241
|
+
timeout: this.config.txMinedWaitSeconds
|
|
242
|
+
});
|
|
243
|
+
const addLiquidityTx = amm.methods.add_liquidity(amount0Max, amount1Max, amount0Min, amount1Min, authwitNonce).send({
|
|
244
|
+
from: liquidityProvider,
|
|
245
|
+
authWitnesses: [
|
|
246
|
+
token0Authwit,
|
|
247
|
+
token1Authwit
|
|
248
|
+
]
|
|
249
|
+
});
|
|
250
|
+
this.log.info(`Sent tx to add liquidity to the AMM: ${(await addLiquidityTx.getTxHash()).toString()}`);
|
|
251
|
+
await addLiquidityTx.wait({
|
|
252
|
+
timeout: this.config.txMinedWaitSeconds
|
|
253
|
+
});
|
|
254
|
+
this.log.info(`Liquidity added`);
|
|
255
|
+
const [newT0Bal, newT1Bal, newLPBal] = await getPrivateBalances();
|
|
256
|
+
this.log.info(`Updated private balances of ${defaultAccountAddress} after minting and funding AMM: token0=${newT0Bal}, token1=${newT1Bal}, lp=${newLPBal}`);
|
|
257
|
+
}
|
|
258
|
+
async registerOrDeployContract(name, deploy, deployOpts) {
|
|
259
|
+
const address = (await deploy.getInstance(deployOpts)).address;
|
|
260
|
+
if ((await this.wallet.getContractMetadata(address)).isContractPublished) {
|
|
261
|
+
this.log.info(`Contract ${name} at ${address.toString()} already deployed`);
|
|
262
|
+
return deploy.register();
|
|
263
|
+
} else {
|
|
264
|
+
this.log.info(`Deploying contract ${name} at ${address.toString()}`);
|
|
265
|
+
const sentTx = deploy.send(deployOpts);
|
|
266
|
+
const txHash = await sentTx.getTxHash();
|
|
267
|
+
this.log.info(`Sent contract ${name} setup tx with hash ${txHash.toString()}`);
|
|
268
|
+
return this.withNoMinTxsPerBlock(()=>sentTx.deployed({
|
|
269
|
+
timeout: this.config.txMinedWaitSeconds
|
|
270
|
+
}));
|
|
154
271
|
}
|
|
155
272
|
}
|
|
156
273
|
/**
|
|
157
274
|
* Mints private and public tokens for the sender if their balance is below the minimum.
|
|
158
275
|
* @param token - Token contract.
|
|
159
|
-
*/ async mintTokens(token) {
|
|
160
|
-
const sender = token.wallet.getAddress();
|
|
276
|
+
*/ async mintTokens(token, minter) {
|
|
161
277
|
const isStandardToken = isStandardTokenContract(token);
|
|
162
278
|
let privateBalance = 0n;
|
|
163
279
|
let publicBalance = 0n;
|
|
164
280
|
if (isStandardToken) {
|
|
165
|
-
({ privateBalance, publicBalance } = await getBalances(token,
|
|
281
|
+
({ privateBalance, publicBalance } = await getBalances(token, minter));
|
|
166
282
|
} else {
|
|
167
|
-
privateBalance = await getPrivateBalance(token,
|
|
283
|
+
privateBalance = await getPrivateBalance(token, minter);
|
|
168
284
|
}
|
|
169
285
|
const calls = [];
|
|
170
286
|
if (privateBalance < MIN_BALANCE) {
|
|
171
|
-
this.log.info(`Minting private tokens for ${
|
|
172
|
-
|
|
173
|
-
calls.push(isStandardToken ? await token.methods.mint_to_private(from, sender, MINT_BALANCE).request() : await token.methods.mint(MINT_BALANCE, sender).request());
|
|
287
|
+
this.log.info(`Minting private tokens for ${minter.toString()}`);
|
|
288
|
+
calls.push(isStandardToken ? token.methods.mint_to_private(minter, MINT_BALANCE) : token.methods.mint(MINT_BALANCE, minter));
|
|
174
289
|
}
|
|
175
290
|
if (isStandardToken && publicBalance < MIN_BALANCE) {
|
|
176
|
-
this.log.info(`Minting public tokens for ${
|
|
177
|
-
calls.push(
|
|
291
|
+
this.log.info(`Minting public tokens for ${minter.toString()}`);
|
|
292
|
+
calls.push(token.methods.mint_to_public(minter, MINT_BALANCE));
|
|
178
293
|
}
|
|
179
294
|
if (calls.length === 0) {
|
|
180
|
-
this.log.info(`Skipping minting as ${
|
|
295
|
+
this.log.info(`Skipping minting as ${minter.toString()} has enough tokens`);
|
|
181
296
|
return;
|
|
182
297
|
}
|
|
183
|
-
const sentTx = new BatchCall(token.wallet, calls).send(
|
|
184
|
-
|
|
185
|
-
this.log.info(`Sent tx with hash ${txHash.toString()}`);
|
|
186
|
-
await this.tryFlushTxs();
|
|
187
|
-
this.log.verbose('Waiting for token mint to settle');
|
|
188
|
-
await sentTx.wait({
|
|
189
|
-
timeout: this.config.txMinedWaitSeconds
|
|
298
|
+
const sentTx = new BatchCall(token.wallet, calls).send({
|
|
299
|
+
from: minter
|
|
190
300
|
});
|
|
301
|
+
const txHash = await sentTx.getTxHash();
|
|
302
|
+
this.log.info(`Sent token mint tx with hash ${txHash.toString()}`);
|
|
303
|
+
await this.withNoMinTxsPerBlock(()=>sentTx.wait({
|
|
304
|
+
timeout: this.config.txMinedWaitSeconds
|
|
305
|
+
}));
|
|
191
306
|
}
|
|
192
|
-
|
|
307
|
+
/**
|
|
308
|
+
* Gets or creates a bridge claim for the recipient.
|
|
309
|
+
* Checks if a claim already exists in the store and reuses it if valid.
|
|
310
|
+
* Only creates a new bridge if fee juice balance is below threshold.
|
|
311
|
+
*/ async getOrCreateBridgeClaim(recipient) {
|
|
312
|
+
// Check if we have an existing claim in the store
|
|
313
|
+
const existingClaim = await this.store.getBridgeClaim(recipient);
|
|
314
|
+
if (existingClaim) {
|
|
315
|
+
this.log.info(`Found existing bridge claim for ${recipient.toString()}, checking validity...`);
|
|
316
|
+
// Check if the message is ready on L2
|
|
317
|
+
try {
|
|
318
|
+
const messageHash = Fr.fromHexString(existingClaim.claim.messageHash);
|
|
319
|
+
await this.withNoMinTxsPerBlock(()=>waitForL1ToL2MessageReady(this.aztecNode, messageHash, {
|
|
320
|
+
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds,
|
|
321
|
+
forPublicConsumption: false
|
|
322
|
+
}));
|
|
323
|
+
return existingClaim.claim;
|
|
324
|
+
} catch (err) {
|
|
325
|
+
this.log.warn(`Failed to verify existing claim, creating new one: ${err}`);
|
|
326
|
+
await this.store.deleteBridgeClaim(recipient);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
const claim = await this.bridgeL1FeeJuice(recipient);
|
|
330
|
+
await this.store.saveBridgeClaim(recipient, claim);
|
|
331
|
+
return claim;
|
|
332
|
+
}
|
|
333
|
+
async bridgeL1FeeJuice(recipient) {
|
|
193
334
|
const l1RpcUrls = this.config.l1RpcUrls;
|
|
194
335
|
if (!l1RpcUrls?.length) {
|
|
195
336
|
throw new Error('L1 Rpc url is required to bridge the fee juice to fund the deployment of the account.');
|
|
196
337
|
}
|
|
197
|
-
const mnemonicOrPrivateKey = this.config.l1PrivateKey
|
|
338
|
+
const mnemonicOrPrivateKey = this.config.l1PrivateKey?.getValue() ?? this.config.l1Mnemonic?.getValue();
|
|
198
339
|
if (!mnemonicOrPrivateKey) {
|
|
199
340
|
throw new Error('Either a mnemonic or private key of an L1 account is required to bridge the fee juice to fund the deployment of the account.');
|
|
200
341
|
}
|
|
201
|
-
const { l1ChainId } = await this.
|
|
342
|
+
const { l1ChainId } = await this.aztecNode.getNodeInfo();
|
|
202
343
|
const chain = createEthereumChain(l1RpcUrls, l1ChainId);
|
|
203
|
-
const
|
|
204
|
-
const portal = await L1FeeJuicePortalManager.new(this.
|
|
205
|
-
const
|
|
206
|
-
const
|
|
207
|
-
await
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
344
|
+
const extendedClient = createExtendedL1Client(chain.rpcUrls, mnemonicOrPrivateKey, chain.chainInfo);
|
|
345
|
+
const portal = await L1FeeJuicePortalManager.new(this.aztecNode, extendedClient, this.log);
|
|
346
|
+
const mintAmount = await portal.getTokenManager().getMintAmount();
|
|
347
|
+
const claim = await portal.bridgeTokensPublic(recipient, mintAmount, true);
|
|
348
|
+
await this.withNoMinTxsPerBlock(()=>waitForL1ToL2MessageReady(this.aztecNode, Fr.fromHexString(claim.messageHash), {
|
|
349
|
+
timeoutSeconds: this.config.l1ToL2MessageTimeoutSeconds,
|
|
350
|
+
forPublicConsumption: false
|
|
351
|
+
}));
|
|
352
|
+
this.log.info(`Created a claim for ${mintAmount} L1 fee juice to ${recipient}.`, claim);
|
|
212
353
|
return claim;
|
|
213
354
|
}
|
|
214
|
-
async
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
355
|
+
async withNoMinTxsPerBlock(fn) {
|
|
356
|
+
if (!this.aztecNodeAdmin || !this.config.flushSetupTransactions) {
|
|
357
|
+
this.log.verbose(`No node admin client or flushing not requested (not setting minTxsPerBlock to 0)`);
|
|
358
|
+
return fn();
|
|
359
|
+
}
|
|
360
|
+
const { minTxsPerBlock } = await this.aztecNodeAdmin.getConfig();
|
|
361
|
+
this.log.warn(`Setting sequencer minTxsPerBlock to 0 from ${minTxsPerBlock} to flush setup transactions`);
|
|
362
|
+
await this.aztecNodeAdmin.setConfig({
|
|
363
|
+
minTxsPerBlock: 0
|
|
364
|
+
});
|
|
365
|
+
try {
|
|
366
|
+
return await fn();
|
|
367
|
+
} finally{
|
|
368
|
+
this.log.warn(`Restoring sequencer minTxsPerBlock to ${minTxsPerBlock}`);
|
|
369
|
+
await this.aztecNodeAdmin.setConfig({
|
|
370
|
+
minTxsPerBlock
|
|
371
|
+
});
|
|
227
372
|
}
|
|
228
373
|
}
|
|
229
374
|
}
|
package/dest/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
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 { type BotConfig, getBotConfigFromEnv, getBotDefaultConfig, botConfigMappings, SupportedTokenContracts, } from './config.js';
|
|
4
|
-
export {
|
|
6
|
+
export { getBotRunnerApiHandler } from './rpc.js';
|
|
5
7
|
export * from './interface.js';
|
|
6
8
|
//# sourceMappingURL=index.d.ts.map
|
package/dest/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EACL,KAAK,SAAS,EACd,mBAAmB,EACnB,mBAAmB,EACnB,iBAAiB,EACjB,uBAAuB,GACxB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,EACL,KAAK,SAAS,EACd,mBAAmB,EACnB,mBAAmB,EACnB,iBAAiB,EACjB,uBAAuB,GACxB,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,sBAAsB,EAAE,MAAM,UAAU,CAAC;AAClD,cAAc,gBAAgB,CAAC"}
|
package/dest/index.js
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 { getBotConfigFromEnv, getBotDefaultConfig, botConfigMappings, SupportedTokenContracts } from './config.js';
|
|
4
|
-
export {
|
|
6
|
+
export { getBotRunnerApiHandler } from './rpc.js';
|
|
5
7
|
export * from './interface.js';
|
package/dest/interface.d.ts
CHANGED
|
@@ -1,11 +1,22 @@
|
|
|
1
|
+
import { AztecAddress } from '@aztec/aztec.js/addresses';
|
|
1
2
|
import type { ApiSchemaFor } from '@aztec/stdlib/schemas';
|
|
3
|
+
import { z } from 'zod';
|
|
2
4
|
import { type BotConfig } from './config.js';
|
|
5
|
+
export declare const BotInfoSchema: z.ZodObject<{
|
|
6
|
+
botAddress: import("@aztec/stdlib/schemas").ZodFor<AztecAddress>;
|
|
7
|
+
}, "strip", z.ZodTypeAny, {
|
|
8
|
+
botAddress: AztecAddress;
|
|
9
|
+
}, {
|
|
10
|
+
botAddress?: any;
|
|
11
|
+
}>;
|
|
12
|
+
export type BotInfo = z.infer<typeof BotInfoSchema>;
|
|
3
13
|
export interface BotRunnerApi {
|
|
4
14
|
start(): Promise<void>;
|
|
5
15
|
stop(): Promise<void>;
|
|
6
16
|
run(): Promise<void>;
|
|
7
17
|
setup(): Promise<void>;
|
|
8
18
|
getConfig(): Promise<BotConfig>;
|
|
19
|
+
getInfo(): Promise<BotInfo>;
|
|
9
20
|
update(config: BotConfig): Promise<void>;
|
|
10
21
|
}
|
|
11
22
|
export declare const BotRunnerApiSchema: ApiSchemaFor<BotRunnerApi>;
|
package/dest/interface.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interface.d.ts","sourceRoot":"","sources":["../src/interface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;
|
|
1
|
+
{"version":3,"file":"interface.d.ts","sourceRoot":"","sources":["../src/interface.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAE1D,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,KAAK,SAAS,EAAmB,MAAM,aAAa,CAAC;AAE9D,eAAO,MAAM,aAAa;;;;;;EAExB,CAAC;AAEH,MAAM,MAAM,OAAO,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,aAAa,CAAC,CAAC;AAEpD,MAAM,WAAW,YAAY;IAC3B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACtB,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACrB,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IACvB,SAAS,IAAI,OAAO,CAAC,SAAS,CAAC,CAAC;IAChC,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IAC5B,MAAM,CAAC,MAAM,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1C;AAED,eAAO,MAAM,kBAAkB,EAAE,YAAY,CAAC,YAAY,CAQzD,CAAC"}
|
package/dest/interface.js
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
|
+
import { AztecAddress } from '@aztec/aztec.js/addresses';
|
|
1
2
|
import { z } from 'zod';
|
|
2
3
|
import { BotConfigSchema } from './config.js';
|
|
4
|
+
export const BotInfoSchema = z.object({
|
|
5
|
+
botAddress: AztecAddress.schema
|
|
6
|
+
});
|
|
3
7
|
export const BotRunnerApiSchema = {
|
|
4
8
|
start: z.function().args().returns(z.void()),
|
|
5
9
|
stop: z.function().args().returns(z.void()),
|
|
6
10
|
run: z.function().args().returns(z.void()),
|
|
7
11
|
setup: z.function().args().returns(z.void()),
|
|
12
|
+
getInfo: z.function().args().returns(BotInfoSchema),
|
|
8
13
|
getConfig: z.function().args().returns(BotConfigSchema),
|
|
9
14
|
update: z.function().args(BotConfigSchema).returns(z.void())
|
|
10
15
|
};
|
package/dest/rpc.d.ts
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
1
|
import type { ApiHandler } from '@aztec/foundation/json-rpc/server';
|
|
2
2
|
import type { BotRunner } from './runner.js';
|
|
3
|
-
/**
|
|
4
|
-
* Wraps a bot runner with a JSON RPC HTTP server.
|
|
5
|
-
* @param botRunner - The BotRunner.
|
|
6
|
-
* @returns An JSON-RPC HTTP server
|
|
7
|
-
*/
|
|
8
|
-
export declare function createBotRunnerRpcServer(botRunner: BotRunner): void;
|
|
9
3
|
export declare function getBotRunnerApiHandler(botRunner: BotRunner): ApiHandler;
|
|
10
4
|
//# sourceMappingURL=rpc.d.ts.map
|
package/dest/rpc.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rpc.d.ts","sourceRoot":"","sources":["../src/rpc.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mCAAmC,CAAC;
|
|
1
|
+
{"version":3,"file":"rpc.d.ts","sourceRoot":"","sources":["../src/rpc.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,mCAAmC,CAAC;AAGpE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C,wBAAgB,sBAAsB,CAAC,SAAS,EAAE,SAAS,GAAG,UAAU,CAEvE"}
|
package/dest/rpc.js
CHANGED
|
@@ -1,15 +1,4 @@
|
|
|
1
|
-
import { createTracedJsonRpcServer } from '@aztec/telemetry-client';
|
|
2
1
|
import { BotRunnerApiSchema } from './interface.js';
|
|
3
|
-
/**
|
|
4
|
-
* Wraps a bot runner with a JSON RPC HTTP server.
|
|
5
|
-
* @param botRunner - The BotRunner.
|
|
6
|
-
* @returns An JSON-RPC HTTP server
|
|
7
|
-
*/ export function createBotRunnerRpcServer(botRunner) {
|
|
8
|
-
createTracedJsonRpcServer(botRunner, BotRunnerApiSchema, {
|
|
9
|
-
http200OnError: false,
|
|
10
|
-
healthCheck: botRunner.isHealthy.bind(botRunner)
|
|
11
|
-
});
|
|
12
|
-
}
|
|
13
2
|
export function getBotRunnerApiHandler(botRunner) {
|
|
14
3
|
return [
|
|
15
4
|
botRunner,
|