Skip to content

Instantly share code, notes, and snippets.

@gclaramunt
Last active March 3, 2021 12:55
Show Gist options
  • Save gclaramunt/620a1938c2dec025be5223248e45cdda to your computer and use it in GitHub Desktop.
Save gclaramunt/620a1938c2dec025be5223248e45cdda to your computer and use it in GitHub Desktop.
Plutus Playground Smart Contract
import Control.Monad (void)
import qualified Data.ByteString.Char8 as C
import Language.Plutus.Contract
import qualified Language.PlutusTx as PlutusTx
import Language.PlutusTx.Prelude hiding (pure, (<$>))
import Ledger (Address, Validator, ValidatorCtx, Value, scriptAddress)
import qualified Ledger.Constraints as Constraints
import qualified Ledger.Typed.Scripts as Scripts
import Playground.Contract
import qualified Prelude
------------------------------------------------------------
type MathBountySchema =
BlockchainActions
.\/ Endpoint "bounty" BountyParams
.\/ Endpoint "solution" SolutionParams
data MathBounty
instance Scripts.ScriptType MathBounty where
type instance RedeemerType MathBounty = Integer
type instance DatumType MathBounty = Integer
bountyInstance :: Scripts.ScriptInstance MathBounty
bountyInstance = Scripts.validator @MathBounty
$$(PlutusTx.compile [|| validateSolution ||])
$$(PlutusTx.compile [|| wrap ||]) where
wrap = Scripts.wrapValidator @Integer @Integer
-- validate that the square of the proposed value is the expected solution
validateSolution :: Integer -> Integer -> ValidatorCtx -> Bool
validateSolution y x _ = x*x == y
bountyValidator :: Validator
bountyValidator = Scripts.validatorScript bountyInstance
-- | The address of the bounty (the hash of its validator script)
bountyAddress :: Address
bountyAddress = Ledger.scriptAddress bountyValidator
-- | Parameters for the "bounty" endpoint
data BountyParams = BountyParams
{ target :: Integer
, amount :: Value
}
deriving stock (Prelude.Eq, Prelude.Show, Generic)
deriving anyclass (FromJSON, ToJSON, IotsType, ToSchema, ToArgument)
-- | Parameters for the "solution" endpoint
newtype SolutionParams = SolutionParams
{ proposed_solution :: Integer
}
deriving stock (Prelude.Eq, Prelude.Show, Generic)
deriving anyclass (FromJSON, ToJSON, IotsType, ToSchema, ToArgument)
-- | The "bounty" contract endpoint.
bounty :: AsContractError e => Contract MathBountySchema e ()
bounty = do
BountyParams target amt <- endpoint @"bounty" @BountyParams
let tx = Constraints.mustPayToTheScript target amt
void (submitTxConstraints bountyInstance tx)
-- | The "solution" contract endpoint.
solution :: AsContractError e => Contract MathBountySchema e ()
solution = do
SolutionParams theProposal <- endpoint @"solution" @SolutionParams
unspentOutputs <- utxoAt bountyAddress
let tx = collectFromScript unspentOutputs theProposal
void (submitTxConstraintsSpending bountyInstance unspentOutputs tx)
mathBounty :: AsContractError e => Contract MathBountySchema e ()
mathBounty = bounty `select` solution
endpoints :: AsContractError e => Contract MathBountySchema e ()
endpoints = mathBounty
mkSchemaDefinitions ''MathBountySchema
$(mkKnownCurrencies [])
[0,[{"simulationWallets":[{"simulatorWalletWallet":{"getWallet":1},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},100]]]]}},{"simulatorWalletWallet":{"getWallet":2},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},10]]]]}},{"simulatorWalletWallet":{"getWallet":3},"simulatorWalletBalance":{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},10]]]]}}],"simulationName":"Simulation 1","simulationId":1,"simulationActions":[{"caller":{"getWallet":1},"argumentValues":{"endpointDescription":{"getEndpointDescription":"bounty"},"argument":{"contents":[["target",{"s":1,"e":0,"c":[4],"tag":"FormIntegerF"}],["amount",{"getValue":[[{"unCurrencySymbol":""},[[{"unTokenName":""},10]]]],"tag":"FormValueF"}]],"tag":"FormObjectF"}},"tag":"CallEndpoint"},{"blocks":1,"tag":"AddBlocks"},{"caller":{"getWallet":2},"argumentValues":{"endpointDescription":{"getEndpointDescription":"solution"},"argument":{"contents":[["proposed_solution",{"s":1,"e":0,"c":[3],"tag":"FormIntegerF"}]],"tag":"FormObjectF"}},"tag":"CallEndpoint"},{"blocks":1,"tag":"AddBlocks"},{"caller":{"getWallet":3},"argumentValues":{"endpointDescription":{"getEndpointDescription":"solution"},"argument":{"contents":[["proposed_solution",{"s":1,"e":0,"c":[2],"tag":"FormIntegerF"}]],"tag":"FormObjectF"}},"tag":"CallEndpoint"},{"blocks":1,"tag":"AddBlocks"}]}]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment