📅  最后修改于: 2023-12-03 14:40:01.466000             🧑  作者: Mango
Cardano is a decentralized public blockchain and cryptocurrency project. It was created by the co-founder of Ethereum, Charles Hoskinson. The project aims to provide a more secure and sustainable infrastructure for cryptocurrencies and decentralized applications.
Proof-of-Stake Consensus: Cardano uses a proof-of-stake consensus mechanism called Ouroboros, which allows for more energy-efficient and secure transactions compared to proof-of-work.
ADA Cryptocurrency: The native cryptocurrency of Cardano is called ADA and is used for transactions and payments on the platform.
Smart Contracts: Cardano has a smart contract platform called Plutus, which allows for the development and execution of decentralized applications.
Interoperability: Cardano aims to provide interoperability between different blockchains and cryptocurrencies, allowing for greater flexibility and ease of use.
Cardano Documentation: The official documentation for Cardano developers, including guides, tutorials, and API reference.
Cardano GitHub: The official GitHub repository for Cardano, containing the source code for the various components of the platform.
Cardano Forum: The official forum for the Cardano community, including discussions around development, governance, and news.
To get started with development on Cardano, you'll need to set up a development environment and download the required tools and libraries.
First, you'll need to install the Cardano Node, which is the software that allows you to participate in the Cardano network and interact with the blockchain.
Next, you'll need to set up a wallet to hold and manage your ADA cryptocurrency.
Finally, you can start developing smart contracts using the Plutus programming language and the Cardano Developer Portal.
Here's a code snippet from the Cardano Developer Portal to get you started:
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MonoLocalBinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Week01.Vesting where
import Plutus.Contract
import PlutusTx.Prelude hiding (Semigroup(..), unless)
import Ledger.TimeSlot
import qualified Ledger.Typed.Scripts as Scripts
import Ledger.Contexts
import Schema
-- Allow us to log strings with `logInfo` during contract execution.
-- Seems like a hack, but is suggested in the official example code.
import Data.Text (Text)
import qualified Data.Text as Text
instance Show (ContractError s) where
show = Text.unpack . displayError
newtype VestingDatum = VestingDatum
{ beneficiary :: PubKeyHash
}
deriving stock (Show)
-- This is the script that locks the funds until the given `vestingDeadline`.
-- The owner can transfer funds to the script with the `Give` endpoint.
vesting :: VestingDatum -> POSIXTime -> Scripts.ScriptContext -> Bool
vesting dat deadline ctx =
traceIfFalse "Vesting period has not ended yet" (to (deadline `contains` txInfoValidRange (scriptContextTxInfo ctx))) &&
traceIfFalse "Beneficiary signature missing" (checkSig (beneficiary dat) (scriptContextTxInfo ctx))
mkValidator :: VestingDatum -> POSIXTime -> ValidatorCtx -> Bool
mkValidator dat deadline ctx =
vesting dat deadline (valCtxScriptContext ctx)
data Vesting
instance Scripts.ScriptType Vesting where
type instance DatumType Vesting = VestingDatum
type instance RedeemerType Vesting = POSIXTime
inst :: Scripts.ScriptInstance Vesting
inst = Scripts.validator @Vesting
$$(PlutusTx.compile [|| mkValidator ||])
$$(PlutusTx.compile [|| wrap ||])
where
wrap = Scripts.wrapValidator @VestingDatum @POSIXTime
validator :: VestingDatum -> Validator
validator = Scripts.validatorScript . inst
valHash :: VestingDatum -> Ledger.ValidatorHash
valHash = Scripts.validatorHash . validator
data GiveParams = GiveParams
{ gpBeneficiary :: !PubKeyHash
, gpDeadline :: !POSIXTime
, gpAmount :: !Integer
}
deriving (Generic, ToSchema)
-- | Endpoint for transferring funds to the script, with the given `deadline`.
give :: AsContractError e => GiveParams -> Contract w s e ()
give gp = do
pkh <- pubKeyHash <$> Contract.ownPubKey
let dat = VestingDatum
{ beneficiary = gpBeneficiary gp
}
tx = mustPayToTheScript dat $ Ada.lovelaceValueOf (gpAmount gp)
<> Constraints.mustValidateIn (to $ gpDeadline gp)
ledgerTx <- submitTxConstraints inst tx
-- Wait until the transaction is confirmed.
void $ awaitTxConfirmed $ txId ledgerTx
logInfo @String $ "received " ++ show (gpAmount gp) ++ " lovelace from " ++ show pkh
endpoints :: Contract () VestingSchema Text ()
endpoints = forever
$ handleError logError
$ awaitPromise
$ endpoint @"give" give
This code defines a vesting script that locks funds until a specified deadline
. It also defines an endpoint for transferring funds to the script and logs the transaction on the blockchain using logInfo
.