Go Back

AMM for NFTs

by out.eth on 15-12-2022

Background

Automated market makers (AMMs) are a relatively old design that found their way into crypto markets in 2018. They allowed users to buy and sell on-chain tokens on Ethereum in a gas-efficient way; in comparison to the order-book-based markets that required users to constantly place and cancel orders, AMMs enabled liquidity providers to deposit liquidity just once and have the smart contracts automatically manage the position for them. In addition to this, AMMs provided a single interface to buy and sell from -- adding a great deal of composability that previously did not exist. This additional composability made AMMs act as a "lego" that could be built on top of. Some examples of this include, yield farming, liquidations, rebase mechanisms, and many more. Order book systems also require a centralized service provider to store all of the orders which are currently live adding a centralized point of failure. AMMs on the other hand are completely decentralized and censorship-resistant.

In contrast to the fungible token ecosystem on Ethereum, the NFT ecosystem has not yet made the transition to AMMs. Instead, the majority of NFT volumes are done through traditional order-book-based markets. This is because building an AMM for NFTs poses quite a few challenges. The non-fungible nature of NFTs means that it is difficult to create an automated system that can accurately price each NFT to be traded. Despite this, the benefits of having a more gas-efficient trading system, a more composable liquidity representation, and a more censorship-resistant marketplace, make building an AMM a worthy task. We think we have a solution and I'll attempt to describe it in the following sections.

Mechanism

From a liquidity provider's perspective, you hold some inventory of NFTs and ETH. Suppose you think that the price of NFTs will stay the same or increase in the future. You can deposit your NFTs and ETH into the AMM and earn fees on the trades that happen. The AMM will automatically manage your position and you will be able to withdraw your NFTs and ETH at any time.

Traders can buy the NFTs that you have deposited in the AMM and also sell any NFTs that they might have and receive the ETH you deposited in return. The AMM will automatically adjust the price in the pool each time a buy or sell occurs and fees will also accrue to you. The obvious intuition here is that each NFT in the pool is essentially treated as fungible. Therefore, any NFTs you deposit into the pool must be of a similar value to the lowest-valued NFT that can be sold against it. While this may be ok if you are trading floor NFTs, it is not ok if you are trading rare NFTs. To solve this problem we can further break up the pool granularity to depend on the rarity of NFTs too. For example, within a given collection there may be four different AMMs; One for floor NFTs, one for uncommon NFTs, one for rare NFTs, and one for grails. Then, depending on the value of your NFT you can add liquidity to the appropriate pool.

This ranking mechanism is subjective. To reliably categorize NFTs into their appropriate pools we need high-quality price estimates so that we can accurately appraise each NFT and determine whether or not it is rare, uncommon, floor or grail. We use a desirability classifier model that we developed in-house to aggregate price estimates from various providers and then weigh them against each other. This provides a robust mechanism for determining the desirability of each NFT. We will be releasing more details about this classifier in the future.

The exact formula we use within the AMM is known as the xy=k invariant. It ensures that there is always idle liquidity available and also adjusts the price impact that each trade has depending on the amount of liquidity deposited. If there is more liquidity there is less price impact and vice versa.

All liquidity in each AMM is represented as an ERC20 token called LP tokens. These LP tokens grant the holders the right to withdraw a certain amount of NFTs and ETH from the AMM depending on how much they own. When liquidity providers add liquidity, the smart contract mints these LP tokens and sends them to the providers. Since they are fundamentally just an ERC20 token they can be used anywhere else in the Ethereum ecosystem. For example, you can use them to stake them in a yield farm or you can use them to vote on governance proposals for each pool.

If you are interested in testing out the AMM you can do so on caviar. Please give us any feedback you have!