Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pool: chore: make reserve caching system more obvious to use #180

Merged
merged 5 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions pool/src/auctions/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,10 @@ mod tests {

let (underlying_0, _) = testutils::create_token_contract(&e, &bombadil);
let (mut reserve_config_0, mut reserve_data_0) = testutils::default_reserve_meta();
reserve_data_0.b_rate = 1_100_000_000;
reserve_data_0.last_time = 12345;
reserve_data_0.backstop_credit = 100_0000000;
reserve_data_0.b_supply = 1000_0000000;
reserve_data_0.d_supply = 750_0000000;
reserve_config_0.index = 0;
testutils::create_reserve(
&e,
Expand All @@ -448,8 +450,10 @@ mod tests {

let (underlying_1, _) = testutils::create_token_contract(&e, &bombadil);
let (mut reserve_config_1, mut reserve_data_1) = testutils::default_reserve_meta();
reserve_data_1.b_rate = 1_100_000_000;
reserve_data_1.last_time = 12345;
reserve_data_1.backstop_credit = 25_0000000;
reserve_data_1.b_supply = 250_0000000;
reserve_data_1.d_supply = 187_5000000;
reserve_config_1.index = 1;
testutils::create_reserve(
&e,
Expand Down Expand Up @@ -495,13 +499,7 @@ mod tests {
};
e.as_contract(&pool_address, || {
storage::set_pool_config(&e, &pool_config);
let pool = Pool::load(&e);
let mut reserve_0 = pool.load_reserve(&e, &underlying_0);
reserve_0.backstop_credit += 100_0000000;
reserve_0.store(&e);
let mut reserve_1 = pool.load_reserve(&e, &underlying_1);
reserve_1.backstop_credit += 25_0000000;
reserve_1.store(&e);

create(&e, 2);
assert!(storage::has_auction(&e, &2, &backstop_address));
});
Expand Down
60 changes: 23 additions & 37 deletions pool/src/auctions/backstop_interest_auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn create_interest_auction_data(e: &Env, backstop: &Address) -> AuctionData
for i in 0..reserve_list.len() {
let res_asset_address = reserve_list.get_unchecked(i);
// don't store updated reserve data back to ledger. This will occur on the the auction's fill.
let reserve = pool.load_reserve(e, &res_asset_address);
let reserve = pool.load_reserve(e, &res_asset_address, false);
if reserve.backstop_credit > 0 {
let asset_to_base = pool.load_price(e, &res_asset_address);
interest_value += i128(asset_to_base)
Expand Down Expand Up @@ -78,9 +78,9 @@ pub fn fill_interest_auction(

// lot contains underlying tokens, but the backstop credit must be updated on the reserve
for (res_asset_address, lot_amount) in auction_data.lot.iter() {
let mut reserve = pool.load_reserve(e, &res_asset_address);
let mut reserve = pool.load_reserve(e, &res_asset_address, true);
reserve.backstop_credit -= lot_amount;
pool.cache_reserve(reserve, true);
pool.cache_reserve(reserve);
TokenClient::new(e, &res_asset_address).transfer(
&e.current_contract_address(),
filler,
Expand Down Expand Up @@ -178,6 +178,7 @@ mod tests {
let (mut reserve_config_0, mut reserve_data_0) = testutils::default_reserve_meta();
reserve_data_0.b_rate = 1_100_000_000;
reserve_data_0.last_time = 12345;
reserve_data_0.backstop_credit = 10_0000000;
reserve_config_0.index = 0;
testutils::create_reserve(
&e,
Expand All @@ -191,6 +192,7 @@ mod tests {
let (mut reserve_config_1, mut reserve_data_1) = testutils::default_reserve_meta();
reserve_data_1.b_rate = 1_100_000_000;
reserve_data_1.last_time = 12345;
reserve_data_1.backstop_credit = 2_5000000;
reserve_config_1.index = 1;
testutils::create_reserve(
&e,
Expand Down Expand Up @@ -236,15 +238,8 @@ mod tests {
};
e.as_contract(&pool_address, || {
storage::set_pool_config(&e, &pool_config);
let pool = Pool::load(&e);
let mut reserve_0 = pool.load_reserve(&e, &underlying_0);
reserve_0.backstop_credit += 10_0000000;
reserve_0.store(&e);
let mut reserve_1 = pool.load_reserve(&e, &underlying_1);
reserve_1.backstop_credit += 2_5000000;
reserve_1.store(&e);
let result = create_interest_auction_data(&e, &backstop_address);

let result = create_interest_auction_data(&e, &backstop_address);
assert_eq!(result.block, 51);
assert_eq!(result.bid.get_unchecked(usdc_id), 42_0000000);
assert_eq!(result.bid.len(), 1);
Expand Down Expand Up @@ -288,8 +283,10 @@ mod tests {

let (underlying_0, _) = testutils::create_token_contract(&e, &bombadil);
let (mut reserve_config_0, mut reserve_data_0) = testutils::default_reserve_meta();
reserve_data_0.b_rate = 1_100_000_000;
reserve_data_0.last_time = 12345;
reserve_data_0.backstop_credit = 100_0000000;
reserve_data_0.b_supply = 1000_0000000;
reserve_data_0.d_supply = 750_0000000;
reserve_config_0.index = 0;
testutils::create_reserve(
&e,
Expand All @@ -301,8 +298,10 @@ mod tests {

let (underlying_1, _) = testutils::create_token_contract(&e, &bombadil);
let (mut reserve_config_1, mut reserve_data_1) = testutils::default_reserve_meta();
reserve_data_1.b_rate = 1_100_000_000;
reserve_data_1.last_time = 12345;
reserve_data_1.backstop_credit = 25_0000000;
reserve_data_1.b_supply = 250_0000000;
reserve_data_1.d_supply = 187_5000000;
reserve_config_1.index = 1;
testutils::create_reserve(
&e,
Expand All @@ -314,7 +313,6 @@ mod tests {

let (underlying_2, _) = testutils::create_token_contract(&e, &bombadil);
let (mut reserve_config_2, mut reserve_data_2) = testutils::default_reserve_meta();
reserve_data_2.b_rate = 1_100_000_000;
reserve_data_2.last_time = 12345;
reserve_config_2.index = 1;
testutils::create_reserve(
Expand Down Expand Up @@ -348,15 +346,8 @@ mod tests {
};
e.as_contract(&pool_address, || {
storage::set_pool_config(&e, &pool_config);
let pool = Pool::load(&e);
let mut reserve_0 = pool.load_reserve(&e, &underlying_0);
reserve_0.backstop_credit += 100_0000000;
reserve_0.store(&e);
let mut reserve_1 = pool.load_reserve(&e, &underlying_1);
reserve_1.backstop_credit += 25_0000000;
reserve_1.store(&e);
let result = create_interest_auction_data(&e, &backstop_address);

let result = create_interest_auction_data(&e, &backstop_address);
assert_eq!(result.block, 51);
assert_eq!(result.bid.get_unchecked(usdc_id), 420_0000000);
assert_eq!(result.bid.len(), 1);
Expand Down Expand Up @@ -400,8 +391,10 @@ mod tests {

let (underlying_0, _) = testutils::create_token_contract(&e, &bombadil);
let (mut reserve_config_0, mut reserve_data_0) = testutils::default_reserve_meta();
reserve_data_0.b_rate = 1_100_000_000;
reserve_data_0.last_time = 11845;
reserve_data_0.backstop_credit = 100_0000000;
reserve_data_0.b_supply = 1000_0000000;
reserve_data_0.d_supply = 750_0000000;
reserve_config_0.index = 0;
testutils::create_reserve(
&e,
Expand All @@ -413,8 +406,10 @@ mod tests {

let (underlying_1, _) = testutils::create_token_contract(&e, &bombadil);
let (mut reserve_config_1, mut reserve_data_1) = testutils::default_reserve_meta();
reserve_data_1.b_rate = 1_100_000_000;
reserve_data_1.last_time = 11845;
reserve_data_1.backstop_credit = 25_0000000;
reserve_data_1.b_supply = 250_0000000;
reserve_data_1.d_supply = 187_5000000;
reserve_config_1.index = 1;
testutils::create_reserve(
&e,
Expand All @@ -426,7 +421,6 @@ mod tests {

let (underlying_2, _) = testutils::create_token_contract(&e, &bombadil);
let (mut reserve_config_2, mut reserve_data_2) = testutils::default_reserve_meta();
reserve_data_2.b_rate = 1_100_000_000;
reserve_data_2.last_time = 11845;
reserve_config_2.index = 2;
testutils::create_reserve(
Expand Down Expand Up @@ -461,21 +455,13 @@ mod tests {
e.as_contract(&pool_address, || {
storage::set_pool_config(&e, &pool_config);

let pool = Pool::load(&e);
let mut reserve_0 = pool.load_reserve(&e, &underlying_0);
reserve_0.backstop_credit += 100_0000000;
reserve_0.store(&e);
let mut reserve_1 = pool.load_reserve(&e, &underlying_1);
reserve_1.backstop_credit += 25_0000000;
reserve_1.store(&e);

let result = create_interest_auction_data(&e, &backstop_address);
assert_eq!(result.block, 151);
assert_eq!(result.bid.get_unchecked(usdc_id), 420_0009794);
assert_eq!(result.bid.get_unchecked(usdc_id), 420_0012936);
assert_eq!(result.bid.len(), 1);
assert_eq!(result.lot.get_unchecked(underlying_0), 100_0000066);
assert_eq!(result.lot.get_unchecked(underlying_1), 25_0000066);
assert_eq!(result.lot.get_unchecked(underlying_2), 66);
assert_eq!(result.lot.get_unchecked(underlying_0), 100_0000714);
assert_eq!(result.lot.get_unchecked(underlying_1), 25_0000178);
assert_eq!(result.lot.get_unchecked(underlying_2), 71);
assert_eq!(result.lot.len(), 3);
});
}
Expand Down
2 changes: 1 addition & 1 deletion pool/src/auctions/bad_debt_auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn create_bad_debt_auction_data(e: &Env, backstop: &Address) -> AuctionData
for (reserve_index, liability_balance) in backstop_positions.liabilities.iter() {
let res_asset_address = reserve_list.get_unchecked(reserve_index);
if liability_balance > 0 {
let reserve = pool.load_reserve(e, &res_asset_address);
let reserve = pool.load_reserve(e, &res_asset_address, false);
let asset_to_base = pool.load_price(e, &res_asset_address);
let asset_balance = reserve.to_asset_from_d_token(liability_balance);
debt_value += i128(asset_to_base)
Expand Down
1 change: 1 addition & 0 deletions pool/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub enum PoolError {
InvalidPoolStatus = 11,
InvalidUtilRate = 12,
MaxPositionsExceeded = 13,
InternalReserveNotFound = 14,
// Emission Errors (20-29)
EmissionFailure = 20,
// Oracle Errors (30-39)
Expand Down
Loading