Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into fix-hd-acc-activation
Browse files Browse the repository at this point in the history
  • Loading branch information
shamardy committed Aug 23, 2023
2 parents 5324bca + 483f04c commit afe2e08
Show file tree
Hide file tree
Showing 42 changed files with 1,821 additions and 958 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

22 changes: 11 additions & 11 deletions mm2src/coins/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ use crate::nft::{find_wallet_nft_amount, WithdrawNftResult};
use v2_activation::{build_address_and_priv_key_policy, EthActivationV2Error};

mod nonce;
use crate::{PrivKeyPolicy, WithdrawFrom};
use crate::{PrivKeyPolicy, TransactionResult, WithdrawFrom};
use nonce::ParityNonce;

/// https://github.com/artemii235/etomic-swap/blob/master/contracts/EtomicSwap.sol
Expand Down Expand Up @@ -1094,18 +1094,18 @@ impl SwapOps for EthCoin {
)
}

fn send_taker_refunds_payment(&self, taker_refunds_payment_args: RefundPaymentArgs) -> TransactionFut {
Box::new(
self.refund_hash_time_locked_payment(taker_refunds_payment_args)
.map(TransactionEnum::from),
)
async fn send_taker_refunds_payment(&self, taker_refunds_payment_args: RefundPaymentArgs<'_>) -> TransactionResult {
self.refund_hash_time_locked_payment(taker_refunds_payment_args)
.map(TransactionEnum::from)
.compat()
.await
}

fn send_maker_refunds_payment(&self, maker_refunds_payment_args: RefundPaymentArgs) -> TransactionFut {
Box::new(
self.refund_hash_time_locked_payment(maker_refunds_payment_args)
.map(TransactionEnum::from),
)
async fn send_maker_refunds_payment(&self, maker_refunds_payment_args: RefundPaymentArgs<'_>) -> TransactionResult {
self.refund_hash_time_locked_payment(maker_refunds_payment_args)
.map(TransactionEnum::from)
.compat()
.await
}

fn validate_fee(&self, validate_fee_args: ValidateFeeArgs<'_>) -> ValidatePaymentFut<()> {
Expand Down
10 changes: 2 additions & 8 deletions mm2src/coins/eth/eth_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,7 @@ fn send_and_refund_erc20_payment() {
swap_unique_data: &[],
watcher_reward: false,
};
let refund = coin
.send_maker_refunds_payment(maker_refunds_payment_args)
.wait()
.unwrap();
let refund = block_on(coin.send_maker_refunds_payment(maker_refunds_payment_args)).unwrap();
log!("{:?}", refund);

let status = block_on(
Expand Down Expand Up @@ -470,10 +467,7 @@ fn send_and_refund_eth_payment() {
swap_unique_data: &[],
watcher_reward: false,
};
let refund = coin
.send_maker_refunds_payment(maker_refunds_payment_args)
.wait()
.unwrap();
let refund = block_on(coin.send_maker_refunds_payment(maker_refunds_payment_args)).unwrap();

log!("{:?}", refund);

Expand Down
26 changes: 16 additions & 10 deletions mm2src/coins/lightning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ use crate::{BalanceFut, CheckIfMyPaymentSentArgs, CoinBalance, CoinFutSpawner, C
RefundResult, SearchForSwapTxSpendInput, SendMakerPaymentSpendPreimageInput, SendPaymentArgs,
SignatureError, SignatureResult, SpendPaymentArgs, SwapOps, TakerSwapMakerCoin, TradeFee,
TradePreimageFut, TradePreimageResult, TradePreimageValue, Transaction, TransactionEnum, TransactionErr,
TransactionFut, TxMarshalingErr, UnexpectedDerivationMethod, UtxoStandardCoin, ValidateAddressResult,
ValidateFeeArgs, ValidateInstructionsErr, ValidateOtherPubKeyErr, ValidatePaymentError,
ValidatePaymentFut, ValidatePaymentInput, VerificationError, VerificationResult, WaitForHTLCTxSpendArgs,
WatcherOps, WatcherReward, WatcherRewardError, WatcherSearchForSwapTxSpendInput,
TransactionFut, TransactionResult, TxMarshalingErr, UnexpectedDerivationMethod, UtxoStandardCoin,
ValidateAddressResult, ValidateFeeArgs, ValidateInstructionsErr, ValidateOtherPubKeyErr,
ValidatePaymentError, ValidatePaymentFut, ValidatePaymentInput, VerificationError, VerificationResult,
WaitForHTLCTxSpendArgs, WatcherOps, WatcherReward, WatcherRewardError, WatcherSearchForSwapTxSpendInput,
WatcherValidatePaymentInput, WatcherValidateTakerFeeInput, WithdrawError, WithdrawFut, WithdrawRequest};
use async_trait::async_trait;
use bitcoin::bech32::ToBase32;
Expand Down Expand Up @@ -660,16 +660,22 @@ impl SwapOps for LightningCoin {
self.spend_swap_payment(taker_spends_payment_args)
}

fn send_taker_refunds_payment(&self, _taker_refunds_payment_args: RefundPaymentArgs<'_>) -> TransactionFut {
Box::new(futures01::future::err(TransactionErr::Plain(
async fn send_taker_refunds_payment(
&self,
_taker_refunds_payment_args: RefundPaymentArgs<'_>,
) -> TransactionResult {
Err(TransactionErr::Plain(
"Doesn't need transaction broadcast to refund lightning HTLC".into(),
)))
))
}

fn send_maker_refunds_payment(&self, _maker_refunds_payment_args: RefundPaymentArgs<'_>) -> TransactionFut {
Box::new(futures01::future::err(TransactionErr::Plain(
async fn send_maker_refunds_payment(
&self,
_maker_refunds_payment_args: RefundPaymentArgs<'_>,
) -> TransactionResult {
Err(TransactionErr::Plain(
"Doesn't need transaction broadcast to refund lightning HTLC".into(),
)))
))
}

// Todo: This validates the dummy fee for now for the sake of swap P.O.C., this should be implemented probably after agreeing on how fees will work for lightning
Expand Down
135 changes: 133 additions & 2 deletions mm2src/coins/lp_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ pub mod z_coin;
use z_coin::{ZCoin, ZcoinProtocolInfo};

pub type TransactionFut = Box<dyn Future<Item = TransactionEnum, Error = TransactionErr> + Send>;
pub type TransactionResult = Result<TransactionEnum, TransactionErr>;
pub type BalanceResult<T> = Result<T, MmError<BalanceError>>;
pub type BalanceFut<T> = Box<dyn Future<Item = T, Error = MmError<BalanceError>> + Send>;
pub type NonZeroBalanceFut<T> = Box<dyn Future<Item = T, Error = MmError<GetNonZeroBalance>> + Send>;
Expand All @@ -312,6 +313,9 @@ pub type RawTransactionResult = Result<RawTransactionRes, MmError<RawTransaction
pub type RawTransactionFut<'a> =
Box<dyn Future<Item = RawTransactionRes, Error = MmError<RawTransactionError>> + Send + 'a>;
pub type RefundResult<T> = Result<T, MmError<RefundError>>;
pub type GenAndSignDexFeeSpendResult = MmResult<TxPreimageWithSig, TxGenError>;
pub type ValidateDexFeeResult = MmResult<(), ValidateDexFeeError>;
pub type ValidateDexFeeSpendPreimageResult = MmResult<(), ValidateDexFeeSpendPreimageError>;

pub type IguanaPrivKey = Secp256k1Secret;

Expand Down Expand Up @@ -845,9 +849,9 @@ pub trait SwapOps {

fn send_taker_spends_maker_payment(&self, taker_spends_payment_args: SpendPaymentArgs<'_>) -> TransactionFut;

fn send_taker_refunds_payment(&self, taker_refunds_payment_args: RefundPaymentArgs<'_>) -> TransactionFut;
async fn send_taker_refunds_payment(&self, taker_refunds_payment_args: RefundPaymentArgs<'_>) -> TransactionResult;

fn send_maker_refunds_payment(&self, maker_refunds_payment_args: RefundPaymentArgs<'_>) -> TransactionFut;
async fn send_maker_refunds_payment(&self, maker_refunds_payment_args: RefundPaymentArgs<'_>) -> TransactionResult;

fn validate_fee(&self, validate_fee_args: ValidateFeeArgs<'_>) -> ValidatePaymentFut<()>;

Expand Down Expand Up @@ -1013,6 +1017,133 @@ pub trait WatcherOps {
) -> Result<Option<WatcherReward>, MmError<WatcherRewardError>>;
}

pub struct SendDexFeeWithPremiumArgs<'a> {
pub time_lock: u32,
pub secret_hash: &'a [u8],
pub other_pub: &'a [u8],
pub dex_fee_amount: BigDecimal,
pub premium_amount: BigDecimal,
pub swap_unique_data: &'a [u8],
}

pub struct ValidateDexFeeArgs<'a> {
pub dex_fee_tx: &'a [u8],
pub time_lock: u32,
pub secret_hash: &'a [u8],
pub other_pub: &'a [u8],
pub dex_fee_amount: BigDecimal,
pub premium_amount: BigDecimal,
pub swap_unique_data: &'a [u8],
}

pub struct GenDexFeeSpendArgs<'a> {
pub dex_fee_tx: &'a [u8],
pub time_lock: u32,
pub secret_hash: &'a [u8],
pub maker_pub: &'a [u8],
pub taker_pub: &'a [u8],
pub dex_fee_pub: &'a [u8],
pub dex_fee_amount: BigDecimal,
pub premium_amount: BigDecimal,
}

pub struct TxPreimageWithSig {
preimage: Vec<u8>,
signature: Vec<u8>,
}

#[derive(Debug)]
pub enum TxGenError {
Rpc(String),
NumConversion(String),
AddressDerivation(String),
TxDeserialization(String),
InvalidPubkey(String),
Signing(String),
MinerFeeExceedsPremium { miner_fee: BigDecimal, premium: BigDecimal },
Legacy(String),
}

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

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

impl From<UtxoSignWithKeyPairError> for TxGenError {
fn from(err: UtxoSignWithKeyPairError) -> Self { TxGenError::Signing(err.to_string()) }
}

#[derive(Debug)]
pub enum ValidateDexFeeError {
InvalidDestinationOrAmount(String),
InvalidPubkey(String),
NumConversion(String),
Rpc(String),
TxBytesMismatch { from_rpc: BytesJson, actual: BytesJson },
TxDeserialization(String),
TxLacksOfOutputs,
}

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

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

#[derive(Debug)]
pub enum ValidateDexFeeSpendPreimageError {
InvalidPubkey(String),
InvalidTakerSignature,
InvalidPreimage(String),
SignatureVerificationFailure(String),
TxDeserialization(String),
TxGenError(String),
}

impl From<UtxoSignWithKeyPairError> for ValidateDexFeeSpendPreimageError {
fn from(err: UtxoSignWithKeyPairError) -> Self {
ValidateDexFeeSpendPreimageError::SignatureVerificationFailure(err.to_string())
}
}

impl From<TxGenError> for ValidateDexFeeSpendPreimageError {
fn from(err: TxGenError) -> Self { ValidateDexFeeSpendPreimageError::TxGenError(format!("{:?}", err)) }
}

#[async_trait]
pub trait SwapOpsV2 {
async fn send_dex_fee_with_premium(&self, args: SendDexFeeWithPremiumArgs<'_>) -> TransactionResult;

async fn validate_dex_fee_with_premium(&self, args: ValidateDexFeeArgs<'_>) -> ValidateDexFeeResult;

async fn refund_dex_fee_with_premium(&self, args: RefundPaymentArgs<'_>) -> TransactionResult;

async fn gen_and_sign_dex_fee_spend_preimage(
&self,
args: &GenDexFeeSpendArgs<'_>,
swap_unique_data: &[u8],
) -> GenAndSignDexFeeSpendResult;

async fn validate_dex_fee_spend_preimage(
&self,
gen_args: &GenDexFeeSpendArgs<'_>,
preimage: &TxPreimageWithSig,
) -> ValidateDexFeeSpendPreimageResult;

async fn sign_and_broadcast_dex_fee_spend(
&self,
preimage: &TxPreimageWithSig,
gen_args: &GenDexFeeSpendArgs<'_>,
secret: &[u8],
swap_unique_data: &[u8],
) -> TransactionResult;
}

/// Operations that coins have independently from the MarketMaker.
/// That is, things implemented by the coin wallets or public coin services.
pub trait MarketCoinOps {
Expand Down
Loading

0 comments on commit afe2e08

Please sign in to comment.