Crate balancer_sdk

Source
Expand description

§Balancer Rust SDK

§WARNING: THIS CRATE IS IN PRE-RELEASE ALPHA AND CHANGING ALMOST DAILY. THE INTERFACES MAY CHANGE.

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:

§Documentation

For complete usage documentation, including all interfaces, definitions, and extensive usage examples, see the Rust Doc

§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/crates/balancer_sdk. 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 Usage

An example of how to get the WETH address from the Vault contract API

#[tokio::main]
async 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();
}

§Testing

  • Integration tests go in ./tests
  • Unit tests are colocated with files
  • Example tests are in the documentation

To run tests:

$ cargo test -all

§Contributing

TBD

§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

The GIT repository contains a number of complete and working examples which can be found in the ./examples directory.

To run an example:

$ cargo run --example [name]

Modules§

constants
helpers
pools
Defines the pool structs and their methods.
vault
Defines the vault struct and its methods.

Macros§

addr
Simple conversion from a string slice to an Address
i256
Simple conversion from &str or usize to a I256.
pool_id
swap_fee
Simple conversion from &str or usize to a SwapFeePercentage.
u256
Simple conversion from &str or usize to a U256.

Structs§

BatchSwapStep
ERC20
A generic contract for an ERC20 token Generated by ethcontract
ExitPoolRequest
See Balancer documentation
FromDecStrErr
FundManagement
The FundManagement struct defines where the input tokens for the first swap are coming from and where any tokens received from swaps should be sent. See Balancer documentation
I256
Little-endian 256-bit signed integer.
JoinPoolRequest
See Balancer documentation
OracleAccumulatorQuery
OracleAverageQuery
PoolBalanceOp
PoolBalanceOp describes the type of operation (deposit/withdraw/update), the pool ID, the token, and the amount.
PoolId
PrivateKey
A secret key used for signing and hashing.
SingleSwap
The SingleSwap struct defines which pool we’re trading with and what kind of swap we want to perform.
SwapFeePercentage
A struct for working with SwapFeePercentages
SwapRequest
U256
Little-endian large integer type 256-bit unsigned integer.
UserData

Enums§

Account
The account type used for signing the transaction.
PoolBalanceOpKind
PoolSpecialization
SwapKind
Variable

Traits§

FromStr
Parse a value from a string

Type Aliases§

Address
Address
Bytes32
IERC20
ERC20 token address
Web3