Skip to main content

📖 TTSWAP Technical Whitepaper

TTSWAP is a decentralized token trading platform that enables users to swap tokens quickly and securely without relying on centralized exchanges.


1. Overview

TTSWAP (Token-Token Swap) is an Automated Market Maker (AMM) protocol built on EVM-compatible blockchains. The protocol executes automatically via smart contracts, eliminating the need for centralized institutions or individuals to match trades. Its core mechanism facilitates price discovery by automatically triggering market value transfers based on user trading behavior.

TTSWAP innovatively constructs a Constant Value-based Trading Protocol (Constant Value AMM). Compared to traditional AMMs, this protocol maintains mathematical model robustness while significantly reducing Gas fees by avoiding complex exponential operations.

This whitepaper will elaborate on the design logic of TTSWAP:

  1. Token Trading: Users can directly swap between any tokens, supporting native ETH trading without the need for WETH wrapping.
  2. Value Tokens & Normal Tokens: Distinguishes between mainstream value assets and long-tail assets to build a tiered liquidity market.
  3. Liquidity Management: Users can invest (provide liquidity) and divest at any time, with the system automatically handling fees and returns.
  4. Fee Generation & Distribution: A refined fee distribution mechanism incentivizes Liquidity Providers (LPs), promoters, community builders, and other roles.
  5. Fair Launch: The tokenomics model aims to protect the rights of all holders, ensuring long-term project development through a price-driven unlocking mechanism.

In summary, TTSWAP is dedicated to building an efficient, secure, and low-Gas decentralized trading and payment protocol, providing a concise, transparent, and efficient trading experience for DeFi and PayFi users.

2. Features

  1. Constant Value AMM The core idea of the protocol is to ensure "Value Conservation" during the trading process. This model is mathematically compatible with the mainstream CPMM (Constant Product Market Maker, Uniswap) model and supports asymmetric weighted liquidity similar to Balancer, but avoids high Gas consumption through optimized algebraic algorithms.

  2. Concentrated Liquidity & Direct Trading After users add token liquidity to the protocol, this liquidity can be shared with any other token within the protocol. Users can achieve direct token-to-token swaps, effectively avoiding liquidity fragmentation caused by multi-hop routing in traditional DEXs, significantly enhancing trading depth.

  3. Liquidity Amplification The protocol dynamically configures liquidity amplification coefficients based on token stability characteristics. A unit of capital provided by users can generate multi-fold effective liquidity depth, significantly improving capital efficiency and yield levels (similar to Curve's amplification principle but extended to general tokens).

  4. Low Slippage Trading Through liquidity amplification and concentrated liquidity design, TTSWAP significantly reduces trading slippage. Even large trades can achieve excellent price execution.

  5. Impermanent Loss Protection The protocol protects Liquidity Providers (LPs) from Impermanent Loss (IL) at the architectural level through a specific value anchoring algorithm and a three-tiered defense mechanism, enabling LPs to better maintain their original asset value when withdrawing liquidity.

  6. Extremely Low Gas Fees Benefiting from streamlined algebraic logic and bit-packing storage technology, contract execution is highly efficient. Compared to similar complex protocols, TTSWAP can save 50% to 90% of Gas fees, making on-chain trading more economical.

  7. Refined Fee Distribution Fees are automatically distributed according to participant roles, covering Token Administrators (Creators), Token Investors (LPs), Portal Operators, Referrers, and Users. This builds a mutually beneficial ecosystem.

  8. Native ETH Support The protocol natively supports ETH for direct trading and liquidity provision in its underlying architecture. Users do not need to manually wrap it into WETH, simplifying the interaction process and optimizing user experience.

  9. Proof of Investment & Liquidity Mining When users provide liquidity, the protocol automatically generates a Proof of Investment. Users can use this proof for liquidity mining to earn additional TTS token rewards.

  10. Price-Driven Tokenomics The TTS token adopts an innovative unlocking model: token unlocking is pegged to price performance (price doubling unlock mechanism), combined with a community profit buyback and burn mechanism, ensuring token circulation matches project value growth.

  11. X402 Payment Protocol Support The protocol has built-in support for the X402 standard. This is a mechanism that separates the "Signer" from the "Payment Executor" (similar to Account Abstraction/PayFi), greatly expanding payment application scenarios and enhancing user payment experience.

3. Principles of the Constant Value Trading Model

3.1 Core Formulas

When a user swaps Δa\Delta a amount of Token A for Token B, the protocol follows a two-step calculation:

1. Calculate Transfer Value based on input amount:ΔV=2VAΔa2QA+Δa2. Calculate Output Amount based on Transfer Value:Δb=2QBΔV2VB+ΔV\begin{align} \text{1. Calculate Transfer Value based on input amount:} \\ \Delta V &= \frac{2 V_A \Delta a}{2 Q_A + \Delta a} \\[10pt] \text{2. Calculate Output Amount based on Transfer Value:} \\ \Delta b &= \frac{2 Q_B \Delta V}{2 V_B + \Delta V} \end{align}

(For the detailed derivation of the Constant Value Model core formulas, see Appendix A)

Parameter Definitions:

SymbolMeaningRemarks
VAV_ATotal Market Value of Token A in protocolRepresents the token's "weight" in the protocol
QAQ_ACurrent Inventory Quantity of Token AIncludes accumulated fees
Δa\Delta aToken A amount input by userNet amount after fee deduction
VBV_BTotal Market Value of Token B in protocol
QBQ_BCurrent Inventory Quantity of Token BIncludes accumulated fees
Δb\Delta bToken B amount received by userGross amount before output fee

Design Philosophy: The essence of exchange is the mismatch between quantity and value. The protocol automatically balances supply and demand through market means (price slippage).

3.2 Mathematical Properties and Comparisons

3.2.1 Relationship with Uniswap (CPMM)

When the configured values of two tokens are equal (VA=VBV_A = V_B), the above formulas are mathematically equivalent to Uniswap's Constant Product formula (xy=kx \cdot y = k).

Derivation Brief: In the case of VA=VBV_A = V_B, substituting into the formula yields:

Δb=QBΔaQA+Δa\Delta b = \frac{Q_B \Delta a}{Q_A + \Delta a}

(See Appendix B for the simplification process in the case of equal value)

This is exactly the output formula of the standard CPMM model, meaning TTSWAP possesses the same market depth and pricing characteristics as Uniswap when dealing with standard token pairs.

3.2.2 Relationship with Balancer (Weighted Pools)

When VAVBV_A \neq V_B, the model exhibits the characteristics of a weighted pool.

  • Balancer uses exponential operations (xwoywi=k)(x^{w_o} \cdot y^{w_i} = k) to implement different weights, which is computationally complex and gas-intensive.
  • TTSWAP uses the above algebraic formulas to simulate asymmetric liquidity pools without exponential operations. This allows TTSWAP to support customized liquidity management for stablecoins (high weight) or emerging tokens (low weight) at extremely low Gas costs.

(See Appendix C for the proof of equivalence with Balancer in unequal value cases)

3.3 State Update After Trading

After the transaction is completed, the token states are updated, forming a new exchange ratio:

TokenAToken_A (Input Side)TokenBToken_B (Output Side)
Value (Weight)VAV_A (Unchanged)VBV_B (Unchanged)
QuantityQA+ΔaQ_A + \Delta a (Increases)QBΔbQ_B - \Delta b (Decreases)
  • New Marginal Price (Book Price): PAnew=VAQA+ΔaP_A^{\text{new}} = \frac{V_A}{Q_A + \Delta a} PBnew=VBQBΔbP_B^{\text{new}} = \frac{V_B}{Q_B - \Delta b}

  • New Exchange Ratio (Spot Price): RAB=PAnewPBnewR_{A \to B} = \frac{P_A^{\text{new}}}{P_B^{\text{new}}}

Note: Here, PAP_A and PBP_B are internal indicators of the token's "scarcity" relative to its weight within the protocol, not direct fiat prices.

4. Token

4.1 Token Introduction

In the TTSWAP protocol, each Token has four core attributes:

  1. Market Value (VV): Measures the token's weight in the protocol (Total Weight Value of the pool).
  2. Current Quantity (QQ): The current token balance held by the protocol (includes accumulated fees).
  3. Invested Quantity (II): The total number of tokens deposited by LPs (includes accumulated fees).
  4. Invested Share (SS): The total share held by LPs.

Trading Mechanism: When users believe the market price of a token is lower than the protocol price (i.e., V/QV/Q is high), they tend to sell the token; otherwise, they buy.

Token Introduction

4.2 Token Classification

Token CategoryDescriptionGenerates FeesIndividual InvestmentCombined Investment
Meta TokenThe first Token added after protocol deployment (e.g., USDT), serving as a value anchor benchmarkYesYesNo
Value TokenTokens with high market recognition and mature ecosystem (e.g., ETH, WBTC)YesYesNo
Normal TokenNewly added long-tail assets, must be added paired with a Value TokenYesNoYes

4.3 Token Configuration & Gas Optimization

To achieve extreme Gas efficiency, TTSWAP adopts Bit-packing technology, compressing all key configuration parameters of a token into a single 256-bit storage slot.

This means reading or updating token configuration requires only one on-chain storage operation (SSTORE/SLOAD), drastically reducing interaction costs.

(See Appendix D for the detailed 256-bit storage layout)

User-configurable parameters include:

  • Fee Settings: Investment fee, divestment fee, buy/sell fee.
  • Liquidity Amplification Multiplier: Sets the token's liquidity amplification factor (1 ~ Market-set maximum multiplier).
  • Divestment Slicing: A security parameter used to smooth the impact of large withdrawals.

Note: The maximum multiplier set by the market is evaluated based on token project quality and operational quality, with a default value of 1.

5. Adding Meta Tokens

Meta Token refers to the first token added after the protocol is deployed, typically serving as the protocol's value anchor (e.g., USDT). The Meta Token itself is also a Value Token.

alt text

For example, the protocol creator initializes USDT as the Meta Token and injects 2,000 USDT as initial liquidity.

  • State at this time:
    • Market Value (VV) = 2,000
    • Token Quantity (QQ) = 2,000
    • Invested Quantity (II) = 2,000
    • Invested Share (SS) = 2,000

6. Adding Normal Tokens

alt text

When a user adds a Normal Token (e.g., Token A), they must anchor it to an existing Value Token (e.g., USDT) to determine its initial pricing.

Suppose a user adds Token A and anchors it to USDT:

  • USDT State Update:
    • Market Value = 3,000
    • Token Quantity = 3,000
    • Invested Quantity = 3,000
    • Invested Share = 3,000
  • Token A Initial State:
    • Market Value = 1,000
    • Token Quantity = 2,000
    • Invested Quantity = 2,000
    • Invested Share = 2,000

Note: Widely recognized Normal Tokens can be upgraded to Value Tokens upon meeting certain conditions.

7. Token Investment & Divestment

In TTSWAP, providing liquidity is called "Investment," and removing liquidity is called "Divestment."

7.1 Core Concepts

Token Invest State

  • Token Attributes

    • Market Value (V): Measures the token's weight or total market value in the protocol.
    • Current Quantity (Q): The current token balance held by the protocol (includes accumulated fees).
    • Invested Quantity (I): The total number of tokens deposited by LPs (includes accumulated fees).
    • Invested Share (S): The equity share held by LPs. Formula: Share=Invested Quantity/Current Net Asset Value\text{Share} = \text{Invested Quantity} / \text{Current Net Asset Value}.
    • Amplified Quantity(P): Virtual liquidity generated by the liquidity amplification mechanism.
  • Proof of Investment (User Proof) Attributes

    • Invest Value(IV): The market value of the token at the time of user investment.
    • Invest Share(IS): The investment share held by the user.
    • Invest Quantity(IQ): The total quantity of the token at the time of user investment.
    • Invest Actual Quantity(IC): The actual quantity of the token at the time of user investment.

7.2 Token Investment & Divestment Calculation Process

Alt text

7.2.1 Investment (Providing Liquidity)

When a user invests tokens:

  1. Calculate Amplification: Based on the configured "Liquidity Amplification Multiplier," the protocol virtually increases the input quantity, thereby deepening market depth without increasing actual capital occupation.
  2. Calculate Share: Calculate the LP share due to the user based on the current Net Asset Value (NAV) of the pool.
  3. Update State: Increase the pool's Market Value, Token Quantity, and Total Invested Share.

7.2.2 Divestment (Removing Liquidity)

When a user divests:

  1. Calculate Redeemable Quantity: Calculate the token quantity to be redeemed based on the share percentage held by the user.
  2. Calculate Return: Return=Current Redeemable QuantityOriginal Invested Quantity\text{Return} = \text{Current Redeemable Quantity} - \text{Original Invested Quantity}.
  3. Divestment Slicing: To prevent whales from draining the pool instantly or causing drastic price fluctuations through large withdrawals, the protocol introduces a "Divestment Slicing" mechanism, limiting the maximum ratio of a single withdrawal (e.g., not exceeding 1/N of the total pool). If the withdrawal amount is too large, the user needs to operate in batches or bear higher slippage costs.

(See Appendix E for the detailed calculation process of Token Investment & Divestment)

7.3 Triple Defense Mechanism Against Impermanent Loss

To strictly protect the principal safety of ordinary Liquidity Providers (LPs), TTSWAP has designed a unique triple defense system to cope with Impermanent Loss (IL):

  1. First Line of Defense: Protocol-Level Value Anchoring (Protocol Level) Through the built-in value conservation algorithm of the smart contract, the protocol prioritizes ensuring the base value of LPs upon divestment, reducing passive losses caused by price fluctuations from the underlying logic.

  2. Second Line of Defense: Project Party Junior Buffer (Capital Level) In the event of a value gap caused by extreme market conditions, the initial liquidity injected by the token issuer (creator) will serve as "junior capital," prioritizing the absorption of losses caused by value fluctuations, thereby protecting the funds of ordinary users.

  3. Third Line of Defense: Protocol Token Compensation Fund (Governance Level) As a final resort, if the above measures are still insufficient to cover all losses, the protocol will activate the "Risk Compensation Mechanism," raising funds by auctioning or releasing part of the TTS governance tokens to compensate affected users.

8. Token Swap Example

This chapter demonstrates the full process of a user swapping TokenAToken_A for TokenBToken_B.

Token Swap

8.1 Initial State

  • Token A: Value 40,000, Quantity 20,000 (Unit Price 2.0)
  • Token B: Value 20,000, Quantity 40,000 (Unit Price 0.5)
  • Initial Exchange Rate: 1 Token A = 4 Token B

8.2 Trade Calculation

User inputs 2,500 Token A:

  1. Deduct Fee: Assuming a fee rate of 0.01%, deduct 0.25 A, actual input to pool Δa=2,499.75\Delta a = 2,499.75.
  2. Calculate Transfer Value: Calculate ΔV4,705.4\Delta V \approx 4,705.4 based on the formula.
  3. Calculate Output Quantity: Calculate Token B outflow Δb8,420.3\Delta b \approx 8,420.3 based on ΔV\Delta V.
  4. Deduct Output Fee: Deduct 0.84 B, user actually receives 8,419.46 Token B.

(See Appendix F for the detailed calculation process of Token Swap)

8.3 Result Analysis

Token Swap Result

After the trade, Token A quantity increases (price drops), Token B quantity decreases (price rises). The protocol automatically completes price discovery.

8.4 Contract Security Verification

The protocol has passed multiple mathematical consistency tests:

  • Fairness: Swap A for B, then immediately swap B back for A, loss is minimal (excluding fees) (path independence).
  • Rationality: The result of a single large trade is consistent with splitting it into multiple small trades (no arbitrage space).
  • Closed Loop: The logic of A->B->C->A cyclic trading is self-consistent.

See modified_swap_without_fee Test Contract Address

9. Fees

9.1 Fee Sources

Token Fee Source

Fees are deducted in real-time when a trade occurs and are directly accumulated into the token's "Current Quantity" and "Invested Quantity." This means the Net Asset Value (NAV) of LP shares will monotonically increase with trading volume, and distribution occurs upon LP divestment.

9.2 Distribution Mechanism

TTSWAP adopts a multi-role profit-sharing mechanism to promote ecosystem prosperity:

Token Fee Allocation

RoleShare Ratio (Example)Description
Token Administrator1% - 3%Initial creator or operator of the token
Liquidity Provider (LP)50% - 80%Core beneficiary, automatic compounding by share
Service Provider5% - 25%Third parties providing frontend, aggregation, or integration services
Referrer5% - 10%Promotion ambassadors inviting new users
Community Treasury2% - 8%Used for buyback & burn of TTS tokens or technical R&D

If there is no explicit referrer, the relevant ratio will be allocated to the Service Provider or Community Treasury.

(See Appendix G for Token Fee Calculation & Distribution Process)

10. Token Welfare

Token Welfare

Project parties can actively inject tokens into the pool as "welfare," which will directly increase the Net Asset Value (NAV) of LP shares, thereby increasing the Annual Percentage Rate (APR) and attracting more liquidity.

11. Liquidity Mining

The protocol supports an "Investment is Mining" mechanism. Users' LP shares are automatically converted into hashrate to mine the platform governance token TTS. This achieves dual returns: Trading Fees + TTS Token Rewards.

12. X402 Payment Protocol Support

TTSWAP implements support for the X402 Protocol. This is an advanced Intent-Centric Payment Standard, similar to the concept of Account Abstraction or PayFi.

Core Advantages:

  • Separation of Signing and Execution: Users only need to sign payment intents (offline operation, gas-free), while specific on-chain execution and Gas payment can be handled by a third-party "Executor."
  • All-Currency Payment: The application side does not need to care about which token the user holds; the X402 protocol will automatically complete the swap and pay the target currency on-chain.

Process:

  1. User accesses the app, app requests payment.
  2. User signs an offline message (Permit).
  3. Executor submits the signature on-chain.
  4. TTSWAP contract verifies signature, automatically swaps tokens, and completes payment.

13. Main Code Implementation (See Code)

13.1 Contract Deployment GAS

Deployment CostDeployment Size
5,644,29726,543

13.2 Contract Function (Partial Main Functions) GAS

Function NameavgRemarks
buyGood(NativeETH)81,190Buy Token
buyGood(ERC20)89,761Buy Token
disinvestProof164,402Divest
initGood376,985Initialize Token
investGood153,945Invest Token
goodWelfare52,396Add fees as welfare

14. Protocol Roles

The protocol supports 5 roles: Token Administrator, Liquidity Provider, Portal Operator, Referrer, User, and Community.

RoleIncome SourceDescription
Liquidity Provider (LP)50% - 80%Core contributor to the fund pool, highest income share.
Token Administrator1% - 3%Operator responsible for token listing and parameter configuration.
Referrer5% - 10%Promotion ambassador inviting new users.
Portal Operator5% - 25%Third party providing frontend interface and aggregation services.
Community Treasury2% - 8%Used for technical development and buyback & burn.
User Discount-10%Users with referral codes enjoy fee reductions.

15. Tokenomics

TTS Token is the governance and incentive token of the protocol, adopting an innovative "Price-Driven" unlocking mechanism.

15.1 Basic Information

  • Name: TTSWAP Token (TTS)
  • Initial Supply: 50 Million (Fully Locked)
  • Inflation Mechanism: Annual issuance of (200 Million - Unlocked Quantity) × 2% for LP rewards.
  • Deflation Mechanism: 100% of community revenue is used for secondary market buyback and burn of TTS.

15.2 Core Innovation: Price-Driven Unlocking

To prevent team dumping and early whale selling, TTS unlocking is directly pegged to token price:

  • Principle: Tokens unlock only when the price rises.
  • Mechanism:
    • Fully locked upon distribution (except for Public Sale and Liquidity Mining parts).
    • Set Initial Price and Unlock Ratio (e.g., 10%).
    • When the price doubles, unlock 10% of the remaining locked amount.
    • If the price doubles again, unlock another 10% of the remaining amount.
    • Result: The interests of the team and early investors are forcibly bound to long-term price performance.

15.3 4C Community Allocation Model

Token allocation follows the 4C role model to ensure contributors are rewarded:

RoleDescriptionRatioUnlock Limit (Max)Trigger Condition
FounderProject initiator, bearing maximum risk20%< 1/20 (5%)Requires 1,000x rise to unlock ~37%
PartnerCore execution team12%< 1/14 (7.1%)Requires 400x rise to unlock ~45%
Value ContributorCommunity operation, technical development44%< 1/12 (8.3%)Requires 200x rise to unlock ~50%
Capital ContributorEarly investment, public sale, airdrop24%< 1/8 (12.5%)Requires 100x rise to unlock ~60%

15.4 Dynamic Buyback & Burn

  • The portion of fee revenue generated by the protocol allocated to the "Community Treasury" will be entirely used for secondary market buyback and burn of TTS.
  • This constitutes the deflationary engine of TTS. As trading volume grows, the circulating supply will continue to decrease, thereby driving up token value.

16.1 Explanation

To safeguard the legitimate rights and interests of the project while facilitating user understanding of the protocol, different files adopt different open-source licenses. Violating the agreements will result in corresponding legal liabilities.

16.2 License Details

To protect intellectual property and promote technical exchange:

  • Core Logic: Adopts BUSL-1.1 (Business Source License). Allows for learning and research but restricts commercial forking within a specific period.
  • Peripheral Components: Adopts MIT license, encouraging the community to develop peripheral tools and integrated applications.

For detailed copyright information, please refer to the LICENSE file in the GitHub repository.

16.3 File License Information

src
├── TTSwap_Market.sol (BUSL-1.1)
├── TTSwap_Market_Proxy.sol (BUSL-1.1)
├── TTSwap_Token.sol (BUSL-1.1)
├── TTSwap_Token_Proxy.sol (BUSL-1.1)
├── interfaces
│ ├── I_TTSwap_Market.sol (MIT)
│ └── I_TTSwap_Token.sol (MIT)
└── libraries
├── L_Currency.sol (MIT)
├── L_Error.sol (MIT)
├── L_Good.sol (BUSL-1.1)
├── L_GoodConfig.sol (MIT)
├── L_Proof.sol (BUSL-1.1)
├── L_SignatureVerification.sol (MIT)
├── L_Transient.sol (MIT)
├── L_TTSTokenConfig.sol (MIT)
├── L_TTSwapUINT256.sol (MIT)
└── L_UserConfig.sol (MIT)
docs
├── ebook
tests

17. Contract Deploy Address

17.1 Ethernet Mainnet

Contract NameDeploy Address
TTSwap_Market_Proxy0x8E2FF92834C75D3132ad5CED6482E660822f5EF6
TTSwap_Market0x7e311CcA6ed6c63D861e4426B0aBaFCeACEf85D6
TTSwap_Token_Proxy0x441D5687245EE9E7388c51159770736fb142Ea9c
TTSwap_Token0x3aF2E663589A227fE351d2Dea7858d94F0e2be1D

17.2 Ethernet Hoodi

Contract NameDeploy Address
TTSwap_Market_Proxy0x8E2FF92834C75D3132ad5CED6482E660822f5EF6
TTSwap_Market0x7e311CcA6ed6c63D861e4426B0aBaFCeACEf85D6
TTSwap_Token_Proxy0x441D5687245EE9E7388c51159770736fb142Ea9c
TTSwap_Token0x3aF2E663589A227fE351d2Dea7858d94F0e2be1D

18. Contact

Appendix A: Detailed Derivation of Constant Value Trading Model Core Formulas

This appendix details how to derive the core trading formulas of TTSWAP from the "Harmonic Mean Price" principle.

The TTSWAP trading process is divided into two independent steps:

  1. Input Side: Convert input Token A into standard value (ΔV\Delta V).
  2. Output Side: Convert standard value (ΔV\Delta V) into output Token B.

Step 1: Calculate Value ΔV\Delta V corresponding to Input Token A

We use the Harmonic Mean to determine the execution price of the trade. Compared to the arithmetic mean, the harmonic mean better smooths price fluctuations and ensures trade fairness.

1. Define Instantaneous Value Density (Price)

  • Pre-Trade State: The protocol holds QAQ_A Token A with a total value of VAV_A. Pstart=VAQAP_{\text{start}} = \frac{V_A}{Q_A}
  • Post-Trade State (Hypothetical): The protocol holds QA+ΔaQ_A + \Delta a Token A, total value remains VAV_A (calculating marginal contribution). Pend=VAQA+ΔaP_{\text{end}} = \frac{V_A}{Q_A + \Delta a}

2. Calculate Execution Price (Harmonic Mean) Take the harmonic mean of prices before and after the trade to get the effective execution price PexecP_{\text{exec}}:

Pexec=HarmonicMean(Pstart,Pend)=21Pstart+1PendP_{\text{exec}} = \text{HarmonicMean}(P_{\text{start}}, P_{\text{end}}) = \frac{2}{\frac{1}{P_{\text{start}}} + \frac{1}{P_{\text{end}}}}

Substitute PstartP_{\text{start}} and PendP_{\text{end}}:

Pexec=2QAVA+QA+ΔaVA=22QA+ΔaVA=2VA2QA+ΔaP_{\text{exec}} = \frac{2}{\frac{Q_A}{V_A} + \frac{Q_A + \Delta a}{V_A}} = \frac{2}{\frac{2Q_A + \Delta a}{V_A}} = \frac{2 V_A}{2 Q_A + \Delta a}

3. Calculate Transfer Value ΔV\Delta V

ΔV=PexecΔa=2VAΔa2QA+Δa\Delta V = P_{\text{exec}} \cdot \Delta a = \frac{2 V_A \Delta a}{2 Q_A + \Delta a}

Step 2: Calculate Output Quantity Δb\Delta b based on Value ΔV\Delta V

On the output side, we focus on "Token Quantity per Unit Value" (inverse of price, denoted as DD).

1. Define Instantaneous Quantity Density

  • Pre-Trade State: The protocol holds QBQ_B Token B with a total value of VBV_B. Dstart=QBVBD_{\text{start}} = \frac{Q_B}{V_B}
  • Post-Trade State (Hypothetical): Total value increases by ΔV\Delta V, becoming VB+ΔVV_B + \Delta V. Dend=QBVB+ΔVD_{\text{end}} = \frac{Q_B}{V_B + \Delta V}

2. Calculate Execution Density (Harmonic Mean)

Dexec=HarmonicMean(Dstart,Dend)=21Dstart+1DendD_{\text{exec}} = \text{HarmonicMean}(D_{\text{start}}, D_{\text{end}}) = \frac{2}{\frac{1}{D_{\text{start}}} + \frac{1}{D_{\text{end}}}}

Substitute DstartD_{\text{start}} and DendD_{\text{end}}:

Dexec=2VBQB+VB+ΔVQB=2QB2VB+ΔVD_{\text{exec}} = \frac{2}{\frac{V_B}{Q_B} + \frac{V_B + \Delta V}{Q_B}} = \frac{2 Q_B}{2 V_B + \Delta V}

3. Calculate Output Quantity Δb\Delta b

Δb=DexecΔV=2QBΔV2VB+ΔV\Delta b = D_{\text{exec}} \cdot \Delta V = \frac{2 Q_B \Delta V}{2 V_B + \Delta V}

Thus, the core formula derivation is complete.


Appendix B: Formula Simplification in Equal Value Case (Uniswap Equivalence Proof)

This appendix proves that when the weights (values) of two tokens are equal, the TTSWAP trading model is mathematically completely equivalent to the Uniswap Constant Product formula (CPMM).

1. Set Conditions

Assume the configured values of TokenAToken_A and TokenBToken_B are equal, i.e.: VA=VB=VV_A = V_B = V

2. Combine Formulas

Combine the two steps from Appendix A.

Input Side: ΔV=2VΔa2QA+Δa\Delta V = \frac{2 V \Delta a}{2 Q_A + \Delta a}

Output Side: Δb=2QBΔV2V+ΔV\Delta b = \frac{2 Q_B \Delta V}{2 V + \Delta V}

3. Substitution and Simplification

Substitute the expression of ΔV\Delta V into the formula for Δb\Delta b:

Δb=2QB(2VΔa2QA+Δa)2V+(2VΔa2QA+Δa)\Delta b = \frac{2 Q_B \cdot (\frac{2 V \Delta a}{2 Q_A + \Delta a})}{2 V + (\frac{2 V \Delta a}{2 Q_A + \Delta a})}

Divide numerator and denominator by 2V2V:

Δb=QB(2Δa2QA+Δa)1+(Δa2QA+Δa)\Delta b = \frac{Q_B \cdot (\frac{2 \Delta a}{2 Q_A + \Delta a})}{1 + (\frac{\Delta a}{2 Q_A + \Delta a})}

Simplify the denominator:

Denominator=(2QA+Δa)+Δa2QA+Δa=2QA+2Δa2QA+Δa=2(QA+Δa)2QA+Δa\text{Denominator} = \frac{(2 Q_A + \Delta a) + \Delta a}{2 Q_A + \Delta a} = \frac{2 Q_A + 2 \Delta a}{2 Q_A + \Delta a} = \frac{2(Q_A + \Delta a)}{2 Q_A + \Delta a}

Substitute the denominator back into the original equation:

Δb=QB2Δa2QA+Δa2(QA+Δa)2QA+Δa\Delta b = \frac{Q_B \cdot \frac{2 \Delta a}{2 Q_A + \Delta a}}{\frac{2(Q_A + \Delta a)}{2 Q_A + \Delta a}}

Cancel the common term 22QA+Δa\frac{2}{2 Q_A + \Delta a}:

Δb=QBΔaQA+Δa\Delta b = \frac{Q_B \Delta a}{Q_A + \Delta a}

4. Conclusion

The above equation is the standard output formula of Uniswap (ignoring fees): Δy=yΔxx+Δx\Delta y = \frac{y \Delta x}{x + \Delta x}

Q.E.D. This demonstrates that TTSWAP is a generalized extension of the CPMM model: it degenerates into CPMM when VA=VBV_A=V_B and exhibits weighted pool characteristics when VAVBV_A \neq V_B.

Appendix C: Formula Derivation in Unequal Value Case and Balancer Equivalence Proof

This appendix delves into the mathematical properties of the TTSWAP trading model when VAVBV_A \neq V_B, proving its complete equivalence with the Balancer weighted formula under infinitesimal increments (Spot Price) and high fitting under large trades.

1. Balancer Model Review

Balancer uses the Weighted Product Formula: (QA)WA(QB)WB=k(Q_A)^{W_A} \cdot (Q_B)^{W_B} = k Its exchange price (Spot Price) depends on the reserve ratio and weight ratio: SPBalancer=QB/WBQA/WASP_{\text{Balancer}} = \frac{Q_B / W_B}{Q_A / W_A} (Ignoring fees, priced in A for B) SPBalancer=QAWBQBWASP_{\text{Balancer}} = \frac{Q_A W_B}{Q_B W_A}

2. TTSWAP Model Marginal Price Derivation

We calculate the marginal price by differentiating the core formula of TTSWAP.

Input Side (Token A -> Value): ΔV=2VAΔa2QA+Δa\Delta V = \frac{2 V_A \Delta a}{2 Q_A + \Delta a} When Δa0\Delta a \to 0 (Differential perspective): dV=limΔa02VAΔa2QA=VAQAdadV = \lim_{\Delta a \to 0} \frac{2 V_A \Delta a}{2 Q_A} = \frac{V_A}{Q_A} da

Output Side (Value -> Token B): Δb=2QBΔV2VB+ΔV\Delta b = \frac{2 Q_B \Delta V}{2 V_B + \Delta V} When ΔV0\Delta V \to 0: db=limΔV02QBΔV2VB=QBVBdVdb = \lim_{\Delta V \to 0} \frac{2 Q_B \Delta V}{2 V_B} = \frac{Q_B}{V_B} dV

Comprehensive Exchange Rate (Spot Price): Combine the above two equations: db=QBVB(VAQAda)db = \frac{Q_B}{V_B} \cdot (\frac{V_A}{Q_A} da) Instantaneous exchange rate: dbda=QBVAVBQA\frac{db}{da} = \frac{Q_B V_A}{V_B Q_A}

Therefore, TTSWAP's Spot Price is: SPTTSWAP=dadb=1QBVAVBQA=QAVBQBVASP_{\text{TTSWAP}} = \frac{da}{db} = \frac{1}{\frac{Q_B V_A}{V_B Q_A}} = \frac{Q_A V_B}{Q_B V_A}

3. Equivalence Comparison

ParameterBalancer (Weighted Pool)TTSWAP (Constant Value)
Weight DefinitionWeight WiW_iMarket Value ViV_i
Spot PriceSP=QAWBQBWASP = \frac{Q_A W_B}{Q_B W_A}SP=QAVBQBVASP = \frac{Q_A V_B}{Q_B V_A}

Conclusion 1 (Spot Price Equivalence): As long as we regard Market Value VV in TTSWAP as Weight WW in Balancer, the spot price logic of both is completely equivalent. i.e.: VAVB    WAWB\frac{V_A}{V_B} \iff \frac{W_A}{W_B}

4. Curve Fitting for Large Trades

Balancer's exponential curve has constant elasticity, while TTSWAP uses a harmonic mean curve (a form of hyperbola).

Although there are slight differences in slippage curves for large trades (large Δa\Delta a), their mathematical properties are highly consistent:

  1. Convexity: Both are convex functions, ensuring price increases with purchase volume (slippage).
  2. Asymptote: Both have asymptotes, meaning the pool cannot be drained.
  3. Computational Complexity:
    • Balancer: Requires calculating fractional powers (xWA/WB)(x^{W_A/W_B}), which is complex on-chain and consumes high Gas.
    • TTSWAP: Only involves addition, subtraction, multiplication, and division, with constant and extremely low Gas consumption.

Conclusion 2 (Engineering Advantage): TTSWAP achieves the same pricing logic (Spot Price) and market depth behavior as weighted pools while eliminating exponential operations through algebraic approximation, realizing an O(1) complexity weighted trading experience rare in DeFi protocols. This means it can achieve Balancer-level multi-asset portfolio management capabilities at Uniswap low Gas fees.

Appendix D: Token Configuration Storage Layout

For extreme Gas optimization, Token configuration uses the following bit-packed structure (256 bits):

Token Config Bit Layout

  • [1-128] Occupied, stores Market Value field.
  • [129-177] Reserved fields.
  • [178-256] Currently used configuration fields.

Admin Config Area:

idConfig ItemBitsUnitMaxMinStartEndDescription
1Is Value Token1BOOLEAN10256256
2Is Frozen1BOOLEAN10255255
3Investor Fee Share31/1071252254
4Operator Fee Share42/10071248251
5Portal Fee Share34/10071245247
6Referrer Fee Share51/100311240244
7User Fee Share51/100311235239
8Protocol Fee Rate51/100311230234
9Max Liquidity Amp51x311225229
10Applied11x11224224
...

Remark: Sum of fee shares is 100%.

User Config Area:

idConfig ItemBitsUnitMaxMinStartEndDescription
1Invest Fee61/10000630218223(0~63)/10000
2Divest Fee61/10000630212217(0~63)/10000
3Buy Fee71/100001270205211(0~127)/10000
4Sell Fee71/100001270198204(0~127)/10000
5Liquidity Amp51310188192(1~1023)
6Divest Slicing10110230178187(1~1023)

(Note: Specific bit offsets may be fine-tuned with contract version upgrades; refer to on-chain code)

Appendix E: Token Investment & Divestment Detailed Calculation Process

1. Initial State Setting

This case aims to demonstrate the full lifecycle calculation of LP participating in liquidity investment and divestment.

1.1 Token State

  • Market Value (VV): 40,000
  • Current Quantity (QQ): 20,000
  • Total Invested Quantity (II): 30,000
  • Total Invested Share (SS): 20,000
  • Liquidity Amp Multiplier: 5x (Virtual Amplification)

1.2 User Proof Initial State

  • Invested Value: 0
  • Invested Share: 0
  • Invested Quantity: 0
  • Actual Input: 0

2. Investment Process

User decides to invest 200 tokens to provide liquidity.

Step 1: Calculate Virtual Invested Quantity Based on the 5x multiplier, the protocol maps the user's actual input to a virtual invested amount: Virtual Invested Amount=200×5=1,000\text{Virtual Invested Amount} = 200 \times 5 = 1,000

Step 2: Calculate Invested Value Lock the value at the time of investment based on current market price (V/QV/Q): Invested Value=VQ×Virtual Invested Amount=40,00020,000×1,000=2,000\text{Invested Value} = \frac{V}{Q} \times \text{Virtual Invested Amount} = \frac{40,000}{20,000} \times 1,000 = 2,000

Step 3: Calculate Acquired Share Calculate the LP share obtained by the user based on the current NAV: New Share=Total Invested ShareTotal Invested Quantity×Virtual Invested Amount=20,00030,000×1,000667\text{New Share} = \frac{\text{Total Invested Share}}{\text{Total Invested Quantity}} \times \text{Virtual Invested Amount} = \frac{20,000}{30,000} \times 1,000 \approx 667

Step 4: Calculate Amplified Quantity (Virtual Part) Amplified Quantity=Virtual Invested AmountActual Input=1,000200=800\text{Amplified Quantity} = \text{Virtual Invested Amount} - \text{Actual Input} = 1,000 - 200 = 800

Step 5: Update System State

  • Market Value: 40,000+2,00042,00040,000 + 2,000 \rightarrow 42,000
  • Current Inventory: 20,000+1,00021,00020,000 + 1,000 \rightarrow 21,000 (Includes virtual increment)
  • Total Invested Quantity: 30,000+1,00031,00030,000 + 1,000 \rightarrow 31,000
  • Total Invested Share: 20,000+66720,66720,000 + 667 \rightarrow 20,667

Step 6: Update User Proof

  • Invested Value: 2,000
  • Invested Share: 667
  • Invested Quantity: 1,000
  • Actual Input: 200

3. Market Transition

Assume after a period, the token pool state changes due to market trading (price fluctuation or fee accumulation):

  • Market Value: 42,000 (Unchanged)
  • Current Inventory: 21,000 \rightarrow 24,000 (Increased)
  • Total Invested Quantity: 31,000 \rightarrow 33,000 (Increased)
  • Total Invested Share: 20,667 (Unchanged)

4. Divestment Process

User decides to redeem 100 LP shares.

Step 1: Calculate Current Redeemable Quantity Current Quantity=Total Invested QuantityTotal Invested Share×Redeem Share=33,00020,667×100159.67\text{Current Quantity} = \frac{\text{Total Invested Quantity}}{\text{Total Invested Share}} \times \text{Redeem Share} = \frac{33,000}{20,667} \times 100 \approx 159.67

Step 2: Calculate Original Invested Principal Trace back original input based on the ratio in the user's investment proof: Original Quantity=Invested Quantity in ProofInvested Share in Proof×Redeem Share=1,000667×100150\text{Original Quantity} = \frac{\text{Invested Quantity in Proof}}{\text{Invested Share in Proof}} \times \text{Redeem Share} = \frac{1,000}{667} \times 100 \approx 150

Step 3: Calculate Value at Investment Original Value=Invested Value in ProofInvested Share in Proof×Redeem Share=2,000667×100300\text{Original Value} = \frac{\text{Invested Value in Proof}}{\text{Invested Share in Proof}} \times \text{Redeem Share} = \frac{2,000}{667} \times 100 \approx 300

Step 4: Calculate Net Return Return=Current QuantityOriginal Quantity=159.67150=9.67\text{Return} = \text{Current Quantity} - \text{Original Quantity} = 159.67 - 150 = 9.67

Step 5: Deduct Virtual Amplified Part User can only withdraw actual assets, need to deduct the initial virtual amplification amount: Deduct Amplified Amount=Amplified Quantity in ProofInvested Share in Proof×Redeem Share=800667×100120\text{Deduct Amplified Amount} = \frac{\text{Amplified Quantity in Proof}}{\text{Invested Share in Proof}} \times \text{Redeem Share} = \frac{800}{667} \times 100 \approx 120

Step 6: Fee Distribution Assume LP share ratio is 70%, remaining 30% allocated to platform, referrers, etc.: User Net Return=Total Return×70%=9.67×0.76.76\text{User Net Return} = \text{Total Return} \times 70\% = 9.67 \times 0.7 \approx 6.76 Commission Deduction=Total Return×30%=9.67×0.32.90\text{Commission Deduction} = \text{Total Return} \times 30\% = 9.67 \times 0.3 \approx 2.90

Step 7: Calculate Actual Receipt Actual Receipt=Current QuantityDeduct Amplified AmountCommission Deduction=159.671202.90=36.77\text{Actual Receipt} = \text{Current Quantity} - \text{Deduct Amplified Amount} - \text{Commission Deduction} = 159.67 - 120 - 2.90 = 36.77 (Includes: Principal part 150120=30150-120=30, Net Return part 6.766.76)

Step 8: Calculate Yield Yield=User Net ReturnActual Principal=6.763022.5%\text{Yield} = \frac{\text{User Net Return}}{\text{Actual Principal}} = \frac{6.76}{30} \approx 22.5\%

Step 9: Update System State Deduct corresponding market value, inventory, and share.

Step 10: Update User Proof Deduct redeemed shares and corresponding value/quantity records from user proof.

Note: The protocol has a "Divestment Slicing" protection mechanism. If a single withdrawal exceeds a certain proportion of the total pool (e.g., 1/10), the transaction will fail to prevent whales from crashing the market.

Appendix F: Token Swap Detailed Calculation Process

1. Initial State

1.1 Token A (Input Token)

  • Market Value (VAV_A): 40,000
  • Current Inventory (QAQ_A): 20,000 (Unit Price PA=2.0P_A = 2.0)
  • Trade Fee Rate: 0.01%

1.2 Token B (Output Token)

  • Market Value (VBV_B): 20,000
  • Current Inventory (QBQ_B): 40,000 (Unit Price PB=0.5P_B = 0.5)
  • Trade Fee Rate: 0.01%

2. Input Calculation

User inputs 2,500 Token A for swap.

Step 1: Deduct Input Fee Input Fee=Input Amount×Rate=2,500×0.01%=0.25\text{Input Fee} = \text{Input Amount} \times \text{Rate} = 2,500 \times 0.01\% = 0.25 Δa(Net Input)=2,5000.25=2,499.75\Delta a (\text{Net Input}) = 2,500 - 0.25 = 2,499.75

Step 2: Calculate Transfer Value Calculate value increment ΔV\Delta V brought by Token A using the core formula: ΔV=2VAΔa2QA+Δa=240,0002,499.75220,000+2,499.754,705.4\Delta V = \frac{2 V_A \Delta a}{2 Q_A + \Delta a} = \frac{2 \cdot 40,000 \cdot 2,499.75}{2 \cdot 20,000 + 2,499.75} \approx 4,705.4


3. Output Calculation

Step 3: Calculate Gross Output Quantity Calculate Token B quantity to be output based on transfer value ΔV\Delta V: Δbgross=2QBΔV2VB+ΔV=240,0004,705.4220,000+4,705.48,420.3\Delta b_{\text{gross}} = \frac{2 Q_B \Delta V}{2 V_B + \Delta V} = \frac{2 \cdot 40,000 \cdot 4,705.4}{2 \cdot 20,000 + 4,705.4} \approx 8,420.3

Step 4: Deduct Output Fee Output Fee=Δbgross×Rate=8,420.3×0.01%0.84\text{Output Fee} = \Delta b_{\text{gross}} \times \text{Rate} = 8,420.3 \times 0.01\% \approx 0.84 Δbnet(User Actual Receive)=8,420.30.84=8,419.46\Delta b_{\text{net}} (\text{User Actual Receive}) = 8,420.3 - 0.84 = 8,419.46


4. Post-Trade Update

4.1 Token A Update

  • Market Value: 40,000 (Unchanged)
  • Current Inventory: 20,000+2,499.75+0.25=22,50020,000 + 2,499.75 + 0.25 = 22,500
  • Total Invested Quantity: 30,000+0.25=30,000.2530,000 + 0.25 = 30,000.25 (Fees go into pool)

4.2 Token B Update

  • Market Value: 20,000 (Unchanged)
  • Current Inventory: 40,0008,420.3+0.84=31,580.5440,000 - 8,420.3 + 0.84 = 31,580.54
  • Total Invested Quantity: 30,000+0.84=30,000.8430,000 + 0.84 = 30,000.84 (Fees go into pool)

4.3 Result Analysis

  • User: Pays 2,500 Token A, receives 8,419.46 Token B.
  • Protocol: Collects fees 0.25 A + 0.84 B, accumulated into respective LP pools, increasing LP NAV.
  • Price Impact:
    • Token A Price: 40,000/22,5001.7840,000 / 22,500 \approx 1.78 (Drops)
    • Token B Price: 20,000/31,5800.6320,000 / 31,580 \approx 0.63 (Rises)
    • Protocol automatically completes price discovery.

Appendix G: Token Fee Detailed Calculation and Distribution Process (Fee Logic)

1. Fee Accumulation

Principle: In TTSWAP, fees generated from every trade (whether Token A on the input side or Token B on the output side) are directly retained in the token's Current Inventory (QQ) and Invested Quantity (II), without changing the Market Value (VV) or the LP's Invested Share (SS).

Formula: Inew=Iold+FeeI_{new} = I_{old} + \text{Fee} Net Asset Value (NAV)=I+Accumulated FeesS\text{Net Asset Value (NAV)} = \frac{I + \text{Accumulated Fees}}{S}

  • As trading volume increases, Accumulated Fees\text{Accumulated Fees} continue to accumulate.
  • Therefore, the Net Asset Value per LP Share monotonically increases.

2. Lifecycle Demo

Phase 1: Initial State (Fig 1)

  • User has not entered, initial NAV in system is set to 1.5.

Phase 2: User Investment (Fig 2)

  • User deposits funds, system calculates shares obtained based on current NAV.
  • Suser=User Invested AmountCurrent NAVS_{user} = \frac{\text{User Invested Amount}}{\text{Current NAV}}
  • At this point, the shares held by the user are fixed, but the value behind the shares floats with NAV.

Phase 3: Value Growth (Fig 3)

  • As market trading becomes active, fees are continuously injected into the fund pool.
  • Result: Total token quantity (QQ) in the pool increases, but total share (SS) remains unchanged (assuming no deposits/withdrawals).
  • Effect: NAV rises, each share in the user's hand can now redeem more tokens.

Phase 4: User Divestment & Distribution (Fig 4)

  • User applies to redeem shares.
  • Principal Return: System first calculates and returns the user's original principal.
  • Return Calculation: Total Return=(Redeemed Share×Current NAV)Original Principal\text{Total Return} = (\text{Redeemed Share} \times \text{Current NAV}) - \text{Original Principal}
  • Profit Sharing: Distribute the calculated total return in real-time according to preset ratios (e.g., LP 70%, Platform 30%).

Through this mechanism, TTSWAP achieves Automatic Compounding: LPs do not need to manually claim rewards and restake; fees are retained in the pool to generate new liquidity until settlement upon divestment.