DEV Community

Jerome Barton
Jerome Barton

Posted on

SunSwap Smart Contracts Explained: From Factory to Position

SunSwap runs on TRON through factories that create pools, pools that hold tokens and price trades, routers that move swaps through those pools, and a position manager that records V3 liquidity.

Factories create pools, and routers connect them

A factory creates a pool contract and records its address. In SunSwap V2, each TRC-20 token pair has one pair contract. In SunSwap V3, the factory can create separate pools for the same pair at different fee tiers. Anyone can create a pool, while a router can serve many pools through one entry point.

The pool holds the tokens available for trading. Its contract uses the amounts in the pool to determine how much output a swap receives, collects the trade fee, and updates its balances afterward. A larger trade relative to those balances moves the price further.

The router takes a user's approved input tokens and calls the pools in the chosen order. It checks the trade's minimum acceptable output and deadline; if either condition fails, the swap reverts. That is the path from tokens in the wallet, through changing pool balances, to output tokens returned to the wallet.

Imagine swapping USDT for SUN through TRX. The router pulls USDT, trades it in a USDT/TRX pool, passes the resulting TRX into a TRX/SUN pool, then sends SUN to the wallet. In V2 and V3 token-pair pools, the middle asset is wrapped TRX, the token form of TRX. SunSwap V1 uses TRX as the bridge between token swaps.

SunSwap V2 has a pair contract for each token pair, but a trade can reach more than one pair. The SunSwap exchange illustrates this with its Universal Router, which can route a single trade across pools from different versions.

V3 liquidity belongs to individual positions

V3 liquidity is recorded as an individual position tied to a chosen price range. When a provider adds tokens, the position manager issues an NFT: a unique token identifying that position and its range. To remove liquidity or collect its accrued fees, the owner acts through the position manager using that NFT's ID.

V2 records ownership differently. Its pair contract holds both token reserves and mints fungible TRC-20 LP tokens. Each LP token represents the same kind of share of that pair's liquidity. A V3 NFT instead identifies one provider's settings, so two positions in the same pool need not behave alike.

Say two providers put the same tokens into the same V3 pool at the same fee tier. One chooses a narrow price range; the other chooses a wider range. If the market price moves outside the narrow range but remains inside the wider one, only the wider position remains active for trades at that price. The difference comes from the ranges recorded for their NFTs, not from a different router. The V3 contract reference shows the position manager functions used to inspect and reduce those positions.

Related questions

The version determines which factory, pool, router, and liquidity receipt to inspect.

Where to Find SunSwap Contract Addresses on TRONSCAN?

Start with the published address for the version's factory or router, then search that address on TRONSCAN. To identify a particular pool, read the V2 factory's getPair result for its two token addresses, or the V3 factory's getPool result for the tokens and fee tier. Search the returned pool address on TRONSCAN to inspect that contract. The published V2 contract addresses provide a starting point.

Is SunSwap Based on Uniswap Code?

Its V2 factory, pair, and router follow the Uniswap V2 contract pattern; V3 uses the concentrated liquidity pattern associated with Uniswap V3. Those shared roles explain the architecture, while the published V2 source shows the implementation to inspect for code-level questions. When tracing your own liquidity, identify its version and receipt first: a V2 LP token points to a pair, while a V3 NFT ID points to the position manager. That saves time looking for a V3 withdrawal in the pair contract.

Top comments (0)