Skip to content

Instantly share code, notes, and snippets.

@ahaxu
Created March 18, 2023 06:14
Show Gist options
  • Save ahaxu/d7d97ff2615f07e822ea474483a8313f to your computer and use it in GitHub Desktop.
Save ahaxu/d7d97ff2615f07e822ea474483a8313f to your computer and use it in GitHub Desktop.
vesting contract generated by chat gpt
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
module VestingSpecificRecipient
( vestingSpecificRecipient
, LockParams(..)
, VestingSchema
, VestingError(..)
) where
import Control.Monad hiding (fmap)
import qualified Data.Map as Map
import Data.Maybe (fromMaybe)
import Plutus.Contract
import Plutus.Contract.Types hiding (Wallet)
import qualified PlutusTx
import PlutusTx.Prelude hiding (Semigroup(..), unless)
import Ledger
import Ledger.TimeSlot (SlotRange)
import qualified Ledger.Typed.Scripts as Scripts
import Ledger.Value (Value)
import qualified Ledger.Value as Value
-- | The parameters for the vesting contract.
data LockParams = LockParams
{ lockAmount :: Value
, lockDeadline :: Slot
}
-- | The error types for the vesting contract.
data VestingError = VestingAmountExceeded
| VestingDeadlineMissed
deriving Show
PlutusTx.makeLift ''LockParams
-- | The schema for the vesting contract.
type VestingSchema =
Endpoint "vest" ()
-- | The vesting contract for a specific recipient.
vestingSpecificRecipient :: AsContractError e => Address -> LockParams -> Contract VestingSchema e ()
vestingSpecificRecipient recipientAddress params = do
logInfo @String "Vesting contract started"
vestEndpoint <- getEndpoint @"vest"
let checkAmount p = if Value.geq (lockAmount p) $ Value.negate $ scriptContextTxOutValue @() @() ctx
then Right ()
else Left VestingAmountExceeded
checkDeadline p = if contains (to $ lockDeadline p) $ txInfoValidRange $ scriptContextTxInfo ctx
then Right ()
else Left VestingDeadlineMissed
-- | Define the validator script for the vesting contract.
vestingValidator :: LockParams -> () -> ScriptContext -> Bool
vestingValidator p _ ctx =
checkAmount p == Right () && checkDeadline p == Right ()
where
contains d r = d `from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment