Skip to content

Commit

Permalink
implement box size for ErgoBox c-bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
Alesfatalis committed May 20, 2024
1 parent b4c2526 commit 3f2a868
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions bindings/ergo-lib-c-core/src/ergo_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use std::convert::{TryFrom, TryInto};

use ergo_lib::ergotree_ir::chain::{self, ergo_box::NonMandatoryRegisters};
use ergo_lib::ergotree_ir::serialization::SigmaSerializable;

use crate::{
collections::{CollectionPtr, ConstCollectionPtr},
Expand Down Expand Up @@ -326,6 +327,13 @@ pub unsafe fn ergo_box_to_json_eip12(ergo_box_ptr: ConstErgoBoxPtr) -> Result<St
Ok(s)
}

/// Calculate serialized box size(in bytes)
pub unsafe fn ergo_box_bytes_size(ergo_box_ptr: ConstErgoBoxPtr) -> Result<usize, Error> {
let ergo_box = const_ptr_as_ref(ergo_box_ptr, "ergo_box_ptr")?;
let b = ergo_box.0.sigma_serialize_bytes()?.len();
Ok(b)
}

/// Pair of <value, tokens> for a box
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct ErgoBoxAssetsData(pub(crate) ergo_lib::wallet::box_selector::ErgoBoxAssetsData);
Expand Down
7 changes: 7 additions & 0 deletions bindings/ergo-lib-c/src/ergo_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,13 @@ pub unsafe extern "C" fn ergo_lib_ergo_box_to_json_eip12(
Error::c_api_from(res)
}

/// Calculate serialized box size(in bytes)
#[no_mangle]
pub unsafe extern "C" fn ergo_lib_ergo_box_bytes_size(ergo_box_ptr: ConstErgoBoxPtr) -> usize {
#[allow(clippy::unwrap_used)]
ergo_box_bytes_size(ergo_box_ptr).unwrap()
}

/// Drop `ErgoBox`
#[no_mangle]
pub unsafe extern "C" fn ergo_lib_ergo_box_delete(ptr: ErgoBoxPtr) {
Expand Down

0 comments on commit 3f2a868

Please sign in to comment.