Created
March 18, 2023 06:14
-
-
Save ahaxu/d7d97ff2615f07e822ea474483a8313f to your computer and use it in GitHub Desktop.
vesting contract generated by chat gpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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