Skip to content

Commit

Permalink
[Tests]: Add tests for Ociswap v2.
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOmarA committed Feb 10, 2024
1 parent b6d34fe commit 13dbfd5
Show file tree
Hide file tree
Showing 10 changed files with 776 additions and 23 deletions.
19 changes: 19 additions & 0 deletions packages/ociswap-v2-adapter-v1/src/blueprint_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@ define_interface! {
fn y_address(&self) -> ResourceAddress;
fn registry(&self) -> ComponentAddress;
fn next_sync_time(&self) -> u64;
fn active_tick(&self) -> i32;
fn price_sqrt(&self) -> PreciseDecimal;
fn total_fees(&self, position_id: NonFungibleLocalId) -> (Decimal, Decimal);
}
}

define_interface! {
Registry as OciswapV2Registry impl [
ScryptoStub,
ScryptoTestStub,
#[cfg(feature = "manifest-builder-stubs")]
ManifestBuilderStub
] {
fn instantiate(
owner_badge_address: ResourceAddress,
fee_protocol_share: Decimal,
sync_period: u64,
sync_slots: u64
) -> Self;
}
}

Expand Down
106 changes: 96 additions & 10 deletions packages/ociswap-v2-adapter-v1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ macro_rules! define_error {
};
}

define_error! {}
define_error! {
RESOURCE_DOES_NOT_BELONG_ERROR
=> "One or more of the resources do not belong to pool.";
OVERFLOW_ERROR => "Calculation overflowed.";
UNEXPECTED_ERROR => "Unexpected error.";
}

#[blueprint_with_traits]
pub mod adapter {
Expand Down Expand Up @@ -60,23 +65,104 @@ pub mod adapter {
impl PoolAdapterInterfaceTrait for OciswapV2Adapter {
fn open_liquidity_position(
&mut self,
_pool_address: ComponentAddress,
_buckets: (Bucket, Bucket),
pool_address: ComponentAddress,
buckets: (Bucket, Bucket),
) -> OpenLiquidityPositionOutput {
todo!()
let mut pool = Self::pool(pool_address);

// Sorting the buckets according to the ordering of the pool itself.
let (bucket_x, bucket_y) = {
let resource_x = pool.x_address();
let resource_y = pool.y_address();

if buckets.0.resource_address() == resource_x
&& buckets.1.resource_address() == resource_y
{
(buckets.0, buckets.1)
} else if buckets.1.resource_address() == resource_x
&& buckets.0.resource_address() == resource_y
{
(buckets.1, buckets.0)
} else {
panic!("{}", RESOURCE_DOES_NOT_BELONG_ERROR)
}
};

// Contributing liquidity to the pool - the offset that is defined
// here is the amount of ticks that we need to contribute to get to
// a 20x upside and downside. We calculate this through a function
// provided by Ociswap: offset = ln(multiplier) / ln(1.0001) and
// then round up.
let active_tick = pool.active_tick();
let offset = 29959;

let lower_tick =
active_tick.checked_sub(offset).expect(OVERFLOW_ERROR);
let upper_tick =
active_tick.checked_add(offset).expect(OVERFLOW_ERROR);

let (receipt, change_x, change_y) =
pool.add_liquidity(lower_tick, upper_tick, bucket_x, bucket_y);

OpenLiquidityPositionOutput {
pool_units: receipt,
change: indexmap! {
change_x.resource_address() => change_x,
change_y.resource_address() => change_y,
},
others: Default::default(),
adapter_specific_information: AnyValue::from_typed(&())
.expect(UNEXPECTED_ERROR),
}
}

fn close_liquidity_position(
&mut self,
_pool_address: ComponentAddress,
_pool_units: Bucket,
_adapter_specific_information: AnyValue,
pool_address: ComponentAddress,
pool_units: Bucket,
_: AnyValue,
) -> CloseLiquidityPositionOutput {
todo!()
let mut pool = Self::pool(pool_address);

// Calculate how much fees were earned on the position while it was
// opened.
let resource_address_x = pool.x_address();
let resource_address_y = pool.y_address();
let (fees_x, fees_y) = pool.total_fees(
pool_units.as_non_fungible().non_fungible_local_id(),
);

// Close the liquidity position
let (resource_x, resource_y) =
pool.remove_liquidity(pool_units.as_non_fungible());

CloseLiquidityPositionOutput {
resources: indexmap! {
resource_x.resource_address() => resource_x,
resource_y.resource_address() => resource_y,
},
others: vec![],
fees: indexmap! {
resource_address_x => fees_x,
resource_address_y => fees_y,
},
}
}

fn price(&mut self, _pool_address: ComponentAddress) -> Price {
todo!()
fn price(&mut self, pool_address: ComponentAddress) -> Price {
let pool = Self::pool(pool_address);
let price_sqrt = pool.price_sqrt();
let price = price_sqrt
.checked_powi(2)
.and_then(|value| Decimal::try_from(value).ok())
.expect(OVERFLOW_ERROR);
let (resource_x, resource_y) =
self.resource_addresses(pool_address);
Price {
base: resource_x,
quote: resource_y,
price,
}
}

fn resource_addresses(
Expand Down
Binary file removed tests/assets/ociswap_v2.wasm
Binary file not shown.
Binary file not shown.
Binary file added tests/assets/ociswap_v2_pool.wasm
Binary file not shown.
Binary file added tests/assets/ociswap_v2_registry.rpd
Binary file not shown.
Binary file added tests/assets/ociswap_v2_registry.wasm
Binary file not shown.
98 changes: 86 additions & 12 deletions tests/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,34 @@ impl ScryptoTestEnv {
ociswap_v2_adapter_v1_package,
ociswap_v2_pools,
) = {
let ociswap_v2_package = {
let ociswap_v2_pool_package = {
let ociswap_v2_package_wasm =
include_bytes!("../assets/ociswap_v2.wasm");
include_bytes!("../assets/ociswap_v2_pool.wasm");
let ociswap_v2_package_rpd =
include_bytes!("../assets/ociswap_v2.rpd");
include_bytes!("../assets/ociswap_v2_pool.rpd");
let ociswap_v2_package_definition =
manifest_decode::<PackageDefinition>(
ociswap_v2_package_rpd,
)
.unwrap();

env.call_function_typed::<_, PackagePublishWasmOutput>(
PACKAGE_PACKAGE,
PACKAGE_BLUEPRINT,
PACKAGE_PUBLISH_WASM_IDENT,
&PackagePublishWasmInput {
code: ociswap_v2_package_wasm.to_vec(),
definition: ociswap_v2_package_definition,
metadata: Default::default(),
},
)?
.0
};
let ociswap_v2_registry_package = {
let ociswap_v2_package_wasm =
include_bytes!("../assets/ociswap_v2_registry.wasm");
let ociswap_v2_package_rpd =
include_bytes!("../assets/ociswap_v2_registry.rpd");
let ociswap_v2_package_definition =
manifest_decode::<PackageDefinition>(
ociswap_v2_package_rpd,
Expand All @@ -307,6 +330,16 @@ impl ScryptoTestEnv {
let ociswap_v2_adapter_v1_package =
Self::publish_package("ociswap-v2-adapter-v1", &mut env)?;

let registry =
OciswapV2RegistryInterfaceScryptoTestStub::instantiate(
GLOBAL_CALLER_VIRTUAL_BADGE,
dec!(0.03),
10080,
20,
ociswap_v2_registry_package,
&mut env,
)?;

let ociswap_v2_pools =
resource_addresses.try_map(|resource_address| {
let (resource_x, resource_y) = if XRD < *resource_address {
Expand All @@ -322,10 +355,10 @@ impl ScryptoTestEnv {
pdec!(1),
dec!(0.03),
dec!(0.03),
FAUCET,
registry.try_into().unwrap(),
vec![],
FAUCET,
ociswap_v2_package,
ociswap_v2_pool_package,
&mut env,
)?;

Expand All @@ -342,7 +375,7 @@ impl ScryptoTestEnv {
})?;

(
ociswap_v2_package,
ociswap_v2_pool_package,
ociswap_v2_adapter_v1_package,
ociswap_v2_pools,
)
Expand Down Expand Up @@ -599,6 +632,7 @@ impl ScryptoUnitEnv {
});
TestRunnerBuilder::new()
.with_custom_database(in_memory_substate_database)
.without_trace()
.build()
};

Expand Down Expand Up @@ -769,11 +803,11 @@ impl ScryptoUnitEnv {
ociswap_v2_adapter_v1_package,
ociswap_v2_pools,
) = {
let ociswap_v2_package = {
let ociswap_v2_pool_package = {
let ociswap_v2_package_wasm =
include_bytes!("../assets/ociswap_v2.wasm");
include_bytes!("../assets/ociswap_v2_pool.wasm");
let ociswap_v2_package_rpd =
include_bytes!("../assets/ociswap_v2.rpd");
include_bytes!("../assets/ociswap_v2_pool.rpd");
let ociswap_v2_package_definition =
manifest_decode::<PackageDefinition>(
ociswap_v2_package_rpd,
Expand All @@ -789,6 +823,46 @@ impl ScryptoUnitEnv {
Default::default(),
)
};
let ociswap_v2_registry_package = {
let ociswap_v2_package_wasm =
include_bytes!("../assets/ociswap_v2_registry.wasm");
let ociswap_v2_package_rpd =
include_bytes!("../assets/ociswap_v2_registry.rpd");
let ociswap_v2_package_definition =
manifest_decode::<PackageDefinition>(
ociswap_v2_package_rpd,
)
.unwrap();

test_runner.publish_package(
(
ociswap_v2_package_wasm.to_vec(),
ociswap_v2_package_definition,
),
Default::default(),
Default::default(),
)
};

let registry = test_runner
.execute_manifest(
ManifestBuilder::new()
.lock_fee_from_faucet()
.ociswap_v2_registry_instantiate(
ociswap_v2_registry_package,
GLOBAL_CALLER_VIRTUAL_BADGE,
dec!(0.03),
10080,
20,
)
.build(),
vec![],
)
.expect_commit_success()
.new_component_addresses()
.first()
.copied()
.unwrap();

let (code, definition) =
package_loader::PackageLoader::get("ociswap-v2-adapter-v1");
Expand All @@ -808,13 +882,13 @@ impl ScryptoUnitEnv {
let manifest = ManifestBuilder::new()
.lock_fee_from_faucet()
.ociswap_v2_pool_instantiate(
ociswap_v2_package,
ociswap_v2_pool_package,
resource_x,
resource_y,
pdec!(1),
dec!(0.03),
dec!(0.03),
FAUCET,
registry,
vec![],
FAUCET,
)
Expand Down Expand Up @@ -855,7 +929,7 @@ impl ScryptoUnitEnv {
});

(
ociswap_v2_package,
ociswap_v2_pool_package,
ociswap_v2_adapter_v1_package,
ociswap_v2_pools,
)
Expand Down
2 changes: 1 addition & 1 deletion tests/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![allow(ambiguous_glob_reexports)]
#![allow(ambiguous_glob_reexports, ambiguous_glob_imports)]

pub use crate::environment::*;
pub use crate::errors::*;
Expand Down
Loading

0 comments on commit 13dbfd5

Please sign in to comment.