Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
139 lines (118 sloc) 3.77 KB
inset('refund.se')
### Storage of all data associated with trades
# reporterID is index
data tradeCommits[](hash, block)
# tradeID is index
data trades[](id, type, market, amount, price, owner, block, outcome)
# indices are marketID and outcomeID
data bestTrade[][](bid, ask)
def makeTradeHash(max_value, max_amount, trade_ids: arr):
refund()
i = 0
trades = 0
while i < len(trade_ids):
trades += trade_ids[i]
i+=1
tradeInfo = array(3)
tradeInfo[0] = trades
tradeInfo[1] = max_amount
tradeInfo[2] = max_value
tradeHash = sha3(tradeInfo, items=3)
return(tradeHash)
def commitTrade(hash):
refund()
self.tradeCommits[msg.sender].hash = hash
self.tradeCommits[msg.sender].block = block.number
return(1)
def zeroHash():
refund()
self.tradeCommits[msg.sender].hash = 0
return(1)
def checkHash(tradeHash, sender):
refund()
if(self.tradeCommits[sender].hash != tradeHash || block.number <= self.tradeCommits[sender].block):
return(-1)
else:
return(1)
def getID(tradeID):
refund()
return(self.trades[tradeID].id)
def getBestBidID(market, outcome):
return(self.bestTrade[market][outcome].bid)
def getBestAskID(market, outcome):
return(self.bestTrade[market][outcome].ask)
def saveTrade(trade_id, type, market, amount, price, sender, outcome):
refund()
self.trades[trade_id].id = trade_id
self.trades[trade_id].type = type
self.trades[trade_id].market = market
self.trades[trade_id].amount = amount
self.trades[trade_id].price = price
self.trades[trade_id].owner = sender
self.trades[trade_id].block = block.number
self.trades[trade_id].outcome = outcome
# Update best bid and ask trade IDs:
# - if this outcome's best trade is set, but this trade price is better,
# then this trade ID is the new best trade
# - if this outcome's best trade is unset, then this trade ID is the new
# best trade
if type == 1:
bestBidID = self.bestTrade[market][outcome].bid
if bestBidID == 0 or price > self.trades[bestBidID].price:
self.bestTrade[market][outcome].bid = trade_id
else:
bestAskID = self.bestTrade[market][outcome].ask
if bestAskID == 0 or price < self.trades[bestAskID].price:
self.bestTrade[market][outcome].ask = trade_id
return(1)
def getTradeOwner(id):
refund()
return(self.trades[id].owner)
def get_trade(id):
refund()
trade = array(8)
trade[0] = self.trades[id].id
trade[1] = self.trades[id].type
trade[2] = self.trades[id].market
trade[3] = self.trades[id].amount
trade[4] = self.trades[id].price
trade[5] = self.trades[id].owner
trade[6] = self.trades[id].block
trade[7] = self.trades[id].outcome
if trade:
return(trade:arr)
return([0]:arr)
def get_trade_block(id):
refund()
return(self.trades[id].block)
def get_amount(id):
refund()
return(self.trades[id].amount)
def get_price(id):
refund()
return(self.trades[id].price)
def update_trade(id, price):
refund()
self.trades[id].price = price
self.trades[id].block = block.number
def remove_trade(id):
refund()
market = self.trades[id].market
outcome = self.trades[id].outcome
if self.bestTrade[market][outcome].bid == id:
self.bestTrade[market][outcome].bid = 0
elif self.bestTrade[market][outcome].ask == id:
self.bestTrade[market][outcome].ask = 0
self.trades[id].id = 0
self.trades[id].type = 0
self.trades[id].market = 0
self.trades[id].amount = 0
self.trades[id].price = 0
self.trades[id].owner = 0
self.trades[id].block = 0
self.trades[id].outcome = 0
return(1)
def fill_trade(id, fill):
refund()
self.trades[id].amount -= fill
return(1)