Skip to content

Commit

Permalink
feat(eth-swap): eth tpu v2 methods, eth docker test enhancements (#2169)
Browse files Browse the repository at this point in the history
This commit implements EVM TPU taker methods and adds some enhancements for eth docker tests.
  • Loading branch information
laruh authored Aug 30, 2024
1 parent 932669a commit f6e8348
Show file tree
Hide file tree
Showing 24 changed files with 2,217 additions and 678 deletions.
20 changes: 15 additions & 5 deletions mm2src/coins/coin_errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::eth::nft_swap_v2::errors::{Erc721FunctionError, HtlcParamsError, PaymentStatusErr, PrepareTxDataError};
use crate::eth::eth_swap_v2::{PaymentStatusErr, ValidatePaymentV2Err};
use crate::eth::nft_swap_v2::errors::{Erc721FunctionError, HtlcParamsError, PrepareTxDataError};
use crate::eth::{EthAssocTypesError, EthNftAssocTypesError, Web3RpcError};
use crate::{utxo::rpc_clients::UtxoRpcError, NumConversError, UnexpectedDerivationMethod};
use enum_derives::EnumFromStringify;
Expand Down Expand Up @@ -50,6 +51,7 @@ pub enum ValidatePaymentError {
TimelockOverflow(TryFromIntError),
#[display(fmt = "Nft Protocol is not supported yet!")]
NftProtocolNotSupported,
InvalidData(String),
}

impl From<SPVError> for ValidatePaymentError {
Expand Down Expand Up @@ -84,9 +86,8 @@ 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),
PaymentStatusErr::ABIError(e) | PaymentStatusErr::Internal(e) => Self::InternalError(e),
PaymentStatusErr::InvalidData(e) => Self::InvalidData(e),
}
}
}
Expand All @@ -95,7 +96,16 @@ impl From<HtlcParamsError> for ValidatePaymentError {
fn from(err: HtlcParamsError) -> Self {
match err {
HtlcParamsError::WrongPaymentTx(e) => ValidatePaymentError::WrongPaymentTx(e),
HtlcParamsError::TxDeserializationError(e) => ValidatePaymentError::TxDeserializationError(e),
HtlcParamsError::ABIError(e) | HtlcParamsError::InvalidData(e) => ValidatePaymentError::InvalidData(e),
}
}
}

impl From<ValidatePaymentV2Err> for ValidatePaymentError {
fn from(err: ValidatePaymentV2Err) -> Self {
match err {
ValidatePaymentV2Err::UnexpectedPaymentState(e) => ValidatePaymentError::UnexpectedPaymentState(e),
ValidatePaymentV2Err::WrongPaymentTx(e) => ValidatePaymentError::WrongPaymentTx(e),
}
}
}
Expand Down
100 changes: 78 additions & 22 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ mod eip1559_gas_fee;
pub(crate) use eip1559_gas_fee::FeePerGasEstimated;
use eip1559_gas_fee::{BlocknativeGasApiCaller, FeePerGasSimpleEstimator, GasApiConfig, GasApiProvider,
InfuraGasApiCaller};
mod eth_swap_v2;
pub(crate) mod eth_swap_v2;

/// https://github.com/artemii235/etomic-swap/blob/master/contracts/EtomicSwap.sol
/// Dev chain (195.201.137.5:8565) contract address: 0x83965C539899cC0F918552e5A26915de40ee8852
Expand Down Expand Up @@ -827,9 +827,18 @@ impl EthCoinImpl {
/// The id used to differentiate payments on Etomic swap smart contract
pub(crate) fn etomic_swap_id(&self, time_lock: u32, secret_hash: &[u8]) -> Vec<u8> {
let timelock_bytes = time_lock.to_le_bytes();
self.generate_etomic_swap_id(&timelock_bytes, secret_hash)
}

let mut input = Vec::with_capacity(timelock_bytes.len() + secret_hash.len());
input.extend_from_slice(&timelock_bytes);
/// The id used to differentiate payments on Etomic swap v2 smart contracts
pub(crate) fn etomic_swap_id_v2(&self, time_lock: u64, secret_hash: &[u8]) -> Vec<u8> {
let timelock_bytes = time_lock.to_le_bytes();
self.generate_etomic_swap_id(&timelock_bytes, secret_hash)
}

fn generate_etomic_swap_id(&self, time_lock_bytes: &[u8], secret_hash: &[u8]) -> Vec<u8> {
let mut input = Vec::with_capacity(time_lock_bytes.len() + secret_hash.len());
input.extend_from_slice(time_lock_bytes);
input.extend_from_slice(secret_hash);
sha256(&input).to_vec()
}
Expand Down Expand Up @@ -7031,32 +7040,38 @@ impl Eip1559Ops for EthCoin {

#[async_trait]
impl TakerCoinSwapOpsV2 for EthCoin {
/// Wrapper for [EthCoin::send_taker_funding_impl]
async fn send_taker_funding(&self, args: SendTakerFundingArgs<'_>) -> Result<Self::Tx, TransactionErr> {
self.send_taker_funding_impl(args).await
}

async fn validate_taker_funding(&self, _args: ValidateTakerFundingArgs<'_, Self>) -> ValidateSwapV2TxResult {
todo!()
/// Wrapper for [EthCoin::validate_taker_funding_impl]
async fn validate_taker_funding(&self, args: ValidateTakerFundingArgs<'_, Self>) -> ValidateSwapV2TxResult {
self.validate_taker_funding_impl(args).await
}

async fn refund_taker_funding_timelock(&self, _args: RefundPaymentArgs<'_>) -> Result<Self::Tx, TransactionErr> {
todo!()
async fn refund_taker_funding_timelock(
&self,
args: RefundTakerPaymentArgs<'_>,
) -> Result<Self::Tx, TransactionErr> {
self.refund_taker_payment_with_timelock_impl(args).await
}

async fn refund_taker_funding_secret(
&self,
_args: RefundFundingSecretArgs<'_, Self>,
args: RefundFundingSecretArgs<'_, Self>,
) -> Result<Self::Tx, TransactionErr> {
todo!()
self.refund_taker_funding_secret_impl(args).await
}

/// Wrapper for [EthCoin::search_for_taker_funding_spend_impl]
async fn search_for_taker_funding_spend(
&self,
_tx: &Self::Tx,
tx: &Self::Tx,
_from_block: u64,
_secret_hash: &[u8],
) -> Result<Option<FundingTxSpend<Self>>, SearchForFundingSpendErr> {
todo!()
self.search_for_taker_funding_spend_impl(tx).await
}

/// Eth doesnt have preimages
Expand All @@ -7080,18 +7095,21 @@ impl TakerCoinSwapOpsV2 for EthCoin {
Ok(())
}

/// Eth doesnt use multisig
/// Wrapper for [EthCoin::taker_payment_approve]
async fn sign_and_send_taker_funding_spend(
&self,
_preimage: &TxPreimageWithSig<Self>,
_args: &GenTakerFundingSpendArgs<'_, Self>,
args: &GenTakerFundingSpendArgs<'_, Self>,
_swap_unique_data: &[u8],
) -> Result<Self::Tx, TransactionErr> {
todo!()
self.taker_payment_approve(args).await
}

async fn refund_combined_taker_payment(&self, _args: RefundPaymentArgs<'_>) -> Result<Self::Tx, TransactionErr> {
todo!()
async fn refund_combined_taker_payment(
&self,
args: RefundTakerPaymentArgs<'_>,
) -> Result<Self::Tx, TransactionErr> {
self.refund_taker_payment_with_timelock_impl(args).await
}

/// Eth doesnt have preimages
Expand All @@ -7115,23 +7133,25 @@ impl TakerCoinSwapOpsV2 for EthCoin {
Ok(())
}

/// Wrapper for [EthCoin::sign_and_broadcast_taker_payment_spend_impl]
async fn sign_and_broadcast_taker_payment_spend(
&self,
_preimage: &TxPreimageWithSig<Self>,
_gen_args: &GenTakerPaymentSpendArgs<'_, Self>,
_secret: &[u8],
gen_args: &GenTakerPaymentSpendArgs<'_, Self>,
secret: &[u8],
_swap_unique_data: &[u8],
) -> Result<Self::Tx, TransactionErr> {
todo!()
self.sign_and_broadcast_taker_payment_spend_impl(gen_args, secret).await
}

/// Wrapper for [EthCoin::wait_for_taker_payment_spend_impl]
async fn wait_for_taker_payment_spend(
&self,
_taker_payment: &Self::Tx,
taker_payment: &Self::Tx,
_from_block: u64,
_wait_until: u64,
wait_until: u64,
) -> MmResult<Self::Tx, WaitForTakerPaymentSpendError> {
todo!()
self.wait_for_taker_payment_spend_impl(taker_payment, wait_until).await
}
}

Expand Down Expand Up @@ -7162,3 +7182,39 @@ impl CommonSwapOpsV2 for EthCoin {
self.derive_htlc_pubkey_v2(swap_unique_data).to_bytes()
}
}

#[cfg(all(feature = "for-tests", not(target_arch = "wasm32")))]
impl EthCoin {
pub async fn set_coin_type(&self, new_coin_type: EthCoinType) -> EthCoin {
let coin = EthCoinImpl {
ticker: self.ticker.clone(),
coin_type: new_coin_type,
priv_key_policy: self.priv_key_policy.clone(),
derivation_method: Arc::clone(&self.derivation_method),
sign_message_prefix: self.sign_message_prefix.clone(),
swap_contract_address: self.swap_contract_address,
swap_v2_contracts: self.swap_v2_contracts,
fallback_swap_contract: self.fallback_swap_contract,
contract_supports_watchers: self.contract_supports_watchers,
web3_instances: AsyncMutex::new(self.web3_instances.lock().await.clone()),
decimals: self.decimals,
history_sync_state: Mutex::new(self.history_sync_state.lock().unwrap().clone()),
required_confirmations: AtomicU64::new(
self.required_confirmations.load(std::sync::atomic::Ordering::SeqCst),
),
swap_txfee_policy: Mutex::new(self.swap_txfee_policy.lock().unwrap().clone()),
max_eth_tx_type: self.max_eth_tx_type,
ctx: self.ctx.clone(),
chain_id: self.chain_id,
trezor_coin: self.trezor_coin.clone(),
logs_block_range: self.logs_block_range,
address_nonce_locks: Arc::clone(&self.address_nonce_locks),
erc20_tokens_infos: Arc::clone(&self.erc20_tokens_infos),
nfts_infos: Arc::clone(&self.nfts_infos),
platform_fee_estimator_state: Arc::clone(&self.platform_fee_estimator_state),
gas_limit: EthGasLimit::default(),
abortable_system: self.abortable_system.create_subsystem().unwrap(),
};
EthCoin(Arc::new(coin))
}
}
Loading

0 comments on commit f6e8348

Please sign in to comment.