MarketFactory

Market factory is used to create prediction markets of different types.

Market creation logic:

  1. Ask reality questions.

  2. Prepare condition.

  3. Create new erc20 outcome tokens.

  4. Initialize a new market contract.

Variables

CreateMarketParams

Workaround "stack too deep" errors.

struct CreateMarketParams {
  string marketName;
  string[] outcomes;
  string questionStart;
  string questionEnd;
  string outcomeType;
  uint256 parentOutcome;
  address parentMarket;
  string category;
  string lang;
  uint256 lowerBound;
  uint256 upperBound;
  uint256 minBond;
  uint32 openingTime;
  string[] tokenNames;
}

InternalMarketConfig

Workaround "stack too deep" errors.

struct InternalMarketConfig {
  string[] encodedQuestions;
  uint256 outcomeSlotCount;
  uint256 templateId;
}

REALITY_UINT_TEMPLATE

uint8 REALITY_UINT_TEMPLATE

Template for scalar and multi scalar markets.

REALITY_SINGLE_SELECT_TEMPLATE

uint8 REALITY_SINGLE_SELECT_TEMPLATE

Template for categorical markets.

REALITY_MULTI_SELECT_TEMPLATE

uint8 REALITY_MULTI_SELECT_TEMPLATE

Template for multi categorical markets.

questionTimeout

uint32 questionTimeout

Reality question timeout.

arbitrator

address arbitrator

Arbitrator contract.

realitio

contract IRealityETH_v3_0 realitio

Reality.eth contract.

wrapped1155Factory

contract IWrapped1155Factory wrapped1155Factory

Wrapped1155Factory contract.

conditionalTokens

contract IConditionalTokens conditionalTokens

Conditional Tokens contract.

collateralToken

address collateralToken

Conditional Tokens collateral token contract.

realityProxy

contract RealityProxy realityProxy

Oracle contract.

markets

address[] markets

Markets created by this factory.

market

address market

Market contract.

Events

NewMarket

event NewMarket(address market, string marketName, address parentMarket, bytes32 conditionId, bytes32 questionId, bytes32[] questionsIds)

To be emitted when a new market is created.

Parameters

Functions

constructor

constructor(address _market, address _arbitrator, contract IRealityETH_v3_0 _realitio, contract IWrapped1155Factory _wrapped1155Factory, contract IConditionalTokens _conditionalTokens, address _collateralToken, contract RealityProxy _realityProxy, uint32 _questionTimeout) public

Constructor.

createCategoricalMarket

function createCategoricalMarket(struct MarketFactory.CreateMarketParams params) external returns (address)

Categorical markets are associated with a Reality question that has only one answer.

Creates a Categorical market.

Parameters

Return Values

Logic

  1. Validate outcomes length (>= 2).

  2. Encode 1 reality question.

  3. Call createMarket.

createMultiCategoricalMarket

function createMultiCategoricalMarket(struct MarketFactory.CreateMarketParams params) external returns (address)

Multi Categorical markets are associated with a Reality question that has one or more answers.

Creates a Multi Categorical market.

Parameters

Return Values

Logic

  1. Validate outcomes length (>=2).

  2. Encode 1 reality question.

  3. Call createMarket.

createScalarMarket

function createScalarMarket(struct MarketFactory.CreateMarketParams params) external returns (address)

Scalar markets are associated with a Reality question that resolves to a numeric value.

Creates a Scalar market.

Parameters

Return Values

Logic

  1. Validate upperBound and lowerBound.

  2. Validate outcomes length (2).

  3. Encode 1 reality question.

  4. Call createMarket.

createMultiScalarMarket

function createMultiScalarMarket(struct MarketFactory.CreateMarketParams params) external returns (address)

Multi Scalar markets are associated with two or more Reality questions, and each one of them resolves to a numeric value.

Creates a Multi Scalar market.

Parameters

Return Values

Logic

  1. Validate outcomes length (>=2).

  2. Encode reality questions, one for each outcome.

  3. Instead of using params.marketName, encode a new marketName.

    abi.encodePacked(
        params.questionStart,
        "[",
        params.outcomeType,
        "]",
        params.questionEnd
    )
  4. Call createMarket.

createMarket

function createMarket(struct MarketFactory.CreateMarketParams params, string marketName, struct MarketFactory.InternalMarketConfig config) internal returns (address)

Creates the Market and deploys the wrapped ERC20 tokens.

Parameters

Return Values

Logic

  1. Ask reality questions.

  2. Prepare condition.

  3. Create new erc20 outcome tokens.

  4. Initialize a new market contract.

createNewMarketParams

function createNewMarketParams(struct MarketFactory.CreateMarketParams params, struct MarketFactory.InternalMarketConfig config) internal returns (struct Market.ConditionalTokensParams, struct Market.RealityParams)

Creates the structures needed to initialize the new market.

Parameters

Return Values

encodeRealityQuestionWithOutcomes

function encodeRealityQuestionWithOutcomes(string question, string[] outcomes, string category, string lang) internal pure returns (string)

Encodes the question, outcomes, category and language following the Reality structure. If any parameter has a special character like quotes, it must be properly escaped.

Parameters

Return Values

encodeRealityQuestionWithoutOutcomes

function encodeRealityQuestionWithoutOutcomes(string question, string category, string lang) internal pure returns (string)

Encodes the question, category and language following the Reality structure. If any parameter has a special character like quotes, it must be properly escaped.

Parameters

Return Values

askRealityQuestion

function askRealityQuestion(string encodedQuestion, uint256 templateId, uint32 openingTime, uint256 minBond) internal returns (bytes32)

Asks a question on reality.

Parameters

Return Values

prepareCondition

function prepareCondition(bytes32 questionId, uint256 outcomeSlotCount) internal returns (bytes32)

Prepares the CTF condition and returns the conditionId.

Parameters

Return Values

Pre-condition

  1. Reality questions asked and return questionsIds.

  2. questionId is hashed from all the values that RealityProxy.resolve() uses to resolve a market (questionsIds, outcomes.length, templateId, lowerBound, upperBound).

Logic

  1. It will check if there is a prepared condition on ConditionalTokens, and call conditionalTokens.prepareCondition() if there isn't any.

deployERC20Positions

function deployERC20Positions(bytes32 parentCollectionId, bytes32 conditionId, uint256 outcomeSlotCount, string[] tokenNames) internal returns (contract IERC20[] wrapped1155, bytes[] data)

Wraps the ERC1155 outcome tokens to ERC20. The INVALID_RESULT outcome is always called SER-INVALID.

Parameters

Return Values

Logic

  1. It will call wrapped1155Factory.requireWrapped1155 to create an ERC20 token contract for each position. The new tokens addresses and data is saved in Market contract.

toString31

function toString31(string value) internal pure returns (bytes32 encodedString)

Encodes a short string (less than than 31 bytes long) as for storage as expected by Solidity. See https://github.com/gnosis/1155-to-20/pull/4#discussion_r573630922.

Parameters

Return Values

allMarkets

function allMarkets() external view returns (address[])

Returns all the markets created by this factory.

Return Values

marketCount

function marketCount() external view returns (uint256)

Returns the amount of markets created by this factory.

Return Values

Last updated