Skip to content

Commit

Permalink
start using nft_maker_swap_v2_contract from EthCoin in Nft maker sw…
Browse files Browse the repository at this point in the history
…ap ops, remove `swap_contract_addres` from NFT swap args
  • Loading branch information
laruh committed Sep 10, 2024
1 parent f1063c7 commit 60f11f8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 30 deletions.
68 changes: 52 additions & 16 deletions mm2src/coins/eth/nft_swap_v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ impl EthCoin {
) -> ValidatePaymentResult<()> {
match self.coin_type {
EthCoinType::Nft { .. } => {
let nft_maker_swap_v2_contract = self
.swap_v2_contracts
.as_ref()
.map(|contracts| contracts.nft_maker_swap_v2_contract)
.ok_or_else(|| {
ValidatePaymentError::InternalError(
"Expected swap_v2_contracts to be Some, but found None".to_string(),
)
})?;
let contract_type = args.nft_swap_info.contract_type;
validate_payment_args(
args.taker_secret_hash,
Expand All @@ -66,14 +75,12 @@ impl EthCoin {
contract_type,
)
.map_err(ValidatePaymentError::InternalError)?;
// TODO use swap contract address from self
let etomic_swap_contract = args.nft_swap_info.swap_contract_address;
let token_address = args.nft_swap_info.token_address;
let maker_address = public_to_address(args.maker_pub);
let swap_id = self.etomic_swap_id_v2(args.time_lock, args.maker_secret_hash);
let maker_status = self
.payment_status_v2(
*etomic_swap_contract,
nft_maker_swap_v2_contract,
Token::FixedBytes(swap_id.clone()),
&NFT_MAKER_SWAP_V2,
EthPaymentType::MakerPayments,
Expand Down Expand Up @@ -107,7 +114,7 @@ impl EthCoin {

let validation_params = ValidationParams {
maker_address,
etomic_swap_contract: *etomic_swap_contract,
nft_maker_swap_v2_contract,
token_id: args.nft_swap_info.token_id,
amount,
};
Expand Down Expand Up @@ -139,7 +146,13 @@ impl EthCoin {
) -> Result<SignedEthTx, TransactionErr> {
match self.coin_type {
EthCoinType::Nft { .. } => {
let etomic_swap_contract = args.swap_contract_address;
let nft_maker_swap_v2_contract = self
.swap_v2_contracts
.as_ref()
.map(|contracts| contracts.nft_maker_swap_v2_contract)
.ok_or_else(|| {
TransactionErr::Plain(ERRL!("Expected swap_v2_contracts to be Some, but found None"))
})?;
if args.maker_secret.len() != 32 {
return Err(TransactionErr::Plain(ERRL!("maker_secret must be 32 bytes")));
}
Expand All @@ -150,7 +163,7 @@ impl EthCoin {

let (state, htlc_params) = try_tx_s!(
self.status_and_htlc_params_from_tx_data(
*etomic_swap_contract,
nft_maker_swap_v2_contract,
&NFT_MAKER_SWAP_V2,
&decoded,
bytes_index,
Expand All @@ -162,7 +175,7 @@ impl EthCoin {
let data = try_tx_s!(self.prepare_spend_nft_maker_v2_data(&args, decoded, htlc_params, state));
self.sign_and_send_transaction(
ZERO_VALUE.into(),
Action::Call(*etomic_swap_contract),
Action::Call(nft_maker_swap_v2_contract),
data,
U256::from(self.gas_limit.eth_max_trade_gas), // TODO: fix to a more accurate const or estimated value
)
Expand All @@ -181,15 +194,21 @@ impl EthCoin {
) -> Result<SignedEthTx, TransactionErr> {
match self.coin_type {
EthCoinType::Nft { .. } => {
let etomic_swap_contract = args.swap_contract_address;
let nft_maker_swap_v2_contract = self
.swap_v2_contracts
.as_ref()
.map(|contracts| contracts.nft_maker_swap_v2_contract)
.ok_or_else(|| {
TransactionErr::Plain(ERRL!("Expected swap_v2_contracts to be Some, but found None"))
})?;
let (decoded, bytes_index) = try_tx_s!(get_decoded_tx_data_and_bytes_index(
args.contract_type,
args.maker_payment_tx.unsigned().data()
));

let (state, htlc_params) = try_tx_s!(
self.status_and_htlc_params_from_tx_data(
*etomic_swap_contract,
nft_maker_swap_v2_contract,
&NFT_MAKER_SWAP_V2,
&decoded,
bytes_index,
Expand All @@ -202,7 +221,7 @@ impl EthCoin {
try_tx_s!(self.prepare_refund_nft_maker_payment_v2_timelock(&args, decoded, htlc_params, state));
self.sign_and_send_transaction(
ZERO_VALUE.into(),
Action::Call(*etomic_swap_contract),
Action::Call(nft_maker_swap_v2_contract),
data,
U256::from(self.gas_limit.eth_max_trade_gas), // TODO: fix to a more accurate const or estimated value
)
Expand All @@ -221,15 +240,21 @@ impl EthCoin {
) -> Result<SignedEthTx, TransactionErr> {
match self.coin_type {
EthCoinType::Nft { .. } => {
let etomic_swap_contract = args.swap_contract_address;
let nft_maker_swap_v2_contract = self
.swap_v2_contracts
.as_ref()
.map(|contracts| contracts.nft_maker_swap_v2_contract)
.ok_or_else(|| {
TransactionErr::Plain(ERRL!("Expected swap_v2_contracts to be Some, but found None"))
})?;
let (decoded, bytes_index) = try_tx_s!(get_decoded_tx_data_and_bytes_index(
args.contract_type,
args.maker_payment_tx.unsigned().data()
));

let (state, htlc_params) = try_tx_s!(
self.status_and_htlc_params_from_tx_data(
*etomic_swap_contract,
nft_maker_swap_v2_contract,
&NFT_MAKER_SWAP_V2,
&decoded,
bytes_index,
Expand All @@ -243,7 +268,7 @@ impl EthCoin {
try_tx_s!(self.prepare_refund_nft_maker_payment_v2_secret(&args, decoded, htlc_params, state));
self.sign_and_send_transaction(
ZERO_VALUE.into(),
Action::Call(*etomic_swap_contract),
Action::Call(nft_maker_swap_v2_contract),
data,
U256::from(self.gas_limit.eth_max_trade_gas), // TODO: fix to a more accurate const or estimated value
)
Expand All @@ -261,14 +286,21 @@ impl EthCoin {
args: &SendNftMakerPaymentArgs<'_, Self>,
htlc_data: Vec<u8>,
) -> Result<Vec<u8>, PrepareTxDataError> {
let nft_maker_swap_v2_contract = self
.swap_v2_contracts
.as_ref()
.map(|contracts| contracts.nft_maker_swap_v2_contract)
.ok_or_else(|| {
PrepareTxDataError::Internal("Expected swap_v2_contracts to be Some, but found None".to_string())
})?;
match args.nft_swap_info.contract_type {
ContractType::Erc1155 => {
let function = ERC1155_CONTRACT.function("safeTransferFrom")?;
let amount_u256 = U256::from_dec_str(&args.amount.to_string())
.map_err(|e| PrepareTxDataError::Internal(e.to_string()))?;
let data = function.encode_input(&[
Token::Address(self.my_addr().await),
Token::Address(*args.nft_swap_info.swap_contract_address),
Token::Address(nft_maker_swap_v2_contract),
Token::Uint(U256::from(args.nft_swap_info.token_id)),
Token::Uint(amount_u256),
Token::Bytes(htlc_data),
Expand All @@ -279,7 +311,7 @@ impl EthCoin {
let function = erc721_transfer_with_data()?;
let data = function.encode_input(&[
Token::Address(self.my_addr().await),
Token::Address(*args.nft_swap_info.swap_contract_address),
Token::Address(nft_maker_swap_v2_contract),
Token::Uint(U256::from(args.nft_swap_info.token_id)),
Token::Bytes(htlc_data),
])?;
Expand Down Expand Up @@ -438,7 +470,11 @@ impl EthCoin {
fn validate_decoded_data(decoded: &[Token], params: &ValidationParams) -> Result<(), MmError<ValidatePaymentError>> {
let checks = vec![
(0, Token::Address(params.maker_address), "maker_address"),
(1, Token::Address(params.etomic_swap_contract), "etomic_swap_contract"),
(
1,
Token::Address(params.nft_maker_swap_v2_contract),
"nft_maker_swap_v2_contract",
),
(2, Token::Uint(U256::from(params.token_id)), "token_id"),
];

Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/eth/nft_swap_v2/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub(crate) struct ExpectedHtlcParams {

pub(crate) struct ValidationParams<'a> {
pub(crate) maker_address: Address,
pub(crate) etomic_swap_contract: Address,
pub(crate) nft_maker_swap_v2_contract: Address,
pub(crate) token_id: &'a [u8],
// Optional, as it's not needed for ERC721
pub(crate) amount: Option<String>,
Expand Down
6 changes: 0 additions & 6 deletions mm2src/coins/lp_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1650,8 +1650,6 @@ pub struct NftSwapInfo<'a, Coin: ParseNftAssocTypes + ?Sized> {
pub token_id: &'a [u8],
/// The type of smart contract that governs this NFT
pub contract_type: &'a Coin::ContractType,
/// Etomic swap contract address
pub swap_contract_address: &'a Coin::ContractAddress,
}

pub struct SendNftMakerPaymentArgs<'a, Coin: ParseCoinAssocTypes + ParseNftAssocTypes + ?Sized> {
Expand Down Expand Up @@ -1742,8 +1740,6 @@ pub struct RefundNftMakerPaymentArgs<'a, Coin: ParseCoinAssocTypes + ParseNftAss
pub swap_unique_data: &'a [u8],
/// The type of smart contract that governs this NFT
pub contract_type: &'a Coin::ContractType,
/// Etomic swap contract address
pub swap_contract_address: &'a Coin::ContractAddress,
}

pub struct SpendMakerPaymentArgs<'a, Coin: ParseCoinAssocTypes + ?Sized> {
Expand Down Expand Up @@ -1779,8 +1775,6 @@ pub struct SpendNftMakerPaymentArgs<'a, Coin: ParseCoinAssocTypes + ParseNftAsso
pub swap_unique_data: &'a [u8],
/// The type of smart contract that governs this NFT
pub contract_type: &'a Coin::ContractType,
/// Etomic swap contract address
pub swap_contract_address: &'a Coin::ContractAddress,
}

/// Operations specific to maker coin in [Trading Protocol Upgrade implementation](https://github.com/KomodoPlatform/komodo-defi-framework/issues/1895)
Expand Down
8 changes: 1 addition & 7 deletions mm2src/mm2_main/tests/docker_tests/eth_docker_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1198,9 +1198,8 @@ pub struct TestNftSwapInfo<Coin: ParseNftAssocTypes + ?Sized> {
pub token_id: Vec<u8>,
/// The type of smart contract that governs this NFT
pub contract_type: Coin::ContractType,
/// Etomic swap contract address
pub swap_contract_address: Coin::ContractAddress,
}

struct NftActivationV2Args {
swap_contract_address: Address,
fallback_swap_contract_address: Address,
Expand Down Expand Up @@ -1268,7 +1267,6 @@ fn setup_test(
token_address: token_contract,
token_id,
contract_type,
swap_contract_address: activation.swap_v2_contracts.nft_maker_swap_v2_contract,
};

NftTestSetup {
Expand All @@ -1288,7 +1286,6 @@ fn send_nft_maker_payment(setup: &NftTestSetup, amount: BigDecimal) -> SignedEth
token_address: &setup.nft_swap_info.token_address,
token_id: &setup.nft_swap_info.token_id,
contract_type: &setup.nft_swap_info.contract_type,
swap_contract_address: &setup.nft_swap_info.swap_contract_address,
};
let send_payment_args = SendNftMakerPaymentArgs::<EthCoin> {
time_lock: setup.time_lock,
Expand Down Expand Up @@ -1318,7 +1315,6 @@ fn validate_nft_maker_payment(setup: &NftTestSetup, maker_payment: &SignedEthTx,
token_address: &setup.nft_swap_info.token_address,
token_id: &setup.nft_swap_info.token_id,
contract_type: &setup.nft_swap_info.contract_type,
swap_contract_address: &setup.nft_swap_info.swap_contract_address,
};
let validate_args = ValidateNftMakerPaymentArgs {
maker_payment_tx: maker_payment,
Expand Down Expand Up @@ -1347,7 +1343,6 @@ fn spend_nft_maker_payment(
maker_pub: &setup.maker_global_nft.derive_htlc_pubkey_v2(&[]),
swap_unique_data: &[],
contract_type,
swap_contract_address: &setup.nft_swap_info.swap_contract_address,
};
block_on(setup.taker_global_nft.spend_nft_maker_payment_v2(spend_payment_args)).unwrap()
}
Expand All @@ -1365,7 +1360,6 @@ fn refund_nft_maker_payment(
taker_secret: &setup.taker_secret,
swap_unique_data: &[],
contract_type,
swap_contract_address: &setup.nft_swap_info.swap_contract_address,
};
match refund_type {
RefundType::Timelock => {
Expand Down

0 comments on commit 60f11f8

Please sign in to comment.