Market interaction

This page will make a brief example of how to interact with an already available Helena Market, where we will bet on YES outcome, 1 Proton in a categorical market.

1. Instantiating

Instantiating a Market is the first step to interact with Helena Markets. Given a well known Helena Market address, it is easily accessible via:

const market = gnosis.contracts.Market.at("0x858c01c4db1b9f4baa7ebc8e14b84138a3f7d207")

For reference, all contract instances, will have the contract functions (for both read and write operations) you can check which ones in the contract source. There are also more advanced functions that we will explain later (e.g buy and sell shares).

2. Market

Every market has an easy to access endpoint on our REST API. In the case of our previously defined market, market information can be found at:

const getMarketInfo = async (address) => {
    const response = await fetch(`https://olympia-api.helena.network/api/markets/${address}/`)
    const json = await response.json();
    console.log(json);
} 
const marketInfo = await getMarketInfo('0x858c01c4db1b9f4baa7ebc8e14b84138a3f7d207') 

3. Price Calculation

First of all, you will probably have an amount of Protons you will want to allocate to a prediction market. In order to buy outcomes and interact with the Smart Contract, you will need to know in advance the amount of protons you want to buy. This is possible via calcLMSROutcomeTokenCount` function. Let's say we want to bet on YES outcome (outcome index 0).

const getOutcomeTokenCount = async (address) => {
    const= Gnosis.calcLMSRCost(marketInfo.netOutcomeTokensSold,
     marketInfo.funding, 
     0, 
     1e18,
     0)
    return outcomeTokenCount
}

4. Buy Outcomes

Let's say now that you've decided that these outcome tokens are worth purchasing. To buy these outcome tokens, you can use the following code:

await gnosis.buyOutcomeTokens({
        market,
        outcomeTokenIndex: 0,
        outcomeTokenCount: outcomeTokenCount,
 })
console.info('Bought 1 Outcome Token of Outcome with index 0')

5. Sell Outcomes

f you want to sell the outcome tokens you have bought, you can do the following:

async function sellOutcomeTokens() {
    await gnosis.sellOutcomeTokens({
        market,
        outcomeTokenIndex: 0,
        outcomeTokenCount: 1e18,
    })
}

6. Accessing Market Probabilities

First of all, you will probably have an amount of Protons you will want to allocate to a prediction market. In order to buy outcomes and interact with the Smart Contract, you will need to know in advance the amount of protons you want to buy. This is possible via calcLMSROutcomeTokenCount` function. Let's say we want to bet on YES outcome (outcome index 0).

let outcomeTokenCount = await gnosis.calcLMSROutcomeTokenCount (){
        marketInfo.netOutcomeTokensSold,
        marketInfo.funding,
        0,
        1e18,
    })
}

Last updated

Was this helpful?