0

At the bottom of this post are lines 52-68 of file lucid-vesting of week 4 (PPP fourth iteration). I am having a hard time understanding line 60:

const dtm: Datum = Data.to<VestingDatum>(datum,VestingDatum);

Why are there two instances of VestingDatum? It seems that the command to is converting datum to VestingDatum, but datum had already been declared to be of type VestingDatum.

// Creating a datum with a beneficiary and deadline
const datum: VestingDatum = {
    beneficiary: beneficiaryPKH,
    deadline: deadlinePosIx,
};

// An asynchronous function that sends an amount of Lovelace to the script with the above datum.
async function vestFunds(amount: bigint): Promise<TxHash> {
    const dtm: Datum = Data.to<VestingDatum>(datum,VestingDatum);
    const tx = await lucid
      .newTx()
      .payToContract(vestingAddress, { inline: dtm }, { lovelace: amount })
      .complete();
    const signedTx = await tx.sign().complete();
    const txHash = await signedTx.submit();
    return txHash
}

1 Answer 1

4

Lucid also has a minimal example for typed data here.

The function Data.to has the following signature (as seen from here):

/**
 * Convert PlutusData to Cbor encoded data.\
 * Or apply a shape and convert the provided data struct to Cbor encoded data.
 */
function to<T = Data>(data: T, shape?: Json): Datum | Redeemer {

Where the type Data which is a wrapper around your usual Plutus primitives (see this).

Lucid defines higher types here which correspond to their serialized version as one would expect.

Now seeing the definition of function to from Lucid's source code, you'll realize that it first wants to obtain this Data (this line) and then convert it for corresponding plutus types (the serialize function) in Cardano Multiplatform Library. Now either you can play with so called raw/untyped datum (like in this example) in which case your T above will simply be that Data or you can play at a bit higher type level (like you would do when writing Plutus contracts) where lucid helps you get this T using Data.Static as seen here. The second parameter as you would understand by now, is not at type level and is for this - the shape of your structure. Also Json is essentially any type.

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.