Skip to content

Commit

Permalink
[+] add history
Browse files Browse the repository at this point in the history
  • Loading branch information
heng30 committed Jul 4, 2024
1 parent 01ea30e commit dea5548
Show file tree
Hide file tree
Showing 20 changed files with 460 additions and 60 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ anyhow = "=1.0.86"
clipboard = "=0.5.0"
once_cell = "=1.19.0"
webbrowser = "=1.0.1"
serde_with = "=3.8.3"
serde_json = "=1.0.118"

uuid = { version = "=1.9.1", features = ["v4"] }
Expand Down
11 changes: 1 addition & 10 deletions libs/cutil/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use aes::Aes256;
use anyhow::{anyhow, Context, Result};
use block_modes::{block_padding, BlockMode, Cbc};
use crypto_hash::{hex_digest, Algorithm};
use rand::Rng;

type Aes256Cbc = Cbc<Aes256, block_padding::Pkcs7>;

Expand Down Expand Up @@ -49,18 +48,10 @@ pub fn hash(text: &str) -> String {
)
}

#[allow(dead_code)]
pub fn random_string(length: usize) -> String {
let mut rng = rand::thread_rng();
let chars: Vec<char> = ('a'..='z').collect();
(0..length)
.map(|_| chars[rng.gen_range(0..chars.len())])
.collect()
}

#[cfg(test)]
mod tests {
use super::*;
use super::super::str::random_string;

#[test]
fn test_random_string() {
Expand Down
10 changes: 10 additions & 0 deletions libs/cutil/src/str.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use rand::Rng;

pub fn split_string_to_fixed_length_parts(input: &str, length: usize) -> Vec<String> {
input
.chars()
Expand All @@ -15,3 +17,11 @@ pub fn pretty_size_string(size: u64) -> String {
_ => format!("{}G", size / (1024 * 1024 * 1024)),
}
}

pub fn random_string(length: usize) -> String {
let mut rng = rand::thread_rng();
let chars: Vec<char> = ('a'..='z').collect();
(0..length)
.map(|_| chars[rng.gen_range(0..chars.len())])
.collect()
}
5 changes: 4 additions & 1 deletion libs/sqldb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ edition = "2021"
[dependencies]
anyhow = "^1"
once_cell = "^1"
tokio = { version = "^1", features = ["full"] }
tokio = { version = "^1", features = ["sync"] }
serde = { version = "^1", features = ["serde_derive"] }
sqlx = { version = "0.6", features = ["runtime-tokio-native-tls", "sqlite"] }

[dev-dependencies]
tokio = { version = "^1", features = ["full"] }
29 changes: 27 additions & 2 deletions libs/sqldb/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ pub async fn select_all(table: &str) -> Result<Vec<ComEntry>> {
)
}

pub async fn row_counts(table: &str) -> Result<i64> {
let count: (i64,) = sqlx::query_as(&format!("SELECT COUNT(*) FROM {table}"))
.fetch_one(&pool().await)
.await?;

Ok(count.0)
}

pub async fn is_exist(table: &str, uuid: &str) -> Result<()> {
select(table, uuid).await?;
Ok(())
Expand All @@ -74,8 +82,8 @@ pub async fn is_exist(table: &str, uuid: &str) -> Result<()> {
#[cfg(test)]
mod tests {
use super::*;
use tokio::sync::Mutex;
use once_cell::sync::Lazy;
use tokio::sync::Mutex;

static MTX: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));
const DB_PATH: &str = "/tmp/entry-db-test.db";
Expand Down Expand Up @@ -137,7 +145,10 @@ mod tests {
insert(TABLE_NAME, "uuid-1", "data-1").await?;
update(TABLE_NAME, "uuid-1", "data-1-1").await?;

assert_eq!(select(TABLE_NAME, "uuid-1").await?.data, "data-1-1".to_string());
assert_eq!(
select(TABLE_NAME, "uuid-1").await?.data,
"data-1-1".to_string()
);

Ok(())
}
Expand Down Expand Up @@ -178,6 +189,20 @@ mod tests {
Ok(())
}

#[tokio::test]
async fn test_row_counts() -> Result<()> {
let _mtx = MTX.lock().await;
init(DB_PATH).await;
new(TABLE_NAME).await?;
delete_all(TABLE_NAME).await?;

assert!(row_counts(TABLE_NAME).await? == 0);
insert(TABLE_NAME, "uuid-1", "data-1").await?;
assert!(row_counts(TABLE_NAME).await? == 1);

Ok(())
}

#[tokio::test]
async fn test_is_exist() -> Result<()> {
let _mtx = MTX.lock().await;
Expand Down
1 change: 1 addition & 0 deletions libs/wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod props;
pub mod seed;
pub mod transation;
pub mod util;
pub mod network;

pub mod prelude {
pub use bip39::MnemonicType;
Expand Down
88 changes: 88 additions & 0 deletions libs/wallet/src/network.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use std::{str::FromStr, string::ToString};

#[derive(Clone, Debug)]
pub enum NetworkType {
Main,
Test,
Dev,
}

impl FromStr for NetworkType {
type Err = String;

fn from_str(ty: &str) -> Result<Self, Self::Err> {
let ty = match ty.to_lowercase().as_str() {
"main" => NetworkType::Main,
"test" => NetworkType::Test,
"dev" => NetworkType::Dev,
_ => return Err(format!("Unknown Rpc type {ty}")),
};

Ok(ty)
}
}

impl ToString for NetworkType {
fn to_string(&self) -> String {
String::from(match self {
NetworkType::Main => "Main",
NetworkType::Test => "Test",
NetworkType::Dev => "Dev",
})
}
}

impl NetworkType {
pub fn address_detail_url(&self, address: &str) -> String {
let url = "https://explorer.solana.com/address";
match *self {
NetworkType::Main => format!("{url}/{address}"),
NetworkType::Test => format!("{url}/{address}?cluster=testnet"),
NetworkType::Dev => format!("{url}/{address}?cluster=devnet"),
}
}

pub fn tx_detail_url(&self, hash: &str) -> String {
let url = "https://explorer.solana.com/tx";
match *self {
NetworkType::Main => format!("{url}/{hash}"),
NetworkType::Test => format!("{url}/{hash}?cluster=testnet"),
NetworkType::Dev => format!("{url}/{hash}?cluster=devnet"),
}

}
}

#[derive(Clone, Debug)]
pub enum RpcUrlType {
Main,
Test,
Dev,
}

impl ToString for RpcUrlType {
fn to_string(&self) -> String {
String::from(match self {
RpcUrlType::Main => "https://api.mainnet-beta.solana.com",
RpcUrlType::Test => "https://api.testnet.solana.com",
RpcUrlType::Dev => "https://api.devnet.solana.com",
})
}
}

#[derive(Clone, Debug)]
pub enum WssUrlType {
Main,
Test,
Dev,
}

impl ToString for WssUrlType {
fn to_string(&self) -> String {
String::from(match self {
WssUrlType::Main => "wss://api.mainnet-beta.solana.com",
WssUrlType::Test => "wss://api.testnet.solana.com",
WssUrlType::Dev => "wss://api.devnet.solana.com",
})
}
}
35 changes: 1 addition & 34 deletions libs/wallet/src/transation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,43 +22,10 @@ use solana_sdk::{
use solana_transaction_status::UiTransactionEncoding;
use spl_token::state::Mint;
use std::{str::FromStr, string::ToString, time::Duration};
use super::network::{RpcUrlType, WssUrlType};

pub const DEFAULT_TIMEOUT_MS: u64 = 10_000;

#[derive(Clone, Debug)]
pub enum RpcUrlType {
Main,
Test,
Dev,
}

impl ToString for RpcUrlType {
fn to_string(&self) -> String {
String::from(match self {
RpcUrlType::Main => "https://api.mainnet-beta.solana.com",
RpcUrlType::Test => "https://api.testnet.solana.com",
RpcUrlType::Dev => "https://api.devnet.solana.com",
})
}
}

#[derive(Clone, Debug)]
pub enum WssUrlType {
Main,
Test,
Dev,
}

impl ToString for WssUrlType {
fn to_string(&self) -> String {
String::from(match self {
WssUrlType::Main => "wss://api.mainnet-beta.solana.com",
WssUrlType::Test => "wss://api.testnet.solana.com",
WssUrlType::Dev => "wss://api.devnet.solana.com",
})
}
}

#[derive(Default, Debug, Clone)]
pub struct AccountToken {
pub token_account_address: Pubkey,
Expand Down
5 changes: 5 additions & 0 deletions src/config/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ pub fn ui() -> data::UI {
CONFIG.lock().unwrap().ui.clone()
}

pub fn network() -> data::Network {
CONFIG.lock().unwrap().network.clone()
}

pub fn db_path() -> PathBuf {
CONFIG.lock().unwrap().db_path.clone()
}
Expand Down Expand Up @@ -121,6 +125,7 @@ impl Config {
Ok(c) => {
self.appid = c.appid;
self.ui = c.ui;
self.network = c.network;
Ok(())
}
Err(_) => {
Expand Down
14 changes: 14 additions & 0 deletions src/config/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct Config {
pub appid: String,

pub ui: UI,
pub network: Network,
}

pub fn appid_default() -> String {
Expand All @@ -44,3 +45,16 @@ impl Default for UI {
}
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Network {
pub ty: String,
}

impl Default for Network {
fn default() -> Self {
Self {
ty: "Main".to_string(),
}
}
}
2 changes: 1 addition & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod conf;
mod data;

pub use conf::{all, db_path, init, is_first_run, save, ui};
pub use conf::{all, db_path, init, is_first_run, save, ui, network};
Loading

0 comments on commit dea5548

Please sign in to comment.