Skip to content

Commit 3fc3b18

Browse files
committed
changes to all spenders for loop
1 parent f049565 commit 3fc3b18

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

sentry/src/db/spendable.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ pub async fn fetch_spendable(
4444
Ok(row.map(Spendable::from))
4545
}
4646

47+
static GET_ALL_SPENDERS_STATEMENT: &str = "SELECT spender, channel_id, channel, total, still_on_create2 FROM spendable WHERE channel_id = $1";
48+
49+
// TODO: Include pagination
50+
pub async fn get_all_spendables_for_channel(
51+
pool: DbPool,
52+
channel_id: &ChannelId,
53+
) -> Result<Vec<Spendable>, PoolError> {
54+
let client = pool.get().await?;
55+
let statement = client.prepare(GET_ALL_SPENDERS_STATEMENT).await?;
56+
57+
let rows = client.query(&statement, &[channel_id]).await?;
58+
let spendables: Vec<Spendable> = rows.into_iter().map(Spendable::from).collect();
59+
60+
Ok(spendables)
61+
}
62+
4763
static UPDATE_SPENDABLE_STATEMENT: &str = "INSERT INTO spendable(spender, channel_id, channel, total, still_on_create2) VALUES($1, $2, $3, $4, $5) ON CONFLICT ON CONSTRAINT spendable_pkey DO UPDATE SET total = $4, still_on_create2 = $5 WHERE spendable.spender = $1 AND spendable.channel_id = $2 RETURNING spender, channel_id, channel, total, still_on_create2";
4864

4965
// Updates spendable entry deposit or inserts a new spendable entry if it doesn't exist

sentry/src/routes/channel.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::db::{
44
latest_new_state_v5,
55
},
66
get_channel_by_id, insert_channel, insert_validator_messages, list_channels,
7-
spendable::{fetch_spendable, update_spendable},
7+
spendable::{fetch_spendable, get_all_spendables_for_channel, update_spendable},
88
DbPool, PoolError,
99
};
1010
use crate::{success_response, Application, Auth, ResponseError, RouteParams};
@@ -353,30 +353,32 @@ pub async fn get_all_spender_limits<A: Adapter + 'static>(
353353

354354
let mut all_spender_limits: HashMap<Address, Spender> = HashMap::new();
355355

356+
let all_spendables = get_all_spendables_for_channel(app.pool.clone(), &channel.id()).await?;
357+
356358
// Using for loop to avoid async closures
357-
for (spender_addr, balance) in new_state.msg.balances.spenders.iter() {
358-
let latest_spendable =
359-
match fetch_spendable(app.pool.clone(), spender_addr, &channel.id()).await? {
360-
Some(spendable) => spendable,
361-
None => continue, // skipping spender if not found
362-
};
363-
364-
let total_deposited = latest_spendable.deposit.total;
365-
let total_spent = total_deposited.checked_sub(balance).ok_or_else(|| {
366-
ResponseError::FailedValidation("Couldn't calculate total_spent".to_string())
367-
})?;
368-
369-
let spender_leaf = SpenderLeaf {
370-
total_spent,
371-
// merkle_proof: [u8; 32], // TODO
359+
for spendable in all_spendables {
360+
let spender = spendable.spender;
361+
let spender_leaf = if new_state.msg.balances.spenders.contains_key(&spender) {
362+
let balance = new_state.msg.balances.spenders.get(&spender).unwrap();
363+
364+
Some(SpenderLeaf {
365+
total_spent: spendable
366+
.deposit
367+
.total
368+
.checked_sub(balance)
369+
.unwrap_or_default(),
370+
// merkle_proof: [u8; 32], // TODO
371+
})
372+
} else {
373+
None
372374
};
373375

374376
let spender_info = Spender {
375-
total_deposited,
376-
spender_leaf: Some(spender_leaf),
377+
total_deposited: spendable.deposit.total,
378+
spender_leaf,
377379
};
378380

379-
all_spender_limits.insert(*spender_addr, spender_info);
381+
all_spender_limits.insert(spender, spender_info);
380382
}
381383

382384
let res = AllSpendersResponse {

0 commit comments

Comments
 (0)