BalancerPool

class BalancerPool[source]

Bases: AbstractPool

Implementation of Balancer’s constant-weight liquidity pool.

Unlike TFMM pools that can adjust weights dynamically, Balancer pools maintain fixed weight ratios between tokens. These weights are determined at initialization and remain constant, making the pool non-trainable.

Core Features:

  • Fixed weights (unlike TFMM’s dynamic weights)

  • Simple initial weight calculation

  • No parameter processing from web interface needed

  • Non-trainable design

Calculation Modes:

  1. Standard trading with fees (calculate_reserves_with_fees) - Handles regular trading with configurable fees - Supports arbitrage simulation with gas costs - Uses _jax_calc_balancer_reserves_with_fees_using_precalcs

  2. Zero-fee trading (calculate_reserves_zero_fees) - Special case for theoretical analysis - Perfect arbitrage simulation - Uses _jax_calc_balancer_reserve_ratios

  3. Dynamic input trading (calculate_reserves_with_dynamic_inputs) - Supports time-varying fees and parameters - Handles custom trade sequences - Uses _jax_calc_balancer_reserves_with_dynamic_inputs

param params:

Pool parameters including: - initial_weights_logits: Determines fixed token weight ratios

type params:

Dict[str, Any]

param run_fingerprint:

Simulation parameters including: - initial_pool_value: Starting total value - fees: Trading fee percentages - gas_cost: Arbitrage threshold - arb_fees: Arbitrage-specific fees - bout_length: Simulation length - n_assets: Number of tokens - do_arb: Enable/disable arbitrage - arb_frequency: Frequency of arbitrage checks

type run_fingerprint:

Dict[str, Any]

Notes

  • Unlike TFMM pools, no raw_weights_outputs or fine_weight_output methods

  • Simple weight calculation using softmax(initial_weights_logits) if provided

  • Non-trainable by design (is_trainable() returns False)

  • No web interface parameter processing needed (as it has no parameters other than initial_weights_logits)

  • JAX-accelerated calculations for efficiency

__init__()[source]
calculate_reserves_with_fees(params, run_fingerprint, prices, start_index, additional_oracle_input=None)[source]

Calculate reserves considering trading fees and arbitrage costs.

Uses JAX-accelerated function _jax_calc_balancer_reserves_with_fees_using_precalcs for efficient computation. Unlike TFMM pools, this uses constant weights and doesn’t require raw weight calculations.

Implementation Notes:

  1. Extracts local price window using dynamic_slice

  2. Uses constant weights from calculate_initial_weights

  3. Handles arbitrage frequency adjustments

  4. Computes initial reserves based on pool value

  5. Delegates core calculation to jitted external function

param params:

Pool parameters containing initial_weights_logits or initial_weights

type params:

Dict[str, Any]

param run_fingerprint:

Simulation parameters including: - bout_length: Length of simulation window - n_assets: Number of tokens - arb_frequency: How often arbitrage is checked - initial_pool_value: Starting pool value - fees: Trading fee percentages - gas_cost: Arbitrage threshold - arb_fees: Arbitrage-specific fees - do_arb: Enable/disable arbitrage

type run_fingerprint:

Dict[str, Any]

param prices:

Price history array

type prices:

jnp.ndarray

param start_index:

Starting index for the calculation window

type start_index:

jnp.ndarray

param additional_oracle_input:

Not used in BalancerPool, kept for interface compatibility

type additional_oracle_input:

Optional[jnp.ndarray]

returns:

Calculated reserves over time

rtype:

jnp.ndarray

Parameters:
Return type:

Array

calculate_reserves_zero_fees(params, run_fingerprint, prices, start_index, additional_oracle_input=None)[source]

Calculate reserves assuming zero fees and perfect arbitrage.

Uses JAX-accelerated function _jax_calc_balancer_reserve_ratios for efficient computation in the theoretical zero-fee case. Simpler than TFMM implementation due to constant weights.

Implementation Notes:

  1. Uses dynamic_slice for price window

  2. Applies constant weights from calculate_initial_weights

  3. Computes reserve ratios directly

  4. Uses cumprod for reserve calculation

  5. Handles no-arbitrage case via broadcasting

param params:

Pool parameters containing initial_weights_logits or initial_weights

type params:

Dict[str, Any]

param run_fingerprint:

Simulation parameters

type run_fingerprint:

Dict[str, Any]

param prices:

Price history array

type prices:

jnp.ndarray

param start_index:

Starting index for the calculation window

type start_index:

jnp.ndarray

param additional_oracle_input:

Not used in BalancerPool, kept for interface compatibility

type additional_oracle_input:

Optional[jnp.ndarray]

returns:

Calculated reserves over time

rtype:

jnp.ndarray

Parameters:
Return type:

Array

calculate_reserves_with_dynamic_inputs(params, run_fingerprint, prices, start_index, fees_array, arb_thresh_array, arb_fees_array, trade_array, lp_supply_array=None, additional_oracle_input=None)[source]

Calculate reserves with time-varying fees and parameters.

Uses JAX-accelerated function _jax_calc_balancer_reserves_with_dynamic_inputs. Simpler than TFMM version due to constant weights, but handles dynamic parameters for fees and arbitrage thresholds.

Implementation Notes:

  1. Handles time-varying parameters via broadcasting

  2. Uses constant weights throughout

  3. Supports custom trade sequences

  4. Maintains arbitrage frequency adjustments

  5. Validates trade array dimensions

param params:

Pool parameters containing initial_weights_logits or initial_weights

type params:

Dict[str, Any]

param run_fingerprint:

Simulation parameters

type run_fingerprint:

Dict[str, Any]

param prices:

Price history array

type prices:

jnp.ndarray

param start_index:

Starting index for the calculation window

type start_index:

jnp.ndarray

param fees_array:

Time-varying trading fees

type fees_array:

jnp.ndarray

param arb_thresh_array:

Time-varying arbitrage thresholds

type arb_thresh_array:

jnp.ndarray

param arb_fees_array:

Time-varying arbitrage fees

type arb_fees_array:

jnp.ndarray

param trade_array:

Custom trade sequence

type trade_array:

jnp.ndarray

returns:

Calculated reserves over time

rtype:

jnp.ndarray

Parameters:
Return type:

Array

init_base_parameters(initial_values_dict, run_fingerprint, n_assets, n_parameter_sets=1, noise='gaussian')[source]
Parameters:
Return type:

Dict[str, Any]

is_trainable()[source]

Indicate if pool weights can be trained.

Returns:

Always False for BalancerPool as weights are fixed

Return type:

bool