Skip to content

Commit

Permalink
Merge pull request #12 from radixdlt/feature/update-ociswap-v2
Browse files Browse the repository at this point in the history
Update Ociswap v2 based on their changes
  • Loading branch information
0xOmarA authored Mar 12, 2024
2 parents 19d95d3 + a6ce678 commit d3a2591
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 35 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ default_to_workspace = false
command = "cargo"
args = [
"test",
"--release",
"--features",
"package-loader/build-time-blueprints",
"--no-fail-fast",
Expand Down
2 changes: 0 additions & 2 deletions libraries/package-loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ scrypto-unit = { workspace = true }
radix-engine-common = { workspace = true }
radix-engine-queries = { workspace = true }

lazy_static = { verison = "1.4.0", optional = true }
getrandom = { version = "0.2.12", features = ["js"] }

[build-dependencies]
Expand All @@ -24,7 +23,6 @@ radix-engine-interface = { workspace = true, optional = true }
[features]
default = []
build-time-blueprints = [
"dep:lazy_static",
"dep:walkdir",
"dep:cargo_toml",
"dep:radix-engine",
Expand Down
12 changes: 6 additions & 6 deletions libraries/package-loader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
mod package_loader {
use radix_engine_common::prelude::*;
use radix_engine_queries::typed_substate_layout::*;
use std::sync::*;

const PACKAGES_BINARY: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/compiled_packages.bin"));

lazy_static::lazy_static! {
static ref PACKAGES: HashMap<String, (Vec<u8>, PackageDefinition)> = {
scrypto_decode(PACKAGES_BINARY).unwrap()
};
}
static PACKAGES: OnceLock<HashMap<String, (Vec<u8>, PackageDefinition)>> =
OnceLock::new();

pub struct PackageLoader;
impl PackageLoader {
pub fn get(name: &str) -> (Vec<u8>, PackageDefinition) {
if let Some(rtn) = PACKAGES.get(name) {
let packages = PACKAGES
.get_or_init(|| scrypto_decode(PACKAGES_BINARY).unwrap());
if let Some(rtn) = packages.get(name) {
rtn.clone()
} else {
panic!("Package \"{}\" not found. Are you sure that this package is: a) in the blueprints folder, b) that this is the same as the package name in the Cargo.toml file?", name)
Expand Down
3 changes: 3 additions & 0 deletions packages/ociswap-v2-adapter-v1/src/blueprint_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ define_interface! {
x_address: ResourceAddress,
y_address: ResourceAddress,
price_sqrt: PreciseDecimal,
tick_spacing: u32,
input_fee_rate: Decimal,
flash_loan_fee_rate: Decimal,
registry_address: ComponentAddress,
Expand All @@ -25,6 +26,7 @@ define_interface! {
#[manifest_type = "ManifestBucket"]
y_bucket: Bucket,
price_sqrt: PreciseDecimal,
tick_spacing: u32,
input_fee_rate: Decimal,
flash_loan_fee_rate: Decimal,
registry_address: ComponentAddress,
Expand All @@ -34,6 +36,7 @@ define_interface! {
left_bound: i32,
right_bound: i32
) -> (Self, ResourceAddress, Bucket, Bucket, Bucket);
fn tick_spacing(&self) -> u32;
fn add_liquidity(
&mut self,
left_bound: i32,
Expand Down
36 changes: 28 additions & 8 deletions packages/ociswap-v2-adapter-v1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,24 @@ pub mod adapter {
.expect(OVERFLOW_ERROR);
let offset = 29959;

// Ociswap, just like Caviarnine, have a tick spacing parameter that
// means that not all ticks are valid. A valid tick is one that is
// divisible by the tick spacing. Therefore, the following step will
// convert the offset defined above to be valid for the tick spacing
// of the pool. If the offset is divisible by the tick spacing then
// nothing needs to be done. If it is not, then we round up to the
// nearest tick space.
let tick_spacing = pool.tick_spacing() as i32;
let offset = if offset % tick_spacing == 0 {
offset
} else {
offset
.checked_div(tick_spacing)
.and_then(|value| value.checked_mul(tick_spacing))
.and_then(|value| value.checked_add(tick_spacing))
.expect(OVERFLOW_ERROR)
};

let lower_tick =
active_tick.checked_sub(offset).expect(OVERFLOW_ERROR);
let upper_tick =
Expand Down Expand Up @@ -274,12 +292,14 @@ impl From<OciswapV2AdapterSpecificInformation> for AnyValue {

#[derive(NonFungibleData, ScryptoSbor, Debug, Clone)]
pub struct LiquidityPosition {
liquidity: PreciseDecimal,
left_bound: i32,
right_bound: i32,
shape_id: Option<NonFungibleLocalId>,
x_fee_checkpoint: PreciseDecimal,
y_fee_checkpoint: PreciseDecimal,
x_total_fee_checkpoint: PreciseDecimal,
y_total_fee_checkpoint: PreciseDecimal,
pub liquidity: PreciseDecimal,
pub left_bound: i32,
pub right_bound: i32,
pub shape_id: Option<NonFungibleLocalId>,
pub added_at: u64,
pub x_fee_checkpoint: PreciseDecimal,
pub y_fee_checkpoint: PreciseDecimal,
pub x_total_fee_checkpoint: PreciseDecimal,
pub y_total_fee_checkpoint: PreciseDecimal,
pub seconds_inside_checkpoint: i64,
}
29 changes: 12 additions & 17 deletions testing/stateful-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
use common::prelude::*;
use extend::*;
use publishing_tool::component_address;
use publishing_tool::configuration_selector::*;
use publishing_tool::database_overlay::*;
use publishing_tool::network_connection_provider::*;
use publishing_tool::publishing::*;
use publishing_tool::*;
use radix_engine::system::system_modules::*;
use radix_engine::transaction::*;
use radix_engine::vm::*;
use radix_engine_interface::blueprints::account::*;
use scrypto_unit::*;
use state_manager::RocksDBStore;
use std::ops::*;
use std::sync::*;
use transaction::prelude::*;

lazy_static::lazy_static! {
/// The substate manager database is a lazy-static since it takes a lot of
/// time to be opened for read-only and this had a very negative impact on
/// tests. Keep in mind that this now means that we should keep all of the
/// tests to one module and that we should use `cargo test` and not nextest.
static ref SUBSTATE_MANAGER_DATABASE: RocksDBStore = {
pub type StatefulTestRunner<'a> = TestRunner<
NoExtension,
UnmergeableSubstateDatabaseOverlay<'a, RocksDBStore>,
>;

fn get_database() -> &'static RocksDBStore {
static DATABASE: OnceLock<RocksDBStore> = OnceLock::new();
DATABASE.get_or_init(|| {
const STATE_MANAGER_DATABASE_PATH_ENVIRONMENT_VARIABLE: &str =
"STATE_MANAGER_DATABASE_PATH";
let Ok(state_manager_database_path) =
Expand All @@ -34,14 +37,9 @@ lazy_static::lazy_static! {
RocksDBStore::new_read_only(state_manager_database_path).expect(
"Failed to create a new instance of the state manager database",
)
};
})
}

pub type StatefulTestRunner<'a> = TestRunner<
NoExtension,
UnmergeableSubstateDatabaseOverlay<'a, RocksDBStore>,
>;

pub fn execute_test_within_environment<F, O>(test_function: F) -> O
where
F: Fn(
Expand All @@ -53,11 +51,8 @@ where
) -> O,
{
// Creating the database and the necessary overlays to run the tests.

let overlayed_state_manager_database =
UnmergeableSubstateDatabaseOverlay::new_unmergeable(
SUBSTATE_MANAGER_DATABASE.deref(),
);
UnmergeableSubstateDatabaseOverlay::new_unmergeable(get_database());

// Creating a test runner from the overlayed state manager database
let mut test_runner = TestRunnerBuilder::new()
Expand Down
Binary file modified testing/tests/assets/ociswap_v2_pool.rpd
Binary file not shown.
Binary file modified testing/tests/assets/ociswap_v2_pool.wasm
Binary file not shown.
Binary file modified testing/tests/assets/ociswap_v2_registry.wasm
Binary file not shown.
2 changes: 2 additions & 0 deletions testing/tests/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ impl ScryptoTestEnv {
resource_x,
resource_y,
pdec!(1),
60,
dec!(0.03),
dec!(0.03),
registry.try_into().unwrap(),
Expand Down Expand Up @@ -1035,6 +1036,7 @@ impl ScryptoUnitEnv {
resource_x,
resource_y,
pdec!(1),
60,
dec!(0.03),
dec!(0.03),
registry,
Expand Down
3 changes: 2 additions & 1 deletion testing/tests/tests/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -730,9 +730,10 @@ fn cant_add_an_allowed_pool_where_neither_of_the_resources_is_the_protocol_resou
let fungible2 = ResourceBuilder::new_fungible(OwnerRole::None)
.mint_initial_supply(100, env)?;
let (pool, ..) = OciswapV2PoolInterfaceScryptoTestStub::instantiate(
fungible1.resource_address(env)?,
fungible2.resource_address(env)?,
fungible1.resource_address(env)?,
pdec!(1),
60,
dec!(0.03),
dec!(0.03),
FAUCET,
Expand Down

0 comments on commit d3a2591

Please sign in to comment.