Skip to content

Commit

Permalink
fix potential overflow issues; refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrokonrad committed Jan 29, 2025
1 parent d0e3739 commit 0038fd6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 17 deletions.
6 changes: 5 additions & 1 deletion examples/typed_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ const api = await globalThis.cardano.nami.enable();
// Assumes you are in a browser environment
lucid.selectWalletFromApi(api);

// Type definition could be auto generated from on-chain script
// (Type definition could be auto generated from on-chain script e.g. Aiken)
const MyDatum = Data.Object({
name: Data.Bytes(),
age: Data.Integer(),
fruits: Data.Enum("Apple", "Banana", { Berry: [Data.Bytes()] }, {
Other: { name: Data.Bytes(), quantity: Data.Integer() },
}),
colors: Data.Array(Data.Bytes()),
description: Data.Nullable(Data.Bytes()),
});
Expand All @@ -23,6 +26,7 @@ export async function send(): Promise<string> {
const datum: typeof MyDatum = {
name: fromText("Lucid"),
age: 0n,
fruits: { Other: { name: "Coconut", quantity: 123n } },
colors: [fromText("Blue"), fromText("Purple")],
description: null,
};
Expand Down
Binary file modified src/core/libs/lucid_core/pkg/lucid_core_bg.wasm
Binary file not shown.
53 changes: 39 additions & 14 deletions src/core/libs/lucid_core/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,11 @@ impl TryFrom<Assets> for Value {
.map(|(asset_name, quantity)| {
Ok((
asset_name.into(),
PositiveCoin::try_from(quantity as u64)
.map_err(CoreError::msg)?,
PositiveCoin::try_from(
u64::try_from(quantity)
.map_err(CoreError::msg)?,
)
.map_err(CoreError::msg)?,
))
})
.collect::<CoreResult<_>>()?,
Expand Down Expand Up @@ -778,7 +781,7 @@ impl TryFrom<TransactionOutput> for Utxo {
.map(|(asset_name, quantity)| {
Ok((
asset_name.clone(),
PositiveCoin::try_from(*quantity as u64)
PositiveCoin::try_from(*quantity)
.map_err(CoreError::msg)?,
))
})
Expand All @@ -803,27 +806,25 @@ impl TryFrom<TransactionOutput> for Utxo {
output.script_ref,
),
};
Ok(Utxo {
Ok(Utxo::from_output(
// placeholder value for input
tx_hash: "00".repeat(32).to_string(),
output_index: 0,
address: Address::from_bytes(&output.0).unwrap().to_bech32().unwrap(),
assets: output.1.into(),
datum: match &output.2 {
Address::from_bytes(&output.0).unwrap().to_bech32().unwrap(),
output.1.into(),
match &output.2 {
Some(option) => match option {
DatumOption::Data(d) => Some(hex::encode(d.0.encode_fragment().unwrap())),
DatumOption::Hash(h) => Some(h.to_string()),
_ => None,
},
_ => None,
},
datum_hash: match &output.2 {
match &output.2 {
Some(option) => match option {
DatumOption::Hash(h) => Some(h.to_string()),
DatumOption::Data(d) => Some(hex::encode(d.0.encode_fragment().unwrap())),
_ => None,
},
_ => None,
},
script_ref: match &output.3 {
match &output.3 {
Some(script_ref) => match &script_ref.0 {
PseudoScript::NativeScript(native_script) => Some(Script::Native {
script: hex::encode(native_script.encode_fragment().unwrap()),
Expand All @@ -840,7 +841,7 @@ impl TryFrom<TransactionOutput> for Utxo {
},
_ => None,
},
})
))
}
}

Expand Down Expand Up @@ -1125,9 +1126,13 @@ impl<A> ConstrConversion<A> for Constr<A> {

#[cfg(test)]
mod tests {
use std::cmp::Ordering;

use crate::codec::ConstrConversion;
use pallas_primitives::{Constr, PlutusData};

use super::Assets;

#[test]
fn compact_tag_range() {
assert_eq!(Constr::from_index(0, Vec::<PlutusData>::new()).tag, 121);
Expand Down Expand Up @@ -1174,4 +1179,24 @@ mod tests {
127
);
}

#[test]
fn test_partial_compare_assets() {
let assets1 = Assets::from([("lovelace".to_string(), 100), ("00".to_string(), 10)]);
let assets2 = Assets::from([("lovelace".to_string(), 100), ("00".to_string(), 12)]);
let assets3 = Assets::from([("lovelace".to_string(), 50), ("00".to_string(), 10)]);
let assets4 = Assets::from([
("lovelace".to_string(), 50),
("00".to_string(), 5),
("11".to_string(), 5),
]);

assert_eq!(assets1.partial_cmp(&assets1), Some(Ordering::Equal));
assert_eq!(assets1.partial_cmp(&assets2), Some(Ordering::Less));
assert_eq!(assets2.partial_cmp(&assets1), Some(Ordering::Greater));
assert_eq!(assets1.partial_cmp(&assets3), Some(Ordering::Greater));
assert_eq!(assets3.partial_cmp(&assets1), Some(Ordering::Less));
assert_eq!(assets1.partial_cmp(&assets4), None);
assert_eq!(assets4.partial_cmp(&assets1), None);
}
}
2 changes: 1 addition & 1 deletion src/lucid/lucid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export class Lucid {
}

/** Query CIP-0068 metadata for a specifc asset. */
async metadataOf<T = Json>(unit: string): Promise<T> {
async metadataOf(unit: string): Promise<Json> {
const { policyId, name, label } = fromUnit(unit);
switch (label) {
case 222:
Expand Down
2 changes: 1 addition & 1 deletion src/lucid/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class Tx {

/**
* All assets should be of the same policy id.
* You can chain mintAssets functions together if you need to mint assets with different policy ids.
* You can chain mint function calls together if you need to mint assets with different policy ids.
* If the plutus script doesn't need a redeemer, you still need to specifiy the void redeemer.
*/
mint(assets: Assets, redeemer?: string): Tx {
Expand Down

0 comments on commit 0038fd6

Please sign in to comment.