Skip to content

Commit 361ca77

Browse files
authored
Merge pull request #65 from buffrr/renew
Add Renew command
2 parents 2d6620d + d2f029e commit 361ca77

File tree

6 files changed

+104
-35
lines changed

6 files changed

+104
-35
lines changed

node/src/bin/space-cli.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ enum Commands {
133133
#[arg(long, short)]
134134
fee_rate: Option<u64>,
135135
},
136+
/// Renew ownership of a space
137+
#[command(name = "renew", )]
138+
Renew {
139+
/// Spaces to renew
140+
#[arg(display_order = 0)]
141+
spaces: Vec<String>,
142+
/// Fee rate to use in sat/vB
143+
#[arg(long, short)]
144+
fee_rate: Option<u64>,
145+
},
136146
/// Estimates the minimum bid needed for a rollout within the given target blocks
137147
#[command(name = "estimatebid")]
138148
EstimateBid {
@@ -554,6 +564,19 @@ async fn handle_commands(
554564
)
555565
.await?
556566
}
567+
Commands::Renew { spaces, fee_rate } => {
568+
let spaces: Vec<_> = spaces.into_iter().map(|s| normalize_space(&s)).collect();
569+
cli.send_request(
570+
Some(RpcWalletRequest::Transfer(TransferSpacesParams {
571+
spaces,
572+
to: None,
573+
})),
574+
None,
575+
fee_rate,
576+
false,
577+
)
578+
.await?
579+
}
557580
Commands::Transfer {
558581
spaces,
559582
to,
@@ -563,7 +586,7 @@ async fn handle_commands(
563586
cli.send_request(
564587
Some(RpcWalletRequest::Transfer(TransferSpacesParams {
565588
spaces,
566-
to,
589+
to: Some(to),
567590
})),
568591
None,
569592
fee_rate,
@@ -741,6 +764,7 @@ async fn handle_commands(
741764
}).await?;
742765
println!("{}", serde_json::to_string_pretty(&result).expect("result"));
743766
}
767+
744768
}
745769

746770
Ok(())

node/src/rpc.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -273,16 +273,18 @@ pub enum RpcWalletRequest {
273273
Register(RegisterParams),
274274
#[serde(rename = "execute")]
275275
Execute(ExecuteParams),
276-
#[serde(rename = "sendspaces")]
276+
#[serde(rename = "transfer")]
277277
Transfer(TransferSpacesParams),
278-
#[serde(rename = "sendcoins")]
278+
#[serde(rename = "send")]
279279
SendCoins(SendCoinsParams),
280280
}
281281

282282
#[derive(Clone, Serialize, Deserialize)]
283283
pub struct TransferSpacesParams {
284284
pub spaces: Vec<String>,
285-
pub to: String,
285+
286+
#[serde(skip_serializing_if = "Option::is_none")]
287+
pub to: Option<String>,
286288
}
287289

288290
#[derive(Clone, Serialize, Deserialize)]

node/src/wallets.rs

+30-13
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub enum WalletCommand {
116116
space: String,
117117
msg: protocol::Bytes,
118118
resp: crate::rpc::Responder<anyhow::Result<SignedMessage>>,
119-
}
119+
},
120120
}
121121

122122
#[derive(Debug, Clone, Copy, Serialize, Deserialize, ValueEnum)]
@@ -184,7 +184,7 @@ impl RpcWallet {
184184
let space = fullspaceout.spaceout.space.as_ref().expect("space").name.to_string();
185185
let foreign_input = fullspaceout.outpoint();
186186
let tx = wallet.buy::<Sha256>(state, &listing, fee_rate)?;
187-
187+
188188
if !skip_tx_check {
189189
let tip = wallet.local_chain().tip().height();
190190
let mut checker = TxChecker::new(state);
@@ -205,7 +205,7 @@ impl RpcWallet {
205205

206206
// Incrementing last_seen by 1 ensures eviction of older tx
207207
// in cases with same-second/last seen replacement.
208-
wallet.apply_unconfirmed_tx_record(tx_record, last_seen+1)?;
208+
wallet.apply_unconfirmed_tx_record(tx_record, last_seen + 1)?;
209209
wallet.commit()?;
210210

211211
Ok(TxResponse {
@@ -248,7 +248,7 @@ impl RpcWallet {
248248

249249
// Incrementing last_seen by 1 ensures eviction of older tx
250250
// in cases with same-second/last seen replacement.
251-
wallet.apply_unconfirmed_tx_record(tx_record, last_seen+1)?;
251+
wallet.apply_unconfirmed_tx_record(tx_record, last_seen + 1)?;
252252
wallet.commit()?;
253253

254254
Ok(vec![TxResponse {
@@ -573,7 +573,7 @@ impl RpcWallet {
573573

574574
fn list_spaces(
575575
wallet: &mut SpacesWallet,
576-
state: &mut LiveSnapshot
576+
state: &mut LiveSnapshot,
577577
) -> anyhow::Result<ListSpacesResponse> {
578578
let unspent = wallet.list_unspent_with_details(state)?;
579579
let recent_events = wallet.list_recent_events()?;
@@ -788,14 +788,20 @@ impl RpcWallet {
788788
.filter_map(|space| SLabel::from_str(space).ok())
789789
.collect();
790790
if spaces.len() != params.spaces.len() {
791-
return Err(anyhow!("sendspaces: some names were malformed"));
791+
return Err(anyhow!("transfer: some names were malformed"));
792792
}
793-
let recipient = match Self::resolve(network, store, &params.to, true)? {
794-
None => {
795-
return Err(anyhow!("sendspaces: could not resolve '{}'", params.to))
793+
794+
let recipient = if let Some(to) = params.to {
795+
match Self::resolve(network, store, &to, true)? {
796+
None => {
797+
return Err(anyhow!("transfer: could not resolve '{}'", to))
798+
}
799+
Some(r) => Some(r),
796800
}
797-
Some(r) => r,
801+
} else {
802+
None
798803
};
804+
799805
for space in spaces {
800806
let spacehash = SpaceKey::from(Sha256::hash(space.as_ref()));
801807
match store.get_space_info(&spacehash)? {
@@ -817,6 +823,18 @@ impl RpcWallet {
817823
}
818824

819825
Some(full) => {
826+
let recipient = match recipient.clone() {
827+
None => {
828+
SpaceAddress(
829+
Address::from_script(
830+
full.spaceout.script_pubkey.as_script(),
831+
wallet.config.network,
832+
).expect("valid script")
833+
)
834+
}
835+
Some(addr) => SpaceAddress(addr)
836+
};
837+
820838
builder = builder.add_transfer(SpaceTransfer {
821839
space: full,
822840
recipient: recipient.clone(),
@@ -938,7 +956,7 @@ impl RpcWallet {
938956
let address = wallet.next_unused_space_address();
939957
spaces.push(SpaceTransfer {
940958
space: spaceout,
941-
recipient: address.0,
959+
recipient: address,
942960
});
943961
}
944962

@@ -1195,7 +1213,7 @@ impl RpcWallet {
11951213
pub async fn send_sign_message(
11961214
&self,
11971215
space: &str,
1198-
msg: protocol::Bytes
1216+
msg: protocol::Bytes,
11991217
) -> anyhow::Result<SignedMessage> {
12001218
let (resp, resp_rx) = oneshot::channel();
12011219
self.sender
@@ -1221,7 +1239,6 @@ impl RpcWallet {
12211239
}
12221240

12231241

1224-
12251242
pub async fn send_force_spend(
12261243
&self,
12271244
outpoint: OutPoint,

node/tests/integration_tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ async fn it_should_allow_batch_transfers_refreshing_expire_height(
364364
ALICE,
365365
vec![RpcWalletRequest::Transfer(TransferSpacesParams {
366366
spaces: registered_spaces.clone(),
367-
to: space_address,
367+
to: Some(space_address),
368368
})],
369369
false,
370370
)
@@ -781,7 +781,7 @@ async fn it_should_not_allow_register_or_transfer_to_same_space_multiple_times(r
781781
ALICE,
782782
vec![RpcWalletRequest::Transfer(TransferSpacesParams {
783783
spaces: vec![transfer.clone()],
784-
to: bob_address.clone(),
784+
to: Some(bob_address.clone()),
785785
})],
786786
false,
787787
).await.expect("send request");
@@ -792,7 +792,7 @@ async fn it_should_not_allow_register_or_transfer_to_same_space_multiple_times(r
792792
ALICE,
793793
vec![RpcWalletRequest::Transfer(TransferSpacesParams {
794794
spaces: vec![transfer],
795-
to: bob_address,
795+
to: Some(bob_address),
796796
})],
797797
false,
798798
).await.expect_err("there's already a transfer submitted");
@@ -849,7 +849,7 @@ async fn it_can_batch_txs(rig: &TestRig) -> anyhow::Result<()> {
849849
vec![
850850
RpcWalletRequest::Transfer(TransferSpacesParams {
851851
spaces: vec!["@test9996".to_string()],
852-
to: bob_address,
852+
to: Some(bob_address),
853853
}),
854854
RpcWalletRequest::Bid(BidParams {
855855
name: "@test100".to_string(),

wallet/src/builder.rs

+27-13
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ pub struct RegisterRequest {
119119
#[derive(Debug, Clone)]
120120
pub struct SpaceTransfer {
121121
pub space: FullSpaceOut,
122-
pub recipient: Address,
122+
pub recipient: SpaceAddress,
123123
}
124124

125125
#[derive(Debug, Clone)]
@@ -555,17 +555,31 @@ impl Iterator for BuilderIterator<'_> {
555555
if !params.transfers.is_empty() {
556556
// TODO: resolved address recipient
557557
for transfer in &params.transfers {
558-
detailed_tx.add_transfer(
559-
transfer
560-
.space
561-
.spaceout
562-
.space
563-
.as_ref()
564-
.expect("space")
565-
.name
566-
.to_string(),
567-
transfer.recipient.script_pubkey(),
568-
);
558+
if self.wallet.is_mine(transfer.recipient.script_pubkey()) {
559+
detailed_tx.add_renew(
560+
transfer
561+
.space
562+
.spaceout
563+
.space
564+
.as_ref()
565+
.expect("space")
566+
.name
567+
.to_string(),
568+
transfer.recipient.script_pubkey(),
569+
);
570+
} else {
571+
detailed_tx.add_transfer(
572+
transfer
573+
.space
574+
.spaceout
575+
.space
576+
.as_ref()
577+
.expect("space")
578+
.name
579+
.to_string(),
580+
transfer.recipient.script_pubkey(),
581+
);
582+
}
569583
}
570584
}
571585
if !params.sends.is_empty() {
@@ -783,7 +797,7 @@ impl Builder {
783797
};
784798
transfers.push(SpaceTransfer {
785799
space: params.space,
786-
recipient: to.0,
800+
recipient: to,
787801
})
788802
}
789803
StackRequest::Send(send) => sends.push(send),

wallet/src/tx_event.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub enum TxEventKind {
8383
Bid,
8484
Register,
8585
Transfer,
86+
Renew,
8687
Send,
8788
FeeBump,
8889
Buy,
@@ -322,6 +323,15 @@ impl TxRecord {
322323
});
323324
}
324325

326+
pub fn add_renew(&mut self, space: String, to: ScriptBuf) {
327+
self.events.push(TxEvent {
328+
kind: TxEventKind::Renew,
329+
space: Some(space),
330+
foreign_input: None,
331+
details: Some(serde_json::to_value(TransferEventDetails { to }).expect("json value")),
332+
});
333+
}
334+
325335
pub fn add_bidout(&mut self, count: usize) {
326336
self.events.push(TxEvent {
327337
kind: TxEventKind::Bidout,
@@ -507,7 +517,8 @@ impl Display for TxEventKind {
507517
TxEventKind::Send => "send",
508518
TxEventKind::Script => "script",
509519
TxEventKind::FeeBump => "fee-bump",
510-
TxEventKind::Buy => "buy"
520+
TxEventKind::Buy => "buy",
521+
TxEventKind::Renew => "renew",
511522
})
512523
}
513524
}
@@ -527,6 +538,7 @@ impl FromStr for TxEventKind {
527538
"script" => Ok(TxEventKind::Script),
528539
"fee-bump" => Ok(TxEventKind::FeeBump),
529540
"buy" => Ok(TxEventKind::Buy),
541+
"renew" => Ok(TxEventKind::Renew),
530542
_ => Err("invalid event kind"),
531543
}
532544
}

0 commit comments

Comments
 (0)