Skip to content

Commit

Permalink
change back to 10^6 namnam = 1 nam, votes_per_token = 1 namnam
Browse files Browse the repository at this point in the history
  • Loading branch information
brentstone committed Sep 8, 2022
1 parent d7f985e commit 0550abc
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 37 deletions.
8 changes: 4 additions & 4 deletions proof_of_stake/src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ impl Default for PosParams {
max_validator_slots: 128,
pipeline_len: 2,
unbonding_len: 6,
// 1 voting power per 100 fundamental tokens (10^7 / NAM or 0.01 / namnam)
votes_per_token: BasisPoints::new(100),
// 1 voting power per 1 fundamental token (10^6 per NAM or 1 per namnam)
votes_per_token: BasisPoints::new(10000),
block_proposer_reward: 100,
block_vote_reward: 1,
// slash 5%
Expand Down Expand Up @@ -72,12 +72,12 @@ pub enum ValidationError {
}

/// The number of fundamental units per whole token of the native staking token
pub const TOKENS_PER_NAM: u64 = 1_000_000_000;
pub const TOKENS_PER_NAM: u64 = 1_000_000;

/// From Tendermint: <https://github.com/tendermint/tendermint/blob/master/spec/abci/apps.md#updating-the-validator-set>
const MAX_TOTAL_VOTING_POWER: i64 = i64::MAX / 8;

/// Assuming token amount is `u64` in nano units.
/// Assuming token amount is `u64` in micro units.
const TOKEN_MAX_AMOUNT: u64 = u64::MAX / TOKENS_PER_NAM;

impl PosParams {
Expand Down
6 changes: 3 additions & 3 deletions proof_of_stake/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ pub trait PublicKeyTmRawHash {
impl VotingPower {
/// Convert token amount into a voting power.
pub fn from_tokens(tokens: impl Into<u64>, params: &PosParams) -> Self {
// The token amount is expected to be in nano units already
// The token amount is expected to be in micro units already
Self(params.votes_per_token * tokens.into())
}
}
Expand All @@ -393,7 +393,7 @@ impl VotingPowerDelta {
change: impl Into<i128>,
params: &PosParams,
) -> Result<Self, TryFromIntError> {
// The token amount is expected to be in nano units already
// The token amount is expected to be in micro units already
let delta: i128 = params.votes_per_token * change.into();
let delta: i64 = TryFrom::try_from(delta)?;
Ok(Self(delta))
Expand All @@ -404,7 +404,7 @@ impl VotingPowerDelta {
tokens: impl Into<u64>,
params: &PosParams,
) -> Result<Self, TryFromIntError> {
// The token amount is expected to be in nano units already
// The token amount is expected to be in micro units already
let delta: i64 =
TryFrom::try_from(params.votes_per_token * tokens.into())?;
Ok(Self(delta))
Expand Down
52 changes: 26 additions & 26 deletions shared/src/types/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::types::address::{Address, Error as AddressError, InternalAddress};
use crate::types::ibc::data::FungibleTokenPacketData;
use crate::types::storage::{DbKeySeg, Key, KeySeg};

/// Amount in nano units. For different granularity another representation
/// Amount in micro units. For different granularity another representation
/// might be more appropriate.
#[derive(
Clone,
Expand All @@ -30,13 +30,13 @@ use crate::types::storage::{DbKeySeg, Key, KeySeg};
Hash,
)]
pub struct Amount {
nano: u64,
micro: u64,
}

/// Maximum decimal places in a token [`Amount`] and [`Change`].
pub const MAX_DECIMAL_PLACES: u32 = 9;
pub const MAX_DECIMAL_PLACES: u32 = 6;
/// Decimal scale of token [`Amount`] and [`Change`].
pub const SCALE: u64 = 1_000_000_000;
pub const SCALE: u64 = 1_000_000;
const SCALE_F64: f64 = SCALE as f64;

/// A change in tokens amount
Expand All @@ -45,31 +45,31 @@ pub type Change = i128;
impl Amount {
/// Get the amount as a [`Change`]
pub fn change(&self) -> Change {
self.nano as Change
self.micro as Change
}

/// Spend a given amount.
/// Panics when given `amount` > `self.nano` amount.
/// Panics when given `amount` > `self.micro` amount.
pub fn spend(&mut self, amount: &Amount) {
self.nano = self.nano.checked_sub(amount.nano).unwrap();
self.micro = self.micro.checked_sub(amount.micro).unwrap();
}

/// Receive a given amount.
/// Panics on overflow.
pub fn receive(&mut self, amount: &Amount) {
self.nano = self.nano.checked_add(amount.nano).unwrap();
self.micro = self.micro.checked_add(amount.micro).unwrap();
}

/// Create a new amount from whole number of tokens
pub const fn whole(amount: u64) -> Self {
Self {
nano: amount * SCALE,
micro: amount * SCALE,
}
}

/// Create a new amount with the maximum value
pub fn max() -> Self {
Self { nano: u64::MAX }
Self { micro: u64::MAX }
}

/// Create amount from Change
Expand All @@ -79,7 +79,7 @@ impl Amount {
/// Panics if the change is negative or overflows `u64`.
pub fn from_change(change: Change) -> Self {
Self {
nano: change as u64,
micro: change as u64,
}
}
}
Expand Down Expand Up @@ -113,37 +113,37 @@ impl From<Amount> for f64 {
/// Warning: `f64` loses precision and it should not be used when exact
/// values are required.
fn from(amount: Amount) -> Self {
amount.nano as f64 / SCALE_F64
amount.micro as f64 / SCALE_F64
}
}

impl From<f64> for Amount {
/// Warning: `f64` loses precision and it should not be used when exact
/// values are required.
fn from(nano: f64) -> Self {
fn from(micro: f64) -> Self {
Self {
nano: (nano * SCALE_F64).round() as u64,
micro: (micro * SCALE_F64).round() as u64,
}
}
}

impl From<u64> for Amount {
fn from(nano: u64) -> Self {
Self { nano }
fn from(micro: u64) -> Self {
Self { micro }
}
}

impl From<Amount> for u64 {
fn from(amount: Amount) -> Self {
amount.nano
amount.micro
}
}

impl Add for Amount {
type Output = Amount;

fn add(mut self, rhs: Self) -> Self::Output {
self.nano += rhs.nano;
self.micro += rhs.micro;
self
}
}
Expand All @@ -152,29 +152,29 @@ impl Mul<Amount> for u64 {
type Output = Amount;

fn mul(mut self, rhs: Amount) -> Self::Output {
self *= rhs.nano;
self *= rhs.micro;
Self::Output::from(self)
}
}

impl AddAssign for Amount {
fn add_assign(&mut self, rhs: Self) {
self.nano += rhs.nano
self.micro += rhs.micro
}
}

impl Sub for Amount {
type Output = Amount;

fn sub(mut self, rhs: Self) -> Self::Output {
self.nano -= rhs.nano;
self.micro -= rhs.micro;
self
}
}

impl SubAssign for Amount {
fn sub_assign(&mut self, rhs: Self) {
self.nano -= rhs.nano
self.micro -= rhs.micro
}
}

Expand Down Expand Up @@ -204,10 +204,10 @@ impl FromStr for Amount {
}
let whole =
decimal * rust_decimal::Decimal::new(SCALE as i64, 0);
let nano: u64 =
let micro: u64 =
rust_decimal::prelude::ToPrimitive::to_u64(&whole)
.ok_or(AmountParseError::InvalidRange)?;
Ok(Self { nano })
Ok(Self { micro })
}
Err(err) => Err(AmountParseError::InvalidDecimal(err)),
}
Expand All @@ -217,7 +217,7 @@ impl FromStr for Amount {
impl Display for Amount {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let decimal = rust_decimal::Decimal::from_i128_with_scale(
self.nano as i128,
self.micro as i128,
MAX_DECIMAL_PLACES,
)
.normalize();
Expand All @@ -227,7 +227,7 @@ impl Display for Amount {

impl From<Amount> for Change {
fn from(amount: Amount) -> Self {
amount.nano as i128
amount.micro as i128
}
}

Expand Down
6 changes: 3 additions & 3 deletions tests/src/native_vp/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ pub mod testing {
let total_delta = validator_total_deltas
.get_at_offset(current_epoch, offset, params)
.unwrap_or_default();
// Votes_per_token is relative to nano units so no need to convert
// Votes_per_token is relative to micro units so no need to convert
let vp_before = params.votes_per_token * total_delta;
let vp_after = params.votes_per_token * (total_delta + token_delta);
// voting power delta
Expand Down Expand Up @@ -998,7 +998,7 @@ pub mod testing {
let total_delta = validator_total_deltas
.get(epoch)
.unwrap_or_default();
// Votes_per_token is relative to nano units so no need to convert
// Votes_per_token is relative to micro units so no need to convert
let vp_before = params.votes_per_token * total_delta;
let vp_after = params.votes_per_token * (total_delta + token_delta);
// voting power delta
Expand Down Expand Up @@ -1071,7 +1071,7 @@ pub mod testing {
let total_delta_cur = validator_total_deltas_cur
.get_at_offset(current_epoch, offset, params)
.unwrap_or_default();
// Votes_per_token is relative to nano units so no need to convert
// Votes_per_token is relative to micro units so no need to convert
let vp_before = params.votes_per_token * total_delta_cur;
let vp_after = params.votes_per_token * (total_delta_cur + token_delta);
// voting power delta
Expand Down
2 changes: 1 addition & 1 deletion wasm/wasm_source/src/vp_testnet_faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use namada_vp_prelude::{SignedTxData, *};
use once_cell::unsync::Lazy;

/// Allows anyone to withdraw up to 1_000 tokens in a single tx
pub const MAX_FREE_DEBIT: i128 = 1_000_000_000_000; // in nano units
pub const MAX_FREE_DEBIT: i128 = 1_000_000_000; // in micro units

#[validity_predicate]
fn validate_tx(
Expand Down

0 comments on commit 0550abc

Please sign in to comment.