Skip to content

Commit

Permalink
Spend from delegation in gui
Browse files Browse the repository at this point in the history
  • Loading branch information
azarovh committed Jan 24, 2024
1 parent 56b579b commit bc39f50
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 11 deletions.
42 changes: 40 additions & 2 deletions node-gui/src/backend/backend_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ use super::{
error::BackendError,
messages::{
AccountId, AccountInfo, AddressInfo, BackendEvent, BackendRequest, CreateDelegationRequest,
DelegateStakingRequest, EncryptionAction, EncryptionState, SendRequest, StakeRequest,
TransactionInfo, WalletId, WalletInfo,
DelegateStakingRequest, EncryptionAction, EncryptionState, SendDelegateToAddressRequest,
SendRequest, StakeRequest, TransactionInfo, WalletId, WalletInfo,
},
p2p_event_handler::P2pEventHandler,
parse_address, parse_coin_amount,
Expand Down Expand Up @@ -484,6 +484,37 @@ impl Backend {
Ok(TransactionInfo { wallet_id })
}

async fn send_delegation_to_address(
&mut self,
request: SendDelegateToAddressRequest,
) -> Result<TransactionInfo, BackendError> {
let SendDelegateToAddressRequest {
wallet_id,
account_id,
address,
amount,
delegation_id,
} = request;

let address = parse_address(&self.chain_config, &address)
.map_err(|err| BackendError::AddressError(err.to_string()))?;

let amount = parse_coin_amount(&self.chain_config, &amount)
.ok_or(BackendError::InvalidAmount(amount))?;

let delegation_id = Address::from_str(&self.chain_config, &delegation_id)
.and_then(|addr| addr.decode_object(&self.chain_config))
.map_err(|e| BackendError::AddressError(e.to_string()))?;

self.synced_wallet_controller(wallet_id, account_id.account_index())
.await?
.send_to_address_from_delegation(address, amount, delegation_id)
.await
.map_err(|e| BackendError::WalletError(e.to_string()))?;

Ok(TransactionInfo { wallet_id })
}

fn get_account_balance(
controller: &ReadOnlyController<WalletHandlesClient>,
) -> BTreeMap<Currency, Amount> {
Expand Down Expand Up @@ -569,6 +600,13 @@ impl Backend {
let result = self.delegate_staking(request).await.map(|tx| (tx, delegation_id));
Self::send_event(&self.event_tx, BackendEvent::DelegateStaking(result));
}
BackendRequest::SendDelegationToAddress(request) => {
let result = self.send_delegation_to_address(request).await;
Self::send_event(
&self.event_tx,
BackendEvent::SendDelegationToAddress(result),
);
}
BackendRequest::TransactionList {
wallet_id,
account_id,
Expand Down
11 changes: 11 additions & 0 deletions node-gui/src/backend/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,15 @@ pub struct DelegateStakingRequest {
pub delegation_amount: String,
}

#[derive(Debug, Clone)]
pub struct SendDelegateToAddressRequest {
pub wallet_id: WalletId,
pub account_id: AccountId,
pub address: String,
pub amount: String,
pub delegation_id: String,
}

#[derive(Debug, Clone)]
pub struct TransactionInfo {
pub wallet_id: WalletId,
Expand Down Expand Up @@ -169,6 +178,7 @@ pub enum BackendRequest {
StakeAmount(StakeRequest),
CreateDelegation(CreateDelegationRequest),
DelegateStaking(DelegateStakingRequest),
SendDelegationToAddress(SendDelegateToAddressRequest),

TransactionList {
wallet_id: WalletId,
Expand Down Expand Up @@ -202,6 +212,7 @@ pub enum BackendEvent {
StakeAmount(Result<TransactionInfo, BackendError>),
CreateDelegation(Result<TransactionInfo, BackendError>),
DelegateStaking(Result<(TransactionInfo, DelegationId), BackendError>),
SendDelegationToAddress(Result<TransactionInfo, BackendError>),
Broadcast(Result<(), BackendError>),

TransactionList(WalletId, AccountId, Result<TransactionList, BackendError>),
Expand Down
64 changes: 58 additions & 6 deletions node-gui/src/main_window/main_widget/tabs/wallet/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,20 @@ const POOL_ID_TOOLTIP_TEXT: &str =
const DELEGATION_ADDRESS_TOOLTIP_TEXT: &str =
"The address, that will have the authority to sign withdrawals from a pool.";

const SEND_DELEGATION_ADDRESS_TOOLTIP_TEXT: &str = " The address that will be receiving the coins";
const SEND_DELEGATION_AMOUNT_TOOLTIP_TEXT: &str =
"The amount that will be taken away from the delegation";
const SEND_DELEGATION_ID_TOOLTIP_TEXT: &str =
"The delegation id, from which the delegated coins will be taken";

pub fn view_delegation(
chain_config: &ChainConfig,
account: &AccountInfo,
pool_id: &str,
delegation_address: &str,
send_delegation_address: &str,
send_delegation_amount: &str,
send_delegation_id: &str,
delegate_staking_amounts: &BTreeMap<DelegationId, String>,
still_syncing: Option<WalletMessage>,
) -> Element<'static, WalletMessage> {
Expand Down Expand Up @@ -111,10 +120,11 @@ pub fn view_delegation(
};

column![
// ----- Create delegation
row![
text_input("Pool id for new delegation", pool_id)
.on_input(|value| { WalletMessage::PoolIdEdit(value) })
.padding(15),
text_input("Pool Id for new delegation", pool_id)
.on_input(|value| { WalletMessage::DelegationPoolIdEdit(value) })
.padding(10),
tooltip(
Text::new(iced_aw::Icon::Question.to_string()).font(iced_aw::ICON_FONT),
POOL_ID_TOOLTIP_TEXT,
Expand All @@ -126,7 +136,7 @@ pub fn view_delegation(
row![
text_input("Delegation address", delegation_address)
.on_input(|value| { WalletMessage::DelegationAddressEdit(value) })
.padding(15),
.padding(10),
tooltip(
Text::new(iced_aw::Icon::Question.to_string()).font(iced_aw::ICON_FONT),
DELEGATION_ADDRESS_TOOLTIP_TEXT,
Expand All @@ -136,9 +146,51 @@ pub fn view_delegation(
.style(iced::theme::Container::Box)
],
iced::widget::button(Text::new("Create delegation"))
.padding(15)
.on_press(still_syncing.unwrap_or(WalletMessage::CreateDelegation)),
.padding(10)
.on_press(still_syncing.clone().unwrap_or(WalletMessage::CreateDelegation)),
iced::widget::horizontal_rule(10),
// ----- Send delegation to address
row![
text_input("Address", send_delegation_address)
.on_input(|value| { WalletMessage::SendDelegationAddressEdit(value) })
.padding(10),
tooltip(
Text::new(iced_aw::Icon::Question.to_string()).font(iced_aw::ICON_FONT),
SEND_DELEGATION_ADDRESS_TOOLTIP_TEXT,
Position::Bottom
)
.gap(10)
.style(iced::theme::Container::Box)
],
row![
text_input("Amount to send", send_delegation_amount)
.on_input(|value| { WalletMessage::SendDelegationAmountEdit(value) })
.padding(10),
tooltip(
Text::new(iced_aw::Icon::Question.to_string()).font(iced_aw::ICON_FONT),
SEND_DELEGATION_AMOUNT_TOOLTIP_TEXT,
Position::Bottom
)
.gap(10)
.style(iced::theme::Container::Box)
],
row![
text_input("Delegation Id", send_delegation_id)
.on_input(|value| { WalletMessage::SendDelegationIdEdit(value) })
.padding(10),
tooltip(
Text::new(iced_aw::Icon::Question.to_string()).font(iced_aw::ICON_FONT),
SEND_DELEGATION_ID_TOOLTIP_TEXT,
Position::Bottom
)
.gap(10)
.style(iced::theme::Container::Box)
],
iced::widget::button(Text::new("Send to address from delegation"))
.padding(10)
.on_press(still_syncing.unwrap_or(WalletMessage::SendDelegationToAddress)),
iced::widget::horizontal_rule(10),
// ----- Delegation balance grid
delegation_balance_grid,
]
.spacing(10)
Expand Down
48 changes: 45 additions & 3 deletions node-gui/src/main_window/main_widget/tabs/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use crate::{
backend::{
messages::{
AccountId, BackendRequest, CreateDelegationRequest, DelegateStakingRequest,
SendRequest, StakeRequest, WalletId,
SendDelegateToAddressRequest, SendRequest, StakeRequest, WalletId,
},
BackendSender,
},
Expand Down Expand Up @@ -85,10 +85,15 @@ pub enum WalletMessage {
CreateStakingPool,
CreateStakingPoolSucceed,

PoolIdEdit(String),
DelegationPoolIdEdit(String),
DelegationAddressEdit(String),
CreateDelegation,
CreateDelegationSucceed,
SendDelegationAddressEdit(String),
SendDelegationAmountEdit(String),
SendDelegationIdEdit(String),
SendDelegationToAddress,
SendDelegationToAddressSucceed,
DelegateStaking(DelegationId),
DelegationAmountEdit((DelegationId, String)),
DelegateStakingSucceed(DelegationId),
Expand All @@ -110,9 +115,14 @@ pub struct AccountState {
margin_per_thousand: String,
cost_per_block_amount: String,
decommission_address: String,

delegation_pool_id: String,
delegation_address: String,
delegate_staking_amounts: BTreeMap<DelegationId, String>,

send_delegation_address: String,
send_delegation_amount: String,
send_delegation_id: String,
}

pub struct WalletTab {
Expand Down Expand Up @@ -275,7 +285,7 @@ impl WalletTab {
backend_sender.send(BackendRequest::CloseWallet(self.wallet_id));
Command::none()
}
WalletMessage::PoolIdEdit(value) => {
WalletMessage::DelegationPoolIdEdit(value) => {
self.account_state.delegation_pool_id = value;
Command::none()
}
Expand Down Expand Up @@ -321,6 +331,35 @@ impl WalletTab {
self.account_state.delegate_staking_amounts.remove(&delegation_id);
Command::none()
}
WalletMessage::SendDelegationAddressEdit(value) => {
self.account_state.send_delegation_address = value;
Command::none()
}
WalletMessage::SendDelegationAmountEdit(value) => {
self.account_state.send_delegation_amount = value;
Command::none()
}
WalletMessage::SendDelegationIdEdit(value) => {
self.account_state.send_delegation_id = value;
Command::none()
}
WalletMessage::SendDelegationToAddress => {
let request = SendDelegateToAddressRequest {
wallet_id: self.wallet_id,
account_id: self.selected_account,
address: self.account_state.send_delegation_address.clone(),
amount: self.account_state.send_delegation_amount.clone(),
delegation_id: self.account_state.send_delegation_id.clone(),
};
backend_sender.send(BackendRequest::SendDelegationToAddress(request));
Command::none()
}
WalletMessage::SendDelegationToAddressSucceed => {
self.account_state.send_delegation_address.clear();
self.account_state.send_delegation_amount.clear();
self.account_state.send_delegation_id.clear();
Command::none()
}
}
}
}
Expand Down Expand Up @@ -390,6 +429,9 @@ impl Tab for WalletTab {
account,
&self.account_state.delegation_pool_id,
&self.account_state.delegation_address,
&self.account_state.send_delegation_address,
&self.account_state.send_delegation_amount,
&self.account_state.send_delegation_id,
&self.account_state.delegate_staking_amounts,
still_syncing.clone(),
),
Expand Down
20 changes: 20 additions & 0 deletions node-gui/src/main_window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,26 @@ impl MainWindow {
self.show_error(error.to_string());
Command::none()
}
BackendEvent::SendDelegationToAddress(Ok(transaction_info)) => {
self.show_info(
"Success. Please wait for your transaction to be included in a block."
.to_owned(),
);

self.main_widget
.update(
MainWidgetMessage::TabsMessage(TabsMessage::WalletMessage(
transaction_info.wallet_id,
WalletMessage::SendDelegationToAddressSucceed,
)),
backend_sender,
)
.map(MainWindowMessage::MainWidgetMessage)
}
BackendEvent::SendDelegationToAddress(Err(error)) => {
self.show_error(error.to_string());
Command::none()
}
BackendEvent::Broadcast(Ok(())) => {
self.show_info("Success".to_owned());
Command::none()
Expand Down

0 comments on commit bc39f50

Please sign in to comment.