dojo-indexer

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Dojo Indexer (Torii)

Dojo索引器(Torii)

Set up and use Torii, the Dojo indexer, for efficient querying and real-time subscriptions to your world state.
设置并使用Dojo索引器Torii,以高效查询和实时订阅你的World状态。

When to Use This Skill

何时使用该技能

  • "Set up Torii indexer"
  • "Configure GraphQL for my world"
  • "Create subscriptions for entity updates"
  • "Query world state efficiently"
  • "设置Torii索引器"
  • "为我的World配置GraphQL"
  • "创建实体更新订阅"
  • "高效查询World状态"

What This Skill Does

该技能的作用

Manages Torii indexer:
  • Start and configure Torii
  • Create GraphQL queries
  • Set up real-time subscriptions
  • Access SQL database directly
管理Torii索引器:
  • 启动并配置Torii
  • 创建GraphQL查询
  • 设置实时订阅
  • 直接访问SQL数据库

Quick Start

快速开始

Start Torii:
bash
torii --world <WORLD_ADDRESS>
This starts Torii with default settings:
  • GraphQL API at
    http://localhost:8080/graphql
  • gRPC API at
    http://localhost:8080
  • In-memory database (for development)
With Controller indexing (recommended):
bash
torii --world <WORLD_ADDRESS> --indexing.controllers
Production configuration:
bash
torii --world <WORLD_ADDRESS> --db-dir ./torii-db --indexing.controllers
启动Torii:
bash
torii --world <WORLD_ADDRESS>
此命令将使用默认设置启动Torii:
  • GraphQL API 地址:
    http://localhost:8080/graphql
  • gRPC API 地址:
    http://localhost:8080
  • 内存数据库(适用于开发环境)
使用控制器索引(推荐):
bash
torii --world <WORLD_ADDRESS> --indexing.controllers
生产环境配置:
bash
torii --world <WORLD_ADDRESS> --db-dir ./torii-db --indexing.controllers

What is Torii?

什么是Torii?

Torii is the Dojo indexer that:
  • Watches blockchain for world events
  • Indexes model state changes
  • Provides GraphQL API for queries
  • Provides gRPC API for subscriptions
  • Offers SQL access for complex queries
Why use Torii:
  • Faster than direct RPC queries
  • Complex queries (filters, pagination)
  • Real-time subscriptions
  • Type-safe GraphQL schema
Torii是Dojo的索引器,具备以下功能:
  • 监控区块链上的World事件
  • 索引模型状态变更
  • 提供GraphQL API用于查询
  • 提供gRPC API用于订阅
  • 支持SQL访问以进行复杂查询
为什么使用Torii:
  • 比直接RPC查询更快
  • 支持复杂查询(过滤、分页)
  • 实时订阅功能
  • 类型安全的GraphQL schema

GraphQL API

GraphQL API

Torii provides GraphQL endpoint at
http://localhost:8080/graphql
Use the GraphiQL IDE in your browser to explore the schema and test queries.
Torii提供的GraphQL端点地址为
http://localhost:8080/graphql
你可以在浏览器中使用GraphiQL IDE来探索schema并测试查询。

Schema Structure

Schema结构

Torii generates two types of queries:
Generic Queries:
  • entities
    - Access all entities with filtering
  • models
    - Retrieve model definitions
  • transactions
    - Query indexed transactions
Model-Specific Queries:
  • {modelName}Models
    - Custom queries for each model
  • Example:
    positionModels
    ,
    movesModels
Torii会生成两种类型的查询:
通用查询:
  • entities
    - 访问所有实体并支持过滤
  • models
    - 获取模型定义
  • transactions
    - 查询已索引的交易
模型特定查询:
  • {modelName}Models
    - 为每个模型定制的查询
  • 示例:
    positionModels
    ,
    movesModels

Basic Queries

基础查询

Get all entities of a model:
graphql
query {
    movesModels {
        edges {
            node {
                player
                remaining
                last_direction
            }
        }
    }
}
Get model metadata:
graphql
query {
    models {
        edges {
            node {
                id
                name
                classHash
                contractAddress
            }
        }
        totalCount
    }
}
获取某个模型的所有实体:
graphql
query {
    movesModels {
        edges {
            node {
                player
                remaining
                last_direction
            }
        }
    }
}
获取元数据模型:
graphql
query {
    models {
        edges {
            node {
                id
                name
                classHash
                contractAddress
            }
        }
        totalCount
    }
}

Pagination

分页

Cursor-based pagination:
graphql
query {
    entities(first: 10) {
        edges {
            cursor
            node {
                id
            }
        }
        pageInfo {
            hasNextPage
            endCursor
        }
    }
}
Get next page:
graphql
query {
    entities(first: 10, after: "cursor_value") {
        edges {
            cursor
            node { id }
        }
    }
}
Offset/limit pagination:
graphql
query {
    entities(offset: 20, limit: 10) {
        edges {
            node { id }
        }
        totalCount
    }
}
基于游标分页:
graphql
query {
    entities(first: 10) {
        edges {
            cursor
            node {
                id
            }
        }
        pageInfo {
            hasNextPage
            endCursor
        }
    }
}
获取下一页:
graphql
query {
    entities(first: 10, after: "cursor_value") {
        edges {
            cursor
            node { id }
        }
    }
}
基于偏移量/限制数分页:
graphql
query {
    entities(offset: 20, limit: 10) {
        edges {
            node { id }
        }
        totalCount
    }
}

Real-time Subscriptions

实时订阅

Subscribe to world state changes via WebSocket.
通过WebSocket订阅World状态变更。

Entity Updates

实体更新

graphql
subscription {
    entityUpdated(id: "0x54f58...") {
        id
        updatedAt
        models {
            __typename
            ... on Position {
                vec {
                    x
                    y
                }
            }
            ... on Moves {
                remaining
            }
        }
    }
}
graphql
subscription {
    entityUpdated(id: "0x54f58...") {
        id
        updatedAt
        models {
            __typename
            ... on Position {
                vec {
                    x
                    y
                }
            }
            ... on Moves {
                remaining
            }
        }
    }
}

Event Stream

事件流

Monitor all world events:
graphql
subscription {
    eventEmitted {
        id
        keys
        data
        transactionHash
    }
}
监控所有World事件:
graphql
subscription {
    eventEmitted {
        id
        keys
        data
        transactionHash
    }
}

Model Registration

模型注册

Listen for new model registrations:
graphql
subscription {
    modelRegistered {
        id
        name
        namespace
    }
}
监听新模型的注册:
graphql
subscription {
    modelRegistered {
        id
        name
        namespace
    }
}

SQL Access

SQL访问

Torii stores data in SQLite, accessible for complex queries.
Connect to database:
bash
sqlite3 torii.db
Example queries:
sql
-- Count entities
SELECT COUNT(*) FROM entities;

-- Custom aggregations
SELECT AVG(value) FROM model_data WHERE model_name = 'Health';
Torii将数据存储在SQLite中,你可以直接访问以进行复杂查询。
连接数据库:
bash
sqlite3 torii.db
查询示例:
sql
-- 统计实体数量
SELECT COUNT(*) FROM entities;

-- 自定义聚合查询
SELECT AVG(value) FROM model_data WHERE model_name = 'Health';

Client Integration

客户端集成

JavaScript/TypeScript

JavaScript/TypeScript

typescript
import { createClient } from '@dojoengine/torii-client';

const client = await createClient({
    rpcUrl: "http://localhost:5050",
    toriiUrl: "http://localhost:8080",
    worldAddress: WORLD_ADDRESS,
});

// Query entities
const positions = await client.getEntities({
    model: "Position",
    limit: 10
});

// Subscribe to updates
await client.onEntityUpdated(
    [{ model: "Position", keys: [playerId] }],
    (entity) => console.log("Position updated:", entity)
);
typescript
import { createClient } from '@dojoengine/torii-client';

const client = await createClient({
    rpcUrl: "http://localhost:5050",
    toriiUrl: "http://localhost:8080",
    worldAddress: WORLD_ADDRESS,
});

// 查询实体
const positions = await client.getEntities({
    model: "Position",
    limit: 10
});

// 订阅更新
await client.onEntityUpdated(
    [{ model: "Position", keys: [playerId] }],
    (entity) => console.log("Position updated:", entity)
);

Apollo Client (GraphQL)

Apollo Client(GraphQL)

typescript
import { ApolloClient, InMemoryCache, gql } from '@apollo/client';

const client = new ApolloClient({
    uri: 'http://localhost:8080/graphql',
    cache: new InMemoryCache(),
});

const { data } = await client.query({
    query: gql`
        query GetMoves {
            movesModels {
                edges {
                    node {
                        player
                        remaining
                    }
                }
            }
        }
    `
});
typescript
import { ApolloClient, InMemoryCache, gql } from '@apollo/client';

const client = new ApolloClient({
    uri: 'http://localhost:8080/graphql',
    cache: new InMemoryCache(),
});

const { data } = await client.query({
    query: gql`
        query GetMoves {
            movesModels {
                edges {
                    node {
                        player
                        remaining
                    }
                }
            }
        }
    `
});

Configuration Options

配置选项

OptionDescriptionDefault
--world
World contract addressOptional (since Torii 1.6.0)
--rpc
RPC endpoint URL
http://localhost:5050
--db-dir
Database directoryIn-memory
--config
Path to TOML configuration fileNone
--http.cors_origins
CORS origins
*
选项描述默认值
--world
World合约地址可选(Torii 1.6.0及以上版本)
--rpc
RPC端点URL
http://localhost:5050
--db-dir
数据库目录内存数据库
--config
TOML配置文件路径
--http.cors_origins
CORS允许的源
*

Slot Deployment (Remote)

Slot部署(远程)

Slot provides hosted Torii instances. Slot requires a TOML configuration file.
Slot提供托管的Torii实例。使用Slot需要一个TOML配置文件。

Create Configuration

创建配置文件

toml
undefined
toml
undefined

torii.toml

torii.toml

world_address = "<WORLD_ADDRESS>" rpc = "<RPC_URL>"
[indexing] controllers = true

See the [Torii configuration guide](/toolchain/torii/configuration) for all TOML options (indexing, polling, namespaces, etc.).
world_address = "<WORLD_ADDRESS>" rpc = "<RPC_URL>"
[indexing] controllers = true

查看[Torii配置指南](/toolchain/torii/configuration)以获取所有TOML配置选项(索引、轮询、命名空间等)。

Deploy

部署

bash
slot auth login

slot deployments create <PROJECT_NAME> torii --config torii.toml --version <DOJO_VERSION>
bash
slot auth login

slot deployments create <PROJECT_NAME> torii --config torii.toml --version <DOJO_VERSION>

Manage

管理

bash
undefined
bash
undefined

Stream logs

流式查看日志

slot deployments logs <PROJECT_NAME> torii -f
slot deployments logs <PROJECT_NAME> torii -f

Delete and recreate (safe — all data is on-chain)

删除并重新创建(安全操作——所有数据都存储在链上)

slot deployments delete <PROJECT_NAME> torii
undefined
slot deployments delete <PROJECT_NAME> torii
undefined

Development Workflow

开发工作流

Terminal 1: Start Katana
bash
katana --dev --dev.no-fee
Terminal 2: Deploy world
bash
sozo build && sozo migrate
Terminal 3: Start Torii
bash
torii --world <WORLD_ADDRESS> --http.cors_origins "*"
终端1:启动Katana
bash
katana --dev --dev.no-fee
终端2:部署World
bash
sozo build && sozo migrate
终端3:启动Torii
bash
torii --world <WORLD_ADDRESS> --http.cors_origins "*"

Troubleshooting

故障排除

"Connection refused"

"连接被拒绝"

  • Check Torii is running
  • Verify port (default 8080)
  • Check firewall rules
  • 检查Torii是否正在运行
  • 验证端口(默认8080)
  • 检查防火墙规则

"World not found"

"World未找到"

  • Verify world address is correct
  • Check RPC URL is accessible
  • Ensure world is deployed
  • 确认World地址正确
  • 检查RPC URL是否可访问
  • 确保World已部署

"Slow queries"

"查询缓慢"

  • Use model-specific queries instead of generic
    entities
  • Use pagination
  • Request only needed fields
  • 使用模型特定查询而非通用的
    entities
    查询
  • 使用分页功能
  • 仅请求所需字段

Next Steps

后续步骤

After Torii setup:
  1. Integrate with client (
    dojo-client
    skill)
  2. Create optimized queries
  3. Set up subscriptions
  4. Monitor performance
完成Torii设置后:
  1. 与客户端集成(使用
    dojo-client
    技能)
  2. 创建优化后的查询
  3. 设置订阅
  4. 监控性能

Related Skills

相关技能

  • dojo-deploy: Deploy world first
  • dojo-client: Use Torii in clients
  • dojo-world: Configure what Torii indexes
  • dojo-migrate: Restart Torii after migrations
  • dojo-deploy:先部署World
  • dojo-client:在客户端中使用Torii
  • dojo-world:配置Torii的索引内容
  • dojo-migrate:迁移后重启Torii