0

How can i generate the CBOR hex of a plutus smart contract. Does it requires to generate CBOR of onchain code only or both onchain and offchain? Also if have a smart contract which requires parameters e.g. deadline. to run, then how will i be able to pass that argument with that cbor at the time of deployment of plutus smart contract. Kindly suggest a recommended way for that and also how to interact efficiently with that CBPOR.

2 Answers 2

-1

You need to create a deploy file that adds parameters to the plutus script and then generates the plutus script to be submitted to the blockchain. If you look at the generated file, you will see the following contents including the cborHex.

$ cat vesting.plutus | jq
{
  "type": "PlutusScriptV2",
  "description": "",
  "cborHex": "5901e55901e20100003232323232323232323232323232323232323232323232323232323232323232323232323232222533357346660080060040022930b1111919191992999ab9a3370e002900009198168998141981300218048041815a49055653313a200013302833019005300b008302b491055653323a2000123302d133028330160043009008302b4901055653333a200013302833019005300a008302b491055653343a200035573a6ea8004004010c040c038004c03000401400c0048d5d09bac001235742600800246ae84c0080048d5d1180100091aba2375800200800246ae84c02c0048c008c008c0080048d5d098080008009191180219b89002375a0026eb40048ccd5cd000a504a2446601c6eb0c00c0088cdd780080100091aba13002001235744600400246ae88c0080048d5d1180100091aba23002001235744600400246ae88c0080048d5d1180100091aba23002001235573c6ea800488c8ccc00400400c0088894ccd5cd1aba300214a02a666ae68c004d5d08010a51133300300335744004002002464466e20008dd68009bad00122335738600c66008002a6006004004426006666ae6800524104747275650049010566616c736500232230043372c004600a002600600246ea4dcc00091b99375c00244a666ae694008540045281"
}

Here is the old Haskell PlutusTx V2 Way:

https://github.com/lley154/traceability-smart-contract/blob/main/src/Traceability/V2/Deploy.hs

Here is the better Helios way:

https://github.com/lley154/helios-examples/blob/main/vesting/src/deploy-vesting.js

Find our more about Helios here:

Helios Github repo https://github.com/Hyperion-BT/Helios

Cardano Smart Contracts with Helios Book: https://www.amazon.com/Cardano-Smart-Contracts-Helios-Lawrence-ebook/dp/B0BTBK6YM7/

Helios examples: https://github.com/lley154/helios-examples

2
  • Hi, Actually i am implementing onchain code in Plutus and offchain in Mesh library. So i am puzzled about how can i interact with a parameterized onchain code. Like how to generate the CBOR in the case where the parameters have to be passed runTime like UTXO. And also how can i validate the customized datums made onchain side. E.g. in case of Vesting contract. I created a custom datum with params of deadline (POSIX) and beneficiary (PayPubKeyHash). But when i pass the same datums on offchain side, my script doesnot detect them and gives the error of "MissingRequiredDatums". Kindly guide me.
    – duaa azhar
    Feb 13, 2023 at 8:48
  • One of the big advantages of Helios is that you can compile code to plutus on the fly in the browser. So this inline Helios code can be passed a UTXO from a browser wallet that will be used in a transaction and then on the fly, be compiled into plutus code and submitted with the transaction. I'll post the code in a answer so it is formatted better.
    – lley154
    Feb 13, 2023 at 13:30
-1

One of the big advantages of Helios is that you can compile code to plutus on the fly in the browser. So this inline Helios code can be passed a UTXO from a browser wallet that will be used in a transaction and then on the fly, compiled into plutus code and submitted with the transaction.

...
   // Off-chain code
   // Get wallet UTXOs
   const walletHelper = new WalletHelper(walletAPI);
   const utxos = await walletHelper.pickUtxos(minUTXOVal);
...

   // On-chain code in mintScript variable
   const mintScript =`minting nft
   const TX_ID: ByteArray = #` + utxos[0][0].txId.hex + `
   const txId: TxId = TxId::new(TX_ID)
   const outputId: TxOutputId = TxOutputId::new(txId, ` + utxos[0][0].utxoIdx + `)

   func main(ctx: ScriptContext) -> Bool {
       tx: Tx = ctx.tx;
       mph: MintingPolicyHash = ctx.get_current_minting_policy_hash();
...

    // Off-chain code
    // Compile the helios minting script
    const mintProgram = Program.new(mintScript).compile(optimize);

    // Add the script as a witness to the transaction
    tx.attachScript(mintProgram);
...

https://github.com/lley154/helios-examples/blob/main/nft/pages/index.tsx

For Datums, you created them during transaction construction and then include them in your transaction. I’ll use Helios as an example because the code is very easy to understand.

Here is the helios code that gets compiled into plutus code (untyped plutus core).

On-chain code

spending vesting

struct Datum {
    creator: PubKeyHash
    beneficiary: PubKeyHash
    deadline: Time
}

enum Redeemer {
    Cancel
    Claim
}

func main(datum: Datum, redeemer: Redeemer, context: ScriptContext) -> Bool {
    tx: Tx = context.tx;
    now: Time = tx.time_range.start;

    redeemer.switch {
        Cancel => {
            // Check if deadline hasn't passed
            (now < datum.deadline).trace("VS1: ") && 

            // Check that the owner signed the transaction
            tx.is_signed_by(datum.creator).trace("VS2: ")
        },
        Claim => {
           // Check if deadline has passed.
           (now > datum.deadline).trace("VS3: ") &&

           // Check that the beneficiary signed the transaction.
           tx.is_signed_by(datum.beneficiary).trace("VS4: ")
        }
    }
}

https://github.com/lley154/helios-examples/blob/main/vesting/src/vesting.hl

off-chain code

...
   // Construct the datum
   const datum = new ListData([new ByteArrayData(ownerPkh.bytes),
                                 new ByteArrayData(benPkh.bytes),
                                 new IntData(BigInt(deadline.getTime()))]);
   const inlineDatum = Datum.inline(datum);
...

   // Add the destination address and the amount of Ada to lock including a datum
   tx.addOutput(new TxOutput(valAddr, lockedVal, inlineDatum));

https://github.com/lley154/helios-examples/blob/main/vesting/pages/index.tsx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.