bitcoind_json_rpc_client/client_sync/v17/
wallet.rs

1// SPDX-License-Identifier: CC0-1.0
2
3//! Macros for implementing JSON-RPC methods on a client.
4//!
5//! Specifically this is methods found under the `== Wallet ==` section of the
6//! API docs of `bitcoind v0.17.1`.
7//!
8//! All macros require `Client` to be in scope.
9//!
10//! See or use the `define_jsonrpc_minreq_client!` macro to define a `Client`.
11
12/// Implements bitcoind JSON-RPC API method `createwallet`
13#[macro_export]
14macro_rules! impl_client_v17__createwallet {
15    () => {
16        impl Client {
17            pub fn create_wallet(&self, wallet: &str) -> Result<CreateWallet> {
18                self.call("createwallet", &[wallet.into()])
19            }
20        }
21    };
22}
23
24/// Implements bitcoind JSON-RPC API method `unloadwallet`
25#[macro_export]
26macro_rules! impl_client_v17__unloadwallet {
27    () => {
28        impl Client {
29            pub fn unload_wallet(&self, wallet: &str) -> Result<()> {
30                self.call("unloadwallet", &[wallet.into()])
31            }
32        }
33    };
34}
35
36/// Implements bitcoind JSON-RPC API method `loadwallet`
37#[macro_export]
38macro_rules! impl_client_v17__loadwallet {
39    () => {
40        impl Client {
41            pub fn load_wallet(&self, wallet: &str) -> Result<LoadWallet> {
42                self.call("loadwallet", &[wallet.into()])
43            }
44        }
45    };
46}
47
48/// Implements bitcoind JSON-RPC API method `getbalance`
49#[macro_export]
50macro_rules! impl_client_v17__getbalance {
51    () => {
52        impl Client {
53            pub fn get_balance(&self) -> Result<GetBalance> { self.call("getbalance", &[]) }
54        }
55    };
56}
57
58/// Implements bitcoind JSON-RPC API method `getnewaddress`
59#[macro_export]
60macro_rules! impl_client_v17__getnewaddress {
61    () => {
62        impl Client {
63            /// Gets a new address from `bitcoind` and parses it assuming its correct.
64            pub fn new_address(&self) -> Result<bitcoin::Address> {
65                use core::str::FromStr;
66
67                let json = self.get_new_address()?;
68                let address = bitcoin::Address::from_str(&json.0)
69                    .expect("assume the address is valid")
70                    .assume_checked(); // Assume bitcoind will return an invalid address for the network its on.
71                Ok(address)
72            }
73
74            /// Gets a new address from `bitcoind` and parses it assuming its correct.
75            pub fn new_address_with_type(&self, ty: AddressType) -> Result<bitcoin::Address> {
76                use core::str::FromStr;
77
78                let json = self.get_new_address_with_type(ty)?;
79                let address = bitcoin::Address::from_str(&json.0)
80                    .expect("assume the address is valid")
81                    .assume_checked(); // Assume bitcoind will return an invalid address for the network its on.
82                Ok(address)
83            }
84
85            pub fn get_new_address(&self) -> Result<GetNewAddress> {
86                self.call("getnewaddress", &[])
87            }
88
89            pub fn get_new_address_with_type(&self, ty: AddressType) -> Result<GetNewAddress> {
90                self.call("getnewaddress", &["".into(), into_json(ty)?])
91            }
92        }
93    };
94}
95
96/// Implements bitcoind JSON-RPC API method `sendtoaddress`
97#[macro_export]
98macro_rules! impl_client_v17__sendtoaddress {
99    () => {
100        impl Client {
101            pub fn send_to_address(
102                &self,
103                address: &Address<NetworkChecked>,
104                amount: Amount,
105            ) -> Result<SendToAddress> {
106                let mut args = [address.to_string().into(), into_json(amount.to_btc())?];
107                self.call("sendtoaddress", handle_defaults(&mut args, &["".into(), "".into()]))
108            }
109        }
110    };
111}
112
113/// Implements bitcoind JSON-RPC API method `gettransaction`
114#[macro_export]
115macro_rules! impl_client_v17__gettransaction {
116    () => {
117        impl Client {
118            pub fn get_transaction(&self, txid: Txid) -> Result<GetTransaction> {
119                self.call("gettransaction", &[into_json(txid)?])
120            }
121        }
122    };
123}