xmr-btc-swap/monero-harness/tests/wallet.rs

69 lines
2.2 KiB
Rust
Raw Normal View History

use monero_harness::{Monero, MoneroWalletRpc};
2021-04-15 04:39:59 -04:00
use monero_rpc::wallet::MoneroWalletRpc as _;
use std::time::Duration;
use testcontainers::clients::Cli;
use tokio::time::sleep;
use tracing_subscriber::util::SubscriberInitExt;
2021-01-10 20:55:19 -05:00
#[tokio::test]
2020-11-01 22:42:08 -05:00
async fn fund_transfer_and_check_tx_key() {
let _guard = tracing_subscriber::fmt()
.with_env_filter("warn,test=debug,monero_harness=debug,monero_rpc=debug")
.set_default();
2021-01-10 20:55:19 -05:00
2020-11-01 18:02:28 -05:00
let fund_alice: u64 = 1_000_000_000_000;
let fund_bob = 0;
let send_to_bob = 5_000_000_000;
let tc = Cli::default();
let (monero, _monerod_container, _wallet_containers) =
Monero::new(&tc, vec!["alice", "bob"]).await.unwrap();
2020-11-02 00:00:35 -05:00
let alice_wallet = monero.wallet("alice").unwrap();
let bob_wallet = monero.wallet("bob").unwrap();
2020-11-01 22:42:08 -05:00
monero.init_miner().await.unwrap();
monero.init_wallet("alice", vec![fund_alice]).await.unwrap();
monero.init_wallet("bob", vec![fund_bob]).await.unwrap();
monero.start_miner().await.unwrap();
2020-11-01 22:42:08 -05:00
// check alice balance
2020-11-02 00:00:35 -05:00
let got_alice_balance = alice_wallet.balance().await.unwrap();
assert_eq!(got_alice_balance, fund_alice);
// transfer from alice to bob
let bob_address = bob_wallet.address().await.unwrap().address;
let transfer = alice_wallet
.transfer(&bob_address, send_to_bob)
.await
.unwrap();
wait_for_wallet_to_catch_up(bob_wallet, send_to_bob).await;
let got_bob_balance = bob_wallet.balance().await.unwrap();
assert_eq!(got_bob_balance, send_to_bob);
2020-11-01 22:42:08 -05:00
// check if tx was actually seen
2020-11-01 22:42:08 -05:00
let tx_id = transfer.tx_hash;
2021-04-15 04:39:59 -04:00
let tx_key = transfer.tx_key.unwrap().to_string();
let res = bob_wallet
2020-11-02 19:49:53 -05:00
.client()
2021-04-15 04:39:59 -04:00
.check_tx_key(tx_id, tx_key, bob_address)
2020-11-01 22:42:08 -05:00
.await
.expect("failed to check tx by key");
assert_eq!(res.received, send_to_bob);
}
async fn wait_for_wallet_to_catch_up(wallet: &MoneroWalletRpc, expected_balance: u64) {
let max_retry = 15;
let mut retry = 0;
loop {
retry += 1;
let balance = wallet.balance().await.unwrap();
if balance == expected_balance || max_retry == retry {
break;
}
sleep(Duration::from_secs(1)).await;
}
}