Skip to content

Commit a0b8488

Browse files
committed
merged adex v5 branch, changed code accordingly
1 parent 49f1a38 commit a0b8488

File tree

1 file changed

+26
-46
lines changed

1 file changed

+26
-46
lines changed

sentry/src/routes/channel.rs

Lines changed: 26 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use primitives::{
1515
adapter::Adapter,
1616
balances::UncheckedState,
1717
channel_v5::Channel as ChannelV5,
18-
config::{Config, TokenInfo},
18+
config::TokenInfo,
1919
sentry::{
2020
channel_list::{ChannelListQuery, LastApprovedQuery},
2121
AllSpendersResponse, LastApproved, LastApprovedResponse, MessageResponse, SpenderResponse,
@@ -290,21 +290,27 @@ pub async fn get_spender_limits<A: Adapter + 'static>(
290290

291291
let spender = Address::from_str(&route_params.index(1))?;
292292

293-
let latest_spendable = get_latest_spendable(
294-
app.pool.clone(),
295-
&app.config,
296-
&app.adapter,
297-
&spender,
298-
&channel,
299-
)
300-
.await?;
293+
let latest_spendable = fetch_spendable(app.pool.clone(), &spender, &channel.id()).await?;
301294

302-
let approve_state = match latest_approve_state_v5(&app.pool, &channel).await? {
303-
Some(approve_state) => approve_state,
304-
None => return spender_response_without_leaf(latest_spendable.deposit.total),
305-
};
295+
let token_info = app
296+
.config
297+
.token_address_whitelist
298+
.get(&channel.token)
299+
.ok_or_else(|| ResponseError::FailedValidation("Unsupported Channel Token".to_string()))?;
306300

307-
let state_root = approve_state.msg.state_root.clone();
301+
let latest_spendable = match latest_spendable {
302+
Some(spendable) => spendable,
303+
None => {
304+
create_or_update_spendable_document(
305+
&app.adapter,
306+
token_info,
307+
app.pool.clone(),
308+
&channel,
309+
spender,
310+
)
311+
.await?
312+
}
313+
};
308314

309315
let new_state = match get_corresponding_new_state(&app.pool, &channel).await {
310316
Some(new_state) => new_state,
@@ -330,29 +336,6 @@ pub async fn get_spender_limits<A: Adapter + 'static>(
330336
Ok(success_response(serde_json::to_string(&res)?))
331337
}
332338

333-
async fn get_latest_spendable(
334-
pool: DbPool,
335-
config: &Config,
336-
adapter: &impl Adapter,
337-
spender: &Address,
338-
channel: &ChannelV5,
339-
) -> Result<Spendable, ResponseError> {
340-
let latest_spendable = fetch_spendable(pool.clone(), spender, &channel.id()).await?;
341-
let token_info = config
342-
.token_address_whitelist
343-
.get(&channel.token)
344-
.ok_or_else(|| ResponseError::FailedValidation("Unsupported Channel Token".to_string()))?;
345-
346-
let latest_spendable = match latest_spendable {
347-
Some(spendable) => spendable,
348-
None => {
349-
create_spendable_document(adapter, token_info, pool.clone(), channel, *spender).await?
350-
}
351-
};
352-
353-
Ok(latest_spendable)
354-
}
355-
356339
pub async fn get_all_spender_limits<A: Adapter + 'static>(
357340
req: Request<Body>,
358341
app: &Application<A>,
@@ -371,14 +354,11 @@ pub async fn get_all_spender_limits<A: Adapter + 'static>(
371354

372355
// Using for loop to avoid async closures
373356
for (spender_addr, balance) in new_state.msg.balances.spenders.iter() {
374-
let latest_spendable = get_latest_spendable(
375-
app.pool.clone(),
376-
&app.config,
377-
&app.adapter,
378-
spender_addr,
379-
&channel,
380-
)
381-
.await?;
357+
let latest_spendable =
358+
match fetch_spendable(app.pool.clone(), spender_addr, &channel.id()).await? {
359+
Some(spendable) => spendable,
360+
None => return Err(ResponseError::NotFound),
361+
};
382362

383363
let total_deposited = latest_spendable.deposit.total;
384364
let spender_leaf = get_spender_leaf_for_spender(balance, &total_deposited);
@@ -401,7 +381,7 @@ pub async fn get_all_spender_limits<A: Adapter + 'static>(
401381
async fn get_corresponding_new_state(
402382
pool: &DbPool,
403383
channel: &ChannelV5,
404-
) -> Option<MessageResponse<NewState>> {
384+
) -> Option<MessageResponse<NewState<UncheckedState>>> {
405385
let approve_state = match latest_approve_state_v5(pool, channel).await.ok()? {
406386
Some(approve_state) => approve_state,
407387
None => return None,

0 commit comments

Comments
 (0)