Plutus Practical Examples(Marathons) - wimsio/universities GitHub Wiki

Marathons

Pasted image (16)

Welcome to Plutus Coding. This web page gives 30 different examples for students to convert to Real World Little Applications called Marathons. These are demos running on testnets but must have onchain, offchain running code solving real world problems.

Here are templates to use:

-Auction -Sundae Swap Dex -Vesting

1. Hello World Contract

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract
import           Prelude (IO)

-- | The helloWorld validator always succeeds
helloWorld :: ScriptContext -> Bool
helloWorld _ = True

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| helloWorld ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract defines a helloWorld function that always returns True, making it a contract that always succeeds. It includes boilerplate code to compile and get the validator script address.

2. Simple Token Minting

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Token minting logic
mint :: TokenName -> Integer -> ScriptContext -> Bool
mint tn amount ctx =
    let
        txInfo = scriptContextTxInfo ctx
        minted = valueMint txInfo
    in
        -- Check if the minted value matches the expected token and amount
        minted == singleton (CurrencySymbol "TokenSymbol") tn amount

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| mint ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract handles token minting. It checks if the minted value matches the expected token and amount. You need to replace "TokenSymbol" with your actual token symbol.

3. Basic Multi-Signature Wallet

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | A basic multi-signature wallet requires signatures from multiple parties
multiSig :: [PubKeyHash] -> ScriptContext -> Bool
multiSig requiredSignatories ctx =
    let
        txInfo = scriptContextTxInfo ctx
        actualSignatories = txInfoSignatories txInfo
    in
        -- Check if all required signatories are in the transaction's signatories
        all (`elem` actualSignatories) requiredSignatories

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| multiSig ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract represents a multi-signature wallet requiring signatures from a list of public key hashes. It verifies that all required signatories are present in the transaction.

4. Simple Voting Contract

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Voting logic
vote :: Integer -> ScriptContext -> Bool
vote option ctx =
    let
        validOptions = [1, 2, 3]  -- Example valid voting options
    in
        -- Check if the vote option is one of the valid options
        option `elem` validOptions

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| vote ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract handles a simple voting mechanism. It checks if the provided vote option is within a list of valid options.

5. Basic Escrow Contract

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Escrow contract logic
escrow :: PubKeyHash -> Bool -> ScriptContext -> Bool
escrow payer condition ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Check if the payer is the one who signed the transaction
        isPayer = payer `elem` txInfoSignatories txInfo
        -- Check if the condition is met
        conditionMet = condition
    in
        isPayer && conditionMet

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| escrow ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract represents a basic escrow mechanism. It checks if the payer is among the signatories and if a condition is met before releasing funds.

Certainly! Here are detailed implementations for Plutus smart contracts 6 through 15:

6. Token Swap Contract

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Token swap logic
swapTokens :: CurrencySymbol -> TokenName -> Integer -> ScriptContext -> Bool
swapTokens currencySymbol tokenName amount ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Check if the amount of the token in the swap is correct
        expectedAmount = singleton currencySymbol tokenName amount
        swappedAmount = valueSpent txInfo
    in
        -- Check if the token amount matches
        swappedAmount == expectedAmount

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| swapTokens ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract represents a token swap mechanism where the contract verifies that the amount of tokens being swapped matches the expected amount.

7. Decentralized Exchange (DEX) Order Matching

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Order matching logic for DEX
matchOrder :: CurrencySymbol -> TokenName -> Integer -> ScriptContext -> Bool
matchOrder currencySymbol tokenName amount ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Check if the order amount matches the expected amount
        expectedAmount = singleton currencySymbol tokenName amount
        matchedAmount = valueSpent txInfo
    in
        -- Check if the token amount matches
        matchedAmount == expectedAmount

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| matchOrder ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract is for order matching in a decentralized exchange. It verifies that the order amount matches the expected amount in the transaction.

8. NFT Minting and Transfer

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | NFT minting and transfer logic
nftMintTransfer :: CurrencySymbol -> TokenName -> Integer -> ScriptContext -> Bool
nftMintTransfer currencySymbol tokenName amount ctx =
    let
        txInfo = scriptContextTxInfo ctx
        mintedAmount = valueMint txInfo
        transferredAmount = valueSpent txInfo
    in
        -- Check if the minted and transferred amount matches
        mintedAmount == singleton currencySymbol tokenName amount &&
        transferredAmount == singleton currencySymbol tokenName amount

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| nftMintTransfer ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract handles NFT minting and transfer. It verifies that the amount of NFTs minted and transferred matches the expected amount.

9. Lending Protocol with Collateral

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Lending protocol with collateral logic
lendingWithCollateral :: Integer -> Integer -> ScriptContext -> Bool
lendingWithCollateral loanAmount collateralAmount ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Ensure collateral is provided for the loan
        collateralProvided = valueSpent txInfo
        sufficientCollateral = collateralProvided >= singleton "CollateralSymbol" "CollateralToken" collateralAmount
    in
        -- Check if collateral is sufficient
        sufficientCollateral

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| lendingWithCollateral ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract represents a lending protocol requiring collateral. It checks if the collateral provided in the transaction meets the required amount.

10. Staking Rewards Contract

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Staking rewards logic
stakingRewards :: Integer -> ScriptContext -> Bool
stakingRewards expectedRewards ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Calculate the actual rewards
        actualRewards = valueSpent txInfo
    in
        -- Check if the actual rewards match the expected rewards
        actualRewards == singleton "RewardTokenSymbol" "RewardToken" expectedRewards

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| stakingRewards ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract calculates and verifies staking rewards. It checks if the actual rewards match the expected rewards.

11. Charity Donation Contract

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Charity donation logic
charityDonation :: Integer -> ScriptContext -> Bool
charityDonation donationAmount ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Ensure that the correct donation amount is received
        receivedAmount = valueSpent txInfo
    in
        -- Check if the donation amount is correct
        receivedAmount == singleton "CharityTokenSymbol" "CharityToken" donationAmount

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| charityDonation ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract ensures that the correct amount is donated to a charity. It checks if the donation amount matches the expected amount.

12. Insurance Claim Contract

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Insurance claim logic
insuranceClaim :: Integer -> Bool -> ScriptContext -> Bool
insuranceClaim claimAmount condition ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Ensure the claim amount is correct and condition is met
        claimedAmount = valueSpent txInfo
    in
        -- Check if the claim amount and condition are valid
        claimedAmount == singleton "InsuranceTokenSymbol" "InsuranceToken" claimAmount &&
        condition

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| insuranceClaim ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract handles insurance claims, checking that the claim amount is correct and the required conditions are met.

13. Subscription Service Contract

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Subscription service logic
subscriptionService :: Integer -> ScriptContext -> Bool
subscriptionService subscriptionFee ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Ensure that the subscription fee is paid
        paidAmount = valueSpent txInfo
    in
        -- Check if the paid amount matches the subscription fee
        paidAmount == singleton "SubscriptionTokenSymbol" "SubscriptionToken" subscriptionFee

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| subscriptionService ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract manages subscriptions by checking if the correct subscription fee is paid.

Sure! Here are the detailed implementations for Plutus smart contracts 14 and 15:

14. Asset Management Contract

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Asset management logic
manageAsset :: CurrencySymbol -> TokenName -> Integer -> ScriptContext -> Bool
manageAsset currencySymbol tokenName expectedAmount ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Get the amount of the asset being managed
        managedAmount = valueSpent txInfo
        -- Define the expected asset amount
        expectedAsset = singleton currencySymbol tokenName expectedAmount
    in
        -- Check if the managed amount matches the expected amount
        managedAmount == expectedAsset

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| manageAsset ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract is for managing assets. It checks if the amount of the asset being managed matches the expected amount. For example, you could use this to manage the transfer or storage of specific assets.

15. Property Rental Contract

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Property rental logic
rentalAgreement :: Integer -> PubKeyHash -> ScriptContext -> Bool
rentalAgreement rentalAmount landlord ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Check if the rental amount matches
        paidAmount = valueSpent txInfo
        -- Ensure the landlord is the one who signed the transaction
        isLandlord = landlord `elem` txInfoSignatories txInfo
    in
        -- Check if the rental amount is correct and the landlord is the signatory
        paidAmount == singleton "RentalTokenSymbol" "RentalToken" rentalAmount &&
        isLandlord

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| rentalAgreement ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract represents a property rental agreement. It ensures that the rental amount paid matches the expected amount and verifies that the landlord is one of the signatories in the transaction.

Certainly! Here are detailed implementations for Plutus smart contracts 16 through 25:

16. Conditional Payment Contract

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Conditional payment logic
conditionalPayment :: Integer -> Bool -> ScriptContext -> Bool
conditionalPayment paymentAmount condition ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Check if the payment amount matches the expected amount
        paidAmount = valueSpent txInfo
        -- Ensure the condition is met
        conditionMet = condition
    in
        -- Check if the payment amount is correct and the condition is satisfied
        paidAmount == singleton "PaymentTokenSymbol" "PaymentToken" paymentAmount &&
        conditionMet

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| conditionalPayment ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract ensures that a payment is made only if a certain condition is met. It verifies that the payment amount is correct and the condition (a boolean value) is true.

17. Escrow Contract with Timelock

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Escrow contract with timelock
escrowWithTimelock :: POSIXTime -> PubKeyHash -> Bool -> ScriptContext -> Bool
escrowWithTimelock releaseTime payer condition ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Check if the timelock has expired
        currentTime = txInfoValidRange txInfo
        timelockExpired = currentTime >= releaseTime
        -- Ensure the payer is among the signatories
        isPayer = payer `elem` txInfoSignatories txInfo
        -- Check if the condition is met
        conditionMet = condition
    in
        -- Check if the timelock is expired and the condition is met
        timelockExpired && isPayer && conditionMet

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| escrowWithTimelock ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract implements an escrow with a timelock. Funds are only released if the timelock has expired, the payer is a signatory, and a condition is met.

18. Dynamic Asset Pricing

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Dynamic asset pricing logic
dynamicPricing :: Integer -> Integer -> ScriptContext -> Bool
dynamicPricing assetPrice paidAmount ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Check if the asset price matches the paid amount
        actualAmount = valueSpent txInfo
    in
        -- Check if the paid amount matches the asset price
        paidAmount >= assetPrice

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| dynamicPricing ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract is used for dynamic asset pricing. It checks if the amount paid for an asset is greater than or equal to the asset price.

19. Auction Contract

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Auction logic
auction :: Integer -> PubKeyHash -> ScriptContext -> Bool
auction highestBid bidder ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Ensure the highest bid is not exceeded
        bidAmount = valueSpent txInfo
        isHighestBidder = bidder `elem` txInfoSignatories txInfo
    in
        -- Check if the bid amount is valid and the bidder is correct
        bidAmount <= highestBid &&
        isHighestBidder

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| auction ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract handles auction logic. It ensures that the bid amount does not exceed the highest bid and that the correct bidder is participating.

20. Vesting Contract

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Vesting logic
vesting :: POSIXTime -> Integer -> ScriptContext -> Bool
vesting releaseTime vestedAmount ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Check if the current time is past the release time
        currentTime = txInfoValidRange txInfo
        timePassed = currentTime >= releaseTime
        -- Ensure the vested amount is correct
        amountVested = valueSpent txInfo
    in
        -- Check if the time has passed and the amount vested is correct
        timePassed && amountVested == singleton "VestingTokenSymbol" "VestingToken" vestedAmount

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| vesting ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract manages vesting schedules. It ensures that tokens are only released if the current time is past the vesting release time and the vested amount is correct.

21. Lottery Contract

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Lottery logic
lottery :: Integer -> PubKeyHash -> ScriptContext -> Bool
lottery ticketPrice winner ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Ensure the ticket price is paid
        paidAmount = valueSpent txInfo
        -- Ensure the winner is correct
        isWinner = winner `elem` txInfoSignatories txInfo
    in
        -- Check if the ticket price is correct and the winner is correct
        paidAmount == singleton "LotteryTokenSymbol" "LotteryToken" ticketPrice &&
        isWinner

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| lottery ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract handles lottery ticket sales. It checks if the ticket price is correct and verifies that the winner is among the signatories.

22. Dividend Distribution

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Dividend distribution logic
distributeDividends :: Integer -> ScriptContext -> Bool
distributeDividends dividendAmount ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Ensure the correct dividend amount is distributed
        distributedAmount = valueSpent txInfo
    in
        -- Check if the distributed amount matches the dividend amount
        distributedAmount == singleton "DividendTokenSymbol" "DividendToken" dividendAmount

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| distributeDividends ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract manages dividend distributions. It ensures that the amount of dividends distributed matches the expected dividend amount.

23. Digital Identity Verification

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Digital identity verification logic
verify

Identity :: PubKeyHash -> ScriptContext -> Bool
verifyIdentity userPubKeyHash ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Check if the user is among the signatories
        isVerifiedUser = userPubKeyHash `elem` txInfoSignatories txInfo
    in
        -- Ensure the user identity is verified
        isVerifiedUser

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| verifyIdentity ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract is used for digital identity verification. It checks if the specified public key hash is among the signatories in the transaction.

24. Simple Voting Contract

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Simple voting logic
vote :: Integer -> PubKeyHash -> ScriptContext -> Bool
vote choice voter ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Ensure the voter is among the signatories
        isVoter = voter `elem` txInfoSignatories txInfo
        -- Ensure the choice is valid (e.g., choice >= 0)
        isValidChoice = choice >= 0
    in
        -- Check if the voter is valid and the choice is valid
        isVoter && isValidChoice

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| vote ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract is a simple voting mechanism. It verifies that the voter is among the signatories and that the voting choice is valid.

25. Rewards Redemption Contract

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Rewards redemption logic
redeemRewards :: Integer -> ScriptContext -> Bool
redeemRewards rewardsAmount ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Ensure the correct amount of rewards is redeemed
        redeemedAmount = valueSpent txInfo
    in
        -- Check if the redeemed amount matches the rewards amount
        redeemedAmount == singleton "RewardsTokenSymbol" "RewardsToken" rewardsAmount

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| redeemRewards ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract handles the redemption of rewards. It verifies that the amount of rewards redeemed matches the expected amount.

Certainly! Here are the detailed implementations for Plutus smart contracts 26 through 30:

26. Token Swap Contract

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Token swap logic
tokenSwap :: CurrencySymbol -> TokenName -> Integer -> CurrencySymbol -> TokenName -> Integer -> ScriptContext -> Bool
tokenSwap fromCurrency fromToken fromAmount toCurrency toToken toAmount ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Get the amounts of tokens being swapped
        fromAmountSpent = valueSpent txInfo `valueOf` fromCurrency fromToken
        toAmountReceived = valueSpent txInfo `valueOf` toCurrency toToken
    in
        -- Check if the correct amounts of tokens are swapped
        fromAmountSpent == fromAmount && toAmountReceived == toAmount

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| tokenSwap ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract handles token swaps. It checks if the amount of tokens being swapped from one type to another matches the expected amounts.

27. Charity Donation Contract

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Charity donation logic
charityDonation :: Integer -> PubKeyHash -> ScriptContext -> Bool
charityDonation donationAmount charityAddr ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Check if the donation amount is correct
        donatedAmount = valueSpent txInfo
        -- Ensure the charity address is among the signatories
        isCharity = charityAddr `elem` txInfoSignatories txInfo
    in
        -- Check if the donation amount is correct and the charity address is verified
        donatedAmount == singleton "CharityTokenSymbol" "CharityToken" donationAmount &&
        isCharity

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| charityDonation ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract manages charity donations. It ensures that the donation amount is correct and that the charity address is among the signatories.

28. License Management Contract

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | License management logic
licenseManagement :: PubKeyHash -> Integer -> ScriptContext -> Bool
licenseManagement licenseHolder licenseFee ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Check if the license fee is paid
        paidAmount = valueSpent txInfo
        -- Ensure the license holder is among the signatories
        isLicenseHolder = licenseHolder `elem` txInfoSignatories txInfo
    in
        -- Check if the paid amount matches the license fee and the holder is correct
        paidAmount == singleton "LicenseTokenSymbol" "LicenseToken" licenseFee &&
        isLicenseHolder

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| licenseManagement ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract manages license fees. It ensures that the fee is paid and verifies that the license holder is a signatory.

29. Reward Points Redemption

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Reward points redemption logic
redeemPoints :: Integer -> PubKeyHash -> ScriptContext -> Bool
redeemPoints pointsRequired user ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Check if the points redeemed match the required points
        redeemedPoints = valueSpent txInfo
        -- Ensure the user is among the signatories
        isUser = user `elem` txInfoSignatories txInfo
    in
        -- Check if the redeemed points match the required points and the user is correct
        redeemedPoints == singleton "RewardPointsTokenSymbol" "RewardPointsToken" pointsRequired &&
        isUser

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| redeemPoints ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract handles the redemption of reward points. It verifies that the points redeemed match the required amount and that the user is a signatory.

30. Subscription Management Contract

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}

import           PlutusTx
import           PlutusTx.Prelude
import           Ledger
import           Ledger.Constraints as Constraints
import           Playground.Contract

-- | Subscription management logic
subscriptionManagement :: Integer -> PubKeyHash -> ScriptContext -> Bool
subscriptionManagement subscriptionFee subscriber ctx =
    let
        txInfo = scriptContextTxInfo ctx
        -- Check if the subscription fee is paid
        paidAmount = valueSpent txInfo
        -- Ensure the subscriber is among the signatories
        isSubscriber = subscriber `elem` txInfoSignatories txInfo
    in
        -- Check if the paid amount matches the subscription fee and the subscriber is correct
        paidAmount == singleton "SubscriptionTokenSymbol" "SubscriptionToken" subscriptionFee &&
        isSubscriber

-- Boilerplate code for compiling the validator
mkValidator :: Validator
mkValidator = mkValidatorScript $$(PlutusTx.compile [|| subscriptionManagement ||])

validatorScript :: Script
validatorScript = unValidatorScript mkValidator

-- Utility function to get the script address
scriptAddress :: Address
scriptAddress = scriptAddress validatorScript

Explanation: This contract manages subscriptions. It ensures that the subscription fee is paid and that the subscriber is a signatory.