Skip to content

Commit

Permalink
Merge pull request #65 from buffrr/renew
Browse files Browse the repository at this point in the history
Add Renew command
  • Loading branch information
buffrr authored Jan 26, 2025
2 parents 2d6620d + d2f029e commit 361ca77
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 35 deletions.
26 changes: 25 additions & 1 deletion node/src/bin/space-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ enum Commands {
#[arg(long, short)]
fee_rate: Option<u64>,
},
/// Renew ownership of a space
#[command(name = "renew", )]
Renew {
/// Spaces to renew
#[arg(display_order = 0)]
spaces: Vec<String>,
/// Fee rate to use in sat/vB
#[arg(long, short)]
fee_rate: Option<u64>,
},
/// Estimates the minimum bid needed for a rollout within the given target blocks
#[command(name = "estimatebid")]
EstimateBid {
Expand Down Expand Up @@ -554,6 +564,19 @@ async fn handle_commands(
)
.await?
}
Commands::Renew { spaces, fee_rate } => {
let spaces: Vec<_> = spaces.into_iter().map(|s| normalize_space(&s)).collect();
cli.send_request(
Some(RpcWalletRequest::Transfer(TransferSpacesParams {
spaces,
to: None,
})),
None,
fee_rate,
false,
)
.await?
}
Commands::Transfer {
spaces,
to,
Expand All @@ -563,7 +586,7 @@ async fn handle_commands(
cli.send_request(
Some(RpcWalletRequest::Transfer(TransferSpacesParams {
spaces,
to,
to: Some(to),
})),
None,
fee_rate,
Expand Down Expand Up @@ -741,6 +764,7 @@ async fn handle_commands(
}).await?;
println!("{}", serde_json::to_string_pretty(&result).expect("result"));
}

}

Ok(())
Expand Down
8 changes: 5 additions & 3 deletions node/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,16 +273,18 @@ pub enum RpcWalletRequest {
Register(RegisterParams),
#[serde(rename = "execute")]
Execute(ExecuteParams),
#[serde(rename = "sendspaces")]
#[serde(rename = "transfer")]
Transfer(TransferSpacesParams),
#[serde(rename = "sendcoins")]
#[serde(rename = "send")]
SendCoins(SendCoinsParams),
}

#[derive(Clone, Serialize, Deserialize)]
pub struct TransferSpacesParams {
pub spaces: Vec<String>,
pub to: String,

#[serde(skip_serializing_if = "Option::is_none")]
pub to: Option<String>,
}

#[derive(Clone, Serialize, Deserialize)]
Expand Down
43 changes: 30 additions & 13 deletions node/src/wallets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub enum WalletCommand {
space: String,
msg: protocol::Bytes,
resp: crate::rpc::Responder<anyhow::Result<SignedMessage>>,
}
},
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, ValueEnum)]
Expand Down Expand Up @@ -184,7 +184,7 @@ impl RpcWallet {
let space = fullspaceout.spaceout.space.as_ref().expect("space").name.to_string();
let foreign_input = fullspaceout.outpoint();
let tx = wallet.buy::<Sha256>(state, &listing, fee_rate)?;

if !skip_tx_check {
let tip = wallet.local_chain().tip().height();
let mut checker = TxChecker::new(state);
Expand All @@ -205,7 +205,7 @@ impl RpcWallet {

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

Ok(TxResponse {
Expand Down Expand Up @@ -248,7 +248,7 @@ impl RpcWallet {

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

Ok(vec![TxResponse {
Expand Down Expand Up @@ -573,7 +573,7 @@ impl RpcWallet {

fn list_spaces(
wallet: &mut SpacesWallet,
state: &mut LiveSnapshot
state: &mut LiveSnapshot,
) -> anyhow::Result<ListSpacesResponse> {
let unspent = wallet.list_unspent_with_details(state)?;
let recent_events = wallet.list_recent_events()?;
Expand Down Expand Up @@ -788,14 +788,20 @@ impl RpcWallet {
.filter_map(|space| SLabel::from_str(space).ok())
.collect();
if spaces.len() != params.spaces.len() {
return Err(anyhow!("sendspaces: some names were malformed"));
return Err(anyhow!("transfer: some names were malformed"));
}
let recipient = match Self::resolve(network, store, &params.to, true)? {
None => {
return Err(anyhow!("sendspaces: could not resolve '{}'", params.to))

let recipient = if let Some(to) = params.to {
match Self::resolve(network, store, &to, true)? {
None => {
return Err(anyhow!("transfer: could not resolve '{}'", to))
}
Some(r) => Some(r),
}
Some(r) => r,
} else {
None
};

for space in spaces {
let spacehash = SpaceKey::from(Sha256::hash(space.as_ref()));
match store.get_space_info(&spacehash)? {
Expand All @@ -817,6 +823,18 @@ impl RpcWallet {
}

Some(full) => {
let recipient = match recipient.clone() {
None => {
SpaceAddress(
Address::from_script(
full.spaceout.script_pubkey.as_script(),
wallet.config.network,
).expect("valid script")
)
}
Some(addr) => SpaceAddress(addr)
};

builder = builder.add_transfer(SpaceTransfer {
space: full,
recipient: recipient.clone(),
Expand Down Expand Up @@ -938,7 +956,7 @@ impl RpcWallet {
let address = wallet.next_unused_space_address();
spaces.push(SpaceTransfer {
space: spaceout,
recipient: address.0,
recipient: address,
});
}

Expand Down Expand Up @@ -1195,7 +1213,7 @@ impl RpcWallet {
pub async fn send_sign_message(
&self,
space: &str,
msg: protocol::Bytes
msg: protocol::Bytes,
) -> anyhow::Result<SignedMessage> {
let (resp, resp_rx) = oneshot::channel();
self.sender
Expand All @@ -1221,7 +1239,6 @@ impl RpcWallet {
}



pub async fn send_force_spend(
&self,
outpoint: OutPoint,
Expand Down
8 changes: 4 additions & 4 deletions node/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ async fn it_should_allow_batch_transfers_refreshing_expire_height(
ALICE,
vec![RpcWalletRequest::Transfer(TransferSpacesParams {
spaces: registered_spaces.clone(),
to: space_address,
to: Some(space_address),
})],
false,
)
Expand Down Expand Up @@ -781,7 +781,7 @@ async fn it_should_not_allow_register_or_transfer_to_same_space_multiple_times(r
ALICE,
vec![RpcWalletRequest::Transfer(TransferSpacesParams {
spaces: vec![transfer.clone()],
to: bob_address.clone(),
to: Some(bob_address.clone()),
})],
false,
).await.expect("send request");
Expand All @@ -792,7 +792,7 @@ async fn it_should_not_allow_register_or_transfer_to_same_space_multiple_times(r
ALICE,
vec![RpcWalletRequest::Transfer(TransferSpacesParams {
spaces: vec![transfer],
to: bob_address,
to: Some(bob_address),
})],
false,
).await.expect_err("there's already a transfer submitted");
Expand Down Expand Up @@ -849,7 +849,7 @@ async fn it_can_batch_txs(rig: &TestRig) -> anyhow::Result<()> {
vec![
RpcWalletRequest::Transfer(TransferSpacesParams {
spaces: vec!["@test9996".to_string()],
to: bob_address,
to: Some(bob_address),
}),
RpcWalletRequest::Bid(BidParams {
name: "@test100".to_string(),
Expand Down
40 changes: 27 additions & 13 deletions wallet/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub struct RegisterRequest {
#[derive(Debug, Clone)]
pub struct SpaceTransfer {
pub space: FullSpaceOut,
pub recipient: Address,
pub recipient: SpaceAddress,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -555,17 +555,31 @@ impl Iterator for BuilderIterator<'_> {
if !params.transfers.is_empty() {
// TODO: resolved address recipient
for transfer in &params.transfers {
detailed_tx.add_transfer(
transfer
.space
.spaceout
.space
.as_ref()
.expect("space")
.name
.to_string(),
transfer.recipient.script_pubkey(),
);
if self.wallet.is_mine(transfer.recipient.script_pubkey()) {
detailed_tx.add_renew(
transfer
.space
.spaceout
.space
.as_ref()
.expect("space")
.name
.to_string(),
transfer.recipient.script_pubkey(),
);
} else {
detailed_tx.add_transfer(
transfer
.space
.spaceout
.space
.as_ref()
.expect("space")
.name
.to_string(),
transfer.recipient.script_pubkey(),
);
}
}
}
if !params.sends.is_empty() {
Expand Down Expand Up @@ -783,7 +797,7 @@ impl Builder {
};
transfers.push(SpaceTransfer {
space: params.space,
recipient: to.0,
recipient: to,
})
}
StackRequest::Send(send) => sends.push(send),
Expand Down
14 changes: 13 additions & 1 deletion wallet/src/tx_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub enum TxEventKind {
Bid,
Register,
Transfer,
Renew,
Send,
FeeBump,
Buy,
Expand Down Expand Up @@ -322,6 +323,15 @@ impl TxRecord {
});
}

pub fn add_renew(&mut self, space: String, to: ScriptBuf) {
self.events.push(TxEvent {
kind: TxEventKind::Renew,
space: Some(space),
foreign_input: None,
details: Some(serde_json::to_value(TransferEventDetails { to }).expect("json value")),
});
}

pub fn add_bidout(&mut self, count: usize) {
self.events.push(TxEvent {
kind: TxEventKind::Bidout,
Expand Down Expand Up @@ -507,7 +517,8 @@ impl Display for TxEventKind {
TxEventKind::Send => "send",
TxEventKind::Script => "script",
TxEventKind::FeeBump => "fee-bump",
TxEventKind::Buy => "buy"
TxEventKind::Buy => "buy",
TxEventKind::Renew => "renew",
})
}
}
Expand All @@ -527,6 +538,7 @@ impl FromStr for TxEventKind {
"script" => Ok(TxEventKind::Script),
"fee-bump" => Ok(TxEventKind::FeeBump),
"buy" => Ok(TxEventKind::Buy),
"renew" => Ok(TxEventKind::Renew),
_ => Err("invalid event kind"),
}
}
Expand Down

0 comments on commit 361ca77

Please sign in to comment.