osmosis-labs / osmosis

The AMM Laboratory
https://app.osmosis.zone
Apache License 2.0
887 stars 580 forks source link

feat: split route multi hop swap message #4869

Closed p0mvn closed 1 year ago

p0mvn commented 1 year ago

Background

Currently, we only support multi-hop swaps that do not allow for route splitting within the same denom pair.

Splitting swaps across multiple pools for the same token pair can be beneficial for several reasons, primarily relating to reduced slippage, price impact, and potentially lower fees. Here's a detailed explanation of these advantages:

Reduced slippage: When a large trade is executed in a single pool, it can be significantly affected if someone else executes a large swap against that pool.

Lower price impact: when executing a large trade in a single pool, the price impact can be substantial, leading to a less favorable exchange rate for the trader. By splitting the swap across multiple pools, the price impact in each pool is minimized, resulting in a better overall exchange rate.

Improved liquidity utilization: Different pools may have varying levels of liquidity, fees, and price curves. By splitting swaps across multiple pools, the router can utilize liquidity from various sources, allowing for more efficient execution of trades. This is particularly useful when the liquidity in a single pool is not sufficient to handle a large trade or when the price curve of one pool becomes less favorable as the trade size increases.

Potentially lower fees: In some cases, splitting swaps across multiple pools may result in lower overall fees. This can happen when different pools have different fee structures, or when the total fee paid across multiple pools is lower than the fee for executing the entire trade in a single pool with higher slippage.

Suggested Design

The core of our smart router is going to be implemented in frontend. However, we still want to expose an interface for the frontend to interact with via single transaction. There are several options to implementing this with different trade-offs. The trade-offs are discussed next in this section.

The following are the parameters that we aim to optimize for:

Option 1: Implement a new on-chain swap message with support for split routes and multi-hop.

Pros:

Cons:

Option 2: Implement a Cosmwasm contract with split route functionality.

General Pros:

General Cons:

2.1 Completely New Contract

Pros:

Cons:

2.2 Integrate Existing Swap Router

Pros:

Cons:

General Details

Suggested message structure

# particularly for balancer pools & large trades, split-swapping through high liquidity pools will achieve a better price
{
  routes [
    {
      pools: [
        {
          pool_id: 1
          token_out_denom: "ujuno"
        },
        {
          pool_id: 6
          token_out_denom: "uusdc"
        }
      ]
      tokenInAmount: Int
    },
    {
      pools: [
        {
          pool_id: 4
          token_out_denom: "ustars"
        },
        {
          pool_id: 3
          token_out_denom: "ujuno"
        },
        {
          pool_id: 2
          token_out_denom: "uusdc"
        }
      ]
      tokenInAmount: Int
    }
  ],
  tokenInDenom: "uosmo",
  tokenOutDenom: "uusdc",
  minOutAmount: Int
}

# OSMO/DAI (balancer) & OSMO/DAI (CL)
{
  routes [
    {
      pools: [
        {
          pool_id: 1
          token_out_denom: "udai"
        },
      ]
      tokenInAmount: Int
    },
    {
      pools: [
        {
          pool_id: 4
          token_out_denom: "udai"
        },
      ]
      tokenInAmount: Int
    }
  ],
  tokenInDenom: "uosmo",
  tokenOutDenom: "udai"
  minOutAmount: Int
}
# at the end minOutAmount is checked (for slippage)

Sources

Acceptance Criteria

p0mvn commented 1 year ago

Haven't had time to update but going to pursue an on-chain message since there are concerns around reverts with cosmwasm and batching