Swap
Swapping is the primary operation of a DEX, allowing users to exchange one asset for another. In DeDust Protocol V2, swaps are executed against a Pool contract using the Constant Product formula ().
Depending on the input asset (Native TON or Jetton), the interaction flow differs slightly, but the core swap logic
defined in tgithe SwapPayload remains the same.
Swap Payload
Swaps are implemented using the payment abstraction. This means that the instructions for the swap are provided as a Payment Payload, which is attached to the asset transfer.
SwapPayload is the specific payment payload structure used to request a swap. It encapsulates all the necessary
parameters for the trade, independent of whether you are paying with TON or Jettons.
struct (0xc442500f) SwapPayload {
minimalAmountOut: coins
deadline: uint40
next: Cell<SwapStep>?
partnerConfig: PartnerConfig?
referrerConfig: ReferrerConfig?
}Parameters
| Field | Description |
|---|---|
minimalAmountOut | Slippage Protection. The minimum amount of the output asset you are willing to accept. If the pool cannot provide at least this amount (due to price changes or low liquidity), the transaction will be aborted and assets refunded. |
deadline | Time Limit. A Unix timestamp. If the transaction is processed by the blockchain after this time, it will be rejected. This protects against miner withholding attacks where a transaction is held until market conditions are unfavorable. |
next | Multi-hop Routing. An optional reference to a SwapStep. If provided, the output of the current swap is automatically forwarded to another pool defined in SwapStep to perform the next trade. |
partnerConfig | Optional configuration for partner fees. |
referrerConfig | Optional configuration for referrer fees. |
Swapping TON (Native -> Jetton)
To swap TON for a Jetton, you must send a PayNative message directly to the Pool contract.
Implementation
When constructing the PayNative message:
- Amount: The amount of TON you want to swap.
- Payment Payload: The
SwapPayloadcell. - Payout Config: Defines where to send the output Jettons and where to send refunds if the swap fails.
Flow Diagram
The following diagram illustrates the flow of a swap from TON to a Jetton:
Swapping Jettons (Jetton → TON / Jetton)
To swap a Jetton (for TON or another Jetton), you perform a standard Jetton Transfer to the Pool's address.
The swap parameters are passed inside the forward_payload of the transfer.
The forward_amount in the transfer must be sufficient to cover the gas costs of the swap execution and any
subsequent transfers.
Payload Structure
The forward_payload of the jetton transfer MUST contain a PayJetton structure.
struct (0xcbc33949) PayJetton {
paymentPayload: Cell<SwapPayload>
payoutConfig: Cell<ExtendedPayoutConfig>
}Jetton to TON Flow
Jetton to Jetton Flow
Swapping one Jetton (Asset X) for another Jetton (Asset Y) follows the same initial process. The Pool detects the output asset is a Jetton and sends a transfer instruction to its own wallet for Asset Y.
Multi-hop Swaps
DeDust Protocol V2 supports atomic multi-hop swaps (e.g., Token A → TON → Token B) in a single transaction chain. This
is achieved using the next field in the SwapPayload.
Instead of sending the output assets to the user, the pool wraps them into a new payment message and forwards them to
the next pool address defined in SwapStep.
SwapStep Structure
struct SwapStep {
pool: address
minimalAmountOut: coins
deadline: uint40
next: Cell<SwapStep>?
}Example Flow (A → TON → B)
- User initiates swap of Token A at Pool 1 (A/TON).
- The
SwapPayloadprovided to Pool 1 has anextfield containing aSwapStep. - The
SwapSteppoints to Pool 2 (TON/B). - Pool 1 executes A → TON.
- Instead of paying TON to the user, Pool 1 sends a
PayNativemessage to Pool 2 with the resulting TON and thenextpayload. - Pool 2 receives the TON, executes TON → B, and sends Token B to the user.