From 9450693cd7b7aebcea8408edc4d7767b20d9fff4 Mon Sep 17 00:00:00 2001 From: Armando Dutra Date: Sun, 26 Nov 2023 17:57:54 -0300 Subject: [PATCH] feat: decimal --- Cargo.lock | 2 +- Cargo.toml | 2 +- lib/web/rgb.ts | 18 +++- src/rgb.rs | 8 +- src/rgb/contract.rs | 8 +- src/rgb/structs.rs | 93 ++++++++++++++++----- src/structs.rs | 2 +- src/web.rs | 31 +++++++ tests/rgb.rs | 3 +- tests/rgb/integration/accept.rs | 8 +- tests/rgb/integration/batch.rs | 32 +++---- tests/rgb/integration/bench.rs | 14 ++-- tests/rgb/integration/dustless.rs | 8 +- tests/rgb/integration/fungibles.rs | 4 +- tests/rgb/integration/proxy.rs | 4 +- tests/rgb/integration/rbf.rs | 2 +- tests/rgb/integration/states.rs | 16 ++-- tests/rgb/integration/swaps.rs | 22 ++--- tests/rgb/integration/transfers.rs | 130 ++++++++++++++--------------- tests/rgb/integration/udas.rs | 6 +- tests/rgb/integration/utils.rs | 24 ++---- tests/rgb/unit/amount.rs | 30 +++++++ tests/rgb/web/inspect.rs | 6 +- tests/rgb/web/swaps.rs | 12 +-- tests/rgb/web/transfers.rs | 16 ++-- 25 files changed, 308 insertions(+), 193 deletions(-) create mode 100644 tests/rgb/unit/amount.rs diff --git a/Cargo.lock b/Cargo.lock index 126872af..541eae96 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -649,7 +649,7 @@ checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" [[package]] name = "bitmask-core" -version = "0.7.0-beta.8" +version = "0.7.0-beta.9" dependencies = [ "amplify", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index 9fe76c66..546e4a18 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "bitmask-core" -version = "0.7.0-beta.8" +version = "0.7.0-beta.9" authors = [ "Jose Diego Robles ", "Hunter Trujillo ", diff --git a/lib/web/rgb.ts b/lib/web/rgb.ts index 0de97bba..4f617793 100644 --- a/lib/web/rgb.ts +++ b/lib/web/rgb.ts @@ -205,6 +205,22 @@ export const importUdaData = async (request: MediaRequest): Promise => export const getMedia = async (mediaId: string): Promise => JSON.parse(await BMC.get_media_metadata(mediaId)); +export const contractAmount = (amount: bigint, precision: number): bigint => + JSON.parse(BMC.contract_amount(amount, precision).toString()); + +export const contractAmountStr = (amount: bigint, precision: number): String => + JSON.parse(BMC.contract_amount_str(amount, precision)); + +export const contractAmountParseStr = (amount: string, precision: number): String => + JSON.parse(BMC.contract_amount_parse_str(amount, precision).toString()); + +export const contractAmountParseValue = (amount: string, precision: number): bigint => + JSON.parse(BMC.contract_amount_parse_value(amount, precision).toString()); + +export const contractDecimalParseValue = (amount: string): bigint => + JSON.parse(BMC.contract_amount_parse_decimal_value(amount).toString()); + + // Core type interfaces based on structs defined within the bitmask-core Rust crate: // https://github.com/diba-io/bitmask-core/blob/development/src/structs.rs @@ -368,7 +384,7 @@ export interface ImportResponse { /// The user contract balance balance: bigint; /// The user contract balance - balance_normalized: number; + balanceNormalized: number; /// The contract allocations allocations: AllocationDetail[]; /// The contract state (multiple formats) diff --git a/src/rgb.rs b/src/rgb.rs index d8c4eb48..eb8c61e4 100644 --- a/src/rgb.rs +++ b/src/rgb.rs @@ -197,7 +197,7 @@ pub async fn issue_contract(sk: &str, request: IssueRequest) -> Result None, }; - let contract_amount = ContractAmount::with(supply, precision); + let contract_amount = ContractAmount::new(supply, precision); let contract = create_contract( &ticker, &name, @@ -470,7 +470,7 @@ async fn internal_create_invoice( let contr_id = ContractId::from_str(&contract_id).map_err(|_| InvoiceError::NoContract)?; let boilerplate = export_boilerplate(contr_id, stock).map_err(|_| InvoiceError::NoContract)?; - let invoice_amount = ContractAmount::from_raw(amount.to_string()); + let invoice_amount = ContractAmount::from_decimal_str(amount.to_string()); if invoice_amount.precision != boilerplate.precision { return Err(InvoiceError::WrongPrecision( boilerplate.precision, @@ -986,7 +986,7 @@ pub async fn create_seller_offer( expire_at, ); - let contract_amount = ContractAmount::from_raw(contract_amount).to_string(); + let contract_amount = ContractAmount::from_decimal_str(contract_amount).to_string(); let contract_amount = f64::from_str(&contract_amount).map_err(|_| RgbSwapError::WrongValue(contract_amount))?; @@ -1197,7 +1197,7 @@ pub async fn create_buyer_bid( } } - let invoice_amount = ContractAmount::with(asset_amount, asset_precision); + let invoice_amount = ContractAmount::new(asset_amount, asset_precision); let invoice_req = InvoiceRequest { iface, contract_id: contract_id.to_string(), diff --git a/src/rgb/contract.rs b/src/rgb/contract.rs index 8d60b4c5..39779cc4 100644 --- a/src/rgb/contract.rs +++ b/src/rgb/contract.rs @@ -225,9 +225,9 @@ where .sum(); } - let balance_normalised = ContractAmount::with(balance, specs.precision.into()).to_string(); - let balance_normalised = f64::from_str(&balance_normalised) - .map_err(|_| ExportContractError::WrongValue(contr_id.clone(), balance_normalised))?; + let balance_normalized = ContractAmount::new(balance, specs.precision.into()).to_string(); + let balance_normalized = f64::from_str(&balance_normalized) + .map_err(|_| ExportContractError::WrongValue(contr_id.clone(), balance_normalized))?; let mut supply = 0; for (index, (_, global_assign)) in contract_bindle.genesis.assignments.iter().enumerate() { @@ -336,7 +336,7 @@ where precision: specs.precision.into(), supply, balance, - balance_normalised, + balance_normalized, allocations, created: created.into(), contract: ContractFormats { diff --git a/src/rgb/structs.rs b/src/rgb/structs.rs index 2641792a..3de34393 100644 --- a/src/rgb/structs.rs +++ b/src/rgb/structs.rs @@ -5,6 +5,7 @@ use bp::Txid; use core::fmt::Display; use rgb::{RgbWallet, TerminalPath}; use std::{ + cmp::Ordering, collections::{BTreeMap, HashMap}, str::FromStr, }; @@ -45,6 +46,7 @@ impl FromStr for AddressAmount { } } +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] pub struct ContractAmount { pub int: u64, pub fract: u64, @@ -66,18 +68,57 @@ impl ContractAmount { /// /// ``` /// // Define the initial value - /// let amount = CoinAmount::with(5, 2); + /// use bitmask_core::rgb::structs::ContractAmount; + /// let amount = ContractAmount::new(100, 2); /// - /// assert_eq!(amount.int, 5); + /// assert_eq!(amount.int, 100); /// assert_eq!(amount.fract, 0); - /// assert_eq!(amount.to_value(), 500); - /// assert_eq!(amount.to_string(), "5"); + /// assert_eq!(amount.clone().to_value(), 10000); + /// assert_eq!(amount.to_string(), "100.00"); /// ``` pub fn new(value: u64, precision: u8) -> Self { let pow = 10_u64.pow(precision as u32); + let int = match precision.cmp(&0) { + Ordering::Less | Ordering::Equal => value, + Ordering::Greater => value / pow, + }; - let int = if value < pow { value } else { value / pow }; - let fract = if value < pow { 0 } else { value - int * pow }; + let fract = match value.cmp(&pow) { + Ordering::Less | Ordering::Equal => 0, + Ordering::Greater => value - int * pow, + }; + + ContractAmount { + int, + fract, + precision, + } + } + + /// Define a contract value. + /// + /// A value ([`u64`]) combine with precision ([`u8`]), load + /// a contract value, compabitle with rgb contract value. + /// + /// Remeber: All contract amounts are represents in [`u64`]. + /// The [`ContractAmount`] abstract the calculation. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// // Define the initial value + /// use bitmask_core::rgb::structs::ContractAmount; + /// let amount = ContractAmount::with(2, 50, 2); + /// + /// assert_eq!(amount.int, 2); + /// assert_eq!(amount.fract, 50); + /// assert_eq!(amount.clone().to_value(), 250); + /// assert_eq!(amount.to_string(), "2.50"); + /// ``` + pub fn with(value: u64, fract: u64, precision: u8) -> Self { + let int = value; ContractAmount { int, fract, @@ -99,14 +140,15 @@ impl ContractAmount { /// /// ``` /// // Define the initial value - /// let amount = ContractAmount::with(1100, 3); + /// use bitmask_core::rgb::structs::ContractAmount; + /// let amount = ContractAmount::new(1100, 3); /// /// assert_eq!(amount.int, 1); /// assert_eq!(amount.fract, 100); - /// assert_eq!(amount.to_value(), 1100); + /// assert_eq!(amount.clone().to_value(), 1100); /// assert_eq!(amount.to_string(), "1.100"); /// ``` - pub fn with(value: u64, precision: u8) -> Self { + pub fn load(value: u64, precision: u8) -> Self { let pow = 10_u64.pow(precision as u32); let int = value / pow; let fract = value - int * pow; @@ -117,7 +159,7 @@ impl ContractAmount { } } - /// Convert a raw string representation of the + /// Convert a raw u64 representation of the /// number in Contract Amount. /// /// A value ([`String`]) return a contract value, @@ -129,17 +171,19 @@ impl ContractAmount { /// /// ``` /// // Define the initial value - /// let amount = ContractAmount::from("1.100"); + /// use bitmask_core::rgb::structs::ContractAmount; + /// let amount = ContractAmount::from("1".to_string(), 3); /// - /// assert_eq!(amount.int, 1); - /// assert_eq!(amount.fract, 100); - /// assert_eq!(amount.to_value(), 1100); - /// assert_eq!(amount.to_string(), "1.100"); + /// assert_eq!(amount.int.clone(), 1); + /// assert_eq!(amount.fract, 0); + /// assert_eq!(amount.clone().to_value(), 1000); + /// assert_eq!(amount.to_string().clone(), "1.000"); /// ``` pub fn from(value: String, precision: u8) -> Self { - let mut fract = 0; + let int; + let mut fract; - let int = if value.contains('.') { + if value.contains('.') { let parts = value.split('.'); let collection: Vec<&str> = parts.collect(); @@ -150,9 +194,11 @@ impl ContractAmount { fract *= pow; } - collection[0].parse().unwrap() + int = collection[0].parse().unwrap(); } else { - value.parse().unwrap() + let unsafe_contract_amount = Self::load(value.parse().unwrap(), precision); + fract = unsafe_contract_amount.fract; + int = unsafe_contract_amount.int }; ContractAmount { @@ -162,7 +208,7 @@ impl ContractAmount { } } - /// Convert a raw string representation of the + /// Convert a decimal string representation of the /// number in Contract Amount. /// /// A value ([`String`]) return a contract value, @@ -174,14 +220,15 @@ impl ContractAmount { /// /// ``` /// // Define the initial value - /// let amount = ContractAmount::from_raw("1.100"); + /// use bitmask_core::rgb::structs::ContractAmount; + /// let amount = ContractAmount::from_decimal_str("1.100".to_string()); /// /// assert_eq!(amount.int, 1); /// assert_eq!(amount.fract, 100); - /// assert_eq!(amount.to_value(), 1100); + /// assert_eq!(amount.clone().to_value(), 1100); /// assert_eq!(amount.to_string(), "1.100"); /// ``` - pub fn from_raw(value: String) -> Self { + pub fn from_decimal_str(value: String) -> Self { let mut fract = 0; let mut precision = 0; diff --git a/src/structs.rs b/src/structs.rs index 35b1caac..cb88c602 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -442,7 +442,7 @@ pub struct ContractResponse { /// Current balance pub balance: u64, /// Current balance (Humanized) - pub balance_normalised: f64, + pub balance_normalized: f64, /// The contract allocations pub allocations: Vec, /// The contract state (multiple formats) diff --git a/src/web.rs b/src/web.rs index 7278a2bd..ecea196e 100644 --- a/src/web.rs +++ b/src/web.rs @@ -5,6 +5,7 @@ use serde::de::DeserializeOwned; use wasm_bindgen::prelude::*; use wasm_bindgen_futures::{future_to_promise, JsFuture}; +use crate::rgb::structs::ContractAmount; use crate::structs::{ AcceptRequest, FullRgbTransferRequest, ImportRequest, InvoiceRequest, IssueRequest, MediaRequest, PsbtRequest, PublishPsbtRequest, ReIssueRequest, RgbBidRequest, RgbOfferRequest, @@ -995,6 +996,36 @@ pub mod rgb { } }) } + + #[wasm_bindgen] + pub fn contract_amount(amount: u64, precision: u8) -> u64 { + set_panic_hook(); + ContractAmount::new(amount, precision).to_value() + } + + #[wasm_bindgen] + pub fn contract_amount_str(amount: u64, precision: u8) -> String { + set_panic_hook(); + ContractAmount::new(amount, precision).to_string() + } + + #[wasm_bindgen] + pub fn contract_amount_parse_str(amount: String, precision: u8) -> String { + set_panic_hook(); + ContractAmount::from(amount, precision).to_string() + } + + #[wasm_bindgen] + pub fn contract_amount_parse_value(amount: String, precision: u8) -> u64 { + set_panic_hook(); + ContractAmount::from(amount, precision).to_value() + } + + #[wasm_bindgen] + pub fn contract_amount_parse_decimal_value(amount: String) -> u64 { + set_panic_hook(); + ContractAmount::from_decimal_str(amount).to_value() + } } pub mod lightning { diff --git a/tests/rgb.rs b/tests/rgb.rs index 4841003d..dd323ae8 100644 --- a/tests/rgb.rs +++ b/tests/rgb.rs @@ -1,6 +1,7 @@ mod rgb { mod unit { + mod amount; mod invoice; mod issue; mod psbt; @@ -36,7 +37,7 @@ mod rgb { mod web { mod contracts; mod imports; - // mod inspect; + mod inspect; mod proxy; mod stl_ids; mod stl_load; diff --git a/tests/rgb/integration/accept.rs b/tests/rgb/integration/accept.rs index 80b54d9c..b6ecfefd 100644 --- a/tests/rgb/integration/accept.rs +++ b/tests/rgb/integration/accept.rs @@ -53,7 +53,7 @@ pub async fn allow_save_read_remove_transfers() -> Result<()> { let issuer_resp = issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, true, None, @@ -68,7 +68,7 @@ pub async fn allow_save_read_remove_transfers() -> Result<()> { let owner_invoice = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 2.00, + ContractAmount::with(2, 0, issuer_resp.precision), owner_keys.clone(), None, Some(issuer_resp.clone().contract.strict), @@ -214,7 +214,7 @@ pub async fn accept_all_transfers() -> Result<()> { let issuer_resp = issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, true, None, @@ -229,7 +229,7 @@ pub async fn accept_all_transfers() -> Result<()> { let owner_invoice = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 2.00, + ContractAmount::with(2, 0, issuer_resp.precision), owner_keys.clone(), None, Some(issuer_resp.clone().contract.strict), diff --git a/tests/rgb/integration/batch.rs b/tests/rgb/integration/batch.rs index 12c12735..72b66bb5 100644 --- a/tests/rgb/integration/batch.rs +++ b/tests/rgb/integration/batch.rs @@ -35,7 +35,7 @@ pub async fn create_strict_transfer() -> Result<()> { let fungibles_resp = issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, true, None, @@ -100,7 +100,7 @@ pub async fn create_strict_transfer() -> Result<()> { let owner_resp = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), owner_keys.clone(), None, Some(issuer_resp.clone().contract.strict), @@ -152,7 +152,7 @@ pub async fn create_transfer_rbf() -> Result<()> { let fungibles_resp = issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, true, None, @@ -217,7 +217,7 @@ pub async fn create_transfer_rbf() -> Result<()> { let owner_resp = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), owner_keys.clone(), None, Some(issuer_resp.clone().contract.strict), @@ -305,7 +305,7 @@ pub async fn create_batch_transfer() -> Result<()> { let fungibles_resp = issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, true, None, @@ -369,7 +369,7 @@ pub async fn create_batch_transfer() -> Result<()> { let owner_resp = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), owner_keys.clone(), None, Some(issuer_resp.clone().contract.strict), @@ -391,7 +391,7 @@ pub async fn create_batch_transfer() -> Result<()> { let other_resp = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 2.0, + ContractAmount::with(2, 0, issuer_resp.precision), other_keys.clone(), None, Some(issuer_resp.clone().contract.strict), @@ -515,9 +515,9 @@ pub async fn create_batch_transfer() -> Result<()> { // println!("Owner Contract: \n {:#?}", owner_contracts.allocations); // println!("other Contract: \n {:#?}", other_contracts.allocations); - assert_eq!(2.0, issuer_contracts.balance_normalised); - assert_eq!(1.0, owner_contracts.balance_normalised); - assert_eq!(2.0, other_contracts.balance_normalised); + assert_eq!(2., issuer_contracts.balance_normalized); + assert_eq!(1., owner_contracts.balance_normalized); + assert_eq!(2., other_contracts.balance_normalized); Ok(()) } @@ -532,7 +532,7 @@ pub async fn create_transfer_skipping_invalid_states() -> Result<()> { let fungibles_resp = issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, true, None, @@ -597,7 +597,7 @@ pub async fn create_transfer_skipping_invalid_states() -> Result<()> { let owner_resp = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), owner_keys.clone(), None, Some(issuer_resp.clone().contract.strict), @@ -627,7 +627,7 @@ pub async fn create_transfer_skipping_invalid_states() -> Result<()> { // 7. Check Contract State let contract = get_contract(issuer_sk, &issuer_resp.contract_id).await?; - assert_eq!(4., contract.balance_normalised); + assert_eq!(4., contract.balance_normalized); // 8. Create PSBT (Second Transaction) let psbt_resp = create_new_psbt_v2( @@ -672,7 +672,7 @@ pub async fn create_transfer_skipping_invalid_states() -> Result<()> { let owner_resp = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 2.0, + ContractAmount::with(2, 0, issuer_resp.precision), owner_keys.clone(), None, Some(issuer_resp.clone().contract.strict), @@ -724,10 +724,10 @@ pub async fn create_transfer_skipping_invalid_states() -> Result<()> { // 15. Check Contract State let contract = get_contract(&owner_sk, &issuer_resp.contract_id).await?; - assert_eq!(3., contract.balance_normalised); + assert_eq!(3., contract.balance_normalized); let contract = get_contract(issuer_sk, &issuer_resp.contract_id).await?; - assert_eq!(2., contract.balance_normalised); + assert_eq!(2., contract.balance_normalized); Ok(()) } diff --git a/tests/rgb/integration/bench.rs b/tests/rgb/integration/bench.rs index de57e76c..8e07c443 100644 --- a/tests/rgb/integration/bench.rs +++ b/tests/rgb/integration/bench.rs @@ -35,7 +35,7 @@ pub async fn measure_batch_operation() -> Result<()> { let fungibles_resp = issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, true, None, @@ -100,7 +100,7 @@ pub async fn measure_batch_operation() -> Result<()> { let owner_resp = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), owner_keys.clone(), None, Some(issuer_resp.clone().contract.strict), @@ -153,7 +153,7 @@ pub async fn create_transfer_rbf() -> Result<()> { let fungibles_resp = issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, true, None, @@ -218,7 +218,7 @@ pub async fn create_transfer_rbf() -> Result<()> { let owner_resp = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), owner_keys.clone(), None, Some(issuer_resp.clone().contract.strict), @@ -306,7 +306,7 @@ pub async fn create_batch_transfer() -> Result<()> { let fungibles_resp = issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, true, None, @@ -370,7 +370,7 @@ pub async fn create_batch_transfer() -> Result<()> { let owner_resp = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), owner_keys.clone(), None, Some(issuer_resp.clone().contract.strict), @@ -392,7 +392,7 @@ pub async fn create_batch_transfer() -> Result<()> { let other_resp = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 2.0, + ContractAmount::with(2, 0, issuer_resp.precision), other_keys.clone(), None, Some(issuer_resp.clone().contract.strict), diff --git a/tests/rgb/integration/dustless.rs b/tests/rgb/integration/dustless.rs index a9ddf7ea..8437ab9b 100644 --- a/tests/rgb/integration/dustless.rs +++ b/tests/rgb/integration/dustless.rs @@ -27,7 +27,7 @@ async fn create_dustless_transfer_with_fee_value() -> anyhow::Result<()> { let issuer_resp = issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, true, None, @@ -40,7 +40,7 @@ async fn create_dustless_transfer_with_fee_value() -> anyhow::Result<()> { let owner_resp = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), owner_keys.clone(), None, Some(issuer_resp.clone().contract.strict), @@ -180,7 +180,7 @@ async fn create_dustless_transfer_with_fee_rate() -> anyhow::Result<()> { let issuer_resp = issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, false, None, @@ -195,7 +195,7 @@ async fn create_dustless_transfer_with_fee_rate() -> anyhow::Result<()> { let owner_resp = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), owner_keys.clone(), None, Some(issuer_resp.clone().contract.strict), diff --git a/tests/rgb/integration/fungibles.rs b/tests/rgb/integration/fungibles.rs index c035c30d..40630969 100644 --- a/tests/rgb/integration/fungibles.rs +++ b/tests/rgb/integration/fungibles.rs @@ -24,7 +24,7 @@ async fn accept_fungible_transfer() -> anyhow::Result<()> { let issuer_resp = issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, true, None, @@ -38,7 +38,7 @@ async fn accept_fungible_transfer() -> anyhow::Result<()> { let owner_resp = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), owner_keys.clone(), None, Some(issuer_resp.clone().contract.legacy), diff --git a/tests/rgb/integration/proxy.rs b/tests/rgb/integration/proxy.rs index 3cca198e..c24aa3d7 100644 --- a/tests/rgb/integration/proxy.rs +++ b/tests/rgb/integration/proxy.rs @@ -30,7 +30,7 @@ pub async fn store_and_retrieve_transfer_by_proxy() -> Result<()> { let fungibles_resp = issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, true, None, @@ -77,7 +77,7 @@ pub async fn store_and_retrieve_transfer_by_proxy() -> Result<()> { let owner_resp = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), owner_keys.clone(), None, Some(issuer_resp.clone().contract.strict), diff --git a/tests/rgb/integration/rbf.rs b/tests/rgb/integration/rbf.rs index ef60953f..d8328cfe 100644 --- a/tests/rgb/integration/rbf.rs +++ b/tests/rgb/integration/rbf.rs @@ -28,7 +28,7 @@ pub async fn create_simple_rbf_bitcoin_transfer() -> Result<()> { let issuer_resp = issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, true, None, diff --git a/tests/rgb/integration/states.rs b/tests/rgb/integration/states.rs index a9040e00..0635a1ce 100644 --- a/tests/rgb/integration/states.rs +++ b/tests/rgb/integration/states.rs @@ -44,7 +44,7 @@ async fn check_fungible_allocations() -> anyhow::Result<()> { let issuer_resp = issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, true, None, @@ -58,7 +58,7 @@ async fn check_fungible_allocations() -> anyhow::Result<()> { let owner_resp = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), owner_keys.clone(), None, Some(issuer_resp.clone().contract.legacy), @@ -108,7 +108,7 @@ async fn check_fungible_allocations() -> anyhow::Result<()> { let contract_id = &issuer_resp.contract_id; let resp = get_contract(&issuer_sk, contract_id).await; assert!(resp.is_ok()); - assert_eq!(4.0, resp?.balance_normalised); + assert_eq!(4.0, resp?.balance_normalized); // 6. Create Watcher (Owner Side) let watcher_name = "default"; @@ -123,7 +123,7 @@ async fn check_fungible_allocations() -> anyhow::Result<()> { let contract_id = &issuer_resp.contract_id; let resp = get_contract(&owner_sk, contract_id).await; assert!(resp.is_ok()); - assert_eq!(1.0, resp?.balance_normalised); + assert_eq!(1., resp?.balance_normalized); Ok(()) } @@ -139,7 +139,7 @@ async fn check_uda_allocations() -> anyhow::Result<()> { let issuer_resp = issuer_issue_contract_v2( 1, "RGB21", - ContractAmount::new(1, 0).to_value(), + ContractAmount::with(1, 0, 0).to_value(), false, true, meta, @@ -152,7 +152,7 @@ async fn check_uda_allocations() -> anyhow::Result<()> { let owner_resp = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), owner_keys.clone(), None, Some(issuer_resp.clone().contract.legacy), @@ -204,7 +204,7 @@ async fn check_uda_allocations() -> anyhow::Result<()> { let contract_id = &issuer_resp.contract_id; let resp = get_contract(&issuer_sk, contract_id).await; assert!(resp.is_ok()); - assert_eq!(0.0, resp?.balance_normalised); + assert_eq!(0.0, resp?.balance_normalized); // 6. Create Watcher (Owner Side) let watcher_name = "default"; @@ -219,7 +219,7 @@ async fn check_uda_allocations() -> anyhow::Result<()> { let contract_id = &issuer_resp.contract_id; let resp = get_contract(&owner_sk, contract_id).await; assert!(resp.is_ok()); - assert_eq!(1.0, resp?.balance_normalised); + assert_eq!(1., resp?.balance_normalized); Ok(()) } diff --git a/tests/rgb/integration/swaps.rs b/tests/rgb/integration/swaps.rs index 77d080e1..baf81012 100644 --- a/tests/rgb/integration/swaps.rs +++ b/tests/rgb/integration/swaps.rs @@ -101,7 +101,7 @@ async fn create_scriptless_swap() -> anyhow::Result<()> { let issuer_resp = issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, false, None, @@ -137,7 +137,7 @@ async fn create_scriptless_swap() -> anyhow::Result<()> { .naive_utc() .timestamp(); - let asset_amount = ContractAmount::with(contract_amount, precision).to_string(); + let asset_amount = ContractAmount::new(contract_amount, precision).to_string(); let seller_swap_req = RgbOfferRequest { contract_id: contract_id.clone(), iface: iface.clone(), @@ -238,12 +238,12 @@ async fn create_scriptless_swap() -> anyhow::Result<()> { // 15. Retrieve Contract (Buyer Side) let resp = get_contract(&buyer_sk, &contract_id).await; assert!(resp.is_ok()); - assert_eq!(4.0, resp?.balance_normalised); + assert_eq!(4.0, resp?.balance_normalized); // 14. Retrieve Contract (Seller Side) let resp = get_contract(&seller_sk, &contract_id).await; assert!(resp.is_ok()); - assert_eq!(1., resp?.balance_normalised); + assert_eq!(1., resp?.balance_normalized); Ok(()) } @@ -330,7 +330,7 @@ async fn create_scriptless_swap_for_uda() -> anyhow::Result<()> { let issuer_resp = issuer_issue_contract_v2( 1, "RGB21", - ContractAmount::new(1, 0).to_value(), + ContractAmount::with(1, 0, 0).to_value(), false, false, Some(metadata), @@ -460,12 +460,12 @@ async fn create_scriptless_swap_for_uda() -> anyhow::Result<()> { // 11. Retrieve Contract (Seller Side) let resp = get_contract(&seller_sk, &contract_id).await; assert!(resp.is_ok()); - assert_eq!(0., resp?.balance_normalised); + assert_eq!(0., resp?.balance_normalized); // 12. Retrieve Contract (Buyer Side) let resp = get_contract(&buyer_sk, &contract_id).await; assert!(resp.is_ok()); - assert_eq!(1., resp?.balance_normalised); + assert_eq!(1., resp?.balance_normalized); // 13. Verify transfers (Seller Side) let resp = verify_transfers(&seller_sk).await; @@ -556,7 +556,7 @@ async fn create_presig_scriptless_swap() -> anyhow::Result<()> { let issuer_resp = issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, false, None, @@ -592,7 +592,7 @@ async fn create_presig_scriptless_swap() -> anyhow::Result<()> { .naive_utc() .timestamp(); - let asset_amount = ContractAmount::with(contract_amount, precision).to_string(); + let asset_amount = ContractAmount::new(contract_amount, precision).to_string(); let seller_swap_req = RgbOfferRequest { contract_id: contract_id.clone(), iface: iface.clone(), @@ -708,12 +708,12 @@ async fn create_presig_scriptless_swap() -> anyhow::Result<()> { // 14. Retrieve Contract (Buyer Side) let resp = get_contract(&buyer_sk, &contract_id).await; assert!(resp.is_ok()); - assert_eq!(4., resp?.balance_normalised); + assert_eq!(4., resp?.balance_normalized); // 13. Retrieve Contract (Seller Side) let resp = get_contract(&seller_sk, &contract_id).await; assert!(resp.is_ok()); - assert_eq!(1., resp?.balance_normalised); + assert_eq!(1., resp?.balance_normalized); Ok(()) } diff --git a/tests/rgb/integration/transfers.rs b/tests/rgb/integration/transfers.rs index 01513d61..26f1011d 100644 --- a/tests/rgb/integration/transfers.rs +++ b/tests/rgb/integration/transfers.rs @@ -51,7 +51,7 @@ async fn create_conseq_transfers_from_issuer() -> anyhow::Result<()> { let issuer_resp = issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, true, None, @@ -66,7 +66,7 @@ async fn create_conseq_transfers_from_issuer() -> anyhow::Result<()> { let owner_invoice = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 2.5, + ContractAmount::with(2, 50, issuer_resp.precision), owner_keys.clone(), None, Some(issuer_resp.clone().contract.strict), @@ -117,13 +117,13 @@ async fn create_conseq_transfers_from_issuer() -> anyhow::Result<()> { assert!(resp.is_ok()); let issuer_contract = resp?; - assert_eq!(2.5, issuer_contract.balance_normalised); + assert_eq!(2.5, issuer_contract.balance_normalized); let resp = get_contract(&owner_sk, contract_id).await; assert!(resp.is_ok()); let owner_contract = resp?; - assert_eq!(2.5, owner_contract.balance_normalised); + assert_eq!(2.5, owner_contract.balance_normalized); // 7. reate new Invoices (Issuer Side) let address = watcher_next_address(&owner_sk, watcher_name, "RGB20").await?; @@ -134,7 +134,7 @@ async fn create_conseq_transfers_from_issuer() -> anyhow::Result<()> { let invoice_2 = &create_new_invoice_v2( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), &utxos.utxos[0].outpoint, owner_keys.clone(), None, @@ -205,7 +205,7 @@ async fn create_conseq_transfers_from_owner() -> anyhow::Result<()> { let issuer_resp = issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, true, None, @@ -218,7 +218,7 @@ async fn create_conseq_transfers_from_owner() -> anyhow::Result<()> { let owner_resp = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 2.0, + ContractAmount::with(2, 0, issuer_resp.precision), owner_keys.clone(), None, Some(issuer_resp.clone().contract.strict), @@ -280,13 +280,13 @@ async fn create_conseq_transfers_from_owner() -> anyhow::Result<()> { assert!(resp.is_ok()); let owner_contract = resp?; - assert_eq!(2., owner_contract.balance_normalised); + assert_eq!(2., owner_contract.balance_normalized); // 7. Create Invoice (Issuer Side) let issuer_invoice_resp = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), issuer_keys.clone(), None, None, @@ -338,13 +338,13 @@ async fn create_conseq_transfers_from_owner() -> anyhow::Result<()> { assert!(resp.is_ok()); let owner_contract = resp?; - assert_eq!(1., owner_contract.balance_normalised); + assert_eq!(1., owner_contract.balance_normalized); // 11. Create Invoice (Issuer Side) let issuer_invoice_resp = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), issuer_keys.clone(), None, None, @@ -357,7 +357,7 @@ async fn create_conseq_transfers_from_owner() -> anyhow::Result<()> { assert!(resp.is_ok()); let owner_contract = resp?; - assert_eq!(1., owner_contract.balance_normalised); + assert_eq!(1., owner_contract.balance_normalized); let contract_utxo = owner_contract .allocations @@ -403,7 +403,7 @@ async fn create_conseq_transfers_from_owner() -> anyhow::Result<()> { assert!(resp.is_ok()); let owner_contract = resp?; - assert_eq!(0., owner_contract.balance_normalised); + assert_eq!(0., owner_contract.balance_normalized); Ok(()) } @@ -445,7 +445,7 @@ async fn create_transfers_from_3_owners() -> anyhow::Result<()> { let issuer_resp = issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, true, None, @@ -460,7 +460,7 @@ async fn create_transfers_from_3_owners() -> anyhow::Result<()> { let owner_invoice = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 2.0, + ContractAmount::with(2, 0, issuer_resp.precision), owner_keys.clone(), None, Some(issuer_resp.clone().contract.strict), @@ -511,19 +511,19 @@ async fn create_transfers_from_3_owners() -> anyhow::Result<()> { assert!(resp.is_ok()); let issuer_contract = resp?; - assert_eq!(3., issuer_contract.balance_normalised); + assert_eq!(3., issuer_contract.balance_normalized); let resp = get_contract(&owner_sk, contract_id).await; assert!(resp.is_ok()); let owner_contract = resp?; - assert_eq!(2., owner_contract.balance_normalised); + assert_eq!(2., owner_contract.balance_normalized); // 7. Create 2 Invoices (Another Owner Side) let another_invoice_1 = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), another_owner_keys.clone(), None, Some(issuer_resp.clone().contract.strict), @@ -533,7 +533,7 @@ async fn create_transfers_from_3_owners() -> anyhow::Result<()> { let another_invoice_2 = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), another_owner_keys.clone(), None, None, @@ -627,19 +627,19 @@ async fn create_transfers_from_3_owners() -> anyhow::Result<()> { assert!(resp.is_ok()); let issuer_contract = resp?; - assert_eq!(2., issuer_contract.balance_normalised); + assert_eq!(2., issuer_contract.balance_normalized); let resp = get_contract(&owner_sk, contract_id).await; assert!(resp.is_ok()); let owner_contract = resp?; - assert_eq!(1., owner_contract.balance_normalised); + assert_eq!(1., owner_contract.balance_normalized); let resp = get_contract(&another_owner_sk, contract_id).await; assert!(resp.is_ok()); let another_owner_contract = resp?; - assert_eq!(2., another_owner_contract.balance_normalised); + assert_eq!(2., another_owner_contract.balance_normalized); Ok(()) } @@ -682,7 +682,7 @@ async fn create_transfers_between_2_owners() -> anyhow::Result<()> { let issuer_resp = issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, true, None, @@ -697,7 +697,7 @@ async fn create_transfers_between_2_owners() -> anyhow::Result<()> { let owner_invoice = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 2.0, + ContractAmount::with(2, 0, issuer_resp.precision), owner_keys.clone(), None, Some(issuer_resp.clone().contract.strict), @@ -750,7 +750,7 @@ async fn create_transfers_between_2_owners() -> anyhow::Result<()> { assert!(resp.is_ok()); let issuer_contract = resp?; - assert_eq!(3., issuer_contract.balance_normalised); + assert_eq!(3., issuer_contract.balance_normalized); let resp = get_contract(&owner_sk, contract_id).await; assert!(resp.is_ok()); @@ -869,25 +869,25 @@ async fn create_transfers_between_2_owners() -> anyhow::Result<()> { assert!(resp.is_ok()); let issuer_contract = resp?; - assert_eq!(2., issuer_contract.balance_normalised); + assert_eq!(2., issuer_contract.balance_normalized); let resp = get_contract(&owner_sk, contract_id).await; assert!(resp.is_ok()); let owner_contract = resp?; - assert_eq!(1., owner_contract.balance_normalised); + assert_eq!(1., owner_contract.balance_normalized); let resp = get_contract(&another_owner_sk, contract_id).await; assert!(resp.is_ok()); let another_owner_contract = resp?; - assert_eq!(2., another_owner_contract.balance_normalised); + assert_eq!(2., another_owner_contract.balance_normalized); // 12. Generate Invoice (Issuer Side) let issuer_invoice = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 2.0, + ContractAmount::with(2, 0, issuer_resp.precision), issuer_keys.clone(), None, Some(issuer_resp.clone().contract.strict), @@ -934,7 +934,7 @@ async fn create_transfers_between_2_owners() -> anyhow::Result<()> { assert!(resp.is_ok()); let another_owner_contract = resp?; - assert_eq!(0., another_owner_contract.balance_normalised); + assert_eq!(0., another_owner_contract.balance_normalized); Ok(()) } @@ -977,7 +977,7 @@ async fn create_transfers_with_2_transitions() -> anyhow::Result<()> { let issuer_resp = issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, true, None, @@ -992,7 +992,7 @@ async fn create_transfers_with_2_transitions() -> anyhow::Result<()> { let owner_invoice = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 2.0, + ContractAmount::with(2, 0, issuer_resp.precision), owner_keys.clone(), None, Some(issuer_resp.clone().contract.strict), @@ -1041,13 +1041,13 @@ async fn create_transfers_with_2_transitions() -> anyhow::Result<()> { assert!(resp.is_ok()); let issuer_contract = resp?; - assert_eq!(3., issuer_contract.balance_normalised); + assert_eq!(3., issuer_contract.balance_normalized); let resp = get_contract(&owner_sk, contract_id).await; assert!(resp.is_ok()); let owner_contract = resp?; - assert_eq!(2., owner_contract.balance_normalised); + assert_eq!(2., owner_contract.balance_normalized); // 7. reate new Invoices (Issuer Side) let address = watcher_next_address(&another_owner_sk, watcher_name, "RGB20").await?; @@ -1058,7 +1058,7 @@ async fn create_transfers_with_2_transitions() -> anyhow::Result<()> { let another_invoice_1 = &create_new_invoice_v2( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), &utxos.utxos[0].outpoint, another_owner_keys.clone(), None, @@ -1069,7 +1069,7 @@ async fn create_transfers_with_2_transitions() -> anyhow::Result<()> { let another_invoice_2 = &create_new_invoice_v2( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), &utxos.utxos[1].outpoint, another_owner_keys.clone(), None, @@ -1164,25 +1164,25 @@ async fn create_transfers_with_2_transitions() -> anyhow::Result<()> { assert!(resp.is_ok()); let issuer_contract = resp?; - assert_eq!(2., issuer_contract.balance_normalised); + assert_eq!(2., issuer_contract.balance_normalized); let resp = get_contract(&owner_sk, contract_id).await; assert!(resp.is_ok()); let owner_contract = resp?; - assert_eq!(1., owner_contract.balance_normalised); + assert_eq!(1., owner_contract.balance_normalized); let resp = get_contract(&another_owner_sk, contract_id).await; assert!(resp.is_ok()); let another_owner_contract = resp?; - assert_eq!(2., another_owner_contract.balance_normalised); + assert_eq!(2., another_owner_contract.balance_normalized); // 12. Generate Invoice (Issuer Side) let issuer_invoice = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 2.0, + ContractAmount::with(2, 0, issuer_resp.precision), issuer_keys.clone(), None, Some(issuer_resp.clone().contract.strict), @@ -1239,7 +1239,7 @@ async fn create_transfers_with_2_transitions() -> anyhow::Result<()> { assert!(resp.is_ok()); let another_owner_contract = resp?; - assert_eq!(0., another_owner_contract.balance_normalised); + assert_eq!(0., another_owner_contract.balance_normalized); Ok(()) } @@ -1261,7 +1261,7 @@ async fn create_transfers_with_2_contracts_in_same_utxo() -> anyhow::Result<()> let issue_contracts_resp = &issuer_issue_contract_v2( 2, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, true, None, @@ -1289,7 +1289,7 @@ async fn create_transfers_with_2_contracts_in_same_utxo() -> anyhow::Result<()> let owner_resp = &create_new_invoice_v2( &issue_contract_a_resp.contract_id, &issue_contract_a_resp.iface, - 1.0, + ContractAmount::with(1, 0, issue_contract_a_resp.precision), &owner_utxos.utxos[0].outpoint, owner_keys.clone(), None, @@ -1339,19 +1339,19 @@ async fn create_transfers_with_2_contracts_in_same_utxo() -> anyhow::Result<()> assert!(resp.is_ok()); let issuer_contract = resp?; - assert_eq!(4., issuer_contract.balance_normalised); + assert_eq!(4., issuer_contract.balance_normalized); let resp = get_contract(&owner_sk, contract_id).await; assert!(resp.is_ok()); let owner_contract = resp?; - assert_eq!(1., owner_contract.balance_normalised); + assert_eq!(1., owner_contract.balance_normalized); // 5. Create Second Invoice (Owner Side) let owner_resp = &create_new_invoice_v2( &issue_contract_b_resp.contract_id, &issue_contract_b_resp.iface, - 2.0, + ContractAmount::with(1, 0, issue_contract_b_resp.precision), &owner_utxos.utxos[0].outpoint, owner_keys.clone(), None, @@ -1389,7 +1389,7 @@ async fn create_transfers_with_2_contracts_in_same_utxo() -> anyhow::Result<()> // 7. Accept Consig (Both Side) let request = AcceptRequest { consignment: transfer_resp.clone().consig, - force: true, + force: false, }; let resp = accept_transfer(&issuer_sk, request.clone()).await; assert!(resp.is_ok()); @@ -1406,13 +1406,13 @@ async fn create_transfers_with_2_contracts_in_same_utxo() -> anyhow::Result<()> assert!(resp.is_ok()); let issuer_contract = resp?; - assert_eq!(3., issuer_contract.balance_normalised); + assert_eq!(4., issuer_contract.balance_normalized); let resp = get_contract(&owner_sk, contract_id).await; assert!(resp.is_ok()); let owner_contract = resp?; - assert_eq!(2., owner_contract.balance_normalised); + assert_eq!(1., owner_contract.balance_normalized); Ok(()) } @@ -1433,7 +1433,7 @@ async fn create_transfers_with_2_ifaces_in_same_utxo() -> anyhow::Result<()> { let issue_contracts_resp = &issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(5, 2).to_value(), + ContractAmount::with(5, 0, 2).to_value(), false, true, None, @@ -1448,7 +1448,7 @@ async fn create_transfers_with_2_ifaces_in_same_utxo() -> anyhow::Result<()> { let issue_contracts_resp = &issuer_issue_contract_v2( 1, "RGB21", - ContractAmount::new(1, 0).to_value(), + ContractAmount::with(1, 0, 0).to_value(), false, true, meta, @@ -1478,7 +1478,7 @@ async fn create_transfers_with_2_ifaces_in_same_utxo() -> anyhow::Result<()> { let owner_resp = &create_new_invoice_v2( &issue_contract_a_resp.contract_id, &issue_contract_a_resp.iface, - 1.0, + ContractAmount::with(1, 0, issue_contract_a_resp.precision), &owner_utxos.utxos[0].outpoint, owner_keys.clone(), None, @@ -1528,20 +1528,20 @@ async fn create_transfers_with_2_ifaces_in_same_utxo() -> anyhow::Result<()> { assert!(resp.is_ok()); let issuer_contract = resp?; - assert_eq!(4., issuer_contract.balance_normalised); + assert_eq!(4., issuer_contract.balance_normalized); let resp = get_contract(&owner_sk, contract_id).await; assert!(resp.is_ok()); let owner_contract = resp?; - assert_eq!(1., owner_contract.balance_normalised); + assert_eq!(1., owner_contract.balance_normalized); // 5. Create Second Invoice (Owner Side) let owner_utxos = watcher_unspent_utxos(&owner_sk, watcher_name, "RGB21").await?; let owner_resp = &create_new_invoice_v2( &issue_contract_b_resp.contract_id, &issue_contract_b_resp.iface, - 1.0, + ContractAmount::with(1, 0, issue_contract_b_resp.precision), &owner_utxos.utxos[0].outpoint, owner_keys.clone(), None, @@ -1597,13 +1597,13 @@ async fn create_transfers_with_2_ifaces_in_same_utxo() -> anyhow::Result<()> { assert!(resp.is_ok()); let issuer_contract = resp?; - assert_eq!(0., issuer_contract.balance_normalised); + assert_eq!(0., issuer_contract.balance_normalized); let resp = get_contract(&owner_sk, contract_id).await; assert!(resp.is_ok()); let owner_contract = resp?; - assert_eq!(1., owner_contract.balance_normalised); + assert_eq!(1., owner_contract.balance_normalized); Ok(()) } @@ -1640,7 +1640,7 @@ async fn allow_fungible_full_transfer_op() -> anyhow::Result<()> { let owner_resp = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), owner_keys.clone(), None, Some(issuer_resp.clone().contract.strict), @@ -1696,7 +1696,7 @@ async fn allow_uda_full_transfer_op() -> anyhow::Result<()> { let issuer_resp = issuer_issue_contract_v2( 1, "RGB21", - ContractAmount::new(1, 0).to_value(), + ContractAmount::with(1, 0, 0).to_value(), false, true, meta, @@ -1711,7 +1711,7 @@ async fn allow_uda_full_transfer_op() -> anyhow::Result<()> { let owner_resp = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), owner_keys.clone(), None, None, @@ -1879,7 +1879,7 @@ async fn allow_consecutive_full_transfer_bidirectional() -> anyhow::Result<()> { let wallet_b_invoice = create_new_invoice_v2( &contract_id.to_string(), &iface.to_string(), - 1_000.00, + ContractAmount::new(1_000, 2), &utxo_unspent.outpoint.to_string(), wallet_b.clone(), None, @@ -1943,7 +1943,7 @@ async fn allow_consecutive_full_transfer_bidirectional() -> anyhow::Result<()> { let wallet_a_invoice = create_new_invoice_v2( &contract_id.to_string(), &iface.to_string(), - 50.00, + ContractAmount::new(50, 2), &utxo_unspent.outpoint.to_string(), wallet_a.clone(), None, @@ -2043,7 +2043,7 @@ async fn allow_save_transfer_and_verify() -> anyhow::Result<()> { let issuer_resp = issuer_issue_contract_v2( 1, "RGB20", - ContractAmount::new(1, 2).to_value(), + ContractAmount::with(1, 0, 2).to_value(), false, true, None, @@ -2059,7 +2059,7 @@ async fn allow_save_transfer_and_verify() -> anyhow::Result<()> { let owner_resp = &create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), owner_keys.clone(), None, Some(issuer_resp.clone().contract.strict), @@ -2110,7 +2110,7 @@ async fn allow_save_transfer_and_verify() -> anyhow::Result<()> { assert!(resp.is_ok()); let contract = get_contract(&owner_sk, &issuer_resp.contract_id).await?; - assert_eq!(1., contract.balance_normalised); + assert_eq!(1., contract.balance_normalized); Ok(()) } diff --git a/tests/rgb/integration/udas.rs b/tests/rgb/integration/udas.rs index da4f569a..e98a08e7 100644 --- a/tests/rgb/integration/udas.rs +++ b/tests/rgb/integration/udas.rs @@ -25,7 +25,7 @@ async fn accept_uda_transfer() -> anyhow::Result<()> { let issuer_resp = issuer_issue_contract_v2( 1, "RGB21", - ContractAmount::new(1, 0).to_value(), + ContractAmount::with(1, 0, 0).to_value(), false, true, meta, @@ -38,7 +38,7 @@ async fn accept_uda_transfer() -> anyhow::Result<()> { let owner_resp = create_new_invoice( &issuer_resp.contract_id, &issuer_resp.iface, - 1.0, + ContractAmount::with(1, 0, issuer_resp.precision), owner_keys, None, Some(issuer_resp.clone().contract.legacy), @@ -91,7 +91,7 @@ async fn create_uda_save_medias() -> anyhow::Result<()> { let _issuer_resp = issuer_issue_contract_v2( 1, "RGB21", - ContractAmount::new(1, 0).to_value(), + ContractAmount::with(1, 0, 0).to_value(), false, true, meta, diff --git a/tests/rgb/integration/utils.rs b/tests/rgb/integration/utils.rs index 8705cfac..ffe5b6e2 100644 --- a/tests/rgb/integration/utils.rs +++ b/tests/rgb/integration/utils.rs @@ -290,7 +290,7 @@ pub async fn import_new_contract( pub async fn create_new_invoice( contract_id: &str, iface: &str, - amount: f64, + amount: ContractAmount, owner_keys: DecryptedWalletData, params: Option>, contract: Option, @@ -309,12 +309,6 @@ pub async fn create_new_invoice( _ => AssetType::Contract, }; - let precision = match iface { - "RGB20" => 2, - "RGB21" => 0, - _ => DEFAULT_PRECISION, - }; - if let Some(contract) = contract { // Import Contract let import_req = ImportRequest { @@ -342,7 +336,6 @@ pub async fn create_new_invoice( let params = params.unwrap_or_default(); - let amount = ContractAmount::from(amount.to_string(), precision); let invoice_req = InvoiceRequest { contract_id: contract_id.to_owned(), iface: iface.to_owned(), @@ -520,7 +513,7 @@ pub async fn issuer_issue_contract_v2( pub async fn create_new_invoice_v2( contract_id: &str, iface: &str, - amount: f64, + amount: ContractAmount, utxo: &str, owner_keys: DecryptedWalletData, params: Option>, @@ -528,13 +521,16 @@ pub async fn create_new_invoice_v2( ) -> Result { // Create Watcher let sk = owner_keys.private.nostr_prv.clone(); + + let seal = format!("tapret1st:{utxo}"); + let params = params.unwrap_or_default(); + let contract_type = match iface { "RGB20" => AssetType::RGB20, "RGB21" => AssetType::RGB21, _ => AssetType::Contract, }; - let mut precision = DEFAULT_PRECISION; if let Some(contract) = contract { // Import Contract let import_req = ImportRequest { @@ -544,16 +540,8 @@ pub async fn create_new_invoice_v2( let resp = import(&sk, import_req).await; assert!(resp.is_ok()); - - precision = resp?.precision; } - let seal = format!("tapret1st:{utxo}"); - let params = params.unwrap_or_default(); - - let mut amount = ContractAmount::from(amount.to_string(), precision); - amount.precision = precision; - let invoice_req = InvoiceRequest { contract_id: contract_id.to_owned(), iface: iface.to_owned(), diff --git a/tests/rgb/unit/amount.rs b/tests/rgb/unit/amount.rs new file mode 100644 index 00000000..6a1c9535 --- /dev/null +++ b/tests/rgb/unit/amount.rs @@ -0,0 +1,30 @@ +#![cfg(not(target_arch = "wasm32"))] +use bitmask_core::rgb::structs::ContractAmount; +#[tokio::test] +async fn create_contract_amount() -> anyhow::Result<()> { + let amount = ContractAmount::new(9900, 2); + assert_eq!(amount.int, 99); + assert_eq!(amount.fract, 0); + assert_eq!(amount.to_value(), 9900); + assert_eq!(amount.to_string(), "99.00"); + + let amount = ContractAmount::new(10000, 2); + assert_eq!(amount.int, 100); + assert_eq!(amount.fract, 0); + assert_eq!(amount.to_value(), 10000); + assert_eq!(amount.to_string(), "100.00"); + + let amount = ContractAmount::with(4000, 0, 2); + assert_eq!(amount.int, 4000); + assert_eq!(amount.fract, 0); + assert_eq!(amount.to_value(), 400000); + assert_eq!(amount.to_string(), "4000.00"); + + let amount = ContractAmount::with(1000, 1, 2); + assert_eq!(amount.int, 1000); + assert_eq!(amount.fract, 1); + assert_eq!(amount.to_value(), 100001); + assert_eq!(amount.to_string(), "1000.01"); + + Ok(()) +} diff --git a/tests/rgb/web/inspect.rs b/tests/rgb/web/inspect.rs index 3a9d21ca..f27e8e07 100644 --- a/tests/rgb/web/inspect.rs +++ b/tests/rgb/web/inspect.rs @@ -15,8 +15,8 @@ use bitmask_core::{ constants::switch_network, json_parse, resolve, rgb::{ - create_watcher, get_contract_state, import_contract, issue_contract, - watcher_next_address, watcher_next_utxo, + create_watcher, get_contract, import_contract, issue_contract, watcher_next_address, + watcher_next_utxo, }, set_panic_hook, }, @@ -58,7 +58,7 @@ async fn inspect_contract_states() { info!("Get Contract"); let contract_id = ""; let get_contract_resp: JsValue = - resolve(get_contract_state(sk.to_string(), contract_id.to_string())).await; + resolve(get_contract(sk.to_string(), contract_id.to_string())).await; let get_contract_resp: ContractResponse = json_parse(&get_contract_resp); info!(format!( "Contract {} ({}): \n {:#?}", diff --git a/tests/rgb/web/swaps.rs b/tests/rgb/web/swaps.rs index fa9e2823..8667a70c 100644 --- a/tests/rgb/web/swaps.rs +++ b/tests/rgb/web/swaps.rs @@ -206,7 +206,7 @@ async fn create_transfer_swap_flow() { let resp: ContractResponse = json_parse(&resp); let mut total_issuer = - f64::from_str(&ContractAmount::with(supply, precision).to_string()).unwrap(); + f64::from_str(&ContractAmount::new(supply, precision).to_string()).unwrap(); let mut total_owner = 0.0; let rounds = vec![TransferRounds::with(4_000.00, 1_000, true)]; @@ -269,10 +269,11 @@ async fn create_transfer_swap_flow() { .naive_utc() .timestamp(); let sender_asset_desc = sender_desc.clone(); + let asset_amount = ContractAmount::with(round.send_amount as u64, 0, 2); let sender_swap_req = RgbOfferRequest { contract_id: issuer_resp.contract_id.clone(), iface: issuer_resp.iface.clone(), - contract_amount: round.send_amount.to_string(), + contract_amount: asset_amount.to_value().to_string(), bitcoin_price: round.satoshi_price, descriptor: SecretString(sender_asset_desc), change_terminal: "/20/1".to_string(), @@ -288,9 +289,10 @@ async fn create_transfer_swap_flow() { info!(format!("Receiver ({receiver}) Create Bid")); let receiver_btc_desc = receiver_desc.clone(); + let asset_amount = ContractAmount::with(round.send_amount as u64, 0, 2); let receiver_swap_req = RgbBidRequest { offer_id: sender_swap_resp.offer_id.clone(), - asset_amount: round.send_amount.to_string(), + asset_amount: asset_amount.to_value().to_string(), descriptor: SecretString(receiver_btc_desc), change_terminal: "/1/0".to_string(), fee: PsbtFeeRequest::Value(1000), @@ -387,7 +389,7 @@ async fn create_transfer_swap_flow() { "Contract ({sender}): {} ({})\n {:#?}", contract_resp.contract_id, contract_resp.balance, contract_resp.allocations )); - let sender_current_balance = contract_resp.balance_normalised; + let sender_current_balance = contract_resp.balance_normalized; info!(format!("Get Contract Balancer ({receiver})")); let contract_resp = @@ -397,7 +399,7 @@ async fn create_transfer_swap_flow() { "Contract ({receiver}): {} ({})\n {:#?}", contract_resp.contract_id, contract_resp.balance, contract_resp.allocations )); - let receiver_current_balance = contract_resp.balance_normalised; + let receiver_current_balance = contract_resp.balance_normalized; info!(format!("<<<< ROUND #{index} Finish >>>>")); assert_eq!(sender_current_balance, sender_balance); diff --git a/tests/rgb/web/transfers.rs b/tests/rgb/web/transfers.rs index 4c20ce13..1abd2780 100644 --- a/tests/rgb/web/transfers.rs +++ b/tests/rgb/web/transfers.rs @@ -181,7 +181,7 @@ async fn create_transfer_with_fee_value() { let resp: ContractResponse = json_parse(&resp); let mut total_issuer = - f64::from_str(&ContractAmount::with(supply, precision).to_string()).unwrap(); + f64::from_str(&ContractAmount::new(supply, precision).to_string()).unwrap(); let mut total_owner = 0.0; let rounds = vec![ TransferRounds::with(20.00, true), @@ -240,7 +240,7 @@ async fn create_transfer_with_fee_value() { let params = HashMap::new(); let receiver_utxo = receiver_next_utxo.utxo.unwrap().outpoint.to_string(); let receiver_seal = format!("tapret1st:{receiver_utxo}"); - let invoice_amount = ContractAmount::from(round.send_amount.to_string(), precision); + let invoice_amount = ContractAmount::with(round.send_amount as u64, 0, precision); let invoice_req = InvoiceRequest { contract_id: issuer_resp.contract_id.to_string(), iface: issuer_resp.iface.to_string(), @@ -344,7 +344,7 @@ async fn create_transfer_with_fee_value() { "Contract ({sender}): {} ({})\n {:#?}", contract_resp.contract_id, contract_resp.balance, contract_resp.allocations )); - let sender_current_balance = contract_resp.balance_normalised; + let sender_current_balance = contract_resp.balance_normalized; info!(format!("Get Contract Balancer ({receiver})")); let contract_resp = resolve(get_contract( @@ -357,7 +357,7 @@ async fn create_transfer_with_fee_value() { "Contract ({receiver}): {} ({})\n {:#?}", contract_resp.contract_id, contract_resp.balance, contract_resp.allocations )); - let receiver_current_balance = contract_resp.balance_normalised; + let receiver_current_balance = contract_resp.balance_normalized; info!(format!("<<<< ROUND #{index} Finish >>>>")); assert_eq!(sender_current_balance, sender_balance); @@ -486,7 +486,7 @@ async fn create_transfer_with_fee_rate() { let resp: ContractResponse = json_parse(&resp); let mut total_issuer = - f64::from_str(&ContractAmount::with(supply, precision).to_string()).unwrap(); + f64::from_str(&ContractAmount::new(supply, precision).to_string()).unwrap(); let mut total_owner = 0.00; let rounds = vec![TransferRounds::with(20.00, true)]; @@ -540,7 +540,7 @@ async fn create_transfer_with_fee_rate() { info!(format!("Create Invoice ({receiver})")); let params = HashMap::new(); - let invoice_amount = ContractAmount::from(round.send_amount.to_string(), precision); + let invoice_amount = ContractAmount::with(round.send_amount as u64, 0, precision); let receiver_utxo = receiver_next_utxo.utxo.unwrap().outpoint.to_string(); let receiver_seal = format!("tapret1st:{receiver_utxo}"); let invoice_req = InvoiceRequest { @@ -646,7 +646,7 @@ async fn create_transfer_with_fee_rate() { "Contract ({sender}): {} ({})\n {:#?}", contract_resp.contract_id, contract_resp.balance, contract_resp.allocations )); - let sender_current_balance = contract_resp.balance_normalised; + let sender_current_balance = contract_resp.balance_normalized; info!(format!("Get Contract Balancer ({receiver})")); let contract_resp = resolve(get_contract( @@ -659,7 +659,7 @@ async fn create_transfer_with_fee_rate() { "Contract ({receiver}): {} ({})\n {:#?}", contract_resp.contract_id, contract_resp.balance, contract_resp.allocations )); - let receiver_current_balance = contract_resp.balance_normalised; + let receiver_current_balance = contract_resp.balance_normalized; info!(format!("<<<< ROUND #{index} Finish >>>>")); assert_eq!(sender_current_balance, sender_balance);