Categories
Crypto

Cardano with examples

Cardano is a blockchain platform that uses a proof-of-stake consensus algorithm called Ouroboros. It is built using the Haskell programming language and is designed to be a more secure, flexible, and sustainable platform for the development and execution of smart contracts and decentralized applications (dapps).

One key feature of Cardano is its multi-layer architecture, which separates the settlement layer (where transactions are recorded) from the computation layer (where smart contracts are executed). This allows for greater flexibility and scalability, as changes to the computation layer can be made without affecting the settlement layer.

Another important aspect of Cardano is its use of formal verification, a method of mathematically proving the correctness of smart contract code. This helps to ensure the security and reliability of dapps built on the platform.

One example of a dapp built on Cardano is Plutus, a smart contract programming language that is specifically designed for the platform. Plutus allows for the creation of highly secure and reliable smart contracts, and also enables developers to write code that is more easily verifiable and less prone to errors.

Example: auction contract

Here is a practical smart contract in Plutus:

module Main where

import Language.PlutusTx
import Language.PlutusTx.Prelude
import Language.PlutusTx.Builtins

data Auction = Auction {
    itemName :: String,
    itemDescription :: String,
    startPrice :: Integer,
    highestBid :: Integer,
    highestBidder :: Address
}

placeBid : Auction -> Integer -> (Boolean, Auction)
placeBid auction bid =
    if bid > highestBid auction then
        (True, auction { highestBid = bid, highestBidder = txSender })
    else
        (False, auction)

endAuction : Auction -> (Boolean, String)
endAuction auction =
    if txSender == highestBidder auction then
        (True, "Auction sold to " ++ show (highestBidder auction) ++ " for " ++ show (highestBid auction))
    else
        (False, "Auction not ended")

main : () -> ()
main _ =
    let
        auction = Auction {
            itemName = "My Item",
            itemDescription = "This is a very nice item",
            startPrice = 100,
            highestBid = 0,
            highestBidder = Address "0x0"
        }
    in
    trace auction
    trace $ placeBid auction 150

This smart contract example is an auction contract, it defines a custom data type called “Auction” which has five fields, itemName, itemDescription, startPrice, highestBid, and highestBidder. It also defines two functions to interact with the auction, placeBid, endAuction.

The placeBid function allows a user to place a bid on an auction. It checks that the bid is greater than the current highest bid and if so, it updates the highest bid and highest bidder. It returns a tuple of Boolean and the updated auction. The boolean value is true if the bid is successfully placed, otherwise it is false.

The endAuction function allows the highest bidder to end the auction and it checks that the sender of the transaction is the highest bidder. If so, it returns a tuple of Boolean and a message indicating that the auction is sold to the highest bidder for the highest bid amount. Otherwise, it returns a tuple of Boolean and a message indicating that the auction is not ended.

The main function is the entry point of the contract, it creates a new auction, traces it, and traces the result of the placeBid function.

The source code is here https://github.com/michaldrozd/cardano-smart-contracts/blob/main/AuctionContract.plutus

Example: crowdfunding

Another example of a practical smart contract that implements a simple crowdfunding campaign written in Plutus for Cardano:

module Main where

import Language.PlutusTx
import Language.PlutusTx.Prelude
import Language.PlutusTx.Builtins

data Campaign = Campaign {
    campaignName :: String,
    campaignDescription :: String,
    targetFunding :: Integer,
    currentFunding :: Integer,
    deadline :: Integer
}

contribute : Campaign -> Integer -> (Boolean, Campaign)
contribute campaign amount =
    if (txTimestamp <= deadline campaign) && (currentFunding campaign + amount <= targetFunding campaign) then
        (True, campaign { currentFunding = currentFunding campaign + amount })
    else
        (False, campaign)

refund : Campaign -> Address -> (Boolean, Integer)
refund campaign contributor =
    if (txSender == contributor) && (currentFunding campaign < targetFunding campaign) then
        (True, currentFunding campaign)
    else
        (False, 0)

main : () -> ()
main _ =
    let
        campaign = Campaign {
            campaignName = "My Campaign",
            campaignDescription = "This is a very nice campaign",
            targetFunding = 1000,
            currentFunding = 0,
            deadline = 10000000000
        }
    in
    trace campaign
    trace $ contribute campaign 500

This smart contract defines a custom data type called “Campaign” which has five fields, campaignName, campaignDescription, targetFunding, currentFunding, and deadline. It also defines two functions to interact with the campaign, contribute, refund.

The contribute function allows a user to contribute to a campaign. It checks that the transaction timestamp is less than or equal to the campaign deadline and that the total funding after the contribution is less than or equal to the target funding. If the checks pass, it updates the current funding and returns a tuple of Boolean and the updated campaign. The boolean value is true if the contribution is successfully placed, otherwise it is false.

The refund function allows a contributor to get a refund if the campaign is not fully funded. It checks that the sender of the transaction is the contributor and that the current funding is less than the target funding. If the checks pass, it returns a tuple of Boolean and the current funding. Otherwise, it returns a tuple of Boolean and 0.

The main function is the entry point of the contract, it creates a new campaign, traces it, and traces the result of the contribute function.

The source code is here https://github.com/michaldrozd/cardano-smart-contracts/blob/main/CrowdfundingCampaign.plutus

Example: Escrow

module Main where

import Language.PlutusTx
import Language.PlutusTx.Prelude
import Language.PlutusTx.Builtins

data Escrow = Escrow {
    buyer :: Address,
    seller :: Address,
    arbitrator :: Address,
    item :: String,
    amount :: Integer,
    status :: String
}

create : Address -> Address -> Address -> String -> Integer -> Escrow
create buyer seller arbitrator item amount =
    Escrow {
        buyer = buyer,
        seller = seller,
        arbitrator = arbitrator,
        item = item,
        amount = amount,
        status = "created"
    }

release : Escrow -> (Boolean, Escrow)
release escrow =
    if (txSender == buyer escrow) && (status escrow == "created") then
        (True, escrow { status = "released" })
    else
        (False, escrow)

refund : Escrow -> (Boolean, Escrow)
refund escrow =
    if (txSender == seller escrow) && (status escrow == "created") then
        (True, escrow { status = "refunded" })
    else
        (False, escrow)

arbitrate : Escrow -> (Boolean, Escrow)
arbitrate escrow =
    if (txSender == arbitrator escrow) && (status escrow == "created") then
        (True, escrow { status = "arbitrated" })
    else
        (False, escrow)

main : () -> ()
main _ =
    let
        escrow = create (Address "0x1") (Address "0x2") (Address "0x3") "My Item" 500
    in
    trace escrow
    trace $ release escrow

This smart contract example is an escrow service, it defines a custom data type called “Escrow” which has six fields, buyer, seller, arbitrator, item, amount and status. It also defines three functions to interact with the escrow create, release, refund and arbitrate.

The create function allows a user to create a new escrow transaction, it takes the buyer, seller and arbitrator addresses, an item name and the amount of the transaction, it creates an escrow object with the given data.

The release function allows the buyer to release the funds to the seller after they have received the item. It checks that the sender of the transaction is the buyer and that the status of the escrow is “created”, if so it updates the status of the escrow to “released” and returns a tuple of Boolean and the updated escrow. The boolean value is true if the release is successful, otherwise it is false.

The refund function allows the seller to refund the funds to the buyer if they have not received the item. It checks that the sender of the transaction is the seller and that the status of the escrow is “created”, if so it updates the status of the escrow to “refunded” and returns a tuple of Boolean and the updated escrow. The boolean value is true if the refund is successful, otherwise it is false.

The arbitrate function allows the arbitrator to resolve the escrow if there is a dispute between the buyer and the seller. It checks that the sender of the transaction is the arbitrator and that the status of the escrow is “created”, if so it updates the status of the escrow to “arbitrated” and returns a tuple of Boolean and the updated escrow. The boolean value is true if the arbitration is successful, otherwise it is false.

The main function is the entry point of the contract, it creates a new escrow, traces it, and traces the result of the release function.

Here is full source code: https://github.com/michaldrozd/cardano-smart-contracts/blob/main/EscrowService.plutus

Other real-world examples

Another example of a dapp built on cardano is Atala PRISM, which is a decentralized identity solution that allows individuals and organizations to have full control over their own digital identities.

A practical use case for Cardano is in the field of voting systems. A company called Agora is building a decentralized voting platform on Cardano that uses blockchain technology to ensure the security and transparency of the voting process. The platform allows for remote voting and eliminates the need for paper ballots, making the voting process more efficient and accessible.

Another example is in the field of charity and donation, a company called Giveth is building a platform that allows individuals to donate to charity organizations in a transparent and efficient way, by using smart contract to make sure the donation reach its intended target.

In summary, Cardano is a blockchain platform that uses a proof-of-stake consensus algorithm, is built using the Haskell programming language and designed to be more secure, flexible, and sustainable platform for the development and execution of smart contracts and decentralized applications (dapps). Its multi-layer architecture separates the settlement layer from the computation layer and uses formal verification as a method of mathematically proving the correctness of smart contract code.

Leave a Reply

Your email address will not be published. Required fields are marked *