Permalink
Find file
6dea637 Jul 25, 2016
@joeykrug @tinybike @kevinday
executable file 341 lines (283 sloc) 12 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 thes
# 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
inset('refund.se')
# Markets' index is the marketID, markets stores data about markets [which are able to be traded on] in augur
# Events is a 0 indexed array of events in the market in the format of
# [1, event, event, 2, event, 3, event, event, event]
# 1 means dimension 1, 2 is events in dimension 2, 3 is the dimension 3 events
# Sharespurchased keeps track of the number of shares purchased for each outcome
# sharesPurchased states starts at 1 - (same for participants[].shares[] array)
# Participants is an array of participants, their addresses as keys and the shares in each outcome they've purchased of an event as the values
# e.g. participants[addr].shares[1]
# currentParticipant is the number of participants [traders] in the market
# cumulativeScale == range of the market for scalars
# numOutcomes is the number of outcomes in a market [up to 8 for now]
# a binary outcome state 1 is no, 2 is true, 0 if not done, 1.5 if indeterminate
# tradingFee is a percent in fixedPoint and the base trading fee for a market: takers pay 100%-150% of this, makers pay 0-50% of this
# tradingPeriod is which eventExpPeriod market expires in
# pushing forward is whether a market has been pushed forward to attempt early resolution
# bondsman is the person/address who/that posted the early resolution bond
# originalPeriod is the original period the market's event would resolve in
# trade_ids stores trades for a given market
# last_trade is the last trade that happened in a given market
# total_trades is the total number of trades in a give market
# tags are the subject tags / categories / keywords for a given market
# makerFees - from 0-50% - amount of fee that the maker orders pay
# extraInfo is any extra info that didn't fit well in the regular description
# sharesValue is the value of the shares traded in a given market
# gasSubsidy is paid by the creator to pay for closing the market [closing is just setting the winning outcomes and allowing people to withdraw funds if they own winning shares]
# fees is total amount of fees a market has paid to a branch [the reporters]
# lastExpDate is when the market's last event expires
# prices keeps track of the latest traded price for a given outcome in a market
data Markets[](events[], lenEvents, sharesPurchased[], participants[](shares[]), winningOutcomes[], cumulativeScale, numOutcomes, tradingPeriod, tradingFee, branch, volume, pushingForward, bondsMan, originalPeriod, trade_ids[](id, next_id, prev_id), last_trade, total_trades, tag1, tag2, tag3, makerFees, extraInfo[], extraInfoLen, sharesValue, gasSubsidy, fees, creationTime, lastExpDate, prices[])
# takes branch as param, composite hash of all markets in the system
data marketsHash[]
def getMarketsHash(branch):
return(self.marketsHash[branch])
def addToMarketsHash(branch, newHash):
x = array(2)
x[0] = self.marketsHash[branch]
x[1] = newHash
self.marketsHash[branch] = sha3(x, items=2)
return(1)
def addFees(market, amount):
self.Markets[market].fees += amount
return(1)
def setPrice(market, outcome, price):
self.Markets[market].prices[outcome] = price
return(1)
# refund closing cost for a market
def refundClosing(market, to):
subsidy = self.Markets[market].gasSubsidy
self.Markets[market].gasSubsidy -= subsidy
send(subsidy, to)
return(1)
def getLastExpDate(market):
return(self.Markets[market].lastExpDate)
def getLastOutcomePrice(market, outcome):
return(self.Markets[market].prices[outcome])
def getFees(market):
return(self.Markets[market].fees)
def getMakerFees(market):
return(self.Markets[market].makerFees)
def getgasSubsidy(market):
return(self.Markets[market].gasSubsidy)
def getSharesValue(market):
return(self.Markets[market].sharesValue)
def returnTags(market):
tags = array(3)
tags[0] = self.Markets[market].tag1
tags[1] = self.Markets[market].tag2
tags[2] = self.Markets[market].tag3
return(tags: arr)
def getTotalSharesPurchased(market):
refund()
total = 0
n = self.Markets[market].numOutcomes
i = 1
while i <= n:
total += self.Markets[market].sharesPurchased[i]
i += 1
return(total)
def getMarketEvent(market, index):
refund()
return(self.Markets[market].events[index])
def getCreationTime(market):
return(self.Markets[market].creationTime)
def getMarketEvents(market):
refund()
return(load(self.Markets[market].events[0], items=self.Markets[market].lenEvents): arr)
def getSharesPurchased(market, outcome):
refund()
return(self.Markets[market].sharesPurchased[outcome])
def getExtraInfoLength(market):
return(self.Markets[market].extraInfoLen)
def getExtraInfo(market):
refund()
length=self.Markets[market].extraInfoLen
return(load(self.Markets[market].extraInfo[0], chars=length): str)
def getVolume(market):
refund()
return(self.Markets[market].volume)
def getParticipantSharesPurchased(market, trader, outcome):
refund()
return(self.Markets[market].participants[trader].shares[outcome])
def getNumEvents(market):
refund()
return(self.Markets[market].lenEvents)
def getCumScale(market):
refund()
return(self.Markets[market].cumulativeScale)
def getMarketNumOutcomes(market):
refund()
return(self.Markets[market].numOutcomes)
def getTradingPeriod(market):
refund()
return(self.Markets[market].tradingPeriod)
def getOriginalTradingPeriod(market):
refund()
return(self.Markets[market].originalPeriod)
def setTradingPeriod(market, period):
refund()
self.Markets[market].tradingPeriod = period
return(1)
def getTradingFee(market):
refund()
return(self.Markets[market].tradingFee)
def getBranchID(market):
refund()
return(self.Markets[market].branch)
macro market($marketID):
self.Markets[$marketID]
macro cumScale($marketID):
market($marketID).cumulativeScale
macro numOutcomes($marketID):
market($marketID).numOutcomes
macro sharesPurchased($marketID):
market($marketID).sharesPurchased
# check that msg.sender is one of our function contracts
def initializeMarket(marketID, events: arr, tradingPeriod, tradingFee, branch, tag1, tag2, tag3, makerFees, cumScale, numOutcomes, extraInfo: str, gasSubsidy, creationFee, lastExpDate):
refund()
#if(!self.whitelist.check(msg.sender)):
# return(-1)
# add event list to the market
save(self.Markets[marketID].events[0], events, items=len(events))
self.Markets[marketID].lenEvents = len(events)
save(self.Markets[marketID].extraInfo[0], extraInfo, chars=len(extraInfo))
self.Markets[marketID].extraInfoLen = len(extraInfo)
self.Markets[marketID].tradingPeriod = tradingPeriod
self.Markets[marketID].originalPeriod = tradingPeriod
self.Markets[marketID].tradingFee = tradingFee
self.Markets[marketID].branch = branch
self.Markets[marketID].cumulativeScale = cumScale
self.Markets[marketID].numOutcomes = numOutcomes
self.Markets[marketID].tag1 = tag1
self.Markets[marketID].tag2 = tag2
self.Markets[marketID].gasSubsidy = gasSubsidy
self.Markets[marketID].tag3 = tag3
self.Markets[marketID].makerFees = makerFees
self.Markets[marketID].fees = creationFee
self.Markets[marketID].creationTime = block.timestamp
self.Markets[marketID].lastExpDate = lastExpDate
return(1)
def modifyShares(marketID, outcome, amount):
refund()
#if(!self.whitelist.check(msg.sender)):
# return(-1)
sharesPurchased(marketID)[outcome] += amount
return(1)
def modifySharesValue(marketID, amount):
self.Markets[marketID].sharesValue += amount
return(1)
def modifyParticipantShares(marketID, trader, outcome, amount, cancel):
refund()
#if(!self.whitelist.check(msg.sender)):
# return(-1)
self.Markets[marketID].participants[trader].shares[outcome] += amount
if(amount<0):
amount = amount*-1
if(cancel):
amount = -1*amount
self.Markets[marketID].volume += amount
return(1)
def setWinningOutcomes(market, outcomes: arr):
refund()
#if(!self.whitelist.check(msg.sender)):
# return(-1)
# largest # of winning outcomes is 8
save(self.Markets[market].winningOutcomes[0], outcomes, items=len(outcomes))
return(1)
def getWinningOutcomes(market):
refund()
# do whitelist, if oracle system branch, always return outcome, if in whitelist it's good, if not, log
#if(!self.whitelist.check(msg.sender)):
# return(-1)
# largest # of winning outcomes is 8
return(load(self.Markets[market].winningOutcomes[0], items=8): arr)
def getOneWinningOutcome(market, num):
refund()
# whitelist
return(self.Markets[market].winningOutcomes[num])
def setTradingFee(market, fee):
refund()
self.Markets[market].tradingFee = fee
return(fee)
def setMakerFees(market, makerFees):
refund()
self.Markets[market].makerFees = makerFees
return(makerFees)
def setPushedForward(market, bool, sender):
refund()
self.Markets[market].pushingForward = bool
self.Markets[market].bondsMan = sender
return(1)
def getPushedForward(market):
refund()
return(self.Markets[market].pushingForward)
def getBondsMan(market):
refund()
return(self.Markets[market].bondsMan)
def getLastTrade(market):
refund()
return(self.Markets[market].last_trade)
def addTrade(market, trade_id, last_id):
refund()
self.Markets[market].trade_ids[last_id].next_id = trade_id
self.Markets[market].trade_ids[trade_id].prev_id = last_id
self.Markets[market].trade_ids[trade_id].id = trade_id
self.Markets[market].last_trade = trade_id
self.Markets[market].total_trades += 1
return(1)
def remove_trade_from_market(market_id, trade_id):
refund()
prev_id = self.Markets[market_id].trade_ids[trade_id].prev_id
next_id = self.Markets[market_id].trade_ids[trade_id].next_id
if prev_id and next_id:
self.Markets[market_id].trade_ids[prev_id].next_id = next_id
self.Markets[market_id].trade_ids[next_id].prev_id = prev_id
elif prev_id:
self.Markets[market_id].last_trade = prev_id
self.Markets[market_id].trade_ids[prev_id].next_id = 0
if next_id:
self.Markets[market_id].trade_ids[trade_id].next_id = 0
if prev_id:
self.Markets[market_id].trade_ids[trade_id].prev_id = 0
self.Markets[market_id].trade_ids[trade_id].id = 0
self.Markets[market_id].total_trades -= 1
return(1)
def get_trade_ids(market_id):
refund()
trades_count = self.Markets[market_id].total_trades
trade_id = self.Markets[market_id].last_trade
trade_ids = array(trades_count)
i = 0
while i < trades_count:
trade_ids[i] = self.Markets[market_id].trade_ids[trade_id].id
trade_id = self.Markets[market_id].trade_ids[trade_id].prev_id
i = i + 1
if trade_ids:
return(trade_ids:arr)
return([FAILURE]:arr)
def get_total_trades(market_id):
refund()
return(self.Markets[market_id].total_trades)
def getSender():
return(msg.sender)