Skip to content

Instantly share code, notes, and snippets.

@diaspogift
Last active December 16, 2020 21:01
Show Gist options
  • Save diaspogift/37a7965c591c65ba4461219271d0308b to your computer and use it in GitHub Desktop.
Save diaspogift/37a7965c591c65ba4461219271d0308b to your computer and use it in GitHub Desktop.
Plutus Playground Smart Contract
-- This is a starter contract, based on the Game contract,
-- containing the bare minimum required scaffolding.
--
-- What you should change to something more suitable for
-- your use case:
-- * The DataScript type
-- * The Redeemer type
--
-- And add function implementations (and rename them to
-- something suitable) for the endpoints:
-- * publish
-- * redeem
import qualified Language.PlutusTx as PlutusTx
import Language.PlutusTx.Prelude
import Ledger (Address, DataScript (DataScript), PendingTx, PubKey,
RedeemerScript (RedeemerScript), TxId, ValidatorScript (ValidatorScript),
applyScript, compileScript, hashTx, lifted, pendingTxValidRange,
scriptAddress, valueSpent)
import qualified Ledger.Interval as Interval
import Ledger.Slot (Slot, SlotRange)
import qualified Ledger.Validation as V
import Ledger.Value (Value)
import qualified Ledger.Value as Value
import Playground.Contract
import Wallet (EventHandler (EventHandler), EventTrigger, MonadWallet, andT,
collectFromScript, collectFromScriptTxn, fundsAtAddressGeqT, logMsg,
ownPubKey, payToScript, register, slotRangeT)
import qualified Wallet as W
import Wallet.Emulator (Wallet)
import qualified Wallet.Emulator as EM
type Quorum = Integer
type TransferId = Integer
data SentStatus = Sent | NotSent deriving (Generic, ToJSON, FromJSON, ToSchema)
PlutusTx.makeLift ''SentStatus
data Transfer = Transfer {
transferId :: TransferId
, amount :: Value
, payTo :: PubKey
, approvalsCount :: Integer
, sent :: SentStatus
} deriving (Generic, ToJSON, FromJSON, ToSchema)
PlutusTx.makeLift ''Transfer
data MultiSigWallet = MultiSigWallet {
approvers :: [PubKey]
, transfers :: [Transfer]
, quorum :: Quorum
} deriving (Generic, ToJSON, FromJSON, ToSchema)
PlutusTx.makeLift ''MultiSigWallet
data MSWalletAction = Request | Fund
deriving (Generic, ToJSON, FromJSON, ToSchema)
PlutusTx.makeLift ''MSWalletAction
type MultiSigWalletValidator = PubKey -> MultiSigWallet -> PendingTx -> Bool
mswValidatoScript :: MultiSigWallet -> ValidatorScript
mswValidatoScript msw = ValidatorScript $ Ledger.applyScript validator (Ledger.lifted msw)
where
validator =
$$(Ledger.compileScript [||
\(msw :: MultiSigWallet) (dataScript :: ()) (redeemerScript :: () )(_ :: PendingTx) -> ()
||])
mkMultiSigWallet :: [PubKey] -> Quorum -> Value -> Wallet -> MultiSigWallet
mkMultiSigWallet apprs qrm seedFund ownerWallet =
MultiSigWallet {
approvers = EM.walletPubKey ownerWallet : apprs
, transfers = []
, quorum = qrm
}
helloAction :: MonadWallet m => m ()
helloAction = W.logMsg "Hello world!"
contractAddress :: MultiSigWallet -> Address
contractAddress msw = Ledger.scriptAddress $ mswValidatoScript msw
contribute :: MonadWallet m => MultiSigWallet -> Value -> m ()
contribute msw value = do
ownPK <- ownPubKey
let dataScript = DataScript $ Ledger.lifted ownPK
range = W.defaultSlotRange
tx <- payToScript range (contractAddress msw) value dataScript
logMsg "Contribution submited"
--register refundTrigger $ refundHandler msw (Ledger.hashTx tx)
collect :: MonadWallet m => MultiSigWallet -> m ()
collect msw = do
logMsg "collecting contributions ..."
let redeemerScript = RedeemerScript $ Ledger.lifted ()
range = W.interval 10 15
collectFromScript range (mswValidatoScript msw) redeemerScript
watchContract :: MonadWallet m => MultiSigWallet -> m ()
watchContract msw = W.startWatching $ contractAddress msw
$(mkFunctions ['helloAction, 'contribute,'collect, 'watchContract])
[0,[{"wallets":[{"simulatorWalletWallet":{"getWallet":1},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},10]]]]}},{"simulatorWalletWallet":{"getWallet":2},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},10]]]]}}],"signatures":[{"functionName":"helloAction","argumentSchema":[]},{"functionName":"contribute","argumentSchema":[{"contents":[["approvers",{"contents":[["getPubKey",{"tag":"FormSchemaString"}]],"tag":"FormSchemaArray"}],["transfers",{"contents":[["transferId",{"tag":"FormSchemaInt"}],["amount",{"tag":"FormSchemaValue"}],["payTo",{"contents":[["getPubKey",{"tag":"FormSchemaString"}]],"tag":"FormSchemaObject"}],["approvalsCount",{"tag":"FormSchemaInt"}],["sent",{"contents":["Sent","NotSent"],"tag":"FormSchemaRadio"}]],"tag":"FormSchemaArray"}],["quorum",{"tag":"FormSchemaInt"}]],"tag":"FormSchemaObject"},{"tag":"FormSchemaValue"}]},{"functionName":"collect","argumentSchema":[{"contents":[["approvers",{"contents":[["getPubKey",{"tag":"FormSchemaString"}]],"tag":"FormSchemaArray"}],["transfers",{"contents":[["transferId",{"tag":"FormSchemaInt"}],["amount",{"tag":"FormSchemaValue"}],["payTo",{"contents":[["getPubKey",{"tag":"FormSchemaString"}]],"tag":"FormSchemaObject"}],["approvalsCount",{"tag":"FormSchemaInt"}],["sent",{"contents":["Sent","NotSent"],"tag":"FormSchemaRadio"}]],"tag":"FormSchemaArray"}],["quorum",{"tag":"FormSchemaInt"}]],"tag":"FormSchemaObject"}]},{"functionName":"watchContract","argumentSchema":[{"contents":[["approvers",{"contents":[["getPubKey",{"tag":"FormSchemaString"}]],"tag":"FormSchemaArray"}],["transfers",{"contents":[["transferId",{"tag":"FormSchemaInt"}],["amount",{"tag":"FormSchemaValue"}],["payTo",{"contents":[["getPubKey",{"tag":"FormSchemaString"}]],"tag":"FormSchemaObject"}],["approvalsCount",{"tag":"FormSchemaInt"}],["sent",{"contents":["Sent","NotSent"],"tag":"FormSchemaRadio"}]],"tag":"FormSchemaArray"}],["quorum",{"tag":"FormSchemaInt"}]],"tag":"FormSchemaObject"}]},{"functionName":"payToWallet_","argumentSchema":[{"tag":"FormSchemaValue"},{"contents":[["getWallet",{"tag":"FormSchemaInt"}]],"tag":"FormSchemaObject"}]}],"currencies":[{"knownTokens":[{"unTokenName":""}],"hash":"","friendlyName":"Ada"}],"actions":[{"simulatorWallet":{"simulatorWalletWallet":{"getWallet":1},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},10]]]]}},"functionSchema":{"functionName":"watchContract","argumentSchema":[{"contents":[["approvers",{"contents":[{"contents":[["getPubKey",{"tag":"FormSchemaString"}]],"tag":"FormSchemaObject"},[{"contents":[["getPubKey",{"contents":"3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c","tag":"FormString"}]],"tag":"FormObject"},{"contents":[["getPubKey",{"contents":"fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025","tag":"FormString"}]],"tag":"FormObject"}]],"tag":"FormArray"}],["transfers",{"contents":[{"contents":[["transferId",{"tag":"FormSchemaInt"}],["amount",{"tag":"FormSchemaValue"}],["payTo",{"contents":[["getPubKey",{"tag":"FormSchemaString"}]],"tag":"FormSchemaObject"}],["approvalsCount",{"tag":"FormSchemaInt"}],["sent",{"contents":["Sent","NotSent"],"tag":"FormSchemaRadio"}]],"tag":"FormSchemaObject"},[{"contents":[["transferId",{"contents":1,"tag":"FormInt"}],["amount",{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},2]]]],"tag":"FormValue"}],["payTo",{"contents":[["getPubKey",{"contents":"fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025","tag":"FormString"}]],"tag":"FormObject"}],["approvalsCount",{"contents":0,"tag":"FormInt"}],["sent",{"contents":[["Sent","NotSent"],"NotSent"],"tag":"FormRadio"}]],"tag":"FormObject"}]],"tag":"FormArray"}],["quorum",{"contents":2,"tag":"FormInt"}]],"tag":"FormObject"}]},"tag":"Action"},{"simulatorWallet":{"simulatorWalletWallet":{"getWallet":2},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},10]]]]}},"functionSchema":{"functionName":"contribute","argumentSchema":[{"contents":[["approvers",{"contents":[{"contents":[["getPubKey",{"tag":"FormSchemaString"}]],"tag":"FormSchemaObject"},[{"contents":[["getPubKey",{"contents":"3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c","tag":"FormString"}]],"tag":"FormObject"},{"contents":[["getPubKey",{"contents":"fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025","tag":"FormString"}]],"tag":"FormObject"}]],"tag":"FormArray"}],["transfers",{"contents":[{"contents":[["transferId",{"tag":"FormSchemaInt"}],["amount",{"tag":"FormSchemaValue"}],["payTo",{"contents":[["getPubKey",{"tag":"FormSchemaString"}]],"tag":"FormSchemaObject"}],["approvalsCount",{"tag":"FormSchemaInt"}],["sent",{"contents":["Sent","NotSent"],"tag":"FormSchemaRadio"}]],"tag":"FormSchemaObject"},[{"contents":[["transferId",{"contents":1,"tag":"FormInt"}],["amount",{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},2]]]],"tag":"FormValue"}],["payTo",{"contents":[["getPubKey",{"contents":"fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025","tag":"FormString"}]],"tag":"FormObject"}],["approvalsCount",{"contents":0,"tag":"FormInt"}],["sent",{"contents":[["Sent","NotSent"],"NotSent"],"tag":"FormRadio"}]],"tag":"FormObject"}]],"tag":"FormArray"}],["quorum",{"contents":2,"tag":"FormInt"}]],"tag":"FormObject"},{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},5]]]],"tag":"FormValue"}]},"tag":"Action"},{"simulatorWallet":{"simulatorWalletWallet":{"getWallet":2},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},10]]]]}},"functionSchema":{"functionName":"collect","argumentSchema":[{"contents":[["approvers",{"contents":[{"contents":[["getPubKey",{"tag":"FormSchemaString"}]],"tag":"FormSchemaObject"},[{"contents":[["getPubKey",{"contents":"3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c","tag":"FormString"}]],"tag":"FormObject"},{"contents":[["getPubKey",{"contents":"fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025","tag":"FormString"}]],"tag":"FormObject"}]],"tag":"FormArray"}],["transfers",{"contents":[{"contents":[["transferId",{"tag":"FormSchemaInt"}],["amount",{"tag":"FormSchemaValue"}],["payTo",{"contents":[["getPubKey",{"tag":"FormSchemaString"}]],"tag":"FormSchemaObject"}],["approvalsCount",{"tag":"FormSchemaInt"}],["sent",{"contents":["Sent","NotSent"],"tag":"FormSchemaRadio"}]],"tag":"FormSchemaObject"},[{"contents":[["transferId",{"contents":1,"tag":"FormInt"}],["amount",{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},2]]]],"tag":"FormValue"}],["payTo",{"contents":[["getPubKey",{"contents":"fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025","tag":"FormString"}]],"tag":"FormObject"}],["approvalsCount",{"contents":0,"tag":"FormInt"}],["sent",{"contents":[["Sent","NotSent"],"NotSent"],"tag":"FormRadio"}]],"tag":"FormObject"}]],"tag":"FormArray"}],["quorum",{"contents":2,"tag":"FormInt"}]],"tag":"FormObject"}]},"tag":"Action"}]}]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment