Skip to content

Commit

Permalink
[Caviarnine Adapter]: Update adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOmarA committed Jun 18, 2024
1 parent 0ac9014 commit 12eeaa2
Show file tree
Hide file tree
Showing 26 changed files with 675 additions and 387 deletions.
31 changes: 31 additions & 0 deletions 98c16ce3e2d33c78ad0d5699983193f4111c724519c10cadd1acf9417e749513

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ define_interface! {
&mut self,
pool_address: ComponentAddress,
buckets: (Bucket, Bucket),
_: LockupPeriod
) -> OpenLiquidityPositionOutput;
fn close_liquidity_position(
&mut self,
Expand Down
28 changes: 28 additions & 0 deletions c6cdb2b9b2f7382fce3a3b41963f96d1670ccc67fc2dd546fc9de52fe2829c69
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"manifest": "CALL_METHOD\n Address(\"account_rdx129gevyk2dkt2zmm8rh2mtkqhdn3gnkyw3sxngjwccgaampfhjafv50\")\n \"try_deposit_batch_or_abort\"\n Expression(\"ENTIRE_WORKTOP\")\n Enum<0u8>()\n;\n",
"blobs_hex": [],
"start_epoch_inclusive": 108597,
"end_epoch_exclusive": 108607,
"notary_public_key": {
"key_type": "EddsaEd25519",
"key_hex": "da9cf1363a2d9a7d15d63d28562768b61c94a935c1374d18224e27a8f98ade7b"
},
"notary_is_signatory": true,
"tip_percentage": 0,
"nonce": 198232210,
"signer_public_keys": [
{
"key_type": "EddsaEd25519",
"key_hex": "da9cf1363a2d9a7d15d63d28562768b61c94a935c1374d18224e27a8f98ade7b"
},
{
"key_type": "EddsaEd25519",
"key_hex": "0263adf695eabf6c18a7eb86088b03f93640f0f756aae5b3ec9a9761b9d3870a"
}
],
"flags": {
"use_free_credit": true,
"assume_all_signature_proofs": false,
"skip_epoch_check": false
}
}
2 changes: 1 addition & 1 deletion libraries/common/src/lockup_period.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use humantime::format_duration;

/// A type used for the lockup period that can be creates from various time
/// durations and that implements display in the desired way.
#[derive(Clone, Copy, Sbor, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Sbor, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[sbor(transparent)]
pub struct LockupPeriod(u64);

Expand Down
1 change: 1 addition & 0 deletions libraries/ports-interface/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ define_interface! {
pool_address: ComponentAddress,
#[manifest_type = "(ManifestBucket, ManifestBucket)"]
buckets: (Bucket, Bucket),
lockup_period: LockupPeriod
) -> OpenLiquidityPositionOutput;

/// Closes a liquidity position on the passed pool.
Expand Down
1 change: 1 addition & 0 deletions packages/caviarnine-v1-adapter-v1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ pub mod adapter {
&mut self,
pool_address: ComponentAddress,
buckets: (Bucket, Bucket),
_: LockupPeriod,
) -> OpenLiquidityPositionOutput {
let mut pool = pool!(pool_address);

Expand Down
39 changes: 31 additions & 8 deletions packages/caviarnine-v1-adapter-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,16 @@ macro_rules! pool {
};
}

type InnerKv = KeyValueStore<LockupPeriod, ContributionBinConfiguration>;

#[blueprint_with_traits]
#[types(ComponentAddress, PoolInformation, ContributionBinConfiguration)]
#[types(
ComponentAddress,
PoolInformation,
ContributionBinConfiguration,
LockupPeriod,
InnerKv
)]
pub mod adapter {
enable_method_auth! {
roles {
Expand Down Expand Up @@ -106,8 +114,10 @@ pub mod adapter {
/// a given pool. The key is the pool's component address and the value
/// is the start and end bins where the end bin must be strictly larger
/// than the start bin and can't be equal.
pool_contribution_bin_configuration:
KeyValueStore<ComponentAddress, ContributionBinConfiguration>,
pool_contribution_bin_configuration: KeyValueStore<
ComponentAddress,
KeyValueStore<LockupPeriod, ContributionBinConfiguration>,
>,
}

impl CaviarnineV1Adapter {
Expand Down Expand Up @@ -150,10 +160,20 @@ pub mod adapter {
pub fn upsert_pool_contribution_bin_configuration(
&mut self,
pool: ComponentAddress,
lockup_period: LockupPeriod,
configuration: ContributionBinConfiguration,
) {
self.pool_contribution_bin_configuration
.insert(pool, configuration)
let entry = self.pool_contribution_bin_configuration.get_mut(&pool);
match entry {
Some(kv_store) => kv_store.insert(lockup_period, configuration),
None => {
drop(entry);
let kv_store = KeyValueStore::new_with_registered_type();
kv_store.insert(lockup_period, configuration);
self.pool_contribution_bin_configuration
.insert(pool, kv_store)
}
}
}

pub fn preload_pool_information(
Expand Down Expand Up @@ -262,6 +282,7 @@ pub mod adapter {
&mut self,
pool_address: ComponentAddress,
buckets: (Bucket, Bucket),
lockup_period: LockupPeriod,
) -> OpenLiquidityPositionOutput {
let mut pool = pool!(pool_address);

Expand Down Expand Up @@ -302,9 +323,10 @@ pub mod adapter {
let ContributionBinConfiguration {
start_tick: lowest_tick,
end_tick: highest_tick,
} = *self
} = self
.pool_contribution_bin_configuration
.get(&pool_address)
.and_then(|value| value.get(&lockup_period).map(|value| *value))
.expect(POOL_HAS_NO_BIN_CONFIG);

let lower_ticks = (lowest_tick..active_tick)
Expand All @@ -316,6 +338,7 @@ pub mod adapter {

// Ensure that the currently active tick is in the span of ticks
// that we're allowed to contribute to. If not then error out.
info!("active_tick = {active_tick}");
if active_tick > highest_tick || active_tick < lowest_tick {
panic!("{}", ACTIVE_TICK_IS_OUTSIDE_OF_ALLOWED_RANGE);
}
Expand Down Expand Up @@ -364,10 +387,10 @@ pub mod adapter {

let position_amount_x = amount_x
.checked_div(higher_ticks.len() as u32)
.expect(OVERFLOW_ERROR);
.unwrap_or(Decimal::ZERO);
let position_amount_y = amount_y
.checked_div(lower_ticks.len() as u32)
.expect(OVERFLOW_ERROR);
.unwrap_or(Decimal::ZERO);

(position_amount_x, position_amount_y)
};
Expand Down
1 change: 1 addition & 0 deletions packages/defiplaza-v2-adapter-v1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ pub mod adapter {
&mut self,
pool_address: ComponentAddress,
buckets: (Bucket, Bucket),
_: LockupPeriod,
) -> OpenLiquidityPositionOutput {
// When opening a liquidity position we follow the algorithm that
// Jazzer described to us:
Expand Down
1 change: 1 addition & 0 deletions packages/ignition/src/blueprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ mod ignition {
} = adapter.open_liquidity_position(
pool_address,
(user_side_of_liquidity.0, protocol_side_of_liquidity.0),
lockup_period,
);

// Calculate the amount of resources that was actually contributed
Expand Down
1 change: 1 addition & 0 deletions packages/ociswap-v1-adapter-v1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ pub mod adapter {
&mut self,
pool_address: ComponentAddress,
buckets: (Bucket, Bucket),
_: LockupPeriod,
) -> OpenLiquidityPositionOutput {
let mut pool = pool!(pool_address);

Expand Down
1 change: 1 addition & 0 deletions packages/ociswap-v2-adapter-v1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ pub mod adapter {
&mut self,
pool_address: ComponentAddress,
buckets: (Bucket, Bucket),
_: LockupPeriod,
) -> OpenLiquidityPositionOutput {
let mut pool = pool!(pool_address);

Expand Down
7 changes: 1 addition & 6 deletions testing/stateful-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,7 @@ fn get_database() -> &'static ActualStateManagerDatabase {
STATE_MANAGER_DATABASE_PATH_ENVIRONMENT_VARIABLE
);
};
ActualStateManagerDatabase::new(
state_manager_database_path,
Default::default(),
&NetworkDefinition::mainnet(),
)
.unwrap()
ActualStateManagerDatabase::new_read_only(state_manager_database_path)
})
}

Expand Down
Loading

0 comments on commit 12eeaa2

Please sign in to comment.