-
Notifications
You must be signed in to change notification settings - Fork 13
Rate user #102
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
Rate user #102
Changes from all commits
3b32ede
68465dd
b63417f
e399ccd
9567024
bbb0f4c
6c9c4f3
787c815
e026e6f
f5b27e0
7b2b268
2053f6a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| [toolchain] | ||
| channel = "1.82.0" | ||
| profile = "minimal" | ||
| components = ["clippy", "rust-docs", "rustfmt"] | ||
| targets = ["wasm32-unknown-unknown"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| use crate::util::get_mcli_path; | ||
| use anyhow::Result; | ||
| use mostro_core::order::SmallOrder; | ||
| use mostro_core::NOSTR_REPLACEABLE_EVENT_KIND; | ||
| use nip06::FromMnemonic; | ||
|
|
@@ -9,15 +10,15 @@ use sqlx::SqlitePool; | |
| use std::fs::File; | ||
| use std::path::Path; | ||
|
|
||
| pub async fn connect() -> Result<Pool<Sqlite>, sqlx::Error> { | ||
| pub async fn connect() -> Result<Pool<Sqlite>> { | ||
| let mcli_dir = get_mcli_path(); | ||
| let mcli_db_path = format!("{}/mcli.db", mcli_dir); | ||
| let db_url = format!("sqlite://{}", mcli_db_path); | ||
| let pool: Pool<Sqlite>; | ||
| if !Path::exists(Path::new(&mcli_db_path)) { | ||
| if let Err(res) = File::create(&mcli_db_path) { | ||
| println!("Error in creating db file: {}", res); | ||
| return Err(sqlx::Error::Io(res)); | ||
| return Err(res.into()); | ||
| } | ||
| pool = SqlitePool::connect(&db_url).await?; | ||
| println!("Creating database file with orders table..."); | ||
|
|
@@ -59,10 +60,10 @@ pub async fn connect() -> Result<Pool<Sqlite>, sqlx::Error> { | |
| Ok(m) => m.to_string(), | ||
| Err(e) => { | ||
| println!("Error generating mnemonic: {}", e); | ||
| return Err(sqlx::Error::Decode(Box::new(e))); | ||
| return Err(e.into()); | ||
| } | ||
| }; | ||
| let user = User::new(mnemonic, &pool).await.unwrap(); | ||
| let user = User::new(mnemonic, &pool).await?; | ||
| println!("User created with pubkey: {}", user.i0_pubkey); | ||
| } else { | ||
| pool = SqlitePool::connect(&db_url).await?; | ||
|
|
@@ -81,10 +82,7 @@ pub struct User { | |
| } | ||
|
|
||
| impl User { | ||
| pub async fn new( | ||
| mnemonic: String, | ||
| pool: &SqlitePool, | ||
| ) -> Result<Self, Box<dyn std::error::Error>> { | ||
| pub async fn new(mnemonic: String, pool: &SqlitePool) -> Result<Self> { | ||
| let mut user = User::default(); | ||
| let account = NOSTR_REPLACEABLE_EVENT_KIND as u32; | ||
| let i0_keys = | ||
|
|
@@ -118,7 +116,7 @@ impl User { | |
| } | ||
|
|
||
| // Applying changes to the database | ||
| pub async fn save(&self, pool: &SqlitePool) -> Result<(), Box<dyn std::error::Error>> { | ||
| pub async fn save(&self, pool: &SqlitePool) -> Result<()> { | ||
| sqlx::query( | ||
| r#" | ||
| UPDATE users | ||
|
|
@@ -140,7 +138,7 @@ impl User { | |
| Ok(()) | ||
| } | ||
|
|
||
| pub async fn get(pool: &SqlitePool) -> Result<User, Box<dyn std::error::Error>> { | ||
| pub async fn get(pool: &SqlitePool) -> Result<User> { | ||
| let user = sqlx::query_as::<_, User>( | ||
| r#" | ||
| SELECT i0_pubkey, mnemonic, last_trade_index, created_at | ||
|
|
@@ -154,15 +152,15 @@ impl User { | |
| Ok(user) | ||
| } | ||
|
|
||
| pub async fn get_next_trade_index(pool: SqlitePool) -> Result<i64, Box<dyn std::error::Error>> { | ||
| pub async fn get_next_trade_index(pool: SqlitePool) -> Result<i64> { | ||
| let user = User::get(&pool).await?; | ||
| match user.last_trade_index { | ||
| Some(index) => Ok(index + 1), | ||
| None => Ok(1), | ||
| } | ||
| } | ||
|
|
||
| pub async fn get_identity_keys(pool: &SqlitePool) -> Result<Keys, Box<dyn std::error::Error>> { | ||
| pub async fn get_identity_keys(pool: &SqlitePool) -> Result<Keys> { | ||
| let user = User::get(pool).await?; | ||
| let account = NOSTR_REPLACEABLE_EVENT_KIND as u32; | ||
| let keys = | ||
|
|
@@ -171,10 +169,10 @@ impl User { | |
| Ok(keys) | ||
| } | ||
|
|
||
| pub async fn get_next_trade_keys( | ||
| pool: &SqlitePool, | ||
| ) -> Result<(Keys, i64), Box<dyn std::error::Error>> { | ||
| let trade_index = User::get_next_trade_index(pool.clone()).await?; | ||
| pub async fn get_next_trade_keys(pool: &SqlitePool) -> Result<(Keys, i64)> { | ||
| let mut trade_index = User::get_next_trade_index(pool.clone()).await?; | ||
| trade_index = trade_index - 1; | ||
|
|
||
| let user = User::get(pool).await?; | ||
| let account = NOSTR_REPLACEABLE_EVENT_KIND as u32; | ||
| match trade_index.try_into() { | ||
|
|
@@ -225,7 +223,7 @@ impl Order { | |
| order: SmallOrder, | ||
| trade_keys: &Keys, | ||
| request_id: Option<i64>, | ||
| ) -> Result<Self, Box<dyn std::error::Error>> { | ||
| ) -> Result<Self> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Storing trade_keys in plaintext. |
||
| let trade_keys_hex = trade_keys.secret_key().to_secret_hex(); | ||
| let id = match order.id { | ||
| Some(id) => id.to_string(), | ||
|
|
@@ -349,7 +347,7 @@ impl Order { | |
| } | ||
|
|
||
| // Applying changes to the database | ||
| pub async fn save(&self, pool: &SqlitePool) -> Result<(), Box<dyn std::error::Error>> { | ||
| pub async fn save(&self, pool: &SqlitePool) -> Result<()> { | ||
| // Validation if an identity document is present | ||
| if let Some(ref id) = self.id { | ||
| sqlx::query( | ||
|
|
@@ -385,7 +383,7 @@ impl Order { | |
|
|
||
| println!("Order with id {} updated in the database.", id); | ||
| } else { | ||
| return Err("Order must have an ID to be updated.".into()); | ||
| return Err(anyhow::anyhow!("Order must have an ID to be updated.")); | ||
| } | ||
|
|
||
| Ok(()) | ||
|
|
@@ -412,10 +410,7 @@ impl Order { | |
| Ok(rows_affected > 0) | ||
| } | ||
|
|
||
| pub async fn get_by_id( | ||
| pool: &SqlitePool, | ||
| id: &str, | ||
| ) -> Result<Order, Box<dyn std::error::Error>> { | ||
| pub async fn get_by_id(pool: &SqlitePool, id: &str) -> Result<Order> { | ||
| let order = sqlx::query_as::<_, Order>( | ||
| r#" | ||
| SELECT * FROM orders WHERE id = ? | ||
|
|
@@ -429,7 +424,7 @@ impl Order { | |
| Ok(order) | ||
| } | ||
|
|
||
| pub async fn get_all(pool: &SqlitePool) -> Result<Vec<Order>, Box<dyn std::error::Error>> { | ||
| pub async fn get_all(pool: &SqlitePool) -> Result<Vec<Order>> { | ||
| let orders = sqlx::query_as::<_, Order>(r#"SELECT * FROM orders"#) | ||
| .fetch_all(pool) | ||
| .await?; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.