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

Overview

flashloan-rs • ci license solidity Crates.io

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

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 wallet_address = Address::from_str("YOUR_ADDRESS").unwrap();
let lender = Address::from_str("LENDER_ADDRESS").unwrap();
let token_to_flashloan = Address::from_str("TOKEN_ADDRESS_TO_FLASHLOAN").unwrap();
let amount_to_flashloan = U256::from_dec_str("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(token_to_flashloan),   // token address to flashloan
    Some(amount_to_flashloan),  // 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 optional_tx_receipt = builder.execute().await.unwrap();
let tx_receipt = optional_tx_receipt.unwrap();

Builder Pattern

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 wallet_address = Address::from_str("YOUR_ADDRESS").unwrap();
let lender = Address::from_str("LENDER_ADDRESS").unwrap();
let token_to_flashloan = Address::from_str("TOKEN_ADDRESS_TO_FLASHLOAN").unwrap();
let amount_to_flashloan = U256::from_dec_str("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.with_owner(wallet_address).with_lender(lender).with_token(token_to_flashloan).with_amount(amount_to_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

MIT

Acknowledgements

A few very notable repositories that were used as reference:

You might also like...
Extensible, Minimal Template for Sway Projects

crib • Extensible, Minimal Template for Sway Projects. Developing First Time? New to rust? Install with: curl --proto '=https' --tlsv1.2 -sSf https://

Symmetric key-wrapping algorithms

RustCrypto: Key Wrapping Functions Collection of symmetric Key Wrapping Functions (KW) written in pure Rust. About "Key Wrapping" describes symmetric

A minimal template for building smart contracts with Cairo 1.0

Minimal Cairo 1.0 Template Built with auditless/cairo-template A minimal template for building smart contracts with Cairo 1.0 using the Quaireaux proj

Minimal template for simulating contracts with arbiter.

arbiter-template Minimal template for simulating contracts with arbiter. Usage Clone this repository git clone https://github.com/primitivefinance/arb

A Minimal Windows SDK.

Minimal Windows 10 SDK Installs only the necessary Windows 10 .lib files to save you having to download the full Visual Studio package. You can either

Go to Rust calls with Apache Arrow datatypes.
Go to Rust calls with Apache Arrow datatypes.

🔎 Overview alloy is a standalone Go module that enables calls to Rust code with Apache Arrow datatypes through its defined C data interface. The over

Generate Nix fetcher calls from repository URLs [maintainer=@figsoda]

nurl Generate Nix fetcher calls from repository URLs $ nurl https://github.com/nix-community/patsh v0.2.0 2/dev/null fetchFromGitHub { owner = "nix

Sample lightning node command-line app built on top of Ldk Node (similar to ldk-sample).

ldk-node-sample Sample lightning node command-line app built on top of Ldk Node (similar to ldk-sample ). Installation git clone https://github.com/op

An easy-to-use generic trainer for Linux, written in Rust.
An easy-to-use generic trainer for Linux, written in Rust.

An easy-to-use generic trainer for Linux written in Rust, with basic memory hacking features. Use it for your games or applications where needed, there is also the tuxtraind daemon which will probe for processes and apply matching trainers automatically.

Owner
White Noise
Dark Forest Operations Control | Semper Invictus
White Noise
A simple example demonstrating cross-contract calls in CosmWasm smart contracts

Cross-contract calls This tutorial demonstrates cross-contract calls with CosmWasm v1. Overview An end user calls the reservation contract to register

csli Tools 3 Sep 12, 2022
An example re-entrancy attack on a flashloan vault.

CosmWasm re-entrancy Exploit Example The exploit example is located in contracts/liquidity_hub/vault-network/exploit_contract. A vault in contracts/li

null 11 Dec 29, 2022
L2 validity rollup combined with blind signatures over elliptic curves inside zkSNARK, to provide offchain anonymous voting with onchain binding execution on Ethereum

blind-ovote Blind-OVOTE is a L2 voting solution which combines the validity rollup ideas with blind signatures over elliptic curves inside zkSNARK, to

Aragon ZK Research 3 Nov 18, 2022
Tool to retrieve mev-share events data and scan for refunds onchain

MEV-Share Analysis This repository can be used to retrieve historical events sent to mev-share and scan for any refunds that were sent to the users. T

Yash Atreya 7 Nov 30, 2023
evm2near compiles Solidity contracts into NEAR WebAssembly contracts.

EVM → NEAR evm2near is a project for compiling EVM bytecode into wasm bytecode, with the particular goal of having that wasm artifact be executable on

Aurora 125 Dec 3, 2022
Ticketed Discreet Log Contracts (DLCs) to enable instant buy-in for wager-like contracts on Bitcoin.

dlctix Ticketed Discreet Log Contracts (DLCs) to enable instant buy-in for wager-like contracts on Bitcoin. This project is part of the Backdrop Build

null 7 Feb 29, 2024
A template to build smart contracts in Rust to run inside a Cosmos SDK module on all chains that enable it.

CosmWasm Starter Pack This is a template to build smart contracts in Rust to run inside a Cosmos SDK module on all chains that enable it. To understan

null 1 Mar 7, 2022
Unofficial Rust SDK library for Uniswap smart contracts.

uniswap-rs Unofficial Rust SDK library for Uniswap smart contracts. Quickstart Add this to your Cargo.toml: [dependencies] uniswap-rs = { git = "https

HopefulHeart 3 Sep 22, 2023
Unofficial Rust SDK library for Uniswap smart contracts.

uniswap-rs Unofficial Rust SDK library for Uniswap smart contracts. Quickstart Add this to your Cargo.toml: [dependencies] uniswap-rs = { git = "https

SlickCharmer 3 Mar 29, 2024
Crib - Extensible, Minimal Template for Sway Projects

crib • Extensible, Minimal Template for Sway Projects. Developing First Time? New to rust? Install with: curl --proto '=https' --tlsv1.2 -sSf https://

velleity.eth 17 Jul 31, 2022