This contract implements a bounty for solving linear optimization problems.

An example problem is defined in the contracts `_calculateNewSolution(x1: uint256, x2: uint256)`

method:

def _calculateNewSolution(_x1: uint256, _x2: uint256) -> uint256:assert x1 <= 40assert x2 <= 35assert (3 * x1) + (2 * x2) <= 200assert x1 + x2 <= 120assert x1 > 0 and x2 > 0# calculate and return new solutionreturn (4 * x1) + (6 * x2)

Users can submit solutions using the `submitSolution(_x1: uint256, _x2: uint256)`

method. The contract checks the submitted values against the problems constraints and saves/rejects them depending on whether the solution respects all of them. When the end of the competition is reached, the address that submitted the best solution can call `claimBounty()`

to claim the Ether that is locked in the contract.

| |

Truffle Project | https://github.com/vyperhub-io/vyper-smart-contracts/tree/master/linear_optimization_problem_bounty |

Contract | |

Tests |

linear_optimization_problem_bounty.vy# Author: Sören Steiger, twitter.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:@publicdef __init__(_durationInBlocks: uint256):self.owner = msg.senderself.bestSolution = 0self.durationInBlocks = _durationInBlocksself.competitionEnd = block.number + _durationInBlocksself.addressOfWinner = ZERO_ADDRESS# set claim periode to three days# assuming an average blocktime of 14 seconds -> 86400/14self.claimPeriodeLength = 6172@public@payabledef __default__():# return any funds sent to the contract address directlysend(msg.sender, msg.value)@privatedef _calculateNewSolution(_x1: uint256, _x2: uint256) -> uint256:# check new parameters against constraintsassert _x1 <= 8assert _x2 <= 12assert (3 * _x1) + (2 * _x2) <= 100assert _x1 + _x2 <= 24assert _x1 > 0 and _x2 > 0# calculate and return new solutionreturn (3 * _x1) + (2 * _x2)@publicdef submitSolution(_x1: uint256, _x2: uint256) -> uint256:newSolution: uint256newSolution = self._calculateNewSolution(_x1, _x2)assert newSolution > self.bestSolution# save the solution and it's valuesself.x1 = _x1self.x2 = _x2self.bestSolution = newSolutionself.addressOfWinner = msg.senderlog.NewSolutionFound(msg.sender, newSolution)return newSolution@publicdef claimBounty():assert block.number > self.competitionEndif (self.addressOfWinner == ZERO_ADDRESS):# no solution was found -> extend duration of competitionself.competitionEnd = block.number + self.durationInBlockselse:assert block.number < (self.competitionEnd + self.claimPeriodeLength)assert msg.sender == self.addressOfWinnersend(self.addressOfWinner, self.balance)# extend duration of competitionself.competitionEnd = block.number + self.durationInBlockslog.BountyTransferred(self.addressOfWinner, self.balance)@public@payabledef topUpBounty():log.BountyIncreased(msg.value)@publicdef extendCompetition():# only if no valid solution has been submittedassert self.addressOfWinner == ZERO_ADDRESSassert block.number > (self.competitionEnd + self.claimPeriodeLength)# extend duration of competitionself.competitionEnd = block.number + self.durationInBlocks# reset winner addressself.addressOfWinner = ZERO_ADDRESSlog.CompetitionTimeExtended(self.competitionEnd)