Introducing the iFi DEX
If you have already read the previous blog posts on Althea L1’s design and scalability, you’ve likely noticed a recurring theme: Althea L1 is built for practical, robust problem-solving, with a focus on meeting real-world customer needs today.
Althea L1 is focused on Infrastructure Finance (iFi), a new category of blockchain applications that combines concepts from DeFi (Decentralized Finance), Physical Infrastructure, and RWA (on-chain Real World Assets).
Like most innovations, iFi builds on what came before it. One key foundational piece is a decentralized exchange (DEX)—essential for Althea L1 users to buy gas for EVM interactions, swap stablecoins for iFi transactions, and participate in DeFi built around iFi.
To power this, Ambient Finance was selected as a base due to its combination of concentrated liquidity, incredible feature density, and copyleft licensing. But we didn’t just stop there—we made major improvements to ensure the DEX fully aligns with Althea L1’s standards for reliability, decentralization, and performance.
This series of blog posts will introduce the iFi DEX. Althea L1’s canonical DEX. We’ve made significant changes to the Ambient base, focused on providing an experience that compliments iFi and meets the Althea ecosystem standard for reliability and decentralization.
Supercharging the DEX Backend
This first blog post in the series focuses on the Ambient graphcache-go backend and our work to create a drop-in replacement that syncs 1000x faster and uses 1000x less disk space. Every DEX interaction—whether querying a pool, performing a swap, or adding/removing liquidity—relies on the backend for data. Ambient Finance’s strength lies in its feature density and efficiency, but that also adds backend complexity. We tackled this challenge head-on, ensuring the iFi DEX backend is not only ultra-fast and scalable but also operationally seamless for users and developers alike.
Phenomenal Cosmic Power, Itty-Bitty Living Space
Ambient is a single-contract DEX, meaning all the pools and functionality exist as a single contract. Unlike decentralized exchanges such as Uniswap v2 where each pool is its own contract, Ambient pools exist as structured data in one giant contract. As deployed, the main Ambient contract is 23.824KB in size, just shy of the Solidity contract size limit of 24KB. This size does not include ‘sidecar’ contracts used to move code out of the main contract. Events are costly in terms of contract size. While the number of lines it may take to describe events is small, adding enough events to describe the full state of swaps and LP actions on the DEX would add roughly 1KB of storage to the main contract. Furthermore, this 1KB cannot be moved to sidecar contracts without resulting in a confusing experience where event consumers have to track events across multiple contracts.
Because of this, the canonical Ambient backend does not use events at all. Instead, it leverages an incredible level of EVM introspection provided by a Graph Protocol subgraph and a custom caching layer called graphcache-go. The subgraph doesn’t read the status of the DEX, it infers behavior directly from transaction stack traces and generates pseudo-events through a method much like forensic analysis of closed-source contracts. From this introspection, the graphcache-go backend can quickly and simply index the DEX history, simulate transactions to query live state, and generate up-to-date trading information. Additionally, graphcache-go stores historical information and serves most of its data as of a previous block upon request.This level of introspection comes at a steep operational cost, where graphcache-go and its subgraph:
- Require an Archive Node for detailed transaction inspection
- Can only sync blocks serially in order, limiting parallel processing
- Must be fully resynced from scratch to compute new data
None of these are bad design decisions; they are the inevitable result of not having events. At first, this might not seem problematic. However, things go sideways when dealing with older instances of Ambient. By the time a DEX is just 2-3 years old, it can take more than a month to sync a new graphcache-go instance from scratch.This places enormous centralizing pressure on operating an Ambient frontend. Fast iteration is difficult, and operators must invest in significant hardware or hosting costs just to get started. As the DEX ages, these barriers will only grow.
Change Your Perspective, Change the Problem
Imagine a day five years after an Ambient instance launches. A major sporting event is driving high iFi transaction volumes. One by one, overloaded graphcache-go instances fail. Even with state snapshots, new instances require over an hour just to copy the data and start syncing. User experience and iFi applications suffer.
This is not the future we want to build. A key design goal for iFi DEX is to ensure the frontend and backend are deployable with full automation on a low-cost VM (under $15/month) in under 15 minutes.
Part of the reason we chose Ambient as our base is that its primary limitation—contract size— is something we could adjust within the Althea L1 EVM. However, to our surprise, that adjustment wasn’t necessary.
In the time since Ambient’s initial deployment, Solidity has advanced from v0.8.19 to v0.8.28. Simply upgrading to the latest compiler shrinks the main contract from 23.824KB → 23.376KB.
The real breakthrough came with Solidity’s new via-IR compiler optimization, which translates Solidity code into an intermediate representation (IR) before converting it into EVM bytecode.
Enabling via-IR freed an additional 2.236KB of contract space. The result? The same functionality, the same operations—now using just 21.558KB instead of 23.824KB.
This unexpectedly large reduction in contract size gave us more than enough room to add events and additional features without sacrificing performance.
Ultra-Fast, Event-Based Backend
With this newly available space, we added essential events to the iFi DEX codebase and built a pure Rust backend that mirrors the graphcache-go API. This creates a seamless drop-in replacement, ensuring compatibility with existing Ambient DEX tooling and frontends.
The iFi DEX backend is a monolithic, statically linked Rust application with an embedded RocksDB database. This means no local dependencies, and the same binary can be deployed across any Linux distribution—or even on Mac and Windows.
Even with basic event syncing, processing 10,000 blocks per second is trivial. With highly parallelized logic, the only constraints become network bandwidth and disk write speed.
Unlike graphcache-go, iFi DEX can sync using any full node, making high-throughput infrastructure far easier to build.
From a storage perspective, only the data necessary to serve queries is retained, leading to disk usage that is approximately 1000x lower than a graphcache-go subgraph.
Operationally, a full sync that previously took 10 days or more now completes in just 10 minutes—on dramatically lower-cost hardware.
Conclusion
The next generation of DEX software is about abstraction— allowing users to swap seamlessly as part of larger applications, whether cross-chain or within iFi on Althea L1.
Achieving this requires better tooling and reliability. Open access, combined with ease of deployment, fosters greater decentralization and better user outcomes. We’re excited that iFi DEX is pushing the boundaries here.
When we set out to replace the original Ambient backend for iFi DEX, we didn’t expect to find a practical solution for Ethereum mainnet. But we now anticipate Ambient deployments transitioning to event-based backends and contracts using the strategy we have pioneered here.
Next week, we’ll be diving into iFi DEX’s governance integration!