Skip to content

Commit

Permalink
Merge branch 'main' into feat/multi-strategy-sims
Browse files Browse the repository at this point in the history
  • Loading branch information
kinrezC authored Jan 22, 2024
2 parents 8e2de7b + 2d80cb8 commit 1b97552
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 11 deletions.
7 changes: 5 additions & 2 deletions crates/app/src/controller/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ impl MessageWrapperView for Message {
type ParentMessage = RootViewMessage;
}

// normal message which are not clone
impl From<Message> for <Message as MessageWrapper>::ParentMessage {
fn from(_message: Message) -> Self {
Self::Empty
fn from(message: Message) -> Self {
Self::View(view::Message::Settings(message))
}
}

// for view messages which are clone
impl From<Message> for <Message as MessageWrapperView>::ParentMessage {
fn from(message: Message) -> Self {
Self::Settings(message)
Expand Down Expand Up @@ -180,6 +182,7 @@ impl State for SettingsScreen {
.signers
.update(message)
.map(|x| Message::Signers(x).into()),

Message::Contacts(message) => self
.contacts
.update(message)
Expand Down
176 changes: 167 additions & 9 deletions crates/app/src/controller/settings/signers.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
//! Signers are any entity that can sign and execute transactions.
//! These signers can be used within the app.
use alloy_primitives::Address;
use anyhow::Error;
use clients::ledger::{types::DerivationType, *};

use super::*;
use crate::components::system::label;
use crate::components::{
system::{label, Card, ExcaliburButton},
tables::{builder::TableBuilder, cells, columns::ColumnBuilder, rows::RowBuilder},
};

#[derive(Debug, Clone, Default)]
#[derive(Debug, Default, Clone)]
pub enum Message {
#[default]
Empty,
NotConnected,
Connected(Result<(Arc<LedgerClient>, Address), Arc<Error>>),
ConnectLedger,
}

impl MessageWrapper for Message {
Expand All @@ -24,11 +33,78 @@ impl From<Message> for <Message as MessageWrapper>::ParentMessage {
}
}

pub struct SignerManagement;
pub enum SignerManagement {
NotConnected,
Connected(Arc<LedgerClient>, Address),
Connecting,
Error,
}
pub async fn connect_to_ledger() -> Result<(Arc<LedgerClient>, Address), Arc<Error>> {
let ledger = Arc::new(LedgerClient::new_connection(DerivationType::LedgerLive(0)).await?);
let address = ledger.get_address().await.unwrap();
tracing::info!("Address: {:?}", address);
Ok((ledger, address))
}

impl SignerManagement {
pub fn new() -> Self {
Self
Self::NotConnected
}

pub fn signer_table(&self) -> TableBuilder<Message> {
match self {
SignerManagement::NotConnected => TableBuilder::new().padding_cell(Sizes::Md).column(
ColumnBuilder::new()
.headers(vec!["Name".to_string(), "Address".to_string()])
.rows(vec![RowBuilder::new()
.cell(cells::CellBuilder::new().value(Some("Wallet Name".to_string())))
.cell(
cells::CellBuilder::new().value(Some("0xb0b".to_string())),
)]),
),
SignerManagement::Connecting => TableBuilder::new().padding_cell(Sizes::Md).column(
ColumnBuilder::new()
.headers(vec!["Name".to_string(), "Address".to_string()])
.rows(vec![RowBuilder::new()
.cell(cells::CellBuilder::new().value(Some("Wallet Name".to_string())))
.cell(
cells::CellBuilder::new().value(Some("0xb0b".to_string())),
)]),
),
SignerManagement::Connected(_ledger, _address) => {
TableBuilder::new().padding_cell(Sizes::Md).column(
ColumnBuilder::new()
.headers(vec!["Name".to_string(), _address.to_string()])
.rows(vec![RowBuilder::new()
.cell(cells::CellBuilder::new().value(Some("Wallet Name".to_string())))
.cell(
cells::CellBuilder::new().value(Some("0xb0b".to_string())),
)]),
)
}
SignerManagement::Error => TableBuilder::new().padding_cell(Sizes::Md).column(
ColumnBuilder::new()
.headers(vec!["Name".to_string(), "Address".to_string()])
.rows(vec![RowBuilder::new()
.cell(cells::CellBuilder::new().value(Some("Wallet Name".to_string())))
.cell(
cells::CellBuilder::new().value(Some("0xb0b".to_string())),
)]),
),
}
}

pub fn singer_view() -> Row<'static, Message> {
let row_1 = Row::new()
.spacing(Sizes::Sm)
.push(label("labeled_name_input").build());
let row_2 = Row::new().spacing(Sizes::Sm).push(
Column::new()
.spacing(Sizes::Md)
.push(label("Instructions").build())
.width(Length::FillPortion(2)),
);
Row::new().push(Column::new().push(row_1).push(row_2).spacing(Sizes::Md))
}
}

Expand All @@ -40,14 +116,96 @@ impl State for SignerManagement {
Command::none()
}

fn update(&mut self, _message: Self::AppMessage) -> Command<Self::AppMessage> {
Command::none()
fn update(&mut self, message: Self::AppMessage) -> Command<Self::AppMessage> {
tracing::info!("Update message: {:?}", message);
match message {
Message::Connected(Ok(res)) => {
tracing::info!("Connected to ledger");
*self = SignerManagement::Connected(res.0, res.1);
Command::none()
}
Message::Connected(Err(err)) => {
tracing::error!("Error connecting to ledger: {:?}", err);
*self = SignerManagement::Error;
Command::none()
}
Message::ConnectLedger => {
*self = SignerManagement::Connecting;
Command::perform(connect_to_ledger(), Message::Connected)
}
_ => Command::none(),
}
}

fn view(&self) -> Element<'_, Self::ViewMessage> {
Container::new(Column::new().push(label("Select an app to get started.").title2().build()))
let mut content = Column::new().spacing(Sizes::Lg).padding(Sizes::Lg);

let mut upper_half = Column::new().spacing(Sizes::Md).push(
label("Manage Signer Settings")
.title2()
.primary()
.middle()
.build(),
);

let mut lower_half = Column::new().spacing(Sizes::Md);

let (upper_content, lower_content) = match self {
SignerManagement::NotConnected => (
Row::new().spacing(Sizes::Md).push(
ExcaliburButton::new()
.primary()
.build(label("Connect Ledger Device").build())
.padding(Sizes::Sm)
.on_press(Message::ConnectLedger),
),
Row::<Message>::new(),
),
SignerManagement::Connecting => (
Row::new().spacing(Sizes::Md).push(
ExcaliburButton::new()
.primary()
.build(label("Connecting").build())
.padding(Sizes::Sm)
.on_press(Message::ConnectLedger),
),
Row::<Message>::new(),
),
SignerManagement::Connected(_ledger, address) => (
Row::new().spacing(Sizes::Md).push(
ExcaliburButton::new()
.primary()
.build(
label(format!("Connected to wallet with address : {}", address))
.build(),
)
.padding(Sizes::Sm)
.on_press(Message::ConnectLedger),
),
SignerManagement::singer_view(),
),
SignerManagement::Error => (
Row::new().spacing(Sizes::Md).push(
ExcaliburButton::new()
.primary()
.build(
label("Error connecting, Is your ledger plugged in an authenticated?")
.build(),
)
.padding(Sizes::Sm)
.on_press(Message::ConnectLedger),
),
Row::<Message>::new().spacing(Sizes::Md),
),
};
upper_half = upper_half
.push(upper_content)
.push(Card::build_container(self.signer_table().build()).padding(Sizes::Md));
lower_half = lower_half.push(lower_content);
content = content.push(upper_half);
content = content.push(lower_half);
Container::new(content)
.center_x()
.center_y()
.width(Length::Fill)
.height(Length::Fill)
.into()
Expand Down

0 comments on commit 1b97552

Please sign in to comment.