Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indigo/update plutus apps #117

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
2e5409a
bump deps
KristianBalaj Jun 10, 2022
1a754f2
Merge branch 'main' into indigo/update-plutus-apps
KristianBalaj Jun 16, 2022
c957040
Merge branch 'main' into indigo/update-plutus-apps
KristianBalaj Jul 28, 2022
3a446c4
chore: fit with indigo project
KristianBalaj Jul 28, 2022
c1922a1
Merge branch 'main' into indigo/update-plutus-apps
KristianBalaj Aug 3, 2022
4dcb7cf
Merge branch 'main' into indigo/update-plutus-apps
KristianBalaj Aug 23, 2022
7c3aebf
Merge branch 'main' into indigo/update-plutus-apps
KristianBalaj Oct 25, 2022
b198fb5
feat: add untyped validator and spendScriptRefUntyped
KristianBalaj Oct 25, 2022
bc736ca
feat: add spendScriptUntyped
KristianBalaj Oct 25, 2022
fbc9fe4
style: lint fix
KristianBalaj Oct 25, 2022
e6696f0
feat: add mkUntypedValidator
KristianBalaj Oct 25, 2022
39fed20
feat: add payToScriptUntyped
KristianBalaj Oct 25, 2022
2962ade
feat: lower the constraints for box related functions
KristianBalaj Oct 26, 2022
4789aec
Fix check balance with ref. scripts
borja-bonet-segui Nov 12, 2022
31a18e5
Merge pull request #58 from mlabs-haskell/borja/fix-check-balance-ref…
borja-bonet-segui Nov 12, 2022
23ac781
feat: untyped validator hash
KristianBalaj Dec 22, 2022
e7d8cb9
feat: update constraints in payToScriptUntyped
KristianBalaj Dec 22, 2022
900f62c
feat: typed validator construction from validator
KristianBalaj Jul 19, 2023
fc7e4e9
feat: add typed mp construction from mp
KristianBalaj Aug 13, 2023
4ca4cd3
feat: `payToKeyDatum` now uses `HasAddress`
KristianBalaj Jan 28, 2024
90c4001
feat: spendScript untyped changes
KristianBalaj Feb 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@
haskell-nix-extra-hackage.inputs.haskell-nix.follows = "haskell-nix";
haskell-nix-extra-hackage.inputs.nixpkgs.follows = "nixpkgs";

cardano-base.url = "github:input-output-hk/cardano-base/631cb6cf1fa01ab346233b610a38b3b4cba6e6ab";
cardano-base.url = "github:input-output-hk/cardano-base/0f3a867493059e650cda69e20a5cbf1ace289a57";
cardano-base.flake = false;
cardano-crypto.url = "github:input-output-hk/cardano-crypto/f73079303f663e028288f9f4a9e08bcca39a923e";
cardano-crypto.flake = false;
cardano-ledger.url = "github:input-output-hk/cardano-ledger/389b266d6226dedf3d2aec7af640b3ca4984c5ea";
cardano-ledger.url = "github:input-output-hk/cardano-ledger/3be8a19083fc13d9261b1640e27dd389b51bb08e";
cardano-ledger.flake = false;
cardano-prelude.url = "github:input-output-hk/cardano-prelude/533aec85c1ca05c7d171da44b89341fb736ecfe5";
cardano-prelude.url = "github:input-output-hk/cardano-prelude/bb4ed71ba8e587f672d06edf9d2e376f4b055555";
cardano-prelude.flake = false;
flat.url = "github:Quid2/flat/ee59880f47ab835dbd73bea0847dab7869fc20d8";
flat.flake = false;
goblins.url = "github:input-output-hk/goblins/cde90a2b27f79187ca8310b6549331e59595e7ba";
goblins.flake = false;
weigh.url = "github:fpco/weigh/bfcf4415144d7d2817dfcb91b6f9a6dfd7236de7";
weigh.flake = false;
plutus.url = "github:input-output-hk/plutus/8ab4c3355c5fdf67dcf6acc1f5a14668d5e6f0a9";
plutus.url = "github:input-output-hk/plutus/69ab98c384703172f898eb5bcad1078ded521426";
plutus.flake = false;
Win32-network.url = "github:input-output-hk/Win32-network/3825d3abf75f83f406c1f7161883c438dac7277d";
Win32-network.flake = false;
Expand Down
67 changes: 60 additions & 7 deletions src/Plutus/Model/Contract.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,17 @@ module Plutus.Model.Contract (
payToKey,
payToKeyDatum,
payToScript,
payToScriptUntyped,
loadRefScript,
loadRefScriptDatum,
payToRef,
payFee,
userSpend,
spendPubKey,
spendScript,
spendScriptUntyped,
spendScriptRef,
spendScriptRefUntyped,
spendBox,
refInputInline,
refInputHash,
Expand Down Expand Up @@ -336,10 +339,10 @@ fromDatumMode = \case
-- build Tx

-- | Pay to public key with datum
payToKeyDatum :: ToData a => PubKeyHash -> DatumMode a -> Value -> Tx
payToKeyDatum :: (ToData a, HasAddress pubKeyHash) => pubKeyHash -> DatumMode a -> Value -> Tx
payToKeyDatum pkh dat val = toExtra $
mempty
{ P.txOutputs = [TxOut (pubKeyHashAddress pkh) val outDatum Nothing]
{ P.txOutputs = [TxOut (toAddress pkh) val outDatum Nothing]
, P.txData = datumMap
}
where
Expand All @@ -365,6 +368,21 @@ payToScript script dat val = toExtra $
where
(outDatum, datumMap) = fromDatumMode dat

-- | Pay to the script untyped.
-- The a type parameter represents the contents of the datum.
-- Example for datum: `()` and not `Datum $ toBuiltinDatum ()`.
payToScriptUntyped :: (ToData a, HasAddress script) =>
script -> DatumMode a -> Value -> Tx
payToScriptUntyped script dat val = toExtra $
mempty
{ P.txOutputs = [TxOut (toAddress script) val outDatum Nothing]
, P.txData = datumMap
}
where
(outDatum, datumMap) = fromDatumMode dat



-- | Uploads the reference script to blockchain
loadRefScript :: (IsValidator script) => script -> Value -> Tx
loadRefScript script val = loadRefScriptBy script Nothing val
Expand Down Expand Up @@ -426,6 +444,21 @@ spendScript tv ref red dat = toExtra $
{ P.txInputs = S.singleton $ Fork.TxIn ref (Just $ Fork.ConsumeScriptAddress (Just $ Versioned (getLanguage tv) (toValidator tv)) (toRedeemer red) (toDatum dat))
}

-- | Spend script input untyped.
spendScriptUntyped ::
(HasValidator script, HasLanguage script) =>
script ->
TxOutRef ->
Redeemer ->
Datum ->
Tx
spendScriptUntyped script ref red dat = toExtra $
mempty
{ P.txInputs = S.singleton $ Fork.TxIn ref (Just $ Fork.ConsumeScriptAddress (Just validator) red dat)
}
where
validator = Versioned (getLanguage script) ( toValidator script)

-- | Spends script that references other script
spendScriptRef ::
(IsValidator script) =>
Expand All @@ -445,6 +478,25 @@ spendScriptRef refScript script refOut red dat = toExtra $
sh = scriptHash script
validator = toVersionedScript script

-- | Spends script that references other script untyped version
spendScriptRefUntyped ::
(HasValidator script, HasLanguage script) =>
TxOutRef ->
script ->
TxOutRef ->
Redeemer ->
Datum ->
Tx
spendScriptRefUntyped refScript script refOut red dat = toExtra $
mempty
{ P.txInputs = S.singleton $ Fork.TxIn refOut (Just $ Fork.ConsumeScriptAddress Nothing red dat)
, P.txReferenceInputs = S.singleton $ Fork.TxIn refScript Nothing
, P.txScripts = M.singleton sh validator
}
where
sh = scriptHash script
validator = Versioned (getLanguage script) (getValidator $ toValidator script)

-- | Reference input with inlined datum
refInputInline :: TxOutRef -> Tx
refInputInline ref = toExtra $
Expand Down Expand Up @@ -564,25 +616,25 @@ txBoxValue :: TxBox a -> Value
txBoxValue = txOutValue . txBoxOut

-- | Read UTXOs with datums.
boxAt :: (IsValidator script) => script -> Run [TxBox script]
boxAt :: (HasAddress script, HasDatum script) => script -> Run [TxBox script]
boxAt addr = do
utxos <- utxoAt (toAddress addr)
fmap catMaybes $ mapM (\(ref, tout) -> fmap (\dat -> TxBox ref tout dat) <$> datumAt ref) utxos

-- | It expects that Typed validator can have only one UTXO
-- which is NFT.
nftAt :: IsValidator script => script -> Run (TxBox script)
nftAt :: (HasAddress script, HasDatum script) => script -> Run (TxBox script)
nftAt tv = head <$> boxAt tv

-- | Safe query for single Box
withBox :: IsValidator script => (TxBox script -> Bool) -> script -> (TxBox script -> Run ()) -> Run ()
withBox :: (HasAddress script, HasDatum script) => (TxBox script -> Bool) -> script -> (TxBox script -> Run ()) -> Run ()
withBox isBox script cont =
withMayBy readMsg (L.find isBox <$> boxAt script) cont
where
readMsg = ("No UTxO box for: " <> ) <$> getPrettyAddress (toAddress script)

-- | Reads single box from the list. we expect NFT to be a single UTXO for a given script.
withNft :: IsValidator script => script -> (TxBox script -> Run ()) -> Run ()
withNft :: (HasAddress script, HasDatum script) => script -> (TxBox script -> Run ()) -> Run ()
withNft = withBox (const True)

----------------------------------------------------------------------
Expand Down Expand Up @@ -718,10 +770,11 @@ checkBalanceBy :: (a -> BalanceDiff) -> Run a -> Run a
checkBalanceBy getDiffs act = do
beforeSt <- get
res <- act
afterSt <- get
let BalanceDiff diffs = getDiffs res
addrs = M.keys diffs
before = fmap (`valueAtState` beforeSt) addrs
after <- mapM valueAt addrs
after = fmap (`valueAtState` afterSt) addrs
mapM_ (logError . show . vcat <=< mapM ppError) (check addrs diffs before after)
pure res
where
Expand Down
4 changes: 2 additions & 2 deletions src/Plutus/Model/Fork/Cardano/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import Cardano.Ledger.Shelley.API.Types qualified as Shelley (Hash)
import Cardano.Ledger.TxIn qualified as C
import Cardano.Ledger.ShelleyMA.Timelocks qualified as C
import Cardano.Ledger.Keys qualified as C
import Cardano.Ledger.Keys.WitVKey
import Cardano.Ledger.Shelley.TxBody (Delegation (Delegation), WitVKey)
import Cardano.Ledger.Shelley.UTxO qualified as C
import qualified Cardano.Crypto.Hash.Class as Crypto
import Cardano.Ledger.Mary.Value qualified as C
Expand Down Expand Up @@ -170,7 +170,7 @@ toDCert :: Network -> C.Coin -> C.Coin -> P.DCert -> Either ToCardanoError (C.DC
toDCert network poolDeposit minPoolCost = \case
P.DCertDelegRegKey (P.StakingHash stakingCredential) -> C.DCertDeleg . C.RegKey <$> toCredential stakingCredential
P.DCertDelegDeRegKey (P.StakingHash stakingCredential) -> C.DCertDeleg . C.DeRegKey <$> toCredential stakingCredential
P.DCertDelegDelegate (P.StakingHash stakingCredential) pubKeyHash -> C.DCertDeleg . C.Delegate <$> (C.Delegation <$> toCredential stakingCredential <*> toPubKeyHash pubKeyHash)
P.DCertDelegDelegate (P.StakingHash stakingCredential) pubKeyHash -> C.DCertDeleg . C.Delegate <$> (Delegation <$> toCredential stakingCredential <*> toPubKeyHash pubKeyHash)
P.DCertPoolRegister poolKeyHash poolVfr -> C.DCertPool . C.RegPool <$> toPoolParams poolKeyHash poolVfr
P.DCertPoolRetire pkh n -> C.DCertPool . (\key -> C.RetirePool key (C.EpochNo (fromIntegral n)) ) <$> toPubKeyHash pkh
P.DCertGenesis -> Left "DCertGenesis is not supported"
Expand Down
28 changes: 28 additions & 0 deletions src/Plutus/Model/Validator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ module Plutus.Model.Validator(
IsValidatorHash,

TypedValidator(..),
UntypedValidator(..),
UntypedValidatorHash(..),
TypedValidatorHash(..),
TypedPolicy(..),
TypedStake(..),
Expand Down Expand Up @@ -99,6 +101,32 @@ instance HasValidator (TypedValidator datum redeemer) where
instance HasAddress (TypedValidator datum redeemer) where
toAddress = toAddress . toValidatorHash

---------------------------------------------------------------------
-- untyped validator

newtype UntypedValidator =
UntypedValidator { unUntypedValidator :: Versioned Validator }
deriving newtype (HasLanguage)

instance HasValidator UntypedValidator where
toValidator (UntypedValidator (Versioned _lang validator)) = validator

instance HasAddress UntypedValidator where
toAddress = toAddress . toValidatorHash

---------------------------------------------------------------------
-- untyped validator hash

newtype UntypedValidatorHash =
UntypedValidatorHash { unTypedValidatorHash :: Versioned ValidatorHash }
deriving newtype (HasLanguage)

instance HasValidatorHash UntypedValidatorHash where
toValidatorHash (UntypedValidatorHash (Versioned _lang vh)) = vh

instance HasAddress UntypedValidatorHash where
toAddress (UntypedValidatorHash (Versioned _lang vh)) = toAddress vh

---------------------------------------------------------------------
-- typed validator hash

Expand Down
15 changes: 14 additions & 1 deletion src/Plutus/Model/Validator/V2.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
-- | Creation of typed validators for Plutus V2
module Plutus.Model.Validator.V2(
mkTypedValidator,
mkTypedValidator',
mkUntypedValidator,
mkTypedPolicy,
mkTypedPolicy',
mkTypedStake,
toBuiltinValidator,
toBuiltinPolicy,
Expand All @@ -13,13 +16,23 @@ import PlutusTx.Prelude qualified as Plutus
import Plutus.V2.Ledger.Api
import PlutusTx.Code (CompiledCode)

import Plutus.Model.Validator (TypedValidator(..), TypedPolicy(..), TypedStake(..))
import Plutus.Model.Validator (TypedValidator(..), TypedPolicy(..), TypedStake(..), UntypedValidator (UntypedValidator))
import Plutus.Model.Fork.Ledger.Scripts (toV2)

mkTypedValidator' :: Validator -> TypedValidator datum redeemer
mkTypedValidator' = TypedValidator . toV2

-- | Create Plutus V2 typed validator
mkTypedValidator :: CompiledCode (BuiltinData -> BuiltinData -> BuiltinData -> ()) -> TypedValidator datum redeemer
mkTypedValidator = TypedValidator . toV2 . mkValidatorScript

-- | Create Plutus V2 untyped validator
mkUntypedValidator :: CompiledCode (BuiltinData -> BuiltinData -> BuiltinData -> ()) -> UntypedValidator
mkUntypedValidator = UntypedValidator . toV2 . mkValidatorScript

mkTypedPolicy' :: MintingPolicy -> TypedPolicy redeemer
mkTypedPolicy' = TypedPolicy . toV2

-- | Create Plutus V2 typed minting policy
mkTypedPolicy :: CompiledCode (BuiltinData -> BuiltinData -> ()) -> TypedPolicy redeemer
mkTypedPolicy = TypedPolicy . toV2 . mkMintingPolicyScript
Expand Down