diff --git a/python/examples/wallet_utils.py b/python/examples/wallet_utils.py new file mode 100644 index 000000000..a1720b947 --- /dev/null +++ b/python/examples/wallet_utils.py @@ -0,0 +1,8 @@ +from kaspa import kaspa_to_sompi, sompi_to_kaspa, sompi_to_kaspa_string_with_suffix + +if __name__ == "__main__": + print(kaspa_to_sompi(100.833)) + + sompi = 499_922_100 + print(sompi_to_kaspa(sompi)) + print(sompi_to_kaspa_string_with_suffix(sompi, "mainnet")) \ No newline at end of file diff --git a/python/kaspa.pyi b/python/kaspa.pyi index c8b10a121..20dfc1928 100644 --- a/python/kaspa.pyi +++ b/python/kaspa.pyi @@ -682,6 +682,12 @@ class PaymentOutput: def __init__(self, address: Address, amount: int) -> None: ... +def kaspa_to_sompi(kaspa: float) -> int: ... + +def sompi_to_kaspa(sompi: int) -> float: ... + +def sompi_to_kaspa_string_with_suffix(sompi: int, network: str) -> str: ... + def create_transaction( utxo_entry_source: list[dict], outputs: list[dict], diff --git a/python/src/lib.rs b/python/src/lib.rs index 1df6565d2..ca8a752d8 100644 --- a/python/src/lib.rs +++ b/python/src/lib.rs @@ -30,6 +30,9 @@ cfg_if::cfg_if! { m.add_class::()?; m.add_class::()?; m.add_class::()?; + m.add_function(wrap_pyfunction!(kaspa_wallet_core::python::utils::kaspa_to_sompi, m)?)?; + m.add_function(wrap_pyfunction!(kaspa_wallet_core::python::utils::sompi_to_kaspa, m)?)?; + m.add_function(wrap_pyfunction!(kaspa_wallet_core::python::utils::sompi_to_kaspa_string_with_suffix, m)?)?; m.add_function(wrap_pyfunction!(kaspa_wallet_core::python::tx::utils::create_transaction_py, m)?)?; m.add_function(wrap_pyfunction!(kaspa_wallet_core::python::tx::utils::create_transactions_py, m)?)?; m.add_function(wrap_pyfunction!(kaspa_wallet_core::python::signer::py_sign_transaction, m)?)?; diff --git a/wallet/core/src/python/mod.rs b/wallet/core/src/python/mod.rs index 46a9c4c2b..f58d5259a 100644 --- a/wallet/core/src/python/mod.rs +++ b/wallet/core/src/python/mod.rs @@ -1,2 +1,3 @@ pub mod signer; pub mod tx; +pub mod utils; diff --git a/wallet/core/src/python/utils.rs b/wallet/core/src/python/utils.rs new file mode 100644 index 000000000..0221acb90 --- /dev/null +++ b/wallet/core/src/python/utils.rs @@ -0,0 +1,20 @@ +use crate::result::Result; +use kaspa_consensus_core::network::NetworkType; +use pyo3::prelude::*; +use std::str::FromStr; + +#[pyfunction] +pub fn kaspa_to_sompi(kaspa: f64) -> u64 { + crate::utils::kaspa_to_sompi(kaspa) +} + +#[pyfunction] +pub fn sompi_to_kaspa(sompi: u64) -> f64 { + crate::utils::sompi_to_kaspa(sompi) +} + +#[pyfunction] +pub fn sompi_to_kaspa_string_with_suffix(sompi: u64, network: &str) -> Result { + let network_type = NetworkType::from_str(network)?; + Ok(crate::utils::sompi_to_kaspa_string_with_suffix(sompi, &network_type)) +}