Skip to content

Commit

Permalink
fix: gas price denomination
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed Jun 27, 2024
1 parent 5c4f8d9 commit b4698d7
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 42 deletions.
42 changes: 7 additions & 35 deletions orm/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,31 @@
// @generated automatically by Diesel CLI.

pub mod sql_types {
#[derive(
diesel::query_builder::QueryId,
std::fmt::Debug,
diesel::sql_types::SqlType,
)]
#[derive(diesel::query_builder::QueryId, std::fmt::Debug, diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "governance_kind"))]
pub struct GovernanceKind;

#[derive(
diesel::query_builder::QueryId,
std::fmt::Debug,
diesel::sql_types::SqlType,
)]
#[derive(diesel::query_builder::QueryId, std::fmt::Debug, diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "governance_result"))]
pub struct GovernanceResult;

#[derive(
diesel::query_builder::QueryId,
std::fmt::Debug,
diesel::sql_types::SqlType,
)]
#[derive(diesel::query_builder::QueryId, std::fmt::Debug, diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "governance_tally_type"))]
pub struct GovernanceTallyType;

#[derive(
diesel::query_builder::QueryId,
std::fmt::Debug,
diesel::sql_types::SqlType,
)]
#[derive(diesel::query_builder::QueryId, std::fmt::Debug, diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "transaction_kind"))]
pub struct TransactionKind;

#[derive(
diesel::query_builder::QueryId,
std::fmt::Debug,
diesel::sql_types::SqlType,
)]
#[derive(diesel::query_builder::QueryId, std::fmt::Debug, diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "transaction_result"))]
pub struct TransactionResult;

#[derive(
diesel::query_builder::QueryId,
std::fmt::Debug,
diesel::sql_types::SqlType,
)]
#[derive(diesel::query_builder::QueryId, std::fmt::Debug, diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "validator_state"))]
pub struct ValidatorState;

#[derive(
diesel::query_builder::QueryId,
std::fmt::Debug,
diesel::sql_types::SqlType,
)]
#[derive(diesel::query_builder::QueryId, std::fmt::Debug, diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "vote_kind"))]
pub struct VoteKind;
}
Expand Down
40 changes: 40 additions & 0 deletions orm/src/token.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::str::FromStr;

use bigdecimal::BigDecimal;
use diesel::{Insertable, Queryable, Selectable};
use shared::token::Token;

use crate::schema::token;

pub type DenominationDb = i16;

#[derive(Clone, Queryable, Selectable, Insertable)]
#[diesel(table_name = token)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct TokenDb {
pub address: String,
pub denomination: i16,
pub gas_price: BigDecimal,
}

#[derive(Clone, Queryable, Selectable)]
#[diesel(table_name = token)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct GasPriceDb {
#[diesel(column_name = "address")]
pub token: String,
#[diesel(column_name = "gas_price")]
pub raw_amount: BigDecimal,
pub denomination: DenominationDb,
}

impl From<Token> for TokenDb {
fn from(value: Token) -> Self {
Self {
address: value.address,
denomination: value.denomination as i16,
gas_price: BigDecimal::from_str(&value.gas_price.to_string())
.expect("Invalid gas_price amount"),
}
}
}
4 changes: 3 additions & 1 deletion parameters/src/services/namada.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use namada_sdk::queries::RPC;
use namada_sdk::rpc::{
self, get_token_total_supply, get_total_staked_tokens, query_storage_value,
};
use namada_sdk::token::Amount as NamadaSdkAmount;
use namada_sdk::token::{
Amount as NamadaSdkAmount,
};
use shared::balance::Amount;
use shared::block::Epoch;
use shared::gas::GasPrice;
Expand Down
1 change: 1 addition & 0 deletions shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ path = "src/lib.rs"
anyhow.workspace = true
async-stream.workspace = true
bimap.workspace = true
bigdecimal.workspace = true
futures-core.workspace = true
futures-util.workspace = true
futures.workspace = true
Expand Down
47 changes: 44 additions & 3 deletions shared/src/balance.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use std::fmt::Display;

use bigdecimal::BigDecimal;
use fake::Fake;
use namada_sdk::token::Amount as NamadaAmount;
use namada_sdk::token::DenominatedAmount as NamadaDenominatedAmount;
use namada_sdk::token::Denomination as NamadaDenomination;

use crate::id::Id;

Expand All @@ -14,6 +17,21 @@ impl From<NamadaAmount> for Amount {
}
}

impl From<BigDecimal> for Amount {
fn from(amount: BigDecimal) -> Amount {
Amount(
NamadaAmount::from_string_precise(&amount.to_string())
.expect("Invalid amount"),
)
}
}

impl Display for Amount {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

impl Amount {
pub fn checked_add(&self, other: &Self) -> Option<Self> {
self.0.checked_add(other.0).map(Self)
Expand All @@ -24,9 +42,32 @@ impl Amount {
}
}

impl Display for Amount {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
pub type Denomination = u8;

pub struct DenominatedAmount(NamadaDenominatedAmount);

impl DenominatedAmount {
pub fn native(amount: Amount) -> DenominatedAmount {
Self(NamadaDenominatedAmount::native(amount.0))
}

pub fn to_string_precise(&self) -> String {
self.0.to_string_precise()
}
}

impl From<NamadaDenominatedAmount> for DenominatedAmount {
fn from(amount: NamadaDenominatedAmount) -> DenominatedAmount {
DenominatedAmount(amount)
}
}

impl From<(Amount, Denomination)> for DenominatedAmount {
fn from((amount, denom): (Amount, Denomination)) -> DenominatedAmount {
DenominatedAmount(NamadaDenominatedAmount::new(
amount.0,
NamadaDenomination(denom),
))
}
}

Expand Down
1 change: 1 addition & 0 deletions webserver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ namada_sdk.workspace = true
namada_parameters.workspace = true
deadpool-redis = "0.13.0"
bigdecimal.workspace = true
shared.workspace = true

[build-dependencies]
vergen = { version = "8.0.0", features = ["build", "git", "gitcl"] }
4 changes: 3 additions & 1 deletion webserver/src/response/gas.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use orm::gas::{GasDb, GasPriceDb};
use serde::{Deserialize, Serialize};

use crate::service::utils::raw_amount_to_nam;

use super::transaction::TransactionKind;

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -30,7 +32,7 @@ impl From<GasPriceDb> for GasPrice {
fn from(value: GasPriceDb) -> Self {
Self {
token: value.token,
amount: value.amount.to_string(),
amount: raw_amount_to_nam(value.amount.to_string()),
}
}
}
9 changes: 7 additions & 2 deletions webserver/src/service/balance.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::utils::raw_amount_to_nam;
use shared::balance::{Amount, DenominatedAmount};

use crate::appstate::AppState;
use crate::error::balance::BalanceError;
use crate::repository::balance::{BalanceRepo, BalanceRepoTrait};
Expand Down Expand Up @@ -32,7 +33,11 @@ impl BalanceService {
.cloned()
.map(|balance| AddressBalance {
token_address: balance.token,
balance: raw_amount_to_nam(balance.raw_amount.to_string()),
// TODO: change native to new once we support multiple tokens
balance: DenominatedAmount::native(Amount::from(
balance.raw_amount,
))
.to_string_precise(),
})
.collect();

Expand Down

0 comments on commit b4698d7

Please sign in to comment.