radix_native_sdk/resource/
worktop.rs

1use radix_common::constants::RESOURCE_PACKAGE;
2use radix_common::data::scrypto::model::*;
3use radix_common::data::scrypto::{scrypto_decode, scrypto_encode};
4use radix_common::math::Decimal;
5use radix_common::prelude::ManifestResourceConstraints;
6use radix_engine_interface::api::*;
7use radix_engine_interface::blueprints::resource::*;
8use radix_engine_interface::types::*;
9use sbor::rust::collections::IndexSet;
10use sbor::rust::fmt::Debug;
11use sbor::rust::vec::Vec;
12
13#[derive(Debug, Copy, Clone, PartialEq, Eq)]
14pub struct Worktop(pub Own);
15
16impl Worktop {
17    pub fn drop<Y: SystemApi<E>, E: SystemApiError>(self, api: &mut Y) -> Result<(), E> {
18        let _rtn = api.call_function(
19            RESOURCE_PACKAGE,
20            WORKTOP_BLUEPRINT,
21            WORKTOP_DROP_IDENT,
22            scrypto_encode(&WorktopDropInput {
23                worktop: OwnedWorktop(self.0),
24            })
25            .unwrap(),
26        )?;
27
28        Ok(())
29    }
30
31    pub fn put<Y: SystemApi<E>, E: SystemApiError>(
32        &self,
33        bucket: Bucket,
34        api: &mut Y,
35    ) -> Result<(), E> {
36        let _rtn = api.call_method(
37            self.0.as_node_id(),
38            WORKTOP_PUT_IDENT,
39            scrypto_encode(&WorktopPutInput { bucket }).unwrap(),
40        )?;
41
42        Ok(())
43    }
44
45    pub fn take<Y: SystemApi<E>, E: SystemApiError>(
46        &self,
47        resource_address: ResourceAddress,
48        amount: Decimal,
49        api: &mut Y,
50    ) -> Result<Bucket, E> {
51        let rtn = api.call_method(
52            self.0.as_node_id(),
53            WORKTOP_TAKE_IDENT,
54            scrypto_encode(&WorktopTakeInput {
55                resource_address,
56                amount,
57            })
58            .unwrap(),
59        )?;
60
61        Ok(scrypto_decode(&rtn).unwrap())
62    }
63
64    pub fn take_non_fungibles<Y: SystemApi<E>, E: SystemApiError>(
65        &self,
66        resource_address: ResourceAddress,
67        ids: IndexSet<NonFungibleLocalId>,
68        api: &mut Y,
69    ) -> Result<Bucket, E> {
70        let rtn = api.call_method(
71            self.0.as_node_id(),
72            WORKTOP_TAKE_NON_FUNGIBLES_IDENT,
73            scrypto_encode(&WorktopTakeNonFungiblesInput {
74                resource_address,
75                ids,
76            })
77            .unwrap(),
78        )?;
79
80        Ok(scrypto_decode(&rtn).unwrap())
81    }
82
83    pub fn take_all<Y: SystemApi<E>, E: SystemApiError>(
84        &self,
85        resource_address: ResourceAddress,
86        api: &mut Y,
87    ) -> Result<Bucket, E> {
88        let rtn = api.call_method(
89            self.0.as_node_id(),
90            WORKTOP_TAKE_ALL_IDENT,
91            scrypto_encode(&WorktopTakeAllInput { resource_address }).unwrap(),
92        )?;
93        Ok(scrypto_decode(&rtn).unwrap())
94    }
95
96    pub fn assert_contains<Y: SystemApi<E>, E: SystemApiError>(
97        &self,
98        resource_address: ResourceAddress,
99        api: &mut Y,
100    ) -> Result<(), E> {
101        let _rtn = api.call_method(
102            self.0.as_node_id(),
103            WORKTOP_ASSERT_CONTAINS_IDENT,
104            scrypto_encode(&WorktopAssertContainsInput { resource_address }).unwrap(),
105        )?;
106        Ok(())
107    }
108
109    pub fn assert_contains_amount<Y: SystemApi<E>, E: SystemApiError>(
110        &self,
111        resource_address: ResourceAddress,
112        amount: Decimal,
113        api: &mut Y,
114    ) -> Result<(), E> {
115        let _rtn = api.call_method(
116            self.0.as_node_id(),
117            WORKTOP_ASSERT_CONTAINS_AMOUNT_IDENT,
118            scrypto_encode(&WorktopAssertContainsAmountInput {
119                resource_address,
120                amount,
121            })
122            .unwrap(),
123        )?;
124        Ok(())
125    }
126
127    pub fn assert_contains_non_fungibles<Y: SystemApi<E>, E: SystemApiError>(
128        &self,
129        resource_address: ResourceAddress,
130        ids: IndexSet<NonFungibleLocalId>,
131        api: &mut Y,
132    ) -> Result<(), E> {
133        let _rtn = api.call_method(
134            self.0.as_node_id(),
135            WORKTOP_ASSERT_CONTAINS_NON_FUNGIBLES_IDENT,
136            scrypto_encode(&WorktopAssertContainsNonFungiblesInput {
137                resource_address,
138                ids,
139            })
140            .unwrap(),
141        )?;
142        Ok(())
143    }
144
145    pub fn assert_resources_include<Y: SystemApi<E>, E: SystemApiError>(
146        &self,
147        constraints: ManifestResourceConstraints,
148        api: &mut Y,
149    ) -> Result<(), E> {
150        let _rtn = api.call_method(
151            self.0.as_node_id(),
152            WORKTOP_ASSERT_RESOURCES_INCLUDE_IDENT,
153            scrypto_encode(&WorktopAssertResourcesIncludeInput { constraints }).unwrap(),
154        )?;
155        Ok(())
156    }
157
158    pub fn assert_resources_only<Y: SystemApi<E>, E: SystemApiError>(
159        &self,
160        constraints: ManifestResourceConstraints,
161        api: &mut Y,
162    ) -> Result<(), E> {
163        let _rtn = api.call_method(
164            self.0.as_node_id(),
165            WORKTOP_ASSERT_RESOURCES_ONLY_IDENT,
166            scrypto_encode(&WorktopAssertResourcesOnlyInput { constraints }).unwrap(),
167        )?;
168        Ok(())
169    }
170
171    pub fn drain<Y: SystemApi<E>, E: SystemApiError>(&self, api: &mut Y) -> Result<Vec<Bucket>, E> {
172        let rtn = api.call_method(
173            self.0.as_node_id(),
174            WORKTOP_DRAIN_IDENT,
175            scrypto_encode(&WorktopDrainInput {}).unwrap(),
176        )?;
177        Ok(scrypto_decode(&rtn).unwrap())
178    }
179}