balancer_sdk 0.1.6-alpha

A Rust SDK which provides commonly used utilties for interacting with Balancer Protocol V2
Documentation

Balancer Rust SDK

This project was funded by the Balancer Grants subDAO. Read the press release on Medium.

Rust tools for interacting with Balancer Protocol V2 in Rust.

DISCLAIMER: While balancer-rs is intended to be a useful tool to simplify interacting with Balancer V2 Smart Contracts, this package is an ALPHA-build and should be considered as such. Use at your own risk! This package is capable of sending Ethereum (or EVM compatible) tokens controlled by whatever private key you provide. User assumes all liability for using this software; contributors to this package are not liable for any undesirable results. Users are STRONGLY encouraged to experiment with this package on testnets before using it on mainnet with valuable assets.

Project Goal

There isn’t currently any Rust support in the Balancer ecosystem, so this SDK is a great way to attract Rust developers, as well as bring a popular, fast, and secure language to the growing collection of SDKs Balancer provides. Specifically, the Rust SDK will provide full Rust language support for interacting with:

Usage

balancer-rs has been tested on:

  • MacOS using Rust 2021 edition

Install from crate

The balancer-rs sdk can be installed as a crate from https://crates.io/. Simply add the following to your [dependencies] in your Rust project's Cargo.toml file:

[dependencies]
balancer_sdk = "*"

Once installed, you will be able to use the Balancer Rust module as follows:

Example Vault usage (see in examples):

fn main() {
  let rpc_url = "https://rpc.flashbots.net/";
  let transport = ethcontract::web3::transports::Http::new(rpc_url).unwrap();
  let web3 = ethcontract::Web3::new(transport);

  let vault_instance = balancer_sdk::vault::Vault::new(web3);

  let weth_address = vault_instance.weth().call().await.unwrap();
}

Example to get the vault address via a pool (see in examples):

use balancer_sdk::helpers::macros::*;

fn main() {
  let rpc_url = "https://rpc.flashbots.net/";
  let transport = ethcontract::web3::transports::Http::new(rpc_url).unwrap();
  let web3 = ethcontract::Web3::new(transport);

  let pool_address = addr!("0x01abc00e86c7e258823b9a055fd62ca6cf61a163");
  let weighted_pool_instance = balancer_sdk::pools::WeightedPool::new(web3, addr!(pool_address));
  let vault_address = weighted_pool_instance.getVault().call().await.unwrap();
}

Testing

Integration tests go in ./tests

To run tests:

cargo test

Contributing

Adding new pool support

Balancer continues to add new pool types that have new contract APIs. To add support for a new pool:

  • Add a new json file with a contract name (important) and abi filed with the contract's abi
  • Add the pool to the build.rs script like the others
  • Run cargo build
  • Add the new pool to the pools module
  • Done!

Examples

To see examples of all Vault methods available, see the examples here.

To run an example:

cargo run --example [name]

Examples List - Vault Methods

Authorization

Internal Balances

  • getInternalBalances
  • manageUserBalance

Pools

  • regiserPools
  • getPool
  • registerTokens
  • deregisterTokens
  • getPoolTokenInfo
  • getPoolTokens

Joins and Exits

  • joinPool
  • exitPool

Single Swaps

  • swap

Batch Swaps

  • batchSwap
  • queryBatchSwap

Flash Loans

  • flashLoan

Asset Management

  • managePoolBalance

Miscellaneous

  • getProtocolFeesCollector
  • setPaused
  • WETH #weth

Examples List - Pool Methods

Base Pool

  • getVault
  • getPoolId
  • setSwapFeePercentage
  • setPaused
  • on{Join,Exit}Pool
  • on{Join,Exit}Pool

Weighted Pool

  • onSwap

WeightedPool2Tokens

  • onSwap
  • enableOracle
  • getMiscData
  • getLargestSafeQueryWindow
  • getLatest
  • getTimeWeightedAverage
  • getPastAccumulators

LiquidityBootstrappingPool

API

  • onSwap
  • getSwapEnabled
  • getGradualWeightUpdateParam
  • getGradualWeightUpdateParam

Permissioned Functions

  • setSwapEnabled
  • updateWeightsGradually

ManagedPools

API

  • onSwap
  • getSwapEnabled
  • getManagementSwapFeePercentage
  • getMinimumWeightChangeDuration
  • getCollectedManagementFees
  • getCollectedManagementFees

Permissioned Functions

  • setSwapEnabled
  • updateWeightsGradually
  • withdrawCollectedManagementFees

StablePools

API

  • onSwap
  • getAmplificationParameter

Permissioned Functions

  • startAmplificationParameterUpdate
  • stopAmplificationParameterUpdate

MetaStablePools

API

  • onSwap
  • getAmplificationParameter
  • enableOracle
  • getMiscData
  • getLargestSafeQueryWindow
  • getLatest
  • getTimeWeightedAverage
  • getPastAccumulators
  • getRateProviders
  • getPriceRateCache
  • updatePriceRateCache

Permissioned Functions

  • startAmplificationParameterUpdate
  • stopAmplificationParameterUpdate
  • setPriceRateCacheDuration