Skip to content

Commit

Permalink
feat - add repay function
Browse files Browse the repository at this point in the history
  • Loading branch information
Dvisacker committed Sep 27, 2024
1 parent 7283cb2 commit fa68e0d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,12 @@ impl AaveBot {
Ok(())
}

pub async fn approve_tokens(&self, token: Token, amount: U256) -> Result<(), Box<dyn Error>> {
pub async fn approve_tokens(
&self,
asset_address: Address,
amount: U256,
) -> Result<(), Box<dyn Error>> {
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);
Expand Down Expand Up @@ -197,9 +202,22 @@ impl AaveBot {
Ok(())
}

pub async fn repay_tokens(
&self,
token_address: Address,
amount: U256,
) -> Result<(), Box<dyn Error>> {
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<dyn Error>> {
// 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
Expand Down
33 changes: 33 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SignerProvider>) -> Result<(), Box<dyn Error>> {
Expand Down Expand Up @@ -168,6 +174,33 @@ pub async fn run_cli(provider: Arc<SignerProvider>) -> Result<(), Box<dyn Error>
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::<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 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(())
Expand Down

0 comments on commit fa68e0d

Please sign in to comment.