Skip to content

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

MyAMM Protocol

A decentralized automated market maker (AMM) protocol implemented with Solidity.

MyAMM enables users to provide liquidity for a token pair and swap tokens through an automated pricing mechanism based on the constant product formula:

x * y = k

The protocol allows liquidity providers to earn trading fees while maintaining decentralized token exchange without relying on order books.


Features

Liquidity Pool

Users can provide two different ERC20 tokens to create liquidity.

Example:

Token0 + Token1

The contract maintains reserves:

token0Reserve
token1Reserve

and uses these reserves to calculate swap prices.


Add Liquidity

Liquidity providers can deposit both tokens into the pool:

User
 |
 | deposit Token0 + Token1
 ↓
MyAMM Contract

The protocol calculates liquidity shares based on the user's contribution.

LP shares are represented internally by:

mapping(address => uint256) balances;

Remove Liquidity

Liquidity providers can withdraw their proportional share of the pool.

Example:

User LP Share = 10%

Pool:
1000 Token0
1000 Token1

Withdraw:

100 Token0
100 Token1

The contract burns the user's liquidity share and transfers the corresponding tokens.


Token Swap

Users can exchange Token0 and Token1 directly through the pool.

Example:

User:
Token0

↓

MyAMM

↓

Token1

The swap price is calculated using the constant product formula:

(x + Δx) * (y - Δy) = k

AMM Mechanism

MyAMM follows the constant product AMM model.

The pool maintains:

Token0 Reserve * Token1 Reserve = k

When a swap happens:

  • One token reserve increases
  • Another token reserve decreases
  • The invariant remains approximately constant

Trading Fee

The swap mechanism includes a 0.3% trading fee.

Example:

Input Amount:

1000 Token0

Fee:

0.3%

Effective Swap Amount:

997 Token0

The fee remains inside the liquidity pool and benefits liquidity providers.


Smart Contract Architecture

MyAMM

├── Token0 ERC20
│
├── Token1 ERC20
│
├── Liquidity Management
│
│   ├── addFlow()
│   └── subFlow()
│
├── Swap Engine
│
│   └── swap()
│
└── LP Share Accounting

Technical Stack

  • Solidity ^0.8.13
  • Foundry
  • ERC20 Token Standard
  • EVM Compatible Blockchain

Contract Functions

Add Liquidity

function addFlow(
    uint256 token0Num,
    uint256 token1Num
)

Users provide liquidity and receive LP shares.


Remove Liquidity

function subFlow(
    uint256 liquRequest
)

Users burn LP shares and withdraw proportional tokens.


Swap

function swap(
    tokenType token_type,
    uint256 amount
)

Swap Token0 and Token1 using AMM pricing.


Events

The contract emits events for important operations:

Add Liquidity

event AddFlow(...)

Records:

  • Provider address
  • Token amounts
  • Minted liquidity shares

Remove Liquidity

event SubFlow(...)

Records:

  • Withdrawer address
  • Returned token amounts

Swap

event Swap(...)

Records:

  • Trader address
  • Received token amount
  • Paid token amount

Security Considerations

Implemented:

Checks-Effects-Interactions Pattern

State updates are performed before external token transfers where applicable.


ERC20 Transfer Validation

External token transfers are checked:

require(success, "transfer failed");

Immutable Token Addresses

Token addresses are stored as immutable:

address public immutable token0Address;
address public immutable token1Address;

This prevents modification after deployment.


Testing

Run tests:

forge test

Recommended test coverage:

  • Add liquidity
  • Remove liquidity
  • Swap Token0 → Token1
  • Swap Token1 → Token0
  • Multiple liquidity providers
  • Insufficient liquidity
  • Incorrect token amounts

Known Limitations

This implementation is simplified and does not include some production-level features.

Future improvements:

  • LP ERC20 token implementation
  • Slippage protection
  • Minimum output amount parameter
  • Deadline protection
  • ReentrancyGuard
  • Flash swap support
  • Better precision handling
  • Full Uniswap V2 style invariant checking

Learning Goals

This project demonstrates:

  • AMM mechanism design
  • Constant product formula
  • Liquidity provider accounting
  • ERC20 interaction
  • Smart contract state management
  • DeFi protocol architecture

Author

Solidity Developer focusing on:

  • EVM
  • DeFi Protocol Development
  • Smart Contract Security
  • Foundry Testing

About

solidity+foundry like Uniswap V2,but this model let LP get more ,test 100%

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages