Skip to content

Commit

Permalink
[Tests]: Add a test for oracle price cutoffs in closing position.
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOmarA committed Feb 14, 2024
1 parent 93ce873 commit 05a1066
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 10 deletions.
8 changes: 0 additions & 8 deletions packages/caviarnine-v1-adapter-v1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,14 +604,6 @@ fn calculate_bin_amounts_due_to_price_action(
bin_amount.resource_x == Decimal::ZERO,
bin_amount.resource_y == Decimal::ZERO,
) {
// TODO: I think that this is impossible? I believe that
// it is impossible since we only capture the bins that
// we have contributed to and THEN store the reserves in
// the bin. If we contributed to it, how come its empty
// of both resources?
//
// If this is somehow possible, what do we want to do
// in this case?
(true, true) => return None,
(true, false) => Composition::EntirelyY,
(false, true) => Composition::EntirelyX,
Expand Down
1 change: 0 additions & 1 deletion packages/ociswap-v1-adapter-v1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ pub mod adapter {
) -> OpenLiquidityPositionOutput {
let mut pool = Self::pool(pool_address);

// TODO: Is this actually pool units and change?
let (pool_units, change) = pool.add_liquidity(buckets.0, buckets.1);

let user_share = pool_units
Expand Down
72 changes: 71 additions & 1 deletion tests/tests/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ pub fn can_open_liquidity_position_when_oracle_price_is_lower_than_pool_but_with
Ok(())
}

// TODO: Similar test is required for closing of a position.
#[test]
#[allow(unused_must_use)]
fn oracle_price_cutoffs_for_opening_liquidity_positions_are_implemented_correctly(
Expand Down Expand Up @@ -403,6 +402,77 @@ fn test_open_position_oracle_price_cutoffs(
)
}

#[test]
#[allow(unused_must_use)]
fn oracle_price_cutoffs_for_closing_liquidity_positions_are_implemented_correctly(
) {
const SMALL_DECIMAL: Decimal = dec!(0.000000000000000010);

test_close_position_oracle_price_cutoffs(dec!(1) / dec!(1.01), dec!(0.01))
.expect("Should succeed!");
test_close_position_oracle_price_cutoffs(dec!(1) / dec!(0.99), dec!(0.01))
.expect("Should succeed!");
test_close_position_oracle_price_cutoffs(
dec!(1) / dec!(0.99) - SMALL_DECIMAL,
dec!(0.01),
)
.expect("Should succeed!");
test_close_position_oracle_price_cutoffs(
dec!(1) / dec!(1.01) + SMALL_DECIMAL,
dec!(0.01),
)
.expect("Should succeed!");

assert_is_ignition_relative_price_difference_larger_than_allowed_error(
&test_close_position_oracle_price_cutoffs(
dec!(1) / dec!(0.99) + SMALL_DECIMAL,
dec!(0.01),
),
);
assert_is_ignition_relative_price_difference_larger_than_allowed_error(
&test_close_position_oracle_price_cutoffs(
dec!(1) / dec!(1.01) - SMALL_DECIMAL,
dec!(0.01),
),
);
}

fn test_close_position_oracle_price_cutoffs(
oracle_price: Decimal,
allowed_price_difference: Decimal,
) -> Result<Vec<Bucket>, RuntimeError> {
let Environment {
environment: ref mut env,
mut protocol,
ociswap_v1,
resources,
..
} = ScryptoTestEnv::new_with_configuration(Configuration {
maximum_allowed_relative_price_difference: allowed_price_difference,
..Default::default()
})?;

let bitcoin_bucket =
ResourceManager(resources.bitcoin).mint_fungible(dec!(100), env)?;

// Act
let (receipt, ..) = protocol.ignition.open_liquidity_position(
FungibleBucket(bitcoin_bucket),
ociswap_v1.pools.bitcoin.try_into().unwrap(),
LockupPeriod::from_months(6).unwrap(),
env,
)?;

let current_time = env.get_current_time();
env.set_current_time(current_time.add_days(12 * 30).unwrap());

protocol
.oracle
.set_price(resources.bitcoin, XRD, oracle_price, env)?;

protocol.ignition.close_liquidity_position(receipt, env)
}

#[test]
fn cant_open_a_liquidity_position_with_an_invalid_lockup_period(
) -> Result<(), RuntimeError> {
Expand Down

0 comments on commit 05a1066

Please sign in to comment.