BalancerPool
- class BalancerPool[source]
Bases:
AbstractPoolImplementation 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:
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
Zero-fee trading (calculate_reserves_zero_fees) - Special case for theoretical analysis - Perfect arbitrage simulation - Uses _jax_calc_balancer_reserve_ratios
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
- 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:
Extracts local price window using dynamic_slice
Uses constant weights from calculate_initial_weights
Handles arbitrage frequency adjustments
Computes initial reserves based on pool value
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
- 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:
Uses dynamic_slice for price window
Applies constant weights from calculate_initial_weights
Computes reserve ratios directly
Uses cumprod for reserve calculation
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
- 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:
Handles time-varying parameters via broadcasting
Uses constant weights throughout
Supports custom trade sequences
Maintains arbitrage frequency adjustments
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