Skip to content

Commit

Permalink
feat(nft-swap): nft swap protocol v2 POC (#2084)
Browse files Browse the repository at this point in the history
This commit implements the NFT maker payment sending, validating, spending and refunding. This process is tested using the dockerized geth dev node.
  • Loading branch information
laruh authored Apr 9, 2024
1 parent 92199d8 commit 6d45cfe
Show file tree
Hide file tree
Showing 27 changed files with 3,513 additions and 354 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 36 additions & 50 deletions mm2src/coins/coin_errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{eth::Web3RpcError, my_tx_history_v2::MyTxHistoryErrorV2, utxo::rpc_clients::UtxoRpcError, DelegationError,
NumConversError, TxHistoryError, UnexpectedDerivationMethod, WithdrawError};
use crate::eth::nft_swap_v2::errors::{Erc721FunctionError, HtlcParamsError, PaymentStatusErr, PrepareTxDataError};
use crate::eth::{EthAssocTypesError, EthNftAssocTypesError, Web3RpcError};
use crate::{utxo::rpc_clients::UtxoRpcError, NumConversError, UnexpectedDerivationMethod};
use enum_derives::EnumFromStringify;
use futures01::Future;
use mm2_err_handle::prelude::MmError;
use spv_validation::helpers_validation::SPVError;
Expand All @@ -11,11 +13,21 @@ pub type ValidatePaymentFut<T> = Box<dyn Future<Item = T, Error = MmError<Valida
pub type ValidatePaymentResult<T> = Result<T, MmError<ValidatePaymentError>>;

/// Enum covering possible error cases of swap payment validation
#[derive(Debug, Display)]
#[derive(Debug, Display, EnumFromStringify)]
pub enum ValidatePaymentError {
/// Should be used to indicate internal MM2 state problems (e.g., DB errors, etc.).
#[from_stringify(
"EthAssocTypesError",
"Erc721FunctionError",
"EthNftAssocTypesError",
"NumConversError",
"UnexpectedDerivationMethod",
"keys::Error",
"PrepareTxDataError"
)]
InternalError(String),
/// Problem with deserializing the transaction, or one of the transaction parts is invalid.
#[from_stringify("rlp::DecoderError", "serialization::Error")]
TxDeserializationError(String),
/// One of the input parameters is invalid.
InvalidParameter(String),
Expand All @@ -28,6 +40,7 @@ pub enum ValidatePaymentError {
/// Payment transaction is in unexpected state. E.g., `Uninitialized` instead of `Sent` for ETH payment.
UnexpectedPaymentState(String),
/// Transport (RPC) error.
#[from_stringify("web3::Error")]
Transport(String),
/// Transaction has wrong properties, for example, it has been sent to a wrong address.
WrongPaymentTx(String),
Expand All @@ -39,30 +52,10 @@ pub enum ValidatePaymentError {
NftProtocolNotSupported,
}

impl From<rlp::DecoderError> for ValidatePaymentError {
fn from(err: rlp::DecoderError) -> Self { Self::TxDeserializationError(err.to_string()) }
}

impl From<web3::Error> for ValidatePaymentError {
fn from(err: web3::Error) -> Self { Self::Transport(err.to_string()) }
}

impl From<NumConversError> for ValidatePaymentError {
fn from(err: NumConversError) -> Self { Self::InternalError(err.to_string()) }
}

impl From<SPVError> for ValidatePaymentError {
fn from(err: SPVError) -> Self { Self::SPVError(err) }
}

impl From<serialization::Error> for ValidatePaymentError {
fn from(err: serialization::Error) -> Self { Self::TxDeserializationError(err.to_string()) }
}

impl From<UnexpectedDerivationMethod> for ValidatePaymentError {
fn from(err: UnexpectedDerivationMethod) -> Self { Self::InternalError(err.to_string()) }
}

impl From<UtxoRpcError> for ValidatePaymentError {
fn from(err: UtxoRpcError) -> Self {
match err {
Expand All @@ -86,36 +79,29 @@ impl From<Web3RpcError> for ValidatePaymentError {
}
}

impl From<keys::Error> for ValidatePaymentError {
fn from(err: keys::Error) -> Self { Self::InternalError(err.to_string()) }
impl From<PaymentStatusErr> for ValidatePaymentError {
fn from(err: PaymentStatusErr) -> Self {
match err {
PaymentStatusErr::Transport(e) => Self::Transport(e),
PaymentStatusErr::AbiError(e)
| PaymentStatusErr::Internal(e)
| PaymentStatusErr::TxDeserializationError(e) => Self::InternalError(e),
}
}
}

impl From<HtlcParamsError> for ValidatePaymentError {
fn from(err: HtlcParamsError) -> Self {
match err {
HtlcParamsError::WrongPaymentTx(e) => ValidatePaymentError::WrongPaymentTx(e),
HtlcParamsError::TxDeserializationError(e) => ValidatePaymentError::TxDeserializationError(e),
}
}
}

#[derive(Debug, Display)]
#[derive(Debug, Display, EnumFromStringify)]
pub enum MyAddressError {
#[from_stringify("UnexpectedDerivationMethod")]
UnexpectedDerivationMethod(String),
InternalError(String),
}

impl From<UnexpectedDerivationMethod> for MyAddressError {
fn from(err: UnexpectedDerivationMethod) -> Self { Self::UnexpectedDerivationMethod(err.to_string()) }
}

impl From<MyAddressError> for WithdrawError {
fn from(err: MyAddressError) -> Self { Self::InternalError(err.to_string()) }
}

impl From<MyAddressError> for UtxoRpcError {
fn from(err: MyAddressError) -> Self { Self::Internal(err.to_string()) }
}

impl From<MyAddressError> for DelegationError {
fn from(err: MyAddressError) -> Self { Self::InternalError(err.to_string()) }
}

impl From<MyAddressError> for TxHistoryError {
fn from(err: MyAddressError) -> Self { Self::InternalError(err.to_string()) }
}

impl From<MyAddressError> for MyTxHistoryErrorV2 {
fn from(err: MyAddressError) -> Self { Self::Internal(err.to_string()) }
}
Loading

0 comments on commit 6d45cfe

Please sign in to comment.