massalabs / massa

The Decentralized and Scaled Blockchain
https://massa.net
5.57k stars 711 forks source link

Improve Amount usage in api #4688

Open modship opened 5 months ago

modship commented 5 months ago

First

And then we should also :

modship commented 4 months ago

Some modifications should be done, we can't safely accept Massa and nanoMassa in from str.

So, now the number provided is in nanoMassa not in Massa and we return errors when a decimal is provided

@AurelienFT can u validate if this test verify all case

#[test]
fn test_amount() {
    // decimal now return error
    assert!(Amount::from_str("15463.123").is_err());
    assert!(Amount::from_str("100.0").is_err());

    // from str now uses nanoMassa
    let amount = Amount::from_str("100").unwrap(); // nanoMassa

    // print no longer display massa unit as decimal
    // print display nanoMassa
    println!("{}", amount); // Should print 100 (nanoMassa)

    // to_raw return nanoMassa as u64 like before
    assert_eq!(amount.to_raw(), 100);
    // to_string return nanoMassa as string
    assert_eq!(amount.to_string(), "100".to_string());

    let serialized_serde = serde_json::to_string(&amount).unwrap();
    assert_eq!(serialized_serde, "100".to_string());

    // serde from_str
    let deserialized_serde: Amount = serde_json::from_str(&serialized_serde).unwrap();
    // amount from_str
    let deserialized_amount: Amount = Amount::from_str(&serialized_serde).unwrap();

    assert_eq!(deserialized_serde.to_string(), "100");
    assert_eq!(deserialized_serde.to_raw(), 100);

    assert_eq!(deserialized_amount.to_string(), "100");
    assert_eq!(deserialized_amount.to_raw(), 100);
    assert_eq!(deserialized_serde, deserialized_amount);
}
AurelienFT commented 4 months ago

@modship sorry for the late reply the test seems very effective.

modship commented 4 months ago

impact in config files :

Also interface_impl.rs :

massa-client :

Api :