Minimal flashloan borrower contracts with an extensible rust sdk to abstract wrapping generic onchain calls (similar to multicall3) with flashloans.

flashloan-rs โข

Minimal flashloan borrower contracts with an extensible rust sdk.
Getting Started
Flashloan-rs is published to crates.io as flashloan-rs.
To use the crate in a Rust project, run the cargo add command like so: cargo add flashloan-rs.
Or, add the following to your Cargo.toml:
[dependencies]
flashloan-rs = "0.2.3"
Usage
Flashloan-rs is built to be extremely simple to use.
Quick Construction
,ignore
use std::{str::FromStr, sync::Arc};
use flashloan_rs::prelude::*;
use ethers::prelude::*;
// Create a web3 provider let client = Provider::<Http>::try_from("https://eth-mainnet.g.alchemy.com/v2/your-api-key").unwrap(); let arc_client = Arc::new(client);
// Config let walletaddress = Address::fromstr("YOUR_ADDRESS").unwrap(); let lender = Address::fromstr("LENDERADDRESS").unwrap(); let tokentoflashloan = Address::fromstr("TOKENADDRESSTOFLASHLOAN").unwrap(); let amounttoflashloan = U256::fromdecstr("1000000000000000000").unwrap();
// Create a flashloan builder // Alternatively, these parameters can be set using the builder pattern (see the next example) let mut builder = FlashloanBuilder::new( Arc::clone(&arc_client), // web3 provider 1, // chain id Some(wallet_address), // wallet public address Some(lender), // address of the EIP-3156 Compliant Flash Lender Some(tokentoflashloan), // token address to flashloan Some(amounttoflashloan), // amount to flashloan None, // override the flash borrower contract );
// Deploy the flashloan borrower contract builder.deploy(None, None).await.unwrap();
// Execute the flashloan and grab the transaction receipt let optionaltxreceipt = builder.execute().await.unwrap(); let txreceipt = optionaltx_receipt.unwrap();
Builder Pattern
,ignore
use std::{str::FromStr, sync::Arc};
use flashloan_rs::prelude::*;
use ethers::prelude::*;
// Create a web3 provider let client = Provider::<Http>::try_from("https://eth-mainnet.g.alchemy.com/v2/your-api-key").unwrap(); let arc_client = Arc::new(client);
// Config let walletaddress = Address::fromstr("YOUR_ADDRESS").unwrap(); let lender = Address::fromstr("LENDERADDRESS").unwrap(); let tokentoflashloan = Address::fromstr("TOKENADDRESSTOFLASHLOAN").unwrap(); let amounttoflashloan = U256::fromdecstr("1000000000000000000").unwrap();
// Create a flashloan builder let mut builder = FlashloanBuilder::new(arc_client, 1, None, None, None, None, None);
// Set values using the builder pattern builder.withowner(walletaddress).withlender(lender).withtoken(tokentoflashloan).withamount(amountto_flashloan);
// ...
Blueprint
flashloan-rs
โโ contracts
โ โโ interfaces
โ โ โโ IERC20.sol โ ERC20 interface
โ โ โโ IERC3156FlashBorrower.sol โ Flashloan borrower interface
| | โโ IERC3156FlashLender.sol โ Flashloan lender interface
โ โโ FlashBorrower.huff โ A https://github.com/huff-language Flashloan Receiver Contract Implementation
โ โโ FlashBorrower.sol โ An Extensible Flashloan Receiver Contract
โโ examples
โ โโ custom_borrower.rs โ Flashloan-rs usage with a custom borrower contract
โ โโ pure_arb.rs โ Executing a pure arbitrage with flashloan-rs
โโ lib โ Foundry Libraries
โโ src
โ โโ builder.rs โ The primary rust FlashloanBuilder library
โ โโ contract.rs โ Abi Generated FlashBorrower Contract
โ โโ errors.rs โ Custom errors for flashloan-rs
โ โโ lib.rs โ Module Exports
โโ tests
โ โโ contracts
โ โ โโ FlashBorrower.t.sol โ FlashBorrower.sol test suite
โ โโ crate
| โโ builder.rs โ Unit tests for flashloan-rs
โโ foundry.toml โ Foundry Config
โโ Cargo.toml โ The flashloan-rs Cargo Manifest
License
Acknowledgements
A few very notable repositories that were used as reference: