docs/vyper-by-example/market-maker.rst
.. index:: market maker, AMM
.. _market_maker:
On-Chain Market Maker
.. warning::
This is example code for learning purposes. Do not use in production without thorough review and testing.
A simple automated market maker (AMM) using the constant product formula (x * y = k).
.. literalinclude:: ../../examples/market_maker/on_chain_market_maker.vy :language: vyper :linenos:
How it works:
initiate() with initial ETH and tokens, setting the invariant (k = ETH * tokens)ethToTokens()tokensToEth()The 0.2% fee (msg.value // 500) on ETH-to-token swaps goes to the liquidity provider.
.. note::
Production AMMs need price oracles, slippage protection, and liquidity management. This example demonstrates the core swap mechanism only.