spacebudz / lucid

Lucid is a library, which allows you to create Cardano transactions and off-chain code for your Plutus contracts in JavaScript, Deno and Node.js.
https://lucid.spacebudz.io
MIT License
339 stars 139 forks source link

Unlock transaction: Uncaught (in promise) Redeemer (Spend, 0): Failed to deserialise PlutusData using UnConstrData: #244

Open NicolasEliasArias opened 7 months ago

NicolasEliasArias commented 7 months ago

Hi! I'm creating a simple transaction to unlock some ada:

    const redeemer = Data.to(100n);

    const tx = await lucid
      .newTx()
      .collectFrom([utxo], redeemer)
      .addSigner(await lucid.wallet.address())
      .attachSpendingValidator(lucidValidator)
      .complete();

And I'm getting this error when .complete() is called:

Uncaught (in promise) Redeemer (Spend, 0): Failed to deserialise PlutusData using UnConstrData: ...

The problem is in the redeemer, I tested multiple ways to create different redeemers and I'm getting similar errors every time:

    const json = Data.fromJson({
       msg: "Hello, World!",
     });
    const redeemer = Data.to(json);
    const redeemer = Data.to(
      new Constr(0, [Buffer.from("Hello World", "utf8").toString("hex")]),
    );
    const redeemer = Data.to(
      new Constr(0, [toHex(Buffer.from("Hello World", "utf8"))]),
    );
    const redeemer = Data.void()
cmorgado commented 7 months ago

Can you provide the redemeer in plutus?

NicolasEliasArias commented 7 months ago

@cmorgado How can I do that?

cmorgado commented 7 months ago

show us your validator ... lucidValidator

NicolasEliasArias commented 7 months ago
const lucidValidator: Script = {
  type: "PlutusV2",
  script: "58f2010000323232323232323222232325333008323232533300b002100114a06644646600200200644a66602200229404c8c94ccc040cdc78010028a511330040040013014002375c60240026eb0c038c03cc03cc03cc03cc03cc03cc03cc03cc020c008c020014dd71801180400399b8f375c6002600e00a91010d48656c6c6f2c20576f726c6421002300d00114984d958c94ccc020cdc3a400000226464a66601a601e0042930b1bae300d00130060041630060033253330073370e900000089919299980618070010a4c2c6eb8c030004c01401058c01400c8c014dd5000918019baa0015734aae7555cf2ab9f5742ae881",
};
NicolasEliasArias commented 7 months ago

2 observations:

in the lucid-cardano package for node js I can't find the utf8ToHex function.

Also tried with this validator and redeemer:

function utf8ToHex(str: string) {
  return Array.from(str).map(c => 
    c.charCodeAt(0) < 128 ? c.charCodeAt(0).toString(16) : 
    encodeURIComponent(c).replace(/\%/g,'').toLowerCase()
  ).join('');
}

const lucidValidator: Script = {
  type: "PlutusV2",
  script: toHex(cbor.encode(fromHex(scriptCompiledCode)))
};

const redeemer = Data.to(new Constr(0, [utf8ToHex("Hello, World!")]));

const tx = await lucid
    .newTx()
    .collectFrom([utxo], redeemer)
    .addSigner(await lucid.wallet.address())
    .attachSpendingValidator(lucidValidator)
    .complete();

And I'm getting this error:

Uncaught (in promise) Redeemer (Spend, 0): unexpected type tag at position 0: expected bytes (definite length)

dchambers commented 4 months ago

2 observations:

in the lucid-cardano package for node js I can't find the utf8ToHex function.

I just hit the same thing, and for posterity it was renamed to fromText from version 0.8.4.

dchambers commented 4 months ago

@NicolasEliasArias, did you end up managing to get past this as I'm now stuck on the same Failed to deserialise PlutusData using UnConstrData issue?