Skip to content

Commit

Permalink
Multihop, Pool: testing the resource usage of the contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
gangov committed Nov 13, 2023
1 parent 45a671f commit d4c128c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
24 changes: 12 additions & 12 deletions contracts/multihop/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,31 +73,31 @@ impl MultihopTrait for Multihop {
if operations.is_empty() {
panic!("Multihop: Swap: operations is empty!");
}
std::println!("{}", "verify operations");
env.budget().reset_default();
// std::println!("{}", "verify operations");
// env.budget().reset_default();
verify_swap(&operations);
env.budget().print();
// env.budget().print();

recipient.require_auth();

// first offer amount is an input from the user,
// subsequent are the results of the previous swap
let mut next_offer_amount: i128 = amount;

std::println!("{}", "make factory client");
env.budget().reset_default();
// std::println!("{}", "make factory client");
// env.budget().reset_default();
let factory_client = factory_contract::Client::new(&env, &get_factory(&env));
env.budget().print();
// env.budget().print();

operations.iter().for_each(|op| {
std::println!("{}", "factory query for lp");
env.budget().reset_default();
// std::println!("{}", "factory query for lp");
// env.budget().reset_default();
let liquidity_pool_addr: Address = factory_client
.query_for_pool_by_token_pair(&op.clone().offer_asset, &op.ask_asset.clone());
env.budget().print();
// env.budget().print();

std::println!("{}", "lp contract call for swap");
env.budget().reset_default();
// std::println!("{}", "lp contract call for swap");
// env.budget().reset_default();
let lp_client = lp_contract::Client::new(&env, &liquidity_pool_addr);
if let Some(referral) = referral.clone() {
next_offer_amount = lp_client.swap(
Expand All @@ -118,7 +118,7 @@ impl MultihopTrait for Multihop {
&max_spread_bps,
);
}
env.budget().print();
// env.budget().print();
});
}

Expand Down
12 changes: 12 additions & 0 deletions contracts/multihop/src/tests/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ fn swap_three_equal_pools_no_fees() {

let operations = vec![&env, swap1, swap2, swap3];

// env.budget().reset_default();
multihop.swap(&recipient, &None, &operations, &None, &None, &50i128);
// env.budget().print();

// 5. check if it goes according to plan
assert_eq!(token1.balance(&recipient), 0i128);
Expand Down Expand Up @@ -167,6 +169,7 @@ fn swap_three_equal_pools_no_fees_referral_fee() {
fee: 1_000,
};

// env.budget().reset_default();
multihop.swap(
&recipient,
&Some(referral),
Expand All @@ -176,6 +179,7 @@ fn swap_three_equal_pools_no_fees_referral_fee() {
&50i128,
);

// env.budget().print();
// 5. check if it goes according to plan
assert_eq!(token1.balance(&recipient), 0i128);
assert_eq!(token4.balance(&recipient), 37i128);
Expand Down Expand Up @@ -226,7 +230,9 @@ fn swap_single_pool_no_fees() {

let operations = vec![&env, swap1];

env.budget().reset_default();
multihop.swap(&recipient, &None, &operations, &None, &None, &1_000);
env.budget().print();

// 5. check if it goes according to plan
assert_eq!(token1.balance(&recipient), 4_000i128); // -1_000 token0
Expand Down Expand Up @@ -312,7 +318,9 @@ fn swap_single_pool_with_fees() {

let operations = vec![&env, swap1];

// env.budget().reset_default();
multihop.swap(&recipient, &None, &operations, &None, &None, &300i128);
// env.budget().print();

// 5. check if it goes according to plan
// 1000 tokens initially
Expand Down Expand Up @@ -393,7 +401,9 @@ fn swap_three_different_pools_no_fees() {

let operations = vec![&env, swap1, swap2, swap3];

// env.budget().reset_default();
multihop.swap(&recipient, &None, &operations, &None, &None, &5_000i128);
// env.budget().print();

// 5. check if it goes according to plan
assert_eq!(token1.balance(&recipient), 0i128);
Expand Down Expand Up @@ -473,7 +483,9 @@ fn swap_three_different_pools_with_fees() {

let operations = vec![&env, swap1, swap2, swap3];

env.budget().reset_default();
multihop.swap(&recipient, &None, &operations, &None, &None, &10_000i128);
env.budget().print();

// we start swapping 10_000 tokens

Expand Down

0 comments on commit d4c128c

Please sign in to comment.