Skip to content

Commit

Permalink
refactor: returned shared structs from services
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed Jul 30, 2024
1 parent 7746a2a commit 7d4410e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
15 changes: 14 additions & 1 deletion orm/src/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::str::FromStr;

use bigdecimal::BigDecimal;
use diesel::{Insertable, Queryable, Selectable};
use shared::balance::Balance;
use shared::{
balance::{Amount, Balance},
id::Id,
};

use crate::schema::balances;

Expand All @@ -27,3 +30,13 @@ impl BalancesInsertDb {
}
}
}

impl From<BalanceDb> for Balance {
fn from(balance: BalanceDb) -> Self {
Self {
owner: Id::from_account_str(&balance.owner),
token: Id::from_account_str(&balance.token),
amount: Amount::from(balance.raw_amount),
}
}
}
8 changes: 8 additions & 0 deletions shared/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,11 @@ impl From<common::PublicKey> for Id {
Id::Account(value.to_string())
}
}

impl Id {
pub fn from_account_str(address: &str) -> Self {
let account =
NamadaAddress::from_str(address).expect("Invalid address");
Self::from(account)
}
}
13 changes: 12 additions & 1 deletion webserver/src/handler/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use axum::extract::{Path, State};
use axum::http::HeaderMap;
use axum::Json;
use axum_macros::debug_handler;
use shared::balance::DenominatedAmount;

use crate::error::api::ApiError;
use crate::response::balance::AddressBalance;
Expand All @@ -15,5 +16,15 @@ pub async fn get_address_balance(
) -> Result<Json<Vec<AddressBalance>>, ApiError> {
let balances = state.balance_service.get_address_balances(address).await?;

Ok(Json(balances))
let balances_response: Vec<AddressBalance> = balances
.iter()
.map(|balance| AddressBalance {
token_address: balance.token.to_string(),
// TODO: temporary solution as we only store NAM balances
balance: DenominatedAmount::native(balance.amount.clone())
.to_string_precise(),
})
.collect();

Ok(Json(balances_response))
}
24 changes: 6 additions & 18 deletions webserver/src/service/balance.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use shared::balance::{Amount, DenominatedAmount};
use shared::balance::{Amount, Balance, DenominatedAmount};

Check failure on line 1 in webserver/src/service/balance.rs

View workflow job for this annotation

GitHub Actions / Clippy

unused imports: `Amount`, `DenominatedAmount`

Check failure on line 1 in webserver/src/service/balance.rs

View workflow job for this annotation

GitHub Actions / Build

unused imports: `Amount`, `DenominatedAmount`

use crate::appstate::AppState;
use crate::error::balance::BalanceError;
use crate::repository::balance::{BalanceRepo, BalanceRepoTrait};
use crate::response::balance::AddressBalance;

#[derive(Clone)]
pub struct BalanceService {
Expand All @@ -20,27 +19,16 @@ impl BalanceService {
pub async fn get_address_balances(
&self,
address: String,
) -> Result<Vec<AddressBalance>, BalanceError> {
let balances = self
) -> Result<Vec<Balance>, BalanceError> {
let balances_db = self
.balance_repo
.get_address_balances(address)
.await
.map_err(BalanceError::Database)?;

// TODO: temporary solution as we only store NAM balances
let denominated_balances: Vec<AddressBalance> = balances
.iter()
.cloned()
.map(|balance| AddressBalance {
token_address: balance.token,
// TODO: change native to new once we support multiple tokens
balance: DenominatedAmount::native(Amount::from(
balance.raw_amount,
))
.to_string_precise(),
})
.collect();
let balances: Vec<Balance> =
balances_db.iter().cloned().map(Balance::from).collect();

Ok(denominated_balances)
Ok(balances)
}
}

0 comments on commit 7d4410e

Please sign in to comment.