Coinfair Swap Protocol Integration Guide

This document provides guidance on integrating with the Coinfair swap protocol, focusing on how to select the optimal swap path in the frontend.

How to Select the Optimal Swap Path

Pool Types and Fee Combinations

Coinfair supports multiple pool types and fee combinations:

Pool Types (5 types): 1, 2, 3, 4, 5

Fee Types (3 rates): 3, 5, 10

Optimal Path Selection Process

Finding the optimal trading path requires a comprehensive evaluation of all possible combinations:

1. Initialization and Setup

  • Establish connection to blockchain provider

  • Initialize Router and Factory contract interfaces

  • Setup multicall infrastructure for batch queries

  • Define available pool types, fee rates, and intermediary tokens

2. Direct Path Evaluation

  • For all 15 combinations of pool types and fee rates:

    • Create a direct path array: [TokenA, TokenB]

    • Create corresponding poolTypePath and feePath arrays

    • Prepare requests to Router.getAmountsOut() or Router.getAmountsIn()

3. Multi-Hop Path Evaluation

  • For each intermediary token:

    • Skip if intermediary is the same as input or output token

    • For all combinations of first-hop and second-hop parameters:

      • Create a multi-hop path array: [TokenA, Intermediary, TokenB]

      • Create corresponding poolTypePath and feePath arrays with both hops' parameters

      • Prepare requests to Router.getAmountsOut() or Router.getAmountsIn()

4. Comparison and Selection

  • For exact input swaps: select the path with maximum output amount

  • For exact output swaps: select the path with minimum input amount

  • Consider gas costs when comparing direct vs. multi-hop paths

  • Return the optimal path information (path, poolTypePath, feePath, expected amounts)

TypeScript Implementation

Last updated