Skip to content

Commit

Permalink
add sepolia test features
Browse files Browse the repository at this point in the history
  • Loading branch information
laruh committed Sep 16, 2024
1 parent 6961d50 commit f2aee82
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
2 changes: 2 additions & 0 deletions mm2src/mm2_main/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ default = []
trezor-udp = ["crypto/trezor-udp"] # use for tests to connect to trezor emulator over udp
run-device-tests = []
enable-sia = ["coins/enable-sia", "coins_activation/enable-sia"]
sepolia-maker-swap-v2-tests = []
sepolia-taker-swap-v2-tests = []

[dependencies]
async-std = { version = "1.5", features = ["unstable"] }
Expand Down
21 changes: 15 additions & 6 deletions mm2src/mm2_main/tests/docker_tests/docker_tests_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ lazy_static! {
/// This approach addresses the `replacement transaction` issue, which occurs when different transactions share the same nonce.
pub static ref MM_CTX1: MmArc = MmCtxBuilder::new().with_conf(json!({"use_trading_proto_v2": true})).into_mm_arc();
pub static ref GETH_WEB3: Web3<Http> = Web3::new(Http::new(GETH_RPC_URL).unwrap());
#[cfg(any(feature = "sepolia-maker-swap-v2-tests", feature = "sepolia-taker-swap-v2-tests"))]
pub static ref SEPOLIA_WEB3: Web3<Http> = Web3::new(Http::new(SEPOLIA_RPC_URL).unwrap());
// Mutex used to prevent nonce re-usage during funding addresses used in tests
pub static ref GETH_NONCE_LOCK: Mutex<()> = Mutex::new(());
Expand All @@ -79,14 +80,17 @@ pub static mut QTUM_CONF_PATH: Option<PathBuf> = None;
pub static mut GETH_ACCOUNT: H160Eth = H160Eth::zero();
/// ERC20 token address on Geth dev node
pub static mut GETH_ERC20_CONTRACT: H160Eth = H160Eth::zero();
#[cfg(any(feature = "sepolia-maker-swap-v2-tests", feature = "sepolia-taker-swap-v2-tests"))]
pub static mut SEPOLIA_ERC20_CONTRACT: H160Eth = H160Eth::zero();
/// Swap contract address on Geth dev node
pub static mut GETH_SWAP_CONTRACT: H160Eth = H160Eth::zero();
/// Maker Swap V2 contract address on Geth dev node
pub static mut GETH_MAKER_SWAP_V2: H160Eth = H160Eth::zero();
/// Taker Swap V2 contract address on Geth dev node
pub static mut GETH_TAKER_SWAP_V2: H160Eth = H160Eth::zero();
#[cfg(any(feature = "sepolia-maker-swap-v2-tests", feature = "sepolia-taker-swap-v2-tests"))]
pub static mut SEPOLIA_TAKER_SWAP_V2: H160Eth = H160Eth::zero();
#[cfg(any(feature = "sepolia-maker-swap-v2-tests", feature = "sepolia-taker-swap-v2-tests"))]
pub static mut SEPOLIA_MAKER_SWAP_V2: H160Eth = H160Eth::zero();
/// Swap contract (with watchers support) address on Geth dev node
pub static mut GETH_WATCHERS_SWAP_CONTRACT: H160Eth = H160Eth::zero();
Expand All @@ -96,9 +100,11 @@ pub static mut GETH_ERC721_CONTRACT: H160Eth = H160Eth::zero();
pub static mut GETH_ERC1155_CONTRACT: H160Eth = H160Eth::zero();
/// NFT Maker Swap V2 contract address on Geth dev node
pub static mut GETH_NFT_MAKER_SWAP_V2: H160Eth = H160Eth::zero();
#[cfg(any(feature = "sepolia-maker-swap-v2-tests", feature = "sepolia-taker-swap-v2-tests"))]
/// NFT Maker Swap V2 contract address on Sepolia testnet
pub static mut SEPOLIA_ETOMIC_MAKER_NFT_SWAP_V2: H160Eth = H160Eth::zero();
pub static GETH_RPC_URL: &str = "http://127.0.0.1:8545";
#[cfg(any(feature = "sepolia-maker-swap-v2-tests", feature = "sepolia-taker-swap-v2-tests"))]
pub static SEPOLIA_RPC_URL: &str = "https://ethereum-sepolia-rpc.publicnode.com";

pub const UTXO_ASSET_DOCKER_IMAGE: &str = "docker.io/artempikulin/testblockchain";
Expand Down Expand Up @@ -1583,12 +1589,15 @@ pub fn init_geth_node() {
thread::sleep(Duration::from_millis(100));
}

SEPOLIA_ETOMIC_MAKER_NFT_SWAP_V2 = EthAddress::from_str("0x9eb88cd58605d8fb9b14652d6152727f7e95fb4d").unwrap();
SEPOLIA_ERC20_CONTRACT = EthAddress::from_str("0xF7b5F8E8555EF7A743f24D3E974E23A3C6cB6638").unwrap();
SEPOLIA_TAKER_SWAP_V2 = EthAddress::from_str("0x7Cc9F2c1c3B797D09B9d1CCd7FDcD2539a4b3874").unwrap();
// deploy tx https://sepolia.etherscan.io/tx/0x6f743d79ecb806f5899a6a801083e33eba9e6f10726af0873af9f39883db7f11
SEPOLIA_MAKER_SWAP_V2 = EthAddress::from_str("0xf9000589c66Df3573645B59c10aa87594Edc318F").unwrap();

#[cfg(any(feature = "sepolia-maker-swap-v2-tests", feature = "sepolia-taker-swap-v2-tests"))]
{
SEPOLIA_ETOMIC_MAKER_NFT_SWAP_V2 =
EthAddress::from_str("0x9eb88cd58605d8fb9b14652d6152727f7e95fb4d").unwrap();
SEPOLIA_ERC20_CONTRACT = EthAddress::from_str("0xF7b5F8E8555EF7A743f24D3E974E23A3C6cB6638").unwrap();
SEPOLIA_TAKER_SWAP_V2 = EthAddress::from_str("0x7Cc9F2c1c3B797D09B9d1CCd7FDcD2539a4b3874").unwrap();
// deploy tx https://sepolia.etherscan.io/tx/0x6f743d79ecb806f5899a6a801083e33eba9e6f10726af0873af9f39883db7f11
SEPOLIA_MAKER_SWAP_V2 = EthAddress::from_str("0xf9000589c66Df3573645B59c10aa87594Edc318F").unwrap();
}
let alice_passphrase = get_passphrase!(".env.client", "ALICE_PASSPHRASE").unwrap();
let alice_keypair = key_pair_from_seed(&alice_passphrase).unwrap();
let alice_eth_addr = addr_from_raw_pubkey(alice_keypair.public()).unwrap();
Expand Down
41 changes: 30 additions & 11 deletions mm2src/mm2_main/tests/docker_tests/eth_docker_tests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use super::docker_tests_common::{random_secp256k1_secret, ERC1155_TEST_ABI, ERC721_TEST_ABI, GETH_ACCOUNT,
GETH_ERC1155_CONTRACT, GETH_ERC20_CONTRACT, GETH_ERC721_CONTRACT, GETH_MAKER_SWAP_V2,
GETH_NFT_MAKER_SWAP_V2, GETH_NONCE_LOCK, GETH_RPC_URL, GETH_SWAP_CONTRACT,
GETH_TAKER_SWAP_V2, GETH_WATCHERS_SWAP_CONTRACT, GETH_WEB3, MM_CTX, MM_CTX1,
SEPOLIA_ERC20_CONTRACT, SEPOLIA_ETOMIC_MAKER_NFT_SWAP_V2, SEPOLIA_MAKER_SWAP_V2,
GETH_TAKER_SWAP_V2, GETH_WATCHERS_SWAP_CONTRACT, GETH_WEB3, MM_CTX, MM_CTX1};
#[cfg(any(feature = "sepolia-maker-swap-v2-tests", feature = "sepolia-taker-swap-v2-tests"))]
use super::docker_tests_common::{SEPOLIA_ERC20_CONTRACT, SEPOLIA_ETOMIC_MAKER_NFT_SWAP_V2, SEPOLIA_MAKER_SWAP_V2,
SEPOLIA_NONCE_LOCK, SEPOLIA_RPC_URL, SEPOLIA_TAKER_SWAP_V2, SEPOLIA_WEB3};
use crate::common::Future01CompatExt;
use bitcrypto::{dhash160, sha256};
Expand All @@ -27,7 +28,9 @@ use ethereum_types::U256;
use futures01::Future;
use mm2_core::mm_ctx::MmArc;
use mm2_number::{BigDecimal, BigUint};
use mm2_test_helpers::for_tests::{erc20_dev_conf, eth_dev_conf, eth_sepolia_conf, nft_dev_conf, sepolia_erc20_dev_conf};
use mm2_test_helpers::for_tests::{erc20_dev_conf, eth_dev_conf, nft_dev_conf};
#[cfg(any(feature = "sepolia-maker-swap-v2-tests", feature = "sepolia-taker-swap-v2-tests"))]
use mm2_test_helpers::for_tests::{eth_sepolia_conf, sepolia_erc20_dev_conf};
use serde_json::Value as Json;
use std::str::FromStr;
use std::thread;
Expand All @@ -36,7 +39,9 @@ use web3::contract::{Contract, Options};
use web3::ethabi::Token;
use web3::types::{Address, BlockNumber, TransactionRequest, H256};

#[cfg(any(feature = "sepolia-maker-swap-v2-tests", feature = "sepolia-taker-swap-v2-tests"))]
const SEPOLIA_MAKER_PRIV: &str = "6e2f3a6223b928a05a3a3622b0c3f3573d03663b704a61a6eb73326de0487928";
#[cfg(any(feature = "sepolia-maker-swap-v2-tests", feature = "sepolia-taker-swap-v2-tests"))]
const SEPOLIA_TAKER_PRIV: &str = "e0be82dca60ff7e4c6d6db339ac9e1ae249af081dba2110bddd281e711608f16";
const NFT_ETH: &str = "NFT_ETH";
const ETH: &str = "ETH";
Expand All @@ -58,7 +63,9 @@ pub fn maker_swap_v2() -> Address { unsafe { GETH_MAKER_SWAP_V2 } }
///
/// GETH_TAKER_SWAP_V2 is set once during initialization before tests start
pub fn taker_swap_v2() -> Address { unsafe { GETH_TAKER_SWAP_V2 } }
#[cfg(any(feature = "sepolia-maker-swap-v2-tests", feature = "sepolia-taker-swap-v2-tests"))]
pub fn sepolia_taker_swap_v2() -> Address { unsafe { SEPOLIA_TAKER_SWAP_V2 } }
#[cfg(any(feature = "sepolia-maker-swap-v2-tests", feature = "sepolia-taker-swap-v2-tests"))]
pub fn sepolia_maker_swap_v2() -> Address { unsafe { SEPOLIA_MAKER_SWAP_V2 } }
/// # Safety
///
Expand All @@ -72,9 +79,11 @@ pub fn watchers_swap_contract() -> Address { unsafe { GETH_WATCHERS_SWAP_CONTRAC
///
/// GETH_ERC20_CONTRACT is set once during initialization before tests start
pub fn erc20_contract() -> Address { unsafe { GETH_ERC20_CONTRACT } }
#[cfg(any(feature = "sepolia-maker-swap-v2-tests", feature = "sepolia-taker-swap-v2-tests"))]
pub fn sepolia_erc20_contract() -> Address { unsafe { SEPOLIA_ERC20_CONTRACT } }
/// Return ERC20 dev token contract address in checksum format
pub fn erc20_contract_checksum() -> String { checksum_address(&format!("{:02x}", erc20_contract())) }
#[cfg(any(feature = "sepolia-maker-swap-v2-tests", feature = "sepolia-taker-swap-v2-tests"))]
pub fn sepolia_erc20_contract_checksum() -> String { checksum_address(&format!("{:02x}", sepolia_erc20_contract())) }
/// # Safety
///
Expand All @@ -84,6 +93,7 @@ pub fn geth_erc721_contract() -> Address { unsafe { GETH_ERC721_CONTRACT } }
///
/// GETH_ERC1155_CONTRACT is set once during initialization before tests start
pub fn geth_erc1155_contract() -> Address { unsafe { GETH_ERC1155_CONTRACT } }
#[cfg(any(feature = "sepolia-maker-swap-v2-tests", feature = "sepolia-taker-swap-v2-tests"))]
/// # Safety
///
/// SEPOLIA_ETOMIC_MAKER_NFT_SWAP_V2 address is set once during initialization before tests start
Expand Down Expand Up @@ -401,6 +411,7 @@ fn global_nft_with_random_privkey(
global_nft
}

#[cfg(any(feature = "sepolia-maker-swap-v2-tests", feature = "sepolia-taker-swap-v2-tests"))]
/// Can be used to generate coin from Sepolia Maker/Taker priv keys.
fn sepolia_coin_from_privkey(ctx: &MmArc, secret: &'static str, ticker: &str, conf: &Json, erc20: bool) -> EthCoin {
let swap_addr = SwapAddresses {
Expand Down Expand Up @@ -461,6 +472,7 @@ fn sepolia_coin_from_privkey(ctx: &MmArc, secret: &'static str, ticker: &str, co
coin
}

#[cfg(any(feature = "sepolia-maker-swap-v2-tests", feature = "sepolia-taker-swap-v2-tests"))]
fn get_or_create_sepolia_coin(ctx: &MmArc, priv_key: &'static str, ticker: &str, conf: &Json, erc20: bool) -> EthCoin {
match block_on(lp_coinfind(ctx, ticker)).unwrap() {
None => sepolia_coin_from_privkey(ctx, priv_key, ticker, conf, erc20),
Expand Down Expand Up @@ -861,6 +873,7 @@ fn send_and_spend_erc20_maker_payment_priority_fee() {
send_and_spend_erc20_maker_payment_impl(SwapTxFeePolicy::Medium);
}

#[cfg(any(feature = "sepolia-maker-swap-v2-tests", feature = "sepolia-taker-swap-v2-tests"))]
/// Wait for all pending transactions for the given address to be confirmed
fn wait_pending_transactions(wallet_address: Address) {
let _guard = SEPOLIA_NONCE_LOCK.lock().unwrap();
Expand Down Expand Up @@ -1446,7 +1459,7 @@ fn eth_coin_v2_activation_with_random_privkey(
coin
}

#[ignore]
#[cfg(feature = "sepolia-taker-swap-v2-tests")]
#[test]
fn send_and_refund_taker_funding_by_secret_eth() {
// sepolia test
Expand Down Expand Up @@ -1507,7 +1520,7 @@ fn send_and_refund_taker_funding_by_secret_eth() {
wait_for_confirmations(&taker_coin, &funding_tx_refund, 100);
}

#[ignore]
#[cfg(feature = "sepolia-taker-swap-v2-tests")]
#[test]
fn send_and_refund_taker_funding_by_secret_erc20() {
thread::sleep(Duration::from_secs(30));
Expand Down Expand Up @@ -1569,7 +1582,7 @@ fn send_and_refund_taker_funding_by_secret_erc20() {
wait_for_confirmations(&taker_coin, &funding_tx_refund, 200);
}

#[ignore]
#[cfg(feature = "sepolia-taker-swap-v2-tests")]
#[test]
fn send_and_refund_taker_funding_exceed_pre_approve_timelock_eth() {
// sepolia test
Expand Down Expand Up @@ -1634,7 +1647,7 @@ fn send_and_refund_taker_funding_exceed_pre_approve_timelock_eth() {
wait_for_confirmations(&taker_coin, &funding_tx_refund, 100);
}

#[ignore]
#[cfg(feature = "sepolia-taker-swap-v2-tests")]
#[test]
fn taker_send_approve_and_spend_eth() {
// sepolia test
Expand Down Expand Up @@ -1744,7 +1757,7 @@ fn taker_send_approve_and_spend_eth() {
block_on(taker_coin.wait_for_taker_payment_spend(&spend_tx, 0u64, payment_time_lock)).unwrap();
}

#[ignore]
#[cfg(feature = "sepolia-taker-swap-v2-tests")]
#[test]
fn taker_send_approve_and_spend_erc20() {
// sepolia test
Expand Down Expand Up @@ -1854,7 +1867,7 @@ fn taker_send_approve_and_spend_erc20() {
block_on(taker_coin.wait_for_taker_payment_spend(&spend_tx, 0u64, payment_time_lock)).unwrap();
}

#[ignore]
#[cfg(feature = "sepolia-taker-swap-v2-tests")]
#[test]
fn send_and_refund_taker_funding_exceed_payment_timelock_eth() {
// sepolia test
Expand Down Expand Up @@ -1938,7 +1951,7 @@ fn send_and_refund_taker_funding_exceed_payment_timelock_eth() {
wait_for_confirmations(&taker_coin, &funding_tx_refund, 100);
}

#[ignore]
#[cfg(feature = "sepolia-taker-swap-v2-tests")]
#[test]
fn send_and_refund_taker_funding_exceed_payment_timelock_erc20() {
// sepolia test
Expand Down Expand Up @@ -2024,7 +2037,7 @@ fn send_and_refund_taker_funding_exceed_payment_timelock_erc20() {
wait_for_confirmations(&taker_coin, &funding_tx_refund, 100);
}

#[ignore]
#[cfg(feature = "sepolia-taker-swap-v2-tests")]
#[test]
fn send_and_refund_taker_funding_exceed_pre_approve_timelock_erc20() {
// sepolia test
Expand Down Expand Up @@ -2091,6 +2104,7 @@ fn send_and_refund_taker_funding_exceed_pre_approve_timelock_erc20() {
wait_for_confirmations(&taker_coin, &funding_tx_refund, 150);
}

#[cfg(feature = "sepolia-maker-swap-v2-tests")]
#[test]
fn send_maker_payment_and_refund_timelock_eth() {
thread::sleep(Duration::from_secs(150));
Expand Down Expand Up @@ -2143,6 +2157,7 @@ fn send_maker_payment_and_refund_timelock_eth() {
wait_for_confirmations(&maker_coin, &payment_tx_refund, 100);
}

#[cfg(feature = "sepolia-maker-swap-v2-tests")]
#[test]
fn send_maker_payment_and_refund_timelock_erc20() {
thread::sleep(Duration::from_secs(120));
Expand Down Expand Up @@ -2196,6 +2211,7 @@ fn send_maker_payment_and_refund_timelock_erc20() {
wait_for_confirmations(&maker_coin, &payment_tx_refund, 100);
}

#[cfg(feature = "sepolia-maker-swap-v2-tests")]
#[test]
fn send_maker_payment_and_refund_secret_eth() {
thread::sleep(Duration::from_secs(90));
Expand Down Expand Up @@ -2245,6 +2261,7 @@ fn send_maker_payment_and_refund_secret_eth() {
wait_for_confirmations(&maker_coin, &payment_tx_refund, 100);
}

#[cfg(feature = "sepolia-maker-swap-v2-tests")]
#[test]
fn send_maker_payment_and_refund_secret_erc20() {
thread::sleep(Duration::from_secs(60));
Expand Down Expand Up @@ -2295,6 +2312,7 @@ fn send_maker_payment_and_refund_secret_erc20() {
wait_for_confirmations(&maker_coin, &payment_tx_refund, 100);
}

#[cfg(feature = "sepolia-maker-swap-v2-tests")]
#[test]
fn send_and_spend_maker_payment_eth() {
let taker_coin = get_or_create_sepolia_coin(&MM_CTX1, SEPOLIA_TAKER_PRIV, ETH, &eth_sepolia_conf(), false);
Expand Down Expand Up @@ -2356,6 +2374,7 @@ fn send_and_spend_maker_payment_eth() {
block_on(maker_coin.wait_for_maker_payment_spend(&spend_tx, 0u64, payment_time_lock)).unwrap();
}

#[cfg(feature = "sepolia-maker-swap-v2-tests")]
#[test]
fn send_and_spend_maker_payment_erc20() {
thread::sleep(Duration::from_secs(30));
Expand Down

0 comments on commit f2aee82

Please sign in to comment.