Skip to content

Commit

Permalink
Merge pull request #35 from gnosis/std_market_w_price_2
Browse files Browse the repository at this point in the history
Std market w price 2
  • Loading branch information
cag authored Aug 23, 2017
2 parents a160623 + 1908f0e commit 056d8d5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 29 deletions.
49 changes: 33 additions & 16 deletions contracts/Markets/StandardMarketWithPriceLogger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ contract StandardMarketWithPriceLogger is StandardMarket {
/*
* Constants
*/
uint constant ONE = 0x10000000000000000;
uint8 public constant LONG = 1;

/*
* Storage
*/
uint public startDate;
uint public endDate;
uint public lastTrade;
uint public lastTradeDate;
uint public lastTradePrice;
uint public priceIntegral;

/*
Expand All @@ -30,13 +32,19 @@ contract StandardMarketWithPriceLogger is StandardMarket {
public
StandardMarket(_creator, _eventContract, _marketMaker, _fee)
{
require(eventContract.getOutcomeCount() == 2);

if (_startDate == 0)
startDate = now;
else {
// The earliest start date is the market creation date
require(_startDate >= now);
startDate = _startDate;
}

lastTradeDate = startDate;
// initialize lastTradePrice to assuming uniform probabilities of outcomes
lastTradePrice = ONE / 2;
}

/// @dev Allows market creator to close the markets by transferring all remaining outcome tokens to the creator
Expand All @@ -56,8 +64,9 @@ contract StandardMarketWithPriceLogger is StandardMarket {
public
returns (uint cost)
{
logPrice();
logPriceBefore();
cost = super.buy(outcomeTokenIndex, outcomeTokenCount, maxCost);
logPriceAfter();
}

/// @dev Allows to sell outcome tokens to market maker
Expand All @@ -69,8 +78,9 @@ contract StandardMarketWithPriceLogger is StandardMarket {
public
returns (uint profit)
{
logPrice();
logPriceBefore();
profit = super.sell(outcomeTokenIndex, outcomeTokenCount, minProfit);
logPriceAfter();
}

/// @dev Buys all outcomes, then sells all shares of selected outcome which were bought, keeping
Expand All @@ -83,35 +93,42 @@ contract StandardMarketWithPriceLogger is StandardMarket {
public
returns (uint cost)
{
logPrice();
logPriceBefore();
cost = super.shortSell(outcomeTokenIndex, outcomeTokenCount, minProfit);
logPriceAfter();
}

/// @dev Calculates average price for long tokens based on price integral
/// @return Average price for long tokens
/// @return Average price for long tokens over time
function getAvgPrice()
public
returns (uint)
{
return priceIntegral / (now - startDate);
if(endDate > 0)
return (priceIntegral + lastTradePrice * (endDate - lastTradeDate)) / (endDate - startDate);
return (priceIntegral + lastTradePrice * (now - lastTradeDate)) / (now - startDate);
}

/*
* Private functions
*/
/// @dev Adds price integral since the last trade to the total price integral and updates last
/// trade timestamp
function logPrice()
/// @dev Adds price integral since the last trade to the total price integral
function logPriceBefore()
private
{
if (now >= startDate) {
// Calculate price of long tokens
uint price = marketMaker.calcMarginalPrice(this, LONG);
if (lastTrade > 0)
priceIntegral += price * (now - lastTrade);
else
priceIntegral += price * (now - startDate);
lastTrade = now;
// Accumulate price integral only if logging has begun
priceIntegral += lastTradePrice * (now - lastTradeDate);
}
}

/// @dev Updates last trade timestamp and price
function logPriceAfter()
private
{
// Refresh lastTradePrice after every transactions as we don't know if
// this will be the last transaction before logging period starts
lastTradePrice = marketMaker.calcMarginalPrice(this, LONG);
lastTradeDate = now;
}
}
16 changes: 8 additions & 8 deletions contracts/Oracles/FutarchyOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ contract FutarchyOracle is Oracle {
address creator;
StandardMarketWithPriceLogger[] public markets;
CategoricalEvent public categoricalEvent;
uint public deadline;
uint public tradingPeriod;
uint public winningMarketIndex;
bool public isSet;

Expand All @@ -54,7 +54,7 @@ contract FutarchyOracle is Oracle {
/// @param marketFactory Market factory contract
/// @param marketMaker Market maker contract
/// @param fee Market fee
/// @param _deadline Decision deadline
/// @param _tradingPeriod Trading period before decision can be determined
/// @param startDate Start date for price logging
function FutarchyOracle(
address _creator,
Expand All @@ -67,14 +67,14 @@ contract FutarchyOracle is Oracle {
StandardMarketWithPriceLoggerFactory marketFactory,
MarketMaker marketMaker,
uint24 fee,
uint _deadline,
uint _tradingPeriod,
uint startDate

)
public
{
// Deadline is in the future
require(_deadline > 0);
// trading period is at least a second
require(_tradingPeriod > 0);
// Create decision event
categoricalEvent = eventFactory.createCategoricalEvent(collateralToken, this, outcomeCount);
// Create outcome events
Expand All @@ -88,7 +88,7 @@ contract FutarchyOracle is Oracle {
markets.push(marketFactory.createMarket(scalarEvent, marketMaker, fee, startDate));
}
creator = _creator;
deadline = _deadline;
tradingPeriod = _tradingPeriod;
}

/// @dev Funds all markets with equal amount of funding
Expand Down Expand Up @@ -133,8 +133,8 @@ contract FutarchyOracle is Oracle {
function setOutcome()
public
{
// Outcome is not set yet and deadline has passed
require(!isSet && markets[0].startDate() + deadline < now);
// Outcome is not set yet and trading period is over
require(!isSet && markets[0].startDate() + tradingPeriod < now);
// Find market with highest marginal price for long outcome tokens
uint highestAvgPrice = markets[0].getAvgPrice();
uint highestIndex = 0;
Expand Down
10 changes: 5 additions & 5 deletions contracts/Oracles/FutarchyOracleFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ contract FutarchyOracleFactory {
int upperBound,
MarketMaker marketMaker,
uint24 fee,
uint deadline,
uint tradingPeriod,
uint startDate
);

Expand Down Expand Up @@ -51,7 +51,7 @@ contract FutarchyOracleFactory {
/// @param upperBound Lower bound for event outcome
/// @param marketMaker Market maker contract
/// @param fee Market fee
/// @param deadline Decision deadline
/// @param tradingPeriod Trading period before decision can be determined
/// @param startDate Start date for price logging
/// @return Oracle contract
function createFutarchyOracle(
Expand All @@ -62,7 +62,7 @@ contract FutarchyOracleFactory {
int upperBound,
MarketMaker marketMaker,
uint24 fee,
uint deadline,
uint tradingPeriod,
uint startDate
)
public
Expand All @@ -79,7 +79,7 @@ contract FutarchyOracleFactory {
marketFactory,
marketMaker,
fee,
deadline,
tradingPeriod,
startDate
);
FutarchyOracleCreation(
Expand All @@ -92,7 +92,7 @@ contract FutarchyOracleFactory {
upperBound,
marketMaker,
fee,
deadline,
tradingPeriod,
startDate
);
}
Expand Down

0 comments on commit 056d8d5

Please sign in to comment.