LMSRMarketMaker

contract LMSRMarketMaker is MarketMaker
Title:LMSR market maker contract - Calculates share prices based on share distribution and initial funding
Author:Alan Lu - <alan.lu@gnosis.pm>
uint constant ONE
int constant EXP_LIMIT
function calcNetCost(Market market, int[] memory outcomeTokenAmounts)
public
view
returns (int netCost)

Calculates the net cost for executing a given trade.

Parameters:
  • market – Market contract
  • outcomeTokenAmounts – Amounts of outcome tokens to buy from the market. If an amount is negative, represents an amount to sell to the market.
Return:

Net cost of trade. If positive, represents amount of collateral which would be paid to the market for the trade. If negative, represents amount of collateral which would be received from the market for the trade.

function calcCost(Market market, uint8 outcomeTokenIndex, uint outcomeTokenCount)
public
view
returns (uint cost)

Returns cost to buy given number of outcome tokens

Parameters:
  • market – Market contract
  • outcomeTokenIndex – Index of outcome to buy
  • outcomeTokenCount – Number of outcome tokens to buy
Return:

Cost

function calcProfit(Market market, uint8 outcomeTokenIndex, uint outcomeTokenCount)
public
view
returns (uint profit)

Returns profit for selling given number of outcome tokens

Parameters:
  • market – Market contract
  • outcomeTokenIndex – Index of outcome to sell
  • outcomeTokenCount – Number of outcome tokens to sell
Return:

Profit

function calcMarginalPrice(Market market, uint8 outcomeTokenIndex)
public
view
returns (uint price)

Returns marginal price of an outcome

Parameters:
  • market – Market contract
  • outcomeTokenIndex – Index of outcome to determine marginal price of
Return:

Marginal price of an outcome as a fixed point number

function calcCostLevel(int logN, int[] memory netOutcomeTokensSold, uint funding, Fixed192x64Math.EstimationMode estimationMode)
private
pure
returns (int costLevel)

Calculates the result of the LMSR cost function which is used to derive prices from the market state

Parameters:
  • logN – Logarithm of the number of outcomes
  • netOutcomeTokensSold – Net outcome tokens sold by market
  • funding – Initial funding for market
Return:

Cost level

function sumExpOffset(int logN, int[] memory netOutcomeTokensSold, uint funding, uint8 outcomeIndex, Fixed192x64Math.EstimationMode estimationMode)
private
pure
returns (uint sum, int offset, uint outcomeExpTerm)

Calculates sum(exp(q/b - offset) for q in quantities), where offset is set so that the sum fits in 248-256 bits

Parameters:
  • logN – Logarithm of the number of outcomes
  • netOutcomeTokensSold – Net outcome tokens sold by market
  • funding – Initial funding for market
  • outcomeIndex – Index of exponential term to extract (for use by marginal price function)
Return:

A result structure composed of the sum, the offset used, and the summand associated with the supplied index

function getNetOutcomeTokensSold(Market market)
private
view
returns (int[] memory quantities)

Gets net outcome tokens sold by market. Since all sets of outcome tokens are backed by corresponding collateral tokens, the net quantity of a token sold by the market is the number of collateral tokens (which is the same as the number of outcome tokens the market created) subtracted by the quantity of that token held by the market.

Parameters:
  • market – Market contract
Return:

Net outcome tokens sold by market