@aztec/ethereum 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 (229) hide show
  1. package/dest/account.d.ts +2 -0
  2. package/dest/account.d.ts.map +1 -0
  3. package/dest/account.js +4 -0
  4. package/dest/client.d.ts +5 -3
  5. package/dest/client.d.ts.map +1 -1
  6. package/dest/client.js +16 -2
  7. package/dest/config.d.ts +107 -16
  8. package/dest/config.d.ts.map +1 -1
  9. package/dest/config.js +456 -22
  10. package/dest/contracts/empire_base.d.ts +21 -6
  11. package/dest/contracts/empire_base.d.ts.map +1 -1
  12. package/dest/contracts/empire_base.js +75 -2
  13. package/dest/contracts/empire_slashing_proposer.d.ts +65 -0
  14. package/dest/contracts/empire_slashing_proposer.d.ts.map +1 -0
  15. package/dest/contracts/empire_slashing_proposer.js +194 -0
  16. package/dest/contracts/errors.d.ts +7 -0
  17. package/dest/contracts/errors.d.ts.map +1 -0
  18. package/dest/contracts/errors.js +12 -0
  19. package/dest/contracts/fee_asset_handler.d.ts +19 -0
  20. package/dest/contracts/fee_asset_handler.d.ts.map +1 -0
  21. package/dest/contracts/fee_asset_handler.js +57 -0
  22. package/dest/contracts/fee_juice.d.ts +5 -6
  23. package/dest/contracts/fee_juice.d.ts.map +1 -1
  24. package/dest/contracts/fee_juice.js +27 -20
  25. package/dest/contracts/governance.d.ts +36 -25
  26. package/dest/contracts/governance.d.ts.map +1 -1
  27. package/dest/contracts/governance.js +87 -84
  28. package/dest/contracts/governance_proposer.d.ts +13 -11
  29. package/dest/contracts/governance_proposer.d.ts.map +1 -1
  30. package/dest/contracts/governance_proposer.js +32 -18
  31. package/dest/contracts/gse.d.ts +32 -0
  32. package/dest/contracts/gse.d.ts.map +1 -0
  33. package/dest/contracts/gse.js +72 -0
  34. package/dest/contracts/inbox.d.ts +26 -0
  35. package/dest/contracts/inbox.d.ts.map +1 -0
  36. package/dest/contracts/inbox.js +45 -0
  37. package/dest/contracts/index.d.ts +8 -2
  38. package/dest/contracts/index.d.ts.map +1 -1
  39. package/dest/contracts/index.js +8 -2
  40. package/dest/contracts/multicall.d.ts +21 -0
  41. package/dest/contracts/multicall.d.ts.map +1 -0
  42. package/dest/contracts/multicall.js +156 -0
  43. package/dest/contracts/registry.d.ts +9 -4
  44. package/dest/contracts/registry.d.ts.map +1 -1
  45. package/dest/contracts/registry.js +44 -16
  46. package/dest/contracts/rollup.d.ts +202 -29
  47. package/dest/contracts/rollup.d.ts.map +1 -1
  48. package/dest/contracts/rollup.js +500 -55
  49. package/dest/contracts/slasher_contract.d.ts +44 -0
  50. package/dest/contracts/slasher_contract.d.ts.map +1 -0
  51. package/dest/contracts/slasher_contract.js +75 -0
  52. package/dest/contracts/tally_slashing_proposer.d.ts +138 -0
  53. package/dest/contracts/tally_slashing_proposer.d.ts.map +1 -0
  54. package/dest/contracts/tally_slashing_proposer.js +313 -0
  55. package/dest/contracts/utils.d.ts +3 -0
  56. package/dest/contracts/utils.d.ts.map +1 -0
  57. package/dest/contracts/utils.js +11 -0
  58. package/dest/deploy_l1_contracts.d.ts +128 -21112
  59. package/dest/deploy_l1_contracts.d.ts.map +1 -1
  60. package/dest/deploy_l1_contracts.js +1204 -418
  61. package/dest/eth-signer/eth-signer.d.ts +21 -0
  62. package/dest/eth-signer/eth-signer.d.ts.map +1 -0
  63. package/dest/eth-signer/eth-signer.js +5 -0
  64. package/dest/eth-signer/index.d.ts +2 -0
  65. package/dest/eth-signer/index.d.ts.map +1 -0
  66. package/dest/eth-signer/index.js +1 -0
  67. package/dest/index.d.ts +6 -2
  68. package/dest/index.d.ts.map +1 -1
  69. package/dest/index.js +6 -2
  70. package/dest/l1_artifacts.d.ts +76184 -0
  71. package/dest/l1_artifacts.d.ts.map +1 -0
  72. package/dest/l1_artifacts.js +166 -0
  73. package/dest/l1_contract_addresses.d.ts +21 -1
  74. package/dest/l1_contract_addresses.d.ts.map +1 -1
  75. package/dest/l1_contract_addresses.js +22 -18
  76. package/dest/l1_reader.d.ts +1 -1
  77. package/dest/l1_reader.d.ts.map +1 -1
  78. package/dest/l1_reader.js +8 -8
  79. package/dest/l1_tx_utils/config.d.ts +59 -0
  80. package/dest/l1_tx_utils/config.d.ts.map +1 -0
  81. package/dest/l1_tx_utils/config.js +73 -0
  82. package/dest/l1_tx_utils/constants.d.ts +6 -0
  83. package/dest/l1_tx_utils/constants.d.ts.map +1 -0
  84. package/dest/l1_tx_utils/constants.js +14 -0
  85. package/dest/l1_tx_utils/factory.d.ts +24 -0
  86. package/dest/l1_tx_utils/factory.d.ts.map +1 -0
  87. package/dest/l1_tx_utils/factory.js +12 -0
  88. package/dest/l1_tx_utils/index.d.ts +10 -0
  89. package/dest/l1_tx_utils/index.d.ts.map +1 -0
  90. package/dest/l1_tx_utils/index.js +10 -0
  91. package/dest/l1_tx_utils/interfaces.d.ts +76 -0
  92. package/dest/l1_tx_utils/interfaces.d.ts.map +1 -0
  93. package/dest/l1_tx_utils/interfaces.js +4 -0
  94. package/dest/l1_tx_utils/l1_tx_utils.d.ts +95 -0
  95. package/dest/l1_tx_utils/l1_tx_utils.d.ts.map +1 -0
  96. package/dest/l1_tx_utils/l1_tx_utils.js +610 -0
  97. package/dest/l1_tx_utils/l1_tx_utils_with_blobs.d.ts +26 -0
  98. package/dest/l1_tx_utils/l1_tx_utils_with_blobs.d.ts.map +1 -0
  99. package/dest/l1_tx_utils/l1_tx_utils_with_blobs.js +26 -0
  100. package/dest/l1_tx_utils/readonly_l1_tx_utils.d.ts +94 -0
  101. package/dest/l1_tx_utils/readonly_l1_tx_utils.d.ts.map +1 -0
  102. package/dest/l1_tx_utils/readonly_l1_tx_utils.js +431 -0
  103. package/dest/l1_tx_utils/signer.d.ts +4 -0
  104. package/dest/l1_tx_utils/signer.d.ts.map +1 -0
  105. package/dest/l1_tx_utils/signer.js +16 -0
  106. package/dest/l1_tx_utils/types.d.ts +67 -0
  107. package/dest/l1_tx_utils/types.d.ts.map +1 -0
  108. package/dest/l1_tx_utils/types.js +26 -0
  109. package/dest/l1_tx_utils/utils.d.ts +4 -0
  110. package/dest/l1_tx_utils/utils.d.ts.map +1 -0
  111. package/dest/l1_tx_utils/utils.js +14 -0
  112. package/dest/l1_types.d.ts +6 -0
  113. package/dest/l1_types.d.ts.map +1 -0
  114. package/dest/l1_types.js +1 -0
  115. package/dest/publisher_manager.d.ts +15 -0
  116. package/dest/publisher_manager.d.ts.map +1 -0
  117. package/dest/publisher_manager.js +88 -0
  118. package/dest/queries.d.ts +3 -1
  119. package/dest/queries.d.ts.map +1 -1
  120. package/dest/queries.js +51 -12
  121. package/dest/test/chain_monitor.d.ts +72 -0
  122. package/dest/test/chain_monitor.d.ts.map +1 -0
  123. package/dest/test/chain_monitor.js +216 -0
  124. package/dest/test/delayed_tx_utils.d.ts +7 -2
  125. package/dest/test/delayed_tx_utils.d.ts.map +1 -1
  126. package/dest/test/delayed_tx_utils.js +13 -6
  127. package/dest/{eth_cheat_codes.d.ts → test/eth_cheat_codes.d.ts} +87 -13
  128. package/dest/test/eth_cheat_codes.d.ts.map +1 -0
  129. package/dest/test/eth_cheat_codes.js +552 -0
  130. package/dest/test/eth_cheat_codes_with_state.d.ts +1 -1
  131. package/dest/test/eth_cheat_codes_with_state.d.ts.map +1 -1
  132. package/dest/test/eth_cheat_codes_with_state.js +1 -1
  133. package/dest/test/index.d.ts +3 -0
  134. package/dest/test/index.d.ts.map +1 -1
  135. package/dest/test/index.js +3 -0
  136. package/dest/test/rollup_cheat_codes.d.ts +86 -0
  137. package/dest/test/rollup_cheat_codes.d.ts.map +1 -0
  138. package/dest/test/rollup_cheat_codes.js +268 -0
  139. package/dest/test/start_anvil.d.ts +5 -0
  140. package/dest/test/start_anvil.d.ts.map +1 -1
  141. package/dest/test/start_anvil.js +15 -7
  142. package/dest/test/tx_delayer.d.ts +17 -6
  143. package/dest/test/tx_delayer.d.ts.map +1 -1
  144. package/dest/test/tx_delayer.js +95 -19
  145. package/dest/test/upgrade_utils.d.ts +5 -4
  146. package/dest/test/upgrade_utils.d.ts.map +1 -1
  147. package/dest/test/upgrade_utils.js +23 -16
  148. package/dest/types.d.ts +6 -7
  149. package/dest/types.d.ts.map +1 -1
  150. package/dest/types.js +3 -1
  151. package/dest/utils.d.ts +1 -0
  152. package/dest/utils.d.ts.map +1 -1
  153. package/dest/utils.js +43 -88
  154. package/dest/zkPassportVerifierAddress.d.ts +15 -0
  155. package/dest/zkPassportVerifierAddress.d.ts.map +1 -0
  156. package/dest/zkPassportVerifierAddress.js +11 -0
  157. package/package.json +24 -16
  158. package/src/account.ts +5 -0
  159. package/src/client.ts +42 -4
  160. package/src/config.ts +584 -31
  161. package/src/contracts/empire_base.ts +75 -6
  162. package/src/contracts/empire_slashing_proposer.ts +259 -0
  163. package/src/contracts/errors.ts +13 -0
  164. package/src/contracts/fee_asset_handler.ts +63 -0
  165. package/src/contracts/fee_juice.ts +29 -15
  166. package/src/contracts/governance.ts +80 -77
  167. package/src/contracts/governance_proposer.ts +60 -24
  168. package/src/contracts/gse.ts +88 -0
  169. package/src/contracts/inbox.ts +63 -0
  170. package/src/contracts/index.ts +8 -2
  171. package/src/contracts/multicall.ts +155 -0
  172. package/src/contracts/registry.ts +51 -26
  173. package/src/contracts/rollup.ts +585 -56
  174. package/src/contracts/slasher_contract.ts +89 -0
  175. package/src/contracts/tally_slashing_proposer.ts +315 -0
  176. package/src/contracts/utils.ts +14 -0
  177. package/src/deploy_l1_contracts.ts +1467 -566
  178. package/src/eth-signer/eth-signer.ts +25 -0
  179. package/src/eth-signer/index.ts +1 -0
  180. package/src/index.ts +6 -2
  181. package/src/l1_artifacts.ts +254 -0
  182. package/src/l1_contract_addresses.ts +32 -19
  183. package/src/l1_reader.ts +9 -9
  184. package/src/l1_tx_utils/README.md +177 -0
  185. package/src/l1_tx_utils/config.ts +140 -0
  186. package/src/l1_tx_utils/constants.ts +18 -0
  187. package/src/l1_tx_utils/factory.ts +64 -0
  188. package/src/l1_tx_utils/index.ts +12 -0
  189. package/src/l1_tx_utils/interfaces.ts +86 -0
  190. package/src/l1_tx_utils/l1_tx_utils.ts +718 -0
  191. package/src/l1_tx_utils/l1_tx_utils_with_blobs.ts +77 -0
  192. package/src/l1_tx_utils/readonly_l1_tx_utils.ts +559 -0
  193. package/src/l1_tx_utils/signer.ts +28 -0
  194. package/src/l1_tx_utils/types.ts +85 -0
  195. package/src/l1_tx_utils/utils.ts +16 -0
  196. package/src/l1_types.ts +6 -0
  197. package/src/publisher_manager.ts +106 -0
  198. package/src/queries.ts +70 -15
  199. package/src/test/chain_monitor.ts +243 -0
  200. package/src/test/delayed_tx_utils.ts +34 -6
  201. package/src/test/eth_cheat_codes.ts +582 -0
  202. package/src/test/eth_cheat_codes_with_state.ts +1 -1
  203. package/src/test/index.ts +3 -0
  204. package/src/test/rollup_cheat_codes.ts +310 -0
  205. package/src/test/start_anvil.ts +20 -5
  206. package/src/test/tx_delayer.ts +127 -26
  207. package/src/test/upgrade_utils.ts +30 -21
  208. package/src/types.ts +10 -8
  209. package/src/utils.ts +49 -90
  210. package/src/zkPassportVerifierAddress.ts +15 -0
  211. package/dest/contracts/forwarder.d.ts +0 -24
  212. package/dest/contracts/forwarder.d.ts.map +0 -1
  213. package/dest/contracts/forwarder.js +0 -101
  214. package/dest/contracts/slashing_proposer.d.ts +0 -21
  215. package/dest/contracts/slashing_proposer.d.ts.map +0 -1
  216. package/dest/contracts/slashing_proposer.js +0 -47
  217. package/dest/eth_cheat_codes.d.ts.map +0 -1
  218. package/dest/eth_cheat_codes.js +0 -303
  219. package/dest/l1_tx_utils.d.ts +0 -192
  220. package/dest/l1_tx_utils.d.ts.map +0 -1
  221. package/dest/l1_tx_utils.js +0 -641
  222. package/dest/l1_tx_utils_with_blobs.d.ts +0 -12
  223. package/dest/l1_tx_utils_with_blobs.d.ts.map +0 -1
  224. package/dest/l1_tx_utils_with_blobs.js +0 -64
  225. package/src/contracts/forwarder.ts +0 -132
  226. package/src/contracts/slashing_proposer.ts +0 -51
  227. package/src/eth_cheat_codes.ts +0 -314
  228. package/src/l1_tx_utils.ts +0 -847
  229. package/src/l1_tx_utils_with_blobs.ts +0 -86
@@ -0,0 +1,89 @@
1
+ import { EthAddress } from '@aztec/foundation/eth-address';
2
+ import { createLogger } from '@aztec/foundation/log';
3
+ import { SlasherAbi } from '@aztec/l1-artifacts/SlasherAbi';
4
+
5
+ import { type GetContractReturnType, getContract } from 'viem';
6
+
7
+ import type { ViemClient } from '../types.js';
8
+
9
+ /**
10
+ * Typescript wrapper around the Slasher contract.
11
+ */
12
+ export class SlasherContract {
13
+ private contract: GetContractReturnType<typeof SlasherAbi, ViemClient>;
14
+
15
+ constructor(
16
+ private readonly client: ViemClient,
17
+ private readonly address: EthAddress,
18
+ private readonly log = createLogger('slasher-contract'),
19
+ ) {
20
+ this.contract = getContract({
21
+ address: this.address.toString(),
22
+ abi: SlasherAbi,
23
+ client: this.client,
24
+ });
25
+ }
26
+
27
+ /**
28
+ * Checks if a slash payload is vetoed.
29
+ * @param payloadAddress - The address of the payload to check
30
+ * @returns True if the payload is vetoed, false otherwise
31
+ */
32
+ public async isPayloadVetoed(payloadAddress: EthAddress): Promise<boolean> {
33
+ try {
34
+ return await this.contract.read.vetoedPayloads([payloadAddress.toString()]);
35
+ } catch (error) {
36
+ this.log.error(`Error checking if payload ${payloadAddress} is vetoed`, error);
37
+ throw error;
38
+ }
39
+ }
40
+
41
+ /**
42
+ * Checks if slashing is currently enabled. Slashing can be disabled by the vetoer.
43
+ * @returns True if slashing is enabled, false otherwise
44
+ */
45
+ public async isSlashingEnabled(): Promise<boolean> {
46
+ try {
47
+ return await this.contract.read.isSlashingEnabled();
48
+ } catch (error) {
49
+ this.log.error(`Error checking if slashing is enabled`, error);
50
+ throw error;
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Gets the current vetoer address.
56
+ * @returns The vetoer address
57
+ */
58
+ public async getVetoer(): Promise<EthAddress> {
59
+ const vetoer = await this.contract.read.VETOER();
60
+ return EthAddress.fromString(vetoer);
61
+ }
62
+
63
+ /**
64
+ * Gets the disable duration by the vetoer.
65
+ * @returns The disable duration in seconds
66
+ */
67
+ public async getSlashingDisableDuration(): Promise<number> {
68
+ const duration = await this.contract.read.SLASHING_DISABLE_DURATION();
69
+ return Number(duration);
70
+ }
71
+
72
+ /**
73
+ * Gets the current governance address.
74
+ * @returns The governance address
75
+ */
76
+ public async getGovernance(): Promise<EthAddress> {
77
+ const governance = await this.contract.read.GOVERNANCE();
78
+ return EthAddress.fromString(governance);
79
+ }
80
+
81
+ /**
82
+ * Gets the current proposer address.
83
+ * @returns The proposer address
84
+ */
85
+ public async getProposer(): Promise<EthAddress> {
86
+ const proposer = await this.contract.read.PROPOSER();
87
+ return EthAddress.fromString(proposer);
88
+ }
89
+ }
@@ -0,0 +1,315 @@
1
+ import { type L1TxRequest, type ViemClient, tryExtractEvent } from '@aztec/ethereum';
2
+ import { Buffer32 } from '@aztec/foundation/buffer';
3
+ import { EthAddress } from '@aztec/foundation/eth-address';
4
+ import { Signature } from '@aztec/foundation/eth-signature';
5
+ import { hexToBuffer } from '@aztec/foundation/string';
6
+ import { TallySlashingProposerAbi } from '@aztec/l1-artifacts/TallySlashingProposerAbi';
7
+
8
+ import {
9
+ type GetContractReturnType,
10
+ type Hex,
11
+ type Log,
12
+ type TypedDataDefinition,
13
+ encodeFunctionData,
14
+ getContract,
15
+ } from 'viem';
16
+
17
+ /**
18
+ * Wrapper around the TallySlashingProposer contract that provides
19
+ * a TypeScript interface for interacting with the consensus-based slashing system.
20
+ */
21
+ export class TallySlashingProposerContract {
22
+ private readonly contract: GetContractReturnType<typeof TallySlashingProposerAbi, ViemClient>;
23
+
24
+ public readonly type = 'tally' as const;
25
+
26
+ constructor(
27
+ public readonly client: ViemClient,
28
+ address: Hex | EthAddress,
29
+ ) {
30
+ this.contract = getContract({
31
+ address: typeof address === 'string' ? address : address.toString(),
32
+ abi: TallySlashingProposerAbi,
33
+ client,
34
+ });
35
+ }
36
+
37
+ public get address() {
38
+ return EthAddress.fromString(this.contract.address);
39
+ }
40
+
41
+ public getQuorumSize(): Promise<bigint> {
42
+ return this.contract.read.QUORUM();
43
+ }
44
+
45
+ public getRoundSize(): Promise<bigint> {
46
+ return this.contract.read.ROUND_SIZE();
47
+ }
48
+
49
+ public getCommitteeSize(): Promise<bigint> {
50
+ return this.contract.read.COMMITTEE_SIZE();
51
+ }
52
+
53
+ public getRoundSizeInEpochs(): Promise<bigint> {
54
+ return this.contract.read.ROUND_SIZE_IN_EPOCHS();
55
+ }
56
+
57
+ public getLifetimeInRounds(): Promise<bigint> {
58
+ return this.contract.read.LIFETIME_IN_ROUNDS();
59
+ }
60
+
61
+ public getExecutionDelayInRounds(): Promise<bigint> {
62
+ return this.contract.read.EXECUTION_DELAY_IN_ROUNDS();
63
+ }
64
+
65
+ public getSlashingAmounts(): Promise<[bigint, bigint, bigint]> {
66
+ return Promise.all([
67
+ this.contract.read.SLASH_AMOUNT_SMALL(),
68
+ this.contract.read.SLASH_AMOUNT_MEDIUM(),
69
+ this.contract.read.SLASH_AMOUNT_LARGE(),
70
+ ]);
71
+ }
72
+
73
+ public getSlashOffsetInRounds(): Promise<bigint> {
74
+ return this.contract.read.SLASH_OFFSET_IN_ROUNDS();
75
+ }
76
+
77
+ public getCurrentRound(): Promise<bigint> {
78
+ return this.contract.read.getCurrentRound();
79
+ }
80
+
81
+ /**
82
+ * Get round information
83
+ * @param round - The round number to query
84
+ * @returns Round status information
85
+ */
86
+ public async getRound(round: bigint): Promise<{
87
+ isExecuted: boolean;
88
+ voteCount: bigint;
89
+ }> {
90
+ const [isExecuted, voteCount] = await this.contract.read.getRound([round]);
91
+ return { isExecuted, voteCount };
92
+ }
93
+
94
+ /**
95
+ * Check if a round is ready to execute at a given slot
96
+ * @param round - The round number to check
97
+ * @param slot - The slot number to check at
98
+ * @returns Whether the round is ready to execute
99
+ */
100
+ public async isRoundReadyToExecute(round: bigint, slot: bigint): Promise<boolean> {
101
+ return await this.contract.read.isRoundReadyToExecute([round, slot]);
102
+ }
103
+
104
+ /** Returns the slash actions and payload address for a given round (zero if no slash actions) */
105
+ public async getPayload(
106
+ round: bigint,
107
+ ): Promise<{ actions: { slashAmount: bigint; validator: EthAddress }[]; address: EthAddress }> {
108
+ const { result: committees } = await this.contract.simulate.getSlashTargetCommittees([round]);
109
+ const tally = await this.contract.read.getTally([round, committees]);
110
+ const address = await this.contract.read.getPayloadAddress([round, tally]);
111
+ const actions = this.mapSlashActions(tally);
112
+ return { actions, address: EthAddress.fromString(address) };
113
+ }
114
+
115
+ /** Returns the slash actions to be executed for a given round based on votes */
116
+ public async getTally(
117
+ round: bigint,
118
+ ): Promise<{ actions: { slashAmount: bigint; validator: EthAddress }[]; committees: EthAddress[][] }> {
119
+ const { result: committees } = await this.contract.simulate.getSlashTargetCommittees([round]);
120
+ const tally = await this.contract.read.getTally([round, committees]);
121
+ return { actions: this.mapSlashActions(tally), committees: committees.map(c => c.map(EthAddress.fromString)) };
122
+ }
123
+
124
+ private mapSlashActions(
125
+ actions: readonly { slashAmount: bigint; validator: Hex }[],
126
+ ): { slashAmount: bigint; validator: EthAddress }[] {
127
+ return actions.map(({ validator, slashAmount }) => ({
128
+ validator: EthAddress.fromString(validator),
129
+ slashAmount,
130
+ }));
131
+ }
132
+
133
+ /** Tries to extract a VoteCast event from the given logs. */
134
+ public tryExtractVoteCastEvent(logs: Log[]) {
135
+ return tryExtractEvent(logs, this.address.toString(), TallySlashingProposerAbi, 'VoteCast');
136
+ }
137
+
138
+ /** Tries to extract a RoundExecuted event from the given logs. */
139
+ public tryExtractRoundExecutedEvent(logs: Log[]) {
140
+ return tryExtractEvent(logs, this.address.toString(), TallySlashingProposerAbi, 'RoundExecuted');
141
+ }
142
+
143
+ /**
144
+ * Create a transaction to vote for slashing offenses
145
+ * @param votes - The encoded votes for slashing
146
+ * @param slot - The slot number for the vote
147
+ * @param signer - The signer to produce the signature
148
+ * @returns L1 transaction request
149
+ */
150
+ public async buildVoteRequestFromSigner(
151
+ votes: Hex,
152
+ slot: bigint,
153
+ signer: (msg: TypedDataDefinition) => Promise<Hex>,
154
+ ): Promise<L1TxRequest> {
155
+ const typedData = this.buildVoteTypedData(votes, slot);
156
+ const signature = Signature.fromString(await signer(typedData));
157
+
158
+ return {
159
+ to: this.contract.address,
160
+ data: encodeFunctionData({
161
+ abi: TallySlashingProposerAbi,
162
+ functionName: 'vote',
163
+ args: [votes, signature.toViemSignature()],
164
+ }),
165
+ };
166
+ }
167
+
168
+ /** Returns the typed data definition to EIP712-sign for voting */
169
+ public buildVoteTypedData(votes: Hex, slot: bigint): TypedDataDefinition {
170
+ const domain = {
171
+ name: 'TallySlashingProposer',
172
+ version: '1',
173
+ chainId: this.client.chain.id,
174
+ verifyingContract: this.contract.address,
175
+ };
176
+
177
+ const types = {
178
+ EIP712Domain: [
179
+ { name: 'name', type: 'string' },
180
+ { name: 'version', type: 'string' },
181
+ { name: 'chainId', type: 'uint256' },
182
+ { name: 'verifyingContract', type: 'address' },
183
+ ],
184
+ Vote: [
185
+ { name: 'votes', type: 'bytes' },
186
+ { name: 'slot', type: 'uint256' },
187
+ ],
188
+ };
189
+
190
+ return { domain, types, primaryType: 'Vote', message: { votes, slot } };
191
+ }
192
+
193
+ /** Gets the digest to sign for voting directly from the contract */
194
+ public async getVoteDataDigest(votes: Hex, slot: bigint): Promise<Buffer32> {
195
+ return Buffer32.fromString(await this.contract.read.getVoteSignatureDigest([votes, slot]));
196
+ }
197
+
198
+ /**
199
+ * Create a transaction to vote for slashing offenses
200
+ * @param votes - The encoded votes for slashing
201
+ * @param signature - The signature from the current proposer
202
+ * @returns L1 transaction request
203
+ */
204
+ public buildVoteRequestWithSignature(votes: Hex, signature: { v: number; r: Hex; s: Hex }): L1TxRequest {
205
+ return {
206
+ to: this.contract.address,
207
+ data: encodeFunctionData({
208
+ abi: TallySlashingProposerAbi,
209
+ functionName: 'vote',
210
+ args: [votes, signature],
211
+ }),
212
+ };
213
+ }
214
+
215
+ /**
216
+ * Create a transaction to execute a slashing round
217
+ * @param round - The round number to execute
218
+ * @param committees - The committees for each epoch in the round
219
+ * @returns L1 transaction request
220
+ */
221
+ public buildExecuteRoundRequest(round: bigint, committees: EthAddress[][]): L1TxRequest {
222
+ return {
223
+ to: this.contract.address,
224
+ data: encodeFunctionData({
225
+ abi: TallySlashingProposerAbi,
226
+ functionName: 'executeRound',
227
+ args: [round, committees.map(c => c.map(addr => addr.toString()))],
228
+ }),
229
+ };
230
+ }
231
+
232
+ /** Returns the last vote emitted for a given round */
233
+ public async getLastVote(round: bigint) {
234
+ const { voteCount } = await this.getRound(round);
235
+ const validators = (await this.contract.simulate.getSlashTargetCommittees([round])).result.flat();
236
+ const vote = await this.contract.read.getVotes([round, voteCount - 1n]);
237
+ const decoded = decodeSlashConsensusVotes(hexToBuffer(vote));
238
+ const slashAmounts = await this.getSlashingAmounts();
239
+ return decoded
240
+ .map((units, i) => ({
241
+ validator: EthAddress.fromString(validators[i]),
242
+ slashAmount: slashAmounts[units - 1] ?? 0n,
243
+ }))
244
+ .filter(v => v.slashAmount > 0n);
245
+ }
246
+
247
+ /**
248
+ * Listen for VoteCast events
249
+ * @param callback - Callback function to handle vote cast events
250
+ * @returns Unwatch function
251
+ */
252
+ public listenToVoteCast(callback: (args: { round: bigint; proposer: string }) => void): () => void {
253
+ return this.contract.watchEvent.VoteCast(
254
+ {},
255
+ {
256
+ onLogs: logs => {
257
+ for (const log of logs) {
258
+ const { round, proposer } = log.args;
259
+ if (round !== undefined && proposer) {
260
+ callback({ round, proposer });
261
+ }
262
+ }
263
+ },
264
+ },
265
+ );
266
+ }
267
+
268
+ /**
269
+ * Listen for RoundExecuted events
270
+ * @param callback - Callback function to handle round executed events
271
+ * @returns Unwatch function
272
+ */
273
+ public listenToRoundExecuted(
274
+ callback: (args: { round: bigint; slashCount: bigint; l1BlockHash: Hex }) => void,
275
+ ): () => void {
276
+ return this.contract.watchEvent.RoundExecuted(
277
+ {},
278
+ {
279
+ onLogs: logs => {
280
+ for (const log of logs) {
281
+ const { round, slashCount } = log.args;
282
+ if (round !== undefined && slashCount !== undefined) {
283
+ callback({ round, slashCount, l1BlockHash: log.blockHash });
284
+ }
285
+ }
286
+ },
287
+ },
288
+ );
289
+ }
290
+ }
291
+
292
+ /**
293
+ * Decodes a Buffer containing slash votes back into an array of numbers.
294
+ * Each vote is represented as a 2-bit value (0, 1, 2, or 3) representing slashing units.
295
+ * @dev This should live in stdlib next to encodeSlashConsensusVotes but is here since we
296
+ * do not have a dependency to stdlib from the ethereum package. We need a larger refactor to fix this.
297
+ * @param buffer - The Buffer containing encoded slash votes
298
+ * @returns An array of numbers representing the slash votes
299
+ */
300
+ export function decodeSlashConsensusVotes(buffer: Buffer): number[] {
301
+ const votes: number[] = [];
302
+ for (let i = 0; i < buffer.length; i++) {
303
+ const voteByte = buffer.readUInt8(i);
304
+ // Decode votes from Solidity's bit order (LSB to MSB)
305
+ // Bits 0-1: validator at index i*4
306
+ // Bits 2-3: validator at index i*4+1
307
+ // Bits 4-5: validator at index i*4+2
308
+ // Bits 6-7: validator at index i*4+3
309
+ votes.push((voteByte >> 0) & 0x03);
310
+ votes.push((voteByte >> 2) & 0x03);
311
+ votes.push((voteByte >> 4) & 0x03);
312
+ votes.push((voteByte >> 6) & 0x03);
313
+ }
314
+ return votes;
315
+ }
@@ -0,0 +1,14 @@
1
+ import type { ViemClient } from '../types.js';
2
+ import { BlockTagTooOldError } from './errors.js';
3
+
4
+ const L1_NON_ARCHIVE_BLOCK_HISTORY_LENGTH = 128n;
5
+
6
+ export async function checkBlockTag(block: bigint | undefined, publicClient: ViemClient) {
7
+ if (block === undefined) {
8
+ return;
9
+ }
10
+ const latestBlock = await publicClient.getBlockNumber();
11
+ if (block < latestBlock - L1_NON_ARCHIVE_BLOCK_HISTORY_LENGTH) {
12
+ throw new BlockTagTooOldError(block, latestBlock);
13
+ }
14
+ }