Skip to content

Balancer

Connector for Balancer DEX.

almanak.framework.connectors.balancer

Balancer Flash Loan Connector.

This module provides an adapter for executing flash loans via Balancer's Vault contract. Balancer flash loans have zero fees (no premium), making them ideal for arbitrage.

Balancer Vault features: - Zero-fee flash loans (no premium) - Single or multi-token flash loans - All supported tokens available from liquidity pools - Simple interface: flashLoan(recipient, tokens, amounts, userData)

Supported chains: - Ethereum - Arbitrum - Optimism - Polygon - Base

Example

from almanak.framework.connectors.balancer import BalancerFlashLoanAdapter, BalancerFlashLoanConfig

config = BalancerFlashLoanConfig( chain="arbitrum", wallet_address="0x...", ) adapter = BalancerFlashLoanAdapter(config)

Execute flash loan

result = adapter.flash_loan( recipient="0x...", tokens=["USDC"], amounts=[Decimal("100000")], )

BalancerFlashLoanAdapter

BalancerFlashLoanAdapter(
    chain: str, protocol: str = "balancer"
)

Adapter for Balancer Vault flash loans.

Balancer flash loans have zero fees, making them ideal for arbitrage strategies. The Vault contract holds all pool liquidity, enabling large flash loans.

Example

config = BalancerFlashLoanConfig( chain="arbitrum", wallet_address="0x...", ) adapter = BalancerFlashLoanAdapter(config)

Get flash loan calldata

calldata = adapter.get_flash_loan_calldata( recipient="0x...", tokens=["0x...USDC", "0x...WETH"], amounts=[1000000000, 500000000000000000], user_data=b"", )

Initialize the adapter.

Parameters:

Name Type Description Default
chain str

Target blockchain

required
protocol str

Protocol name (always "balancer")

'balancer'

get_vault_address

get_vault_address() -> str

Get the Balancer Vault address.

get_flash_loan_calldata

get_flash_loan_calldata(
    recipient: str,
    tokens: list[str],
    amounts: list[int],
    user_data: bytes = b"",
) -> bytes

Generate calldata for a Balancer flash loan.

Balancer flashLoan function: flashLoan( IFlashLoanRecipient recipient, IERC20[] memory tokens, uint256[] memory amounts, bytes memory userData )

Parameters:

Name Type Description Default
recipient str

Contract address that will receive and handle the flash loan

required
tokens list[str]

List of token addresses to borrow

required
amounts list[int]

List of amounts to borrow (in token's smallest units)

required
user_data bytes

Extra data to pass to receiver's receiveFlashLoan

b''

Returns:

Type Description
bytes

Encoded calldata for the flashLoan transaction

get_flash_loan_simple_calldata

get_flash_loan_simple_calldata(
    recipient: str,
    token: str,
    amount: int,
    user_data: bytes = b"",
) -> bytes

Generate calldata for a single-token flash loan.

This is a convenience method that wraps get_flash_loan_calldata for single-token flash loans.

Parameters:

Name Type Description Default
recipient str

Contract address that will receive the flash loan

required
token str

Token address to borrow

required
amount int

Amount to borrow (in token's smallest units)

required
user_data bytes

Extra data to pass to receiver's receiveFlashLoan

b''

Returns:

Type Description
bytes

Encoded calldata for the flashLoan transaction

estimate_flash_loan_gas

estimate_flash_loan_gas() -> int

Estimate gas for a multi-token flash loan (base only, not including callbacks).

estimate_flash_loan_simple_gas

estimate_flash_loan_simple_gas() -> int

Estimate gas for a single-token flash loan (base only, not including callbacks).

BalancerFlashLoanConfig dataclass

BalancerFlashLoanConfig(chain: str, wallet_address: str)

Configuration for Balancer flash loan adapter.

Attributes:

Name Type Description
chain str

Target blockchain (ethereum, arbitrum, optimism, polygon, base)

wallet_address str

Address executing the flash loan

BalancerFlashLoanParams dataclass

BalancerFlashLoanParams(
    recipient: str,
    tokens: list[str],
    amounts: list[int],
    user_data: bytes = bytes(),
)

Parameters for a Balancer flash loan.

Attributes:

Name Type Description
recipient str

Contract that will receive the flash loan and handle callbacks

tokens list[str]

List of token addresses to borrow

amounts list[int]

List of amounts to borrow (in wei)

user_data bytes

Arbitrary bytes to pass to the receiver's receiveFlashLoan()

TransactionResult dataclass

TransactionResult(
    success: bool,
    calldata: bytes = bytes(),
    to: str = "",
    value: int = 0,
    gas_estimate: int = 0,
    error: str | None = None,
)

Result of a transaction operation.

Attributes:

Name Type Description
success bool

Whether the operation succeeded

calldata bytes

Generated calldata for the transaction

to str

Target contract address

value int

ETH value to send (always 0 for flash loans)

gas_estimate int

Estimated gas for the transaction

error str | None

Error message if failed