From 87f5beb5ef50459a9e91c2946cd74c71b06c5089 Mon Sep 17 00:00:00 2001 From: Omar Date: Tue, 27 Feb 2024 15:14:14 +0300 Subject: [PATCH] [Tests]: Integrate defiplaza into the test environment. --- .vscode/settings.json | 1 + libraries/scrypto-interface/Cargo.toml | 1 + .../src/blueprint_interface.rs | 68 ++++- packages/ociswap-v1-adapter-v1/Cargo.toml | 2 +- tests/Cargo.toml | 4 + tests/src/environment.rs | 251 +++++++++++++++++- tests/src/prelude.rs | 2 + 7 files changed, 318 insertions(+), 11 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 2e06caaf..4264e1f7 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -9,6 +9,7 @@ "cSpell.words": [ "caviarnine", "coffieicnet", + "defi", "humantime", "ociswap", "OLYPS", diff --git a/libraries/scrypto-interface/Cargo.toml b/libraries/scrypto-interface/Cargo.toml index 1a4a20b7..850f17c0 100644 --- a/libraries/scrypto-interface/Cargo.toml +++ b/libraries/scrypto-interface/Cargo.toml @@ -11,6 +11,7 @@ quote = { version = "1.0.35" } syn = { version = "2.0.48", features = ["full", "extra-traits"] } [lib] +doctest = false proc-macro = true [lints] diff --git a/packages/defiplaza-v2-adapter-v1/src/blueprint_interface.rs b/packages/defiplaza-v2-adapter-v1/src/blueprint_interface.rs index 762a2a96..37993a83 100644 --- a/packages/defiplaza-v2-adapter-v1/src/blueprint_interface.rs +++ b/packages/defiplaza-v2-adapter-v1/src/blueprint_interface.rs @@ -2,7 +2,7 @@ use scrypto::prelude::*; use scrypto_interface::*; define_interface! { - PlazaDex as DefiPlazaV2Pool impl [ + PlazaPair as DefiPlazaV2Pool impl [ ScryptoStub, ScryptoTestStub, #[cfg(feature = "manifest-builder-stubs")] @@ -47,7 +47,16 @@ define_interface! { } #[derive( - ScryptoSbor, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, + ScryptoSbor, + ManifestSbor, + Copy, + Clone, + Debug, + PartialEq, + Eq, + PartialOrd, + Ord, + Hash, )] pub struct PairConfig { pub k_in: Decimal, @@ -57,7 +66,16 @@ pub struct PairConfig { } #[derive( - ScryptoSbor, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, + ScryptoSbor, + ManifestSbor, + Copy, + Clone, + Debug, + PartialEq, + Eq, + PartialOrd, + Ord, + Hash, )] pub struct TradeAllocation { pub base_base: Decimal, @@ -67,7 +85,16 @@ pub struct TradeAllocation { } #[derive( - ScryptoSbor, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, + ScryptoSbor, + ManifestSbor, + Copy, + Clone, + Debug, + PartialEq, + Eq, + PartialOrd, + Ord, + Hash, )] pub struct PairState { pub p0: Decimal, @@ -78,7 +105,16 @@ pub struct PairState { } #[derive( - ScryptoSbor, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, + ScryptoSbor, + ManifestSbor, + Copy, + Clone, + Debug, + PartialEq, + Eq, + PartialOrd, + Ord, + Hash, )] pub enum Shortage { BaseShortage, @@ -87,7 +123,16 @@ pub enum Shortage { } #[derive( - ScryptoSbor, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, + ScryptoSbor, + ManifestSbor, + Copy, + Clone, + Debug, + PartialEq, + Eq, + PartialOrd, + Ord, + Hash, )] pub enum ShortageState { Equilibrium, @@ -95,7 +140,16 @@ pub enum ShortageState { } #[derive( - ScryptoSbor, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, + ScryptoSbor, + ManifestSbor, + Copy, + Clone, + Debug, + PartialEq, + Eq, + PartialOrd, + Ord, + Hash, )] pub enum Asset { Base, diff --git a/packages/ociswap-v1-adapter-v1/Cargo.toml b/packages/ociswap-v1-adapter-v1/Cargo.toml index 1c6ceaa9..db42f94b 100644 --- a/packages/ociswap-v1-adapter-v1/Cargo.toml +++ b/packages/ociswap-v1-adapter-v1/Cargo.toml @@ -2,7 +2,7 @@ name = "ociswap-v1-adapter-v1" version.workspace = true edition.workspace = true -description = "Defines the adapter for Ociswap" +description = "Defines the adapter for Ociswap v1 - this is effectively deprecated since we won't be supporting Ociswap v1." [dependencies] sbor = { workspace = true } diff --git a/tests/Cargo.toml b/tests/Cargo.toml index 024ebdc7..929462f2 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -26,6 +26,10 @@ ociswap-v2-adapter-v1 = { path = "../packages/ociswap-v2-adapter-v1", features = "test", "manifest-builder-stubs" ] } +defiplaza-v2-adapter-v1 = { path = "../packages/defiplaza-v2-adapter-v1", features = [ + "test", + "manifest-builder-stubs" +] } caviarnine-v1-adapter-v1 = { path = "../packages/caviarnine-v1-adapter-v1", features = [ "test", "manifest-builder-stubs" diff --git a/tests/src/environment.rs b/tests/src/environment.rs index 34400f06..792a6f0f 100644 --- a/tests/src/environment.rs +++ b/tests/src/environment.rs @@ -14,10 +14,12 @@ pub trait EnvironmentSpecifier { type SimpleOracle; type OciswapV1Pool; type OciswapV2Pool; + type DefiPlazaV2Pool; type CaviarnineV1Pool; type OciswapV1Adapter; type OciswapV2Adapter; + type DefiPlazaV2Adapter; type CaviarnineV1Adapter; // Badges @@ -35,10 +37,12 @@ impl EnvironmentSpecifier for ScryptoTestEnvironmentSpecifier { type SimpleOracle = SimpleOracle; type OciswapV1Pool = OciswapV1PoolInterfaceScryptoTestStub; type OciswapV2Pool = OciswapV2PoolInterfaceScryptoTestStub; + type DefiPlazaV2Pool = DefiPlazaV2PoolInterfaceScryptoTestStub; type CaviarnineV1Pool = CaviarnineV1PoolInterfaceScryptoTestStub; type OciswapV1Adapter = OciswapV1Adapter; type OciswapV2Adapter = OciswapV2Adapter; + type DefiPlazaV2Adapter = DefiPlazaV2Adapter; type CaviarnineV1Adapter = CaviarnineV1Adapter; // Badges @@ -56,10 +60,12 @@ impl EnvironmentSpecifier for ScryptoUnitEnvironmentSpecifier { type SimpleOracle = ComponentAddress; type OciswapV1Pool = ComponentAddress; type OciswapV2Pool = ComponentAddress; + type DefiPlazaV2Pool = ComponentAddress; type CaviarnineV1Pool = ComponentAddress; type OciswapV1Adapter = ComponentAddress; type OciswapV2Adapter = ComponentAddress; + type DefiPlazaV2Adapter = ComponentAddress; type CaviarnineV1Adapter = ComponentAddress; // Badges @@ -80,6 +86,12 @@ impl EnvironmentSpecifier for ScryptoUnitEnvironmentSpecifier { /// returned back to the caller. Additionally, the auth module will be disabled /// by default for the created test environment. If it needs to be enabled then /// that must happen after the creation of the environment. +// Not quite a todo but more of a thought for the future. We publish all of the +// packages and create pools for all of the dexes when we realistically will not +// be using more than a single dex in a test (and if we do then its weird.). It +// would have been better for this to only be initialized with the state of the +// dex that we want instead of all of them. That would make it much faster to +// run. pub struct Environment where S: EnvironmentSpecifier, @@ -92,6 +104,7 @@ where /* Supported Dexes */ pub ociswap_v1: DexEntities, pub ociswap_v2: DexEntities, + pub defiplaza_v2: DexEntities, pub caviarnine_v1: DexEntities, } @@ -144,6 +157,7 @@ impl ScryptoTestEnv { // environment. If somebody want it, they can enable it after they // instantiate the environment. env.disable_auth_module(); + env.disable_limits_module(); // Creating the badges and their access rules let protocol_manager_badge = @@ -188,7 +202,7 @@ impl ScryptoTestEnv { // Creating the liquidity receipt resource that each of the exchanges // will use. - let [ociswap_v1_liquidity_receipt_resource, ociswap_v2_liquidity_receipt_resource, caviarnine_v1_liquidity_receipt_resource] = + let [ociswap_v1_liquidity_receipt_resource, ociswap_v2_liquidity_receipt_resource, defiplaza_v2_liquidity_receipt_resource, caviarnine_v1_liquidity_receipt_resource] = std::array::from_fn(|_| { ResourceBuilder::new_ruid_non_fungible::< LiquidityReceipt, @@ -383,6 +397,89 @@ impl ScryptoTestEnv { ) }; + let ( + defiplaza_v2_package, + defiplaza_v2_adapter_v1_package, + defiplaza_v2_pools, + ) = { + let defiplaza_v2_pool_package = { + let defiplaza_v2_package_wasm = + include_bytes!("../assets/defiplaza_v2.wasm"); + let defiplaza_v2_package_rpd = + include_bytes!("../assets/defiplaza_v2.rpd"); + let defiplaza_v2_package_definition = + manifest_decode::( + defiplaza_v2_package_rpd, + ) + .unwrap(); + + env.call_function_typed::<_, PackagePublishWasmOutput>( + PACKAGE_PACKAGE, + PACKAGE_BLUEPRINT, + PACKAGE_PUBLISH_WASM_IDENT, + &PackagePublishWasmInput { + code: defiplaza_v2_package_wasm.to_vec(), + definition: defiplaza_v2_package_definition, + metadata: Default::default(), + }, + )? + .0 + }; + + let defiplaza_v2_adapter_v1_package = + Self::publish_package("defiplaza-v2-adapter-v1", &mut env)?; + + let defiplaza_v2_pools = resource_addresses.try_map(|resource_address| { + let (resource_x, resource_y) = if XRD < *resource_address { + (XRD, *resource_address) + } else { + (*resource_address, XRD) + }; + + let mut defiplaza_pool = DefiPlazaV2PoolInterfaceScryptoTestStub::instantiate_pair( + OwnerRole::None, + resource_x, + resource_y, + // This pair config is obtained from DefiPlaza's + // repo. + PairConfig { + k_in: dec!("0.4"), + k_out: dec!("1"), + fee: dec!("0"), + decay_factor: dec!("0.9512"), + }, + dec!(1), + defiplaza_v2_pool_package, + &mut env, + )?; + + let resource_x = + ResourceManager(resource_x).mint_fungible(dec!(100_000_000), &mut env)?; + let resource_y = + ResourceManager(resource_y).mint_fungible(dec!(100_000_000), &mut env)?; + + let (_, change1) = + defiplaza_pool.add_liquidity(resource_x, None, &mut env)?; + let (_, change2) = defiplaza_pool.add_liquidity(resource_y, None, &mut env)?; + let change_amount1 = change1 + .map(|bucket| bucket.amount(&mut env).unwrap()) + .unwrap_or_default(); + assert_eq!(change_amount1, dec!(0)); + let change_amount2 = change2 + .map(|bucket| bucket.amount(&mut env).unwrap()) + .unwrap_or_default(); + assert_eq!(change_amount2, dec!(0)); + + Ok::<_, RuntimeError>(defiplaza_pool) + })?; + + ( + defiplaza_v2_pool_package, + defiplaza_v2_adapter_v1_package, + defiplaza_v2_pools, + ) + }; + // Instantiating the components. let mut simple_oracle = SimpleOracle::instantiate( protocol_manager_rule.clone(), @@ -420,6 +517,13 @@ impl ScryptoTestEnv { ociswap_v2_adapter_v1_package, &mut env, )?; + let defiplaza_v2_adapter_v1 = DefiPlazaV2Adapter::instantiate( + Default::default(), + OwnerRole::None, + None, + defiplaza_v2_adapter_v1_package, + &mut env, + )?; let caviarnine_v1_adapter_v1 = CaviarnineV1Adapter::instantiate( Default::default(), OwnerRole::None, @@ -524,6 +628,21 @@ impl ScryptoTestEnv { &mut env, )?; + ignition.insert_pool_information( + DefiPlazaV2PoolInterfaceScryptoTestStub::blueprint_id( + defiplaza_v2_package, + ), + PoolBlueprintInformation { + adapter: defiplaza_v2_adapter_v1.try_into().unwrap(), + allowed_pools: defiplaza_v2_pools + .iter() + .map(|pool| pool.try_into().unwrap()) + .collect(), + liquidity_receipt: defiplaza_v2_liquidity_receipt_resource, + }, + &mut env, + )?; + ignition.insert_pool_information( CaviarnineV1PoolInterfaceScryptoTestStub::blueprint_id( caviarnine_v1_package, @@ -565,6 +684,13 @@ impl ScryptoTestEnv { adapter: ociswap_v2_adapter_v1, liquidity_receipt: ociswap_v2_liquidity_receipt_resource, }, + defiplaza_v2: DexEntities { + package: defiplaza_v2_package, + pools: defiplaza_v2_pools, + adapter_package: defiplaza_v2_adapter_v1_package, + adapter: defiplaza_v2_adapter_v1, + liquidity_receipt: defiplaza_v2_liquidity_receipt_resource, + }, caviarnine_v1: DexEntities { package: caviarnine_v1_package, pools: caviarnine_v1_pools, @@ -670,7 +796,7 @@ impl ScryptoUnitEnv { ) }); - let [ociswap_v1_liquidity_receipt_resource, ociswap_v2_liquidity_receipt_resource, caviarnine_v1_liquidity_receipt_resource] = + let [ociswap_v1_liquidity_receipt_resource, ociswap_v2_liquidity_receipt_resource, defiplaza_v2_liquidity_receipt_resource, caviarnine_v1_liquidity_receipt_resource] = std::array::from_fn(|_| { test_runner .execute_manifest( @@ -937,6 +1063,110 @@ impl ScryptoUnitEnv { ) }; + let ( + defiplaza_v2_package, + defiplaza_v2_adapter_v1_package, + defiplaza_v2_pools, + ) = { + let defiplaza_v2_pool_package = { + let defiplaza_v2_package_wasm = + include_bytes!("../assets/defiplaza_v2.wasm"); + let defiplaza_v2_package_rpd = + include_bytes!("../assets/defiplaza_v2.rpd"); + let defiplaza_v2_package_definition = + manifest_decode::( + defiplaza_v2_package_rpd, + ) + .unwrap(); + + test_runner.publish_package( + ( + defiplaza_v2_package_wasm.to_vec(), + defiplaza_v2_package_definition, + ), + Default::default(), + Default::default(), + ) + }; + + let (code, definition) = + package_loader::PackageLoader::get("defiplaza-v2-adapter-v1"); + let defiplaza_v2_adapter_v1_package = test_runner.publish_package( + (code, definition), + Default::default(), + OwnerRole::None, + ); + + let defiplaza_v2_pools = + resource_addresses.map(|resource_address| { + let (resource_x, resource_y) = if XRD < *resource_address { + (XRD, *resource_address) + } else { + (*resource_address, XRD) + }; + + let manifest = ManifestBuilder::new() + .lock_fee_from_faucet() + .defi_plaza_v2_pool_instantiate_pair( + defiplaza_v2_pool_package, + OwnerRole::None, + resource_x, + resource_y, + PairConfig { + k_in: dec!("0.4"), + k_out: dec!("1"), + fee: dec!("0"), + decay_factor: dec!("0.9512"), + }, + dec!(1), + ) + .build(); + let component_address = *test_runner + .execute_manifest(manifest, vec![]) + .expect_commit_success() + .new_component_addresses() + .first() + .unwrap(); + + let manifest = ManifestBuilder::new() + .lock_fee_from_faucet() + .mint_fungible(XRD, dec!(100_000_000)) + .mint_fungible(*resource_address, dec!(100_000_000)) + .take_all_from_worktop(resource_x, "resource_x_bucket") + .take_all_from_worktop(resource_y, "resource_y_bucket") + .with_name_lookup(|builder, _| { + let resource_x_bucket = + builder.bucket("resource_x_bucket"); + let resource_y_bucket = + builder.bucket("resource_y_bucket"); + builder + .defi_plaza_v2_pool_add_liquidity( + component_address, + resource_x_bucket, + None, + ) + .defi_plaza_v2_pool_add_liquidity( + component_address, + resource_y_bucket, + None, + ) + }) + .try_deposit_entire_worktop_or_abort(account, None) + .build(); + test_runner + .execute_manifest_without_auth(manifest) + .expect_commit_success(); + + component_address + }); + + ( + defiplaza_v2_pool_package, + defiplaza_v2_adapter_v1_package, + defiplaza_v2_pools, + ) + }; + let simple_oracle = test_runner .execute_manifest( ManifestBuilder::new() @@ -1010,10 +1240,11 @@ impl ScryptoUnitEnv { .copied() .unwrap(); - let [ociswap_v1_adapter_v1, ociswap_v2_adapter_v1, caviarnine_v1_adapter_v1] = + let [ociswap_v1_adapter_v1, ociswap_v2_adapter_v1, defiplaza_v2_adapter_v1, caviarnine_v1_adapter_v1] = [ (ociswap_v1_adapter_v1_package, "OciswapV1Adapter"), (ociswap_v2_adapter_v1_package, "OciswapV2Adapter"), + (defiplaza_v2_adapter_v1_package, "DefiPlazaV2Adapter"), (caviarnine_v1_adapter_v1_package, "CaviarnineV1Adapter"), ] .map(|(package_address, blueprint_name)| { @@ -1142,6 +1373,13 @@ impl ScryptoUnitEnv { ociswap_v2_package, "LiquidityPool", ), + ( + defiplaza_v2_adapter_v1, + defiplaza_v2_pools, + defiplaza_v2_liquidity_receipt_resource, + defiplaza_v2_package, + "PlazaDex", + ), ( caviarnine_v1_adapter_v1, caviarnine_v1_pools, @@ -1217,6 +1455,13 @@ impl ScryptoUnitEnv { adapter: ociswap_v2_adapter_v1, liquidity_receipt: ociswap_v2_liquidity_receipt_resource, }, + defiplaza_v2: DexEntities { + package: defiplaza_v2_package, + pools: defiplaza_v2_pools, + adapter_package: defiplaza_v2_adapter_v1_package, + adapter: defiplaza_v2_adapter_v1, + liquidity_receipt: defiplaza_v2_liquidity_receipt_resource, + }, caviarnine_v1: DexEntities { package: caviarnine_v1_package, pools: caviarnine_v1_pools, diff --git a/tests/src/prelude.rs b/tests/src/prelude.rs index f0f29cce..e397db6d 100644 --- a/tests/src/prelude.rs +++ b/tests/src/prelude.rs @@ -12,6 +12,7 @@ pub use scrypto_test::prelude::*; pub use scrypto_unit::*; pub use ::caviarnine_v1_adapter_v1::test_bindings::*; +pub use ::defiplaza_v2_adapter_v1::test_bindings::*; pub use ::ignition::test_bindings::*; pub use ::ignition::*; pub use ::ociswap_v1_adapter_v1::test_bindings::*; @@ -19,6 +20,7 @@ pub use ::ociswap_v2_adapter_v1::test_bindings::*; pub use ::simple_oracle::test_bindings::*; pub use ::caviarnine_v1_adapter_v1::*; +pub use ::defiplaza_v2_adapter_v1::*; pub use ::ociswap_v1_adapter_v1::*; pub use ::ociswap_v2_adapter_v1::*;