<p>Yes, you're correct. When creating an output for a transaction, by default you only need to provide the datum hash. The consumer of that output is by default required to provide the datum that matches that hash.</p>
<p>If you want to provide the datum nonetheless for an output that you're creating in order to do assertions on it inside your spending validator you can do so like this:</p>
<pre><code>const scripts = PlutusScripts.new();
scripts.add(PlutusScript.from_bytes(Buffer.from(&lt;your script contract cborHex&gt;, 'hex')));

const datumFields = CSL.PlutusList.new();
datumFields.add(CSL.PlutusData.new_integer(CSL.BigInt.from_str( ... )));
const datumConstr = CSL.ConstrPlutusData.new(
    CSL.BigNum.from_str('0'),
    datumFields
);
const datum = CSL.PlutusData.new_constr_plutus_data(datumConstr);

const transactionWitnessSet = CSL.TransactionWitnessSet.new();
transactionWitnessSet.set_plutus_scripts(scripts);
transactionWitnessSet.set_plutus_data(datum);
transactionWitnessSet.set_redeemers(...);

txBody.set_script_data_hash(hash_script_data(..., getCostModels(), datum));

{...}

export const COST_MODEL = [197209, 0, 1, 1, 396231, 621, 0, 1, 150000, 1000, 0, 1, 150000, 32, 2477736, 29175, 4, 29773, 100, 29773, 100, 29773, 100, 29773, 100, 29773, 100, 29773, 100, 100, 100, 29773, 100, 150000, 32, 150000, 32, 150000, 32, 150000, 1000, 0, 1, 150000, 32, 150000, 1000, 0, 8, 148000, 425507, 118, 0, 1, 1, 150000, 1000, 0, 8, 150000, 112536, 247, 1, 150000, 10000, 1, 136542, 1326, 1, 1000, 150000, 1000, 1, 150000, 32, 150000, 32, 150000, 32, 1, 1, 150000, 1, 150000, 4, 103599, 248, 1, 103599, 248, 1, 145276, 1366, 1, 179690, 497, 1, 150000, 32, 150000, 32, 150000, 32, 150000, 32, 150000, 32, 150000, 32, 148000, 425507, 118, 0, 1, 1, 61516, 11218, 0, 1, 150000, 32, 148000, 425507, 118, 0, 1, 1, 148000, 425507, 118, 0, 1, 1, 2477736, 29175, 4, 0, 82363, 4, 150000, 5000, 0, 1, 150000, 32, 197209, 0, 1, 1, 150000, 32, 150000, 32, 150000, 32, 150000, 32, 150000, 32, 150000, 32, 150000, 32, 3345831, 1, 1];
export const getCostModel = () =&gt; {
  const costModel = CostModel.new();
  COST_MODEL.forEach((x, i) =&gt; costModel.set(i, Int.new_i32(x)));
  const costModels = Costmdls.new();
  costModels.insert(Language.new_plutus_v1(), costModel);
  return costModels;
}

</code></pre>
<p>For more examples I recommend the following resources:</p>
<p><a href="https://github.com/dynamicstrategies/cardano-wallet-connector/blob/master/src/App.js" rel="nofollow noreferrer">DApp Connector Examples</a></p>
<p><a href="https://github.com/SpaceBudz/spacebudz/blob/c5571863c4c8b370033ee72969c95d931d2fba6b/src/cardano/market/index.js#L985" rel="nofollow noreferrer">SpaceBudz Datum Creation</a></p>

CC BY-SA 4.0
2022-05-27T00:21:38.193Z
I am running into similar issue reported by bakon (from the reply to my question). It appears the CSL is not accounting for the datum in the output, so the fee is being miscalculated.
Latheesan
CC BY-SA 4.0
2022-05-25T08:51:22.930Z
2022-05-25T08:51:22.930Z
Will
<p>I generate my datum like so:</p>
<pre><code>// Build datum
const sellerAddressPKH = changeAddressValue.slice(2,58);
const datumFields = CSL.PlutusList.new();
datumFields.add(CSL.PlutusData.new_integer(CSL.BigInt.from_str( ... )));
const datumConstr = CSL.ConstrPlutusData.new(
    CSL.BigNum.from_str('0'),
    datumFields
);
const datum = CSL.PlutusData.new_constr_plutus_data(datumConstr);
const datumHash = CSL.hash_plutus_data(datum);
</code></pre>
<p>Part of the <code>output</code> where I am sending native asset into a smart contract, I am attaching the datum hash like so:</p>
<pre><code>txBuilder.add_output(
    CSL.TransactionOutputBuilder
        .new()
        .with_address(scriptAddress)
        .with_data_hash(datumHash)
        .next()
        .with_asset_and_min_required_coin(
            multiAsset,
            CSL.BigNum.from_str(protocolParameters.costPerWord.toString())
        )
        .build()
);
</code></pre>
<p>This only appears to be attaching the datum hash, and I cannot easily see how you would attach the datum <code>value</code> with the above output.</p>
<p>Any ideas?</p>

CC BY-SA 4.0
2022-05-26T04:00:58.237Z
So I am able to get the lib to include values in cbor now with witnesses.set_plutus_data(datumFields); however that doesn't get included in add_change_if_needed and when I try txBuilder.set_plutus_data(datumFields) I get function doesn't exist. witnesses = CardanoWasm.TransactionWitnessSet.new();
bakon
CC BY-SA 4.0
2022-05-27T07:25:52.400Z
You call set_plutus_data on the transaction witness set not the txBuilder.
Will
<smart-contract><cardano-serialization-lib><datum><datum-value>
Attaching Datum Value with CSL 10.x
# Attaching Datum Value with CSL 10.x

(Asked by: username_0 on 2022-05-25)

I generate my datum like so:
// Build datum
const sellerAddressPKH = changeAddressValue.slice(2,58);
const datumFields = CSL.PlutusList.new();
datumFields.add(CSL.PlutusData.new_integer(CSL.BigInt.from_str( ... )));
const datumConstr = CSL.ConstrPlutusData.new(
    CSL.BigNum.from_str('0'),
    datumFields
);
const datum = CSL.PlutusData.new_constr_plutus_data(datumConstr);
const datumHash = CSL.hash_plutus_data(datum);

Part of the output where I am sending native asset into a smart contract, I am attaching the datum hash like so:
txBuilder.add_output(
    CSL.TransactionOutputBuilder
        .new()
        .with_address(scriptAddress)
        .with_data_hash(datumHash)
        .next()
        .with_asset_and_min_required_coin(
            multiAsset,
            CSL.BigNum.from_str(protocolParameters.costPerWord.toString())
        )
        .build()
);

This only appears to be attaching the datum hash, and I cannot easily see how you would attach the datum value with the above output.
Any ideas?

**Tags:** smart-contract, cardano-serialization-lib, datum, datum-value
> **Comments:**
> **username_1**: So I am able to get the lib to include values in cbor now with witnesses.set_plutus_data(datumFields); however that doesn't get included in add_change_if_needed and when I try txBuilder.set_plutus_data(datumFields) I get function doesn't exist. witnesses = CardanoWasm.TransactionWitnessSet.new();  
> **username_2**: You call set_plutus_data on the transaction witness set not the txBuilder.  

---

## 1 answers

### Answer by username_2 on 2022-05-25. Score: 1

Yes, you're correct. When creating an output for a transaction, by default you only need to provide the datum hash. The consumer of that output is by default required to provide the datum that matches that hash.
If you want to provide the datum nonetheless for an output that you're creating in order to do assertions on it inside your spending validator you can do so like this:
const scripts = PlutusScripts.new();
scripts.add(PlutusScript.from_bytes(Buffer.from(, 'hex')));

const datumFields = CSL.PlutusList.new();
datumFields.add(CSL.PlutusData.new_integer(CSL.BigInt.from_str( ... )));
const datumConstr = CSL.ConstrPlutusData.new(
    CSL.BigNum.from_str('0'),
    datumFields
);
const datum = CSL.PlutusData.new_constr_plutus_data(datumConstr);

const transactionWitnessSet = CSL.TransactionWitnessSet.new();
transactionWitnessSet.set_plutus_scripts(scripts);
transactionWitnessSet.set_plutus_data(datum);
transactionWitnessSet.set_redeemers(...);

txBody.set_script_data_hash(hash_script_data(..., getCostModels(), datum));

{...}

export const COST_MODEL = [197209, 0, 1, 1, 396231, 621, 0, 1, 150000, 1000, 0, 1, 150000, 32, 2477736, 29175, 4, 29773, 100, 29773, 100, 29773, 100, 29773, 100, 29773, 100, 29773, 100, 100, 100, 29773, 100, 150000, 32, 150000, 32, 150000, 32, 150000, 1000, 0, 1, 150000, 32, 150000, 1000, 0, 8, 148000, 425507, 118, 0, 1, 1, 150000, 1000, 0, 8, 150000, 112536, 247, 1, 150000, 10000, 1, 136542, 1326, 1, 1000, 150000, 1000, 1, 150000, 32, 150000, 32, 150000, 32, 1, 1, 150000, 1, 150000, 4, 103599, 248, 1, 103599, 248, 1, 145276, 1366, 1, 179690, 497, 1, 150000, 32, 150000, 32, 150000, 32, 150000, 32, 150000, 32, 150000, 32, 148000, 425507, 118, 0, 1, 1, 61516, 11218, 0, 1, 150000, 32, 148000, 425507, 118, 0, 1, 1, 148000, 425507, 118, 0, 1, 1, 2477736, 29175, 4, 0, 82363, 4, 150000, 5000, 0, 1, 150000, 32, 197209, 0, 1, 1, 150000, 32, 150000, 32, 150000, 32, 150000, 32, 150000, 32, 150000, 32, 150000, 32, 3345831, 1, 1];
export const getCostModel = () => {
  const costModel = CostModel.new();
  COST_MODEL.forEach((x, i) => costModel.set(i, Int.new_i32(x)));
  const costModels = Costmdls.new();
  costModels.insert(Language.new_plutus_v1(), costModel);
  return costModels;
}


For more examples I recommend the following resources:
DApp Connector Examples
SpaceBudz Datum Creation

> **Comments:**
> **username_0**: I am running into similar issue reported by bakon (from the reply to my question). It appears the CSL is not accounting for the datum in the output, so the fee is being miscalculated.  

---

