Skip to content

Commit

Permalink
fix - fix leverage function bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Dvisacker committed Oct 13, 2024
1 parent dd6562e commit f1fa070
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 20 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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



Expand Down
26 changes: 17 additions & 9 deletions src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -57,6 +58,7 @@ impl AaveBot {
pub async fn new(
provider: Arc<SignerProvider>,
aave_address: Address,
looper_address: Address,
asset_address: Address,
max_amount: U256,
leverage: u8,
Expand All @@ -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 {
Expand All @@ -79,6 +79,7 @@ impl AaveBot {
lending_pool,
asset,
looper,
looper_address,
asset_address,
max_amount,
leverage,
Expand Down Expand Up @@ -179,10 +180,11 @@ impl AaveBot {
pub async fn approve_tokens(
&self,
asset_address: Address,
spender_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 tx = token.approve(spender_address, amount);
let receipt = tx.send().await?.get_receipt().await?;
println!("Approved AAVE to spend tokens: {:?}", receipt);
Ok(())
Expand Down Expand Up @@ -233,8 +235,12 @@ impl AaveBot {

pub async fn enter_position(&self) -> Result<(), Box<dyn Error>> {
// 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)
Expand All @@ -255,16 +261,18 @@ impl AaveBot {

pub async fn leverage(
&self,
asset_address: Address,
supply_asset: Address,
borrow_asset: Address,
amount: U256,
) -> Result<(), Box<dyn Error>> {
// 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)
Expand Down
50 changes: 40 additions & 10 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,19 @@ 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,
},
}

pub async fn run_cli(provider: Arc<SignerProvider>) -> Result<(), Box<dyn Error>> {
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 {
Expand All @@ -76,12 +81,14 @@ pub async fn run_cli(provider: Arc<SignerProvider>) -> Result<(), Box<dyn Error>
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)); // 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,
Expand Down Expand Up @@ -118,6 +125,7 @@ pub async fn run_cli(provider: Arc<SignerProvider>) -> Result<(), Box<dyn Error>
let bot = AaveBot::new(
provider,
aave_address,
looper_address,
asset_address,
amount_wei,
*leverage,
Expand All @@ -142,6 +150,7 @@ pub async fn run_cli(provider: Arc<SignerProvider>) -> Result<(), Box<dyn Error>
let bot = AaveBot::new(
provider.clone(),
aave_address,
looper_address,
asset_address,
amount_wei,
1, // Leverage not used for supply
Expand All @@ -167,6 +176,7 @@ pub async fn run_cli(provider: Arc<SignerProvider>) -> Result<(), Box<dyn Error>
let bot = AaveBot::new(
provider.clone(),
aave_address,
looper_address,
asset_address,
amount_wei,
1, // Leverage not used for borrow
Expand All @@ -192,6 +202,7 @@ pub async fn run_cli(provider: Arc<SignerProvider>) -> Result<(), Box<dyn Error>
let bot = AaveBot::new(
provider.clone(),
aave_address,
looper_address,
asset_address,
amount_wei,
1, // Leverage not used for repay
Expand All @@ -201,27 +212,45 @@ pub async fn run_cli(provider: Arc<SignerProvider>) -> Result<(), Box<dyn Error>
)
.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::<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 supply_asset_address =
get_token_address(chain, &supply_asset).ok_or_else(|| {
Box::<dyn Error>::from(format!(
"{} address not found for this chain",
supply_asset
))
})?;
let borrow_asset_address =
get_token_address(chain, &borrow_asset).ok_or_else(|| {
Box::<dyn Error>::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,
Expand All @@ -230,9 +259,10 @@ pub async fn run_cli(provider: Arc<SignerProvider>) -> Result<(), Box<dyn Error>

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!");
}
}
Expand Down

0 comments on commit f1fa070

Please sign in to comment.