Skip to content

Commit

Permalink
feat: add from Balance to BalanceAddress response
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed Jul 30, 2024
1 parent 7d4410e commit e2206ba
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 20 deletions.
6 changes: 2 additions & 4 deletions orm/src/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use std::str::FromStr;

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

use crate::schema::balances;

Expand Down
12 changes: 2 additions & 10 deletions webserver/src/handler/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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 @@ -16,15 +15,8 @@ pub async fn get_address_balance(
) -> Result<Json<Vec<AddressBalance>>, ApiError> {
let balances = state.balance_service.get_address_balances(address).await?;

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();
let balances_response: Vec<AddressBalance> =
balances.iter().cloned().map(AddressBalance::from).collect();

Ok(Json(balances_response))
}
12 changes: 7 additions & 5 deletions webserver/src/response/balance.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use orm::balances::BalanceDb;
use serde::{Deserialize, Serialize};
use shared::balance::{Balance, DenominatedAmount};

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
Expand All @@ -8,11 +8,13 @@ pub struct AddressBalance {
pub balance: String,
}

impl From<BalanceDb> for AddressBalance {
fn from(value: BalanceDb) -> Self {
impl From<Balance> for AddressBalance {
fn from(value: Balance) -> Self {
Self {
token_address: value.token,
balance: value.raw_amount.to_string(),
token_address: value.token.to_string(),
// TODO: temporary solution as we only store NAM balances
balance: DenominatedAmount::native(value.amount.clone())
.to_string_precise(),
}
}
}
2 changes: 1 addition & 1 deletion webserver/src/service/balance.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use shared::balance::{Amount, Balance, DenominatedAmount};
use shared::balance::Balance;

use crate::appstate::AppState;
use crate::error::balance::BalanceError;
Expand Down

0 comments on commit e2206ba

Please sign in to comment.