Skip to content

Commit

Permalink
Merge pull request #23 from brozorec/transfers
Browse files Browse the repository at this point in the history
Redundant Transfers
  • Loading branch information
mootz12 authored Jan 7, 2025
2 parents a09314a + 708ef0c commit 13e3ad6
Show file tree
Hide file tree
Showing 2 changed files with 234 additions and 191 deletions.
194 changes: 21 additions & 173 deletions pool/src/pool/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ impl Actions {
/// * pool - The pool
/// * from - The sender of the requests
/// * requests - The requests to be processed
/// * use_allowance - A bool indicating if the transfer_from method should be used
///
/// ### Returns
/// A tuple of (actions, positions, check_health) where:
Expand All @@ -108,7 +107,6 @@ pub fn build_actions_from_request(
pool: &mut Pool,
from: &Address,
requests: Vec<Request>,
use_allowance: bool,
) -> (Actions, User, bool) {
let mut actions = Actions::new(e);
let mut from_state = User::load(e, from);
Expand Down Expand Up @@ -213,14 +211,10 @@ pub fn build_actions_from_request(
let d_tokens_burnt = reserve.to_d_token_down(request.amount);
if d_tokens_burnt > cur_d_tokens {
let cur_underlying_borrowed = reserve.to_asset_from_d_token(cur_d_tokens);
if use_allowance {
actions.add_for_spender_transfer(&reserve.asset, cur_underlying_borrowed);
} else {
let amount_to_refund = request.amount - cur_underlying_borrowed;
require_nonnegative(e, &amount_to_refund);
actions.add_for_spender_transfer(&reserve.asset, request.amount);
actions.add_for_pool_transfer(&reserve.asset, amount_to_refund);
}
let amount_to_refund = request.amount - cur_underlying_borrowed;
require_nonnegative(e, &amount_to_refund);
actions.add_for_spender_transfer(&reserve.asset, request.amount);
actions.add_for_pool_transfer(&reserve.asset, amount_to_refund);
from_state.remove_liabilities(e, &mut reserve, cur_d_tokens);
PoolEvents::repay(
e,
Expand Down Expand Up @@ -382,7 +376,7 @@ mod tests {
},
];
let (actions, user, health_check) =
build_actions_from_request(&e, &mut pool, &samwise, requests, false);
build_actions_from_request(&e, &mut pool, &samwise, requests);

assert_eq!(health_check, false);

Expand Down Expand Up @@ -458,7 +452,7 @@ mod tests {
},
];
let (actions, user, health_check) =
build_actions_from_request(&e, &mut pool, &samwise, requests, false);
build_actions_from_request(&e, &mut pool, &samwise, requests);

assert_eq!(health_check, false);

Expand Down Expand Up @@ -531,7 +525,7 @@ mod tests {
},
];
let (actions, user, health_check) =
build_actions_from_request(&e, &mut pool, &samwise, requests, false);
build_actions_from_request(&e, &mut pool, &samwise, requests);

assert_eq!(health_check, false);

Expand Down Expand Up @@ -596,7 +590,7 @@ mod tests {
},
];
let (actions, user, health_check) =
build_actions_from_request(&e, &mut pool, &samwise, requests, false);
build_actions_from_request(&e, &mut pool, &samwise, requests);

assert_eq!(health_check, false);

Expand Down Expand Up @@ -674,7 +668,7 @@ mod tests {
},
];
let (actions, user, health_check) =
build_actions_from_request(&e, &mut pool, &samwise, requests, false);
build_actions_from_request(&e, &mut pool, &samwise, requests);

assert_eq!(health_check, true);

Expand Down Expand Up @@ -747,7 +741,7 @@ mod tests {
},
];
let (actions, user, health_check) =
build_actions_from_request(&e, &mut pool, &samwise, requests, false);
build_actions_from_request(&e, &mut pool, &samwise, requests);

assert_eq!(health_check, true);

Expand Down Expand Up @@ -811,7 +805,7 @@ mod tests {
},
];
let (actions, user, health_check) =
build_actions_from_request(&e, &mut pool, &samwise, requests, false);
build_actions_from_request(&e, &mut pool, &samwise, requests);
assert_eq!(health_check, true);

let spender_transfer = actions.spender_transfer;
Expand Down Expand Up @@ -882,81 +876,7 @@ mod tests {
},
];
let (actions, user, health_check) =
build_actions_from_request(&e, &mut pool, &samwise, requests, false);

assert_eq!(health_check, false);

let spender_transfer = actions.spender_transfer;
let pool_transfer = actions.pool_transfer;
assert_eq!(spender_transfer.len(), 1);
assert_eq!(
spender_transfer.get_unchecked(underlying.clone()),
10_1234567
);
assert_eq!(pool_transfer.len(), 0);

let positions = user.positions.clone();
assert_eq!(positions.liabilities.len(), 1);
assert_eq!(positions.collateral.len(), 0);
assert_eq!(positions.supply.len(), 0);
let d_tokens_repaid = 10_1234451;
assert_eq!(user.get_liabilities(0), 20_0000000 - d_tokens_repaid);

let reserve = pool.load_reserve(&e, &underlying, false);
assert_eq!(reserve.d_supply, reserve_data.d_supply - d_tokens_repaid);
});
}

#[test]
fn test_build_actions_from_request_use_allowance_repay() {
let e = Env::default();
e.mock_all_auths();

let bombadil = Address::generate(&e);
let samwise = Address::generate(&e);
let pool = testutils::create_pool(&e);

let (underlying, _) = testutils::create_token_contract(&e, &bombadil);
let (reserve_config, reserve_data) = testutils::default_reserve_meta();
testutils::create_reserve(&e, &pool, &underlying, &reserve_config, &reserve_data);

e.ledger().set(LedgerInfo {
timestamp: 600,
protocol_version: 22,
sequence_number: 1234,
network_id: Default::default(),
base_reserve: 10,
min_temp_entry_ttl: 10,
min_persistent_entry_ttl: 10,
max_entry_ttl: 3110400,
});
let pool_config = PoolConfig {
oracle: Address::generate(&e),
bstop_rate: 0_2000000,
status: 0,
max_positions: 2,
};
let user_positions = Positions {
liabilities: map![&e, (0, 20_0000000)],
collateral: map![&e],
supply: map![&e],
};
e.as_contract(&pool, || {
storage::set_pool_config(&e, &pool_config);
storage::set_user_positions(&e, &samwise, &user_positions);

let mut pool = Pool::load(&e);

let requests = vec![
&e,
Request {
request_type: RequestType::Repay as u32,
address: underlying.clone(),
amount: 10_1234567,
},
];
let (actions, user, health_check) =
build_actions_from_request(&e, &mut pool, &samwise, requests, true);
build_actions_from_request(&e, &mut pool, &samwise, requests);

assert_eq!(health_check, false);

Expand Down Expand Up @@ -1030,7 +950,7 @@ mod tests {
},
];
let (actions, user, health_check) =
build_actions_from_request(&e, &mut pool, &samwise, requests.clone(), false);
build_actions_from_request(&e, &mut pool, &samwise, requests.clone());

assert_eq!(health_check, false);

Expand All @@ -1054,78 +974,6 @@ mod tests {
});
}

#[test]
fn test_build_actions_from_request_use_allowance_repay_over_balance() {
let e = Env::default();
e.mock_all_auths();

let bombadil = Address::generate(&e);
let samwise = Address::generate(&e);
let pool = testutils::create_pool(&e);

let (underlying, _) = testutils::create_token_contract(&e, &bombadil);
let (reserve_config, reserve_data) = testutils::default_reserve_meta();
testutils::create_reserve(&e, &pool, &underlying, &reserve_config, &reserve_data);

e.ledger().set(LedgerInfo {
timestamp: 600,
protocol_version: 22,
sequence_number: 1234,
network_id: Default::default(),
base_reserve: 10,
min_temp_entry_ttl: 10,
min_persistent_entry_ttl: 10,
max_entry_ttl: 3110400,
});
let pool_config = PoolConfig {
oracle: Address::generate(&e),
bstop_rate: 0_2000000,
status: 0,
max_positions: 2,
};
let user_positions = Positions {
liabilities: map![&e, (0, 20_0000000)],
collateral: map![&e],
supply: map![&e],
};

e.as_contract(&pool, || {
storage::set_pool_config(&e, &pool_config);
storage::set_user_positions(&e, &samwise, &user_positions);

let mut pool = Pool::load(&e);

let requests = vec![
&e,
Request {
request_type: RequestType::Repay as u32,
address: underlying.clone(),
amount: 21_0000000,
},
];
let (actions, user, health_check) =
build_actions_from_request(&e, &mut pool, &samwise, requests, true);

assert_eq!(health_check, false);

let spender_transfer = actions.spender_transfer;
let pool_transfer = actions.pool_transfer;
assert_eq!(spender_transfer.len(), 1);
assert_eq!(
spender_transfer.get_unchecked(underlying.clone()),
20_0000229
);
assert_eq!(pool_transfer.len(), 0);

let positions = user.positions.clone();
assert_eq!(positions.liabilities.len(), 0);
assert_eq!(positions.collateral.len(), 0);
assert_eq!(positions.supply.len(), 0);

let reserve = pool.load_reserve(&e, &underlying, false);
assert_eq!(reserve.d_supply, reserve_data.d_supply - 20_0000000);
});
}
#[test]
fn test_aggregating_actions() {
let e = Env::default();
Expand Down Expand Up @@ -1203,7 +1051,7 @@ mod tests {
},
];
let (actions, user, health_check) =
build_actions_from_request(&e, &mut pool, &samwise, requests, false);
build_actions_from_request(&e, &mut pool, &samwise, requests);

assert_eq!(health_check, true);

Expand Down Expand Up @@ -1343,7 +1191,7 @@ mod tests {
},
];
let (actions, _, health_check) =
build_actions_from_request(&e, &mut pool, &frodo, requests, false);
build_actions_from_request(&e, &mut pool, &frodo, requests);

assert_eq!(health_check, true);
let exp_new_auction = AuctionData {
Expand Down Expand Up @@ -1475,7 +1323,7 @@ mod tests {
},
];
let (actions, _, health_check) =
build_actions_from_request(&e, &mut pool, &frodo, requests, false);
build_actions_from_request(&e, &mut pool, &frodo, requests);

assert_eq!(health_check, true);
assert_eq!(
Expand Down Expand Up @@ -1622,7 +1470,7 @@ mod tests {
];
let pre_fill_backstop_token_balance = backstop_token_client.balance(&backstop_address);
let (actions, _, health_check) =
build_actions_from_request(&e, &mut pool, &samwise, requests, false);
build_actions_from_request(&e, &mut pool, &samwise, requests);

assert_eq!(backstop_token_client.balance(&samwise), 25_0000000);
assert_eq!(
Expand Down Expand Up @@ -1707,7 +1555,7 @@ mod tests {
},
];
let (actions, _, health_check) =
build_actions_from_request(&e, &mut pool, &samwise, requests, false);
build_actions_from_request(&e, &mut pool, &samwise, requests);

assert_eq!(health_check, true);
assert_eq!(
Expand Down Expand Up @@ -1776,7 +1624,7 @@ mod tests {
},
];

let (_, user, _) = build_actions_from_request(&e, &mut pool, &samwise, requests, false);
let (_, user, _) = build_actions_from_request(&e, &mut pool, &samwise, requests);
assert_eq!(user.positions.effective_count(), 3)
});
}
Expand Down Expand Up @@ -1833,7 +1681,7 @@ mod tests {
},
];

build_actions_from_request(&e, &mut pool, &samwise, requests, false);
build_actions_from_request(&e, &mut pool, &samwise, requests);
});
}

Expand Down Expand Up @@ -1872,7 +1720,7 @@ mod tests {
storage::set_pool_config(&e, &pool_config);
let mut pool = Pool::load(&e);

build_actions_from_request(&e, &mut pool, &samwise, requests, false);
build_actions_from_request(&e, &mut pool, &samwise, requests);
});
}
}
Loading

0 comments on commit 13e3ad6

Please sign in to comment.