import { orangeKitSafeFactoryConfig } from "@mezo-org/orangekit-contracts" import { GetContractReturnType as Contract, Hex, encodeFunctionData, getAddress, PublicClient, Address, getContract, } from "viem" import { OrangeKitSafeFactory } from "../contracts" import { SafeDeploymentTransaction } from "../contracts/safe-factory" export default class EthereumOrangeKitSafeFactory implements OrangeKitSafeFactory { #client: PublicClient #instance: Contract constructor(client: PublicClient) { this.#client = client this.#instance = getContract({ ...orangeKitSafeFactoryConfig, client }) } async predictAddress(truncatedBitcoinAddress: Hex): Promise { const [safeAddress] = await this.#instance.read.predictAddresses([ truncatedBitcoinAddress, ]) return getAddress(safeAddress) } async populateSafeDeploymentTransaction( truncatedBitcoinAddress: Hex, ): Promise { const to = this.#instance.address const data = encodeFunctionData({ abi: this.#instance.abi, functionName: "deploySafe", args: [truncatedBitcoinAddress], }) return { to, data } } async simulateSafeDeploymentTransaction( truncatedBitcoinAddress: Hex, from: Address, ): Promise { try { const response = await this.#client.simulateContract({ address: this.#instance.address, abi: this.#instance.abi, functionName: "deploySafe", args: [truncatedBitcoinAddress], account: from, }) return !!response.result } catch (error) { if (error instanceof Error) { throw new Error( `Safe deployment transaction simulation failed: ${error.message}`, ) } throw new Error("Safe deployment transaction simulation failed") } } }