forked from arkade-os/rust-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e_two_party.rs
More file actions
176 lines (140 loc) · 5.22 KB
/
e2e_two_party.rs
File metadata and controls
176 lines (140 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#![allow(clippy::unwrap_used)]
use crate::common::wait_until_balance;
use bitcoin::key::Secp256k1;
use bitcoin::Amount;
use common::init_tracing;
use common::set_up_client;
use common::Nigiri;
use rand::thread_rng;
use std::str::FromStr;
use std::sync::Arc;
mod common;
#[tokio::test]
#[ignore]
pub async fn e2e() {
init_tracing();
let nigiri = Arc::new(Nigiri::new());
let secp = Secp256k1::new();
let mut rng = thread_rng();
let (alice, _) = set_up_client("alice".to_string(), nigiri.clone(), secp.clone()).await;
let (bob, _) = set_up_client("bob".to_string(), nigiri.clone(), secp).await;
let alice_offchain_balance = alice.offchain_balance().await.unwrap();
let bob_offchain_balance = bob.offchain_balance().await.unwrap();
let alice_boarding_address = alice.get_boarding_address().unwrap();
tracing::info!(
?alice_boarding_address,
?alice_offchain_balance,
?bob_offchain_balance,
"Funding Alice's boarding output"
);
assert_eq!(alice_offchain_balance.total(), Amount::ZERO);
assert_eq!(bob_offchain_balance.total(), Amount::ZERO);
let alice_fund_amount = Amount::ONE_BTC;
let alice_boarding_outpoint = nigiri
.faucet_fund(&alice_boarding_address, alice_fund_amount)
.await;
let alice_offchain_balance = alice.offchain_balance().await.unwrap();
let bob_offchain_balance = bob.offchain_balance().await.unwrap();
tracing::info!(
?alice_boarding_outpoint,
?alice_offchain_balance,
?bob_offchain_balance,
"Funded Alice's boarding output"
);
assert_eq!(alice_offchain_balance.total(), Amount::ZERO);
assert_eq!(bob_offchain_balance.total(), Amount::ZERO);
alice.settle(&mut rng, false).await.unwrap();
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
let alice_offchain_balance = alice.offchain_balance().await.unwrap();
let bob_offchain_balance = bob.offchain_balance().await.unwrap();
tracing::info!(
?alice_offchain_balance,
?bob_offchain_balance,
"Lifted Alice's VTXO"
);
assert_eq!(alice_offchain_balance.confirmed(), alice_fund_amount);
assert_eq!(alice_offchain_balance.pending(), Amount::ZERO);
assert_eq!(bob_offchain_balance.total(), Amount::ZERO);
let send_to_bob_vtxo_amount = Amount::from_sat(100_000);
let (bob_offchain_address, _) = bob.get_offchain_address().unwrap();
tracing::info!(
%send_to_bob_vtxo_amount,
?bob_offchain_address,
?alice_offchain_balance,
?bob_offchain_balance,
"Sending VTXO from Alice to Bob"
);
let virtual_txid = alice
.send_vtxo(bob_offchain_address, send_to_bob_vtxo_amount)
.await
.unwrap();
let alice_offchain_balance = alice.offchain_balance().await.unwrap();
let bob_offchain_balance = bob.offchain_balance().await.unwrap();
tracing::info!(
?alice_offchain_balance,
?bob_offchain_balance,
virtual_txid = %virtual_txid,
"Sent VTXO from Alice to Bob"
);
wait_until_balance(
&alice,
Amount::ZERO,
alice_fund_amount - send_to_bob_vtxo_amount,
)
.await
.unwrap();
wait_until_balance(&bob, Amount::ZERO, send_to_bob_vtxo_amount)
.await
.unwrap();
bob.settle(&mut rng, false).await.unwrap();
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
let alice_offchain_balance = alice.offchain_balance().await.unwrap();
let bob_offchain_balance = bob.offchain_balance().await.unwrap();
tracing::info!(
?alice_offchain_balance,
?bob_offchain_balance,
"Lifted Bob's VTXO"
);
assert_eq!(alice_offchain_balance.confirmed(), Amount::ZERO);
assert_eq!(
alice_offchain_balance.pending(),
alice_fund_amount - send_to_bob_vtxo_amount
);
assert_eq!(bob_offchain_balance.confirmed(), send_to_bob_vtxo_amount);
assert_eq!(bob_offchain_balance.pending(), Amount::ZERO);
alice.settle(&mut rng, false).await.unwrap();
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
let alice_offchain_balance = alice.offchain_balance().await.unwrap();
let bob_offchain_balance = bob.offchain_balance().await.unwrap();
tracing::info!(
?alice_offchain_balance,
?bob_offchain_balance,
"Lifted Alice's change VTXO"
);
assert_eq!(
alice_offchain_balance.confirmed(),
alice_fund_amount - send_to_bob_vtxo_amount
);
assert_eq!(alice_offchain_balance.pending(), Amount::ZERO);
assert_eq!(bob_offchain_balance.confirmed(), send_to_bob_vtxo_amount);
assert_eq!(bob_offchain_balance.pending(), Amount::ZERO);
let address = bitcoin::Address::from_str(
"bcrt1puq2gdfn97qd0ep0m335gc7r7uh0hpyhjhnmy90tklyywkdpcdd9sfag5y0",
)
.unwrap();
let txid = bob
.collaborative_redeem(
&mut rng,
address.assume_checked(),
send_to_bob_vtxo_amount / 2,
false,
)
.await
.unwrap();
let bob_offchain_balance = bob.offchain_balance().await.unwrap();
assert_eq!(
bob_offchain_balance.confirmed(),
send_to_bob_vtxo_amount / 2
);
tracing::info!(?txid, "Collaboratively redeemed from Bob");
}