Cross-Chain Operations
This guide provides a deeper dive into cross-chain operations with the TAC SDK, covering advanced scenarios and best practices for building robust hybrid dApps.
Cross-chain operations are the core of TAC’s functionality, allowing developers to create seamless interactions between TON and EVM environments. Understanding how these operations work “under the hood” will help you build more efficient and reliable applications.
Understanding the Cross-Chain Flow
When a cross-chain operation is initiated, it follows this general flow:
- Initiation on TON: Transaction begins on TON, with data and/or assets
- Sequencer Processing: TON Adapter’s sequencer network validates and processes the message
- Consensus Formation: Sequencers form Merkle trees and reach consensus
- EVM Execution: The validated message is executed on the TAC EVM side
- Return Path (Optional): Results or assets may be returned to TON
Sharded Messages
Due to TON’s architecture, it’s not possible to send multiple tokens in a single transaction. TAC solves this with a sharded messaging system.
How Sharded Messages Work
When you need to send multiple tokens (like when adding liquidity to a DEX), each token is sent as a separate transaction on TON. The TAC SDK and sequencer network handle linking these transactions together:
Transaction Linking
These separate transactions are linked using a triplet of identifiers:
caller
: The address initiating the transactionshardsKey
: A unique identifier for the set of sharded messagesshardCount
: The total number of shards in the transaction
The TransactionLinker
object returned by sendCrossChainTransaction
contains this information and is used for tracking the status of the entire operation.
TAC Headers and Proxy Contracts
Every cross-chain message includes a TAC header, which contains essential metadata:
Proxy Contract Interface
The target contract on the EVM side must implement a specific interface to receive cross-chain messages:
The TAC SDK handles the encoding of your parameters into the arguments parameter. You only need to encode the data specific to your contract method.
Advanced Message Types
One-Way Messages (TON to TAC)
Simple messages that only go from TON to TAC without expecting a return:
Round-Trip Messages (TON to TAC to TON)
Messages that initiate on TON, execute on TAC, and then return results to TON:
The proxy contract on the EVM side determines whether a message is one-way or round-trip based on whether it creates a return message to TON.
Message Types by Operation
Different operations use different message patterns
Operation Type | Flow | Description |
---|---|---|
TON_TAC | One-way | Simple transfer from TON to TAC |
TON_TAC_TON | Round-trip | Complete cycle: TON → TAC → TON |
ROLLBACK | Failed | Transaction failed and assets rolled back |
TAC_TON | Return | Assets returned from TAC to TON |
You can check the operation type using the tracker:
Gas Management
Cross-chain operations involve gas costs on both chains:
- TON Gas: Paid for the TON-side transaction in TON tokens
- TAC Gas: Paid for the EVM-side execution in TAC tokens
The SDK can automatically estimate the gas required for EVM execution:
Performance Considerations
Cross-chain operations involve multiple blockchains and validation steps, which affects performance:
- Latency: Cross-chain operations take longer than single-chain transactions
- Message Size: There are limits to how much data can be sent in a single message
- State Dependencies: Applications requiring rapid state updates across chains need careful design
Optimizing Performance
To optimize performance:
- Minimize Round Trips: Design operations to require minimal back-and-forth between chains
- Batch Operations: Group related operations when possible
- Asynchronous UX: Design your UI to handle the asynchronous nature of cross-chain operations
- Status Tracking: Implement robust status tracking to keep users informed
Security Considerations
Cross-chain operations introduce unique security considerations:
- Asset Security: Ensure proper handling of assets during bridging
- Error Recovery: Implement robust error handling and recovery mechanisms
- Input Validation: Validate all inputs on both chains
- Transaction Timing: Be aware of the potential for timing-related issues
Handling Failed Transactions
If a transaction fails on the EVM side, the TON Adapter automatically handles the rollback of assets to the TON side. However, your application should still provide appropriate feedback to users:
Real-World Complex Example: DEX Aggregator
Here’s a more complex example showing how to create a DEX aggregator that finds the best price across multiple DEXes:
Was this page helpful?