diff --git a/consensus/client/src/outpoint.rs b/consensus/client/src/outpoint.rs index 6cf8dd1be..c789024a2 100644 --- a/consensus/client/src/outpoint.rs +++ b/consensus/client/src/outpoint.rs @@ -176,10 +176,10 @@ impl TryFrom<&JsValue> for TransactionOutpoint { } #[cfg(feature = "py-sdk")] -impl TryFrom> for TransactionOutpoint { +impl TryFrom<&Bound<'_, PyDict>> for TransactionOutpoint { type Error = PyErr; - fn try_from(dict: Bound) -> PyResult { - let inner: TransactionOutpointInner = serde_pyobject::from_pyobject(dict)?; + fn try_from(dict: &Bound) -> PyResult { + let inner: TransactionOutpointInner = serde_pyobject::from_pyobject(dict.clone())?; Ok(TransactionOutpoint { inner: Arc::new(inner) }) } } diff --git a/consensus/client/src/utxo.rs b/consensus/client/src/utxo.rs index 61ab554da..da56b9603 100644 --- a/consensus/client/src/utxo.rs +++ b/consensus/client/src/utxo.rs @@ -566,14 +566,11 @@ impl TryFrom<&Bound<'_, PyDict>> for UtxoEntryReference { dict.get_item("address")?.ok_or_else(|| PyException::new_err("Key `address` not present"))?.extract::()?, )?; - let outpoint = TransactionOutpoint::try_from(dict - .get_item("outpoint")? - .ok_or_else(|| PyException::new_err("Key `outpoint` not present"))? - .downcast::()? - .clone() + let outpoint = TransactionOutpoint::try_from( + dict.get_item("outpoint")?.ok_or_else(|| PyException::new_err("Key `outpoint` not present"))?.downcast::()?, )?; - let utxo_entry_value= dict.get_item("utxoEntry")?.ok_or_else(|| PyException::new_err("Key `utxoEntry` not present"))?; + let utxo_entry_value = dict.get_item("utxoEntry")?.ok_or_else(|| PyException::new_err("Key `utxoEntry` not present"))?; let utxo_entry = utxo_entry_value.downcast::()?; let amount: u64 = utxo_entry.get_item("amount")?.ok_or_else(|| PyException::new_err("Key `amount` not present"))?.extract()?; diff --git a/python/core/src/types.rs b/python/core/src/types.rs index 28c75900d..c2d91c549 100644 --- a/python/core/src/types.rs +++ b/python/core/src/types.rs @@ -28,9 +28,9 @@ impl<'a> FromPyObject<'a> for PyBinary { } } -impl TryFrom> for PyBinary { +impl TryFrom<&Bound<'_, PyAny>> for PyBinary { type Error = PyErr; - fn try_from(value: Bound) -> Result { + fn try_from(value: &Bound) -> Result { if let Ok(str) = value.extract::() { // Python `str` (of valid hex) let mut data = vec![0u8; str.len() / 2]; diff --git a/python/examples/message-signing.py b/python/examples/message_signing.py similarity index 100% rename from python/examples/message-signing.py rename to python/examples/message_signing.py diff --git a/python/examples/rpc/all_calls.py b/python/examples/rpc/all_calls.py index 56a28a9d1..b9ebabaca 100644 --- a/python/examples/rpc/all_calls.py +++ b/python/examples/rpc/all_calls.py @@ -4,7 +4,7 @@ async def main(): - client = RpcClient(resolver=Resolver(), network_id="testnet-11") + client = RpcClient(resolver=Resolver()) await client.connect() ### @@ -169,6 +169,8 @@ async def main(): # await client.unban(request) + await client.disconnect() + if __name__ == "__main__": asyncio.run(main()) diff --git a/rpc/wrpc/python/src/client.rs b/rpc/wrpc/python/src/client.rs index 149783e4a..4aa92f455 100644 --- a/rpc/wrpc/python/src/client.rs +++ b/rpc/wrpc/python/src/client.rs @@ -59,7 +59,7 @@ struct PyCallback { } impl PyCallback { - fn append_to_args(&self, py: Python, event: Bound) -> PyResult> { + fn add_event_to_args(&self, py: Python, event: Bound) -> PyResult> { match &self.args { Some(existing_args) => { let tuple_ref = existing_args.bind(py); @@ -74,7 +74,7 @@ impl PyCallback { } fn execute(&self, py: Python, event: Bound) -> PyResult { - let args = self.append_to_args(py, event)?; + let args = self.add_event_to_args(py, event)?; let kwargs = self.kwargs.as_ref().map(|kw| kw.bind(py)); let result = self diff --git a/wallet/core/src/python/tx/generator/generator.rs b/wallet/core/src/python/tx/generator/generator.rs index b325c2319..30b0e1716 100644 --- a/wallet/core/src/python/tx/generator/generator.rs +++ b/wallet/core/src/python/tx/generator/generator.rs @@ -12,22 +12,25 @@ impl FromPyObject<'_> for PyUtxoEntries { // Must be list let list = ob.downcast::()?; - let entries = list.iter().map(|item| { - if let Ok(entry) = item.extract::() { - Ok(entry) - } else if let Ok(entry) = item.downcast::() { - UtxoEntryReference::try_from(entry) - } else { - Err(PyException::new_err("All entries must be UtxoEntryReference instance or compatible dict")) - } - }).collect::>>()?; + let entries = list + .iter() + .map(|item| { + if let Ok(entry) = item.extract::() { + Ok(entry) + } else if let Ok(entry) = item.downcast::() { + UtxoEntryReference::try_from(entry) + } else { + Err(PyException::new_err("All entries must be UtxoEntryReference instance or compatible dict")) + } + }) + .collect::>>()?; Ok(PyUtxoEntries { entries }) } } pub struct PyOutputs { - pub outputs: Vec + pub outputs: Vec, } impl FromPyObject<'_> for PyOutputs { @@ -35,21 +38,23 @@ impl FromPyObject<'_> for PyOutputs { // Must be list let list = ob.downcast::()?; - let outputs = list.iter().map(|item| { - if let Ok(output) = item.extract::() { - Ok(output) - } else if let Ok(output) = item.downcast::() { - PaymentOutput::try_from(output) - } else { - Err(PyException::new_err("All outputs must be PaymentOutput instance or compatible dict")) - } - }).collect::>>()?; + let outputs = list + .iter() + .map(|item| { + if let Ok(output) = item.extract::() { + Ok(output) + } else if let Ok(output) = item.downcast::() { + PaymentOutput::try_from(output) + } else { + Err(PyException::new_err("All outputs must be PaymentOutput instance or compatible dict")) + } + }) + .collect::>>()?; Ok(PyOutputs { outputs }) } } - #[pyclass] pub struct Generator { inner: Arc, diff --git a/wallet/core/src/python/tx/utils.rs b/wallet/core/src/python/tx/utils.rs index 070ac1831..9634b45fe 100644 --- a/wallet/core/src/python/tx/utils.rs +++ b/wallet/core/src/python/tx/utils.rs @@ -1,6 +1,5 @@ use crate::imports::*; -use crate::python::tx::generator::{Generator, GeneratorSummary, PendingTransaction, PyUtxoEntries, PyOutputs}; -use crate::tx::payment::PaymentOutput; +use crate::python::tx::generator::{Generator, GeneratorSummary, PendingTransaction, PyOutputs, PyUtxoEntries}; use kaspa_consensus_client::*; use kaspa_consensus_core::subnets::SUBNETWORK_ID_NATIVE; @@ -9,13 +8,11 @@ use kaspa_consensus_core::subnets::SUBNETWORK_ID_NATIVE; #[pyo3(signature = (utxo_entry_source, outputs, priority_fee, payload=None, sig_op_count=None))] pub fn create_transaction_py( utxo_entry_source: PyUtxoEntries, - outputs: Vec>, + outputs: PyOutputs, priority_fee: u64, payload: Option, sig_op_count: Option, ) -> PyResult { - let outputs: Vec = outputs.iter().map(|utxo| PaymentOutput::try_from(utxo)).collect::, _>>()?; - let payload: Vec = payload.map(Into::into).unwrap_or_default(); let sig_op_count = sig_op_count.unwrap_or(1); @@ -38,7 +35,7 @@ pub fn create_transaction_py( return Err(PyException::new_err(format!("priority fee({priority_fee}) > amount({total_input_amount})"))); } - let outputs = outputs.into_iter().map(|output| output.into()).collect::>(); + let outputs = outputs.outputs.into_iter().map(|output| output.into()).collect::>(); let transaction = Transaction::new(None, 0, inputs, outputs, 0, SUBNETWORK_ID_NATIVE, 0, payload, 0)?; Ok(transaction) diff --git a/wallet/core/src/tx/payment.rs b/wallet/core/src/tx/payment.rs index 3cb0714e7..7d3e3f18e 100644 --- a/wallet/core/src/tx/payment.rs +++ b/wallet/core/src/tx/payment.rs @@ -210,10 +210,10 @@ impl TryCastFromJs for PaymentOutputs { } #[cfg(feature = "py-sdk")] -impl TryFrom>> for PaymentOutputs { +impl TryFrom>> for PaymentOutputs { type Error = PyErr; - fn try_from(value: Vec>) -> PyResult { - let outputs: Vec = value.iter().map(|utxo| PaymentOutput::try_from(utxo)).collect::, _>>()?; + fn try_from(value: Vec<&Bound>) -> PyResult { + let outputs: Vec = value.iter().map(|utxo| PaymentOutput::try_from(*utxo)).collect::, _>>()?; Ok(PaymentOutputs { outputs }) } } diff --git a/wallet/keys/src/xprv.rs b/wallet/keys/src/xprv.rs index ed052ec5c..a21d3b599 100644 --- a/wallet/keys/src/xprv.rs +++ b/wallet/keys/src/xprv.rs @@ -155,7 +155,7 @@ impl XPrv { } #[pyo3(name = "derive_path")] - pub fn derive_path_py(&self, path: Bound) -> PyResult { + pub fn derive_path_py(&self, path: &Bound) -> PyResult { let path = if let Ok(path_str) = path.extract::() { Ok(DerivationPath::new(path_str.as_str())?) } else if let Ok(path_obj) = path.extract::() {