Skip to main content

Idea box

Introduction

This contract implements an Idea Box on the Tezos blockchain. The chairman registers voters and selects the best ideas. Voters are granted a fixed number of votes.

You can see this contract in action in the Idea Box DApp example.

API

Storage

Ideas data (title and description) are stored as bytes.

NameTypeDescription
chairmanaddressAddress of the box's chairman
maxvotesnatMaximum number of votes per voter.
ideacollectionAn idea is defined by:
  • an identifier (key)
  • a title
  • a description
  • a number of votes
  • a creation date
  • the author's address
votercollectionA voter is defined by:
  • an address (key)
  • a number of remaining votes
selectedcollecterThis is the collection of selected ideas.
_statestatesBox state, one of Activated, Terminated.

Entrypoints

NameParametersDescription
registeraCalled by chairman to register a new voter at address a and remaining votes at maxvotes.
add_ideaititle, descriptionAdds an idea in the box if box not terminated (anyone can add an idea).
voten, weightCalled by a voter to increment by weight the number of votes of idea n. It fails if box is terminated.
terminateCalled by chairman to close the box and select the top 3 best ideas with numbers of votes above maxvotes.

Originate

Originate this contract with the widget below.

Click "Connect to Wallet" button, fill the fields "Owner" and "Rate", and click "Originate".

Command line

Originate the contract from Archetype code below with the following Completium CLI example command:

completium-cli deploy ideabox.arl --init '(@tz1LLJ3nxbpGGMLmjzcp9sTMYui87tycG6nG, 5)'

The command sets:

  • chairman variable to tz1LLJ3nxbpGGMLmjzcp9sTMYui87tycG6nG
  • maxvotes variable to 2.5

Code

ideabox.arl
archetype ideasbox(chairman : address, maxvotes : nat)

states =
| Activated initial
| Terminated

asset idea {
id : nat;
title : bytes;
desc : bytes;
nbvotes : nat = 0;
creation : date;
author : address;
}

asset voter {
addr : address;
remaining : nat = maxvotes;
}

asset selected {
sid : nat;
}

entry register (a_voter : address) {
called by chairman
require {
r0 : state = Activated;
}
effect { voter.add({ addr = a_voter }) }
}

entry add_idea(ititle : bytes, description : bytes) {
require {
r1 : state = Activated;
}
effect {
idea.add({
id = idea.count();
title = ititle;
desc = description;
creation = now;
author = caller
})
}
}

entry vote(n : nat, weight : nat) {
require {
r2 : voter.contains(caller);
r3 : voter[caller].remaining >= weight;
r4 : state = Activated;
}
effect {
voter[caller].remaining -= weight;
idea[n].nbvotes += weight;
}
}

transition terminate () {
called by chairman
from Activated to Terminated
with effect {
for i in idea.select(the.nbvotes > maxvotes).sort(desc(nbvotes)).head(3) do
selected.add({i})
done
}
}