Skip to content

Commit

Permalink
wallet: show asset balances
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Jul 21, 2023
1 parent 4d56545 commit 67d6127
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/view/wallet/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ impl Component {
.update_electrum_state(ElectrumState::RetrievingHistory(no as usize * 2 + 1));
let wallet = self.model.wallet_mut();
wallet.update_utxos(batch);
self.widgets.update_utxos(&wallet.utxos());
self.widgets.update_outpoints(&mut self.model);
self.widgets.update_balance(&mut self.model);
}
electrum::Msg::TxBatch(batch, progress) => {
Expand Down Expand Up @@ -591,7 +591,7 @@ impl Widget for Component {

fn view(relm: &Relm<Self>, mut model: Self::Model) -> Self {
let glade_src = include_str!("wallet.glade");
let widgets = Widgets::from_string(glade_src).expect("glade file broken");
let mut widgets = Widgets::from_string(glade_src).expect("glade file broken");

let settings = init::<settings::Component>(()).expect("error in settings component");
settings.emit(settings::Msg::SetWallet(relm.stream().clone()));
Expand Down
18 changes: 15 additions & 3 deletions src/view/wallet/view_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use rgb::BlockchainResolver;
use rgbstd::containers::{Bindle, BindleParseError, Contract};
use rgbstd::contract::ContractId;
use rgbstd::interface::rgb20::Rgb20;
use rgbstd::interface::FungibleAllocation;
use rgbstd::persistence::{Inventory, InventoryError};
use rgbstd::validation;
use wallet::descriptors::DescriptorClass;
Expand Down Expand Up @@ -198,7 +199,7 @@ impl ViewModel {
Ok(status)
}

fn asset_info_for(&mut self, id: ContractId) -> AssetInfo {
fn asset_for(&mut self, id: ContractId) -> Rgb20 {
let rgb = self
.wallet
.rgb_mut()
Expand All @@ -207,9 +208,20 @@ impl ViewModel {
let iface = rgb
.contract_iface_named(id, "RGB20")
.expect("Not an RGB20 contract");
let iface = Rgb20::from(iface);
let spec = iface.spec();
Rgb20::from(iface)
}

pub fn asset_allocations(&mut self) -> Vec<FungibleAllocation> {
let Some(id) = self.asset else {
return vec![];
};
let iface = self.asset_for(id);
iface.allocations(&self.wallet).into_inner()
}

fn asset_info_for(&mut self, id: ContractId) -> AssetInfo {
let iface = self.asset_for(id);
let spec = iface.spec();
AssetInfo::with(
spec.name(),
spec.ticker(),
Expand Down
2 changes: 1 addition & 1 deletion src/view/wallet/wallet.glade
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@
</packing>
</child>
<child>
<object class="GtkNotebook">
<object class="GtkNotebook" id="notebook">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="vexpand">True</property>
Expand Down
47 changes: 43 additions & 4 deletions src/view/wallet/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ use gtk::gdk_pixbuf::Pixbuf;
use gtk::prelude::*;
use gtk::{
gdk, Adjustment, ApplicationWindow, Button, CheckButton, Entry, HeaderBar, Image, Label,
ListBox, ListStore, Menu, MenuItem, Popover, RadioMenuItem, Separator, SortColumn, SortType,
SpinButton, Spinner, Statusbar, TextBuffer, TreeView,
ListBox, ListStore, Menu, MenuItem, Notebook, Popover, RadioMenuItem, Separator, SortColumn,
SortType, SpinButton, Spinner, Statusbar, TextBuffer, TreeView,
};
use relm::Relm;
use rgbstd::interface::FungibleAllocation;
use wallet::hd::SegmentIndexes;

use super::asset_row::{self, AssetModel};
Expand Down Expand Up @@ -109,6 +110,7 @@ pub struct Widgets {
refresh_spin: Spinner,
refresh_img: Image,

notebook: Notebook,
history_store: ListStore,
utxo_store: ListStore,
address_store: ListStore,
Expand Down Expand Up @@ -421,7 +423,7 @@ impl Widgets {
);
}

pub fn init_ui(&self, model: &ViewModel) {
pub fn init_ui(&mut self, model: &ViewModel) {
let settings = model.as_settings();

self.assets_box.set_visible(settings.is_rgb());
Expand Down Expand Up @@ -461,7 +463,7 @@ impl Widgets {
self.update_invoice(model);
}

pub fn update_ui(&self, model: &mut ViewModel) {
pub fn update_ui(&mut self, model: &mut ViewModel) {
let info = model.asset_info();
self.ticker_lbl.set_text(&info.ticker());
self.asset_lbl.set_text(&info.name());
Expand All @@ -480,6 +482,10 @@ impl Widgets {
self.sep1.set_visible(!is_asset);
self.sep2.set_visible(!is_asset);

self.notebook.set_page(if is_asset { 1 } else { 0 });
self.notebook.set_show_tabs(!is_asset);

self.update_outpoints(model);
self.update_balance(model);
}

Expand Down Expand Up @@ -614,6 +620,39 @@ impl Widgets {
}
}

pub fn update_outpoints(&mut self, model: &mut ViewModel) {
match model.asset() {
None => {
self.update_utxos(model.wallet().utxos());
}
Some(_) => {
let info = model.asset_info();
self.update_allocations(&model.asset_allocations(), info.precision());
}
}
}

pub fn update_allocations(&mut self, allocations: &[FungibleAllocation], precision: u8) {
let pow = 10u64.pow(precision as u32);
self.utxo_store.clear();
for allocation in allocations {
let int = allocation.value / pow;
let fract = allocation.value - int * pow;
self.utxo_store.insert_with_values(None, &[
(0, &""),
(1, &allocation.owner.to_string()),
(
2,
&format!("{int}.{fract}")
.trim_end_matches('0')
.trim_end_matches('.'),
),
(3, &""),
(4, &0u32),
]);
}
}

pub fn update_utxos(&mut self, utxos: &BTreeSet<UtxoTxid>) {
self.utxo_store.clear();
for item in utxos {
Expand Down

0 comments on commit 67d6127

Please sign in to comment.