Skip to content

Commit

Permalink
feat - add individual supply and borrow functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Dvisacker committed Sep 26, 2024
1 parent 6cf2b1e commit 7283cb2
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 31 deletions.
77 changes: 50 additions & 27 deletions src/aave.rs → src/bot.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::provider::{get_provider, SignerProvider};
use crate::provider::SignerProvider;
use alloy::sol;
use alloy::{providers::WalletProvider, transports::BoxTransport};
use alloy_primitives::{Address, U256};
Expand Down Expand Up @@ -26,20 +26,23 @@ sol! {
}
}

pub struct AaveLooper {
type Token = IERC20Instance<BoxTransport, Arc<SignerProvider>>;
type LendingPool = ILendingPoolInstance<BoxTransport, Arc<SignerProvider>>;

pub struct AaveBot {
provider: Arc<SignerProvider>,
signer_address: Address,
asset_address: Address,
lending_pool: ILendingPoolInstance<BoxTransport, Arc<SignerProvider>>,
asset: IERC20Instance<BoxTransport, Arc<SignerProvider>>,
lending_pool: LendingPool,
asset: Token,
max_amount: U256,
leverage: u8,
threshold: U256,
// telegram_bot: Bot,
// chat_id: i64,
}

impl AaveLooper {
impl AaveBot {
pub async fn new(
provider: Arc<SignerProvider>,
aave_address: Address,
Expand Down Expand Up @@ -157,34 +160,54 @@ impl AaveLooper {
Ok(())
}

pub async fn enter_position(&self) -> Result<(), Box<dyn Error>> {
// Approve AAVE to spend our tokens
let tx = self
.asset
.approve(*self.lending_pool.address(), self.max_amount);
pub async fn approve_tokens(&self, token: Token, amount: U256) -> Result<(), Box<dyn Error>> {
let tx = token.approve(*self.lending_pool.address(), amount);
let receipt = tx.send().await?.get_receipt().await?;
println!("Approved AAVE to spend tokens: {:?}", receipt);
Ok(())
}

// Supply assets to AAVE
let tx =
self.lending_pool
.supply_0(self.asset_address, self.max_amount, self.signer_address, 0);
pub async fn supply_tokens(
&self,
token_address: Address,
amount: U256,
) -> Result<(), Box<dyn Error>> {
let tx = self
.lending_pool
.supply_0(token_address, amount, self.signer_address, 0);
let receipt = tx.send().await?.get_receipt().await?;
println!("Supplied assets to AAVE: {:?}", receipt);
Ok(())
}

// // Calculate borrow amount based on leverage
// let borrow_amount = self.amount * U256::from(self.leverage - 1) / U256::from(self.leverage);

// // Borrow assets from AAVE
// let tx = self.aave.borrow(
// self.asset_address,
// U256::from(borrow_amount),
// U256::from(2),
// 0,
// self.signer_address,
// );
// let receipt = tx.send().await?.get_receipt().await?;
// println!("Borrowed assets from AAVE: {:?}", receipt);
pub async fn borrow_tokens(
&self,
token_address: Address,
amount: U256,
) -> Result<(), Box<dyn Error>> {
let tx = self.lending_pool.borrow_0(
token_address,
amount,
U256::from(2),
0,
self.signer_address,
);
let receipt = tx.send().await?.get_receipt().await?;
println!("Borrowed assets from AAVE: {:?}", receipt);
Ok(())
}

pub async fn enter_position(&self) -> Result<(), Box<dyn Error>> {
// Approve AAVE to spend our tokens
self.approve_tokens(self.asset.clone(), self.max_amount)
.await?;

// Supply assets to AAVE
self.supply_tokens(self.asset_address, self.max_amount)
.await?;

self.borrow_tokens(self.asset_address, self.max_amount / U256::from(2))
.await?;

Ok(())
}
Expand Down
68 changes: 65 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::aave::AaveLooper;
use crate::addressbook::{get_aave_lending_pool_address, get_token_address};
use crate::bot::AaveBot;
use crate::provider::SignerProvider;
use alloy::providers::Provider;
use alloy_chains::Chain;
Expand Down Expand Up @@ -33,6 +33,18 @@ enum Commands {
#[arg(short, long, default_value_t = 100)]
threshold: u64,
},
Supply {
#[arg(short, long)]
amount: u64,
#[arg(short, long)]
token: String,
},
Borrow {
#[arg(short, long)]
amount: u64,
#[arg(short, long)]
token: String,
},
}

pub async fn run_cli(provider: Arc<SignerProvider>) -> Result<(), Box<dyn Error>> {
Expand All @@ -55,7 +67,7 @@ pub async fn run_cli(provider: Arc<SignerProvider>) -> Result<(), Box<dyn Error>
let amount_wei = U256::from(*amount) * U256::from(10).pow(U256::from(6)); // Convert to USDC wei
let threshold = U256::from(0); // Set threshold to 0 for immediate execution

let looper = AaveLooper::new(
let looper = AaveBot::new(
provider,
aave_address,
asset_address,
Expand Down Expand Up @@ -91,7 +103,7 @@ pub async fn run_cli(provider: Arc<SignerProvider>) -> Result<(), Box<dyn Error>
.parse()
.expect("CHAT_ID should be a valid integer");

let bot = AaveLooper::new(
let bot = AaveBot::new(
provider,
aave_address,
asset_address,
Expand All @@ -106,6 +118,56 @@ pub async fn run_cli(provider: Arc<SignerProvider>) -> Result<(), Box<dyn Error>
println!("Starting Aave bot...");
bot.run().await?;
}
Commands::Supply { amount, token } => {
let aave_address = get_aave_lending_pool_address(chain).ok_or_else(|| {
Box::<dyn Error>::from("Aave lending pool address not found for this chain")
})?;
let asset_address = get_token_address(chain, token).ok_or_else(|| {
Box::<dyn Error>::from(format!("{} address not found for this chain", token))
})?;
let amount_wei = U256::from(*amount) * U256::from(10).pow(U256::from(6)); // Assuming 6 decimals, adjust if needed

let bot = AaveBot::new(
provider.clone(),
aave_address,
asset_address,
amount_wei,
1, // Leverage not used for supply
U256::from(0), // Threshold not used for supply
String::new(),
0,
)
.await?;

println!("Supplying {} {} to Aave...", amount, token);
bot.supply_tokens(asset_address, amount_wei).await?;
println!("Supply successful!");
}
Commands::Borrow { amount, token } => {
let aave_address = get_aave_lending_pool_address(chain).ok_or_else(|| {
Box::<dyn Error>::from("Aave lending pool address not found for this chain")
})?;
let asset_address = get_token_address(chain, token).ok_or_else(|| {
Box::<dyn Error>::from(format!("{} address not found for this chain", token))
})?;
let amount_wei = U256::from(*amount) * U256::from(10).pow(U256::from(6)); // Assuming 6 decimals, adjust if needed

let bot = AaveBot::new(
provider.clone(),
aave_address,
asset_address,
amount_wei,
1, // Leverage not used for borrow
U256::from(0), // Threshold not used for borrow
String::new(),
0,
)
.await?;

println!("Borrowing {} {} from Aave...", amount, token);
bot.borrow_tokens(asset_address, amount_wei).await?;
println!("Borrow successful!");
}
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use alloy_chains::{Chain, NamedChain};
use provider::get_provider;
use std::error::Error;

pub mod aave;
pub mod addressbook;
pub mod bot;
pub mod cli;
pub mod config;
pub mod provider;
Expand Down

0 comments on commit 7283cb2

Please sign in to comment.