@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.
Files changed (49) hide show
  1. package/dest/amm_bot.d.ts +33 -0
  2. package/dest/amm_bot.d.ts.map +1 -0
  3. package/dest/amm_bot.js +97 -0
  4. package/dest/base_bot.d.ts +21 -0
  5. package/dest/base_bot.d.ts.map +1 -0
  6. package/dest/base_bot.js +80 -0
  7. package/dest/bot.d.ts +12 -17
  8. package/dest/bot.d.ts.map +1 -1
  9. package/dest/bot.js +24 -86
  10. package/dest/config.d.ts +66 -41
  11. package/dest/config.d.ts.map +1 -1
  12. package/dest/config.js +55 -32
  13. package/dest/factory.d.ts +38 -16
  14. package/dest/factory.d.ts.map +1 -1
  15. package/dest/factory.js +277 -132
  16. package/dest/index.d.ts +3 -1
  17. package/dest/index.d.ts.map +1 -1
  18. package/dest/index.js +3 -1
  19. package/dest/interface.d.ts +11 -0
  20. package/dest/interface.d.ts.map +1 -1
  21. package/dest/interface.js +5 -0
  22. package/dest/rpc.d.ts +0 -6
  23. package/dest/rpc.d.ts.map +1 -1
  24. package/dest/rpc.js +0 -11
  25. package/dest/runner.d.ts +14 -10
  26. package/dest/runner.d.ts.map +1 -1
  27. package/dest/runner.js +33 -25
  28. package/dest/store/bot_store.d.ts +44 -0
  29. package/dest/store/bot_store.d.ts.map +1 -0
  30. package/dest/store/bot_store.js +107 -0
  31. package/dest/store/index.d.ts +2 -0
  32. package/dest/store/index.d.ts.map +1 -0
  33. package/dest/store/index.js +1 -0
  34. package/dest/utils.d.ts +7 -4
  35. package/dest/utils.d.ts.map +1 -1
  36. package/dest/utils.js +14 -5
  37. package/package.json +24 -21
  38. package/src/amm_bot.ts +124 -0
  39. package/src/base_bot.ts +96 -0
  40. package/src/bot.ts +52 -103
  41. package/src/config.ts +66 -38
  42. package/src/factory.ts +321 -145
  43. package/src/index.ts +3 -1
  44. package/src/interface.ts +9 -0
  45. package/src/rpc.ts +0 -13
  46. package/src/runner.ts +38 -21
  47. package/src/store/bot_store.ts +141 -0
  48. package/src/store/index.ts +1 -0
  49. package/src/utils.ts +17 -6
@@ -0,0 +1,33 @@
1
+ import { AztecAddress } from '@aztec/aztec.js/addresses';
2
+ import { SentTx } from '@aztec/aztec.js/contracts';
3
+ import { TxReceipt } from '@aztec/aztec.js/tx';
4
+ import type { AMMContract } from '@aztec/noir-contracts.js/AMM';
5
+ import type { TokenContract } from '@aztec/noir-contracts.js/Token';
6
+ import type { AztecNode, AztecNodeAdmin } from '@aztec/stdlib/interfaces/client';
7
+ import type { TestWallet } from '@aztec/test-wallet/server';
8
+ import { BaseBot } from './base_bot.js';
9
+ import type { BotConfig } from './config.js';
10
+ import type { BotStore } from './store/index.js';
11
+ type Balances = {
12
+ token0: bigint;
13
+ token1: bigint;
14
+ };
15
+ export declare class AmmBot extends BaseBot {
16
+ readonly amm: AMMContract;
17
+ readonly token0: TokenContract;
18
+ readonly token1: TokenContract;
19
+ protected constructor(node: AztecNode, wallet: TestWallet, defaultAccountAddress: AztecAddress, amm: AMMContract, token0: TokenContract, token1: TokenContract, config: BotConfig);
20
+ static create(config: BotConfig, wallet: TestWallet, aztecNode: AztecNode, aztecNodeAdmin: AztecNodeAdmin | undefined, store: BotStore): Promise<AmmBot>;
21
+ protected createAndSendTx(logCtx: object): Promise<SentTx>;
22
+ protected onTxMined(receipt: TxReceipt, logCtx: object): Promise<void>;
23
+ getAmmBalances(): Promise<Balances>;
24
+ getBalances(): Promise<{
25
+ senderPublic: Balances;
26
+ senderPrivate: Balances;
27
+ amm: Balances;
28
+ }>;
29
+ private getPublicBalanceFor;
30
+ private getPrivateBalanceFor;
31
+ }
32
+ export {};
33
+ //# sourceMappingURL=amm_bot.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"amm_bot.d.ts","sourceRoot":"","sources":["../src/amm_bot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAC;AAEnD,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAE/C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAChE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gCAAgC,CAAC;AACpE,OAAO,KAAK,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AACjF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAE5D,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAKjD,KAAK,QAAQ,GAAG;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAEnD,qBAAa,MAAO,SAAQ,OAAO;aAKf,GAAG,EAAE,WAAW;aAChB,MAAM,EAAE,aAAa;aACrB,MAAM,EAAE,aAAa;IANvC,SAAS,aACP,IAAI,EAAE,SAAS,EACf,MAAM,EAAE,UAAU,EAClB,qBAAqB,EAAE,YAAY,EACnB,GAAG,EAAE,WAAW,EAChB,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,aAAa,EACrC,MAAM,EAAE,SAAS;WAKN,MAAM,CACjB,MAAM,EAAE,SAAS,EACjB,MAAM,EAAE,UAAU,EAClB,SAAS,EAAE,SAAS,EACpB,cAAc,EAAE,cAAc,GAAG,SAAS,EAC1C,KAAK,EAAE,QAAQ,GACd,OAAO,CAAC,MAAM,CAAC;cAWF,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;cA4CvC,SAAS,CAAC,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAK9E,cAAc,IAAI,OAAO,CAAC,QAAQ,CAAC;IAI7B,WAAW,IAAI,OAAO,CAAC;QAAE,YAAY,EAAE,QAAQ,CAAC;QAAC,aAAa,EAAE,QAAQ,CAAC;QAAC,GAAG,EAAE,QAAQ,CAAA;KAAE,CAAC;YAQzF,mBAAmB;YAMnB,oBAAoB;CAMnC"}
@@ -0,0 +1,97 @@
1
+ import { Fr } from '@aztec/aztec.js/fields';
2
+ import { jsonStringify } from '@aztec/foundation/json-rpc';
3
+ import { BaseBot } from './base_bot.js';
4
+ import { BotFactory } from './factory.js';
5
+ const TRANSFER_BASE_AMOUNT = 1_000;
6
+ const TRANSFER_VARIANCE = 200;
7
+ export class AmmBot extends BaseBot {
8
+ amm;
9
+ token0;
10
+ token1;
11
+ constructor(node, wallet, defaultAccountAddress, amm, token0, token1, config){
12
+ super(node, wallet, defaultAccountAddress, config), this.amm = amm, this.token0 = token0, this.token1 = token1;
13
+ }
14
+ static async create(config, wallet, aztecNode, aztecNodeAdmin, store) {
15
+ const { defaultAccountAddress, token0, token1, amm } = await new BotFactory(config, wallet, store, aztecNode, aztecNodeAdmin).setupAmm();
16
+ return new AmmBot(aztecNode, wallet, defaultAccountAddress, amm, token0, token1, config);
17
+ }
18
+ async createAndSendTx(logCtx) {
19
+ const { feePaymentMethod } = this.config;
20
+ const { wallet, amm, token0, token1 } = this;
21
+ const balances = this.getBalances();
22
+ this.log.info(`Preparing tx with ${feePaymentMethod} fee to swap tokens. Balances: ${jsonStringify(balances)}`, {
23
+ ...logCtx,
24
+ balances
25
+ });
26
+ // 1000 ± 200
27
+ const amountIn = Math.floor(TRANSFER_BASE_AMOUNT + (Math.random() - 0.5) * TRANSFER_VARIANCE);
28
+ const authwitNonce = Fr.random();
29
+ const [tokenIn, tokenOut] = Math.random() < 0.5 ? [
30
+ token0,
31
+ token1
32
+ ] : [
33
+ token1,
34
+ token0
35
+ ];
36
+ const swapAuthwit = await wallet.createAuthWit(this.defaultAccountAddress, {
37
+ caller: amm.address,
38
+ call: await tokenIn.methods.transfer_to_public(this.defaultAccountAddress, amm.address, amountIn, authwitNonce).getFunctionCall()
39
+ });
40
+ const amountOutMin = await amm.methods.get_amount_out_for_exact_in(await tokenIn.methods.balance_of_public(amm.address).simulate({
41
+ from: this.defaultAccountAddress
42
+ }), await tokenOut.methods.balance_of_public(amm.address).simulate({
43
+ from: this.defaultAccountAddress
44
+ }), amountIn).simulate({
45
+ from: this.defaultAccountAddress
46
+ });
47
+ const swapExactTokensInteraction = amm.methods.swap_exact_tokens_for_tokens(tokenIn.address, tokenOut.address, amountIn, amountOutMin, authwitNonce).with({
48
+ authWitnesses: [
49
+ swapAuthwit
50
+ ]
51
+ });
52
+ const opts = await this.getSendMethodOpts(swapExactTokensInteraction);
53
+ this.log.verbose(`Sending transaction`, logCtx);
54
+ this.log.info(`Tx. Balances: ${jsonStringify(balances)}`, {
55
+ ...logCtx,
56
+ balances
57
+ });
58
+ return swapExactTokensInteraction.send(opts);
59
+ }
60
+ async onTxMined(receipt, logCtx) {
61
+ const balances = await this.getBalances();
62
+ this.log.info(`Balances after swap in tx ${receipt.txHash}: ${jsonStringify(balances)}`, {
63
+ ...logCtx,
64
+ balances
65
+ });
66
+ }
67
+ getAmmBalances() {
68
+ return this.getPublicBalanceFor(this.amm.address);
69
+ }
70
+ async getBalances() {
71
+ return {
72
+ senderPublic: await this.getPublicBalanceFor(this.defaultAccountAddress),
73
+ senderPrivate: await this.getPrivateBalanceFor(this.defaultAccountAddress),
74
+ amm: await this.getPublicBalanceFor(this.amm.address, this.defaultAccountAddress)
75
+ };
76
+ }
77
+ async getPublicBalanceFor(address, from) {
78
+ return {
79
+ token0: await this.token0.methods.balance_of_public(address).simulate({
80
+ from: from ?? address
81
+ }),
82
+ token1: await this.token1.methods.balance_of_public(address).simulate({
83
+ from: from ?? address
84
+ })
85
+ };
86
+ }
87
+ async getPrivateBalanceFor(address, from) {
88
+ return {
89
+ token0: await this.token0.methods.balance_of_private(address).simulate({
90
+ from: from ?? address
91
+ }),
92
+ token1: await this.token1.methods.balance_of_private(address).simulate({
93
+ from: from ?? address
94
+ })
95
+ };
96
+ }
97
+ }
@@ -0,0 +1,21 @@
1
+ import { AztecAddress } from '@aztec/aztec.js/addresses';
2
+ import { BatchCall, ContractFunctionInteraction, type SendInteractionOptions, SentTx } from '@aztec/aztec.js/contracts';
3
+ import { TxHash, TxReceipt } from '@aztec/aztec.js/tx';
4
+ import type { AztecNode } from '@aztec/stdlib/interfaces/client';
5
+ import type { TestWallet } from '@aztec/test-wallet/server';
6
+ import type { BotConfig } from './config.js';
7
+ export declare abstract class BaseBot {
8
+ readonly node: AztecNode;
9
+ readonly wallet: TestWallet;
10
+ readonly defaultAccountAddress: AztecAddress;
11
+ config: BotConfig;
12
+ protected log: import("@aztec/aztec.js/log").Logger;
13
+ protected attempts: number;
14
+ protected successes: number;
15
+ protected constructor(node: AztecNode, wallet: TestWallet, defaultAccountAddress: AztecAddress, config: BotConfig);
16
+ run(): Promise<TxReceipt | TxHash>;
17
+ protected abstract createAndSendTx(logCtx: object): Promise<SentTx>;
18
+ protected onTxMined(_receipt: TxReceipt, _logCtx: object): Promise<void>;
19
+ protected getSendMethodOpts(interaction: ContractFunctionInteraction | BatchCall): Promise<SendInteractionOptions>;
20
+ }
21
+ //# sourceMappingURL=base_bot.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"base_bot.d.ts","sourceRoot":"","sources":["../src/base_bot.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AACzD,OAAO,EACL,SAAS,EACT,2BAA2B,EAC3B,KAAK,sBAAsB,EAC3B,MAAM,EAEP,MAAM,2BAA2B,CAAC;AAEnC,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAEvD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iCAAiC,CAAC;AACjE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAE5D,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C,8BAAsB,OAAO;aAOT,IAAI,EAAE,SAAS;aACf,MAAM,EAAE,UAAU;aAClB,qBAAqB,EAAE,YAAY;IAC5C,MAAM,EAAE,SAAS;IAT1B,SAAS,CAAC,GAAG,uCAAuB;IAEpC,SAAS,CAAC,QAAQ,EAAE,MAAM,CAAK;IAC/B,SAAS,CAAC,SAAS,EAAE,MAAM,CAAK;IAEhC,SAAS,aACS,IAAI,EAAE,SAAS,EACf,MAAM,EAAE,UAAU,EAClB,qBAAqB,EAAE,YAAY,EAC5C,MAAM,EAAE,SAAS;IAGb,GAAG,IAAI,OAAO,CAAC,SAAS,GAAG,MAAM,CAAC;IAoC/C,SAAS,CAAC,QAAQ,CAAC,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAEnE,SAAS,CAAC,SAAS,CAAC,QAAQ,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;cAKxD,iBAAiB,CAC/B,WAAW,EAAE,2BAA2B,GAAG,SAAS,GACnD,OAAO,CAAC,sBAAsB,CAAC;CAqBnC"}
@@ -0,0 +1,80 @@
1
+ import { waitForProven } from '@aztec/aztec.js/contracts';
2
+ import { createLogger } from '@aztec/aztec.js/log';
3
+ import { Gas } from '@aztec/stdlib/gas';
4
+ export class BaseBot {
5
+ node;
6
+ wallet;
7
+ defaultAccountAddress;
8
+ config;
9
+ log;
10
+ attempts;
11
+ successes;
12
+ constructor(node, wallet, defaultAccountAddress, config){
13
+ this.node = node;
14
+ this.wallet = wallet;
15
+ this.defaultAccountAddress = defaultAccountAddress;
16
+ this.config = config;
17
+ this.log = createLogger('bot');
18
+ this.attempts = 0;
19
+ this.successes = 0;
20
+ }
21
+ async run() {
22
+ this.attempts++;
23
+ const logCtx = {
24
+ runId: Date.now() * 1000 + Math.floor(Math.random() * 1000)
25
+ };
26
+ const { followChain, txMinedWaitSeconds } = this.config;
27
+ this.log.verbose(`Creating tx`, logCtx);
28
+ const tx = await this.createAndSendTx(logCtx);
29
+ const txHash = await tx.getTxHash();
30
+ if (followChain === 'NONE') {
31
+ this.log.info(`Transaction ${txHash.toString()} sent, not waiting for it to be mined`);
32
+ return txHash;
33
+ }
34
+ this.log.verbose(`Awaiting tx ${txHash.toString()} to be on the ${followChain} chain (timeout ${txMinedWaitSeconds}s)`, logCtx);
35
+ const receipt = await tx.wait({
36
+ timeout: txMinedWaitSeconds
37
+ });
38
+ if (followChain === 'PROVEN') {
39
+ await waitForProven(this.node, receipt, {
40
+ provenTimeout: txMinedWaitSeconds
41
+ });
42
+ }
43
+ this.successes++;
44
+ this.log.info(`Tx #${this.attempts} ${receipt.txHash} successfully mined in block ${receipt.blockNumber} (stats: ${this.successes}/${this.attempts} success)`, logCtx);
45
+ await this.onTxMined(receipt, logCtx);
46
+ return receipt;
47
+ }
48
+ onTxMined(_receipt, _logCtx) {
49
+ // no-op
50
+ return Promise.resolve();
51
+ }
52
+ async getSendMethodOpts(interaction) {
53
+ const { l2GasLimit, daGasLimit, baseFeePadding } = this.config;
54
+ this.wallet.setBaseFeePadding(baseFeePadding);
55
+ let gasSettings;
56
+ if (l2GasLimit !== undefined && l2GasLimit > 0 && daGasLimit !== undefined && daGasLimit > 0) {
57
+ gasSettings = {
58
+ gasLimits: Gas.from({
59
+ l2Gas: l2GasLimit,
60
+ daGas: daGasLimit
61
+ })
62
+ };
63
+ this.log.verbose(`Using gas limits ${l2GasLimit} L2 gas ${daGasLimit} DA gas`);
64
+ } else {
65
+ this.log.verbose(`Estimating gas for transaction`);
66
+ ({ estimatedGas: gasSettings } = await interaction.simulate({
67
+ fee: {
68
+ estimateGas: true
69
+ },
70
+ from: this.defaultAccountAddress
71
+ }));
72
+ }
73
+ return {
74
+ from: this.defaultAccountAddress,
75
+ fee: {
76
+ gasSettings
77
+ }
78
+ };
79
+ }
80
+ }
package/dest/bot.d.ts CHANGED
@@ -1,23 +1,19 @@
1
- import { type AztecAddress, type Wallet } from '@aztec/aztec.js';
2
- import type { EasyPrivateTokenContract } from '@aztec/noir-contracts.js/EasyPrivateToken';
1
+ import type { AztecAddress } from '@aztec/aztec.js/addresses';
2
+ import { SentTx } from '@aztec/aztec.js/contracts';
3
+ import type { PrivateTokenContract } from '@aztec/noir-contracts.js/PrivateToken';
3
4
  import type { TokenContract } from '@aztec/noir-contracts.js/Token';
4
- import type { AztecNode, PXE } from '@aztec/stdlib/interfaces/client';
5
+ import type { AztecNode, AztecNodeAdmin } from '@aztec/stdlib/interfaces/client';
6
+ import type { TestWallet } from '@aztec/test-wallet/server';
7
+ import { BaseBot } from './base_bot.js';
5
8
  import type { BotConfig } from './config.js';
6
- export declare class Bot {
7
- readonly wallet: Wallet;
8
- readonly token: TokenContract | EasyPrivateTokenContract;
9
+ import type { BotStore } from './store/index.js';
10
+ export declare class Bot extends BaseBot {
11
+ readonly token: TokenContract | PrivateTokenContract;
9
12
  readonly recipient: AztecAddress;
10
- config: BotConfig;
11
- private log;
12
- private attempts;
13
- private successes;
14
- protected constructor(wallet: Wallet, token: TokenContract | EasyPrivateTokenContract, recipient: AztecAddress, config: BotConfig);
15
- static create(config: BotConfig, dependencies?: {
16
- pxe?: PXE;
17
- node?: AztecNode;
18
- }): Promise<Bot>;
13
+ protected constructor(node: AztecNode, wallet: TestWallet, defaultAccountAddress: AztecAddress, token: TokenContract | PrivateTokenContract, recipient: AztecAddress, config: BotConfig);
14
+ static create(config: BotConfig, wallet: TestWallet, aztecNode: AztecNode, aztecNodeAdmin: AztecNodeAdmin | undefined, store: BotStore): Promise<Bot>;
19
15
  updateConfig(config: Partial<BotConfig>): void;
20
- run(): Promise<void>;
16
+ protected createAndSendTx(logCtx: object): Promise<SentTx>;
21
17
  getBalances(): Promise<{
22
18
  sender: {
23
19
  privateBalance: bigint;
@@ -28,6 +24,5 @@ export declare class Bot {
28
24
  publicBalance: bigint;
29
25
  };
30
26
  }>;
31
- private getSendMethodOpts;
32
27
  }
33
28
  //# sourceMappingURL=bot.d.ts.map
package/dest/bot.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"bot.d.ts","sourceRoot":"","sources":["../src/bot.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,YAAY,EAIjB,KAAK,MAAM,EAEZ,MAAM,iBAAiB,CAAC;AAEzB,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,2CAA2C,CAAC;AAC1F,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gCAAgC,CAAC;AAGpE,OAAO,KAAK,EAAE,SAAS,EAAE,GAAG,EAAE,MAAM,iCAAiC,CAAC;AAEtE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAM7C,qBAAa,GAAG;aAOI,MAAM,EAAE,MAAM;aACd,KAAK,EAAE,aAAa,GAAG,wBAAwB;aAC/C,SAAS,EAAE,YAAY;IAChC,MAAM,EAAE,SAAS;IAT1B,OAAO,CAAC,GAAG,CAAuB;IAElC,OAAO,CAAC,QAAQ,CAAa;IAC7B,OAAO,CAAC,SAAS,CAAa;IAE9B,SAAS,aACS,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,aAAa,GAAG,wBAAwB,EAC/C,SAAS,EAAE,YAAY,EAChC,MAAM,EAAE,SAAS;WAGb,MAAM,CAAC,MAAM,EAAE,SAAS,EAAE,YAAY,GAAE;QAAE,GAAG,CAAC,EAAE,GAAG,CAAC;QAAC,IAAI,CAAC,EAAE,SAAS,CAAA;KAAO,GAAG,OAAO,CAAC,GAAG,CAAC;IAKjG,YAAY,CAAC,MAAM,EAAE,OAAO,CAAC,SAAS,CAAC;IAKjC,GAAG;IAoEH,WAAW;;;;;;;;;;IAoBxB,OAAO,CAAC,iBAAiB;CAkB1B"}
1
+ {"version":3,"file":"bot.d.ts","sourceRoot":"","sources":["../src/bot.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,EAAa,MAAM,EAAE,MAAM,2BAA2B,CAAC;AAE9D,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,uCAAuC,CAAC;AAClF,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gCAAgC,CAAC;AACpE,OAAO,KAAK,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,iCAAiC,CAAC;AACjF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAE5D,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAE7C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAKjD,qBAAa,GAAI,SAAQ,OAAO;aAKZ,KAAK,EAAE,aAAa,GAAG,oBAAoB;aAC3C,SAAS,EAAE,YAAY;IALzC,SAAS,aACP,IAAI,EAAE,SAAS,EACf,MAAM,EAAE,UAAU,EAClB,qBAAqB,EAAE,YAAY,EACnB,KAAK,EAAE,aAAa,GAAG,oBAAoB,EAC3C,SAAS,EAAE,YAAY,EACvC,MAAM,EAAE,SAAS;WAKN,MAAM,CACjB,MAAM,EAAE,SAAS,EACjB,MAAM,EAAE,UAAU,EAClB,SAAS,EAAE,SAAS,EACpB,cAAc,EAAE,cAAc,GAAG,SAAS,EAC1C,KAAK,EAAE,QAAQ,GACd,OAAO,CAAC,GAAG,CAAC;IAWR,YAAY,CAAC,MAAM,EAAE,OAAO,CAAC,SAAS,CAAC;cAK9B,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IA8BnD,WAAW;;;;;;;;;;CAmBzB"}
package/dest/bot.js CHANGED
@@ -1,29 +1,18 @@
1
- import { BatchCall, FeeJuicePaymentMethod, createLogger } from '@aztec/aztec.js';
2
- import { timesParallel } from '@aztec/foundation/collection';
3
- import { Gas } from '@aztec/stdlib/gas';
1
+ import { BatchCall } from '@aztec/aztec.js/contracts';
2
+ import { times } from '@aztec/foundation/collection';
3
+ import { BaseBot } from './base_bot.js';
4
4
  import { BotFactory } from './factory.js';
5
5
  import { getBalances, getPrivateBalance, isStandardTokenContract } from './utils.js';
6
6
  const TRANSFER_AMOUNT = 1;
7
- export class Bot {
8
- wallet;
7
+ export class Bot extends BaseBot {
9
8
  token;
10
9
  recipient;
11
- config;
12
- log;
13
- attempts;
14
- successes;
15
- constructor(wallet, token, recipient, config){
16
- this.wallet = wallet;
17
- this.token = token;
18
- this.recipient = recipient;
19
- this.config = config;
20
- this.log = createLogger('bot');
21
- this.attempts = 0;
22
- this.successes = 0;
10
+ constructor(node, wallet, defaultAccountAddress, token, recipient, config){
11
+ super(node, wallet, defaultAccountAddress, config), this.token = token, this.recipient = recipient;
23
12
  }
24
- static async create(config, dependencies = {}) {
25
- const { wallet, token, recipient } = await new BotFactory(config, dependencies).setup();
26
- return new Bot(wallet, token, recipient, config);
13
+ static async create(config, wallet, aztecNode, aztecNodeAdmin, store) {
14
+ const { defaultAccountAddress, token, recipient } = await new BotFactory(config, wallet, store, aztecNode, aztecNodeAdmin).setup();
15
+ return new Bot(aztecNode, wallet, defaultAccountAddress, token, recipient, config);
27
16
  }
28
17
  updateConfig(config) {
29
18
  this.log.info(`Updating bot config ${Object.keys(config).join(', ')}`);
@@ -32,91 +21,40 @@ export class Bot {
32
21
  ...config
33
22
  };
34
23
  }
35
- async run() {
36
- this.attempts++;
37
- const logCtx = {
38
- runId: Date.now() * 1000 + Math.floor(Math.random() * 1000)
39
- };
40
- const { privateTransfersPerTx, publicTransfersPerTx, feePaymentMethod, followChain, txMinedWaitSeconds } = this.config;
24
+ async createAndSendTx(logCtx) {
25
+ const { privateTransfersPerTx, publicTransfersPerTx, feePaymentMethod } = this.config;
41
26
  const { token, recipient, wallet } = this;
42
- const sender = wallet.getAddress();
43
27
  this.log.verbose(`Preparing tx with ${feePaymentMethod} fee with ${privateTransfersPerTx} private and ${publicTransfersPerTx} public transfers`, logCtx);
44
- const calls = [];
45
- if (isStandardTokenContract(token)) {
46
- calls.push(...await timesParallel(privateTransfersPerTx, ()=>token.methods.transfer(recipient, TRANSFER_AMOUNT).request()));
47
- calls.push(...await timesParallel(publicTransfersPerTx, ()=>token.methods.transfer_in_public(sender, recipient, TRANSFER_AMOUNT, 0).request()));
48
- } else {
49
- calls.push(...await timesParallel(privateTransfersPerTx, ()=>token.methods.transfer(TRANSFER_AMOUNT, sender, recipient).request()));
50
- }
51
- const opts = this.getSendMethodOpts();
28
+ const calls = isStandardTokenContract(token) ? [
29
+ times(privateTransfersPerTx, ()=>token.methods.transfer(recipient, TRANSFER_AMOUNT)),
30
+ times(publicTransfersPerTx, ()=>token.methods.transfer_in_public(this.defaultAccountAddress, recipient, TRANSFER_AMOUNT, 0))
31
+ ].flat() : times(privateTransfersPerTx, ()=>token.methods.transfer(TRANSFER_AMOUNT, this.defaultAccountAddress, recipient));
52
32
  const batch = new BatchCall(wallet, calls);
33
+ const opts = await this.getSendMethodOpts(batch);
53
34
  this.log.verbose(`Simulating transaction with ${calls.length}`, logCtx);
54
- await batch.simulate();
55
- this.log.verbose(`Proving transaction`, logCtx);
56
- const provenTx = await batch.prove(opts);
57
- this.log.verbose(`Sending tx`, logCtx);
58
- const tx = provenTx.send();
59
- const txHash = await tx.getTxHash();
60
- if (followChain === 'NONE') {
61
- this.log.info(`Transaction ${txHash} sent, not waiting for it to be mined`);
62
- return;
63
- }
64
- this.log.verbose(`Awaiting tx ${txHash} to be on the ${followChain} chain (timeout ${txMinedWaitSeconds}s)`, logCtx);
65
- const receipt = await tx.wait({
66
- timeout: txMinedWaitSeconds,
67
- provenTimeout: txMinedWaitSeconds,
68
- proven: followChain === 'PROVEN'
35
+ await batch.simulate({
36
+ from: this.defaultAccountAddress
69
37
  });
70
- this.log.info(`Tx #${this.attempts} ${receipt.txHash} successfully mined in block ${receipt.blockNumber} (stats: ${this.successes}/${this.attempts} success)`, logCtx);
71
- this.successes++;
38
+ this.log.verbose(`Sending transaction`, logCtx);
39
+ return batch.send(opts);
72
40
  }
73
41
  async getBalances() {
74
42
  if (isStandardTokenContract(this.token)) {
75
43
  return {
76
- sender: await getBalances(this.token, this.wallet.getAddress()),
77
- recipient: await getBalances(this.token, this.recipient)
44
+ sender: await getBalances(this.token, this.defaultAccountAddress),
45
+ recipient: await getBalances(this.token, this.recipient, this.defaultAccountAddress)
78
46
  };
79
47
  } else {
80
48
  return {
81
49
  sender: {
82
- privateBalance: await getPrivateBalance(this.token, this.wallet.getAddress()),
50
+ privateBalance: await getPrivateBalance(this.token, this.defaultAccountAddress),
83
51
  publicBalance: 0n
84
52
  },
85
53
  recipient: {
86
- privateBalance: await getPrivateBalance(this.token, this.recipient),
54
+ privateBalance: await getPrivateBalance(this.token, this.recipient, this.defaultAccountAddress),
87
55
  publicBalance: 0n
88
56
  }
89
57
  };
90
58
  }
91
59
  }
92
- getSendMethodOpts() {
93
- const sender = this.wallet.getAddress();
94
- const { l2GasLimit, daGasLimit, skipPublicSimulation } = this.config;
95
- const paymentMethod = new FeeJuicePaymentMethod(sender);
96
- let gasSettings, estimateGas;
97
- if (l2GasLimit !== undefined && l2GasLimit > 0 && daGasLimit !== undefined && daGasLimit > 0) {
98
- gasSettings = {
99
- gasLimits: Gas.from({
100
- l2Gas: l2GasLimit,
101
- daGas: daGasLimit
102
- })
103
- };
104
- estimateGas = false;
105
- this.log.verbose(`Using gas limits ${l2GasLimit} L2 gas ${daGasLimit} DA gas`);
106
- } else {
107
- estimateGas = true;
108
- this.log.verbose(`Estimating gas for transaction`);
109
- }
110
- const baseFeePadding = 2; // Send 3x the current base fee
111
- this.log.verbose(skipPublicSimulation ? `Skipping public simulation` : `Simulating public transfers`);
112
- return {
113
- fee: {
114
- estimateGas,
115
- paymentMethod,
116
- gasSettings,
117
- baseFeePadding
118
- },
119
- skipPublicSimulation
120
- };
121
- }
122
60
  }