Skip to content

Commit

Permalink
plutus int json serialization fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lisicky committed Mar 12, 2024
1 parent ddcb25f commit 4ba8973
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
2 changes: 1 addition & 1 deletion rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ bech32 = "0.7.2"
hex = "0.4.0"
cfg-if = "1"
hashlink = "0.9.0"
serde_json = "1.0.57"
serde_json = { version = "1.0.57", features = ["arbitrary_precision"] }
num-bigint = "0.4.0"
num-integer = "0.1.45"
# The default can't be compiled to wasm, so it's necessary to use either the 'nightly'
Expand Down
15 changes: 5 additions & 10 deletions rust/src/protocol_types/plutus/plutus_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use cbor_event::{
};

use schemars::JsonSchema;
use serde_json::Number;

#[wasm_bindgen]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
Expand Down Expand Up @@ -540,12 +541,10 @@ pub fn encode_json_value_to_plutus_datum(
) -> Result<PlutusData, JsError> {
use serde_json::Value;
fn encode_number(x: serde_json::Number) -> Result<PlutusData, JsError> {
if let Some(x) = x.as_u64() {
Ok(PlutusData::new_integer(&BigInt::from(x)))
} else if let Some(x) = x.as_i64() {
Ok(PlutusData::new_integer(&BigInt::from(x)))
if let Ok(big_int) = BigInt::from_str(x.as_str()) {
Ok(PlutusData::new_integer(&big_int))
} else {
Err(JsError::from_str("floats not allowed in plutus datums"))
Err(JsError::from_str(&format!("Expected an integer value but got \"{}\"", x)))
}
}
fn encode_string(
Expand Down Expand Up @@ -747,11 +746,7 @@ pub fn decode_plutus_datum_to_json_value(
},
PlutusDataEnum::Integer(bigint) => (
Some("int"),
bigint
.as_int()
.as_ref()
.map(|int| if int.0 >= 0 { Value::from(int.0 as u64) } else { Value::from(int.0 as i64) })
.ok_or_else(|| JsError::from_str(&format!("Integer {} too big for our JSON support", bigint.to_str())))?
Value::Number(Number::from_string_unchecked(bigint.to_str()))
),
PlutusDataEnum::Bytes(bytes) => (Some("bytes"), Value::from(match schema {
PlutusDatumSchema::BasicConversions => {
Expand Down

0 comments on commit 4ba8973

Please sign in to comment.