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%] organiser role
(* start date *)
variable[%mutable organiser (sate = Created)%] startdate date
(* deadline *)
variable[%mutable organiser (state = Created)%] deadline date
variable[%traceable%] prize tez from organizer = 3500
variable competitor role
variable oracle role
asset submission = {
competitor : competitor;
score : uint;
date : date
}
(* state machine *)
states =
| Created initial
| InProgress
| Done with { s1 : balance = 0 }
| Closed
transition confirm from Created = {
to InProgress when { now > startdate }
}
action submit (competitor : competitor) (score[%signedby oracle%] : uint) = {
require {
c1 : state = InProgress
}
effect {
submission.add { competitor = competitor;
score = score;
date = now }
}
}
transition decide from InProgress = {
require {
c2 : now > deadline
}
to Done
with effect {
let submissions = submission.sort(asc(date)).sort(desc(score)) in
if submissions.count() >= 3
then (
let first = submissions.nth(0) in
let second = submissions.nth(1) in
let third = submissions.nth(2) in
let q1 = 0.5 * prize in
let q2 = 0.3 * prize in
let q3 = 0.2 * prize in
transfer q1 to first.competitor;
transfer q2 to second.competitor;
transfer q3 to third.competitor;
transfer (prize - q1 - q2 - q3) to organizer)
else if (submissions.count() >= 2)
then (
let first = submissions.nth(0) in
let second = submissions.nth(1) in
let q1 = 0.6 * prize in
let q2 = 0.4 * prize in
transfer q1 to first.competitor;
transfer q2 to second.competitor;
transfer (prize - q1 - q2) to organizer)
else if (submissions.count() >= 1)
then (
let first = submissions.nth(0) in
transfer prize to first.competitor)
else transfer back prize
}
}