DEV Community

ProgramCrafter
ProgramCrafter

Posted on

Multi-currency swapping pool: MulDEX

Exploring decentralized exchange algorithms, I've found out that most are focused on swap of two tokens, but theoretically they can be extended to multiple ones without loss of functionality.

Examples of two-token liquidity pools

A, B - reserve of tokens in the pool (so, they don't belong to any user, except maybe liquidity providers).

  • AB=constA * B = const .

    For example, A=1000,B=2000A = 1000, B = 2000 .

    Suppose person wants to swap 5 A tokens for some B. Then, pool will have A=1005A' = 1005 , and to maintain invariant AB=2000000A * B = 2'000'000 it will retain B=1990B' = 1990 , sending 10 B out.

  • A3B+AB3=constA^3 * B + A * B^3 = const .

  • ApB1p=constA^p * B^{1-p} = const (Maniswap; p changes over liquidity additions and is used to keep the price same).

Natural extensions

  1. ABCD...=constA * B * C * D * ... = const .

    Any swap here is equivalent to swap in basic pool, since amount of any other token except two chosen is constant. Though, I have been said that it has problems with liquidity.

  2. AB+AC+BC+AD+...=constA * B + A * C + B * C + A * D + ... = const . Sum of pairwise products of token reserves is constant.

    Let's calculate how many B (dB) will pool return when receiving some A (dA).

    A+B+C+D+...=y0AB+AC+BC+...=x2AB+2AC+2BC+...=2x(A+B+C+...)2=y02=2x+A2+B2+C2+...(y0+dA+dB)2=2x+A2+2AdA+dA2+B2+2BdB+dB2+C2+...(dA+dB)(2y0+dA+dB)=2AdA+dA2+2BdB+dB2dB2+dB(2y0+2dA)+dA2+2day0=dB2+dA2+2BdB+2AdA2dB(y0+dAB)=2AdAdB=AdA/(y0+dAB) A + B + C + D + ... = y_0 \newline --- \newline A * B + A * C + B * C + ... = x \newline 2 * A * B + 2 * A * C + 2 * B * C + ... = 2x \newline (A + B + C + ...)^2 = y_0^2 = 2x + A^2 + B^2 + C^2 + ... \newline (y_0 + dA + dB)^2 = 2x + A^2 + 2*A*dA + dA^2 + B^2 + 2*B*dB + dB^2 + C^2 + ... \newline --- \newline (dA + dB)(2y_0 + dA + dB) = 2*A*dA + dA^2 + 2*B*dB + dB^2 \newline dB^2 + dB(2y_0 + 2dA) + dA^2 + 2da*y_0 = dB^2 + dA^2 + 2*B*dB + 2*A*dA \newline 2dB*(y_0+dA-B) = 2*A*dA \newline dB = A * dA / (y_0 + dA - B)

    I haven't found any problems with such a pool so I think it can be used to shorten swaps (now, some can take several steps). It can even be used without computers, meaning possible replacement for barter!

Top comments (0)