Last active
March 29, 2021 01:27
-
-
Save GabrielNicolasAvellaneda/a3e7267615c7d5d86c0f47708993d560 to your computer and use it in GitHub Desktop.
Plutus Playground Smart Contract
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
-- Get the PubKeyHash type | |
import Control.Monad (void) | |
import Ledger | |
import Data.Aeson (FromJSON, ToJSON) | |
import qualified Language.PlutusTx as PlutusTx | |
import qualified Ledger.Ada as Ada | |
import qualified Ledger.Typed.Scripts as Scripts | |
import Wallet.Emulator.Wallet | |
import Playground.Contract | |
import qualified Data.Text as T | |
import qualified Ledger.Constraints as Constraints | |
import Language.PlutusTx.Prelude | |
import Language.Plutus.Contract | |
-- Defining the data type | |
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) | |
-- IsData is used for values that are attached to transactions. | |
PlutusTx.makeIsData ''SplitData | |
PlutusTx.makeLift ''SplitData | |
-- Validator script that runs on the blockchain | |
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) | |
-- Compile validator script | |
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 | |
lock :: Contract SplitSchema T.Text LockArgs | |
lock = endpoint @"lock" | |
unlock :: Contract SplitSchema T.Text LockArgs | |
unlock = endpoint @"unlock" | |
mkSplitData :: LockArgs -> SplitData | |
mkSplitData LockArgs{recipient1Wallet, recipient2Wallet, totalAda} = | |
let convert :: Wallet -> PubKeyHash | |
convert = pubKeyHash . walletPubKey | |
in | |
SplitData | |
{ recipient1 = convert recipient1Wallet | |
, recipient2 = convert recipient2Wallet | |
, amount = totalAda | |
} | |
lockFunds :: SplitData -> Contract SplitSchema T.Text () | |
lockFunds s@SplitData{amount} = do | |
logInfo $ "Locking " <> show amount | |
let tx = Constraints.mustPayToTheScript s (Ada.toValue amount) | |
void $ submitTxConstraints splitInstance tx | |
unlockFunds :: SplitData -> Contract SplitSchema T.Text () | |
unlockFunds SplitData{recipient1, recipient2, amount} = do | |
let contractAddress = (Ledger.scriptAddress (Scripts.validatorScript splitInstance)) | |
utxos <- utxoAt contractAddress | |
let half = Ada.divide amount 2 | |
tx = | |
collectFromScript utxos () | |
<> Constraints.mustPayToPubKey recipient1 (Ada.toValue half) | |
<> Constraints.mustPayToPubKey recipient2 (Ada.toValue $ amount - half) | |
void $ submitTxConstraintsSpending splitInstance utxos tx | |
endpoints :: Contract SplitSchema T.Text () | |
endpoints = (lock >>= lockFunds . mkSplitData) `select` (unlock >>= unlockFunds . mkSplitData) | |
mkSchemaDefinitions ''SplitSchema | |
$(mkKnownCurrencies []) | |
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
[0,[{"simulationWallets":[{"simulatorWalletWallet":{"getWallet":1},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},10]]]]}},{"simulatorWalletWallet":{"getWallet":2},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},10]]]]}}],"simulationName":"Simulation 1","simulationId":1,"simulationActions":[{"caller":{"getWallet":1},"argumentValues":{"endpointDescription":{"getEndpointDescription":"unlock"},"argument":{"contents":[["recipient1Wallet",{"contents":[["getWallet",{"s":1,"e":0,"c":[1],"tag":"FormIntegerF"}]],"tag":"FormObjectF"}],["recipient2Wallet",{"contents":[["getWallet",{"s":1,"e":0,"c":[2],"tag":"FormIntegerF"}]],"tag":"FormObjectF"}],["totalAda",{"contents":[["getLovelace",{"s":1,"e":0,"c":[5],"tag":"FormIntegerF"}]],"tag":"FormObjectF"}]],"tag":"FormObjectF"}},"tag":"CallEndpoint"},{"blocks":10,"tag":"AddBlocks"}]}]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment