opentonapi
repository·master·Indexed 19 days ago
https://github.com/tonkeeper/opentonapiAn open-source blockchain indexer and API compatible with TonAPI. It provides high-level abstractions for TON entities such as Jettons and NFTs, as well as access to accounts, transactions, and traces, simplifying the development of TON-based applications.
What's inside opentonapi
- Opentonapi is an open-source, 100% compatible version of TonAPI. It simplifies TON-based application development by providing an API centered around high-level blockchain concepts such as Jettons and NFTs, while still allowing access to low-level details. Unlike the standard TonAPI, Opentonapi acts as an indexer for the TON blockchain, providing information about entities including Jettons, NFTs, accounts, transactions, and traces.
Understand the project structure and core packages
masterThe project is organized into several key directories that define its functionality. If you are looking to integrate with or extend specific features, use the following mapping:
api/: Contains OpenAPI configuration files used byogento generate API interfaces.cmd/api/main.go: The main entry point for executing the application.internal/: Contains private utility functions and helpers.pkg/: The location of all core, functional packages.ogen.yaml: The OpenAPI configuration for code generation.
Core functionality is located in the
pkg/directory. Key packages include:addressbook: Manages known addresses, jettons, NFT collections, and manual configurations.api: Implements the business logic for API endpoints (e.g., querying addresses, transactions).blockchain: Handles transaction payloads to lite servers and real-time block indexing.config: Manages application configuration via environment variables.gasless: Manages gasless transaction operations.spam: Filters spam/scam actions for TON, Jetton, and NFT transfers.rates: Fetches and converts market prices for TON tokens.
How the project generates API code using Ogen
masterThe project uses
OpenAPI Generator(ogen) to generate Go code from OpenAPI v3 specifications.- The configuration for this process is defined in
ogen.yaml. - The API-related configuration files are stored in the
api/directory. - The
gen.gofile is used to trigger the code generation based on the OGEN specification.
- The configuration for this process is defined in
How LITE_SERVERS and historical data work
masterThe
LITE_SERVERSenvironment variable accepts a comma-separated list of servers in the formatip:port:public-key.Important Note on Historical Data: If you do not explicitly set
LITE_SERVERS, the application defaults to using a random public Lite Server. By default, these are often Full nodes, which do not provide access to historical data. To access historical data, you must explicitly configure Archive nodes. You can find public Archive nodes in the official global-config.json.Get started with opentonapi
masterTo begin using the project, follow the setup and running instructions in the documentation to get the application running in your local environment. For a high-level overview of the project structure and its modular packages, refer to the structure guide.
1. Follow [02_SETUP_AND_RUN.md](./02_SETUP_AND_RUN.md) to set up and run the source code locally. 2. Refer to [03_STRUCTURE.md](./03_STRUCTURE.md) to understand the project structure and packages.Run OpenTonAPI from source with Go
masterTo run OpenTonAPI directly from the source code, you must have Go installed and provide the
libemulator.soshared library from the TON blockchain release repository.Steps:
- Ensure Go is installed (
go version). - Clone the repository and enter the directory.
- Download
libemulator.soand place it in a directory (e.g.,/app/lib). - Set the
LD_LIBRARY_PATHenvironment variable to point to that directory. - Execute the application using
go run cmd/api/main.go.
The application defaults to port
8081unless thePORTenvironment variable is set.# 1. Setup libemulator mkdir -p /app/lib wget -O /app/lib/libemulator.so https://github.com/ton-blockchain/ton/releases/download/v2024.08/libemulator-linux-x86_64.so # 2. Configure library path export LD_LIBRARY_PATH=/app/lib/ # 3. Run the application go run cmd/api/main.go # Or run with custom configuration PORT=8080 LOG_LEVEL=DEBUG go run cmd/api/main.go- Ensure Go is installed (
Run OpenTonAPI via Docker
masterTo run OpenTonAPI using Docker, clone the repository, build the image, and run the container. By default, the API is exposed on port
8081. You can verify the application is running by accessing the/v2/statusendpoint.Steps:
- Clone the repository:
git clone https://github.com/tonkeeper/opentonapi.git - Navigate to the directory:
cd opentonapi - Build the image:
docker build -t myopentonapi . - Run the container:
docker run -d -p 8081:8081 myopentonapi
You can also pass environment variables (like
LOG_LEVEL) using the-eflag.# Build the image docker build -t myopentonapi . # Run the container in the background on port 8081 docker run -d -p 8081:8081 myopentonapi # Run with a specific log level docker run -d -p 8081:8081 -e LOG_LEVEL=DEBUG myopentonapi- Clone the repository:
Run Opentonapi using Docker
masterYou can run Opentonapi as a containerized service using Docker. By default, it maps port 8081.
docker run -d -p8081:8081 tonkeeper/opentonapiEnable advanced features (Traces, NFTs, Jettons) by watching accounts
masterTo use advanced features such as traces, NFTs, and Jettons, you must configure the service to watch specific accounts. You can do this by passing a comma-separated list of raw account addresses to the
ACCOUNTSenvironment variable when running the application.ACCOUNTS="comma-separated-list-of-raw-account-addresses" make runUse the dedicated TonAPI Go SDK
masterThe Go SDK for TonAPI has been moved to a dedicated repository. To use the SDK in your Go projects, visit the new repository instead of this one:
Configure Opentonapi via environment variables
masterOpentonapi can be configured using the following environment variables:
Env variable Default value Description PORT8081Port used to accept incoming HTTP connections LOG_LEVELINFOLogging verbosity level LITE_SERVERS-A comma-separated list of TON lite servers in the format ip:port:public-key(e.g.,127.0.0.1:14395:6PGkPQSbyFp12esf1NqmDOaLoFA8i9+Mp5+cAx5wtTU=)METRICS_PORT9010Port used to expose the /metricsendpoint for PrometheusACCOUNTS-A comma-separated list of raw account addresses to watch Note: To enable advanced features like traces, NFTs, and Jettons, you must provide a list of accounts to watch using the
ACCOUNTSvariable.Configure Sentry via SENTRY_DSN environment variable
masterThe
sentrypackage automatically initializes itself using theSENTRY_DSNenvironment variable.- If
SENTRY_DSNis empty, the package remains uninitialized andSendcalls will do nothing. - If provided, it initializes with a
TracesSampleRateof1.0. - On initialization, it performs a
Flushwith a 2-second timeout.
- If