Skip to content

Commit

Permalink
feat: deposit from Kusama to Kreivo (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
b-avb authored Sep 25, 2024
1 parent 1e8c865 commit 97921a6
Show file tree
Hide file tree
Showing 9 changed files with 647 additions and 19 deletions.
17 changes: 17 additions & 0 deletions public/styles/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1454,6 +1454,8 @@ $fw-bold: 700;
border-radius: 12px;
list-style: none;
z-index: 10;
max-height: 30vh;
overflow: auto;
}

.dropdown--right .dropdown__list {
Expand Down Expand Up @@ -2376,6 +2378,21 @@ textarea::placeholder {
width: max-content;
}

.deposit__form__inputs {
display: flex;
flex-direction: column;
gap: 24px;
}

.deposit__row {
display: flex;
gap: 32px;
}

.summary .dropdown__label {
color: var(--text-secondary);
}

.balances {
display: flex;
flex-direction: column;
Expand Down
109 changes: 109 additions & 0 deletions src/hooks/use_deposit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use std::str::FromStr;

use dioxus::prelude::*;

#[derive(Clone, Debug)]
pub enum DepositTo {
Address(String),
Community(u16),
}

#[derive(Clone, Default, Debug)]
pub struct DepositForm {
pub dest: DepositTo,
pub amount: String,
}

pub enum DepositError {
MalformedAddress,
InvalidAmount,
}

impl DepositForm {
pub fn is_valid(&self) -> bool {
let has_info = match &self.dest {
// Check if is a valid address
DepositTo::Address(addrs) => addrs.len() > 0,
DepositTo::Community(_) => true,
};

has_info && self.amount.len() > 0
}

pub fn address(&self) -> String {
match &self.dest {
DepositTo::Address(addrs) => addrs.to_string(),
_ => String::new(),
}
}

pub fn to_deposit(&self) -> Result<(String, u64, bool), DepositError> {
let amount = self.amount.parse::<f64>().map_err(|_| {
log::warn!("Malformed amount");
DepositError::InvalidAmount
})?;
let amount = (amount * 1_000_000_000_000.0) as u64;
match &self.dest {
DepositTo::Address(addrs) => {
let address = sp_core::sr25519::Public::from_str(&addrs)
.map_err(|_| DepositError::MalformedAddress)?;
let hex_address = format!("0x{}", hex::encode(address.0));
Ok((hex_address, amount, false))
}
DepositTo::Community(id) => Ok((id.to_string(), amount, true)),
}
}
}

impl Default for DepositTo {
fn default() -> Self {
Self::Address(String::new())
}
}

pub fn use_deposit() -> UseDepositState {
let deposit = consume_context::<Signal<DepositForm>>();

use_hook(|| UseDepositState {
inner: UseDepositInner { deposit },
})
}

#[derive(Clone, Copy)]
pub struct UseDepositState {
inner: UseDepositInner,
}

#[derive(Clone, Copy, Default)]
pub struct UseDepositInner {
deposit: Signal<DepositForm>,
}

impl UseDepositState {
pub fn get(&self) -> UseDepositInner {
self.inner.clone()
}

pub fn get_deposit(&self) -> DepositForm {
self.inner.deposit.read().clone()
}

pub fn set_deposit(&mut self, deposit: DepositForm) {
let mut inner = self.inner.deposit.write();
*inner = deposit;
}

pub fn deposit_mut(&mut self) -> Signal<DepositForm> {
self.inner.deposit.clone()
}

pub fn is_form_complete(&self) -> bool {
let deposit = self.inner.deposit.read();

deposit.is_valid()
}

pub fn default(&mut self) {
self.inner = UseDepositInner::default()
}
}
2 changes: 2 additions & 0 deletions src/hooks/use_startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use super::{
use_accounts::{Account, IsDaoOwner},
use_attach::AttachFile,
use_communities::Communities,
use_deposit::DepositForm,
use_initiative::{ActionsForm, ConfirmationForm, InfoForm, SettingsForm},
use_notification::NotificationItem,
use_onboard::{BasicsForm, InvitationForm, ManagementForm},
Expand Down Expand Up @@ -57,6 +58,7 @@ pub fn use_startup() {
use_context_provider::<Signal<IsTimestampHandled>>(|| Signal::new(IsTimestampHandled(false)));

use_context_provider::<Signal<WithdrawForm>>(|| Signal::new(WithdrawForm::default()));
use_context_provider::<Signal<DepositForm>>(|| Signal::new(DepositForm::default()));

// Clients

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pub mod pages {
pub mod account;
pub mod dashboard;
pub mod deposit;
pub mod explore;
pub mod initiative;
pub mod initiatives;
Expand All @@ -25,6 +26,7 @@ pub mod hooks {
pub mod use_attach;
pub mod use_communities;
pub mod use_connect_wallet;
pub mod use_deposit;
pub mod use_initiative;
pub mod use_language;
pub mod use_market_client;
Expand Down
75 changes: 71 additions & 4 deletions src/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@
},
"withdraw": {
"payment": {
"label": "Checkout",
"title": "Payment",
"subtitle": "Payment Method",
"label": "Define the withdraw method",
"title": "Pick a Method",
"subtitle": "Methods",
"methods": {
"card": {
"title": "Credit Card",
Expand All @@ -131,6 +131,70 @@
}
}
},
"deposit": {
"tabs": {
"accounts": "My accounts",
"others": "Other accounts",
"communities": "Communities"
},
"form": {
"title": "Deposit Address",
"account": {
"label": "Account"
},
"address": {
"label": "Address"
},
"community": {
"label": "Community"
},
"amount": {
"label": "Amount",
"placeholder": "Amount"
},
"cta": {
"continue": "Confirm deposit"
}
},
"payment": {
"label": "Define the deposit method",
"title": "Pick a Method",
"subtitle": "Methods",
"methods": {
"card": {
"title": "Credit Card",
"fee": "+{fee}%",
"cta": "Add New Card"
},
"paypal": {
"title": "Paypal",
"fee": "+{fee}%"
},
"pse": {
"title": "PSE",
"fee": "+{fee}%"
},
"kusama": {
"title": "Kusama",
"fee": "Free"
},
"eth": {
"title": "ETH/Polygon",
"fee": "Free"
}
}
},
"tips": {
"loading": {
"title": "The deposit is in process",
"description": "This may take a moment"
},
"created": {
"title": "Excellent! 🚀",
"description": "Your deposit has been successfully"
}
}
},
"dao": {
"tabs": {
"all": "All initiatives",
Expand Down Expand Up @@ -468,7 +532,10 @@
"not_empty": "Oops! ✋ This field cannot be empty",
"upload_fail": "Oops! ✋ The file could not be uploaded. Please try again.",
"community_creation": "Oops! ✋ We couldn't create your community. Please check and try again.",
"initiative_creation": "Oops! ✋ We couldn't create the initiative. Please try again."
"initiative_creation": "Oops! ✋ We couldn't create the initiative. Please try again.",
"invalid_amount": "Oops! ✋ This amount field is invalid",
"invalid_address": "Oops! ✋ This address is invalid",
"deposit_failed": "Oops! ✋ We couldn't make this deposit. Please try again."
},
"market": {
"query_failed": "The price could not be consulted"
Expand Down
69 changes: 68 additions & 1 deletion src/locales/es-ES.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,70 @@
}
}
},
"deposit": {
"tabs": {
"accounts": "Mis cuentas",
"others": "Otras cuentas",
"communities": "Comunidades"
},
"form": {
"title": "Dirección de Depósito",
"account": {
"label": "Cuenta"
},
"address": {
"label": "Dirección"
},
"community": {
"label": "Comunidad"
},
"amount": {
"label": "Cantidad",
"placeholder": "Cantidad"
},
"cta": {
"continue": "Confirmar depósito"
}
},
"payment": {
"label": "Depósito",
"title": "Completa la información de depósito",
"subtitle": "Método",
"methods": {
"card": {
"title": "Tarjeta de Crédito",
"fee": "+{fee}%",
"cta": "Añadir Nueva Tarjeta"
},
"paypal": {
"title": "Paypal",
"fee": "+{fee}%"
},
"pse": {
"title": "PSE",
"fee": "+{fee}%"
},
"kusama": {
"title": "Kusama",
"fee": "Gratis"
},
"eth": {
"title": "ETH/Polygon",
"fee": "Gratis"
}
}
},
"tips": {
"loading": {
"title": "El depósito está en proceso",
"description": "Esto puede tardar un momento"
},
"created": {
"title": "¡Excelente! 🚀",
"description": "Tu depósito ha sido exitoso"
}
}
},
"dao": {
"tabs": {
"all": "Iniciativas",
Expand Down Expand Up @@ -464,7 +528,10 @@
"not_empty": "¡Ups! ✋ Este campo no puede estar vacio.",
"upload_fail": "¡Ups! ✋ No se pudo subir el archivo. Por favor, inténtalo de nuevo.",
"community_creation": "¡Ups! ✋ No logramos crear tu comunidad. Por favor, verifica e inténtalo de nuevo.",
"initiative_creation": "¡Ups! ✋ No logramos crear tu iniciativa. Por favor, inténtalo de nuevo."
"initiative_creation": "¡Ups! ✋ No logramos crear tu iniciativa. Por favor, inténtalo de nuevo.",
"invalid_amount": "¡Ups! ✋ Este campo de cantidad es inválido",
"invalid_address": "¡Ups! ✋ Esta dirección es inválida",
"deposit_failed": "¡Ups! ✋ No pudimos realizar este depósito. Por favor, inténtalo de nuevo."
},
"market": {
"query_failed": "No se pudo consultar el precio"
Expand Down
Loading

0 comments on commit 97921a6

Please sign in to comment.