Skip to content

Commit

Permalink
[Testing]: Additional tests for closing liquidity position.
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOmarA committed Jan 21, 2024
1 parent 9339279 commit 11040c6
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 24 deletions.
6 changes: 3 additions & 3 deletions .vscode/rust.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"body": [
"#[test]",
"fn ${0:test_name}() -> Result<(), RuntimeError> {",
"\t//Arrange",
"\t// Arrange",
"\tlet Environment { environment: ref mut env, protocol, resources, ociswap, caviarnine } = Environment::new()?;",
"\t//Act",
"\t//Assert",
"\t// Act",
"\t// Assert",
"",
"\tOk(())",
"}"
Expand Down
24 changes: 12 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ edition = "2021"
description = "The implementation of project Ignition in Scrypto for the Radix Ledger"

[workspace.dependencies]
sbor = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ea46347402a5553d4f0a594ff003e5f455d2b5d1" }
scrypto = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ea46347402a5553d4f0a594ff003e5f455d2b5d1" }
transaction = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ea46347402a5553d4f0a594ff003e5f455d2b5d1" }
radix-engine = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ea46347402a5553d4f0a594ff003e5f455d2b5d1" }
radix-engine-common = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ea46347402a5553d4f0a594ff003e5f455d2b5d1" }
radix-engine-stores = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ea46347402a5553d4f0a594ff003e5f455d2b5d1" }
radix-engine-derive = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ea46347402a5553d4f0a594ff003e5f455d2b5d1" }
radix-engine-queries = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ea46347402a5553d4f0a594ff003e5f455d2b5d1" }
radix-engine-interface = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ea46347402a5553d4f0a594ff003e5f455d2b5d1" }
radix-engine-store-interface = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ea46347402a5553d4f0a594ff003e5f455d2b5d1" }
sbor = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ef169b1e1348b8dbad977ba81d086ee1e80d6ff8" }
scrypto = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ef169b1e1348b8dbad977ba81d086ee1e80d6ff8" }
transaction = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ef169b1e1348b8dbad977ba81d086ee1e80d6ff8" }
radix-engine = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ef169b1e1348b8dbad977ba81d086ee1e80d6ff8" }
radix-engine-common = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ef169b1e1348b8dbad977ba81d086ee1e80d6ff8" }
radix-engine-stores = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ef169b1e1348b8dbad977ba81d086ee1e80d6ff8" }
radix-engine-derive = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ef169b1e1348b8dbad977ba81d086ee1e80d6ff8" }
radix-engine-queries = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ef169b1e1348b8dbad977ba81d086ee1e80d6ff8" }
radix-engine-interface = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ef169b1e1348b8dbad977ba81d086ee1e80d6ff8" }
radix-engine-store-interface = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ef169b1e1348b8dbad977ba81d086ee1e80d6ff8" }

scrypto-unit = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ea46347402a5553d4f0a594ff003e5f455d2b5d1" }
scrypto-test = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ea46347402a5553d4f0a594ff003e5f455d2b5d1" }
scrypto-unit = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ef169b1e1348b8dbad977ba81d086ee1e80d6ff8" }
scrypto-test = { git = "https://github.com/radixdlt/radixdlt-scrypto", rev = "ef169b1e1348b8dbad977ba81d086ee1e80d6ff8" }

[profile.release]
opt-level = 'z'
Expand Down
4 changes: 4 additions & 0 deletions libraries/package-loader/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ fn build_blueprints() -> Result<(), Error> {
"--release",
"--target-dir",
builds_target_path.as_path().display().to_string().as_str(),
"--features",
"scrypto/log-trace",
])
.args(package_names.iter().flat_map(|package_name| {
["--package".to_owned(), package_name.to_owned()]
Expand Down Expand Up @@ -107,6 +109,8 @@ fn build_blueprints() -> Result<(), Error> {
builds_target_path.as_path().display().to_string().as_str(),
"--features",
"scrypto/no-schema",
"--features",
"scrypto/log-trace",
])
.args(package_names.iter().flat_map(|package_name| {
["--package".to_owned(), package_name.to_owned()]
Expand Down
100 changes: 91 additions & 9 deletions tests/tests/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ fn liquidity_receipt_data_matches_component_state() -> Result<(), RuntimeError>
#[test]
fn cant_close_a_liquidity_position_using_a_fake_nft() -> Result<(), RuntimeError>
{
//Arrange
// Arrange
let Environment {
environment: ref mut env,
mut protocol,
Expand Down Expand Up @@ -890,13 +890,13 @@ fn cant_close_a_liquidity_position_using_a_fake_nft() -> Result<(), RuntimeError
env,
)?;

//Act
// Act
let rtn = protocol.ignition.close_liquidity_position(
NonFungibleBucket(fake_liquidity_receipt),
env,
);

//Assert
// Assert
assert_is_ignition_not_a_valid_liquidity_receipt_error(&rtn);

Ok(())
Expand All @@ -905,7 +905,7 @@ fn cant_close_a_liquidity_position_using_a_fake_nft() -> Result<(), RuntimeError
#[test]
fn cant_close_a_liquidity_position_when_closing_is_closed(
) -> Result<(), RuntimeError> {
//Arrange
// Arrange
let Environment {
environment: ref mut env,
mut protocol,
Expand All @@ -927,12 +927,12 @@ fn cant_close_a_liquidity_position_when_closing_is_closed(
env,
)?;

//Act
// Act
let rtn = protocol
.ignition
.close_liquidity_position(NonFungibleBucket(bucket), env);

//Assert
// Assert
assert_is_ignition_closing_liquidity_positions_is_closed_error(&rtn);

Ok(())
Expand All @@ -941,7 +941,7 @@ fn cant_close_a_liquidity_position_when_closing_is_closed(
#[test]
fn cant_close_a_liquidity_position_with_more_than_one_nft(
) -> Result<(), RuntimeError> {
//Arrange
// Arrange
let Environment {
environment: ref mut env,
mut protocol,
Expand Down Expand Up @@ -970,17 +970,99 @@ fn cant_close_a_liquidity_position_with_more_than_one_nft(
)?;
bucket1.put(bucket2, env)?;

//Act
// Act
let rtn = protocol
.ignition
.close_liquidity_position(NonFungibleBucket(bucket1), env);

//Assert
// Assert
assert_is_ignition_more_than_one_liquidity_receipt_nfts_error(&rtn);

Ok(())
}

#[test]
fn cant_close_a_liquidity_position_before_its_maturity_date(
) -> Result<(), RuntimeError> {
// Arrange
let Environment {
environment: ref mut env,
mut protocol,
ociswap,
resources,
..
} = Environment::new()?;

let (bucket, _) = ResourceManager(ociswap.liquidity_receipt)
.mint_non_fungible_single_ruid(
utils::liquidity_receipt_data_with_modifier(|receipt| {
receipt.pool_address =
ociswap.pools.bitcoin.try_into().unwrap();
receipt.user_resource_address = resources.bitcoin;
env.set_current_time(Instant::new(60));
receipt.maturity_date = Instant::new(120);
}),
env,
)?;

// Act
let rtn = protocol
.ignition
.close_liquidity_position(NonFungibleBucket(bucket), env);

// Assert
assert_is_ignition_liquidity_position_has_not_matured_error(&rtn);

Ok(())
}

#[test]
fn can_close_a_liquidity_position_the_minute_it_matures(
) -> Result<(), RuntimeError> {
// Arrange
let Environment {
environment: ref mut env,
mut protocol,
ociswap,
resources,
..
} = Environment::new()?;

let bitcoin_bucket =
ResourceManager(resources.bitcoin).mint_fungible(dec!(100), env)?;
let (liquidity_receipt, _, _) = protocol.ignition.open_liquidity_position(
FungibleBucket(bitcoin_bucket),
ociswap.pools.bitcoin.try_into().unwrap(),
LockupPeriod::from_months(6),
env,
)?;

let liquidity_receipt_data = ResourceManager(ociswap.liquidity_receipt)
.get_non_fungible_data::<_, _, LiquidityReceipt>(
liquidity_receipt
.0
.non_fungible_local_ids(env)?
.first()
.unwrap()
.clone(),
env,
)?;
env.set_current_time(liquidity_receipt_data.maturity_date);
protocol
.oracle
.set_price(resources.bitcoin, XRD, dec!(1), env)?;

// Act
let rtn = protocol
.ignition
.close_liquidity_position(liquidity_receipt, env);

// Assert
assert!(rtn.is_ok(), "{rtn:#?}");

Ok(())
}

mod utils {
use super::*;

Expand Down

0 comments on commit 11040c6

Please sign in to comment.