use alloy_primitives::Address as EvmAddress;
use r402_core::error::VerificationError;
#[derive(Debug, thiserror::Error)]
pub enum TronExactError {
#[error("Can not recover signer from signature: {0}")]
SignatureRecovery(String),
#[error("Signature does not match the declared sender")]
SignerMismatch,
#[error("Insufficient token balance")]
InsufficientBalance,
#[error("Authorization not yet valid")]
NotYetValid,
#[error("Authorization expired")]
Expired,
#[error("Authorization value does not match required amount")]
ValueMismatch,
#[error("Authorization recipient does not match payment requirements")]
RecipientMismatch,
#[error("Authorization asset does not match payment requirements")]
AssetMismatch,
#[error("Permit2 spender is not the canonical x402ExactPermit2Proxy: {0}")]
InvalidPermit2Spender(EvmAddress),
#[error("Authorization nonce already used")]
NonceAlreadyUsed,
#[error("Asset transfer method not supported on this network")]
UnsupportedTransferMethod,
#[error("Chain mismatch")]
ChainMismatch,
#[error("Missing TIP-712 domain parameters for EIP-3009 transfer")]
MissingTip712Domain,
#[error("TronGrid request failed: {0}")]
TronGrid(String),
#[error("Transaction failed: {0}")]
TransactionFailed(String),
#[error("Timed out waiting for transaction confirmation")]
ConfirmationTimeout,
}
impl From<TronExactError> for VerificationError {
fn from(e: TronExactError) -> Self {
match e {
TronExactError::SignatureRecovery(ref msg) => Self::InvalidSignature(msg.clone()),
TronExactError::InsufficientBalance => Self::InsufficientFunds,
TronExactError::NotYetValid => Self::Early,
TronExactError::Expired => Self::Expired,
TronExactError::ValueMismatch => Self::InvalidPaymentAmount,
TronExactError::RecipientMismatch => Self::RecipientMismatch,
TronExactError::AssetMismatch => Self::AssetMismatch,
TronExactError::NonceAlreadyUsed => Self::NonceAlreadyUsed,
TronExactError::ChainMismatch => Self::ChainIdMismatch,
TronExactError::SignerMismatch
| TronExactError::UnsupportedTransferMethod
| TronExactError::MissingTip712Domain
| TronExactError::InvalidPermit2Spender(_)
| TronExactError::TransactionFailed(_)
| TronExactError::ConfirmationTimeout
| TronExactError::TronGrid(_) => Self::SimulationFailed(e.to_string()),
}
}
}
impl From<VerificationError> for TronExactError {
fn from(e: VerificationError) -> Self {
match e {
VerificationError::InvalidSignature(msg) => Self::SignatureRecovery(msg),
VerificationError::InsufficientFunds => Self::InsufficientBalance,
VerificationError::Early => Self::NotYetValid,
VerificationError::Expired => Self::Expired,
VerificationError::InvalidPaymentAmount => Self::ValueMismatch,
VerificationError::RecipientMismatch => Self::RecipientMismatch,
VerificationError::AssetMismatch => Self::AssetMismatch,
VerificationError::NonceAlreadyUsed => Self::NonceAlreadyUsed,
VerificationError::ChainIdMismatch => Self::ChainMismatch,
other => Self::TronGrid(other.to_string()),
}
}
}