Skip to content

Commit

Permalink
[Tests]: Initial tests for DefiPlaza.
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOmarA committed Feb 27, 2024
1 parent 87f5beb commit 48cd4f4
Show file tree
Hide file tree
Showing 3 changed files with 282 additions and 3 deletions.
14 changes: 12 additions & 2 deletions packages/defiplaza-v2-adapter-v1/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,18 @@ pub mod adapter {
// Step 4: Contribute to the pool. The first bucket to provide the
// pool is the bucket of the asset in shortage or the asset that we
// now refer to as "first" and then followed by the "second" bucket.
let (first_pool_units, second_change) =
pool.add_liquidity(first_bucket, Some(second_bucket));
//
// In the case of equilibrium we do not contribute the second bucket
// and instead just the first bucket.
let (first_pool_units, second_change) = match shortage_state {
ShortageState::Equilibrium => (
pool.add_liquidity(first_bucket, None).0,
Some(second_bucket),
),
ShortageState::Shortage(_) => {
pool.add_liquidity(first_bucket, Some(second_bucket))
}
};

// Step 5: Calculate and store the original target of the second
// liquidity position. This is calculated as the amount of assets
Expand Down
2 changes: 1 addition & 1 deletion tests/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,7 @@ impl ScryptoUnitEnv {
defiplaza_v2_pools,
defiplaza_v2_liquidity_receipt_resource,
defiplaza_v2_package,
"PlazaDex",
"PlazaPair",
),
(
caviarnine_v1_adapter_v1,
Expand Down
269 changes: 269 additions & 0 deletions tests/tests/defiplaza_v2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
#![allow(clippy::arithmetic_side_effects)]

use tests::prelude::*;

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

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

// Act
let rtn = protocol.ignition.open_liquidity_position(
FungibleBucket(bitcoin_bucket),
defiplaza_v2.pools.bitcoin.try_into().unwrap(),
LockupPeriod::from_months(6).unwrap(),
env,
);

// Assert
let _ = rtn.expect("Should succeed!");

Ok(())
}

#[test]
fn can_open_a_liquidity_position_in_defiplaza_that_fits_into_fee_limits() {
// Arrange
let ScryptoUnitEnv {
environment: mut test_runner,
resources,
protocol,
defiplaza_v2,
..
} = ScryptoUnitEnv::new_with_configuration(Configuration {
maximum_allowed_relative_price_difference: dec!(0.03),
..Default::default()
});
let (_, private_key, account_address, _) = protocol.protocol_owner_badge;

test_runner
.execute_manifest(
ManifestBuilder::new()
.lock_fee_from_faucet()
.mint_fungible(resources.bitcoin, dec!(100_000_000_000_000))
.try_deposit_entire_worktop_or_abort(account_address, None)
.build(),
vec![],
)
.expect_commit_success();

test_runner
.execute_manifest_with_enabled_modules(
ManifestBuilder::new()
.lock_fee_from_faucet()
.withdraw_from_account(
account_address,
resources.bitcoin,
dec!(100_000),
)
.take_all_from_worktop(resources.bitcoin, "bitcoin")
.with_bucket("bitcoin", |builder, bucket| {
builder.call_method(
protocol.ignition,
"open_liquidity_position",
(
bucket,
defiplaza_v2.pools.bitcoin,
LockupPeriod::from_months(6).unwrap(),
),
)
})
.try_deposit_entire_worktop_or_abort(account_address, None)
.build(),
EnabledModules::for_test_transaction()
& !EnabledModules::AUTH
& !EnabledModules::COSTING,
)
.expect_commit_success();

// Act
let receipt = test_runner.construct_and_execute_notarized_transaction(
ManifestBuilder::new()
.lock_fee_from_faucet()
.withdraw_from_account(
account_address,
resources.bitcoin,
dec!(100_000),
)
.take_all_from_worktop(resources.bitcoin, "bitcoin")
.with_bucket("bitcoin", |builder, bucket| {
builder.call_method(
protocol.ignition,
"open_liquidity_position",
(
bucket,
defiplaza_v2.pools.bitcoin,
LockupPeriod::from_months(6).unwrap(),
),
)
})
.try_deposit_entire_worktop_or_abort(account_address, None)
.build(),
&private_key,
);

// Assert
receipt.expect_commit_success();
let TransactionFeeSummary {
total_execution_cost_in_xrd,
..
} = receipt.fee_summary;
println!(
"Execution cost to open a position: {} XRD",
total_execution_cost_in_xrd
);

assert!(total_execution_cost_in_xrd <= dec!(4.8))
}

#[test]
fn can_close_a_liquidity_position_in_defiplaza_that_fits_into_fee_limits() {
// Arrange
let ScryptoUnitEnv {
environment: mut test_runner,
resources,
protocol,
defiplaza_v2,
..
} = ScryptoUnitEnv::new_with_configuration(Configuration {
maximum_allowed_relative_price_difference: dec!(0.03),
..Default::default()
});
let (_, private_key, account_address, _) = protocol.protocol_owner_badge;

test_runner
.execute_manifest(
ManifestBuilder::new()
.lock_fee_from_faucet()
.mint_fungible(resources.bitcoin, dec!(100_000_000_000_000))
.try_deposit_entire_worktop_or_abort(account_address, None)
.build(),
vec![],
)
.expect_commit_success();

for _ in 0..2 {
test_runner
.execute_manifest_with_enabled_modules(
ManifestBuilder::new()
.lock_fee_from_faucet()
.withdraw_from_account(
account_address,
resources.bitcoin,
dec!(100_000),
)
.take_all_from_worktop(resources.bitcoin, "bitcoin")
.with_bucket("bitcoin", |builder, bucket| {
builder.call_method(
protocol.ignition,
"open_liquidity_position",
(
bucket,
defiplaza_v2.pools.bitcoin,
LockupPeriod::from_months(6).unwrap(),
),
)
})
.try_deposit_entire_worktop_or_abort(account_address, None)
.build(),
EnabledModules::for_test_transaction()
& !EnabledModules::AUTH
& !EnabledModules::COSTING,
)
.expect_commit_success();
}

let current_time = test_runner.get_current_time(TimePrecisionV2::Minute);
let maturity_instant = current_time
.add_seconds(*LockupPeriod::from_months(6).unwrap().seconds() as i64)
.unwrap();
{
let db = test_runner.substate_db_mut();
let mut writer = SystemDatabaseWriter::new(db);

writer
.write_typed_object_field(
CONSENSUS_MANAGER.as_node_id(),
ModuleId::Main,
ConsensusManagerField::ProposerMilliTimestamp.field_index(),
ConsensusManagerProposerMilliTimestampFieldPayload::from_content_source(
ProposerMilliTimestampSubstate { epoch_milli: maturity_instant.seconds_since_unix_epoch * 1000 }
),
)
.unwrap();

writer
.write_typed_object_field(
CONSENSUS_MANAGER.as_node_id(),
ModuleId::Main,
ConsensusManagerField::ProposerMinuteTimestamp.field_index(),
ConsensusManagerProposerMinuteTimestampFieldPayload::from_content_source(
ProposerMinuteTimestampSubstate {
epoch_minute: i32::try_from(maturity_instant.seconds_since_unix_epoch / 60).unwrap(),
}
),
)
.unwrap();
}

test_runner
.execute_manifest_without_auth(
ManifestBuilder::new()
.lock_fee_from_faucet()
.call_method(
protocol.oracle,
"set_price",
(resources.bitcoin, XRD, dec!(1)),
)
.build(),
)
.expect_commit_success();

// Act
let receipt = test_runner.construct_and_execute_notarized_transaction(
ManifestBuilder::new()
.lock_fee_from_faucet()
.withdraw_from_account(
account_address,
defiplaza_v2.liquidity_receipt,
dec!(1),
)
.take_all_from_worktop(defiplaza_v2.liquidity_receipt, "receipt")
.with_bucket("receipt", |builder, bucket| {
builder.call_method(
protocol.ignition,
"close_liquidity_position",
(bucket,),
)
})
.try_deposit_entire_worktop_or_abort(account_address, None)
.build(),
&private_key,
);

// Assert
receipt.expect_commit_success();
let TransactionFeeSummary {
total_execution_cost_in_xrd,
..
} = receipt.fee_summary;
println!(
"Execution cost to close a position: {} XRD",
total_execution_cost_in_xrd
);

assert!(total_execution_cost_in_xrd <= dec!(4.8))
}

0 comments on commit 48cd4f4

Please sign in to comment.