Skip to content

Commit

Permalink
WIP: Move time related operations to Contract, with new abstractions.…
Browse files Browse the repository at this point in the history
… Added new handlers for chain tip and current epoch.
  • Loading branch information
jy14898 committed Nov 27, 2022
1 parent 3455be5 commit 8000a45
Show file tree
Hide file tree
Showing 52 changed files with 457 additions and 454 deletions.
4 changes: 2 additions & 2 deletions examples/AlwaysMints.purs
Expand Up @@ -35,7 +35,7 @@ import Effect.Exception (error)
main :: Effect Unit
main = example testnetNamiConfig

contract :: Contract () Unit
contract :: Contract Unit
contract = do
logInfo' "Running Examples.AlwaysMints"
mp /\ cs <- Helpers.mkCurrencySymbol alwaysMintsPolicy
Expand Down Expand Up @@ -65,7 +65,7 @@ alwaysMintsPolicyMaybe = do
envelope <- decodeTextEnvelope alwaysMints
PlutusMintingPolicy <$> plutusScriptV1FromEnvelope envelope

alwaysMintsPolicy :: Contract () MintingPolicy
alwaysMintsPolicy :: Contract MintingPolicy
alwaysMintsPolicy =
liftMaybe (error "Error decoding alwaysMintsPolicy")
alwaysMintsPolicyMaybe
8 changes: 4 additions & 4 deletions examples/AlwaysSucceeds.purs
Expand Up @@ -42,7 +42,7 @@ import Effect.Exception (error)
main :: Effect Unit
main = example testnetNamiConfig

contract :: Contract () Unit
contract :: Contract Unit
contract = do
logInfo' "Running Examples.AlwaysSucceeds"
validator <- alwaysSucceedsScript
Expand All @@ -57,7 +57,7 @@ example :: ConfigParams () -> Effect Unit
example cfg = launchAff_ do
runContract cfg contract

payToAlwaysSucceeds :: ValidatorHash -> Contract () TransactionHash
payToAlwaysSucceeds :: ValidatorHash -> Contract TransactionHash
payToAlwaysSucceeds vhash = do
-- Send to own stake credential. This is used to test mustPayToScriptAddress.
mbStakeKeyHash <- join <<< head <$> ownStakePubKeysHashes
Expand Down Expand Up @@ -87,7 +87,7 @@ spendFromAlwaysSucceeds
:: ValidatorHash
-> Validator
-> TransactionHash
-> Contract () Unit
-> Contract Unit
spendFromAlwaysSucceeds vhash validator txId = do
-- Use own stake credential if available
mbStakeKeyHash <- join <<< head <$> ownStakePubKeysHashes
Expand Down Expand Up @@ -119,7 +119,7 @@ spendFromAlwaysSucceeds vhash validator txId = do

foreign import alwaysSucceeds :: String

alwaysSucceedsScript :: Contract () Validator
alwaysSucceedsScript :: Contract Validator
alwaysSucceedsScript =
liftMaybe (error "Error decoding alwaysSucceeds") do
envelope <- decodeTextEnvelope alwaysSucceeds
Expand Down
2 changes: 1 addition & 1 deletion examples/AwaitTxConfirmedWithTimeout.purs
Expand Up @@ -27,7 +27,7 @@ example :: ConfigParams () -> Effect Unit
example cfg = launchAff_ do
runContract cfg contract

contract :: Contract () Unit
contract :: Contract Unit
contract = do
logInfo' "Running AwaitTxConfirmedWithTimeout"
let
Expand Down
2 changes: 1 addition & 1 deletion examples/BalanceTxConstraints.purs
Expand Up @@ -107,7 +107,7 @@ assertions =
, assertSelectedUtxoIsNotSpent
]

contract :: ContractParams -> Contract () Unit
contract :: ContractParams -> Contract Unit
contract (ContractParams p) = do
logInfo' "Examples.BalanceTxConstraints"

Expand Down
2 changes: 1 addition & 1 deletion examples/ByUrl.purs
Expand Up @@ -65,7 +65,7 @@ wallets = Map.fromFoldable
, "plutip-lode-mock" /\ mainnetLodeConfig /\ Just MockLode
]

examples :: Map E2ETestName (Contract () Unit)
examples :: Map E2ETestName (Contract Unit)
examples = Map.fromFoldable
[ "AlwaysMints" /\ AlwaysMints.contract
, "NativeScriptMints" /\ NativeScriptMints.contract
Expand Down
6 changes: 3 additions & 3 deletions examples/Cip30.purs
Expand Up @@ -60,7 +60,7 @@ nonConfigFunctions extensionWallet = do
result <- f extensionWallet
log $ msg <> ":" <> (show result)

contract :: Contract () Unit
contract :: Contract Unit
contract = do
logInfo' "Running Examples.Cip30"
logInfo' "Funtions that depend on `Contract`"
Expand All @@ -84,8 +84,8 @@ contract = do
:: forall (a :: Type)
. Show a
=> String
-> Contract () a
-> Contract () a
-> Contract a
-> Contract a
performAndLog logMsg cont = do
result <- cont
logInfo' $ logMsg <> ": " <> show result
Expand Down
6 changes: 3 additions & 3 deletions examples/ContractTestUtils.purs
Expand Up @@ -78,7 +78,7 @@ type ContractResult =

mkAssertions
:: ContractParams
-> Contract ()
-> Contract
( Array (ContractWrapAssertion () ContractResult)
/\ Array (ContractBasicAssertion () ContractResult Unit)
)
Expand Down Expand Up @@ -115,7 +115,7 @@ mkAssertions params@(ContractParams p) = do
TestUtils.assertTxHasMetadata "CIP25 Metadata" txHash p.txMetadata
]

contract :: ContractParams -> Contract () Unit
contract :: ContractParams -> Contract Unit
contract params@(ContractParams p) = do
logInfo' "Running Examples.ContractTestUtils"
ownPkh <- liftedM "Failed to get own PKH" $ head <$> ownPaymentPubKeysHashes
Expand Down Expand Up @@ -181,7 +181,7 @@ contract params@(ContractParams p) = do
OutputDatumHash _ -> true
_ -> false

getReceiverAddress :: ContractParams -> Contract () (Maybe Address)
getReceiverAddress :: ContractParams -> Contract (Maybe Address)
getReceiverAddress (ContractParams { receiverPkh, receiverSkh }) =
getNetworkId <#> \networkId ->
case receiverSkh of
Expand Down
6 changes: 3 additions & 3 deletions examples/Datums.purs
Expand Up @@ -19,7 +19,7 @@ module Ctl.Examples.Datums (main, contract, example) where

import Contract.Prelude

import Contract.Config (ConfigParams, testnetConfig)
import Contract.Config (ContractParams, testnetConfig)
import Contract.Log (logInfo')
import Contract.Monad (Contract, launchAff_, runContract)
import Contract.PlutusData (DataHash, getDatumByHash, getDatumsByHashes)
Expand All @@ -28,7 +28,7 @@ import Contract.Prim.ByteArray (hexToByteArrayUnsafe)
main :: Effect Unit
main = example testnetConfig

contract :: Contract () Unit
contract :: Contract Unit
contract = do
logInfo' "Running Examples.Datums"
logInfo' <<< show =<< getDatumByHash
Expand All @@ -45,6 +45,6 @@ contract = do
mkDatumHash :: String -> DataHash
mkDatumHash = wrap <<< hexToByteArrayUnsafe

example :: ConfigParams () -> Effect Unit
example :: ContractParams -> Effect Unit
example cfg = launchAff_ $ do
runContract cfg contract
6 changes: 3 additions & 3 deletions examples/IncludeDatum.purs
Expand Up @@ -55,7 +55,7 @@ example cfg = launchAff_ do
datum :: Datum
datum = Datum $ Integer $ BigInt.fromInt 42

payToIncludeDatum :: ValidatorHash -> Contract () TransactionHash
payToIncludeDatum :: ValidatorHash -> Contract TransactionHash
payToIncludeDatum vhash =
let
constraints :: TxConstraints Unit Unit
Expand All @@ -75,7 +75,7 @@ spendFromIncludeDatum
:: ValidatorHash
-> Validator
-> TransactionHash
-> Contract () Unit
-> Contract Unit
spendFromIncludeDatum vhash validator txId = do
let scriptAddress = scriptHashAddress vhash Nothing
utxos <- utxosAt scriptAddress
Expand All @@ -97,7 +97,7 @@ spendFromIncludeDatum vhash validator txId = do
foreign import includeDatum :: String

-- | checks if the datum equals 42
only42Script :: Contract () Validator
only42Script :: Contract Validator
only42Script =
liftMaybe (error "Error decoding includeDatum") do
envelope <- decodeTextEnvelope includeDatum
Expand Down
6 changes: 3 additions & 3 deletions examples/KeyWallet/Cip30.purs
Expand Up @@ -24,7 +24,7 @@ import Effect.Exception (error)
main :: Effect Unit
main = runKeyWalletContract_ mkContract

mkContract :: RawBytes -> Contract () Unit
mkContract :: RawBytes -> Contract Unit
mkContract dat = do
logInfo' "Running Examples.KeyWallet.Cip30"
logInfo' "Funtions that depend on `Contract`"
Expand All @@ -44,8 +44,8 @@ mkContract dat = do
:: forall (a :: Type)
. Show a
=> String
-> Contract () a
-> Contract () a
-> Contract a
-> Contract a
performAndLog logMsg cont = do
result <- cont
logInfo' $ logMsg <> ": " <> show result
Expand Down
2 changes: 1 addition & 1 deletion examples/KeyWallet/Internal/Cip30Contract.purs
Expand Up @@ -27,7 +27,7 @@ import Effect.Class (class MonadEffect)
import Effect.Exception (Error, error, message)

runKeyWalletContract_
:: (RawBytes -> Contract () Unit) -> Effect Unit
:: (RawBytes -> Contract Unit) -> Effect Unit
runKeyWalletContract_ contract =
HtmlForm.mkForm \input log' unlock ->
launchAff_ $ flip catchError (errorHandler log' unlock) $ do
Expand Down
2 changes: 1 addition & 1 deletion examples/KeyWallet/Internal/Pkh2PkhContract.purs
Expand Up @@ -31,7 +31,7 @@ import Effect.Class (class MonadEffect)
import Effect.Exception (Error, error, message)

runKeyWalletContract_
:: (PaymentPubKeyHash -> BigInt -> Unlock -> Contract () Unit) -> Effect Unit
:: (PaymentPubKeyHash -> BigInt -> Unlock -> Contract Unit) -> Effect Unit
runKeyWalletContract_ contract =
HtmlForm.mkForm \input log' unlock ->
launchAff_ $ flip catchError (errorHandler log' unlock) $ do
Expand Down
6 changes: 3 additions & 3 deletions examples/Lose7Ada.purs
Expand Up @@ -57,7 +57,7 @@ example cfg = launchAff_ do
logInfo' "Tx submitted successfully, Try to spend locked values"
spendFromAlwaysFails vhash validator txId

payToAlwaysFails :: ValidatorHash -> Contract () TransactionHash
payToAlwaysFails :: ValidatorHash -> Contract TransactionHash
payToAlwaysFails vhash = do
let
constraints :: TxConstraints Unit Unit
Expand All @@ -76,7 +76,7 @@ spendFromAlwaysFails
:: ValidatorHash
-> Validator
-> TransactionHash
-> Contract () Unit
-> Contract Unit
spendFromAlwaysFails vhash validator txId = do
balanceBefore <- fold <$> getWalletBalance
let scriptAddress = scriptHashAddress vhash Nothing
Expand Down Expand Up @@ -116,7 +116,7 @@ spendFromAlwaysFails vhash validator txId = do

foreign import alwaysFails :: String

alwaysFailsScript :: Contract () Validator
alwaysFailsScript :: Contract Validator
alwaysFailsScript =
liftMaybe (error "Error decoding alwaysFails") do
envelope <- decodeTextEnvelope alwaysFails
Expand Down
8 changes: 4 additions & 4 deletions examples/MintsMultipleTokens.purs
Expand Up @@ -37,7 +37,7 @@ import Effect.Exception (error)
main :: Effect Unit
main = example testnetNamiConfig

contract :: Contract () Unit
contract :: Contract Unit
contract = do
logInfo' "Running Examples.MintsMultipleTokens"
tn1 <- Helpers.mkTokenName "Token with a long name"
Expand Down Expand Up @@ -79,19 +79,19 @@ foreign import redeemerInt1 :: String
foreign import redeemerInt2 :: String
foreign import redeemerInt3 :: String

mintingPolicyRdmrInt1 :: Contract () MintingPolicy
mintingPolicyRdmrInt1 :: Contract MintingPolicy
mintingPolicyRdmrInt1 =
liftMaybe (error "Error decoding redeemerInt1") do
envelope <- decodeTextEnvelope redeemerInt3
PlutusMintingPolicy <$> plutusScriptV1FromEnvelope envelope

mintingPolicyRdmrInt2 :: Contract () MintingPolicy
mintingPolicyRdmrInt2 :: Contract MintingPolicy
mintingPolicyRdmrInt2 =
liftMaybe (error "Error decoding redeemerInt2") do
envelope <- decodeTextEnvelope redeemerInt3
PlutusMintingPolicy <$> plutusScriptV1FromEnvelope envelope

mintingPolicyRdmrInt3 :: Contract () MintingPolicy
mintingPolicyRdmrInt3 :: Contract MintingPolicy
mintingPolicyRdmrInt3 =
liftMaybe (error "Error decoding redeemerInt3") do
envelope <- decodeTextEnvelope redeemerInt3
Expand Down
10 changes: 5 additions & 5 deletions examples/MultipleRedeemers.purs
Expand Up @@ -44,7 +44,7 @@ import Data.Map as Map
import Data.Traversable (sequence)
import Effect.Exception (error)

contract :: Contract () Unit
contract :: Contract Unit
contract = do
tokenName <- mkTokenName "Token"
validator1 <- redeemerIs1Validator
Expand All @@ -69,7 +69,7 @@ contract = do
constraints
void $ awaitTxConfirmed txHash

contractWithMintRedeemers :: Contract () Unit
contractWithMintRedeemers :: Contract Unit
contractWithMintRedeemers = do
tokenName <- mkTokenName "Token"
validator1 <- redeemerIs1Validator
Expand Down Expand Up @@ -127,19 +127,19 @@ foreign import vredeemerInt2 :: String
foreign import vredeemerInt3 :: String

-- | checks whether redeemer is 1
redeemerIs1Validator :: Contract () Validator
redeemerIs1Validator :: Contract Validator
redeemerIs1Validator = liftMaybe (error "Error decoding vredeemerInt1") do
envelope <- decodeTextEnvelope vredeemerInt1
Validator <$> plutusScriptV1FromEnvelope envelope

-- | checks whether redeemer is 2
redeemerIs2Validator :: Contract () Validator
redeemerIs2Validator :: Contract Validator
redeemerIs2Validator = liftMaybe (error "Error decoding vredeemerInt2") do
envelope <- decodeTextEnvelope vredeemerInt2
Validator <$> plutusScriptV1FromEnvelope envelope

-- | checks whether redeemer is 3
redeemerIs3Validator :: Contract () Validator
redeemerIs3Validator :: Contract Validator
redeemerIs3Validator = liftMaybe (error "Error decoding vredeemerInt3") do
envelope <- decodeTextEnvelope vredeemerInt3
Validator <$> plutusScriptV1FromEnvelope envelope
4 changes: 2 additions & 2 deletions examples/NativeScriptMints.purs
Expand Up @@ -34,7 +34,7 @@ import Data.BigInt as BigInt
main :: Effect Unit
main = example testnetNamiConfig

contract :: Contract () Unit
contract :: Contract Unit
contract = do
logInfo' "Running Examples.NativeScriptMints"

Expand All @@ -60,7 +60,7 @@ contract = do

toSelfContract cs tn $ BigInt.fromInt 50

toSelfContract :: CurrencySymbol -> TokenName -> BigInt -> Contract () Unit
toSelfContract :: CurrencySymbol -> TokenName -> BigInt -> Contract Unit
toSelfContract cs tn amount = do
pkh <- liftedM "Failed to get own PKH" $ head <$> ownPaymentPubKeysHashes
skh <- join <<< head <$> ownStakePubKeysHashes
Expand Down
12 changes: 6 additions & 6 deletions examples/OneShotMinting.purs
Expand Up @@ -73,14 +73,14 @@ mkAssertions ownAddress nft =
\{ txFinalFee } -> pure txFinalFee
]

contract :: Contract () Unit
contract :: Contract Unit
contract =
mkContractWithAssertions "Examples.OneShotMinting" oneShotMintingPolicy

mkContractWithAssertions
:: String
-> (TransactionInput -> Contract () MintingPolicy)
-> Contract () Unit
-> (TransactionInput -> Contract MintingPolicy)
-> Contract Unit
mkContractWithAssertions exampleName mkMintingPolicy = do
logInfo' ("Running " <> exampleName)

Expand Down Expand Up @@ -116,11 +116,11 @@ mkContractWithAssertions exampleName mkMintingPolicy = do

foreign import oneShotMinting :: String

oneShotMintingPolicy :: TransactionInput -> Contract () MintingPolicy
oneShotMintingPolicy :: TransactionInput -> Contract MintingPolicy
oneShotMintingPolicy =
map PlutusMintingPolicy <<< oneShotMintingPolicyScript

oneShotMintingPolicyScript :: TransactionInput -> Contract () PlutusScript
oneShotMintingPolicyScript :: TransactionInput -> Contract PlutusScript
oneShotMintingPolicyScript txInput = do
script <- liftMaybe (error "Error decoding oneShotMinting") do
envelope <- decodeTextEnvelope oneShotMinting
Expand All @@ -130,7 +130,7 @@ oneShotMintingPolicyScript txInput = do
mkOneShotMintingPolicy
:: PlutusScript
-> TransactionInput
-> Contract () PlutusScript
-> Contract PlutusScript
mkOneShotMintingPolicy unappliedMintingPolicy oref =
let
mintingPolicyArgs :: Array PlutusData
Expand Down
2 changes: 1 addition & 1 deletion examples/Pkh2Pkh.purs
Expand Up @@ -20,7 +20,7 @@ import Data.BigInt as BigInt
main :: Effect Unit
main = example testnetNamiConfig

contract :: Contract () Unit
contract :: Contract Unit
contract = do
logInfo' "Running Examples.Pkh2Pkh"
pkh <- liftedM "Failed to get own PKH" $ head <$> ownPaymentPubKeysHashes
Expand Down
2 changes: 1 addition & 1 deletion examples/PlutusV2/AlwaysSucceeds.purs
Expand Up @@ -25,7 +25,7 @@ import Ctl.Examples.PlutusV2.Scripts.AlwaysSucceeds (alwaysSucceedsScriptV2)
main :: Effect Unit
main = example testnetNamiConfig

contract :: Contract () Unit
contract :: Contract Unit
contract = do
logInfo' "Running Examples.PlutusV2.AlwaysSucceeds"
validator <- alwaysSucceedsScriptV2
Expand Down

0 comments on commit 8000a45

Please sign in to comment.