Skip to content

Commit

Permalink
lints and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
smartgoo committed Nov 2, 2024
1 parent d53302e commit 281d23e
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 44 deletions.
6 changes: 3 additions & 3 deletions consensus/client/src/outpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ impl TryFrom<&JsValue> for TransactionOutpoint {
}

#[cfg(feature = "py-sdk")]
impl TryFrom<Bound<'_, PyDict>> for TransactionOutpoint {
impl TryFrom<&Bound<'_, PyDict>> for TransactionOutpoint {
type Error = PyErr;
fn try_from(dict: Bound<PyDict>) -> PyResult<Self> {
let inner: TransactionOutpointInner = serde_pyobject::from_pyobject(dict)?;
fn try_from(dict: &Bound<PyDict>) -> PyResult<Self> {
let inner: TransactionOutpointInner = serde_pyobject::from_pyobject(dict.clone())?;
Ok(TransactionOutpoint { inner: Arc::new(inner) })
}
}
Expand Down
9 changes: 3 additions & 6 deletions consensus/client/src/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>()?,
)?;

let outpoint = TransactionOutpoint::try_from(dict
.get_item("outpoint")?
.ok_or_else(|| PyException::new_err("Key `outpoint` not present"))?
.downcast::<PyDict>()?
.clone()
let outpoint = TransactionOutpoint::try_from(
dict.get_item("outpoint")?.ok_or_else(|| PyException::new_err("Key `outpoint` not present"))?.downcast::<PyDict>()?,
)?;

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::<PyDict>()?;

let amount: u64 = utxo_entry.get_item("amount")?.ok_or_else(|| PyException::new_err("Key `amount` not present"))?.extract()?;
Expand Down
4 changes: 2 additions & 2 deletions python/core/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ impl<'a> FromPyObject<'a> for PyBinary {
}
}

impl TryFrom<Bound<'_, PyAny>> for PyBinary {
impl TryFrom<&Bound<'_, PyAny>> for PyBinary {
type Error = PyErr;
fn try_from(value: Bound<PyAny>) -> Result<Self, Self::Error> {
fn try_from(value: &Bound<PyAny>) -> Result<Self, Self::Error> {
if let Ok(str) = value.extract::<String>() {
// Python `str` (of valid hex)
let mut data = vec![0u8; str.len() / 2];
Expand Down
File renamed without changes.
4 changes: 3 additions & 1 deletion python/examples/rpc/all_calls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


async def main():
client = RpcClient(resolver=Resolver(), network_id="testnet-11")
client = RpcClient(resolver=Resolver())
await client.connect()

###
Expand Down Expand Up @@ -169,6 +169,8 @@ async def main():

# await client.unban(request)

await client.disconnect()

if __name__ == "__main__":
asyncio.run(main())

4 changes: 2 additions & 2 deletions rpc/wrpc/python/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct PyCallback {
}

impl PyCallback {
fn append_to_args(&self, py: Python, event: Bound<PyDict>) -> PyResult<Py<PyTuple>> {
fn add_event_to_args(&self, py: Python, event: Bound<PyDict>) -> PyResult<Py<PyTuple>> {
match &self.args {
Some(existing_args) => {
let tuple_ref = existing_args.bind(py);
Expand All @@ -74,7 +74,7 @@ impl PyCallback {
}

fn execute(&self, py: Python, event: Bound<PyDict>) -> PyResult<PyObject> {
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
Expand Down
45 changes: 25 additions & 20 deletions wallet/core/src/python/tx/generator/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,49 @@ impl FromPyObject<'_> for PyUtxoEntries {
// Must be list
let list = ob.downcast::<PyList>()?;

let entries = list.iter().map(|item| {
if let Ok(entry) = item.extract::<UtxoEntryReference>() {
Ok(entry)
} else if let Ok(entry) = item.downcast::<PyDict>() {
UtxoEntryReference::try_from(entry)
} else {
Err(PyException::new_err("All entries must be UtxoEntryReference instance or compatible dict"))
}
}).collect::<PyResult<Vec<UtxoEntryReference>>>()?;
let entries = list
.iter()
.map(|item| {
if let Ok(entry) = item.extract::<UtxoEntryReference>() {
Ok(entry)
} else if let Ok(entry) = item.downcast::<PyDict>() {
UtxoEntryReference::try_from(entry)
} else {
Err(PyException::new_err("All entries must be UtxoEntryReference instance or compatible dict"))
}
})
.collect::<PyResult<Vec<UtxoEntryReference>>>()?;

Ok(PyUtxoEntries { entries })
}
}

pub struct PyOutputs {
pub outputs: Vec<PaymentOutput>
pub outputs: Vec<PaymentOutput>,
}

impl FromPyObject<'_> for PyOutputs {
fn extract_bound(ob: &Bound<'_, PyAny>) -> PyResult<Self> {
// Must be list
let list = ob.downcast::<PyList>()?;

let outputs = list.iter().map(|item| {
if let Ok(output) = item.extract::<PaymentOutput>() {
Ok(output)
} else if let Ok(output) = item.downcast::<PyDict>() {
PaymentOutput::try_from(output)
} else {
Err(PyException::new_err("All outputs must be PaymentOutput instance or compatible dict"))
}
}).collect::<PyResult<Vec<PaymentOutput>>>()?;
let outputs = list
.iter()
.map(|item| {
if let Ok(output) = item.extract::<PaymentOutput>() {
Ok(output)
} else if let Ok(output) = item.downcast::<PyDict>() {
PaymentOutput::try_from(output)
} else {
Err(PyException::new_err("All outputs must be PaymentOutput instance or compatible dict"))
}
})
.collect::<PyResult<Vec<PaymentOutput>>>()?;

Ok(PyOutputs { outputs })
}
}


#[pyclass]
pub struct Generator {
inner: Arc<native::Generator>,
Expand Down
9 changes: 3 additions & 6 deletions wallet/core/src/python/tx/utils.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<Bound<PyDict>>,
outputs: PyOutputs,
priority_fee: u64,
payload: Option<PyBinary>,
sig_op_count: Option<u8>,
) -> PyResult<Transaction> {
let outputs: Vec<PaymentOutput> = outputs.iter().map(|utxo| PaymentOutput::try_from(utxo)).collect::<Result<Vec<_>, _>>()?;

let payload: Vec<u8> = payload.map(Into::into).unwrap_or_default();
let sig_op_count = sig_op_count.unwrap_or(1);

Expand All @@ -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::<Vec<TransactionOutput>>();
let outputs = outputs.outputs.into_iter().map(|output| output.into()).collect::<Vec<TransactionOutput>>();
let transaction = Transaction::new(None, 0, inputs, outputs, 0, SUBNETWORK_ID_NATIVE, 0, payload, 0)?;

Ok(transaction)
Expand Down
6 changes: 3 additions & 3 deletions wallet/core/src/tx/payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,10 @@ impl TryCastFromJs for PaymentOutputs {
}

#[cfg(feature = "py-sdk")]
impl TryFrom<Vec<Bound<'_, PyDict>>> for PaymentOutputs {
impl TryFrom<Vec<&Bound<'_, PyDict>>> for PaymentOutputs {
type Error = PyErr;
fn try_from(value: Vec<Bound<PyDict>>) -> PyResult<Self> {
let outputs: Vec<PaymentOutput> = value.iter().map(|utxo| PaymentOutput::try_from(utxo)).collect::<Result<Vec<_>, _>>()?;
fn try_from(value: Vec<&Bound<PyDict>>) -> PyResult<Self> {
let outputs: Vec<PaymentOutput> = value.iter().map(|utxo| PaymentOutput::try_from(*utxo)).collect::<Result<Vec<_>, _>>()?;
Ok(PaymentOutputs { outputs })
}
}
Expand Down
2 changes: 1 addition & 1 deletion wallet/keys/src/xprv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl XPrv {
}

#[pyo3(name = "derive_path")]
pub fn derive_path_py(&self, path: Bound<PyAny>) -> PyResult<XPrv> {
pub fn derive_path_py(&self, path: &Bound<PyAny>) -> PyResult<XPrv> {
let path = if let Ok(path_str) = path.extract::<String>() {
Ok(DerivationPath::new(path_str.as_str())?)
} else if let Ok(path_obj) = path.extract::<DerivationPath>() {
Expand Down

0 comments on commit 281d23e

Please sign in to comment.