• Stars
    star
    179
  • Rank 213,983 (Top 5 %)
  • Language
    Haskell
  • License
    Apache License 2.0
  • Created about 3 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple Plutus contract to help give Charles' stuffed lobster a name

Lobster Challenge

Charles and his lobster

Charles has a lobster, but the poor creature does not yet have a name. Let's use a simple Plutus Smart Contract on Cardano to help Charles find a name for his lobster!

We start with a list of names and a "secret random" number, which we will only reveal in the end. Then we need the Community's help! We need 500 Community members who are willing to help name the lobster by creating transactions which will add their own "random" number (from 1 to 100) to the current total.

In the end, we will reveal our own "secret random" number, add it to the total provided by the Community, and use the result (after taking the remainder after division by the number of available names) as an index into the list of names to pick the lobster name.

Native Tokens

We use three distinct native tokens to help us name the lobster:

Policy Policy Id Token Name Purpose
script cc7888851f0f5aa64c136e0c8fb251e9702f3f6c9efcf3a60a54f419 LobsterNFT Identifies the relevant UTxO.
script fda1b6b487bee2e7f64ecf24d24b1224342484c0195ee1b7b943db50 LobsterCounter Stores the current "random" number.
script fda1b6b487bee2e7f64ecf24d24b1224342484c0195ee1b7b943db50 LobsterVotes Counts the number of votes.

The first one, LobsterNFT, is an NFT and used to identified the "correct" UTxO "sitting" at the contract address. (Recall that anybody can send anything anywhere at anytime, so we can't prevent people from creating other UTxO's at the script address. The UTxO we care about is the one holding the NFT.)

The second one, LobsterCounter, stores the current "random" number. It starts at zero when the contract is deployed, and each "vote" can add a number of tokens between 1 and 100. Once 500 people have "voted", we add our own secret number, which we picked before deploying the contract and which is a parameter to the contract and hence encoded in the script address. Finally we take the rest of dividing the sum by the number of names in the list of possible names. This will be the final amount of LobsterCounter tokens "sitting" at the script address, and it will indicate the index of the chosen lobster name.

The third token, LobsterVotes, is used to count the number of "votes". Each time a user interacts with the contract and increases the amount of LobsterCounter tokens by a "random" number, The amount of LobsterVotes tokens at the script UTxO will go up by one. Once 500 people have "voted", voting stops, and we will set the final result with a last transaction, which will bring the amount of LobsterVotes tokens to its ultimate value of 501.

Script

The validator for the naming contract is paramterized by six parameters:

Parameter Value Explanation ]
Seed TO BE REVEALED Our own "random" number.
NFT LobsterNFTtoken (see above) Token identifying the UTxO.
Counter LobsterCounter token (see above) Token tracking "random" number.
Votes LobsterVotes token (see above) Token counting votes.
Name Count 1219 Number of available names.
Vote Count 500 Number of votes.

We will reveal the "seed", our own "random" contribution, in the end, when we finalize the contract.

Using these parameter values, we get a script with script address addr1w8433zk2shufk42hn4x7zznjjuqwwyfmxffcjszw5l2ulesdt3jff.

Example

Let's look at an example and assume our own "secret random" number is 42.

Voting might progress as follows:

LobsterCounter Lobster Votes Comment
0 0 In the beginning, there have been no votes, and the counter starts at zero.
13 1 The first Community member votes and adds 13 to the counter.
113 2 The second Community member adds 100.
114 3 The third Community member adds 1.
... ... More Community members add their own numbers.
24782 500 The last vote has been cast, which brought the counter to 24782.
444 501 We add our own secret, 42, and get 24824. Dividing this by 1219, the number of names, gives a remainder of 444.

In this example, the lobster will get the name with index 444, which happens to be Stewart.

Voting

We will kick off the process by minting the LobsterNFT NFT and creating a UTxO at the script address, which contains the NFT, but neither LobsterCounter nor LobsterVotes tokens yet, because the counter starts at zero, and there have been no votes.

To vote, you have to create a transaction that spends the UTxO at the script address holding the NFT and creates a new UTxO at the same address, which holds the same NFT, one more LobsterVotes tokens than before and a number between 1 and 100 more LobsterCounter tokens than before.

This means that this transaction will have to mint LobsterVotes and LobsterCounter tokens, but that's fine: The minting policy of those tokens allows arbitrary minting.

The Plutus script defining the script address will make sure that all constraints are satisfied:

  • The NFT has to be present in both the spent UTxO and the newly created one.
  • The amount of LobsterVotes tokens must be smaller than 500 in the beginning, and it must increase by one.
  • The amount of LobsterCounter tokens must go up by a number between 1 and 100.

You can use this Bash script to create, sign and submit a suitable transaction. This assumes that you have a node running, the command line interface cardano-cli available and that your node socket is called node.socket. The script expects seven arguments:

  • A UTxO with some funds to provide collateral and pay for transaction fees.
  • The current script UTxO.
  • The filename of a file containing your address - this is where the change will go.
  • The filename of the file containing your signing key.
  • The current amount of LobsterCounter tokens.
  • The new amount of LobsterCounter tokens (which must be 1-100 higher than the current amount).
  • The current amount of LobsterVotes tokens (which should be less than 500).

I also recorded an Explainer Video.

Finalization

Once 500 votes have been cast, we will submit a final transaction to get the ultimate result and record it as the final amount of LobsterCounter tokens. As stated above, we will add our own secret random number to the total and take the remainder after division by the number of names in our list. We can't "cheat" at this step, because the Plutus contract has been paramterized by our number, and the contract will enforce that we calculate the final value correctly.

Code

You can look at the Plutus code here, where you will find the minting policies for the used native tokens and the validator enforcing the contract logic.

You can build the code with cabal build.

Result

The 500 votes are in! The final counter after 500 votes was 22933. Adding our own "random" number, which was 582757474857012117487873, gives 582757474857012117510806. Taking the remainder after division by 1219, the number of potential names, gives 522. Looking into the list of names, we see that this index points to the name Logan.

Charles' lobster finally has his name!

More Repositories

1

cardano-sl

Cryptographic currency implementing Ouroboros PoS protocol
Haskell
3,757
star
2

cardano-node

The core component that is used to participate in a Cardano decentralised blockchain.
Haskell
2,934
star
3

plutus

The Plutus language implementation and tools
Haskell
1,472
star
4

plutus-pioneer-program

This repository hosts the lectures of the Plutus Pioneers Program. This program is a training course that the IOG Education Team provides to recruit and train software developers in Plutus, the native smart contract language for the Cardano ecosystem.
Haskell
1,389
star
5

daedalus

The open source cryptocurrency wallet for ada, built to grow with the community
TypeScript
1,230
star
6

essential-cardano

Repository for the Essential Cardano list
738
star
7

haskell.nix

Alternative Haskell Infrastructure for Nixpkgs
Nix
557
star
8

Scorex

Modular blockchain framework. Public domain
JavaScript
503
star
9

nami

Nami Wallet is a browser based wallet extension to interact with the Cardano blockchain. Support requests: https://iohk.zendesk.com/hc/en-us/requests/new
JavaScript
373
star
10

jormungandr

privacy voting blockchain node
Rust
366
star
11

plutus-apps

The Plutus application platform
Haskell
304
star
12

rust-byron-cardano

rust client libraries to deal with the current cardano mainnet (byron / cardano-sl)
Rust
276
star
13

hydra

Implementation of the Hydra Head protocol
Haskell
262
star
14

cardano-documentation

TypeScript
258
star
15

cardano-db-sync

A component that follows the Cardano chain and stores blocks and transactions in PostgreSQL
Haskell
254
star
16

ouroboros-network

An implementation of the Ouroboros family of consensus algorithms, with its networking support
Haskell
254
star
17

mantis

A Scala based client for Ethereum-like Blockchains.
Scala
227
star
18

cardano-ledger

The ledger implementation and specifications of the Cardano blockchain.
Haskell
223
star
19

cardano-js-sdk

JavaScript SDK for interacting with Cardano, providing various key management options, with support for popular hardware wallets
TypeScript
216
star
20

scrypto

Cryptographic primitives for Scala
Scala
201
star
21

plutus-starter

A starter project for Plutus apps
Nix
198
star
22

adrestia

APIs & SDK for interacting with Cardano.
Markdown
177
star
23

marlowe

Prototype implementation of domain-specific language for the design of smart-contracts over cryptocurrencies
Isabelle
174
star
24

bitte

Nix Ops for Terraform, Consul, Vault, Nomad
Nix
156
star
25

symphony-2

Immersive 3D Blockchain Explorer
JavaScript
127
star
26

mithril

Stake-based threshold multi-signatures protocol
Rust
126
star
27

cardano-addresses

Addresses and mnemonic manipulation & derivations
Haskell
125
star
28

iohk-ops

NixOps deployment configuration for IOHK devops
Nix
119
star
29

Alonzo-testnet

repository for the Alonzo testnet
Haskell
112
star
30

cardano-tutorials

ARCHIVED-This content in this repository is now located at https://docs.cardano.org/projects/cardano-node/
Makefile
112
star
31

stack2nix

Generate nix expressions for Haskell projects
Nix
99
star
32

iodb

Multiversioned key-value database, especially useful for blockchain
Scala
96
star
33

nix-tools

Translate Cabals Generic Package Description to a Nix expression
95
star
34

marlowe-cardano

Marlowe smart contract language Cardano implementation
Haskell
90
star
35

cardano-byron-cli

Cardano Command Line Interface (CLI) (Deprecated)
Rust
84
star
36

cardano-base

Code used throughout the Cardano eco-system
Haskell
83
star
37

react-polymorph

React components with highly customizable logic, markup and styles.
JavaScript
79
star
38

high-assurance-legacy

Legacy code connected to the high-assurance implementation of the Ouroboros protocol family
Haskell
78
star
39

shelley-testnet

Support triage for the Shelley testnet
70
star
40

cardano-crypto

This repository provides cryptographic libraries that are used in the Byron era of the Cardano node
C
67
star
41

plutus-use-cases

Plutus Use Cases
66
star
42

cardano-ops

NixOps deployment configuration for IOHK/Cardano devops
Nix
65
star
43

iohk-nix

nix scripts shared across projects
Nix
62
star
44

cardano-rt-view

RTView: real-time watching for Cardano nodes (ARCHIVED)
Haskell
59
star
45

spongix

Proxy for Nix Caching
Go
59
star
46

nix-hs-hello-windows

Cross compiling Hello World (haskell) to Windows using nix.
Nix
58
star
47

symphony

Symphony v1
JavaScript
53
star
48

cardano-ledger-byron

A re-implementation of the Cardano ledger layer, replacing the Byron release
Haskell
52
star
49

rscoin-haskell

Haskell implementation of RSCoin
Haskell
51
star
50

cardano-node-tests

System and end-to-end (E2E) tests for cardano-node.
Python
51
star
51

project-icarus

Icarus, a reference implementation for a lightweight wallet developed by the IOHK Engineering Team.
45
star
52

cardano-rest

HTTP interfaces for interacting with the Cardano blockchain.
Haskell
44
star
53

foliage

🌿 Foliage is a tool to create custom Haskell package repositories, in a fully reproducible way.
Haskell
42
star
54

offchain-metadata-tools

Tools for creating, submitting, and managing off-chain metadata such as multi-asset token metadata
Haskell
42
star
55

cicero

event-driven automation for Nomad
Go
41
star
56

medusa

3D github repository visualiser
JavaScript
41
star
57

nothunks

Haskell
39
star
58

partner-chains

Substrate node with capability of communicating with Cardano network
Rust
38
star
59

bech32

Haskell implementation of the Bech32 address format (BIP 0173).
Haskell
37
star
60

catalyst-core

βš™οΈ Core Catalyst Governance Engine and utilities.
Rust
37
star
61

smash

Stakepool Metadata Aggregation Server
Haskell
36
star
62

chain-libs

blockchain libs
Rust
35
star
63

cardanodocs.com-archived

Cardano Settlement Layer Documentation
HTML
35
star
64

devx

The Developer Experience Shell - This repo contains a nix develop shell for haskell. Its primary purpose is to help get a development shell for haskell quickly and across multiple operating systems (and architectures).
Nix
35
star
65

io-sim

Haskell's IO simulator which closely follows core packages (base, async, stm).
Haskell
35
star
66

iohk-monitoring-framework

This framework provides logging, benchmarking and monitoring.
Haskell
33
star
67

mallet

JavaScript
32
star
68

project-icarus-chrome

Icarus, a reference implementation for a lightweight wallet developed by the IOHK Engineering Team.
JavaScript
32
star
69

js-cardano-wasm

various cardano javascript using wasm bindings
Rust
31
star
70

cardano-shell

Node shell, a thin layer for running the node and it's modules.
Haskell
31
star
71

cardano-launcher

Shelley cardano-node and cardano-wallet launcher for NodeJS applications
TypeScript
31
star
72

cardano-world

Cardano world provides preprod and preview cardano networks, configuration documentation and miscellaneous automation.
Nix
30
star
73

rscoin-core

Haskell
29
star
74

quickcheck-dynamic

A library for stateful property-based testing
Haskell
27
star
75

stakepool-management-tools

JavaScript
27
star
76

cardano-haskell-packages

Metadata for Cardano's Haskell package repository
Shell
24
star
77

cardano-haskell

Top level repository for building the Cardano Haskell node and related components and dependencies.
Shell
24
star
78

cardano-configurations

A common place for finding / maintaining configurations of various services of the Cardano eco-system
24
star
79

Developer-Experience-working-group

23
star
80

cardano-transactions

Library utilities for constructing and signing Cardano transactions.
Haskell
23
star
81

psg-cardano-wallet-api

Scala client to the Cardano wallet REST API
Scala
22
star
82

marlowe-ts-sdk

Marlowe TypeScript SDK
TypeScript
22
star
83

sanchonet

Sources for the SanchoNet website
TypeScript
21
star
84

pvss-haskell

Haskell
21
star
85

cardano-wallet-legacy

Official Wallet Backend & API for Cardano-SL
Haskell
21
star
86

scalanet

Scala
20
star
87

cardano-explorer

Backend solution powering the cardano-explorer. ⚠️ See disclaimer below. ⚠️
Haskell
20
star
88

formal-ledger-specifications

Formal specifications of the cardano ledger
Agda
19
star
89

metadata-registry-testnet

Nix
19
star
90

casino

Demo / PoC / implementation of IOHK MPC protocols
Haskell
19
star
91

hackage.nix

Automatically generated Nix expressions for Hackage
19
star
92

cardano-clusterlib-py

Python wrapper for cardano-cli for working with cardano cluster
Python
19
star
93

catalyst-voices

πŸŽ™οΈ Catalyst Voices provides a unified experience and platform including production-ready liquid democracy, meaningful collaboration opportunities & data-driven context for better onboarding & decisions.
Dart
19
star
94

marlowe-starter-kit

This repository contains lessons for using Marlowe via REST and at the command line. It is meant to be used with demeter.run or with a Docker deployment of Marlowe Runtime.
Jupyter Notebook
19
star
95

js-chain-libs

chain-libs javascript SDK
Rust
18
star
96

cardano-coin-selection

A library of algorithms for coin selection and fee balancing.
Haskell
18
star
97

ouroboros-consensus

Implementation of a Consensus Layer for the Ouroboros family of protocols
Haskell
18
star
98

marlowe-pioneer-program

Lectures, documentation, and examples for Marlowe Pioneer Program.
Haskell
18
star
99

chain-wallet-libs

Rust
18
star
100

jortestkit

jormungandr QA
Rust
17
star