diff --git a/src/bot.rs b/src/bot.rs index 851cdaa..e0cda6f 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -160,7 +160,12 @@ impl AaveBot { Ok(()) } - pub async fn approve_tokens(&self, token: Token, amount: U256) -> Result<(), Box> { + pub async fn approve_tokens( + &self, + asset_address: Address, + amount: U256, + ) -> Result<(), Box> { + let token = IERC20::new(asset_address, self.provider.clone()); let tx = token.approve(*self.lending_pool.address(), amount); let receipt = tx.send().await?.get_receipt().await?; println!("Approved AAVE to spend tokens: {:?}", receipt); @@ -197,9 +202,22 @@ impl AaveBot { Ok(()) } + pub async fn repay_tokens( + &self, + token_address: Address, + amount: U256, + ) -> Result<(), Box> { + let tx = + self.lending_pool + .repay_1(token_address, amount, U256::from(2), self.signer_address); + let receipt = tx.send().await?.get_receipt().await?; + println!("Repaid assets to 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) + self.approve_tokens(self.asset_address, self.max_amount) .await?; // Supply assets to AAVE diff --git a/src/cli.rs b/src/cli.rs index a50adfb..85c4ecc 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -45,6 +45,12 @@ enum Commands { #[arg(short, long)] token: String, }, + Repay { + #[arg(short, long)] + amount: u64, + #[arg(short, long)] + token: String, + }, } pub async fn run_cli(provider: Arc) -> Result<(), Box> { @@ -168,6 +174,33 @@ pub async fn run_cli(provider: Arc) -> Result<(), Box bot.borrow_tokens(asset_address, amount_wei).await?; println!("Borrow successful!"); } + Commands::Repay { 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 repay + U256::from(0), // Threshold not used for repay + String::new(), + 0, + ) + .await?; + + bot.approve_tokens(asset_address, amount_wei).await?; + + println!("Repaying {} {} to Aave...", amount, token); + bot.repay_tokens(asset_address, amount_wei).await?; + println!("Repay successful!"); + } } Ok(())