Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Slatepack creation workflow #21

Merged
merged 7 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 77 additions & 4 deletions crates/core/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use dirs;
// Re-exports
pub use global::ChainTypes;
pub use grin_wallet_impls::HTTPNodeClient;
pub use grin_wallet_libwallet::{StatusMessage, TxLogEntry, TxLogEntryType, WalletInfo};
pub use grin_wallet_libwallet::{
InitTxArgs, Slate, StatusMessage, TxLogEntry, TxLogEntryType, WalletInfo,
};

use crate::error::GrinWalletInterfaceError;
use crate::logger;
Expand Down Expand Up @@ -150,7 +152,7 @@ where
w.use_embedded_node = value;
}

/// Sets the top level directory of the wallet and creates default config if config
/// Sets the top level directory of the wallet and creates default config if config
/// doesn't already exist. The initial config is created based off of the chain type.
fn inst_wallet(
wallet_interface: Arc<RwLock<WalletInterface<L, C>>>,
Expand Down Expand Up @@ -248,14 +250,28 @@ where
let tld = {
let mut w_lock = o.wallet_inst.lock();
let p = w_lock.lc_provider()?;
let logging_config = w.config.clone().unwrap().clone().members.unwrap().logging.clone();
let logging_config = w
.config
.clone()
.unwrap()
.clone()
.members
.unwrap()
.logging
.clone();

log::debug!(
"core::wallet::InitWallet Top Level Directory: {:?}",
p.get_top_level_directory(),
);

p.create_config(&chain_type, WALLET_CONFIG_FILE_NAME, Some(args.config), logging_config, None)?;
p.create_config(
&chain_type,
WALLET_CONFIG_FILE_NAME,
Some(args.config),
logging_config,
None,
)?;
p.create_wallet(
None,
args.recovery_phrase,
Expand Down Expand Up @@ -344,6 +360,18 @@ where
let w = wallet_interface.read().unwrap();
if let Some(o) = &w.owner_api {
let res = o.retrieve_txs(None, false, None, None)?;
/*for tx in &mut res.1 {
if tx.amount_credited == 0 && tx.amount_debited == 0 {
let saved_tx = o.get_stored_tx(None, Some(tx.id), None);
if let Ok(st) = saved_tx {
// Todo: have to check more things here, this is just for tx display
if let Some(s) = st {
println!("TWO: {}", s.amount);
tx.amount_debited = s.amount;
}
}
}
};*/
return Ok(res);
} else {
return Err(GrinWalletInterfaceError::OwnerAPINotInstantiated);
Expand All @@ -362,9 +390,54 @@ where
}
}

pub async fn create_tx(
wallet_interface: Arc<RwLock<WalletInterface<L, C>>>,
init_args: InitTxArgs,
) -> Result<Slate, GrinWalletInterfaceError> {
let w = wallet_interface.write().unwrap();
if let Some(o) = &w.owner_api {
let slate = {
o.init_send_tx(None, init_args)?
};
o.tx_lock_outputs(None, &slate)?;
return Ok(slate);
} else {
return Err(GrinWalletInterfaceError::OwnerAPINotInstantiated);
}
}

pub async fn cancel_tx(
wallet_interface: Arc<RwLock<WalletInterface<L, C>>>,
id: u32,
) -> Result<u32, GrinWalletInterfaceError> {
let w = wallet_interface.write().unwrap();
if let Some(o) = &w.owner_api {
o.cancel_tx(None, Some(id), None)?;
return Ok(id);
} else {
return Err(GrinWalletInterfaceError::OwnerAPINotInstantiated);
}
}


/*pub async fn tx_lock_outputs(
wallet_interface: Arc<RwLock<WalletInterface<L, C>>>,
init_args: InitTxArgs,
) -> Result<Slate, GrinWalletInterfaceError> {
let w = wallet_interface.write().unwrap();
if let Some(o) = &w.owner_api {
let slate = {
o.init_send_tx(None, init_args)?
};
return Ok(slate);
} else {
return Err(GrinWalletInterfaceError::OwnerAPINotInstantiated);
}
}*/

/*pub async fn get_recovery_phrase(wallet_interface: Arc<RwLock<WalletInterface<L, C>>>, password: String) -> String {
let mut w = wallet_interface.read().unwrap();
w.owner_api.get_mnemonic(name, password.into())
}*/

}
3 changes: 2 additions & 1 deletion locale/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -223,5 +223,6 @@
"address-instruction": "Provide this address to others to allow them to send you funds",
"recipient-address-instruction": "Paste the address of the recipient's wallet here",
"recipient-address": "Recipient's Slatepack Address",
"create-tx-amount": "Amount"
"create-tx-amount": "Amount",
"cancel-tx": "Cancel Transaction"
}
3 changes: 2 additions & 1 deletion locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,6 @@
"address-instruction": "Provide this address to others to allow them to send you funds",
"recipient-address-instruction": "Input the address of the recipient's wallet here",
"recipient-address": "Recipient's Slatepack Address",
"create-tx-amount": "Amount"
"create-tx-amount": "Amount",
"cancel-tx": "Cancel Transaction"
}
32 changes: 15 additions & 17 deletions src/gui/element/settings/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,13 @@ pub fn handle_message(
);

// set theme for gui
let theme = &state.theme_state.themes.iter().find(|x| theme_name == x.0).unwrap().1;
let theme = &state
.theme_state
.themes
.iter()
.find(|x| theme_name == x.0)
.unwrap()
.1;
grin_gui.theme = theme.clone();

state.theme_state.current_theme_name = theme_name.clone();
Expand Down Expand Up @@ -227,10 +233,7 @@ pub fn handle_message(
Ok(Command::none())
}

pub fn data_container<'a>(
state: &'a StateContainer,
config: &Config,
) -> Container<'a, Message> {
pub fn data_container<'a>(state: &'a StateContainer, config: &Config) -> Container<'a, Message> {
let language_container = {
let title = Container::new(Text::new(localized_string("language")).size(DEFAULT_FONT_SIZE))
.style(grin_gui_core::theme::ContainerStyle::NormalBackground);
Expand Down Expand Up @@ -463,7 +466,7 @@ pub fn data_container<'a>(
Column::new().push(checkbox_container)
};

let column = Column::new()
let mut column = Column::new()
.spacing(1)
.push(language_container)
.push(Space::new(Length::Units(0), Length::Units(10)))
Expand All @@ -472,19 +475,10 @@ pub fn data_container<'a>(
.push(open_theme_row)
.spacing(1);

let scrollable = Scrollable::new(column)
.height(Length::Fill)
.style(grin_gui_core::theme::ScrollableStyle::Primary);

// scrollable = scrollable
// .push(Space::new(Length::Units(0), Length::Units(10)))
// .push(theme_scale_row)
// .push(Space::new(Length::Units(0), Length::Units(10)))
// .push(open_theme_row);

// Systray settings
#[cfg(target_os = "windows")]
{
scrollable = scrollable
column = column
.push(Space::new(Length::Units(0), Length::Units(10)))
.push(close_to_tray_column)
.push(Space::new(Length::Units(0), Length::Units(10)))
Expand All @@ -493,6 +487,10 @@ pub fn data_container<'a>(
.push(toggle_autostart_column);
}

let scrollable = Scrollable::new(column)
.height(Length::Fill)
.style(grin_gui_core::theme::ScrollableStyle::Primary);

// Colum wrapping all the settings content.
//scrollable = scrollable.height(Length::Fill);

Expand Down
106 changes: 81 additions & 25 deletions src/gui/element/wallet/operation/create_tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use {
crate::localization::localized_string,
crate::Result,
anyhow::Context,
grin_gui_core::wallet::{StatusMessage, WalletInfo, WalletInterface},
grin_gui_core::{node::amount_to_hr_string, theme::ColorPalette},
grin_gui_core::wallet::{StatusMessage, WalletInfo, WalletInterface, InitTxArgs, Slate},
grin_gui_core::{node::amount_to_hr_string, theme::{ButtonStyle, ColorPalette, ContainerStyle}},
iced::{alignment, Alignment, Command, Length},
grin_gui_core::theme::{Container, Button, Element, Column, PickList, Row, Scrollable, Text, TextInput, Header, TableRow},
iced::widget::{
Expand All @@ -30,8 +30,6 @@ use {
};

pub struct StateContainer {
// pub back_button_state: button::State,
// pub recipient_address_input_state: text_input::State,
pub recipient_address_value: String,
// pub amount_input_state: text_input::State,
pub amount_value: String,
Expand All @@ -40,48 +38,84 @@ pub struct StateContainer {
impl Default for StateContainer {
fn default() -> Self {
Self {
// back_button_state: Default::default(),
// recipient_address_input_state: Default::default(),
recipient_address_value: Default::default(),
// amount_input_state: Default::default(),
amount_value: Default::default()
amount_value: Default::default(),
}
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Action {
}
pub enum Action {}

#[derive(Debug, Clone)]
pub enum LocalViewInteraction {
Back,
RecipientAddress(String),
Amount(String),
CreateTransaction(),
TxCreatedOk(Slate),
TxCreateError(Arc<RwLock<Option<anyhow::Error>>>),
}

pub fn handle_message<'a>(
grin_gui: &mut GrinGui,
message: LocalViewInteraction,
) -> Result<Command<Message>> {
let state = &mut grin_gui
.wallet_state
.operation_state
.create_tx_state;
let state = &mut grin_gui.wallet_state.operation_state.create_tx_state;

match message {
LocalViewInteraction::Back => {
log::debug!(
"Interaction::WalletOperationCreateTxViewInteraction(Back)"
);
grin_gui.wallet_state.operation_state.mode = crate::gui::element::wallet::operation::Mode::Home;
},
log::debug!("Interaction::WalletOperationCreateTxViewInteraction(Back)");
grin_gui.wallet_state.operation_state.mode =
crate::gui::element::wallet::operation::Mode::Home;
}
LocalViewInteraction::RecipientAddress(s) => {
state.recipient_address_value = s;
}
LocalViewInteraction::Amount(s) => {
state.amount_value = s;
}
LocalViewInteraction::CreateTransaction() => {
grin_gui.error.take();

log::debug!("Interaction::WalletOperationCreateTxViewInteraction");

let w = grin_gui.wallet_interface.clone();

// Todo: Amount parsing + validation, just testing the flow for now
let args = InitTxArgs {
src_acct_name: None,
amount: 1_000_000_000,
minimum_confirmations: 2,
max_outputs: 500,
num_change_outputs: 1,
selection_strategy_is_use_all: false,
late_lock: Some(false),
..Default::default()
};

let fut = move || WalletInterface::create_tx(w, args);

return Ok(Command::perform(fut(), |r| {
match r.context("Failed to Create Transaction") {
Ok(ret) => Message::Interaction(Interaction::WalletOperationCreateTxViewInteraction(
LocalViewInteraction::TxCreatedOk(ret),
)),
Err(e) => Message::Interaction(Interaction::WalletOperationCreateTxViewInteraction(
LocalViewInteraction::TxCreateError(Arc::new(RwLock::new(Some(e)))),
)),
}
}));
},
LocalViewInteraction::TxCreatedOk(slate) => {
log::debug!("{:?}", slate);
}
LocalViewInteraction::TxCreateError(err) => {
grin_gui.error = err.write().unwrap().take();
if let Some(e) = grin_gui.error.as_ref() {
log_error(e);
}
}
}

Ok(Command::none())
Expand Down Expand Up @@ -131,7 +165,11 @@ pub fn data_container<'a>(
let recipient_address_input = TextInput::new(
"",
&state.recipient_address_value,
|s| Interaction::WalletOperationCreateTxViewInteraction(LocalViewInteraction::RecipientAddress(s)),
|s| {
Interaction::WalletOperationCreateTxViewInteraction(
LocalViewInteraction::RecipientAddress(s),
)
},
)
.size(DEFAULT_FONT_SIZE)
.padding(6)
Expand Down Expand Up @@ -160,12 +198,29 @@ pub fn data_container<'a>(

let amount_input: Element<Interaction> = amount_input.into();

let address_instruction_container = Text::new(localized_string("recipient-address-instruction"))
.size(SMALLER_FONT_SIZE)
.horizontal_alignment(alignment::Horizontal::Left);

let address_instruction_container =
Container::new(address_instruction_container).style(grin_gui_core::theme::ContainerStyle::NormalBackground);
Text::new(localized_string("recipient-address-instruction"))
.size(SMALLER_FONT_SIZE)
.horizontal_alignment(alignment::Horizontal::Left);

let address_instruction_container = Container::new(address_instruction_container)
.style(ContainerStyle::NormalBackground);

let submit_button_label_container =
Container::new(Text::new(localized_string("create-transaction")).size(DEFAULT_FONT_SIZE))
.center_x()
.align_x(alignment::Horizontal::Center);

let mut submit_button = Button::new(
submit_button_label_container,
)
.style(ButtonStyle::Bordered);

submit_button = submit_button.on_press(Interaction::WalletOperationCreateTxViewInteraction(
LocalViewInteraction::CreateTransaction(),
));

let submit_button: Element<Interaction> = submit_button.into();

let column = Column::new()
.push(title_row)
Expand All @@ -174,6 +229,7 @@ pub fn data_container<'a>(
.push(recipient_address_input.map(Message::Interaction))
.push(amount_container)
.push(amount_input.map(Message::Interaction))
.push(submit_button.map(Message::Interaction))
.spacing(DEFAULT_PADDING)
.align_items(Alignment::Start);

Expand Down
Loading