Skip to content

Commit

Permalink
updating Currencies.hs
Browse files Browse the repository at this point in the history
  • Loading branch information
mmontin committed Mar 25, 2024
1 parent bd465dc commit d460745
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions src/Cooked/Currencies.hs
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE NoImplicitPrelude #-}

-- | This module introduces currencies (namely the /quick values/ and the
Expand Down Expand Up @@ -38,67 +29,73 @@ module Cooked.Currencies
quickCurrencySymbol,
permanentCurrencyPolicy,
permanentCurrencySymbol,
currencySymbolFromLanguageAndMp,
)
where

import qualified Ledger.Typed.Scripts as Scripts
import qualified Plutus.Script.Utils.V2.Scripts as Validation
import qualified Plutus.Script.Utils.Scripts as Pl
import qualified Plutus.Script.Utils.Typed as Pl
import qualified Plutus.Script.Utils.Value as Pl
import qualified Plutus.V2.Ledger.Api as Pl
import qualified PlutusLedgerApi.V3 as Pl
import qualified PlutusTx
import qualified PlutusTx.Builtins.Class as Pl
import PlutusTx.Prelude hiding (Applicative (..))
import qualified Prelude as Haskell
import PlutusTx.Prelude (Bool (..), Integer, curry, flip, (.))
import Prelude (String)

-- | Takes a minting policy and a language version and returns the
-- associated currency symbol
currencySymbolFromLanguageAndMp :: Pl.Language -> Pl.MintingPolicy -> Pl.CurrencySymbol
currencySymbolFromLanguageAndMp lang = Pl.scriptCurrencySymbol . flip Pl.Versioned lang

-- * Quick Values

-- | Token name of a /quick/ asset class; prefixes the name with a @'q'@ to
-- make it easy to distinguish between quick and permanent tokens.
quickTokenName :: Haskell.String -> Pl.TokenName
quickTokenName :: String -> Pl.TokenName
quickTokenName = Pl.TokenName . Pl.stringToBuiltinByteString

-- | /Quick/ asset class from a token name
quickAssetClass :: Haskell.String -> Pl.AssetClass
quickAssetClass :: String -> Pl.AssetClass
quickAssetClass = curry Pl.AssetClass quickCurrencySymbol . quickTokenName

-- | Constructor for /quick/ values from token name and amount
quickValue :: Haskell.String -> Integer -> Pl.Value
quickValue :: String -> Integer -> Pl.Value
quickValue = Pl.assetClassValue . quickAssetClass

{-# INLINEABLE mkQuickCurrencyPolicy #-}
mkQuickCurrencyPolicy :: () -> Pl.ScriptContext -> Bool
mkQuickCurrencyPolicy _ _ = True

quickCurrencyPolicy :: Scripts.MintingPolicy
quickCurrencyPolicy :: Pl.MintingPolicy
quickCurrencyPolicy =
Pl.mkMintingPolicyScript
$$(PlutusTx.compile [||Scripts.mkUntypedMintingPolicy mkQuickCurrencyPolicy||])
$$(PlutusTx.compile [||Pl.mkUntypedMintingPolicy mkQuickCurrencyPolicy||])

quickCurrencySymbol :: Pl.CurrencySymbol
quickCurrencySymbol = Validation.scriptCurrencySymbol quickCurrencyPolicy
quickCurrencySymbol = currencySymbolFromLanguageAndMp Pl.PlutusV3 quickCurrencyPolicy

-- * Permanent values

-- | Token name of a /permanent/ asset class
permanentTokenName :: Haskell.String -> Pl.TokenName
permanentTokenName :: String -> Pl.TokenName
permanentTokenName = Pl.TokenName . Pl.stringToBuiltinByteString

-- | /Permanent/ asset class from a token name
permanentAssetClass :: Haskell.String -> Pl.AssetClass
permanentAssetClass :: String -> Pl.AssetClass
permanentAssetClass = curry Pl.AssetClass permanentCurrencySymbol . permanentTokenName

-- | Constructor for /Permanent/ values from token name and amount
permanentValue :: Haskell.String -> Integer -> Pl.Value
permanentValue :: String -> Integer -> Pl.Value
permanentValue = Pl.assetClassValue . permanentAssetClass

{-# INLINEABLE mkPermanentCurrencyPolicy #-}
mkPermanentCurrencyPolicy :: () -> Pl.ScriptContext -> Bool
mkPermanentCurrencyPolicy _ _ = False

permanentCurrencyPolicy :: Scripts.MintingPolicy
permanentCurrencyPolicy :: Pl.MintingPolicy
permanentCurrencyPolicy =
Pl.mkMintingPolicyScript
$$(PlutusTx.compile [||Scripts.mkUntypedMintingPolicy mkPermanentCurrencyPolicy||])
$$(PlutusTx.compile [||Pl.mkUntypedMintingPolicy mkPermanentCurrencyPolicy||])

permanentCurrencySymbol :: Pl.CurrencySymbol
permanentCurrencySymbol = Validation.scriptCurrencySymbol permanentCurrencyPolicy
permanentCurrencySymbol = currencySymbolFromLanguageAndMp Pl.PlutusV3 permanentCurrencyPolicy

0 comments on commit d460745

Please sign in to comment.