# Custom Somnia Mainnet APIs and analytics

Human-readable page: https://envio.dev/chains/somnia

An EVM-compatible layer 1 blockchain designed for real-time consumer applications such as games, social apps and metaverses. (Source: https://docs.somnia.network/)

## Chain facts

- Chain ID: 5031
- Native token: Somnia Mainnet (SOMI)
- Network: Mainnet
- HyperSync endpoint: https://somnia.hypersync.xyz
- HyperRPC endpoint: https://somnia.rpc.hypersync.xyz (read-only)
- Block explorer: https://mainnet.somnia.w3us.site
- Official site: https://somnia.network

## Support

First-class: HyperIndex, HyperSync and HyperRPC all support Somnia Mainnet.

HyperSync and HyperRPC need an API token; requests without one are rejected with HTTP 401. Create one at https://envio.dev/app/api-tokens. Indexers deployed to Envio Cloud have their own access and need no token. HyperRPC takes the token on the end of the URL (for example https://eth.rpc.hypersync.xyz/<api-token>). See https://docs.envio.dev/docs/HyperSync/api-tokens.md

## HyperIndex

Index Somnia Mainnet contract events into a GraphQL API, with backfill and live events in one pipeline.

1. Run `pnpx envio init` to scaffold a HyperIndex project. Give it your Envio API token and it saves it to `.env` (`ENVIO_API_TOKEN`), so local runs can read Somnia Mainnet from HyperSync. Indexers deployed to Envio Cloud need none.
2. List Somnia Mainnet under `chains` in config.yaml. HyperSync is the data source by default, so no RPC URL is needed.
3. Run it with `pnpm dev` (needs Docker). It opens the local Hasura dashboard with the indexed data.

```yaml
name: my-indexer
contracts:
  - name: MyContract
    events:
      - event: "Transfer(address indexed from, address indexed to, uint256 value)"
chains:
  - id: 5031 # Somnia Mainnet
    start_block: 0
    contracts:
      - name: MyContract
        address: "0x0000000000000000000000000000000000000000"
```

## HyperSync

Read raw Somnia Mainnet logs, transactions and blocks in bulk, selecting only the fields you need. Endpoint: https://somnia.hypersync.xyz

TypeScript (`npm install @envio-dev/hypersync-client`):

```typescript
import { HypersyncClient } from "@envio-dev/hypersync-client";

const client = new HypersyncClient({
  url: "https://somnia.hypersync.xyz",
  apiToken: process.env.ENVIO_API_TOKEN!,
});

// ERC-20 and ERC-721 Transfer events from the last 10 blocks.
const height = await client.getHeight();
const res = await client.get({
  fromBlock: height - 10,
  logs: [{ topics: [["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"]] }],
  fieldSelection: { log: ["BlockNumber", "Address", "Topic1", "Topic2"] },
});
console.log(res.data.logs.length, "Transfer events in the last 10 blocks");
```

Python (`pip install hypersync`):

```python
import asyncio
import os

import hypersync
from hypersync import ClientConfig, FieldSelection, LogField, LogSelection, Query

TRANSFER = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"


async def main():
    client = hypersync.HypersyncClient(
        ClientConfig(url="https://somnia.hypersync.xyz", api_token=os.environ["ENVIO_API_TOKEN"])
    )
    # ERC-20 and ERC-721 Transfer events from the last 10 blocks.
    height = await client.get_height()
    res = await client.get(
        Query(
            from_block=height - 10,
            logs=[LogSelection(topics=[[TRANSFER]])],
            field_selection=FieldSelection(
                log=[LogField.BLOCK_NUMBER, LogField.ADDRESS, LogField.TOPIC1, LogField.TOPIC2]
            ),
        )
    )
    print(len(res.data.logs), "Transfer events in the last 10 blocks")


asyncio.run(main())
```

## HyperRPC

A read-only JSON-RPC endpoint for existing RPC code: https://somnia.rpc.hypersync.xyz. Add your token to the end of the URL. It serves chain, block, transaction, receipt and log methods such as eth_getLogs, and cannot send transactions.

```bash
curl https://somnia.rpc.hypersync.xyz/$ENVIO_API_TOKEN \
  -X POST -H "Content-Type: application/json" \
  -d '{"jsonrpc":"2.0","id":1,"method":"eth_blockNumber","params":[]}'
```

## More

- All supported chains: https://envio.dev/chains.md
- Envio docs for AI agents: https://envio.dev/llms.txt
