Skip to content

Commit

Permalink
Merge pull request #40 from blend-capital/validate-extreme-supply-size
Browse files Browse the repository at this point in the history
Validate extreme supply size
  • Loading branch information
mootz12 authored Jan 29, 2025
2 parents ed1a137 + 6add0be commit d1bd10d
Show file tree
Hide file tree
Showing 26 changed files with 854 additions and 660 deletions.
43 changes: 17 additions & 26 deletions pool/src/auctions/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ use crate::{
storage,
};
use cast::i128;
use soroban_fixed_point_math::FixedPoint;
use soroban_sdk::{
contracttype, map, panic_with_error, unwrap::UnwrapOptimized, Address, Env, Map, Vec,
};
use soroban_fixed_point_math::SorobanFixedPoint;
use soroban_sdk::{contracttype, map, panic_with_error, Address, Env, Map, Vec};

use super::{
backstop_interest_auction::{create_interest_auction_data, fill_interest_auction},
Expand Down Expand Up @@ -213,35 +211,27 @@ fn scale_auction(
for (asset, amount) in auction_data.bid.iter() {
// apply percent scalar and store remainder to base auction
// round up to avoid rounding exploits
let to_fill_base = amount
.fixed_mul_ceil(percent_filled_i128, SCALAR_7)
.unwrap_optimized();
let to_fill_base = amount.fixed_mul_ceil(e, &percent_filled_i128, &SCALAR_7);
let remaining_base = amount - to_fill_base;
if remaining_base > 0 {
remaining_auction.bid.set(asset.clone(), remaining_base);
}
// apply block scalar to to_fill auction and don't store if 0
let to_fill_scaled = to_fill_base
.fixed_mul_ceil(bid_modifier, SCALAR_7)
.unwrap_optimized();
let to_fill_scaled = to_fill_base.fixed_mul_ceil(e, &bid_modifier, &SCALAR_7);
if to_fill_scaled > 0 {
to_fill_auction.bid.set(asset, to_fill_scaled);
}
}
for (asset, amount) in auction_data.lot.iter() {
// apply percent scalar and store remainder to base auction
// round down to avoid rounding exploits
let to_fill_base = amount
.fixed_mul_floor(percent_filled_i128, SCALAR_7)
.unwrap_optimized();
let to_fill_base = amount.fixed_mul_floor(e, &percent_filled_i128, &SCALAR_7);
let remaining_base = amount - to_fill_base;
if remaining_base > 0 {
remaining_auction.lot.set(asset.clone(), remaining_base);
}
// apply block scalar to to_fill auction and don't store if 0
let to_fill_scaled = to_fill_base
.fixed_mul_floor(lot_modifier, SCALAR_7)
.unwrap_optimized();
let to_fill_scaled = to_fill_base.fixed_mul_floor(e, &lot_modifier, &SCALAR_7);
if to_fill_scaled > 0 {
to_fill_auction.lot.set(asset, to_fill_scaled);
}
Expand All @@ -267,6 +257,7 @@ mod tests {
use soroban_sdk::{
map,
testutils::{Address as _, Ledger, LedgerInfo},
unwrap::UnwrapOptimized,
vec, Symbol,
};

Expand Down Expand Up @@ -314,7 +305,7 @@ 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.d_rate = 1_100_000_000;
reserve_data_0.d_rate = 1_100_000_000_000;
reserve_data_0.last_time = 12345;
reserve_config_0.index = 0;
testutils::create_reserve(
Expand All @@ -327,7 +318,7 @@ 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.d_rate = 1_200_000_000;
reserve_data_1.d_rate = 1_200_000_000_000;
reserve_data_1.last_time = 12345;
reserve_config_1.index = 1;
testutils::create_reserve(
Expand All @@ -340,7 +331,7 @@ 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.b_rate = 1_100_000_000_000;
reserve_data_2.last_time = 12345;
reserve_config_2.index = 1;
testutils::create_reserve(
Expand Down Expand Up @@ -467,7 +458,7 @@ 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.b_rate = 1_100_000_000_000;
reserve_data_2.last_time = 12345;
reserve_config_2.index = 1;
testutils::create_reserve(
Expand Down Expand Up @@ -541,7 +532,7 @@ 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.last_time = 12345;
reserve_data_0.b_rate = 1_100_000_000;
reserve_data_0.b_rate = 1_100_000_000_000;
reserve_config_0.c_factor = 0_8500000;
reserve_config_0.l_factor = 0_9000000;
reserve_config_0.index = 0;
Expand All @@ -555,7 +546,7 @@ 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_200_000_000;
reserve_data_1.b_rate = 1_200_000_000_000;
reserve_config_1.c_factor = 0_7500000;
reserve_config_1.l_factor = 0_7500000;
reserve_data_1.last_time = 12345;
Expand Down Expand Up @@ -654,7 +645,7 @@ 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.last_time = 12345;
reserve_data_0.b_rate = 1_100_000_000;
reserve_data_0.b_rate = 1_100_000_000_000;
reserve_config_0.c_factor = 0_8500000;
reserve_config_0.l_factor = 0_9000000;
reserve_config_0.index = 0;
Expand All @@ -668,7 +659,7 @@ 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_200_000_000;
reserve_data_1.b_rate = 1_200_000_000_000;
reserve_config_1.c_factor = 0_7500000;
reserve_config_1.l_factor = 0_7500000;
reserve_data_1.last_time = 12345;
Expand Down Expand Up @@ -766,7 +757,7 @@ 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.last_time = 12345;
reserve_data_0.b_rate = 1_100_000_000;
reserve_data_0.b_rate = 1_100_000_000_000;
reserve_config_0.c_factor = 0_8500000;
reserve_config_0.l_factor = 0_9000000;
reserve_config_0.index = 0;
Expand All @@ -780,7 +771,7 @@ 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_200_000_000;
reserve_data_1.b_rate = 1_200_000_000_000;
reserve_config_1.c_factor = 0_7500000;
reserve_config_1.l_factor = 0_7500000;
reserve_data_1.last_time = 12345;
Expand Down
49 changes: 24 additions & 25 deletions pool/src/auctions/backstop_interest_auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::{
};
use cast::i128;
use sep_41_token::TokenClient;
use soroban_fixed_point_math::FixedPoint;
use soroban_sdk::{map, panic_with_error, unwrap::UnwrapOptimized, Address, Env, Vec};
use soroban_fixed_point_math::SorobanFixedPoint;
use soroban_sdk::{map, panic_with_error, Address, Env, Vec};

use super::{AuctionData, AuctionType};

Expand Down Expand Up @@ -45,9 +45,11 @@ pub fn create_interest_auction_data(
let reserve = pool.load_reserve(e, &lot_asset, false);
if reserve.data.backstop_credit > 0 {
let asset_to_base = pool.load_price(e, &reserve.asset);
interest_value += i128(asset_to_base)
.fixed_mul_floor(reserve.data.backstop_credit, reserve.scalar)
.unwrap_optimized();
interest_value += i128(asset_to_base).fixed_mul_floor(
e,
&reserve.data.backstop_credit,
&reserve.scalar,
);
auction_data
.lot
.set(reserve.asset, reserve.data.backstop_credit);
Expand All @@ -71,18 +73,15 @@ pub fn create_interest_auction_data(
}

let pool_backstop_data = backstop_client.pool_data(&e.current_contract_address());
let backstop_token_value_base = (pool_backstop_data
.usdc
.fixed_mul_floor(oracle_scalar, SCALAR_7)
.unwrap_optimized()
* 5)
.fixed_div_floor(pool_backstop_data.tokens, SCALAR_7)
.unwrap_optimized();
let backstop_token_value_base =
(pool_backstop_data
.usdc
.fixed_mul_floor(e, &oracle_scalar, &SCALAR_7)
* 5)
.fixed_div_floor(e, &pool_backstop_data.tokens, &SCALAR_7);
let bid_amount = interest_value
.fixed_mul_floor(1_4000000, SCALAR_7)
.unwrap_optimized()
.fixed_div_floor(backstop_token_value_base, SCALAR_7)
.unwrap_optimized();
.fixed_mul_floor(e, &1_4000000, &SCALAR_7)
.fixed_div_floor(e, &backstop_token_value_base, &SCALAR_7);
auction_data.bid.set(backstop_token, bid_amount);

auction_data
Expand Down Expand Up @@ -329,7 +328,7 @@ 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.b_rate = 1_100_000_000_000;
reserve_data_0.last_time = 12345;
reserve_data_0.backstop_credit = 10_0000000;
reserve_config_0.index = 0;
Expand All @@ -343,7 +342,7 @@ 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.b_rate = 1_100_000_000_000;
reserve_data_1.last_time = 12345;
reserve_data_1.backstop_credit = 2_5000000;
reserve_config_1.index = 1;
Expand All @@ -357,7 +356,7 @@ 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.b_rate = 1_100_000_000_000;
reserve_data_2.last_time = 12345;
reserve_config_2.index = 1;
testutils::create_reserve(
Expand Down Expand Up @@ -1094,9 +1093,9 @@ mod tests {
100,
);
assert_eq!(result.block, 151);
assert_eq!(result.bid.get_unchecked(backstop_token_id), 336_0010348);
assert_eq!(result.bid.get_unchecked(backstop_token_id), 336_0010346);
assert_eq!(result.bid.len(), 1);
assert_eq!(result.lot.get_unchecked(underlying_0), 100_0000714);
assert_eq!(result.lot.get_unchecked(underlying_0), 100_0000713);
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 Expand Up @@ -1147,7 +1146,7 @@ mod tests {

let (underlying_0, underlying_0_client) = 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.b_rate = 1_100_000_000_000;
reserve_data_0.b_supply = 200_000_0000000;
reserve_data_0.d_supply = 100_000_0000000;
reserve_data_0.last_time = 12345;
Expand All @@ -1164,7 +1163,7 @@ mod tests {

let (underlying_1, underlying_1_client) = 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.b_rate = 1_100_000_000_000;
reserve_data_0.b_supply = 10_000_0000000;
reserve_data_0.b_supply = 7_000_0000000;
reserve_data_1.last_time = 12345;
Expand Down Expand Up @@ -1275,7 +1274,7 @@ mod tests {

let (underlying_0, underlying_0_client) = 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.b_rate = 1_100_000_000_000;
reserve_data_0.b_supply = 200_000_0000000;
reserve_data_0.d_supply = 100_000_0000000;
reserve_data_0.last_time = 12345;
Expand All @@ -1292,7 +1291,7 @@ mod tests {

let (underlying_1, underlying_1_client) = 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.b_rate = 1_100_000_000_000;
reserve_data_0.b_supply = 10_000_0000000;
reserve_data_0.b_supply = 7_000_0000000;
reserve_data_1.last_time = 12345;
Expand Down
Loading

0 comments on commit d1bd10d

Please sign in to comment.