Skip to content

Instantly share code, notes, and snippets.

@satran004
Created February 7, 2021 14:20
Show Gist options
  • Save satran004/ad5700ae64cbcb1294d32f8bf3d93b98 to your computer and use it in GitHub Desktop.
Save satran004/ad5700ae64cbcb1294d32f8bf3d93b98 to your computer and use it in GitHub Desktop.
Plutus Playground Smart Contract
import Control.Monad (void)
import Data.Aeson (FromJSON, ToJSON)
import qualified Data.Text as T
import GHC.Generics (Generic)
import Language.Plutus.Contract
import qualified Language.PlutusTx as PlutusTx
import Language.PlutusTx.Prelude
import Ledger
import qualified Ledger.Ada as Ada
import qualified Ledger.Constraints as Constraints
import qualified Ledger.Typed.Scripts as Scripts
import Schema
import Wallet.Emulator.Wallet
data SplitData =
SplitData
{ recipient1 :: PubKeyHash -- ^ First recipient of the funds
, recipient2 :: PubKeyHash -- ^ Second recipient of the funds
, amount :: Ada -- ^ How much Ada we want to lock
}
deriving stock (Show, Generic)
PlutusTx.makeIsData ''SplitData
PlutusTx.makeLift ''SplitData
validateSplit :: SplitData -> () -> ValidatorCtx -> Bool
validateSplit SplitData{recipient1, recipient2, amount} _ ValidatorCtx{valCtxTxInfo} =
let half = Ada.divide amount 2 in
Ada.fromValue (valuePaidTo valCtxTxInfo recipient1) >= half &&
Ada.fromValue (valuePaidTo valCtxTxInfo recipient2) >= (amount - half)
data Split
instance Scripts.ScriptType Split where
type instance RedeemerType Split = ()
type instance DatumType Split = SplitData
splitInstance :: Scripts.ScriptInstance Split
splitInstance = Scripts.validator @Split
$$(PlutusTx.compile [|| validateSplit ||])
$$(PlutusTx.compile [|| wrap ||]) where
wrap = Scripts.wrapValidator @SplitData @()
data LockArgs =
LockArgs
{ recipient1Wallet :: Wallet
, recipient2Wallet :: Wallet
, totalAda :: Ada
}
deriving stock (Show, Generic)
deriving anyclass (ToJSON, FromJSON, ToSchema)
type SplitSchema =
BlockchainActions
.\/ Endpoint "lock" LockArgs
.\/ Endpoint "unlock" LockArgs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment