Disclaimer: decentralized finance (DeFi) is a rapidly-changing field. Because of this, information in this post may go out of date. This blog post is not financial or legal advice of any sort.
We are excited to introduce AlgoSwap, a decentralized exchange protocol for the Algorand blockchain. It enables traders to exchange arbitrary fungible Algorand Standard Assets with a secure, fast, and flexible exchange protocol.
If you’re new to the world of decentralized exchanges, don’t worry! This post is intended to give readers a high-level overview of AlgoSwap and the motivation behind it.
The project has been open-sourced, and is currently up for third-party security audit.
AlgoSwap succeeds in satisfying strict performance requirements amidst Algorand’s heavily constrained smart contract environment. As of this blog post’s writing, it is the only implementation with a complete protocol specification.
We will be releasing a sequel to this post focusing on the technical details and implementation of our design. Stay tuned!
What is a decentralized exchange?
Suppose you’re a US company looking to conduct business internationally. You’re hoping to purchase raw materials from Canada to manufacture toys, and want these to be sold in Japanese toy stores.
In order to do business with Japan and Canada, you need the ability to exchange US Dollars for Japanese Yen or Canadian Dollars. Without a currency exchange, you’re going to have a tough time.
Though many other countries denominate things in US Dollars (USD), they still each have their own currency. This is for good reason, since it allows countries to adjust their monetary policy based on their own economic situation.
This ability to exchange currencies is crucial for the global financial system to function as a whole.
In the Ethereum blockchain, a lot of things are denominated in Ether (ETH). But due to the programmable nature of Ethereum, it is possible and common for new types of currencies to be built on top of it, each serving different purposes.
Here, there is also a similar need for a ‘currency exchange’ that allows people to exchange one token for another. In the Ethereum world, protocols such as Uniswap achieve this.
Uniswap is an automated market maker (also called a decentralized exchange, or dex) that uses an algorithm to price currency pairs. Because it’s running on the blockchain, it inherits unique advantages and disadvantages compared to centralized financial exchanges.
This contrasts with proof-of-work blockchains such as Bitcoin, which require a lot of energy and computational power in order to validate transactions.
Algorand was created by Turing Award winner Silvio Micali in 2017. The goal of Algorand is to create technology that “accelerates the convergence between decentralized and traditional finance by enabling the simple creation of next generation financial products, protocols and exchange of value.”
Algorand’s blockchain has benefits that make it appealing for building financial products. Just like trade in the real world, we need a way to convert between different Algorand assets (ASA’s) for this ecosystem to work.
Uniswap did this successfully for the Ethereum blockchain — couldn’t we just reuse that?
In principle, yes! Uniswap is an example of an XY = K market maker — first described by Vitalik Buterin in this Reddit post.
Here, liquidity providers deposit assets in pairs, allowing traders to swap between them. The prices of currency pairs are automatically adjusted using the XY = K formula.
Since the initial Uniswap release, numerous improvements have been made to this basic design. For example, Curve.fi adds a linear term to the formula to better serve stablecoins, and Uniswap v2 eliminates ETH as a mandatory intermediate currency.
However, doing this on Algorand’s blockchain wasn’t as simple as copying Uniswap’s implementation. In fact, when we first started looking into this last year, it wasn’t even feasible to build something like this using Algorand!
But once support for stateful smart contracts was released in August 2020, AlgoSwap became feasible, and our team immediately began working on a protocol design. After much hard work, in December 2020 the project was open-sourced!
The biggest challenge was developing a design that could work around Algorand’s unique constraints and rules for smart contracts. These constraints existed due to the blockchain’s strict performance requirements.
For example, contracts:
- Can only output a TRUE or FALSE value.
- Must be written in TEAL, a Forth-like stack language.
- Must fit within 1000 bytes when compiled. For contrast, Ethereum smart contracts can be up to 24,000 bytes in size.
Within this framework, we designed and built a secure, fast, and flexible exchange using three smart contracts working together. We support:
- Direct ASA-to-ASA swaps.
- Standard liquidity tokens for each liquidity pool, for DeFi integration.
- Slippage limits and slippage handling via refundable balances.
- Fast and efficient two-step deposit/withdrawal.
And this is just the tip of the iceberg — in the sequel to this post, we’ll discuss technical details on AlgoSwap’s design, including
- Front-running resistance & controlling price slippage.
- Incentivizing liquidity & the importance of arbitrageurs.
- Algorand smart contract constraints & workarounds.
- Security considerations.
AlgoSwap is an exciting step towards the future of decentralized finance. It will enable the Algorand ecosystem and network to grow by providing a viable way for everything in the DeFi ecosystem to talk to each other and to trade.
Given the huge rise in popularity of cryptocurrency and NFTs in the last few months, we are excited to see what the future holds for DeFi projects on Algorand!
AlgoSwap was made possible by a generous grant from the Algorand Foundation, and the hard work of the following individuals: Anthony Zhang, Matthew Reyes, Calvin Chan, Ethan Liang, Haardik Haardik, Charlie Zhang, and Jacky Yuan.
Hypotenuse Labs builds incredible web, AI, and blockchain products. We are an elite team with experience building products for some of the world’s largest tech brands such as Facebook, Amazon, and Uber.
If you’re looking to build products at the cutting-edge, shoot us a message at email@example.com!