There are multiple ways to do that.
Option 1:
If you have "script-wide" constants, like your mentioned example of an auction house fee, I suggest parameterizing your contract like it is done in Lecture #3 instead of storing that value in each output's datum.
Option 2:
If you really want to ensure some or all of the datum fields are recreated when it is consumed you can do that too. By default for any spending transaction from a script, Plutus expects you to provide the datum value/file whose hash matches the one stored on-chain for the UTxO you're trying to spend in your transaction.
Your validator can check for an output recreated at its own address with a certain value and datum. If the datum shall be the exact same as previously you can simply check if the hash of the newly created output equals the one you're consuming.
Otherwise, you can also provide the datum (not the hash) of the newly created output in the transaction and validate it within the script. You can do that via the CLI by using:
--tx-out ADDRESS VALUE
[--tx-out-datum-embed-file FILE | --tx-out-datum-embed-value JSON VALUE]
or using CSL:
[...]
TransactionOutputBuilder.new()
.with_address(yourScriptAddress)
.with_data_hash(hashOfYourNewDatum) // does not include the datum value
.next()
.with_value(Value.new(someOutputValueOrMinADA))
.build()
// Adding transaction datum values
// Must at least include a datum value for every script output that is being
// spent + optionally any additional datums necessary for the script validation
const datums = PlutusList.new();
datums.add(datumBuilder(1));
datums.add(datumBuilder(2));
const transactionWitnessSet = TransactionWitnessSet.new();
transactionWitnessSet.set_plutus_data(datums);
[...]
export const datumBuilder(n: number): PlutusData {
const fields = PlutusList.new();
fields.add(PlutusData.new_integer(BigInt.from_str(`${n}`)));
return PlutusData.new_constr_plutus_data(
ConstrPlutusData.new(
BigNum.from_str('0'),
fields
)
);
}
Once, you've added the datum value of the newly created script output to the transaction you can retrieve it in your Plutus code like similarly to any other datum:
[...]
where
-- returns new outputs of this script
ownOutputs :: [TxOut]
ownOutputs = getContinuingOutputs ctx
-- returns Datum value for a tx output if it is provided
outputDatum :: TxOut -> Maybe Datum
outputDatum o = txOutDatumHash o >>= flip findDatum info
Related answers to that topic:
ScriptContext
. An output has a datum hash field available. You can then assert that the hash is equal to the hash of the input datum. Ref: playground.plutus.iohkdev.io/doc/haddock/plutus-ledger-api/html/…