Skip to content

Repository files navigation

TownSquare v2 Core

TownSquare is an intents-based, chain-abstraction liquidity infrastructure that transforms the traditional inventory-based trading paradigm into a credit-based system.

How TownSquare V2 Core Works

When users want to swap token X for token Y, their options are typically limited to either CEX or AMM exchanges like Uniswap. With AMMs, users often face slippage losses, especially when trading tokens with insufficient liquidity.

To address this challenge, TownSquare supports three quoting mechanisms:

  1. RFQ Mode: TownSquare's partnered market makers provide firm, zero-slippage quotes
  2. Traditional AMM quotes: Routing through protocols like Uniswap or 1inch

TownSquare automatically selects the better quote for the swapper.

Credit Margin Engine

While TownSquare may appear to be a traditional RFQ system, it goes beyond by supporting a transition from inventory-based to credit-based trading.

For example:

If Alice wants to sell 10 ETH at 3000 USDC each to a market maker, traditional inventory-based systems would require the market maker to hold 30,000 USDC. However, with TownSquare's credit-based system, market makers can borrow assets from TownSquare's credit vault contract (subject to their credit limit). Market makers only need to pay borrowing fees after a certain period (rates reference perpetual DEX funding rates).

Market makers can choose to operate purely on the credit-based model or utilize both modes simultaneously, significantly improving capital efficiency.

Repository Structure

TownSquareLPToken.sol is a yield-bearing token. Each underlying asset corresponds to a unique LP token contract. LP investors can deposit assets through this contract, but unlike traditional ERC20 tokens, deposited assets are transferred to the CreditVault contract instead of remaining in the LP token contract.

The yield for TownSquareLPToken comes from borrowing fees generated when market makers utilize funds from the credit vault.

CreditVault.sol is TownSquare's core contract holding most assets. All market maker swaps using credit vault assets are executed through this contract.

Example: When Alice sells one ETH at 3000 USDC/ETH, and Halo (market maker) trades using the credit vault, the position changes are reflected as:

position[halo][ETH] += 1;
position[halo][USDC] -= 3000;

The CreditVault contract provides key functions:

  • settle: Market makers can close positions
  • repay: Market makers can repay borrowed assets
  • addCollateral: Market makers can provide TownSquareLPToken as collateral
  • removeCollateral: Market makers can reduce collateral
  • liquidation: Trusted liquidators can liquidate underwater market maker positions

TownSquareRouter.sol serves as the sole entry point for on-chain interactions between swappers and TownSquare, similar to other DeFi router contracts. Users execute trades by calling the Router contract with TownSquare-provided calldata containing market maker-signed quote signatures.

TownSquareRFQPool.sol is a supporting contract. TownSquare supports two pool types:

  1. CreditPool: Uses CreditVault as treasury, handling trades through the credit vault contract
  2. Inventory Pool: Uses market maker's EOA account as treasury for direct asset exchange

Both pools use the TownSquareRFQPool template contract, primarily differing in their treasury address.

Usage

Build

$ forge build

Test

$ forge test

Format

$ forge fmt

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages