Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 165 lines (151 sloc) 6.98 KB
# This software (Augur) allows buying && selling event outcomes in ethereum
# Copyright (C) 2015 Forecast Foundation OU
# This program is free software; you can redistribute it &&/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is free software: you can redistribute it &&/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Any questions please contact joey@augur.net
import branches as BRANCHES
import cash as CASH
import markets as MARKETS
import events as EVENTS
import payout as PAYOUT
import backstops as BACKSTOPS
import eventResolution as RESOLVE
inset('refund.se')
inset('logReturn.se')
event closedMarket(market: indexed, branch: indexed, sender: indexed)
event closeMarket_logReturn(returnValue)
# first param is the market, second param is the subcurrency contract
data cash[][]
macro YES: TWO
macro NO: ONE
macro BAD: 3 * ONEHALF
macro CATCH_TOLERANCE: ONE / 10
# Bins values to 1, 1.5, 2
macro catch($x):
if($x < (BAD - CATCH_TOLERANCE)):
NO
elif($x > (BAD + CATCH_TOLERANCE)):
YES
else:
BAD
# If ethicality value <.5 it's 0, otherwise it's ethical
macro ethic_catch($x):
if($x < ONEHALF):
0
else:
ONE
# Loop through events in the market, get their outcomes && use those to determine the winning outcomes and resolve a market!
# @return 0 if fail/trading not over yet/event not expired or closed already, if success 1
# Error messages otherwise
# -1: Market has no cash anyway / already closed
# -2: 0 outcome / not reported on yet
# -3: not final round 2 event
# -4: market isn't in branch
# -5: Event forked and not final yet
# -6: bonded pushed forward market not ready to be resolved
# -7: already resolved
# if market's events have moved due to a fork branch param passed should be the new fork [todo check to make sure it handles the wrong branch param properly here]
def closeMarket(branch, market, sender):
refund()
numberEvents = MARKETS.getNumEvents(market)
tradingPeriod = MARKETS.getTradingPeriod(market)
period = BRANCHES.getVotePeriod(branch)
event = MARKETS.getMarketEvent(market, 0)
if(MARKETS.getOneWinningOutcome(market, 0)):
logReturn(closeMarket_logReturn, -1)
if(EVENTS.getUncaughtOutcome(event)==0):
logReturn(closeMarket_logReturn, -2)
if(BACKSTOPS.getRoundTwo(event) && !BACKSTOPS.getFinal(event)):
logReturn(closeMarket_logReturn, -3)
if(MARKETS.getBranchID(market)!=branch):
logReturn(closeMarket_logReturn, -4)
# checks whether any events not already resolved
resolved = 1
# what if we forked or round 2 won't it be resolved and thus market never gets resolved todo
if(EVENTS.getOutcome(event)==0 && EVENTS.getmode(event)==0):
resolved = 0
if((period > tradingPeriod) && !resolved):
# look at through event in the market, get its outcomes, resolve it && use those to determine the winning outcomes for a given market!
votingPeriodEvent = EVENTS.getExpiration(event)/BRANCHES.getPeriodLength(branch)
fxpOutcome = EVENTS.getOutcome(event)
resolution = 1
forkPeriod = BRANCHES.getForkPeriod(EVENTS.getEventBranch(event))
currentPeriod = block.timestamp / BRANCHES.getPeriodLength(branch)
if((EVENTS.getForked(event) && !EVENTS.getForkedDone(event)) or (currentPeriod <= (forkPeriod+1))):
logReturn(closeMarket_logReturn, -5)
if(binary(event) && fxpOutcome==0):
resolution = RESOLVE.resolveBinary(event, market, branch, votingPeriodEvent)
elif(scalar(event) && EVENTS.getmode(event)==0):
resolution = RESOLVE.resolveCategoricalOrScalar(EVENTS.getMinValue(event), EVENTS.getMaxValue(event), event, market, branch, votingPeriodEvent)
elif(categorical(event) && EVENTS.getmode(event)==0):
resolution = RESOLVE.resolveCategoricalOrScalar(ONE, ONE*EVENTS.getNumOutcomes(event), event, market, branch, votingPeriodEvent)
if(resolution==-6):
logReturn(closeMarket_logReturn, -6)
winningOutcomes = array(8)
winningOutcomes = RESOLVE.determineWinningOutcomes(event, outitems=8)
MARKETS.setWinningOutcomes(market, winningOutcomes)
# refunds closing cost
MARKETS.refundClosing(market, sender)
log(type=closedMarket, market, branch, sender)
logReturn(closeMarket_logReturn, 1)
else:
logReturn(closeMarket_logReturn, 0)
# Claim trading profits/value per share after a market is resolved
# @returns 1 if successful
# Errors:
# 0: market not resolved
# -1: trader doesn't exist
# -8: invalid branch
def claimProceeds(branch, market):
refund()
if(MARKETS.getBranchID(market)!=branch):
return(-8)
tradingPeriod = MARKETS.getTradingPeriod(market)
period = BRANCHES.getVotePeriod(branch)
event = MARKETS.getMarketEvent(market, 0)
resolved = 1
if(EVENTS.getOutcome(event)==0 && EVENTS.getmode(event)==0):
resolved = 0
# as long as it's resolved money can be collected
if((period > tradingPeriod || reportingDone) && resolved):
winningOutcomes = array(8)
winningOutcomes = MARKETS.getWinningOutcomes(market, outitems=8)
outcome = 0
# market not resolved
if(winningOutcomes[0]==0):
return(0)
if(winningOutcomes[1]==0):
ethical = ethic_catch(EVENTS.getEthical(event))
# unethical or .5 categorical 1d market is resolved with all outcomes having equal values
if(categorical(event) && (EVENTS.getmode(event)==ONEHALF or !ethical)):
outcome = PAYOUT.oneOutcome(market, winningOutcomes[0], msg.sender, 1, EVENTS.getNumOutcomes(event))
# resolves a regular binary market
else:
outcome = PAYOUT.oneOutcome(market, winningOutcomes[0], msg.sender, 0, 0)
# resolves a scalar market
elif(winningOutcomes[1]):
outcome = PAYOUT.twoOutcomes(market, winningOutcomes, event, msg.sender)
return(outcome)
else:
return(0)
macro scalar($event):
((EVENTS.getMaxValue($event)!=TWO || EVENTS.getMinValue($event)!=ONE) && EVENTS.getNumOutcomes($event)==2)
macro binary($event):
(EVENTS.getNumOutcomes($event)==2 and EVENTS.getMaxValue($event)==TWO and EVENTS.getMinValue($event)==ONE)
macro categorical($event):
(EVENTS.getNumOutcomes($event)>2)