Degen Code

Degen Code

Share this post

Degen Code
Degen Code
Balancer — V2 Stable Pool
Copy link
Facebook
Email
Notes
More

Balancer — V2 Stable Pool

Part V: Balanced and Stable But Still Curvy Tho IDK

Dec 24, 2024
∙ Paid
2

Share this post

Degen Code
Degen Code
Balancer — V2 Stable Pool
Copy link
Facebook
Email
Notes
More
1
Share

Part II of the Balancer series introduced the Weighted Pool and Part III demonstrated how token swaps are accomplished via the associated Vault contract.

Balancer — V2 Pool Contract

Balancer — V2 Pool Contract

BowTiedDevil
·
December 11, 2024
Read full story
Balancer — V2 Vault Contract

Balancer — V2 Vault Contract

BowTiedDevil
·
December 16, 2024
Read full story

Weighted is the most common pool type on Balancer, but there is another important type: Stable.

In this lesson we will investigate the structure of the Stable pool type and examine its interaction with the Vault.

Demonstration Pool

Balancer’s app lists several Stable V2 pairs on mainnet. The pool with the highest liquidity is a WETH-osETH pair at address 0xDACf5Fa19b1f720111609043ac67A9818262850c.

The verified contract code at Etherscan tells us the contract is ComposableStablePool.sol, and it was deployed slightly more than a year ago by a factory contract called ComposableStablePoolFactory.sol at address 0xDB8d758BCb971e482B2C45f7F8a7740283A1bd3A.

Luckily for us, the Balancer V2 monorepo seems to have source code for this factory and pool that align with the deployed contract. This is a newer pool type, so this makes sense.

The Balancer documentation says that the ComposableStablePool contract is a superset of previous Stable variants which have been deprecated. We should concentrate our effort on this contract source, and the selected pool is a good representation of others we’ll find as we continue.

Invariant and Calculations

The Balancer Stable pool math is documented HERE. It gives this invariant:

\(A \cdot n^{n} \cdot \sum_{}^{}x_{i} + D = A \cdot D \cdot n^{n} + \frac{D^{n+1}}{n^{n} \cdot \prod_{}^{}x_{i}}\)

Where:

\(\begin{array}{rl} n &\text{is the number of tokens}\\ x_i &\text{is the balance of token } i \\ A &\text{is the amplification parameter} \end{array}\)

Luckily for us, this is the exact invariant developed by Curve!

I’ve already covered the Curve StableSwap pool architecture in Part I of the Curve series, with coverage of the invariant.

Curve StableSwap Liquidity Pool — Part I: Introduction

Curve StableSwap Liquidity Pool — Part I: Introduction

BowTiedDevil
·
November 29, 2023
Read full story

Helpfully, Balancer’s documentation gives two equations for solving for amounts in and amounts out for a Stable pool.

Calculation: Tokens Out, Given Tokens In

\(\begin{align} & y^2 + \left( \frac{D}{An^n} + \sum_{j \neq out}{x'_j} - D \right) y \frac{D^{n+1}}{An^{2n} \prod_{j \neq out}{x'_j}} = 0 \\ & a_{out} = x_{out} - x'_{out} = x_{out} - y \end{align}\)

Where:

\(\begin{array}{rl} x'_i & \text{is the ending amount of each token} \\ a_{out} & \text {is the amount out} \\ x_{out} & \text{is the starting amount of the output token} \\ y = x'_{out} & \text {is the ending amount of the output token} \\ D & \text{is the pool invariant} \\ A & \text{is the amplification parameter} \\ n & \text{is the number of tokens} \\ \end{array}\)

Calculation: Tokens In, Given Tokens Out

\(\begin{align} & y^2 + \left(\frac{D}{An^n} + \sum_{j \neq in}{x'_j} - D\right)y - \frac{D^{n+1}}{An^{2n} \prod_{j \neq in}{x'_j}} = 0 \\ & a_{in} = x'_{in} - x_{in} = y-x_{in} \end{align}\)

Where:

\(\begin{array}{rl} x'_i & \text{is the ending amount of each token} \\ a_{in} & \text {is the amount in} \\ x_{in} & \text{is the starting amount of the input token} \\ y = x'_{in} & \text {is the ending amount of the input token} \\ D & \text{is the pool invariant} \\ A & \text{is the amplification parameter} \\ n & \text{is the number of tokens} \\ \end{array} \)

Solidity Implementation

This post is for paid subscribers

Already a paid subscriber? Sign in
© 2025 BowTiedDevil
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share

Copy link
Facebook
Email
Notes
More