Skip to content

Commit

Permalink
[Caviarnine Adapter V1]: Persist the bins contributed to in the recei…
Browse files Browse the repository at this point in the history
…pt NFT.
  • Loading branch information
0xOmarA committed Jan 25, 2024
1 parent 02dc890 commit 6640473
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 34 deletions.
49 changes: 43 additions & 6 deletions packages/caviarnine-adapter-v1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ define_error! {
=> "Pool has no price.";
}

/// The total number of bins that we will be using on the left and the right
/// excluding the one in the middle.
pub const PREFERRED_TOTAL_NUMBER_OF_HIGHER_AND_LOWER_BINS: u32 = 40 * 2;

#[blueprint_with_traits]
pub mod adapter {
struct CaviarNineAdapter;
Expand Down Expand Up @@ -96,7 +100,11 @@ pub mod adapter {
higher_bins,
lower_bins,
..
} = SelectedBins::select(active_bin, bin_span, 62 * 2);
} = SelectedBins::select(
active_bin,
bin_span,
PREFERRED_TOTAL_NUMBER_OF_HIGHER_AND_LOWER_BINS,
);

// Determine the amount of resources that we will add to each of the
// bins. We have 62 on the left and 62 on the right. But, we also
Expand Down Expand Up @@ -146,10 +154,15 @@ pub mod adapter {
.map(|bin_id| (*bin_id, dec!(0), position_amount_y)),
);
positions.extend(
lower_bins
higher_bins
.iter()
.map(|bin_id| (*bin_id, position_amount_x, dec!(0))),
);
let adapter_specific_information =
CaviarnineAdapterSpecificInformation::from_positions(
&positions,
)
.into();

let (receipt, change_x, change_y) =
pool.add_liquidity(bucket_x, bucket_y, positions);
Expand All @@ -161,8 +174,7 @@ pub mod adapter {
change_y.resource_address() => change_y,
},
others: vec![],
adapter_specific_information:
CaviarnineAdapterSpecificInformation {}.into(),
adapter_specific_information,
}
}

Expand Down Expand Up @@ -224,8 +236,33 @@ pub mod adapter {
}
}

#[derive(ScryptoSbor, Debug, Clone)]
pub struct CaviarnineAdapterSpecificInformation {/* TODO: Determine what is needed here */}
#[derive(ScryptoSbor, Debug, Clone, Default)]
pub struct CaviarnineAdapterSpecificInformation {
/// The amount of liquidity that was provided to each bin. The key of the
/// [`IndexMap`] is the bin number and the value is a tuple of the amount of
/// liquidity provided. The first element of the tuple is the x liquidity
/// and the second one is the y liquidity.
pub liquidity_provided_when_position_opened:
IndexMap<u32, (Decimal, Decimal)>,
}

impl CaviarnineAdapterSpecificInformation {
pub fn from_positions(positions: &[(u32, Decimal, Decimal)]) -> Self {
let mut this = CaviarnineAdapterSpecificInformation::default();

for (bin_number, x_liquidity, y_liquidity) in positions {
let entry = this
.liquidity_provided_when_position_opened
.entry(*bin_number)
.or_default();

entry.0 += *x_liquidity;
entry.1 += *y_liquidity;
}

this
}
}

impl From<CaviarnineAdapterSpecificInformation> for AnyValue {
fn from(value: CaviarnineAdapterSpecificInformation) -> Self {
Expand Down
83 changes: 55 additions & 28 deletions tests/tests/caviarnine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,10 @@ fn can_open_a_liquidity_position_in_caviarnine_that_fits_into_fee_limits() {
receipt.expect_commit_success();
let TransactionFeeSummary {
total_execution_cost_in_xrd,
total_finalization_cost_in_xrd,
total_tipping_cost_in_xrd,
total_storage_cost_in_xrd,
total_royalty_cost_in_xrd,
..
} = receipt.fee_summary;

assert!(
dbg!(
total_execution_cost_in_xrd
+ total_finalization_cost_in_xrd
+ total_tipping_cost_in_xrd
+ total_storage_cost_in_xrd
+ total_royalty_cost_in_xrd
) <= dec!(7)
);
assert!(total_execution_cost_in_xrd <= dec!(4.5))
assert!(total_execution_cost_in_xrd <= dec!(4.8))
}

#[test]
Expand Down Expand Up @@ -291,23 +278,10 @@ fn can_close_a_liquidity_position_in_caviarnine_that_fits_into_fee_limits() {
receipt.expect_commit_success();
let TransactionFeeSummary {
total_execution_cost_in_xrd,
total_finalization_cost_in_xrd,
total_tipping_cost_in_xrd,
total_storage_cost_in_xrd,
total_royalty_cost_in_xrd,
..
} = receipt.fee_summary;

assert!(
dbg!(
total_execution_cost_in_xrd
+ total_finalization_cost_in_xrd
+ total_tipping_cost_in_xrd
+ total_storage_cost_in_xrd
+ total_royalty_cost_in_xrd
) <= dec!(7)
);
assert!(total_execution_cost_in_xrd <= dec!(4.5))
assert!(total_execution_cost_in_xrd <= dec!(4.8))
}

#[test]
Expand Down Expand Up @@ -389,3 +363,56 @@ fn contributions_to_caviarnine_through_adapter_dont_fail_due_to_bucket_ordering(

Ok(())
}

#[test]
fn liquidity_receipt_includes_the_amount_of_liquidity_positions_we_expect_to_see(
) -> Result<(), RuntimeError> {
// Arrange
let Environment {
environment: ref mut env,
mut protocol,
resources,
caviarnine,
..
} = ScryptoTestEnv::new()?;
protocol
.ignition
.set_maximum_allowed_price_difference_percentage(dec!(0.03), env)?;

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

let (liquidity_receipt, _, _) = protocol.ignition.open_liquidity_position(
FungibleBucket(bitcoin_bucket),
caviarnine.pools.bitcoin.try_into().unwrap(),
LockupPeriod::from_months(6),
env,
)?;

// Act
let liquidity_receipt_data = ResourceManager(caviarnine.liquidity_receipt)
.get_non_fungible_data::<_, _, LiquidityReceipt>(
liquidity_receipt
.0
.non_fungible_local_ids(env)?
.first()
.unwrap()
.clone(),
env,
)?;

// Assert
let adapter_information = liquidity_receipt_data
.adapter_specific_information
.as_typed::<CaviarnineAdapterSpecificInformation>()
.unwrap();
assert_eq!(
adapter_information
.liquidity_provided_when_position_opened
.len(),
(PREFERRED_TOTAL_NUMBER_OF_HIGHER_AND_LOWER_BINS + 1) as usize
);

Ok(())
}

0 comments on commit 6640473

Please sign in to comment.