Skip to content

Commit

Permalink
Merge pull request #345 from michaelpj/imp/plutus-tx-catchall
Browse files Browse the repository at this point in the history
plutus-tx: add a top-level "batteries-included" module
  • Loading branch information
j-mueller authored Nov 29, 2018
2 parents 3c0312b + 016b93e commit 22fd01e
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 35 deletions.
1 change: 1 addition & 0 deletions plutus-tx/plutus-tx.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ source-repository head

library
exposed-modules:
Language.PlutusTx
Language.PlutusTx.TH
Language.PlutusTx.Prelude
Language.PlutusTx.Evaluation
Expand Down
5 changes: 5 additions & 0 deletions plutus-tx/src/Language/PlutusTx.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Language.PlutusTx (module Export, makeLift) where

import Language.PlutusTx.Lift (makeLift)
import Language.PlutusTx.Prelude as Export
import Language.PlutusTx.TH as Export
14 changes: 12 additions & 2 deletions plutus-tx/src/Language/PlutusTx/Prelude.hs
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
{-# LANGUAGE TemplateHaskell #-}
module Language.PlutusTx.Prelude where
module Language.PlutusTx.Prelude (
-- * String and tracing functions
toPlutusString,
trace,
traceH,
-- error is the only builtin that people are likely to want to use directly
-- * Re-exported builtins
error) where

import Prelude hiding (error)

import qualified Language.PlutusTx.Builtins as Builtins
import Language.PlutusTx.Builtins (error)

import Language.Haskell.TH
import Language.Haskell.TH

-- | Convert a Haskell 'String' into a 'Builtins.String'.
toPlutusString :: Q (TExp (String -> Builtins.String))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ import GHC.Generics (Generic)

import Language.Plutus.Runtime (Height (..), PendingTx (..), PendingTxIn (..), PendingTxOut, PubKey (..),
ValidatorHash, Value (..))
import qualified Language.PlutusTx.Builtins as Builtins
import Language.PlutusTx.Lift (makeLift)
import Language.PlutusTx.TH (plutus)
import qualified Language.PlutusTx as PlutusTx
import Wallet.API (EventHandler (..), EventTrigger, Range (..), WalletAPI (..),
WalletDiagnostics (..), andT, blockHeightT, fundsAtAddressT, otherError,
ownPubKeyTxOut, payToScript, pubKey, signAndSubmit)
Expand All @@ -58,12 +56,12 @@ data Campaign = Campaign

type CampaignActor = PubKey

makeLift ''Campaign
PlutusTx.makeLift ''Campaign

data CampaignAction = Collect | Refund
deriving Generic

makeLift ''CampaignAction
PlutusTx.makeLift ''CampaignAction

-- | Contribute funds to the campaign (contributor)
--
Expand Down Expand Up @@ -125,7 +123,7 @@ contributionScript cmp = Validator val where

-- See note [Contracts and Validator Scripts] in
-- Language.Plutus.Coordination.Contracts
inner = UTXO.fromPlcCode $$(plutus [|| (\Campaign{..} (act :: CampaignAction) (a :: CampaignActor) (p :: PendingTx ValidatorHash) ->
inner = UTXO.fromPlcCode $$(PlutusTx.plutus [|| (\Campaign{..} (act :: CampaignAction) (a :: CampaignActor) (p :: PendingTx ValidatorHash) ->
let

infixr 3 &&
Expand Down Expand Up @@ -178,7 +176,7 @@ contributionScript cmp = Validator val where
signedByT p campaignOwner
in payToOwner
in
if isValid then () else Builtins.error ()) ||])
if isValid then () else PlutusTx.error ()) ||])

-- | An event trigger that fires when a refund of campaign contributions can be claimed
refundTrigger :: Campaign -> EventTrigger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ import Control.Monad.Error.Class (MonadError (..))
import qualified Data.Set as Set
import GHC.Generics (Generic)
import qualified Language.Plutus.Runtime.TH as TH
import qualified Language.PlutusTx.Builtins as Builtins
import Language.PlutusTx.Lift (makeLift)
import Language.PlutusTx.TH (plutus)
import qualified Language.PlutusTx as PlutusTx
import Wallet.API (WalletAPI (..), WalletAPIError, otherError, pubKey, signAndSubmit)
import Wallet.UTXO (DataScript (..), TxOutRef', Validator (..), scriptTxIn, scriptTxOut)
import qualified Wallet.UTXO as UTXO
Expand Down Expand Up @@ -196,7 +194,7 @@ data FutureRedeemer =
validatorScript :: Future -> Validator
validatorScript ft = Validator val where
val = UTXO.applyScript inner (UTXO.lifted ft)
inner = UTXO.fromPlcCode $$(plutus [||
inner = UTXO.fromPlcCode $$(PlutusTx.plutus [||
\Future{..} (r :: FutureRedeemer) FutureData{..} (p :: (PendingTx ValidatorHash)) ->

let
Expand Down Expand Up @@ -248,7 +246,7 @@ validatorScript ft = Validator val where

verifyOracle :: OracleValue a -> (Height, a)
verifyOracle (OracleValue (Signed (pk, t))) =
if pk `eqPk` futurePriceOracle then t else Builtins.error ()
if pk `eqPk` futurePriceOracle then t else PlutusTx.error ()

isValid =
case r of
Expand Down Expand Up @@ -308,9 +306,9 @@ validatorScript ft = Validator val where

_ -> False
in
if isValid then () else Builtins.error ()
if isValid then () else PlutusTx.error ()
||])

makeLift ''Future
makeLift ''FutureData
makeLift ''FutureRedeemer
PlutusTx.makeLift ''Future
PlutusTx.makeLift ''FutureData
PlutusTx.makeLift ''FutureRedeemer
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ module Language.Plutus.Coordination.Contracts.Swap(
import Language.Plutus.Runtime (OracleValue (..), PendingTx (..), PendingTxIn (..), PendingTxOut (..),
PubKey, ValidatorHash, Value (..))
import qualified Language.Plutus.Runtime.TH as TH
import qualified Language.PlutusTx.Builtins as Builtins
import Language.PlutusTx.TH (plutus)
import qualified Language.PlutusTx as PlutusTx
import Wallet.UTXO (Height, Validator (..))
import qualified Wallet.UTXO as UTXO

Expand Down Expand Up @@ -59,7 +58,7 @@ type SwapOracle = OracleValue (Ratio Int)
-- Language.Plutus.Coordination.Contracts
swapValidator :: Swap -> Validator
swapValidator _ = Validator result where
result = UTXO.fromPlcCode $$(plutus [|| (\(redeemer :: SwapOracle) SwapOwners{..} (p :: PendingTx ValidatorHash) Swap{..} ->
result = UTXO.fromPlcCode $$(PlutusTx.plutus [|| (\(redeemer :: SwapOracle) SwapOwners{..} (p :: PendingTx ValidatorHash) Swap{..} ->
let
infixr 3 &&
(&&) :: Bool -> Bool -> Bool
Expand All @@ -81,14 +80,14 @@ swapValidator _ = Validator result where
minusR (x :% y) (x' :% y') = (x*y' - x'*y) :% (y*y')

extractVerifyAt :: OracleValue (Ratio Int) -> PubKey -> Ratio Int -> Height -> Ratio Int
extractVerifyAt = Builtins.error ()
extractVerifyAt = PlutusTx.error ()

round :: Ratio Int -> Int
round = Builtins.error ()
round = PlutusTx.error ()

-- | Convert an [[Int]] to a [[Ratio Int]]
fromInt :: Int -> Ratio Int
fromInt = Builtins.error ()
fromInt = PlutusTx.error ()

signedBy :: PendingTxIn -> PubKey -> Bool
signedBy = $$(TH.txInSignedBy)
Expand Down Expand Up @@ -174,7 +173,7 @@ swapValidator _ = Validator result where


in
if inConditions && outConditions then () else Builtins.error ()
if inConditions && outConditions then () else PlutusTx.error ()
) ||])

{- Note [Swap Transactions]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ import GHC.Generics (Generic)
import Language.Plutus.Runtime (Height (..), PendingTx (..), PendingTxOut (..), PendingTxOutType (..),
PubKey (..), ValidatorHash, Value (..))
import qualified Language.Plutus.Runtime.TH as TH
import qualified Language.PlutusTx.Builtins as Builtins
import Language.PlutusTx.Lift (makeLift)
import Language.PlutusTx.TH (plutus)
import qualified Language.PlutusTx as PlutusTx
import Prelude hiding ((&&))
import Wallet.API (WalletAPI (..), WalletAPIError, otherError, ownPubKeyTxOut, signAndSubmit)
import Wallet.UTXO (DataScript (..), TxOutRef', Validator (..), scriptTxIn, scriptTxOut)
Expand All @@ -38,7 +36,7 @@ data VestingTranche = VestingTranche {
vestingTrancheAmount :: Value
} deriving Generic

makeLift ''VestingTranche
PlutusTx.makeLift ''VestingTranche

-- | A vesting scheme consisting of two tranches. Each tranche defines a date
-- (block height) after which an additional amount of money can be spent.
Expand All @@ -48,7 +46,7 @@ data Vesting = Vesting {
vestingOwner :: PubKey
} deriving Generic

makeLift ''Vesting
PlutusTx.makeLift ''Vesting

-- | The total amount of money vested
totalAmount :: Vesting -> Value
Expand All @@ -61,7 +59,7 @@ data VestingData = VestingData {
vestingDataPaidOut :: Value -- ^ How much of the vested value has already been retrieved
} deriving (Eq, Generic)

makeLift ''VestingData
PlutusTx.makeLift ''VestingData

-- | Lock some funds with the vesting validator script and return a
-- [[VestingData]] representing the current state of the process
Expand Down Expand Up @@ -109,7 +107,7 @@ validatorScriptHash =
validatorScript :: Vesting -> Validator
validatorScript v = Validator val where
val = UTXO.applyScript inner (UTXO.lifted v)
inner = UTXO.fromPlcCode $$(plutus [|| \Vesting{..} () VestingData{..} (p :: PendingTx ValidatorHash) ->
inner = UTXO.fromPlcCode $$(PlutusTx.plutus [|| \Vesting{..} () VestingData{..} (p :: PendingTx ValidatorHash) ->
let

eqBs :: ValidatorHash -> ValidatorHash -> Bool
Expand All @@ -132,7 +130,7 @@ validatorScript v = Validator val where
amountSpent = case os of
PendingTxOut (Value v') _ (PubKeyTxOut pk):_
| pk `eqPk` vestingOwner -> v'
_ -> Builtins.error ()
_ -> PlutusTx.error ()

-- Value that has been released so far under the scheme
currentThreshold =
Expand All @@ -158,8 +156,8 @@ validatorScript v = Validator val where
txnOutputsValid = case os of
_:PendingTxOut _ (Just (vl', _)) DataTxOut:_ ->
vl' `eqBs` vestingDataHash
_ -> Builtins.error ()
_ -> PlutusTx.error ()

isValid = amountsValid && txnOutputsValid
in
if isValid then () else Builtins.error () ||])
if isValid then () else PlutusTx.error () ||])

0 comments on commit 22fd01e

Please sign in to comment.