-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlinear_optimization_problem_bounty.vy
101 lines (81 loc) · 2.9 KB
/
linear_optimization_problem_bounty.vy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Author: Sören Steiger, github.com/ssteiger
# License: MIT
# EVENTS:
NewSolutionFound: event({_addressOfWinner: indexed(address), _solution: uint256})
BountyTransferred: event({_to: indexed(address), _amount: wei_value})
BountyIncreased: event({_amount: wei_value})
CompetitionTimeExtended: event({_to: uint256})
# STATE VARIABLES:
owner: public(address)
x1: public(uint256)
x2: public(uint256)
bestSolution: public(uint256)
addressOfWinner: public(address)
durationInBlocks: public(uint256)
competitionEnd: public(uint256)
claimPeriodeLength: public(uint256)
# METHODS:
@public
def __init__(_durationInBlocks: uint256):
self.owner = msg.sender
self.bestSolution = 0
self.durationInBlocks = _durationInBlocks
self.competitionEnd = block.number + _durationInBlocks
self.addressOfWinner = ZERO_ADDRESS
# set claim periode to three days
# assuming an average blocktime of 14 seconds -> 86400/14
self.claimPeriodeLength = 6172
@public
@payable
def __default__():
# return any funds sent to the contract address directly
send(msg.sender, msg.value)
@private
def _calculateNewSolution(_x1: uint256, _x2: uint256) -> uint256:
# check new parameters against constraints
assert _x1 <= 40
assert _x2 <= 35
assert (3 * _x1) + (2 * _x2) <= 200
assert _x1 + _x2 <= 120
assert _x1 > 0 and _x2 > 0
# calculate and return new solution
return (4 * _x1) + (6 * _x2)
@public
def submitSolution(_x1: uint256, _x2: uint256) -> uint256:
newSolution: uint256
newSolution = self._calculateNewSolution(_x1, _x2)
assert newSolution > self.bestSolution
# save the solution and it's values
self.x1 = _x1
self.x2 = _x2
self.bestSolution = newSolution
self.addressOfWinner = msg.sender
log.NewSolutionFound(msg.sender, newSolution)
return newSolution
@public
def claimBounty():
assert block.number > self.competitionEnd
if (self.addressOfWinner == ZERO_ADDRESS):
# no solution was found -> extend duration of competition
self.competitionEnd = block.number + self.durationInBlocks
else:
assert block.number < (self.competitionEnd + self.claimPeriodeLength)
assert msg.sender == self.addressOfWinner
send(self.addressOfWinner, self.balance)
# extend duration of competition
self.competitionEnd = block.number + self.durationInBlocks
log.BountyTransferred(self.addressOfWinner, self.balance)
@public
@payable
def topUpBounty():
log.BountyIncreased(msg.value)
@public
def extendCompetition():
# only if no valid solution has been submitted
assert self.addressOfWinner == ZERO_ADDRESS
assert block.number > (self.competitionEnd + self.claimPeriodeLength)
# extend duration of competition
self.competitionEnd = block.number + self.durationInBlocks
# reset winner address
self.addressOfWinner = ZERO_ADDRESS
log.CompetitionTimeExtended(self.competitionEnd)