From f1fa0704cc23350ffbaf8bf2413b9d8976dadf3d Mon Sep 17 00:00:00 2001 From: dvisacker Date: Sun, 13 Oct 2024 23:31:27 +0900 Subject: [PATCH] fix - fix leverage function bugs --- Makefile | 6 +++++- src/bot.rs | 26 +++++++++++++++++--------- src/cli.rs | 50 ++++++++++++++++++++++++++++++++++++++++---------- 3 files changed, 62 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 9747a5e..9c01bd5 100644 --- a/Makefile +++ b/Makefile @@ -135,7 +135,11 @@ get-position-mainnet: run-bot: @echo "Running bot..." - @cd $(BINDINGS_DIR) && $(CARGO) run run-bot --amount 1 --leverage 2 --threshold 100 + $(CARGO) run run-bot --amount 1 --leverage 2 --threshold 100 + +leverage-arbitrum: + @echo "Leveraging on Arbitrum..." + $(CARGO) run leverage --amount 1 --supply-asset USDC --borrow-asset WETH --leverage 2 diff --git a/src/bot.rs b/src/bot.rs index 9d6ffa4..ac8c532 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -45,6 +45,7 @@ pub struct AaveBot { asset_address: Address, lending_pool: LendingPool, asset: Token, + looper_address: Address, looper: Looper, max_amount: U256, leverage: u8, @@ -57,6 +58,7 @@ impl AaveBot { pub async fn new( provider: Arc, aave_address: Address, + looper_address: Address, asset_address: Address, max_amount: U256, leverage: u8, @@ -69,8 +71,6 @@ impl AaveBot { let signer_address = provider.default_signer_address(); // Initialize the AaveLooper contract - // Note: You'll need to deploy this contract and get its address - let looper_address = Address::from_str("YOUR_DEPLOYED_AAVELOOPER_ADDRESS_HERE")?; let looper = AaveLooper::new(looper_address, provider.clone()); Ok(Arc::new(Self { @@ -79,6 +79,7 @@ impl AaveBot { lending_pool, asset, looper, + looper_address, asset_address, max_amount, leverage, @@ -179,10 +180,11 @@ impl AaveBot { pub async fn approve_tokens( &self, asset_address: Address, + spender_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 tx = token.approve(spender_address, amount); let receipt = tx.send().await?.get_receipt().await?; println!("Approved AAVE to spend tokens: {:?}", receipt); Ok(()) @@ -233,8 +235,12 @@ impl AaveBot { pub async fn enter_position(&self) -> Result<(), Box> { // Approve AAVE to spend our tokens - self.approve_tokens(self.asset_address, self.max_amount) - .await?; + self.approve_tokens( + self.asset_address, + *self.lending_pool.address(), + self.max_amount, + ) + .await?; // Supply assets to AAVE self.supply_tokens(self.asset_address, self.max_amount) @@ -255,16 +261,18 @@ impl AaveBot { pub async fn leverage( &self, - asset_address: Address, + supply_asset: Address, + borrow_asset: Address, amount: U256, ) -> Result<(), Box> { // First, approve the AaveLooper contract to spend tokens on our behalf - self.approve_tokens(asset_address, amount).await?; + self.approve_tokens(supply_asset, self.looper_address, amount) + .await?; // Call the leverage function on the AaveLooper contract let tx = self.looper.leverage( - asset_address, // supplyAsset - asset_address, // borrowAsset (same as supply in this case) + supply_asset, // supplyAsset + borrow_asset, // borrowAsset (same as supply in this case) amount, // principal U256::from(1), // iterations (you can adjust this as needed) U24::from(500), // feeTier (0.3% fee tier for Uniswap V3, adjust if needed) diff --git a/src/cli.rs b/src/cli.rs index 48083b2..fd93580 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -55,7 +55,11 @@ enum Commands { #[arg(short, long)] amount: u64, #[arg(short, long)] - token: String, + supply_asset: String, + #[arg(short, long)] + borrow_asset: String, + #[arg(short, long, default_value_t = 2)] + leverage: u8, }, } @@ -63,6 +67,7 @@ pub async fn run_cli(provider: Arc) -> Result<(), Box let cli = Cli::parse(); let id = provider.get_chain_id().await?; let chain = Chain::from_id(id); + let looper_address: Address = "0x5119C3d14c892D710311bE3f102619df669BD62C".parse()?; match &cli.command { Commands::EnterPosition { @@ -76,12 +81,14 @@ pub async fn run_cli(provider: Arc) -> Result<(), Box 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)); // Convert to USDC wei let threshold = U256::from(0); // Set threshold to 0 for immediate execution let looper = AaveBot::new( provider, aave_address, + looper_address, asset_address, amount_wei, *leverage, @@ -118,6 +125,7 @@ pub async fn run_cli(provider: Arc) -> Result<(), Box let bot = AaveBot::new( provider, aave_address, + looper_address, asset_address, amount_wei, *leverage, @@ -142,6 +150,7 @@ pub async fn run_cli(provider: Arc) -> Result<(), Box let bot = AaveBot::new( provider.clone(), aave_address, + looper_address, asset_address, amount_wei, 1, // Leverage not used for supply @@ -167,6 +176,7 @@ pub async fn run_cli(provider: Arc) -> Result<(), Box let bot = AaveBot::new( provider.clone(), aave_address, + looper_address, asset_address, amount_wei, 1, // Leverage not used for borrow @@ -192,6 +202,7 @@ pub async fn run_cli(provider: Arc) -> Result<(), Box let bot = AaveBot::new( provider.clone(), aave_address, + looper_address, asset_address, amount_wei, 1, // Leverage not used for repay @@ -201,27 +212,45 @@ pub async fn run_cli(provider: Arc) -> Result<(), Box ) .await?; - bot.approve_tokens(asset_address, amount_wei).await?; + bot.approve_tokens(asset_address, aave_address, amount_wei) + .await?; println!("Repaying {} {} to Aave...", amount, token); bot.repay_tokens(asset_address, amount_wei).await?; println!("Repay successful!"); } - Commands::Leverage { amount, token } => { + Commands::Leverage { + amount, + supply_asset, + borrow_asset, + leverage, + } => { 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 supply_asset_address = + get_token_address(chain, &supply_asset).ok_or_else(|| { + Box::::from(format!( + "{} address not found for this chain", + supply_asset + )) + })?; + let borrow_asset_address = + get_token_address(chain, &borrow_asset).ok_or_else(|| { + Box::::from(format!( + "{} address not found for this chain", + borrow_asset + )) + })?; 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, + looper_address, + supply_asset_address, amount_wei, - 1, // Leverage not used for this operation + *leverage, U256::from(0), // Threshold not used for this operation String::new(), 0, @@ -230,9 +259,10 @@ pub async fn run_cli(provider: Arc) -> Result<(), Box println!( "Increasing leverage by borrowing {} {} and supplying it back...", - amount, token + amount, borrow_asset ); - bot.leverage(asset_address, amount_wei).await?; + bot.leverage(supply_asset_address, borrow_asset_address, amount_wei) + .await?; println!("Leverage increased successfully!"); } }