Skip to content

Commit dfabd6d

Browse files
authored
Merge pull request #100 from Ximik/unconfirmed-space
Pending spaces list for unconfirmed
2 parents faf6e20 + 697fbd8 commit dfabd6d

File tree

4 files changed

+56
-20
lines changed

4 files changed

+56
-20
lines changed

client/src/format.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ use colored::{Color, Colorize};
33
use jsonrpsee::core::Serialize;
44
use serde::Deserialize;
55
use spaces_protocol::{
6-
bitcoin::{Amount, Network, OutPoint},
7-
Covenant,
6+
bitcoin::{Amount, Network, OutPoint}, Covenant
87
};
98
use spaces_wallet::{
109
address::SpaceAddress,
@@ -37,6 +36,12 @@ struct FormatRpcError {
3736
message: String,
3837
}
3938

39+
#[derive(Tabled)]
40+
#[tabled(rename_all = "UPPERCASE")]
41+
struct PendingSpaces {
42+
space: String,
43+
}
44+
4045
#[derive(Tabled)]
4146
#[tabled(rename_all = "UPPERCASE")]
4247
struct WinningSpaces {
@@ -272,9 +277,13 @@ pub fn print_list_spaces_response(
272277
) {
273278
match format {
274279
Format::Text => {
280+
let mut pendings = Vec::new();
275281
let mut outbids = Vec::new();
276282
let mut winnings = Vec::new();
277283
let mut owned = Vec::new();
284+
for slabel in response.pending {
285+
pendings.push(PendingSpaces { space: slabel.to_string() });
286+
}
278287
for res in response.outbid {
279288
let space = res.spaceout.space.as_ref().expect("space");
280289
let mut outbid = OutbidSpaces {
@@ -342,6 +351,12 @@ pub fn print_list_spaces_response(
342351
owned.push(registered);
343352
}
344353

354+
if !pendings.is_empty() {
355+
println!("⏳ PENDING ({} spaces): ", pendings.len().to_string().bold());
356+
let table = ascii_table(pendings);
357+
println!("{}", table);
358+
}
359+
345360
if !outbids.is_empty() {
346361
println!("⚠️ OUTBID ({} spaces): ", outbids.len().to_string().bold());
347362
let table = ascii_table(outbids);

client/src/wallets.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ pub struct WalletInfoWithProgress {
101101

102102
#[derive(Debug, Clone, Serialize, Deserialize)]
103103
pub struct ListSpacesResponse {
104+
pub pending: Vec<SLabel>,
104105
pub winning: Vec<FullSpaceOut>,
105106
pub outbid: Vec<FullSpaceOut>,
106107
pub owned: Vec<FullSpaceOut>,
@@ -816,12 +817,15 @@ impl RpcWallet {
816817
let unspent = wallet.list_unspent_with_details(state)?;
817818
let recent_events = wallet.list_recent_events()?;
818819

819-
let mut res = ListSpacesResponse {
820-
winning: vec![],
821-
outbid: vec![],
822-
owned: vec![],
823-
};
820+
let mut pending = vec![];
821+
let mut outbid = vec![];
824822
for (txid, event) in recent_events {
823+
let tx = wallet.get_tx(txid);
824+
if tx.as_ref().is_some_and(|tx| !tx.chain_position.is_confirmed()) {
825+
pending.push(SLabel::from_str(event.space.as_ref().unwrap()).expect("valid space name"));
826+
continue;
827+
}
828+
825829
if unspent.iter().any(|out| {
826830
out.space
827831
.as_ref()
@@ -833,9 +837,12 @@ impl RpcWallet {
833837
let spacehash = SpaceKey::from(Sha256::hash(name.as_ref()));
834838
let space = state.get_space_info(&spacehash)?;
835839
if let Some(space) = space {
836-
let tx = wallet.get_tx(txid);
840+
if space.spaceout.space.as_ref().unwrap().is_owned() {
841+
continue;
842+
}
843+
837844
if tx.is_none() {
838-
res.outbid.push(space);
845+
outbid.push(space);
839846
continue;
840847
}
841848

@@ -845,10 +852,13 @@ impl RpcWallet {
845852
{
846853
continue;
847854
}
848-
res.outbid.push(space);
855+
856+
outbid.push(space);
849857
}
850858
}
851859

860+
let mut owned = vec![];
861+
let mut winning = vec![];
852862
for wallet_output in unspent.into_iter().filter(|output| output.space.is_some()) {
853863
let entry = FullSpaceOut {
854864
txid: wallet_output.output.outpoint.txid,
@@ -860,15 +870,19 @@ impl RpcWallet {
860870
},
861871
};
862872

863-
let space = entry.spaceout.space.as_ref().expect("space");
864-
if matches!(space.covenant, spaces_protocol::Covenant::Bid { .. }) {
865-
res.winning.push(entry);
866-
} else if matches!(space.covenant, spaces_protocol::Covenant::Transfer { .. }) {
867-
res.owned.push(entry);
873+
if entry.spaceout.space.as_ref().expect("space").is_owned() {
874+
owned.push(entry);
875+
} else {
876+
winning.push(entry);
868877
}
869878
}
870879

871-
Ok(res)
880+
Ok(ListSpacesResponse {
881+
pending,
882+
winning,
883+
outbid,
884+
owned,
885+
})
872886
}
873887

874888
fn list_transactions(

client/tests/integration_tests.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,13 @@ async fn it_should_open_a_space_for_auction(rig: &TestRig) -> anyhow::Result<()>
4444
assert!(tx_res.error.is_none(), "expect no errors for simple open");
4545
}
4646
assert_eq!(response.result.len(), 2, "must be 2 transactions");
47+
let alices_spaces = rig.spaced.client.wallet_list_spaces(ALICE).await?;
48+
assert!(alices_spaces.pending.first().is_some_and(|s| s.to_string() == TEST_SPACE), "must be a pending space");
4749

4850
rig.mine_blocks(1, None).await?;
4951
rig.wait_until_synced().await?;
52+
let alices_spaces = rig.spaced.client.wallet_list_spaces(ALICE).await?;
53+
assert!(alices_spaces.pending.is_empty(), "must have no pending spaces");
5054

5155
let fullspaceout = rig.spaced.client.get_space(TEST_SPACE).await?;
5256
let fullspaceout = fullspaceout.expect("a fullspace out");
@@ -96,8 +100,11 @@ async fn it_should_allow_outbidding(rig: &TestRig) -> anyhow::Result<()> {
96100
.expect("send request");
97101

98102
println!("{}", serde_json::to_string_pretty(&result).unwrap());
99-
rig.mine_blocks(1, None).await?;
100103

104+
let bob_spaces_updated = rig.spaced.client.wallet_list_spaces(BOB).await?;
105+
assert!(bob_spaces_updated.pending.first().is_some_and(|s| s.to_string() == TEST_SPACE), "must be a pending space");
106+
107+
rig.mine_blocks(1, None).await?;
101108
rig.wait_until_synced().await?;
102109
rig.wait_until_wallet_synced(BOB).await?;
103110
rig.wait_until_wallet_synced(ALICE).await?;
@@ -125,6 +132,7 @@ async fn it_should_allow_outbidding(rig: &TestRig) -> anyhow::Result<()> {
125132
alices_balance.balance + Amount::from_sat(TEST_INITIAL_BID + 662),
126133
"alice must be refunded this exact amount"
127134
);
135+
assert!(bob_spaces_updated.pending.is_empty(), "must have no pending spaces");
128136

129137
let fullspaceout = rig.spaced.client.get_space(TEST_SPACE).await?;
130138
let fullspaceout = fullspaceout.expect("a fullspace out");

wallet/src/tx_event.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ impl TxEvent {
206206
Ok(None)
207207
}
208208

209-
/// Retrieve all spaces the wallet has bid on in the last 2 weeks
209+
/// Retrieve all spaces the wallet has done any operation with
210210
pub fn get_latest_events(
211211
db_tx: &rusqlite::Transaction,
212212
) -> rusqlite::Result<Vec<(Txid, TxEvent)>> {
@@ -216,8 +216,7 @@ impl TxEvent {
216216
WHERE id IN (
217217
SELECT MAX(id)
218218
FROM {table}
219-
WHERE type IN ('bid', 'open')
220-
AND created_at >= strftime('%s', 'now', '-14 days')
219+
WHERE space IS NOT NULL
221220
GROUP BY space
222221
)
223222
ORDER BY id DESC",

0 commit comments

Comments
 (0)