Skip to content

Commit e07992b

Browse files
committed
fixes to logic
1 parent 1af5005 commit e07992b

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

primitives/src/sentry.rs

+2
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ pub struct SpenderResponse {
225225
#[serde(rename_all = "camelCase")]
226226
pub struct AllSpendersResponse {
227227
pub spenders: HashMap<Address, Spender>,
228+
#[serde(flatten)]
229+
pub pagination: Pagination,
228230
}
229231

230232
#[derive(Serialize, Deserialize, Debug)]

sentry/src/routes/channel.rs

+39-22
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use primitives::{
1818
config::TokenInfo,
1919
sentry::{
2020
channel_list::{ChannelListQuery, LastApprovedQuery},
21-
AllSpendersResponse, LastApproved, LastApprovedResponse, MessageResponse, SpenderResponse,
22-
SuccessResponse,
21+
AllSpendersResponse, LastApproved, LastApprovedResponse, MessageResponse, Pagination,
22+
SpenderResponse, SuccessResponse,
2323
},
2424
spender::{Deposit, Spendable, Spender, SpenderLeaf},
2525
validator::{MessageTypes, NewState},
@@ -346,10 +346,7 @@ pub async fn get_all_spender_limits<A: Adapter + 'static>(
346346
.expect("Request should have Channel")
347347
.to_owned();
348348

349-
let new_state = match get_corresponding_new_state(&app.pool, &channel).await? {
350-
Some(new_state) => new_state,
351-
None => return Err(ResponseError::NotFound),
352-
};
349+
let new_state = get_corresponding_new_state(&app.pool, &channel).await?;
353350

354351
let mut all_spender_limits: HashMap<Address, Spender> = HashMap::new();
355352

@@ -358,19 +355,23 @@ pub async fn get_all_spender_limits<A: Adapter + 'static>(
358355
// Using for loop to avoid async closures
359356
for spendable in all_spendables {
360357
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
358+
let spender_leaf = match new_state {
359+
Some(ref new_state) => new_state
360+
.msg
361+
.balances
362+
.spenders
363+
.get(&spender)
364+
.map(|balance| {
365+
SpenderLeaf {
366+
total_spent: spendable
367+
.deposit
368+
.total
369+
.checked_sub(balance)
370+
.unwrap_or_default(),
371+
// merkle_proof: [u8; 32], // TODO
372+
}
373+
}),
374+
None => None,
374375
};
375376

376377
let spender_info = Spender {
@@ -383,6 +384,12 @@ pub async fn get_all_spender_limits<A: Adapter + 'static>(
383384

384385
let res = AllSpendersResponse {
385386
spenders: all_spender_limits,
387+
pagination: Pagination {
388+
// TODO
389+
page: 1,
390+
total: 1,
391+
total_pages: 1,
392+
},
386393
};
387394

388395
Ok(success_response(serde_json::to_string(&res)?))
@@ -394,14 +401,24 @@ async fn get_corresponding_new_state(
394401
) -> Result<Option<MessageResponse<NewState<UncheckedState>>>, ResponseError> {
395402
let approve_state = match latest_approve_state_v5(pool, channel).await? {
396403
Some(approve_state) => approve_state,
397-
None => return Err(ResponseError::NotFound),
404+
None => return Ok(None),
398405
};
399406

400407
let state_root = approve_state.msg.state_root.clone();
401408

402-
let new_state = latest_new_state_v5(pool, channel, &state_root).await?;
409+
let new_state = match latest_new_state_v5(pool, channel, &state_root).await? {
410+
// TODO: Since it's an approved NewState, then it's safe to make sure it's in `CheckedState`, or if it's not - log the error that the balances are not aligned with the fact it's an Approved NewState and return ResponseError::BadRequest
411+
Some(new_state) => Ok(Some(new_state)),
412+
413+
None => {
414+
// TODO: Log the error since this should never happen and its crucial to the Channel
415+
return Err(ResponseError::BadRequest(
416+
"Fatal error! The NewState for the last ApproveState was not found".to_string(),
417+
));
418+
}
419+
};
403420

404-
Ok(new_state)
421+
new_state
405422
}
406423

407424
#[cfg(test)]

0 commit comments

Comments
 (0)