Skip to content

Commit

Permalink
Merge pull request #108 from invariant-labs/add-claim-fee-not-deployer
Browse files Browse the repository at this point in the history
Extended claim fee tests
  • Loading branch information
Sniezka1927 authored Dec 12, 2023
2 parents 51cd994 + 226e500 commit dc22d84
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
16 changes: 16 additions & 0 deletions src/e2e/claim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod e2e_tests {
},
MIN_SQRT_PRICE,
},
InvariantError,
};
use decimal::*;
use ink_e2e::build_message;
Expand Down Expand Up @@ -55,4 +56,19 @@ pub mod e2e_tests {

Ok(())
}

#[ink_e2e::test]
async fn test_claim_not_owner(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> {
let (dex, token_x, token_y) = init_dex_and_tokens!(client, ContractRef, TokenRef);
init_basic_pool!(client, ContractRef, TokenRef, dex, token_x, token_y);
init_basic_position!(client, ContractRef, TokenRef, dex, token_x, token_y);
init_basic_swap!(client, ContractRef, TokenRef, dex, token_x, token_y);

let user = ink_e2e::bob();
let result = claim_fee!(client, ContractRef, dex, 0, user);

assert_eq!(result, Err(InvariantError::PositionNotFound));

Ok(())
}
}
50 changes: 47 additions & 3 deletions src/e2e/protocol_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ pub mod e2e_tests {
use decimal::*;
use ink_e2e::build_message;
use test_helpers::{
add_fee_tier, address_of, approve, balance_of, create_dex, create_pool, create_position,
create_tokens, get_pool, init_basic_pool, init_basic_position, init_basic_swap,
init_dex_and_tokens, mint, swap, withdraw_protocol_fee,
add_fee_tier, address_of, approve, balance_of, change_fee_receiver, create_dex,
create_pool, create_position, create_tokens, get_pool, init_basic_pool,
init_basic_position, init_basic_swap, init_dex_and_tokens, mint, swap,
withdraw_protocol_fee,
};
use token::{TokenRef, PSP22};

Expand Down Expand Up @@ -80,4 +81,47 @@ pub mod e2e_tests {
assert_eq!(result, Err(InvariantError::NotFeeReceiver));
Ok(())
}

#[ink_e2e::test]
async fn test_withdraw_fee_not_deployer(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> {
let (dex, token_x, token_y) = init_dex_and_tokens!(client, ContractRef, TokenRef);
init_basic_pool!(client, ContractRef, TokenRef, dex, token_x, token_y);
init_basic_position!(client, ContractRef, TokenRef, dex, token_x, token_y);
init_basic_swap!(client, ContractRef, TokenRef, dex, token_x, token_y);

let admin = ink_e2e::alice();
let user_address = address_of!(Bob);
let user = ink_e2e::bob();
let fee_tier = FeeTier::new(Percentage::from_scale(6, 3), 10).unwrap();
let pool_key = PoolKey::new(token_x, token_y, fee_tier).unwrap();
change_fee_receiver!(client, ContractRef, dex, pool_key, user_address, admin).unwrap();

let pool = get_pool!(client, ContractRef, dex, token_x, token_y, fee_tier).unwrap();
assert_eq!(pool.fee_receiver, user_address);

withdraw_protocol_fee!(client, ContractRef, dex, pool_key, user).unwrap();

let amount_x = balance_of!(client, TokenRef, token_x, user_address);
let amount_y = balance_of!(client, TokenRef, token_y, user_address);
assert_eq!(amount_x, 1);
assert_eq!(amount_y, 993);

let amount_x = balance_of!(client, TokenRef, token_x, dex);
let amount_y = balance_of!(client, TokenRef, token_y, dex);
assert_eq!(amount_x, 1499);
assert_eq!(amount_y, 7);

let pool_after_withdraw =
get_pool!(client, ContractRef, dex, token_x, token_y, fee_tier).unwrap();
assert_eq!(
pool_after_withdraw.fee_protocol_token_x,
TokenAmount::new(0)
);
assert_eq!(
pool_after_withdraw.fee_protocol_token_y,
TokenAmount::new(0)
);

Ok(())
}
}

0 comments on commit dc22d84

Please sign in to comment.