> For the complete documentation index, see [llms.txt](https://helena-network.gitbook.io/trl/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://helena-network.gitbook.io/trl/javascript-api/interacting-with-markets.md).

# Market interaction

### 1. Instantiating&#x20;

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

```javascript
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](https://github.com/gnosis/pm-contracts/blob/v1.1.0/contracts/Markets/StandardMarket.sol). There are also more advanced functions that we will explain later (e.g buy and sell shares).

### 2. Market&#x20;

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:

```javascript
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).

```javascript
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:

```javascript
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:

```javascript
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).

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

###


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://helena-network.gitbook.io/trl/javascript-api/interacting-with-markets.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
