{-# LANGUAGE DataKinds           #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell     #-}
{-# LANGUAGE TypeApplications    #-}
{-# LANGUAGE TypeFamilies        #-}
module Ledger.Test where

import Cardano.Api qualified as C
import Ledger qualified
import Ledger.Typed.Scripts qualified as Scripts
import Ledger.Value.CardanoAPI (policyId)
import Plutus.Script.Utils.Typed as PSU
import Plutus.Script.Utils.V1.Address qualified as PV1
import Plutus.Script.Utils.V1.Scripts qualified as PV1
import Plutus.Script.Utils.V2.Address qualified as PV2
import Plutus.Script.Utils.V2.Scripts qualified as PV2
import Plutus.V1.Ledger.Api (Address, Validator)
import Plutus.V1.Ledger.Api qualified as PV1
import Plutus.V1.Ledger.Value qualified as Value
import Plutus.V2.Ledger.Api qualified as PV2
import PlutusTx qualified
import Prelude hiding (not)

someCode :: PlutusTx.CompiledCode (PlutusTx.BuiltinData -> PlutusTx.BuiltinData -> PlutusTx.BuiltinData -> ())
someCode :: CompiledCode (BuiltinData -> BuiltinData -> BuiltinData -> ())
someCode = $$(PlutusTx.compile [|| \_ _ _ -> () ||])

someValidator :: Validator
someValidator :: Validator
someValidator = CompiledCode (BuiltinData -> BuiltinData -> BuiltinData -> ())
-> Validator
PV1.mkValidatorScript CompiledCode (BuiltinData -> BuiltinData -> BuiltinData -> ())
someCode

someTypedValidator :: Scripts.TypedValidator Any
someTypedValidator :: TypedValidator Any
someTypedValidator = Versioned Validator -> TypedValidator Any
Scripts.unsafeMkTypedValidator (Validator -> Language -> Versioned Validator
forall script. script -> Language -> Versioned script
Versioned Validator
someValidator Language
PlutusV1)

someValidatorHash :: PV1.ValidatorHash
someValidatorHash :: ValidatorHash
someValidatorHash = Validator -> ValidatorHash
PV1.validatorHash Validator
someValidator

someCardanoAddress :: C.NetworkId -> Ledger.CardanoAddress
someCardanoAddress :: NetworkId -> CardanoAddress
someCardanoAddress = (NetworkId -> Validator -> CardanoAddress)
-> Validator -> NetworkId -> CardanoAddress
forall a b c. (a -> b -> c) -> b -> a -> c
flip NetworkId -> Validator -> CardanoAddress
PV1.mkValidatorCardanoAddress Validator
someValidator

someAddress :: Address
someAddress :: Address
someAddress = ValidatorHash -> Maybe StakingCredential -> Address
Ledger.scriptValidatorHashAddress ValidatorHash
someValidatorHash Maybe StakingCredential
forall a. Maybe a
Nothing

someValidatorV2 :: Validator
someValidatorV2 :: Validator
someValidatorV2 = CompiledCode (BuiltinData -> BuiltinData -> BuiltinData -> ())
-> Validator
PV2.mkValidatorScript CompiledCode (BuiltinData -> BuiltinData -> BuiltinData -> ())
someCode

someTypedValidatorV2 :: Scripts.TypedValidator Any
someTypedValidatorV2 :: TypedValidator Any
someTypedValidatorV2 = Versioned Validator -> TypedValidator Any
Scripts.unsafeMkTypedValidator (Validator -> Language -> Versioned Validator
forall script. script -> Language -> Versioned script
Versioned Validator
someValidator Language
PlutusV2)

someValidatorHashV2 :: PV2.ValidatorHash
someValidatorHashV2 :: ValidatorHash
someValidatorHashV2 = Validator -> ValidatorHash
PV2.validatorHash Validator
someValidatorV2

someCardanoAddressV2 :: C.NetworkId -> Ledger.CardanoAddress
someCardanoAddressV2 :: NetworkId -> CardanoAddress
someCardanoAddressV2 = (NetworkId -> Validator -> CardanoAddress)
-> Validator -> NetworkId -> CardanoAddress
forall a b c. (a -> b -> c) -> b -> a -> c
flip NetworkId -> Validator -> CardanoAddress
PV2.mkValidatorCardanoAddress Validator
someValidatorV2

someAddressV2 :: Address
someAddressV2 :: Address
someAddressV2 = ValidatorHash -> Maybe StakingCredential -> Address
Ledger.scriptValidatorHashAddress ValidatorHash
someValidatorHashV2 Maybe StakingCredential
forall a. Maybe a
Nothing

{-# INLINABLE mkPolicy #-}
mkPolicy :: () -> Ledger.ScriptContext -> Bool
mkPolicy :: () -> ScriptContext -> Bool
mkPolicy ()
_ ScriptContext
_ = Bool
True

{-# INLINABLE mkPolicyV2 #-}
mkPolicyV2 :: () -> PV2.ScriptContext -> Bool
mkPolicyV2 :: () -> ScriptContext -> Bool
mkPolicyV2 ()
_ ScriptContext
_ = Bool
True

coinMintingPolicy :: Language -> Versioned Ledger.MintingPolicy
coinMintingPolicy :: Language -> Versioned MintingPolicy
coinMintingPolicy Language
lang = case Language
lang of
  Language
PlutusV1 -> MintingPolicy -> Language -> Versioned MintingPolicy
forall script. script -> Language -> Versioned script
Versioned MintingPolicy
coinMintingPolicyV1 Language
lang
  Language
PlutusV2 -> MintingPolicy -> Language -> Versioned MintingPolicy
forall script. script -> Language -> Versioned script
Versioned MintingPolicy
coinMintingPolicyV2 Language
lang

coinMintingPolicyV1 :: Ledger.MintingPolicy
coinMintingPolicyV1 :: MintingPolicy
coinMintingPolicyV1 = CompiledCode (BuiltinData -> BuiltinData -> ()) -> MintingPolicy
Ledger.mkMintingPolicyScript
    $$(PlutusTx.compile [|| PSU.mkUntypedMintingPolicy mkPolicy ||])

coinMintingPolicyV2 :: Ledger.MintingPolicy
coinMintingPolicyV2 :: MintingPolicy
coinMintingPolicyV2 = CompiledCode (BuiltinData -> BuiltinData -> ()) -> MintingPolicy
Ledger.mkMintingPolicyScript
    $$(PlutusTx.compile [|| PSU.mkUntypedMintingPolicy mkPolicyV2 ||])

coinMintingPolicyHash :: Language -> Ledger.MintingPolicyHash
coinMintingPolicyHash :: Language -> MintingPolicyHash
coinMintingPolicyHash Language
lang = case Language
lang of
  Language
PlutusV1 -> MintingPolicyHash
coinMintingPolicyHashV1
  Language
PlutusV2 -> MintingPolicyHash
coinMintingPolicyHashV2

coinMintingPolicyHashV1 :: Ledger.MintingPolicyHash
coinMintingPolicyHashV1 :: MintingPolicyHash
coinMintingPolicyHashV1 = MintingPolicy -> MintingPolicyHash
PV1.mintingPolicyHash MintingPolicy
coinMintingPolicyV1

coinMintingPolicyHashV2 :: Ledger.MintingPolicyHash
coinMintingPolicyHashV2 :: MintingPolicyHash
coinMintingPolicyHashV2 = MintingPolicy -> MintingPolicyHash
PV2.mintingPolicyHash MintingPolicy
coinMintingPolicyV2

coinMintingPolicyCurrencySymbol :: Language -> Value.CurrencySymbol
coinMintingPolicyCurrencySymbol :: Language -> CurrencySymbol
coinMintingPolicyCurrencySymbol Language
lang = case Language
lang of
  Language
PlutusV1 -> CurrencySymbol
coinMintingPolicyCurrencySymbolV1
  Language
PlutusV2 -> CurrencySymbol
coinMintingPolicyCurrencySymbolV2

coinMintingPolicyCurrencySymbolV1 :: Value.CurrencySymbol
coinMintingPolicyCurrencySymbolV1 :: CurrencySymbol
coinMintingPolicyCurrencySymbolV1 = MintingPolicyHash -> CurrencySymbol
Value.mpsSymbol (MintingPolicyHash -> CurrencySymbol)
-> MintingPolicyHash -> CurrencySymbol
forall a b. (a -> b) -> a -> b
$ Language -> MintingPolicyHash
coinMintingPolicyHash Language
PlutusV1

coinMintingPolicyCurrencySymbolV2 :: Value.CurrencySymbol
coinMintingPolicyCurrencySymbolV2 :: CurrencySymbol
coinMintingPolicyCurrencySymbolV2 = MintingPolicyHash -> CurrencySymbol
Value.mpsSymbol (MintingPolicyHash -> CurrencySymbol)
-> MintingPolicyHash -> CurrencySymbol
forall a b. (a -> b) -> a -> b
$ Language -> MintingPolicyHash
coinMintingPolicyHash Language
PlutusV2

someToken :: Language -> Value.Value
someToken :: Language -> Value
someToken Language
lang = CurrencySymbol -> TokenName -> Integer -> Value
Value.singleton (Language -> CurrencySymbol
coinMintingPolicyCurrencySymbol Language
lang) TokenName
"someToken" Integer
1

asRedeemer :: PlutusTx.ToData a => a -> Ledger.Redeemer
asRedeemer :: a -> Redeemer
asRedeemer a
a = BuiltinData -> Redeemer
Ledger.Redeemer (BuiltinData -> Redeemer) -> BuiltinData -> Redeemer
forall a b. (a -> b) -> a -> b
$ Data -> BuiltinData
PlutusTx.dataToBuiltinData (Data -> BuiltinData) -> Data -> BuiltinData
forall a b. (a -> b) -> a -> b
$ a -> Data
forall a. ToData a => a -> Data
PlutusTx.toData a
a

asDatum :: PlutusTx.ToData a => a -> Ledger.Datum
asDatum :: a -> Datum
asDatum a
a = BuiltinData -> Datum
Ledger.Datum (BuiltinData -> Datum) -> BuiltinData -> Datum
forall a b. (a -> b) -> a -> b
$ Data -> BuiltinData
PlutusTx.dataToBuiltinData (Data -> BuiltinData) -> Data -> BuiltinData
forall a b. (a -> b) -> a -> b
$ a -> Data
forall a. ToData a => a -> Data
PlutusTx.toData a
a

coinMintingPolicyId :: Language -> C.PolicyId
coinMintingPolicyId :: Language -> PolicyId
coinMintingPolicyId = Versioned MintingPolicy -> PolicyId
policyId (Versioned MintingPolicy -> PolicyId)
-> (Language -> Versioned MintingPolicy) -> Language -> PolicyId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Language -> Versioned MintingPolicy
coinMintingPolicy

testNetworkMagic :: C.NetworkMagic
testNetworkMagic :: NetworkMagic
testNetworkMagic = Word32 -> NetworkMagic
C.NetworkMagic Word32
1097911063

testnet :: C.NetworkId
testnet :: NetworkId
testnet = NetworkMagic -> NetworkId
C.Testnet NetworkMagic
testNetworkMagic