Skip to content

Commit

Permalink
Merge pull request #395 from geniusyield/391-optional-datum
Browse files Browse the repository at this point in the history
feat(#391 & #306): make datum as optional when spending an input in c…
  • Loading branch information
sourabhxyz authored Jan 15, 2025
2 parents d0f64e8 + 5c43b3a commit b642648
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.9.0

* When spending an input, datum is now optional since it is not required to be specified in case it's inlined or not needed by associated Plutus script.

## 0.8.1

* Add combined stake address registration and delegation certificate.
Expand Down
2 changes: 1 addition & 1 deletion atlas-cardano.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 3.8
name: atlas-cardano
version: 0.8.1
version: 0.9.0
synopsis: Application backend for Plutus smart contracts on Cardano
description:
Atlas is an all-in-one, Haskell-native application backend for writing off-chain code for on-chain Plutus smart contracts.
Expand Down
4 changes: 2 additions & 2 deletions src/GeniusYield/Test/Privnet/Examples/Gift.hs
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ grabGifts validator = do
, gyTxInWitness =
GYTxInWitnessScript
(GYBuildPlutusScriptInlined validator)
(datumFromPlutus' od)
(Just $ datumFromPlutus' od)
unitRedeemer
}
| (oref, (_addr, _value, od)) <- itoList datums
Expand Down Expand Up @@ -639,7 +639,7 @@ grabGiftsRef ref validator = do
, gyTxInWitness =
GYTxInWitnessScript
(GYBuildPlutusScriptReference ref $ validatorToScript validator)
(datumFromPlutus' od)
(Just $ datumFromPlutus' od)
unitRedeemer
}
| (oref, (_addr, _value, od)) <- itoList datums
Expand Down
4 changes: 2 additions & 2 deletions src/GeniusYield/Test/Privnet/Examples/Oracle.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ tests setup =
, gyTxInWitness =
GYTxInWitnessScript
(GYBuildPlutusScriptInlined @PlutusV2 readOracleValidatorV2)
(datumFromPlutusData (d :: ()))
(Just $ datumFromPlutusData (d :: ()))
unitRedeemer
}
| (ref, (_, _, d)) <- Map.toList datums
Expand Down Expand Up @@ -108,7 +108,7 @@ tests setup =
, gyTxInWitness =
GYTxInWitnessScript
(GYBuildPlutusScriptInlined @PlutusV2 readOracleValidatorV2)
(datumFromPlutusData (d :: ()))
(Just $ datumFromPlutusData (d :: ()))
unitRedeemer
}
| (ref, (_, _, d)) <- Map.toList datums
Expand Down
2 changes: 1 addition & 1 deletion src/GeniusYield/Test/Privnet/Examples/Treat.hs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ grabTreats validator = do
, gyTxInWitness =
GYTxInWitnessScript
(GYBuildPlutusScriptInlined validator)
(datumFromPlutus' od)
(Just $ datumFromPlutus' od)
unitRedeemer
}
| (oref, (_addr, _value, od)) <- itoList datums
Expand Down
12 changes: 8 additions & 4 deletions src/GeniusYield/TxBuilder/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import Data.List (nubBy)
import Data.List.NonEmpty (NonEmpty ((:|)))
import Data.List.NonEmpty qualified as NE
import Data.Map.Strict qualified as Map
import Data.Maybe (maybeToList)
import Data.Maybe (isNothing, maybeToList)
import Data.Ratio ((%))
import Data.Set qualified as Set
import GeniusYield.Imports
Expand Down Expand Up @@ -300,9 +300,13 @@ buildTxCore ss eh pp ps cstrat ownUtxoUpdateF addrs change reservedCollateral sk
checkDatumMatch _ GYTxInWitnessKey = True
checkDatumMatch _ GYTxInWitnessSimpleScript {} = True
checkDatumMatch ud (GYTxInWitnessScript _ wd _) = case ud of
GYOutDatumNone -> False
GYOutDatumHash h -> h == hashDatum wd
GYOutDatumInline uid -> uid == wd
GYOutDatumNone -> isNothing wd
GYOutDatumHash h -> case wd of
Nothing -> False
Just wd' -> h == hashDatum wd'
GYOutDatumInline uid -> case wd of
Nothing -> True
Just wd' -> uid == wd'

-- This operation is `O(n)` where `n` denotes the number of UTxOs in `ownUtxos'`.
let totalRefScriptSize = foldl' (\acc GYUTxO {..} -> acc + maybe 0 scriptSize utxoRefScript) 0 $ refInsUtxos <> map utxoFromTxInDetailed gyTxInsDetailed
Expand Down
8 changes: 4 additions & 4 deletions src/GeniusYield/Types/TxIn.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ data GYTxIn v = GYTxIn
data GYTxInWitness v
= -- | Key witness without datum.
GYTxInWitnessKey
| -- | Script witness with associated script, datum, and redeemer.
GYTxInWitnessScript !(GYBuildPlutusScript v) !GYDatum !GYRedeemer
| -- | Script witness with associated script, datum, and redeemer. Datum can be omitted if it is inlined in the input or if it's not needed under PlutusV3 (or beyond) script.
GYTxInWitnessScript !(GYBuildPlutusScript v) !(Maybe GYDatum) !GYRedeemer
| -- | Simple script witness.
GYTxInWitnessSimpleScript !(GYBuildSimpleScript v)
deriving stock (Eq, Show)
Expand Down Expand Up @@ -88,13 +88,13 @@ txInToApi useInline (GYTxIn oref m) = (txOutRefToApi oref, Api.BuildTxWith $ f m
where
f :: GYTxInWitness v -> Api.Witness Api.WitCtxTxIn ApiEra
f GYTxInWitnessKey = Api.KeyWitness Api.KeyWitnessForSpending
f (GYTxInWitnessScript v d r) =
f (GYTxInWitnessScript v md r) =
Api.ScriptWitness Api.ScriptWitnessForSpending $
( case v of
GYBuildPlutusScriptInlined s -> validatorToApiPlutusScriptWitness s
GYBuildPlutusScriptReference ref s -> referenceScriptToApiPlutusScriptWitness ref s
)
(if useInline then Api.InlineScriptDatum else Api.ScriptDatumForTxIn $ Just $ datumToApi' d)
(if useInline then Api.InlineScriptDatum else Api.ScriptDatumForTxIn $ datumToApi' <$> md)
(redeemerToApi r)
(Api.ExecutionUnits 0 0)
f (GYTxInWitnessSimpleScript v) =
Expand Down
4 changes: 2 additions & 2 deletions tests-privnet/GeniusYield/Test/Privnet/Blueprint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ blueprintTests setup =
(\e -> if isTxBodyErrorAutoBalance e then pure () else throwError e)
$ asUser user
$ do
void $ buildTxBody $ mustHaveInput @'PlutusV3 $ GYTxIn oref (GYTxInWitnessScript (GYBuildPlutusScriptInlined val) dat unsatRedeemer)
void $ buildTxBody $ mustHaveInput @'PlutusV3 $ GYTxIn oref (GYTxInWitnessScript (GYBuildPlutusScriptInlined val) (Just dat) unsatRedeemer)
lg "Successfully failed to consume from blueprint script for unsatisfying redeemer"
tid <- asUser user $ do
txBody <- buildTxBody $ mustHaveInput @'PlutusV3 $ GYTxIn oref (GYTxInWitnessScript (GYBuildPlutusScriptInlined val) dat satRedeemer)
txBody <- buildTxBody $ mustHaveInput @'PlutusV3 $ GYTxIn oref (GYTxInWitnessScript (GYBuildPlutusScriptInlined val) (Just dat) satRedeemer)
signAndSubmitConfirmed txBody
lg $ "Successfully consumed from blueprint script, with tx id: " <> show tid
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,6 @@ input brp refScript inputRef dat red =
, gyTxInWitness =
GYTxInWitnessScript
(GYBuildPlutusScriptReference refScript $ validatorToScript $ mkBetRefValidator brp)
(datumFromPlutusData dat)
(Just $ datumFromPlutusData dat)
(redeemerFromPlutusData red)
}
10 changes: 5 additions & 5 deletions tests/GeniusYield/Test/FeeTracking.hs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ sendAndConsume Wallets {w1, w2} amt = withWalletBalancesCheckSimple [w1 := value
txBody <-
buildTxBody @PlutusV1 . mustHaveInput $
GYTxIn
{ gyTxInWitness = GYTxInWitnessScript (GYBuildPlutusScriptInlined gyAlwaysSucceedsValidator) unitDatum unitRedeemer
{ gyTxInWitness = GYTxInWitnessScript (GYBuildPlutusScriptInlined gyAlwaysSucceedsValidator) (Just unitDatum) unitRedeemer
, gyTxInTxOutRef = txOutRefFromTuple (txId, 0)
}
signAndSubmitConfirmed_ txBody
Expand All @@ -81,7 +81,7 @@ sendAndContinue Wallets {w1, w2} amt = withWalletBalancesCheckSimple [w1 := valu
mconcat
[ mustHaveInput $
GYTxIn
{ gyTxInWitness = GYTxInWitnessScript (GYBuildPlutusScriptInlined gyAlwaysSucceedsValidator) unitDatum unitRedeemer
{ gyTxInWitness = GYTxInWitnessScript (GYBuildPlutusScriptInlined gyAlwaysSucceedsValidator) (Just unitDatum) unitRedeemer
, gyTxInTxOutRef = txOutRefFromTuple (txId, 0)
}
, mustHaveOutput $ mkGYTxOut target amt unitDatum
Expand All @@ -97,7 +97,7 @@ selfConsume Wallets {w1} amt = withWalletBalancesCheckSimple [w1 := mempty] $ do
consumeBody <-
buildTxBody @PlutusV1 . mustHaveInput $
GYTxIn
{ gyTxInWitness = GYTxInWitnessScript (GYBuildPlutusScriptInlined gyAlwaysSucceedsValidator) unitDatum unitRedeemer
{ gyTxInWitness = GYTxInWitnessScript (GYBuildPlutusScriptInlined gyAlwaysSucceedsValidator) (Just unitDatum) unitRedeemer
, gyTxInTxOutRef = txOutRefFromTuple (txId, 0)
}
signAndSubmitConfirmed_ consumeBody
Expand All @@ -113,7 +113,7 @@ selfContinue Wallets {w1} amt = withWalletBalancesCheckSimple [w1 := valueNegate
mconcat
[ mustHaveInput $
GYTxIn
{ gyTxInWitness = GYTxInWitnessScript (GYBuildPlutusScriptInlined gyAlwaysSucceedsValidator) unitDatum unitRedeemer
{ gyTxInWitness = GYTxInWitnessScript (GYBuildPlutusScriptInlined gyAlwaysSucceedsValidator) (Just unitDatum) unitRedeemer
, gyTxInTxOutRef = txOutRefFromTuple (txId, 0)
}
, mustHaveOutput $ mkGYTxOut target amt unitDatum
Expand All @@ -134,7 +134,7 @@ selfPartialConsume lovelaceConf TestInfo {testWallets = Wallets {w1}, testGoldAs
mconcat
[ mustHaveInput $
GYTxIn
{ gyTxInWitness = GYTxInWitnessScript (GYBuildPlutusScriptInlined gyAlwaysSucceedsValidator) unitDatum unitRedeemer
{ gyTxInWitness = GYTxInWitnessScript (GYBuildPlutusScriptInlined gyAlwaysSucceedsValidator) (Just unitDatum) unitRedeemer
, gyTxInTxOutRef = txOutRefFromTuple (txId, 0)
}
, mustHaveOutput $ mkGYTxOut target partialAmt unitDatum
Expand Down
2 changes: 1 addition & 1 deletion tests/GeniusYield/Test/RefInput.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ guessRefInputRun refInputORef consumeRef guess = do
, gyTxInWitness =
GYTxInWitnessScript
(GYBuildPlutusScriptInlined gyGuessRefInputDatumValidator)
(datumFromPlutusData ())
(Just $ datumFromPlutusData ())
(redeemerFromPlutusData redeemer)
}
<> mustHaveRefInput refInputORef
Expand Down

0 comments on commit b642648

Please sign in to comment.