Skip to content

Commit

Permalink
Sompi/Kaspa conversion utils (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
smartgoo authored Oct 26, 2024
1 parent f8128e7 commit 676fd9d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
8 changes: 8 additions & 0 deletions python/examples/wallet_utils.py
Original file line number Diff line number Diff line change
@@ -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"))
6 changes: 6 additions & 0 deletions python/kaspa.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
3 changes: 3 additions & 0 deletions python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ cfg_if::cfg_if! {
m.add_class::<kaspa_wallet_core::python::tx::generator::pending::PendingTransaction>()?;
m.add_class::<kaspa_wallet_core::python::tx::generator::summary::GeneratorSummary>()?;
m.add_class::<kaspa_wallet_core::tx::payment::PaymentOutput>()?;
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)?)?;
Expand Down
1 change: 1 addition & 0 deletions wallet/core/src/python/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod signer;
pub mod tx;
pub mod utils;
20 changes: 20 additions & 0 deletions wallet/core/src/python/utils.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
let network_type = NetworkType::from_str(network)?;
Ok(crate::utils::sompi_to_kaspa_string_with_suffix(sompi, &network_type))
}

0 comments on commit 676fd9d

Please sign in to comment.