|
| 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 | + * Gets the current vetoer address. |
| 43 | + * @returns The vetoer address |
| 44 | + */ |
| 45 | + public async getVetoer(): Promise<EthAddress> { |
| 46 | + const vetoer = await this.contract.read.VETOER(); |
| 47 | + return EthAddress.fromString(vetoer); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Gets the current governance address. |
| 52 | + * @returns The governance address |
| 53 | + */ |
| 54 | + public async getGovernance(): Promise<EthAddress> { |
| 55 | + const governance = await this.contract.read.GOVERNANCE(); |
| 56 | + return EthAddress.fromString(governance); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Gets the current proposer address. |
| 61 | + * @returns The proposer address |
| 62 | + */ |
| 63 | + public async getProposer(): Promise<EthAddress> { |
| 64 | + const proposer = await this.contract.read.PROPOSER(); |
| 65 | + return EthAddress.fromString(proposer); |
| 66 | + } |
| 67 | +} |
0 commit comments