Skip to content

Commit

Permalink
wallet: format asset balance
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Jul 21, 2023
1 parent d412d75 commit 79cbda8
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 28 deletions.
9 changes: 4 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ serde = ["serde_with", "serde_yaml",

[patch.crates-io]
bpro = { git = "https://github.com/pandora-prime/bpro", branch = "v0.5" }
rgb-std = { git = "https://github.com/RGB-WG/rgb-wallet", branch = "develop" }
34 changes: 27 additions & 7 deletions src/view/wallet/asset_row/view_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ use gtk::{gio, glib};
#[derive(Default)]
pub struct AssetInner {
name: RefCell<String>,
amount: RefCell<String>,
amount: RefCell<u64>,
precision: RefCell<u8>,
ticker: RefCell<String>,
contract: RefCell<String>,
}
Expand Down Expand Up @@ -52,11 +53,22 @@ impl ObjectImpl for AssetInner {
None, // Default value
glib::ParamFlags::READWRITE,
),
glib::ParamSpecString::new(
glib::ParamSpecUInt64::new(
"amount",
"Amount",
"Amount",
None,
0,
u64::MAX,
0,
glib::ParamFlags::READWRITE,
),
glib::ParamSpecUChar::new(
"precision",
"Precision",
"Precision",
0,
u8::MAX,
0,
glib::ParamFlags::READWRITE,
),
glib::ParamSpecString::new(
Expand Down Expand Up @@ -93,6 +105,12 @@ impl ObjectImpl for AssetInner {
.expect("type conformity checked by `Object::set_property`");
self.amount.replace(amount);
}
"precision" => {
let amount = value
.get()
.expect("type conformity checked by `Object::set_property`");
self.precision.replace(amount);
}
"ticker" => {
let ticker = value
.get()
Expand All @@ -113,6 +131,7 @@ impl ObjectImpl for AssetInner {
match pspec.name() {
"name" => self.name.borrow().to_value(),
"amount" => self.amount.borrow().to_value(),
"precision" => self.precision.borrow().to_value(),
"ticker" => self.ticker.borrow().to_value(),
"contract" => self.contract.borrow().to_value(),
_ => unimplemented!(),
Expand Down Expand Up @@ -140,11 +159,10 @@ impl AssetInfo {
precision: u8,
contract_name: &str,
) -> AssetInfo {
let precision = precision as u32;
let amount = amount as f64 / 10_i32.pow(precision) as f64;
glib::Object::new(&[
("name", &name),
("amount", &format!("{}", amount)),
("amount", &amount),
("precision", &precision),
("ticker", &ticker),
("contract", &contract_name),
])
Expand All @@ -156,7 +174,9 @@ impl AssetInfo {

pub fn contract_name(&self) -> String { self.property::<String>("contract") }

pub fn amount(&self) -> String { self.property::<String>("amount") }
pub fn amount(&self) -> u64 { self.property::<u64>("amount") }

pub fn precision(&self) -> u8 { self.property::<u8>("precision") }
}

#[derive(Debug, Default)]
Expand Down
10 changes: 2 additions & 8 deletions src/view/wallet/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,7 @@ impl Component {
let wallet = self.model.wallet_mut();
wallet.update_utxos(batch);
self.widgets.update_utxos(&wallet.utxos());
self.widgets.update_state(
wallet.state(),
wallet.tx_count(),
self.model.exchange_rate,
);
self.widgets.update_balance(&mut self.model);
}
electrum::Msg::TxBatch(batch, progress) => {
self.widgets
Expand All @@ -302,11 +298,9 @@ impl Component {
self.tx_buffer.clear();
self.save();

let exchange_rate = self.model.exchange_rate;
self.widgets.update_balance(&mut self.model);
let wallet = self.model.wallet_mut();
self.widgets.update_history(&wallet.history());
self.widgets
.update_state(wallet.state(), wallet.tx_count(), exchange_rate);
self.widgets.update_addresses(&wallet.address_info(true));
self.widgets.update_electrum_state(ElectrumState::Complete(
self.model.as_settings().electrum().sec,
Expand Down
5 changes: 3 additions & 2 deletions src/view/wallet/view_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl ViewModel {
asset_model.append(&AssetInfo::with(
spec.name(),
spec.ticker(),
0,
iface.balance(&mut wallet),
spec.precision.into(),
&iface.contract_id().to_string(),
));
Expand Down Expand Up @@ -209,10 +209,11 @@ impl ViewModel {
.expect("Not an RGB20 contract");
let iface = Rgb20::from(iface);
let spec = iface.spec();

AssetInfo::with(
spec.name(),
spec.ticker(),
0,
iface.balance(&self.wallet),
spec.precision.into(),
&iface.contract_id().to_string(),
)
Expand Down
36 changes: 30 additions & 6 deletions src/view/wallet/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,8 @@ impl Widgets {
self.price_lbl.set_visible(!is_asset);
self.sep1.set_visible(!is_asset);
self.sep2.set_visible(!is_asset);

self.update_balance(model);
}

fn bind_asset_model(&self, model: &AssetModel) {
Expand Down Expand Up @@ -660,12 +662,34 @@ impl Widgets {
}
}

pub fn update_state(&self, state: WalletState, _tx_count: usize, exchange_rate: f64) {
self.balance_lbl
.set_text(&format!("{} sat", state.balance.to_string()));
self.balance_btc_lbl
.set_text(&format!("{}.", state.balance_btc() as u64));
self.balance_sat_lbl.set_text(&state.balance.to_string());
pub fn update_balance(&self, model: &mut ViewModel) {
let wallet = model.wallet();
let state = wallet.state();
let exchange_rate = model.exchange_rate;

let asset = model.asset_info();
let precision: u8 = asset.property("precision");
let balance: u64 = asset.property("amount");
let pow = 10u64.pow(precision as u32);
let int = balance / pow;
let fract = balance - int * pow;
let remain = format!("{fract}").trim_end_matches('0').to_string();
let zeros = precision as usize - remain.len();

let main = if int == 0 {
self.balance_btc_lbl
.set_text(&format!("0.{:01$}", "", zeros));
remain
} else if fract != 0 {
self.balance_btc_lbl.set_text("");
format!("{}.{}", int, remain)
} else {
self.balance_btc_lbl.set_text("");
format!("{}", int)
};
self.balance_sat_lbl.set_text(&main);

self.balance_lbl.set_text(&format!("{} sat", state.balance));

/*
self.volume_btc_lbl
Expand Down

0 comments on commit 79cbda8

Please sign in to comment.