Aller au contenu

Using Substrate to Build Intent-Based State Machines

Ce contenu n’est pas encore disponible dans votre langue.

Prabal Banerjee - Avail Sub0

At the Sub0 Reset 2024 event, Jakub Panik, the CTO of Intergalactic and Hydration, presented an in-depth session on leveraging the Polkadot SDK and Substrate to build intent-based state machines. His talk focused on modifying the Polkadot SDK chain for specific use cases, optimizing parachain runtime efficiency, and integrating cross-chain capabilities. This article delves into the key aspects of his presentation, exploring how intent-based systems can revolutionize transaction execution and throughput in decentralized finance (DeFi) ecosystems.


Hydration: A DeFi Hub Built on Polkadot

Hydration is a DeFi hub that hosts the Omnipool and other automated market makers (AMMs). It allows users to:

  • Pay transaction fees in any asset.
  • Utilize swap-related automation like dollar-cost averaging (DCA).
  • Access Ethereum Virtual Machine (EVM) compatibility.
  • Engage with upcoming features like a money market (pending audits).

Despite being a standard Polkadot SDK chain, Hydration incorporates numerous plugins that enhance its functionality.


Defining the Problem Space

Jakub outlined several requirements to improve Hydration’s performance:

  1. Enhance Price Execution: Implement limit orders and swap triggers.
  2. Boost Transaction Throughput: Increase the number of transactions processed per second.
  3. Integrate Cross-Chain Capabilities: Enable seamless interoperability with other chains.
  4. Maintain System Integrity: Keep existing features operational and minimize breaking changes for developers.

Exploring Past Solutions

On-Chain Order Matching Engine

In August 2020, Jakub’s team developed an on-chain order matching engine during a hackathon. This engine allowed users to match opposing transactions within a single block, executing transfers or swaps without slippage. However, its slower processing compared to standard transactions led to its discontinuation.

Off-Chain Transaction Matching

Protocols like CoW Swap and Uniswap X introduced off-chain transaction matching, where matches are made off-chain, and solutions are posted on-chain for settlement. This approach inspired the development of an intent and solver-based system.


The Intent and Solver-Based System

Understanding Intents

An intent is a user-defined action specifying what they want to achieve without detailing how to execute it. Key features include:

  • Swap Intents: Currently focused on swaps but can extend to borrowing, adding, or removing liquidity.
  • Triggers: Actions executed upon success, failure, or partial execution of an intent.

Role of Solvers

Solvers observe the blockchain, collect intents from the previous block, and compose optimal solutions. They:

  • Group compatible intents into solving groups.
  • Construct instructions for executing swaps efficiently.
  • Submit solutions to the blockchain for validation and execution.

System Workflow

  1. Intent Collection: Users submit intents via Remote Procedure Calls (RPCs).
  2. Block Inclusion: Block builders include these intents in the blockchain.
  3. Solution Composition: Solvers extract intents, create solutions, and submit them to the next block.
  4. Validation and Execution: The blockchain validates solutions, and matched intents are settled through direct transfers, while unmatched ones fall back to existing pools like the Omnipool.

Technical Implementation

Calculating Transaction Size

The system calculates transaction weights based on the number of intents and the complexity of operations (transfers vs. swaps). This optimization results in:

  • Cheaper execution compared to traditional AMMs.
  • Scalability benefits as more intents are matched together.

Validating Solutions

Solutions are validated through a scoring system that considers:

  • Excess Aggregate Demand or Supply: Measures leftover assets after matching.
  • Optimality: Based on algorithms from research papers like Optimal Routing for Constant Function Market Makers and Outer Approximation.

Only the best solution per block is accepted, and solvers can be penalized (slashed) for submitting invalid solutions.

Custom Block Ordering

To accommodate the new system, the traditional block execution order is modified:

  1. Priority Execution: Solution execution and money market liquidations occur first.
  2. Custom Transaction Priorities: Transactions are ordered based on type rather than user nonce or weight.
  3. Non-Consecutive Nonces: Allows flexibility in transaction ordering within a block.

Cross-Chain Capabilities with ERC 7683

The adoption of ERC 7683, a cross-chain intent standard, enables:

  • Interoperability: Compatibility with over 30 protocols and chains.
  • Solver Networks: Access to existing solver infrastructures for efficient intent resolution.
  • Near-Instant Execution: Rapid cross-chain transactions for users.

How It Works

  1. Intent Submission: Users post intents on the source chain with escrow contracts.
  2. Solver Execution: Solvers settle intents on the destination chain.
  3. Proof and Reimbursement: Solvers provide proof of settlement to receive compensation, often facilitated by bridges.

Benefits and Outcomes

Implementing this intent-based system yields significant advantages:

  • Increased Throughput: Achieves a minimum 5x increase, supporting over 200 swaps per second.
  • Scalability: Can efficiently handle 10,000+ intents simultaneously.
  • Improved Price Execution: Offers swaps without slippage and enhanced market protection.
  • Cross-Chain Integration: Provides almost instant execution across different blockchain networks.
  • Automation Enhancements: Utilizes triggers for advanced automation capabilities in DeFi operations.

Conclusion

Jakub Panik’s presentation at Sub0 Reset 2024 showcased a transformative approach to building intent-based state machines using Substrate and the Polkadot SDK. By reimagining transaction execution through intents and solvers, Hydration significantly improves performance, scalability, and user experience in the DeFi space. The integration of cross-chain standards like ERC 7683 positions Hydration at the forefront of blockchain innovation, paving the way for more efficient and interconnected ecosystems.