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.

archetype competition
variable organizer : role = @tz1Lc2qBKEWCBeDU8npG6zCeCqpmaegRi6Jg
(* start date *)
variable startdate : date = 2019-11-12T00:00:00
(* deadline *)
variable deadline : date = 2020-11-12T00:00:00
variable 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 }
}
entry submit (ckey : pkey of submission, pscore : 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 {
var submissions = submission.sort(desc(score), timestamp);
if submissions.count() >= 3
then (
var first = submissions.nth(0);
var second = submissions.nth(1);
var third = submissions.nth(2);
var q1 = 0.5 * prize;
var q2 = 0.3 * prize;
var q3 = 0.2 * prize;
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 (
var first = submissions.nth(0);
var second = submissions.nth(1);
var q1 = 0.6 * prize;
var q2 = 0.4 * prize;
transfer q1 to first;
transfer q2 to second;
transfer (prize - q1 - q2) to organizer)
else if (submissions.count() >= 1)
then (
var first = submissions.nth(0);
transfer prize to first)
else transfer prize to organizer
}
}
Edit on GitHub