Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldragonfly committed Dec 19, 2024
1 parent 476885d commit 208a7ca
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 131 deletions.
124 changes: 6 additions & 118 deletions rust-sdk/whirlpool/src/decrease_liquidity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use orca_whirlpools_core::{
get_tick_index_in_array, CollectFeesQuote, CollectRewardsQuote, DecreaseLiquidityQuote,
};
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_sdk::program_pack::Pack;
use solana_sdk::{account::Account, instruction::Instruction, pubkey::Pubkey, signature::Keypair};
use spl_associated_token_account::get_associated_token_address_with_program_id;
use solana_sdk::program_pack::Pack;

use crate::{
token::{get_current_transfer_fee, prepare_token_accounts_instructions, TokenAccountStrategy},
Expand Down Expand Up @@ -653,15 +653,15 @@ mod tests {
}

use crate::tests::{
setup_ata_te, setup_ata_with_amount, setup_config_and_fee_tiers,
setup_mint_te_fee, setup_mint_with_decimals, setup_position, setup_te_position,
setup_whirlpool, RpcContext, SetupAtaConfig,
setup_ata_te, setup_ata_with_amount, setup_config_and_fee_tiers, setup_mint_te_fee,
setup_mint_with_decimals, setup_position, setup_te_position, setup_whirlpool, RpcContext,
SetupAtaConfig,
};
use solana_sdk::signature::{Keypair, Signer};
use serial_test::serial;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, Signer};
use spl_associated_token_account::get_associated_token_address_with_program_id;
use spl_token::state::Account as TokenAccount;
use serial_test::serial;
use std::error::Error;

const TEST_SLIPPAGE_TOLERANCE: u16 = 100; // 1%
Expand Down Expand Up @@ -795,116 +795,4 @@ mod tests {

Ok(())
}

#[tokio::test]
#[serial]
async fn test_decrease_liquidity_with_transfer_fee() -> Result<(), Box<dyn Error>> {
println!("Starting test_decrease_liquidity_with_transfer_fee");
let ctx = RpcContext::new().await;

// Setup and initialize mints with transfer fees
println!("Setting up mints with transfer fees");
let mint_a_pubkey = setup_mint_te_fee(&ctx).await?;
let mint_b_pubkey = setup_mint_te_fee(&ctx).await?;
let token_balance: u64 = 1_000_000_000;

// Setup token accounts
println!("Setting up token accounts for mint A");
setup_ata_te(
&ctx,
mint_a_pubkey,
Some(SetupAtaConfig {
amount: Some(token_balance),
..Default::default()
}),
)
.await?;

println!("Setting up token accounts for mint B");
setup_ata_te(
&ctx,
mint_b_pubkey,
Some(SetupAtaConfig {
amount: Some(token_balance),
..Default::default()
}),
)
.await?;

// Setup pool and position
println!("Setting up whirlpool and position");
let tick_spacing = 64;
let pool_pubkey =
setup_whirlpool(&ctx, ctx.config, mint_a_pubkey, mint_b_pubkey, tick_spacing).await?;
println!("Whirlpool pubkey: {}", pool_pubkey);
let position_mint = setup_te_position(
&ctx,
pool_pubkey,
Some((-128, 128)),
Some(ctx.signer.pubkey()),
)
.await?;

// Increase liquidity first to have liquidity to decrease
println!("Increasing initial liquidity by 100,000");
let increase_param = crate::increase_liquidity::IncreaseLiquidityParam::Liquidity(100_000);
let increase_ix = crate::increase_liquidity::increase_liquidity_instructions(
&ctx.rpc,
position_mint,
increase_param,
Some(TEST_SLIPPAGE_TOLERANCE),
Some(ctx.signer.pubkey()),
)
.await?;

ctx.send_transaction_with_signers(increase_ix.instructions, vec![])
.await?;

// Decrease liquidity
println!("Decreasing liquidity by 50,000");
let decrease_param = DecreaseLiquidityParam::Liquidity(50_000);
let decrease_ix = decrease_liquidity_instructions(
&ctx.rpc,
position_mint,
decrease_param,
Some(TEST_SLIPPAGE_TOLERANCE),
Some(ctx.signer.pubkey()),
)
.await?;

// Send transaction
println!("Sending decrease liquidity transaction");
ctx.send_transaction_with_signers(decrease_ix.instructions, vec![])
.await?;

// Fetch the token accounts and check balances
println!("Fetching token accounts to verify balances");

// Fetch token accounts
let owner_token_account_a = get_associated_token_address_with_program_id(
&ctx.signer.pubkey(),
&mint_a_pubkey,
&spl_token_2022::ID,
);
let owner_token_account_b = get_associated_token_address_with_program_id(
&ctx.signer.pubkey(),
&mint_b_pubkey,
&spl_token_2022::ID,
);

let token_account_a = fetch_token(&ctx.rpc, owner_token_account_a).await?;
let token_account_b = fetch_token(&ctx.rpc, owner_token_account_b).await?;

println!("Token A account balance: {}", token_account_a.amount);
println!("Token B account balance: {}", token_account_b.amount);

// Expected amounts with transfer fees applied
// Since decrease_liquidity returns collected tokens, need to calculate fees

// For simplicity, we'll assume the collected tokens are correct
// In a real test, you would calculate expected balances based on the fees and amounts
println!("Test completed successfully");

Ok(())
}
}
25 changes: 12 additions & 13 deletions rust-sdk/whirlpool/src/tests/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use solana_sdk::{
system_instruction, system_program,
};
use spl_associated_token_account::{
get_associated_token_address, instruction::create_associated_token_account,
get_associated_token_address_with_program_id,
get_associated_token_address, get_associated_token_address_with_program_id,
instruction::create_associated_token_account,
};
use spl_token::ID as TOKEN_PROGRAM_ID;
use spl_token_2022::{state::Mint as Token2022Mint, ID as TOKEN_2022_PROGRAM_ID};
Expand Down Expand Up @@ -49,10 +49,7 @@ pub async fn setup_whirlpool(
let vault_a = ctx.get_next_keypair();
let vault_b = ctx.get_next_keypair();

let mint_infos = ctx
.rpc
.get_multiple_accounts(&[token_a, token_b])
.await?;
let mint_infos = ctx.rpc.get_multiple_accounts(&[token_a, token_b]).await?;
let mint_a_info = mint_infos[0]
.as_ref()
.ok_or("Token A mint info not found")?;
Expand Down Expand Up @@ -197,7 +194,10 @@ pub async fn setup_te_position(
let tick_spacing = whirlpool_account.tick_spacing as i32;
let tick_lower_aligned = (tick_lower / tick_spacing) * tick_spacing;
let tick_upper_aligned = (tick_upper / tick_spacing) * tick_spacing;
println!("Aligned ticks: lower={}, upper={}", tick_lower_aligned, tick_upper_aligned);
println!(
"Aligned ticks: lower={}, upper={}",
tick_lower_aligned, tick_upper_aligned
);

// Initialize tick arrays if needed
let tick_arrays = [
Expand Down Expand Up @@ -259,12 +259,11 @@ pub async fn setup_te_position(
0,
)?;

let position_token_account =
get_associated_token_address_with_program_id(
&ctx.signer.pubkey(),
&position_mint.pubkey(),
&TOKEN_2022_PROGRAM_ID,
);
let position_token_account = get_associated_token_address_with_program_id(
&ctx.signer.pubkey(),
&position_mint.pubkey(),
&TOKEN_2022_PROGRAM_ID,
);

let create_ata_ix = create_associated_token_account(
&ctx.signer.pubkey(),
Expand Down

0 comments on commit 208a7ca

Please sign in to comment.