Token Swap

Fungily Token Swap is an on-chain token exchange protocol built on an Automated Market Maker (AMM) model. It enables seamless, secure, and gas-optimized token swaps with real-time price discovery and capital-efficient liquidity provisioning.

🧠 What Is Fungily Token Swap?

Fungily Token Swap is the core infrastructure for swapping:

  • Fungily-native tokens (from presales or mintpads)

  • Any ERC-20 compatible tokens supported by the protocol

It is *non-custodial, *permissionless, and supports high-efficiency trading by relying on an AMM-powered liquidity poolremoving the need for centralized order books or third-party intermediaries.

⚙️ How It Works

At the heart of Fungily Token Swap is a concentrated liquidity AMM mechanism.

Instead of distributing liquidity across the entire price curve, liquidity providers (LPs) can allocate capital within specific price ranges. This creates:

  • Lower slippage for trades

  • Higher capital efficiency

  • Customizable liquidity provisioning for LPs

📈 The AMM Formula

Fungily swap modifies the traditional constant product formula from:

math x * y = k

to a more granular, price-driven mechanism using square root price ticks.

✅ Key Formulas

Formula for Constant Product AMM:

x \cdot y = k

Where:

  • xx = amount of Token A in the liquidity pool

  • yy = amount of Token B in the liquidity pool

  • kk = constant product

Token Swap Formula:

For token swaps, where a user swaps an input amount of Token A for Token B:

(x + x_{\text{input}}) \cdot y_{\text{output}} = k

Where:

  • xinputx_{\text{input}} = amount of Token A the user wants to swap

  • youtputy_{\text{output}} = amount of Token B the user will receive

To solve for the amount of Token B the user receives:

y_{\text{output}} = \frac{k}{x + x_{\text{input}}}

Final Token B Output:

To find the actual amount of Token B the user will receive:

\Delta y = y - y_{\text{output}}

Where:

  • Δy\Delta y = amount of Token B the user receives.

Slippage Formula:

To account for fees (e.g., 2% fee):

x_{\text{effective input}} = x_{\text{input}} \cdot (1 - 0.02)

Example:

k = x \cdot y = 1000 \cdot 500 = 500,000

After the user inputs 100 Token A:

x + x_{\text{input}} = 1000 + 100 = 1100

Now, calculate the new amount of Token B:

y_{\text{output}} = \frac{k}{x + x_{\text{input}}} = \frac{500,000}{1100} \approx 454.55

Finally, the amount of Token B the user receives:

\Delta y = y - y_{\text{output}} = 500 - 454.55 = 45.45

Full Example in Markdown:

The constant product formula for the AMM is:

$$
x \cdot y = k
$$

Where:
- \(x\) = amount of Token A in the liquidity pool
- \(y\) = amount of Token B in the liquidity pool
- \(k\) = constant product

When a user swaps an amount of Token A (\(x_{\text{input}}\)) for Token B, the new amount of Token B (\(y_{\text{output}}\)) is given by:

$$
(x + x_{\text{input}}) \cdot y_{\text{output}} = k
$$

Solving for \(y_{\text{output}}\), the amount of Token B the user will receive:

$$
y_{\text{output}} = \frac{k}{x + x_{\text{input}}}
$$

The actual amount of Token B the user receives is:

$$
\Delta y = y - y_{\text{output}}
$$

This allows the protocol to simulate a concentrated order book, while still functioning as a decentralized AMM.

💡 Key Features

Feature
Description

Concentrated Liquidity

LPs can choose price ranges to allocate capital more efficiently

On-Chain Pricing

No external price feeds prices adjust according to trades and liquidity

Range Orders

LPs can simulate limit orders by providing liquidity at specific ranges

Gas Optimization

Efficient tick-based design improves gas usage per trade


🧮 Price Execution & Slippage

  • All swaps follow bonding curve mechanics

  • Swaps that consume liquidity across multiple price ticks experience progressive slippage

  • The pool enforces price protection and validates minimum output constraints (amountOutMin)

🔐 Security and Integrity

  • Immutable pool contracts prevent malicious upgrades

  • Permissionless architecture: anyone can add liquidity or initiate swaps

  • Slippage tolerance and deadline options protect users from MEV and frontrunning

📈 Example Use Case

Swapping 100 USDC to FGLY (Fungily token):

  1. Call swapExactTokensForTokens with:

    • amountIn = 100e6

    • path = [USDC, FGLY]

    • amountOutMin = estimated - slippage

  2. Contract calculates output based on current tick, liquidity range, and fee tier

  3. Output tokens are transferred to msg.sender

🔗 Integration & Extensibility

  • Compatible with wallet dApps, dashboards, and on-chain trading bots

  • Can be combined with Fungily Presale and Mintpad to bootstrap post-launch liquidity

  • Future support for:

    • Multi-hop routing

    • Limit orders

    • Liquidity mining programs

Last updated