#[cfg(feature = "backends_common")]
pub mod backends_common;
pub mod engines_api;
pub mod idempotency;
pub mod types;
use num_bigint::BigUint;
use num_traits::Zero;
use ring::digest::{digest, SHA256};
use types::{Convert, ConvertDetails};
pub fn scale_with_precision_loss(
amount: BigUint,
local_scale: u8,
remote_scale: u8,
) -> (BigUint, BigUint) {
let scaled = amount
.normalize_scale(ConvertDetails {
from: remote_scale,
to: local_scale,
})
.unwrap();
if local_scale < remote_scale {
let upscaled = scaled
.normalize_scale(ConvertDetails {
from: local_scale,
to: remote_scale,
})
.unwrap();
let precision_loss = if upscaled < amount {
amount - upscaled
} else {
Zero::zero()
};
(scaled, precision_loss)
} else {
(scaled, Zero::zero())
}
}
pub fn get_hash_of(preimage: &[u8]) -> [u8; 32] {
let mut hash = [0; 32];
hash.copy_from_slice(digest(&SHA256, preimage).as_ref());
hash
}