Omniston Guide (Python)
Learn how to create a Python-based terminal client for swapping tokens on TON blockchain using Omniston protocol. Covers wallet setup, API integration, and cross-DEX swaps on STON.fi and DeDust.
This guide will walk you through creating a terminal-based token swap client using the Omniston protocol to swap assets across different DEXes (STON.fi V1, STON.fi V2, DeDust, etc.). Instead of a web UI and TonConnect, we'll use a local TON wallet (created with tonsdk) and submit transactions via Toncenter. The guide is beginner‑friendly and assumes minimal TON experience.
Note: This quickstart intentionally uses a single-file CLI for clarity. You can later modularize or package it (see Advanced Example App).
Note: For reliability when broadcasting transactions, set a TONCENTER_API_KEY (see Configure Assets & Network).
Table of Contents
1. Introduction
In this quickstart, you'll build a minimal Python CLI that can:
Create or reuse a local TON wallet (via
tonsdk).Load network and token settings from
.envandswap_config.json.Request an RFQ (quote) from Omniston over WebSockets.
Build a transfer using Omniston's transaction builder.
Submit the transaction to Toncenter to execute the swap.
You'll use:
tonsdk– wallet generation, signing, and BOCs.websockets– to communicate with Omniston.python-dotenv– to load environment variables.Toncenter API – to broadcast the signed transaction.
2. Setting Up the Project
2.1 Create the Workspace
2.2 Create the Virtual Environment
2.3 Install Dependencies
Create
requirements.txtand add:Install the packages:
Create a single-file CLI script:
3. Wallet Setup
The CLI persists a wallet in data/wallet.json and prints a mnemonic once—store it securely.
3.1 Generate or Load a Wallet
In this step you'll paste core definitions and the wallet helpers (kept at the top of omniston_cli.py).
Tip: The code blocks below are verbatim from the working implementation; paste them as-is and in the given order.
3.2 Fund and Deploy the Wallet
You must fund the address and deploy the wallet contract before sending a swap. The helpers below take care of querying Toncenter and submitting the init BOC when needed.
4. Configure Assets & Network
You'll define RPC endpoints and default swap pair locally.
4.1 Create the .env file
Create .env at the project root:
Getting a Toncenter API Key: Using the API without an API key is limited to 1 request per second. To get higher rate limits:
Contact @toncenter on Telegram
Request an API key for your project
Copy the key and paste it into your
.envfile
TONCENTER_API_KEYis required if you want to execute transactions. Without it, you'll face rate limiting issues and transaction broadcasting will fail.
4.2 Define the swap_config.json
Create swap_config.json:
Common token addresses:
Native TON:
EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9cUSDT:
EQCxE6mUtQJKFnGfaROTKOt1lZbDiiX1kCixRv7Nw2Id_sDsSTON:
EQA2kCVNwVsil2EM2mB0SkXytxCqQjS4mttjDpnXmwG9T6bO
gasless_mode accepts "GASLESS_SETTLEMENT_UNSPECIFIED", "GASLESS_SETTLEMENT_POSSIBLE", "GASLESS_SETTLEMENT_REQUIRED", or their numeric equivalents 0/1/2.
flexible_referrer_fee lets Omniston lower the actual referrer fee below referrer_fee_bps when a resolver can offer a better price. Leave it false to enforce the exact fee.
5. Implementing the CLI (Step‑by‑Step)
Open omniston_cli.py and paste the remaining helper blocks below exactly as shown.
5.1 Define Domain Types
5.2 Wallet Helpers
5.3 Toncenter Helpers
5.4 Quote Helpers
5.5 Transfer Helpers
5.6 Command Entry Point
6. Requesting a Quote
Run the CLI and follow the prompt:
When you accept "Request a quote now?", the CLI will:
Build a request using your configured pair and amount.
Connect to Omniston over WebSockets (v1beta7.quote).
Print a human‑readable summary (e.g., Swap 0.01 -> 12.34).
If a quote can't be produced in time (default: ~15s), you'll see a timeout or error message.
7. Building a Transaction & Sending It
After you approve the quote summary, the CLI:
Calls Omniston's transaction builder (v1beta7.transaction.build_transfer) to obtain TON messages.
Signs an external message with your wallet.
Submits the resulting BOC to Toncenter.
Important: Automatic submission requires TONCENTER_API_KEY in .env. Without it, the CLI skips broadcast.
8. Testing Your Swap
Activate your virtual environment and ensure
.envandswap_config.jsonexist.Fund your wallet and run:
Confirm:
Wallet is created/loaded and balance printed.
Deployment completes (or is already deployed).
A quote is received and summarized.
On approval, the transaction is submitted and you see "Swap submitted.".
Check your wallet on a TON explorer to confirm the swap.
If something fails, the CLI prints a clear message (e.g., timeout, missing API key, seqno issue).
9. Conclusion
You now have a minimal Python CLI that:
Generates or reuses a TON wallet.
Loads local configuration for assets and network.
Requests real‑time quotes from Omniston.
Builds and submits the swap via Toncenter.
Ideas to extend:
Custom CLI flags (amount/pair) with argparse.
Better error reporting and retry/backoff.
Explorer links after submission.
Trade tracking with periodic status checks.
10. Live Demo
Run the Omniston Python CLI directly in your browser via Replit:
Open the project in Replit
Fork to your account to save changes
Add a
TONCENTER_API_KEYin Replit SecretsRun
python omniston_cli.pyin the Replit shellExplore and modify the code freely
11. Using AI Agents for Automated Implementation
For developers looking to accelerate their development process, you can leverage AI coding agents to automatically implement the Omniston swap functionality described in this guide. While we showcase Gemini CLI in our example (due to its generous free tier), you can use any AI coding assistant such as Claude Code, GitHub Copilot, Cursor Agent, or similar tools.
11.1 Why AI Agents?
Modern AI coding agents can:
Understand complex documentation and implement complete features
Set up project structure and dependencies automatically
Handle common configuration and setup errors
Provide working implementations in minutes instead of hours
11.2 Setting Up with Gemini CLI (Example)
We'll demonstrate with Gemini CLI, but the approach works with any AI agent that can read documentation and execute commands.
11.2.1 Installing Gemini CLI
Install the Gemini CLI by following the instructions at: https://github.com/google-gemini/gemini-cli
Authenticate with your Google account when prompted. The free tier includes:
60 model requests per minute
1,000 model requests per day
11.2.2 Setting Up the Implementation Guide
Download the appropriate guide file from the gist: https://gist.github.com/mrruby/a6fba69716fc0d5b8eaafd43998b36c0
For Claude Code: Download
AGENTS.mdand rename it toCLAUDE.mdFor other AI agents (Gemini CLI, GitHub Copilot, Cursor, etc.): Use
AGENTS.mdas-is
Create a new directory for your project and place the guide file inside it:
11.2.3 Running the Automated Implementation
From within your project directory, run the Gemini CLI:
When the CLI interface opens, type:
The AI agent will:
Ask for permission to use commands like
python3,pip, etc.Automatically create the project structure
Install all necessary dependencies
Implement the complete swap functionality
Set up configuration files
Important: After the implementation completes, you must manually configure your Toncenter API key:
Go to TON Center and get your API key
Create a
.envfile in the root of your projectAdd your API key:
TONCENTER_API_KEY=your_api_key_hereAI agents cannot handle this step as it requires your personal API credentials
If any errors occur during the process:
Simply paste the error message back to the AI agent
It will analyze and fix the issue automatically
In most cases, the implementation completes successfully in one shot
11.3 Using Other AI Agents
The same approach works with other AI coding assistants:
Claude Code: Download
AGENTS.mdand rename it toCLAUDE.md, then place it in your project and ask Claude Code to implement the guideGitHub Copilot: Use the
AGENTS.mdfile as-is, open the guide in your editor and use Copilot Chat to implement step by stepCursor Agent: Use the
AGENTS.mdfile as-is, load the documentation and request full implementation via Cursor's agent modeCustom Tools: Any AI assistant with file access and command execution capabilities can follow the guide using the
AGENTS.mdfile
Note: The
AGENTS.mdfile contains instructions compatible with all AI agents (Gemini CLI, GitHub Copilot, Cursor, etc.). However, Claude Code requires the file to be namedCLAUDE.mdto be automatically recognized, so you must renameAGENTS.mdtoCLAUDE.mdwhen using Claude Code.
11.4 Benefits of Using AI Agents
Speed: Get a working implementation in minutes instead of hours
Accuracy: AI agents follow the quickstart guide precisely
Error Handling: Automatically resolve most common setup issues
Learning Tool: Watch how the implementation unfolds step by step
Customization: After the initial setup, you can modify the generated code to fit your specific needs
Cost-Effective: Many AI coding tools offer free tiers (like Gemini CLI) or are included in existing subscriptions
11.5 Best Practices
Review the Code: Always review AI-generated code before using it in production
Understand the Flow: Use the generated code as a learning tool to understand the Omniston protocol
Customize: Adapt the generated code to your specific requirements
Test Thoroughly: Test the implementation with small amounts before processing larger transactions
Security: Never commit API keys or mnemonics to version control
This approach is particularly useful for:
Developers new to the TON ecosystem
Quick prototyping and proof-of-concepts
Learning by example with a working implementation
Avoiding common setup pitfalls and configuration errors
Exploring different implementation approaches rapidly
Happy swapping!
Last updated