bloxbean / cardano-client-lib

Cardano client library in Java
https://cardano-client.dev
MIT License
120 stars 51 forks source link

Support to apply params to script #347

Open iTanChi opened 12 months ago

iTanChi commented 12 months ago

In some cases, we need to pass the param to script at run time instead of build time to get the final script

Lucid lib supports this with rust lib: https://github.com/spacebudz/lucid/blob/457c156e74ef49ce35823aa2bca91b3bbc585221/src/core/libs/cardano_multiplatform_lib/src/tx_builder_utils.rs#L79C15-L79C15

#[wasm_bindgen]
pub fn apply_params_to_plutus_script(
    params: &PlutusList,
    plutus_script: PlutusScript,
) -> Result<PlutusScript, JsError> {
    match uplc::tx::apply_params_to_script(&params.to_bytes(), &plutus_script.bytes()) {
        Ok(res) => Ok(PlutusScript::new(res)),
        Err(err) => Err(JsError::from_str(&err.to_string())),
    }
}
satran004 commented 12 months ago

@iTanChi Parameterized contracts are already supported through aiken-java-binding library (0.0.8).

You can check these tests https://github.com/bloxbean/cardano-client-lib/blob/76496f51f458e0caa8f57758af738ec5bc740f6a/quicktx/src/it/java/com/bloxbean/cardano/client/quicktx/ParameterizedScriptIT.java#L116

iTanChi commented 12 months ago

Could this be done without aiken? I really want to use only this client lib instead of aiken lib

satran004 commented 12 months ago

@iTanChi Currently, there is no UPLC evaluator in pure Java. However, we are not using the Aiken programming language here. Instead, 'aiken-java-binding' utilizes a required library (through JNI) from Aiken to apply parameters to compiled code, so you don't need to add any additional dependencies.

I believe you don't need to write your contract in Aiken. You can simply use the compiled code from your contract (written in other languages like PlutusTx or Helios) and apply the parameters. I need to test this, but you can give it a try.

satran004 commented 12 months ago

Fyi, Lucid JS also follows the same approach. It's using Aiken's rust library through wasm to support this feature.