Skip to content

Commit

Permalink
Add tests for pool contribution
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOmarA committed Jan 17, 2025
1 parent d8747bf commit 6892189
Show file tree
Hide file tree
Showing 7 changed files with 407 additions and 64 deletions.
73 changes: 16 additions & 57 deletions crates/radix-engine-toolkit/tests/classification/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,67 +207,26 @@ fn pool_contributions_and_redemptions_are_permitted_in_general_transaction() {
let mut ledger =
LedgerSimulatorBuilder::new().without_kernel_trace().build();
let (_, _, account) = ledger.new_account(true);
let resource_address1 = ledger.create_freely_mintable_fungible_resource(
Default::default(),
Some(dec!(1000)),
18,
account,
);
let resource_address2 = ledger.create_freely_mintable_fungible_resource(
Default::default(),
Some(dec!(1000)),
18,
account,
);

let manifest = ManifestBuilder::new()
.lock_fee_from_faucet()
.call_function(
POOL_PACKAGE,
ONE_RESOURCE_POOL_BLUEPRINT,
ONE_RESOURCE_POOL_INSTANTIATE_IDENT,
OneResourcePoolInstantiateManifestInput {
owner_role: Default::default(),
pool_manager_rule: rule!(allow_all),
resource_address: resource_address1.into(),
address_reservation: None,
let CreatedPoolEntities {
resource_address1,
resource_address2,
one_resource_pool:
PoolInformation {
component_address: one_pool,
pool_unit_resource_address: one_pool_unit,
},
)
.call_function(
POOL_PACKAGE,
TWO_RESOURCE_POOL_BLUEPRINT,
TWO_RESOURCE_POOL_INSTANTIATE_IDENT,
TwoResourcePoolInstantiateManifestInput {
owner_role: Default::default(),
pool_manager_rule: rule!(allow_all),
resource_addresses: (
resource_address1.into(),
resource_address2.into(),
),
address_reservation: None,
two_resource_pool:
PoolInformation {
component_address: two_pool,
pool_unit_resource_address: two_pool_unit,
},
)
.call_function(
POOL_PACKAGE,
MULTI_RESOURCE_POOL_BLUEPRINT,
MULTI_RESOURCE_POOL_INSTANTIATE_IDENT,
MultiResourcePoolInstantiateManifestInput {
owner_role: Default::default(),
pool_manager_rule: rule!(allow_all),
resource_addresses: indexset![
resource_address1.into(),
resource_address2.into()
],
address_reservation: None,
multi_resource_pool:
PoolInformation {
component_address: multi_pool,
pool_unit_resource_address: multi_pool_unit,
},
)
.build();
let receipt = ledger.execute_manifest(manifest, vec![]);
let commit_result = receipt.expect_commit_success();
let [one_pool, two_pool, multi_pool] =
[0, 1, 2].map(|i| commit_result.new_component_addresses()[i]);
let [one_pool_unit, two_pool_unit, multi_pool_unit] =
[0, 1, 2].map(|i| commit_result.new_resource_addresses()[i]);
} = ledger.create_pool_entities(account);

let manifest = ManifestBuilder::new()
.with_name_lookup(|builder, lookup| {
Expand Down
1 change: 1 addition & 0 deletions crates/radix-engine-toolkit/tests/classification/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

mod general;
mod general_subintent;
mod pool_contribution;
mod transfer;
mod validator_claim;
mod validator_stake;
Expand Down
284 changes: 284 additions & 0 deletions crates/radix-engine-toolkit/tests/classification/pool_contribution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
use crate::prelude::*;

#[test]
fn multiple_pool_contributions_classifies_as_pool_contribution_transaction() {
// Arrange
let mut ledger =
LedgerSimulatorBuilder::new().without_kernel_trace().build();
let (_, _, account) = ledger.new_account(true);

let CreatedPoolEntities {
resource_address1,
resource_address2,
one_resource_pool:
PoolInformation {
component_address: one_resource_pool,
pool_unit_resource_address: one_resource_pool_unit,
},
two_resource_pool:
PoolInformation {
component_address: two_resource_pool,
pool_unit_resource_address: two_resource_pool_unit,
},
multi_resource_pool:
PoolInformation {
component_address: multi_resource_pool,
pool_unit_resource_address: multi_resource_pool_unit,
},
} = ledger.create_pool_entities(account);

let manifest = ManifestBuilder::new()
.with_name_lookup(|builder, lookup| {
builder
.withdraw_from_account(account, resource_address1, 3)
.withdraw_from_account(account, resource_address2, 2)
.take_from_worktop(
resource_address1,
1,
"resource_address1_bucket1",
)
.take_from_worktop(
resource_address1,
1,
"resource_address1_bucket2",
)
.take_from_worktop(
resource_address1,
1,
"resource_address1_bucket3",
)
.take_from_worktop(
resource_address2,
1,
"resource_address2_bucket1",
)
.take_from_worktop(
resource_address2,
1,
"resource_address2_bucket2",
)
.call_method(
one_resource_pool,
ONE_RESOURCE_POOL_CONTRIBUTE_IDENT,
OneResourcePoolContributeManifestInput {
bucket: lookup.bucket("resource_address1_bucket1"),
},
)
.call_method(
two_resource_pool,
TWO_RESOURCE_POOL_CONTRIBUTE_IDENT,
TwoResourcePoolContributeManifestInput {
buckets: (
lookup.bucket("resource_address1_bucket2"),
lookup.bucket("resource_address2_bucket1"),
),
},
)
.call_method(
multi_resource_pool,
MULTI_RESOURCE_POOL_CONTRIBUTE_IDENT,
MultiResourcePoolContributeManifestInput {
buckets: ManifestBucketBatch::ManifestBuckets(vec![
lookup.bucket("resource_address1_bucket3"),
lookup.bucket("resource_address2_bucket2"),
]),
},
)
.try_deposit_entire_worktop_or_abort(account, None)
})
.build();

// Act
let (
StaticAnalysis {
manifest_classification,
..
},
DynamicAnalysis {
detailed_manifest_classification,
..
},
) = ledger.analyze(manifest);

// Assert
assert!(manifest_classification
.contains(&ManifestClassification::PoolContribution));
let Some(DetailedManifestClassification::PoolContribution(
PoolContributionOutput {
contribution_operations,
},
)) = detailed_manifest_classification.last()
else {
panic!("Not a pool contribution transaction")
};
assert_eq!(contribution_operations.len(), 3);

let one_resource_pool_contribution =
contribution_operations.first().unwrap();
let two_resource_pool_contribution =
contribution_operations.get(1).unwrap();
let multi_resource_pool_contribution =
contribution_operations.get(2).unwrap();

assert_eq!(
one_resource_pool_contribution.pool_address,
one_resource_pool
);
assert_eq!(
one_resource_pool_contribution.contributed_resources,
indexmap! {
resource_address1 => dec!(1)
}
);
assert_eq!(
one_resource_pool_contribution.pool_units_resource_address,
one_resource_pool_unit
);
assert_eq!(one_resource_pool_contribution.pool_units_amount, dec!(1));

assert_eq!(
two_resource_pool_contribution.pool_address,
two_resource_pool
);
assert_eq!(
two_resource_pool_contribution.contributed_resources,
indexmap! {
resource_address1 => dec!(1),
resource_address2 => dec!(1),
}
);
assert_eq!(
two_resource_pool_contribution.pool_units_resource_address,
two_resource_pool_unit
);
assert_eq!(two_resource_pool_contribution.pool_units_amount, dec!(1));

assert_eq!(
multi_resource_pool_contribution.pool_address,
multi_resource_pool
);
assert_eq!(
multi_resource_pool_contribution.contributed_resources,
indexmap! {
resource_address1 => dec!(1),
resource_address2 => dec!(1),
}
);
assert_eq!(
multi_resource_pool_contribution.pool_units_resource_address,
multi_resource_pool_unit
);
assert_eq!(multi_resource_pool_contribution.pool_units_amount, dec!(1));
}

#[test]
fn transfer_in_pool_contribution_transaction_qualifies_for_classification_but_not_detailed_classification(
) {
// Arrange
let mut ledger =
LedgerSimulatorBuilder::new().without_kernel_trace().build();
let (_, _, account) = ledger.new_account(true);

let CreatedPoolEntities {
resource_address1,
resource_address2,
one_resource_pool:
PoolInformation {
component_address: one_resource_pool,
pool_unit_resource_address: _,
},
two_resource_pool:
PoolInformation {
component_address: two_resource_pool,
pool_unit_resource_address: _,
},
multi_resource_pool:
PoolInformation {
component_address: multi_resource_pool,
pool_unit_resource_address: _,
},
} = ledger.create_pool_entities(account);

let manifest = ManifestBuilder::new()
.with_name_lookup(|builder, lookup| {
builder
.withdraw_from_account(account, resource_address1, 3)
.withdraw_from_account(account, resource_address2, 3)
.take_from_worktop(
resource_address1,
1,
"resource_address1_bucket1",
)
.take_from_worktop(
resource_address1,
1,
"resource_address1_bucket2",
)
.take_from_worktop(
resource_address1,
1,
"resource_address1_bucket3",
)
.take_from_worktop(
resource_address2,
1,
"resource_address2_bucket1",
)
.take_from_worktop(
resource_address2,
1,
"resource_address2_bucket2",
)
.call_method(
one_resource_pool,
ONE_RESOURCE_POOL_CONTRIBUTE_IDENT,
OneResourcePoolContributeManifestInput {
bucket: lookup.bucket("resource_address1_bucket1"),
},
)
.call_method(
two_resource_pool,
TWO_RESOURCE_POOL_CONTRIBUTE_IDENT,
TwoResourcePoolContributeManifestInput {
buckets: (
lookup.bucket("resource_address1_bucket2"),
lookup.bucket("resource_address2_bucket1"),
),
},
)
.call_method(
multi_resource_pool,
MULTI_RESOURCE_POOL_CONTRIBUTE_IDENT,
MultiResourcePoolContributeManifestInput {
buckets: ManifestBucketBatch::ManifestBuckets(vec![
lookup.bucket("resource_address1_bucket3"),
lookup.bucket("resource_address2_bucket2"),
]),
},
)
.try_deposit_entire_worktop_or_abort(account, None)
})
.build();

// Act
let (
StaticAnalysis {
manifest_classification,
..
},
DynamicAnalysis {
detailed_manifest_classification,
..
},
) = ledger.analyze(manifest);

// Assert
assert!(manifest_classification
.contains(&ManifestClassification::PoolContribution));
assert!(!detailed_manifest_classification.iter().any(
|classification| matches!(
classification,
DetailedManifestClassification::PoolContribution(..)
)
));
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use crate::prelude::*;

#[test]
fn single_validator_claim_is_classified_as_validator_claim_transaction() {
fn single_validator_claim_classifies_as_validator_claim_transaction() {
// Arrange
let mut ledger =
LedgerSimulatorBuilder::new().without_kernel_trace().build();
Expand Down Expand Up @@ -88,7 +88,7 @@ fn single_validator_claim_is_classified_as_validator_claim_transaction() {
}

#[test]
fn single_validator_claim_with_withdraw_of_non_fungibles_is_classified_as_validator_claim_transaction(
fn single_validator_claim_with_withdraw_of_non_fungibles_classifies_as_validator_claim_transaction(
) {
// Arrange
let mut ledger =
Expand Down
Loading

0 comments on commit 6892189

Please sign in to comment.