import { Hash } from "viem" import { TransactionSender, TransactionSenderResponse, type TransactionSenderRequest, } from "./TransactionSender" import { mezoMainnet, mezoTestnet } from "./chains" /** * A TransactionSender that relays through the Mezo relayer. Note that the Mezo * relayer verifies that the relayed call is to a Safe, a call to * execTransaction, and that the call refunds the relayer for spent gas. This * means the underlying smart account must be funded, and the signed * transaction must be performing the expected behavior. */ export default class MezoTransactionSender implements TransactionSender { private relayEndpoint: string // The address of the receiver of refunds; must match the expected receiver // of the relay, or the relay will reject transactions. public refundReceiver: string constructor( chainId: number = mezoMainnet.id, relayEndpoint?: string, refundReceiver?: string, ) { switch (chainId) { case mezoMainnet.id: this.relayEndpoint = relayEndpoint ?? "https://mezo.org/api/v2/relay/transactions" this.refundReceiver = refundReceiver ?? "0xf35c1a20303ddcA464Ebf5b3b023588Cf3181c8a" // Mezo Passport Mainnet Relayer break case mezoTestnet.id: this.relayEndpoint = relayEndpoint ?? "https://test.mezo.org/api/v2/relay/transactions" this.refundReceiver = refundReceiver ?? "0x6e80164ea60673D64d5d6228beb684a1274Bb017" // testertesting.eth break default: throw new Error(`Unsupported chainId: ${chainId}`) } } async sendTransaction( transactionRequest: TransactionSenderRequest, ): Promise { try { const response = await fetch(this.relayEndpoint, { method: "POST", body: JSON.stringify({ txTo: transactionRequest.to, txData: transactionRequest.data, }), headers: { "Content-Type": "application/json" }, }) const { transactionHash } = (await response.json()) as { transactionHash: Hash } // eslint-disable-next-line no-console console.log( `Transaction successfully submitted with hash: ${transactionHash}`, ) return { hash: transactionHash ?? "0x" } } catch (error) { // eslint-disable-next-line no-console console.log(`Error relaying transaction: ${error}`) throw error } } }