From 7283cb21cc7b58a276a53c3e587e8b125672fe69 Mon Sep 17 00:00:00 2001 From: dvisacker Date: Thu, 26 Sep 2024 13:51:00 +0900 Subject: [PATCH] feat - add individual supply and borrow functions --- src/{aave.rs => bot.rs} | 77 ++++++++++++++++++++++++++--------------- src/cli.rs | 68 ++++++++++++++++++++++++++++++++++-- src/main.rs | 2 +- 3 files changed, 116 insertions(+), 31 deletions(-) rename src/{aave.rs => bot.rs} (79%) diff --git a/src/aave.rs b/src/bot.rs similarity index 79% rename from src/aave.rs rename to src/bot.rs index 26575ec..851cdaa 100644 --- a/src/aave.rs +++ b/src/bot.rs @@ -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}; @@ -26,12 +26,15 @@ sol! { } } -pub struct AaveLooper { +type Token = IERC20Instance>; +type LendingPool = ILendingPoolInstance>; + +pub struct AaveBot { provider: Arc, signer_address: Address, asset_address: Address, - lending_pool: ILendingPoolInstance>, - asset: IERC20Instance>, + lending_pool: LendingPool, + asset: Token, max_amount: U256, leverage: u8, threshold: U256, @@ -39,7 +42,7 @@ pub struct AaveLooper { // chat_id: i64, } -impl AaveLooper { +impl AaveBot { pub async fn new( provider: Arc, aave_address: Address, @@ -157,34 +160,54 @@ impl AaveLooper { Ok(()) } - pub async fn enter_position(&self) -> Result<(), Box> { - // 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> { + 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> { + 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> { + 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> { + // 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(()) } diff --git a/src/cli.rs b/src/cli.rs index 589924f..a50adfb 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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; @@ -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) -> Result<(), Box> { @@ -55,7 +67,7 @@ pub async fn run_cli(provider: Arc) -> Result<(), Box 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, @@ -91,7 +103,7 @@ pub async fn run_cli(provider: Arc) -> Result<(), Box .parse() .expect("CHAT_ID should be a valid integer"); - let bot = AaveLooper::new( + let bot = AaveBot::new( provider, aave_address, asset_address, @@ -106,6 +118,56 @@ pub async fn run_cli(provider: Arc) -> Result<(), Box println!("Starting Aave bot..."); bot.run().await?; } + Commands::Supply { amount, token } => { + let aave_address = get_aave_lending_pool_address(chain).ok_or_else(|| { + Box::::from("Aave lending pool address not found for this chain") + })?; + let asset_address = get_token_address(chain, token).ok_or_else(|| { + Box::::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::::from("Aave lending pool address not found for this chain") + })?; + let asset_address = get_token_address(chain, token).ok_or_else(|| { + Box::::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(()) diff --git a/src/main.rs b/src/main.rs index 4b904e0..d20c944 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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;