Skip to content

Commit 905d962

Browse files
committed
Implement version specific types for methods
1 parent 7f1f956 commit 905d962

File tree

16 files changed

+462
-3
lines changed

16 files changed

+462
-3
lines changed

client/src/client_sync/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,3 +279,12 @@ pub enum SetBanCommand {
279279
Add,
280280
Remove,
281281
}
282+
283+
/// Args for the `lockunspent` method
284+
#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
285+
pub struct LockUnspentOutput {
286+
/// The transaction id
287+
pub txid: Txid,
288+
/// The output number
289+
pub vout: u32,
290+
}

client/src/client_sync/v17/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,13 @@ crate::impl_client_v17__importprunedfunds!();
134134
crate::impl_client_v17__importpubkey!();
135135
crate::impl_client_v17__importwallet!();
136136
crate::impl_client_v17__keypoolrefill!();
137+
crate::impl_client_v17__lockunspent!();
138+
crate::impl_client_v17__removeprunedfunds!();
139+
crate::impl_client_v17__sethdseed!();
140+
crate::impl_client_v17__settxfee!();
141+
crate::impl_client_v17__walletlock!();
142+
crate::impl_client_v17__walletpassphrase!();
143+
crate::impl_client_v17__walletpassphrasechange!();
137144

138145
/// Argument to the `Client::get_new_address_with_type` function.
139146
///
@@ -158,3 +165,12 @@ impl fmt::Display for AddressType {
158165
fmt::Display::fmt(s, f)
159166
}
160167
}
168+
169+
const SATS_PER_BTC_F64: f64 = 100_000_000.0;
170+
171+
pub fn fee_rate_to_rpc_arg(fee_rate: bitcoin::FeeRate) -> f64 {
172+
let sat_per_kwu = fee_rate.to_sat_per_kwu();
173+
174+
let sat_per_kvb = (sat_per_kwu as f64) / 4.0;
175+
sat_per_kvb / SATS_PER_BTC_F64
176+
}

client/src/client_sync/v17/wallet.rs

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,3 +671,162 @@ macro_rules! impl_client_v17__keypoolrefill {
671671
}
672672
};
673673
}
674+
675+
/// Implements Bitcoin Core JSON-RPC API method `lockunspent`
676+
#[macro_export]
677+
macro_rules! impl_client_v17__lockunspent {
678+
() => {
679+
use crate::client_sync::LockUnspentOutput;
680+
impl Client {
681+
pub fn lock_unspent(
682+
&self,
683+
unlock: bool,
684+
outputs: Option<&[LockUnspentOutput]>,
685+
persistent: Option<bool>,
686+
) -> Result<bool> {
687+
let mut params = vec![unlock.into()];
688+
689+
match outputs {
690+
Some(outs) => params.push(serde_json::to_value(outs)?),
691+
None => {
692+
if unlock {
693+
params.push(serde_json::Value::Array(vec![]));
694+
} else {
695+
return Err(crate::client_sync::Error::Returned("lockunspent requires specific outputs when locking (unlock=false)".to_string()));
696+
}
697+
}
698+
}
699+
700+
if !unlock {
701+
if let Some(p) = persistent {
702+
if params.len() == 1 {
703+
params.push(serde_json::Value::Array(vec![]));
704+
}
705+
params.push(p.into());
706+
}
707+
}
708+
self.call("lockunspent", &params)
709+
}
710+
}
711+
};
712+
}
713+
714+
/// Implements Bitcoin Core JSON-RPC API method `removeprunedfunds`
715+
#[macro_export]
716+
macro_rules! impl_client_v17__removeprunedfunds {
717+
() => {
718+
impl Client {
719+
pub fn remove_pruned_funds(&self, txid: Txid) -> Result<()> {
720+
match self.call("removeprunedfunds", &[into_json(txid)?]) {
721+
Ok(serde_json::Value::Null) => Ok(()),
722+
Ok(ref val) if val.is_null() => Ok(()),
723+
Ok(other) => Err(crate::client_sync::Error::Returned(format!("removeprunedfunds expected null, got: {}", other))),
724+
Err(e) => Err(e.into()),
725+
}
726+
}
727+
}
728+
};
729+
}
730+
731+
/// Implements Bitcoin Core JSON-RPC API method `sethdseed`
732+
#[macro_export]
733+
macro_rules! impl_client_v17__sethdseed {
734+
() => {
735+
impl Client {
736+
pub fn set_hd_seed(
737+
&self,
738+
new_keypool: Option<bool>,
739+
seed: Option<&PrivateKey>,
740+
) -> Result<()> {
741+
let mut params = vec![];
742+
743+
if new_keypool.is_some() || seed.is_some() {
744+
params.push(new_keypool.map_or(true.into(), |k| k.into()));
745+
}
746+
747+
if let Some(s) = seed {
748+
params.push(s.to_wif().into());
749+
}
750+
751+
match self.call("sethdseed", &params) {
752+
Ok(serde_json::Value::Null) => Ok(()),
753+
Ok(ref val) if val.is_null() => Ok(()),
754+
Ok(other) => Err(crate::client_sync::Error::Returned(format!("sethdseed expected null, got: {}", other))),
755+
Err(e) => Err(e.into()),
756+
}
757+
}
758+
}
759+
};
760+
}
761+
762+
/// Implements Bitcoin Core JSON-RPC API method `settxfee`
763+
#[macro_export]
764+
macro_rules! impl_client_v17__settxfee {
765+
() => {
766+
const SATS_PER_BTC_F64_SETTXFEE: f64 = 100_000_000.0;
767+
fn fee_rate_to_rpc_arg_settxfee(fee_rate: bitcoin::FeeRate) -> f64 {
768+
let sat_per_kwu = fee_rate.to_sat_per_kwu();
769+
let sat_per_kvb = (sat_per_kwu as f64) * 4.0;
770+
sat_per_kvb / SATS_PER_BTC_F64_SETTXFEE
771+
}
772+
773+
impl Client {
774+
pub fn set_tx_fee(&self, fee_rate: bitcoin::FeeRate) -> Result<bool> {
775+
let amount_rpc_arg = fee_rate_to_rpc_arg_settxfee(fee_rate);
776+
777+
self.call("settxfee", &[amount_rpc_arg.into()])
778+
}
779+
}
780+
};
781+
}
782+
783+
/// Implements Bitcoin Core JSON-RPC API method `walletlock`
784+
#[macro_export]
785+
macro_rules! impl_client_v17__walletlock {
786+
() => {
787+
impl Client {
788+
pub fn wallet_lock(&self) -> Result<()> {
789+
match self.call("walletlock", &[]) {
790+
Ok(serde_json::Value::Null) => Ok(()),
791+
Ok(ref val) if val.is_null() => Ok(()),
792+
Ok(other) => Err(crate::client_sync::Error::Returned(format!("walletlock expected null, got: {}", other))),
793+
Err(e) => Err(e.into()),
794+
}
795+
}
796+
}
797+
};
798+
}
799+
800+
/// Implements Bitcoin Core JSON-RPC API method `walletpassphrase`
801+
#[macro_export]
802+
macro_rules! impl_client_v17__walletpassphrase {
803+
() => {
804+
impl Client {
805+
pub fn wallet_passphrase(&self, passphrase: &str, timeout: u64) -> Result<()> {
806+
match self.call("walletpassphrase", &[passphrase.into(), timeout.into()]) {
807+
Ok(serde_json::Value::Null) => Ok(()),
808+
Ok(ref val) if val.is_null() => Ok(()),
809+
Ok(other) => Err(crate::client_sync::Error::Returned(format!("walletpassphrase expected null, got: {}", other))),
810+
Err(e) => Err(e.into()),
811+
}
812+
}
813+
}
814+
};
815+
}
816+
817+
/// Implements Bitcoin Core JSON-RPC API method `walletpassphrasechange`
818+
#[macro_export]
819+
macro_rules! impl_client_v17__walletpassphrasechange {
820+
() => {
821+
impl Client {
822+
pub fn wallet_passphrase_change(&self, old_passphrase: &str, new_passphrase: &str) -> Result<()> {
823+
match self.call("walletpassphrasechange", &[old_passphrase.into(), new_passphrase.into()]) {
824+
Ok(serde_json::Value::Null) => Ok(()),
825+
Ok(ref val) if val.is_null() => Ok(()),
826+
Ok(other) => Err(crate::client_sync::Error::Returned(format!("walletpassphrasechange expected null, got: {}", other))),
827+
Err(e) => Err(e.into()),
828+
}
829+
}
830+
}
831+
};
832+
}

client/src/client_sync/v18/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,10 @@ crate::impl_client_v17__importprunedfunds!();
127127
crate::impl_client_v17__importpubkey!();
128128
crate::impl_client_v17__importwallet!();
129129
crate::impl_client_v17__keypoolrefill!();
130+
crate::impl_client_v17__lockunspent!();
131+
crate::impl_client_v17__removeprunedfunds!();
132+
crate::impl_client_v17__sethdseed!();
133+
crate::impl_client_v17__settxfee!();
134+
crate::impl_client_v17__walletlock!();
135+
crate::impl_client_v17__walletpassphrase!();
136+
crate::impl_client_v17__walletpassphrasechange!();

client/src/client_sync/v19/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,10 @@ crate::impl_client_v17__importprunedfunds!();
129129
crate::impl_client_v17__importpubkey!();
130130
crate::impl_client_v17__importwallet!();
131131
crate::impl_client_v17__keypoolrefill!();
132+
crate::impl_client_v17__lockunspent!();
133+
crate::impl_client_v17__removeprunedfunds!();
134+
crate::impl_client_v17__sethdseed!();
135+
crate::impl_client_v17__settxfee!();
136+
crate::impl_client_v17__walletlock!();
137+
crate::impl_client_v17__walletpassphrase!();
138+
crate::impl_client_v17__walletpassphrasechange!();

client/src/client_sync/v20/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,10 @@ crate::impl_client_v17__importprunedfunds!();
128128
crate::impl_client_v17__importpubkey!();
129129
crate::impl_client_v17__importwallet!();
130130
crate::impl_client_v17__keypoolrefill!();
131+
crate::impl_client_v17__lockunspent!();
132+
crate::impl_client_v17__removeprunedfunds!();
133+
crate::impl_client_v17__sethdseed!();
134+
crate::impl_client_v17__settxfee!();
135+
crate::impl_client_v17__walletlock!();
136+
crate::impl_client_v17__walletpassphrase!();
137+
crate::impl_client_v17__walletpassphrasechange!();

client/src/client_sync/v21/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,10 @@ crate::impl_client_v17__importprunedfunds!();
128128
crate::impl_client_v17__importpubkey!();
129129
crate::impl_client_v17__importwallet!();
130130
crate::impl_client_v17__keypoolrefill!();
131+
crate::impl_client_v17__lockunspent!();
132+
crate::impl_client_v17__removeprunedfunds!();
133+
crate::impl_client_v17__sethdseed!();
134+
crate::impl_client_v17__settxfee!();
135+
crate::impl_client_v17__walletlock!();
136+
crate::impl_client_v17__walletpassphrase!();
137+
crate::impl_client_v17__walletpassphrasechange!();

client/src/client_sync/v22/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,10 @@ crate::impl_client_v17__importprunedfunds!();
129129
crate::impl_client_v17__importpubkey!();
130130
crate::impl_client_v17__importwallet!();
131131
crate::impl_client_v17__keypoolrefill!();
132+
crate::impl_client_v17__lockunspent!();
133+
crate::impl_client_v17__removeprunedfunds!();
134+
crate::impl_client_v17__sethdseed!();
135+
crate::impl_client_v17__settxfee!();
136+
crate::impl_client_v17__walletlock!();
137+
crate::impl_client_v17__walletpassphrase!();
138+
crate::impl_client_v17__walletpassphrasechange!();

client/src/client_sync/v23/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ crate::impl_client_v17__importprunedfunds!();
130130
crate::impl_client_v17__importpubkey!();
131131
crate::impl_client_v17__importwallet!();
132132
crate::impl_client_v17__keypoolrefill!();
133+
crate::impl_client_v17__lockunspent!();
134+
crate::impl_client_v17__removeprunedfunds!();
135+
crate::impl_client_v17__sethdseed!();
136+
crate::impl_client_v17__settxfee!();
137+
crate::impl_client_v17__walletlock!();
138+
crate::impl_client_v17__walletpassphrase!();
139+
crate::impl_client_v17__walletpassphrasechange!();
133140

134141
/// Argument to the `Client::get_new_address_with_type` function.
135142
///

client/src/client_sync/v24.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,10 @@ crate::impl_client_v17__importprunedfunds!();
126126
crate::impl_client_v17__importpubkey!();
127127
crate::impl_client_v17__importwallet!();
128128
crate::impl_client_v17__keypoolrefill!();
129+
crate::impl_client_v17__lockunspent!();
130+
crate::impl_client_v17__removeprunedfunds!();
131+
crate::impl_client_v17__sethdseed!();
132+
crate::impl_client_v17__settxfee!();
133+
crate::impl_client_v17__walletlock!();
134+
crate::impl_client_v17__walletpassphrase!();
135+
crate::impl_client_v17__walletpassphrasechange!();

0 commit comments

Comments
 (0)