diff --git a/CHANGELOG.md b/CHANGELOG.md index 3255dd9a..382dec59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,8 @@ ### Changed -- **aiken**: Fixed overflow issue by changing `i64` to `i128` in `BigInt::Int` instances +- **uplc**: Fixed overflow issue by changing `i64` to `i128` in `BigInt::Int` instances +- **uplc**: Added `apply_params_to_script` function (applies params to script and serializes the new script). ## [v0.0.20] - 2022-10-17 diff --git a/crates/uplc/src/tx.rs b/crates/uplc/src/tx.rs index cf0053e8..daa3aeeb 100644 --- a/crates/uplc/src/tx.rs +++ b/crates/uplc/src/tx.rs @@ -9,7 +9,11 @@ pub use eval::get_script_and_datum_lookup_table; pub use phase_one::eval_phase_one; use script_context::{ResolvedInput, SlotConfig}; -use crate::machine::cost_model::ExBudget; +use crate::{ + ast::{DeBruijn, Program}, + machine::cost_model::ExBudget, + PlutusData, +}; pub mod error; mod eval; @@ -130,3 +134,25 @@ pub fn eval_phase_two_raw( _ => todo!("Wrong era. Please use babbage"), } } + +pub fn apply_params_to_script( + params_bytes: &Vec, // PlutusData array + plutus_script_bytes: &Vec, +) -> Result, Error> { + let params = match PlutusData::decode_fragment(params_bytes).unwrap() { + PlutusData::Array(res) => res, + _ => unreachable!(), + }; + + let mut buffer = Vec::new(); + let mut program = Program::::from_cbor(plutus_script_bytes, &mut buffer)?; + + for param in params { + program = program.apply_data(param); + } + + match program.to_cbor() { + Ok(res) => Ok(res), + Err(_) => Err(Error::ApplyParamsError), + } +} diff --git a/crates/uplc/src/tx/error.rs b/crates/uplc/src/tx/error.rs index b6807d2c..16679fe0 100644 --- a/crates/uplc/src/tx/error.rs +++ b/crates/uplc/src/tx/error.rs @@ -55,4 +55,6 @@ pub enum Error { index: u32, err: Box, }, + #[error("Failed to apply parameters to Plutus script.")] + ApplyParamsError, }