1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use super::HasTypeInfo;
use crate::prelude::{Global, HasStub, ObjectStub, ObjectStubHandle};
use radix_engine_common::prelude::PACKAGE_PACKAGE;
use radix_engine_interface::blueprints::package::{
    PackageClaimRoyaltiesInput, PACKAGE_BLUEPRINT, PACKAGE_CLAIM_ROYALTIES_IDENT,
};
use radix_engine_interface::blueprints::resource::Bucket;
use radix_engine_interface::types::*;
use radix_engine_interface::*;
use sbor::rust::prelude::*;

pub type Package = Global<PackageStub>;

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct PackageStub(pub ObjectStubHandle);

impl HasStub for PackageStub {
    type Stub = Self;
}

impl HasTypeInfo for PackageStub {
    const PACKAGE_ADDRESS: Option<PackageAddress> = Some(PACKAGE_PACKAGE);

    const BLUEPRINT_NAME: &'static str = PACKAGE_BLUEPRINT;

    const OWNED_TYPE_NAME: &'static str = "OwnedPackage";

    const GLOBAL_TYPE_NAME: &'static str = "GlobalPackage";
}

impl ObjectStub for PackageStub {
    type AddressType = PackageAddress;

    fn new(handle: ObjectStubHandle) -> Self {
        Self(handle)
    }

    fn handle(&self) -> &ObjectStubHandle {
        &self.0
    }
}

impl PackageStub {
    pub fn claim_royalties(&self) -> Bucket {
        self.call(
            PACKAGE_CLAIM_ROYALTIES_IDENT,
            &PackageClaimRoyaltiesInput {},
        )
    }
}

impl From<PackageAddress> for Package {
    fn from(value: PackageAddress) -> Self {
        Global(ObjectStub::new(ObjectStubHandle::Global(value.into())))
    }
}