Competition

Best submission decision process

The competition archetype defines a process that enables a competitor to submit a score. The score is signed by an oracle. The signature of the score is generated by the signedby extension.

The decision process specified in the decide transaction allocates the prize to the top 3 scores. Allocation policy manages the situations where less than 3 competitors have submitted a score.

competition.arl
archetype competition
variable[%transferable%] organizer : role = @tz1Lc2qBKEWCBeDU8npG6zCeCqpmaegRi6Jg
(* start date *)
variable[%mutable (organiser, (instate (Created)))%] startdate : date = 2019-11-12T00:00:00
(* deadline *)
variable[%mutable (organiser, (instate (Created)))%] deadline : date = 2020-11-12T00:00:00
variable[%traceable%] prize : tez = 3500tz
variable oracle : role = @tz1bfVgcJC4ukaQSHUe1EbrUd5SekXeP9CWk
asset submission {
competitor : role;
score : int;
timestamp : date;
}
(* state machine *)
states =
| Created initial
| InProgress
| Done with { s1 : balance = 0tz; }
| Closed
transition confirm () {
from Created
to InProgress when { now > startdate }
}
action submit (ckey : pkey of submission, pscore[%signedby (oracle)%] : int) {
require {
c1 : state = InProgress;
}
effect {
submission.add ({ competitor = ckey;
score = pscore;
timestamp = now })
}
}
transition decide () {
require {
c2 : now > deadline;
}
from InProgress
to Done
with effect {
let submissions = submission.sort(desc(score), timestamp) in
if submissions.count() >= 3
then (
let first = submissions.nth(0).competitor in
let second = submissions.nth(1).competitor in
let third = submissions.nth(2).competitor in
let q1 = 0.5 * prize in
let q2 = 0.3 * prize in
let q3 = 0.2 * prize in
transfer q1 to first;
transfer q2 to second;
transfer q3 to third;
transfer (prize - q1 - q2 - q3) to organizer)
else if (submissions.count() >= 2)
then (
let first = submissions.nth(0).competitor in
let second = submissions.nth(1).competitor in
let q1 = 0.6 * prize in
let q2 = 0.4 * prize in
transfer q1 to first;
transfer q2 to second;
transfer (prize - q1 - q2) to organizer)
else if (submissions.count() >= 1)
then (
let first = submissions.nth(0).competitor in
transfer prize to first)
else transfer prize to organizer
}
}