Skip to content

Commit d38dae9

Browse files
authored
Merge pull request #133 from MostroP2P/remove-token
Remove seller/buyer token
2 parents f6c2d7a + aba5108 commit d38dae9

5 files changed

Lines changed: 76 additions & 42 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ uuid = { version = "1.3.0", features = [
3939
dotenvy = "0.15.6"
4040
lightning-invoice = { version = "0.33.2", features = ["std"] }
4141
reqwest = { version = "0.12.23", features = ["json"] }
42-
mostro-core = "0.6.46"
42+
mostro-core = "0.6.49"
4343
lnurl-rs = "0.9.0"
4444
pretty_env_logger = "0.5.0"
4545
openssl = { version = "0.10.73", features = ["vendored"] }

src/cli/get_dm.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ pub async fn execute_get_dm(
2121
if !admin {
2222
for index in 1..=trade_index {
2323
let keys = User::get_trade_keys(&pool, index).await?;
24-
let dm_temp = get_direct_messages(client, &keys, *since, from_user, Some(mostro_pubkey)).await;
24+
let dm_temp =
25+
get_direct_messages(client, &keys, *since, from_user, Some(mostro_pubkey)).await;
2526
dm.extend(dm_temp);
2627
}
2728
} else {
@@ -32,7 +33,8 @@ pub async fn execute_get_dm(
3233
std::process::exit(1);
3334
}
3435
};
35-
let dm_temp = get_direct_messages(client, &id_key, *since, from_user, Some(mostro_pubkey)).await;
36+
let dm_temp =
37+
get_direct_messages(client, &id_key, *since, from_user, Some(mostro_pubkey)).await;
3638
dm.extend(dm_temp);
3739
}
3840

@@ -63,13 +65,10 @@ pub async fn execute_get_dm(
6365
println!("{text}");
6466
println!();
6567
}
66-
Payload::Dispute(id, token, info) => {
68+
Payload::Dispute(id, info) => {
6769
println!("Action: {}", message.action);
6870
println!("Dispute id: {}", id);
69-
if token.is_some() {
70-
println!("Dispute token: {}", token.unwrap());
71-
}
72-
if info.is_some() {
71+
if let Some(info) = info {
7372
println!();
7473
println!("Dispute info: {:#?}", info);
7574
println!();

src/cli/new_order.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ pub async fn execute_new_order(
8282
invoice.as_ref().to_owned().cloned(),
8383
Some(0),
8484
expires_at,
85-
None,
86-
None,
8785
);
8886

8987
// Create new order for mostro

src/db.rs

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ pub async fn connect() -> Result<Pool<Sqlite>> {
3838
counterparty_pubkey TEXT,
3939
is_mine BOOLEAN,
4040
buyer_invoice TEXT,
41-
buyer_token INTEGER,
42-
seller_token INTEGER,
4341
request_id INTEGER,
4442
created_at INTEGER,
4543
expires_at INTEGER
@@ -66,11 +64,68 @@ pub async fn connect() -> Result<Pool<Sqlite>> {
6664
println!("User created with pubkey: {}", user.i0_pubkey);
6765
} else {
6866
pool = SqlitePool::connect(&db_url).await?;
67+
68+
// Migration: Drop buyer_token and seller_token columns if they exist
69+
migrate_remove_token_columns(&pool).await?;
6970
}
7071

7172
Ok(pool)
7273
}
7374

75+
async fn migrate_remove_token_columns(pool: &SqlitePool) -> Result<()> {
76+
println!("Checking for legacy token columns...");
77+
78+
// Check if buyer_token column exists
79+
let buyer_token_exists = sqlx::query_scalar::<_, i64>(
80+
"SELECT COUNT(*) FROM pragma_table_info('orders') WHERE name = 'buyer_token'",
81+
)
82+
.fetch_one(pool)
83+
.await?;
84+
85+
// Check if seller_token column exists
86+
let seller_token_exists = sqlx::query_scalar::<_, i64>(
87+
"SELECT COUNT(*) FROM pragma_table_info('orders') WHERE name = 'seller_token'",
88+
)
89+
.fetch_one(pool)
90+
.await?;
91+
92+
// Drop buyer_token column if it exists
93+
if buyer_token_exists > 0 {
94+
println!("Removing legacy buyer_token column...");
95+
match sqlx::query("ALTER TABLE orders DROP COLUMN buyer_token")
96+
.execute(pool)
97+
.await
98+
{
99+
Ok(_) => println!("Successfully removed buyer_token column"),
100+
Err(e) => {
101+
println!("Warning: Could not remove buyer_token column: {}", e);
102+
// Continue execution - this is not critical
103+
}
104+
}
105+
}
106+
107+
// Drop seller_token column if it exists
108+
if seller_token_exists > 0 {
109+
println!("Removing legacy seller_token column...");
110+
match sqlx::query("ALTER TABLE orders DROP COLUMN seller_token")
111+
.execute(pool)
112+
.await
113+
{
114+
Ok(_) => println!("Successfully removed seller_token column"),
115+
Err(e) => {
116+
println!("Warning: Could not remove seller_token column: {}", e);
117+
// Continue execution - this is not critical
118+
}
119+
}
120+
}
121+
122+
if buyer_token_exists == 0 && seller_token_exists == 0 {
123+
println!("No legacy token columns found - database is up to date");
124+
}
125+
126+
Ok(())
127+
}
128+
74129
#[derive(Debug, Default, Clone, sqlx::FromRow)]
75130
pub struct User {
76131
/// The user's ID is the identity pubkey
@@ -214,8 +269,6 @@ pub struct Order {
214269
pub counterparty_pubkey: Option<String>,
215270
pub is_mine: Option<bool>,
216271
pub buyer_invoice: Option<String>,
217-
pub buyer_token: Option<u16>,
218-
pub seller_token: Option<u16>,
219272
pub request_id: Option<i64>,
220273
pub created_at: Option<i64>,
221274
pub expires_at: Option<i64>,
@@ -248,8 +301,6 @@ impl Order {
248301
counterparty_pubkey: None,
249302
is_mine: Some(true),
250303
buyer_invoice: None,
251-
buyer_token: None,
252-
seller_token: None,
253304
request_id,
254305
created_at: Some(chrono::Utc::now().timestamp()),
255306
expires_at: None,
@@ -259,9 +310,8 @@ impl Order {
259310
r#"
260311
INSERT INTO orders (id, kind, status, amount, min_amount, max_amount,
261312
fiat_code, fiat_amount, payment_method, premium, trade_keys,
262-
counterparty_pubkey, is_mine, buyer_invoice, buyer_token, seller_token,
263-
request_id, created_at, expires_at)
264-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
313+
counterparty_pubkey, is_mine, buyer_invoice, request_id, created_at, expires_at)
314+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
265315
"#,
266316
)
267317
.bind(&order.id)
@@ -278,8 +328,6 @@ impl Order {
278328
.bind(&order.counterparty_pubkey)
279329
.bind(order.is_mine)
280330
.bind(&order.buyer_invoice)
281-
.bind(order.buyer_token)
282-
.bind(order.seller_token)
283331
.bind(order.request_id)
284332
.bind(order.created_at)
285333
.bind(order.expires_at)
@@ -359,8 +407,7 @@ impl Order {
359407
UPDATE orders
360408
SET kind = ?, status = ?, amount = ?, fiat_code = ?, min_amount = ?, max_amount = ?,
361409
fiat_amount = ?, payment_method = ?, premium = ?, trade_keys = ?, counterparty_pubkey = ?,
362-
is_mine = ?, buyer_invoice = ?, created_at = ?, expires_at = ?, buyer_token = ?,
363-
seller_token = ?
410+
is_mine = ?, buyer_invoice = ?, expires_at = ?
364411
WHERE id = ?
365412
"#,
366413
)
@@ -377,10 +424,7 @@ impl Order {
377424
.bind(&self.counterparty_pubkey)
378425
.bind(self.is_mine)
379426
.bind(&self.buyer_invoice)
380-
.bind(self.created_at)
381427
.bind(self.expires_at)
382-
.bind(self.buyer_token)
383-
.bind(self.seller_token)
384428
.bind(id)
385429
.execute(pool)
386430
.await?;
@@ -440,22 +484,15 @@ impl Order {
440484
}
441485

442486
pub async fn get_all_trade_keys(pool: &SqlitePool) -> Result<Vec<String>> {
443-
#[derive(sqlx::FromRow)]
444-
struct TradeKeyRow {
445-
trade_keys: Option<String>,
446-
}
447-
448-
let rows = sqlx::query_as::<_, TradeKeyRow>(
449-
"SELECT DISTINCT trade_keys FROM orders WHERE trade_keys IS NOT NULL"
487+
let trade_keys: Vec<String> = sqlx::query_scalar::<_, Option<String>>(
488+
"SELECT DISTINCT trade_keys FROM orders WHERE trade_keys IS NOT NULL",
450489
)
451490
.fetch_all(pool)
452-
.await?;
453-
454-
let trade_keys: Vec<String> = rows
455-
.into_iter()
456-
.filter_map(|row| row.trade_keys)
457-
.collect();
458-
491+
.await?
492+
.into_iter()
493+
.flatten()
494+
.collect();
495+
459496
Ok(trade_keys)
460497
}
461498

0 commit comments

Comments
 (0)