-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Tests]: Initial tests for DefiPlaza.
- Loading branch information
Showing
3 changed files
with
282 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |