Skip to content
This repository has been archived by the owner on Jul 29, 2022. It is now read-only.

Commit

Permalink
Allow 1.0 as a commission value
Browse files Browse the repository at this point in the history
  • Loading branch information
Demi-Marie committed Sep 25, 2020
1 parent 8e2bd5e commit 3d4e5d9
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/bin/ledgeracio/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,18 @@ pub fn pad(mut zeros: u8, value: u128) -> String {
}

pub fn parse_ppb(data: &str) -> Result<u32, Error> {
if data == "1" {
if data.len() > 11 {
Err("Commission too long. Check for excess trailing zeroes."
.to_owned()
.into())
} else if data == "1" {
Ok(1_000_000_000)
} else if data.starts_with("1.") {
for i in data[2..].bytes() {
if i != b'0' {
return Err("Commission cannot exceed 1".to_owned().into())
}
}
Ok(1_000_000_000)
} else if data == "0" {
Ok(0)
Expand All @@ -154,10 +165,6 @@ pub fn parse_ppb(data: &str) -> Result<u32, Error> {
.to_owned()
.into(),
)
} else if data.len() > 11 {
Err("Commission too long. Check for excess trailing zeroes."
.to_owned()
.into())
} else {
let mut len = data.len() - 2;
let mut res: u32 = str::parse(&data[2..])?;
Expand All @@ -184,6 +191,8 @@ mod tests {

#[test]
fn parse_ppb_works() {
assert_eq!(parse_ppb("1.000000000").unwrap(), 1_000_000_000);
assert!(parse_ppb("1.0000000000").is_err());
assert_eq!(parse_ppb("1").unwrap(), 1_000_000_000);
assert_eq!(parse_ppb("0").unwrap(), 0);
assert!(parse_ppb("0.").is_err());
Expand Down

0 comments on commit 3d4e5d9

Please sign in to comment.