radix_engine_interface/blueprints/
utils.rs

1use crate::blueprints::access_controller::*;
2use crate::blueprints::account::*;
3use crate::blueprints::identity::*;
4use crate::blueprints::package::*;
5use crate::blueprints::pool::*;
6use crate::internal_prelude::*;
7use lazy_static::lazy_static;
8
9/// This function resolves the [`BlueprintId`] based on the entity type for native blueprints.
10///
11/// If the entity type is [`EntityType::GlobalGenericComponent`] then this function returns [`None`]
12/// since it can't tell what the [`BlueprintId`] of a generic component is. Otherwise, in the case
13/// where the [`EntityType`] belongs to a native blueprint this function will translate it into the
14/// appropriate [`BlueprintId`].
15pub fn resolve_main_module_blueprint_id(entity_type: EntityType) -> Option<&'static BlueprintId> {
16    match entity_type {
17        EntityType::GlobalPackage => Some(&PACKAGE_BLUEPRINT_ID),
18        EntityType::GlobalConsensusManager => Some(&CONSENSUS_MANAGER_BLUEPRINT_ID),
19        EntityType::GlobalValidator => Some(&VALIDATOR_BLUEPRINT_ID),
20        EntityType::GlobalTransactionTracker => Some(&TRANSACTION_TRACKER_BLUEPRINT_ID),
21        EntityType::GlobalAccessController => Some(&ACCESS_CONTROLLER_BLUEPRINT_ID),
22        EntityType::GlobalOneResourcePool => Some(&ONE_RESOURCE_POOL_BLUEPRINT_ID),
23        EntityType::GlobalTwoResourcePool => Some(&TWO_RESOURCE_POOL_BLUEPRINT_ID),
24        EntityType::GlobalMultiResourcePool => Some(&MULTI_RESOURCE_POOL_BLUEPRINT_ID),
25        EntityType::GlobalAccountLocker => Some(&ACCOUNT_LOCKER_BLUEPRINT_ID),
26        EntityType::GlobalFungibleResourceManager => Some(&FUNGIBLE_RESOURCE_MANAGER_BLUEPRINT_ID),
27        EntityType::GlobalNonFungibleResourceManager => {
28            Some(&NON_FUNGIBLE_RESOURCE_MANAGER_BLUEPRINT_ID)
29        }
30        EntityType::InternalFungibleVault => Some(&FUNGIBLE_VAULT_BLUEPRINT_ID),
31        EntityType::InternalNonFungibleVault => Some(&NON_FUNGIBLE_VAULT_BLUEPRINT_ID),
32        EntityType::GlobalAccount
33        | EntityType::GlobalPreallocatedSecp256k1Account
34        | EntityType::GlobalPreallocatedEd25519Account => Some(&ACCOUNT_BLUEPRINT_ID),
35        EntityType::GlobalIdentity
36        | EntityType::GlobalPreallocatedSecp256k1Identity
37        | EntityType::GlobalPreallocatedEd25519Identity => Some(&IDENTITY_BLUEPRINT_ID),
38        EntityType::InternalGenericComponent
39        | EntityType::InternalKeyValueStore
40        | EntityType::GlobalGenericComponent => None,
41    }
42}
43
44/// This function resolves the [`BlueprintId`] of the invoked blueprint given the [`BlueprintId`] of
45/// the main module and the [`ModuleId`] being invoked.
46pub fn resolve_invoked_blueprint_id(
47    main_module_blueprint_id: &BlueprintId,
48    module_id: ModuleId,
49) -> Option<&BlueprintId> {
50    match module_id {
51        ModuleId::Main => Some(main_module_blueprint_id),
52        // TODO: We could improve this to take into account if the blueprint has these modules or
53        // not. For the time being this doesn't seem to be needed.
54        ModuleId::Metadata => Some(&METADATA_BLUEPRINT_ID),
55        ModuleId::Royalty => Some(&ROYALTY_BLUEPRINT_ID),
56        ModuleId::RoleAssignment => Some(&ROLE_ASSIGNMENT_BLUEPRINT_ID),
57    }
58}
59
60macro_rules! define_static_blueprint_id {
61    (
62        $(
63            $name: ident => ($package_address: expr, $blueprint_name: expr)
64        ),* $(,)?
65    ) => {
66        paste::paste! {
67            lazy_static! {
68                $(
69                    static ref [< $name:upper _BLUEPRINT_ID >]: BlueprintId = BlueprintId {
70                        package_address: $package_address,
71                        blueprint_name: $blueprint_name.to_owned(),
72                    };
73                )*
74            }
75        }
76    };
77}
78
79define_static_blueprint_id! {
80    package => (PACKAGE_PACKAGE, PACKAGE_BLUEPRINT),
81    consensus_manager => (CONSENSUS_MANAGER_PACKAGE, CONSENSUS_MANAGER_BLUEPRINT),
82    validator => (CONSENSUS_MANAGER_PACKAGE, VALIDATOR_BLUEPRINT),
83    transaction_tracker => (TRANSACTION_TRACKER_PACKAGE, TRANSACTION_TRACKER_BLUEPRINT),
84    account => (ACCOUNT_PACKAGE, ACCOUNT_BLUEPRINT),
85    identity => (IDENTITY_PACKAGE, IDENTITY_BLUEPRINT),
86    access_controller => (ACCESS_CONTROLLER_PACKAGE, ACCESS_CONTROLLER_BLUEPRINT),
87    one_resource_pool => (POOL_PACKAGE, ONE_RESOURCE_POOL_BLUEPRINT),
88    two_resource_pool => (POOL_PACKAGE, TWO_RESOURCE_POOL_BLUEPRINT),
89    multi_resource_pool => (POOL_PACKAGE, MULTI_RESOURCE_POOL_BLUEPRINT),
90    account_locker => (LOCKER_PACKAGE, ACCOUNT_LOCKER_BLUEPRINT),
91    fungible_resource_manager => (RESOURCE_PACKAGE, FUNGIBLE_RESOURCE_MANAGER_BLUEPRINT),
92    non_fungible_resource_manager => (RESOURCE_PACKAGE, NON_FUNGIBLE_RESOURCE_MANAGER_BLUEPRINT),
93    fungible_vault => (RESOURCE_PACKAGE, FUNGIBLE_VAULT_BLUEPRINT),
94    non_fungible_vault => (RESOURCE_PACKAGE, NON_FUNGIBLE_VAULT_BLUEPRINT),
95    metadata => (METADATA_MODULE_PACKAGE, METADATA_BLUEPRINT),
96    role_assignment => (ROLE_ASSIGNMENT_MODULE_PACKAGE, ROLE_ASSIGNMENT_BLUEPRINT),
97    royalty => (ROYALTY_MODULE_PACKAGE, COMPONENT_ROYALTY_BLUEPRINT),
98}