1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use ethers::{
    abi::{Error, ParseError},
    prelude::{
        k256::{
            ecdsa::{RecoveryId, Signature as RecoverableSignature},
            schnorr::signature::hazmat::PrehashSigner,
        },
        signer::SignerMiddlewareError,
        AbiError, ContractError, SignerMiddleware,
    },
    providers::{Middleware, ProviderError},
    signers::{Wallet, WalletError},
    types::transaction::eip712::Eip712Error,
};

use crate::contracts::main_contract::MainContractError;

#[derive(thiserror::Error, Debug)]
pub enum ZKSWalletError<M, D>
where
    M: Middleware,
    D: PrehashSigner<(RecoverableSignature, RecoveryId)> + Sync + Send,
{
    #[error("Provider error: {0}")]
    ProviderError(#[from] ProviderError),
    #[error("Middleware error: {0}")]
    MiddlewareError(#[from] SignerMiddlewareError<M, Wallet<D>>),
    #[error("Wallet error: {0}")]
    EthWalletError(#[from] WalletError),
    #[error("ABI error: {0}")]
    AbiError(#[from] AbiError),
    #[error("EIP712 error: {0}")]
    Eip712Error(#[from] Eip712Error),
    #[error("No L1 Ethereum provider")]
    NoL1ProviderError(),
    #[error("No L2 Ethereum provider")]
    NoL2ProviderError(),
    #[error("Contract error: {0}")]
    ContractError(#[from] ContractError<M>),
    #[error("Contract error: {0}")]
    RequestConversionError(#[from] ZKRequestError),
    #[error("{0}")]
    CustomError(String),
    #[error("Main contract error: {0}")]
    MainContractError(#[from] MainContractError<M, D>),
}

impl<M, D> From<ContractError<SignerMiddleware<M, Wallet<D>>>> for ZKSWalletError<M, D>
where
    M: Middleware,
    D: PrehashSigner<(RecoverableSignature, RecoveryId)> + Sync + Send,
{
    fn from(value: ContractError<SignerMiddleware<M, Wallet<D>>>) -> Self {
        Self::CustomError(format!("{value:?}"))
    }
}

#[derive(thiserror::Error, Debug)]
pub enum ZKRequestError {
    #[error("Error parsing function: {0}")]
    ParseFunctionError(#[from] ParseError),
    #[error("ABI error: {0}")]
    AbiError(#[from] AbiError),
    #[error("Encoding or decoding error: {0}")]
    Error(#[from] Error),
    #[error("{0}")]
    CustomError(String),
}