Skip to content

Commit 6a908af

Browse files
authored
Merge pull request #492 from AmbireTech/bugfix-unified-num-math-operations
Bugfix UnifiedNum math operations
2 parents a7c9338 + 3f52579 commit 6a908af

File tree

12 files changed

+1149
-352
lines changed

12 files changed

+1149
-352
lines changed

adapter/src/ethereum/test_util.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use web3::{
1111
use primitives::{
1212
channel::{Channel, Nonce},
1313
config::{ChainInfo, TokenInfo, GANACHE_CONFIG},
14-
test_util::{ADVERTISER, CREATOR, FOLLOWER, GUARDIAN, GUARDIAN_2, LEADER, PUBLISHER},
14+
test_util::{
15+
ADVERTISER, ADVERTISER_2, CREATOR, FOLLOWER, GUARDIAN, GUARDIAN_2, LEADER, PUBLISHER,
16+
},
1517
Address, BigNum, Chain, ValidatorId,
1618
};
1719

@@ -83,6 +85,15 @@ pub static KEYSTORES: Lazy<HashMap<Address, Options>> = Lazy::new(|| {
8385
*GUARDIAN_2,
8486
keystore_options(&format!("{}_keystore.json", *GUARDIAN_2), "ganache6"),
8587
),
88+
// Address 7
89+
// (
90+
// *PUBLISHER_2,
91+
// keystore_options(&format!("{}_keystore.json", *PUBLISHER_2), "ganache7"),
92+
// ),
93+
(
94+
*ADVERTISER_2,
95+
keystore_options(&format!("{}_keystore.json", *ADVERTISER_2), "ganache8"),
96+
),
8697
]
8798
.into_iter()
8899
.collect()

primitives/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub mod targeting;
4848
#[cfg(feature = "test-util")]
4949
#[cfg_attr(docsrs, doc(cfg(feature = "test-util")))]
5050
pub mod test_util;
51-
mod unified_num;
51+
pub mod unified_num;
5252
pub mod validator;
5353

5454
/// This module is available with the `postgres` feature.

primitives/src/targeting/eval.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::UnifiedNum;
1+
use crate::{unified_num::FromWhole, UnifiedNum};
22
use serde::{Deserialize, Serialize};
33
use serde_json::{value::Value as SerdeValue, Number};
44
use std::{
@@ -453,15 +453,27 @@ impl Value {
453453
}
454454
}
455455

456-
/// The UnifiedNum can be extracted from the DSL either String or UnifiedNum
456+
/// The UnifiedNum can be expressed in the DSL either with a String or UnifiedNum
457457
impl TryFrom<Value> for UnifiedNum {
458458
type Error = Error;
459459
fn try_from(value: Value) -> Result<Self, Self::Error> {
460460
match value {
461461
Value::String(string) => UnifiedNum::from_str(&string).map_err(|_| Error::TypeError),
462462
Value::UnifiedNum(unified) => Ok(unified),
463463
Value::Number(number) => {
464-
UnifiedNum::from_str(&number.to_string()).map_err(|_| Error::TypeError)
464+
if number.is_u64() {
465+
// a whole number
466+
let whole_number = number.as_u64().ok_or(Error::TypeError)?;
467+
468+
UnifiedNum::from_whole_opt(whole_number).ok_or(Error::TypeError)
469+
} else if number.is_f64() {
470+
// a floating point whole number
471+
let whole_number = number.as_f64().ok_or(Error::TypeError)?;
472+
473+
UnifiedNum::from_whole_opt(whole_number).ok_or(Error::TypeError)
474+
} else {
475+
Err(Error::TypeError)
476+
}
465477
}
466478
_ => Err(Error::TypeError),
467479
}

0 commit comments

Comments
 (0)