desmos_bindings/subspaces/
msg.rs

1//! Contains the messages that can be sent to the chain to interact with the x/subspaces module.
2
3use crate::cosmos_types::{Allowance, AuthzGrant};
4use crate::subspaces::types::Permission;
5use crate::subspaces::types::*;
6use cosmwasm_std::Addr;
7
8/// SubspacesMsg is the builder to generate Desmos x/subspaces messages.
9pub struct SubspacesMsg {}
10
11impl SubspacesMsg {
12    /// Creates a new instance of [`MsgCreateSubspace`].
13    ///
14    /// * `name` - Subspace name.
15    /// * `description` - Subspace description.
16    /// * `owner` - Address of who will be the subspace owner.
17    /// * `creator` - Address of who wants to create the subspace.
18    pub fn create_subspace(
19        name: &str,
20        description: &str,
21        owner: Addr,
22        creator: Addr,
23    ) -> MsgCreateSubspace {
24        MsgCreateSubspace {
25            name: name.into(),
26            description: description.into(),
27            owner: owner.into(),
28            creator: creator.into(),
29        }
30    }
31
32    /// Creates a new instance of [`MsgEditSubspace`].
33    ///
34    /// * `subspace_id` - Id of the subspace to edit.
35    /// * `name` - New subspace name.
36    /// * `description` - New subspace description.
37    /// * `owner` - Address of who will be the subspace owner.
38    /// * `signer` - Address of who wants edit the subspace.
39    pub fn edit_subspace(
40        subspace_id: u64,
41        name: &str,
42        description: &str,
43        owner: Addr,
44        signer: Addr,
45    ) -> MsgEditSubspace {
46        MsgEditSubspace {
47            subspace_id,
48            name: name.into(),
49            description: description.into(),
50            owner: owner.into(),
51            signer: signer.into(),
52        }
53    }
54
55    /// Creates a new instance of [`MsgDeleteSubspace`].
56    ///
57    /// * `subspace_id` - id of the subspace to delete.
58    /// * `signer` - Address of who wants delete the subsapce.
59    pub fn delete_subspace(subspace_id: u64, signer: Addr) -> MsgDeleteSubspace {
60        MsgDeleteSubspace {
61            subspace_id,
62            signer: signer.into(),
63        }
64    }
65
66    /// Creates a new instance of [`MsgCreateSection`].
67    ///
68    /// * `subspace_id` - Id of the subspace inside which the section will be placed.
69    /// * `name` - Name of the section to be created.
70    /// * `description` - Description of the section.
71    /// * `parent_id` - Id of the parent section.
72    /// * `creator` - User creating the section.
73    pub fn create_section(
74        subspace_id: u64,
75        name: &str,
76        description: &str,
77        parent_id: u32,
78        creator: Addr,
79    ) -> MsgCreateSection {
80        MsgCreateSection {
81            subspace_id,
82            name: name.into(),
83            description: description.into(),
84            parent_id,
85            creator: creator.into(),
86        }
87    }
88
89    /// Creates a new instance of [`MsgEditSection`].
90    ///
91    /// `subspace_id` - Id of the subspace inside which the section to be edited is.
92    /// `section_id` - Id of the section to be edited.
93    /// `name` - New name of the section.
94    /// `description` - New description of the section.
95    /// `editor` - User editing the section.
96    pub fn edit_section(
97        subspace_id: u64,
98        section_id: u32,
99        name: Option<&str>,
100        description: Option<&str>,
101        editor: Addr,
102    ) -> MsgEditSection {
103        MsgEditSection {
104            subspace_id,
105            section_id,
106            name: name.unwrap_or("[do-not-modify]").into(),
107            description: description.unwrap_or("[do-not-modify]").into(),
108            editor: editor.into(),
109        }
110    }
111
112    /// Creates a new instance of [`MsgMoveSection`].
113    ///
114    /// `subspace_id` - Id of the subspace inside which the section lies.
115    /// `section_id` - Id of the section to be moved.
116    /// `new_parent_id` - Id of the new parent.
117    /// `signer` - User moving the section.
118    pub fn move_section(
119        subspace_id: u64,
120        section_id: u32,
121        new_parent_id: u32,
122        signer: Addr,
123    ) -> MsgMoveSection {
124        MsgMoveSection {
125            subspace_id,
126            section_id,
127            new_parent_id,
128            signer: signer.into(),
129        }
130    }
131
132    /// Creates a new instance of [`MsgDeleteSection`].
133    ///
134    /// `subspace_id` - Id of the subspace inside which the section to be deleted is.
135    /// `section_id` - Id of the section to delete.
136    /// `signer` - User deleting the section.
137    pub fn delete_section(subspace_id: u64, section_id: u32, signer: Addr) -> MsgDeleteSection {
138        MsgDeleteSection {
139            subspace_id,
140            section_id,
141            signer: signer.into(),
142        }
143    }
144
145    /// Creates a new instance of [`MsgCreateUserGroup`].
146    ///
147    /// * `subspace_id` - Subspace id to which the group will belong.
148    /// * `section_id` - Id of the section inside which the group will be created.
149    /// * `name` - Group name.
150    /// * `description` - Group description.
151    /// * `default_permission` - Permissions that the members will inherit.
152    /// * `creator` - Address of who wants to create the group.
153    pub fn create_user_group(
154        subspace_id: u64,
155        section_id: u32,
156        name: &str,
157        description: &str,
158        default_permissions: Vec<Permission>,
159        initial_members: Vec<Addr>,
160        creator: Addr,
161    ) -> MsgCreateUserGroup {
162        MsgCreateUserGroup {
163            subspace_id,
164            section_id,
165            name: name.into(),
166            description: description.into(),
167            default_permissions: default_permissions.into_iter().map(Into::into).collect(),
168            initial_members: initial_members.into_iter().map(Into::into).collect(),
169            creator: creator.into(),
170        }
171    }
172
173    /// Creates a new instance of [`MsgEditUserGroup`].
174    ///
175    /// * `subspace_id` - Subspace id to which the group belongs.
176    /// * `group_id` - Id of the group to edit.
177    /// * `name` - New group name.
178    /// * `description` - New group description.
179    /// * `signer` - Address of who wants edit the user group.
180    pub fn edit_user_group(
181        subspace_id: u64,
182        group_id: u32,
183        name: Option<&str>,
184        description: Option<&str>,
185        signer: Addr,
186    ) -> MsgEditUserGroup {
187        MsgEditUserGroup {
188            subspace_id,
189            group_id,
190            name: name.unwrap_or("[do-not-modify]").into(),
191            description: description.unwrap_or("[do-not-modify]").into(),
192            signer: signer.into(),
193        }
194    }
195
196    /// Creates a new instance of [`MsgMoveUserGroup`].
197    ///
198    /// * `subspace_id` - Id of the subspace inside which the group to move is.
199    /// * `group_id` - Id of the group to be moved.
200    /// * `new_section_id` - d of the new section where to move the group.
201    /// * `signer` - Address of who wants move the user group.
202    pub fn move_user_group(
203        subspace_id: u64,
204        group_id: u32,
205        new_section_id: u32,
206        signer: Addr,
207    ) -> MsgMoveUserGroup {
208        MsgMoveUserGroup {
209            subspace_id,
210            group_id,
211            new_section_id,
212            signer: signer.into(),
213        }
214    }
215
216    /// Creates a new instance of [`MsgSetUserGroupPermissions`].
217    ///
218    /// * `subspace_id` - Subspace to which the user group belongs.
219    /// * `group_id` - Id of user group of interest.
220    /// * `permissions` - The new permissions that will be set to the group.
221    /// * `signer` - Address of who wants set the group permissions.
222    pub fn set_user_group_permissions(
223        subspace_id: u64,
224        group_id: u32,
225        permissions: Vec<Permission>,
226        signer: Addr,
227    ) -> MsgSetUserGroupPermissions {
228        MsgSetUserGroupPermissions {
229            subspace_id,
230            group_id,
231            permissions: permissions.into_iter().map(Into::into).collect(),
232            signer: signer.into(),
233        }
234    }
235
236    /// Creates a new instance of [`MsgDeleteUserGroup`].
237    ///
238    /// * `subspace_id` - Id of the subspace to which the group belongs.
239    /// * `group_id` - Id of the group to delete.
240    /// * `signer` - Address of who wants to delete the group.
241    pub fn delete_user_group(subspace_id: u64, group_id: u32, signer: Addr) -> MsgDeleteUserGroup {
242        MsgDeleteUserGroup {
243            subspace_id,
244            group_id,
245            signer: signer.into(),
246        }
247    }
248
249    /// Creates a new instance of [`MsgAddUserToUserGroup`].
250    ///
251    /// * `subspace_id` - Subspace id to which the group belongs.
252    /// * `group_id` - Id of the group to which will be added the user.
253    /// * `user` - Address of the user to add to the group.
254    /// * `signer` - Address of who wants to add the user to the group.
255    pub fn add_user_to_user_group(
256        subspace_id: u64,
257        group_id: u32,
258        user: Addr,
259        signer: Addr,
260    ) -> MsgAddUserToUserGroup {
261        MsgAddUserToUserGroup {
262            subspace_id,
263            group_id,
264            user: user.into(),
265            signer: signer.into(),
266        }
267    }
268
269    /// Creates a new instance of [`MsgRemoveUserFromUserGroup`].
270    ///
271    /// * `subspace_id` - Subspace id to which the group belongs.
272    /// * `group_id` - Id of the group from which will be removed the user.
273    /// * `user` - Address of the user to remove from the group.
274    /// * `signer` - Address of who wants to remove the user from the group.
275    pub fn remove_user_from_user_group(
276        subspace_id: u64,
277        group_id: u32,
278        user: Addr,
279        signer: Addr,
280    ) -> MsgRemoveUserFromUserGroup {
281        MsgRemoveUserFromUserGroup {
282            subspace_id,
283            group_id,
284            user: user.into(),
285            signer: signer.into(),
286        }
287    }
288
289    /// Creates a new instance of [`MsgSetUserPermissions`].
290    ///
291    /// * `subspace_id` - Subspace id to which the user belongs.
292    /// * `section_id` - Id of the section for which to set the permissions.
293    /// * `user` - User address.
294    /// * `permissions` - New user's permissions.
295    /// * `signer` - Address of who wants to update the user's permissions.
296    pub fn set_user_permissions(
297        subspace_id: u64,
298        section_id: u32,
299        user: Addr,
300        permissions: Vec<Permission>,
301        signer: Addr,
302    ) -> MsgSetUserPermissions {
303        MsgSetUserPermissions {
304            subspace_id,
305            section_id,
306            user: user.into(),
307            permissions: permissions.into_iter().map(Into::into).collect(),
308            signer: signer.into(),
309        }
310    }
311
312    /// Creates a new instance of [`MsgGrantTreasuryAuthorization`].
313    ///
314    /// * `subspace_id` - Id of the subspace where the authorization should be granted.
315    /// * `granter` - Address of the user granting a treasury authorization.
316    /// * `grantee` - Address of the user who is being granted a treasury authorization.
317    /// * `grant` - Grant represents the authorization to execute the provided methods.
318    pub fn grant_treasury_authorization(
319        subspace_id: u64,
320        granter: Addr,
321        grantee: Addr,
322        grant: AuthzGrant,
323    ) -> MsgGrantTreasuryAuthorization {
324        MsgGrantTreasuryAuthorization {
325            subspace_id,
326            granter: granter.into(),
327            grantee: grantee.into(),
328            grant: Some(grant),
329        }
330    }
331
332    /// Creates a new instance of [`MsgRevokeTreasuryAuthorization`].
333    ///
334    /// * `subspace_id` - Id of the subspace from which the authorization should be revoked.
335    /// * `granter` - Address of the user revoking the treasury authorization.
336    /// * `grantee` - Address of the user who is being revoked the treasury authorization.
337    /// * `msg_type_url` - Type url of the authorized message which is being revoked.
338    pub fn revoke_treasury_authorization(
339        subspace_id: u64,
340        granter: Addr,
341        grantee: Addr,
342        msg_type_url: &str,
343    ) -> MsgRevokeTreasuryAuthorization {
344        MsgRevokeTreasuryAuthorization {
345            subspace_id,
346            granter: granter.into(),
347            grantee: grantee.into(),
348            msg_type_url: msg_type_url.into(),
349        }
350    }
351
352    /// Creates a new instance of [`MsgGrantAllowance`].
353    ///
354    /// * `subspace_id` - Id of the subspace inside which where the allowance should be granted.
355    /// * `granter` - Address of the user granting the allowance.
356    /// * `grantee` - Target being granted the allowance.
357    /// * `allowance` - Allowance to be granted to the grantee.
358    pub fn grant_allowance(
359        subspace_id: u64,
360        granter: Addr,
361        grantee: Grantee,
362        allowance: Allowance,
363    ) -> MsgGrantAllowance {
364        MsgGrantAllowance {
365            subspace_id,
366            granter: granter.into(),
367            grantee: Some(grantee.into()),
368            allowance: Some(allowance.into()),
369        }
370    }
371
372    /// Creates a new instance of [`MsgRevokeAllowance`].
373    ///
374    /// * `subspace_id` - Id of the subspace inside which the allowance to be deleted is.
375    /// * `granter` - Address of the user being revoking the allowance.
376    /// * `grantee` - Target being revoked the allowance.
377    pub fn revoke_allowance(
378        subspace_id: u64,
379        granter: Addr,
380        grantee: Grantee,
381    ) -> MsgRevokeAllowance {
382        MsgRevokeAllowance {
383            subspace_id,
384            granter: granter.into(),
385            grantee: Some(grantee.into()),
386        }
387    }
388}
389
390#[cfg(test)]
391mod tests {
392    use super::*;
393    use crate::cosmos_types::Timestamp;
394    use crate::cosmos_types::{BasicAllowance, GenericAuthorization};
395    use chrono::DateTime;
396    use cosmwasm_std::Coin;
397
398    #[test]
399    fn test_create_subspace() {
400        let msg = SubspacesMsg::create_subspace(
401            "test",
402            "test",
403            Addr::unchecked("owner"),
404            Addr::unchecked("signer"),
405        );
406        let expected = MsgCreateSubspace {
407            name: "test".into(),
408            description: "test".into(),
409            owner: "owner".into(),
410            creator: "signer".into(),
411        };
412        assert_eq!(expected, msg)
413    }
414
415    #[test]
416    fn test_edit_subspace() {
417        let msg = SubspacesMsg::edit_subspace(
418            42,
419            "test",
420            "test",
421            Addr::unchecked("owner"),
422            Addr::unchecked("signer"),
423        );
424        let expected = MsgEditSubspace {
425            subspace_id: 42,
426            name: "test".into(),
427            description: "test".into(),
428            owner: "owner".into(),
429            signer: "signer".into(),
430        };
431        assert_eq!(expected, msg)
432    }
433
434    #[test]
435    fn test_delete_subspace() {
436        let msg = SubspacesMsg::delete_subspace(1, Addr::unchecked("signer"));
437
438        let expected = MsgDeleteSubspace {
439            subspace_id: 1,
440            signer: "signer".into(),
441        };
442
443        assert_eq!(expected, msg)
444    }
445
446    #[test]
447    fn test_create_section() {
448        let msg = SubspacesMsg::create_section(
449            1,
450            "test_section",
451            "test description".into(),
452            1,
453            Addr::unchecked("signer"),
454        );
455
456        let expected = MsgCreateSection {
457            subspace_id: 1,
458            name: "test_section".into(),
459            description: "test description".into(),
460            parent_id: 1,
461            creator: "signer".into(),
462        };
463
464        assert_eq!(expected, msg)
465    }
466
467    #[test]
468    fn test_edit_section() {
469        let msg = SubspacesMsg::edit_section(
470            1,
471            1,
472            Some("test_section".into()),
473            Some("test description".into()),
474            Addr::unchecked("signer"),
475        );
476
477        let expected = MsgEditSection {
478            subspace_id: 1,
479            section_id: 1,
480            name: "test_section".into(),
481            description: "test description".into(),
482            editor: "signer".into(),
483        };
484
485        assert_eq!(expected, msg)
486    }
487
488    #[test]
489    fn test_move_section() {
490        let msg = SubspacesMsg::move_section(1, 1, 2, Addr::unchecked("signer"));
491
492        let expected = MsgMoveSection {
493            subspace_id: 1,
494            section_id: 1,
495            new_parent_id: 2,
496            signer: "signer".into(),
497        };
498
499        assert_eq!(expected, msg)
500    }
501
502    #[test]
503    fn test_delete_section() {
504        let msg = SubspacesMsg::delete_section(1, 1, Addr::unchecked("signer"));
505
506        let expected = MsgDeleteSection {
507            subspace_id: 1,
508            section_id: 1,
509            signer: "signer".into(),
510        };
511
512        assert_eq!(expected, msg)
513    }
514
515    #[test]
516    fn test_create_user_group() {
517        let msg = SubspacesMsg::create_user_group(
518            1,
519            1,
520            "test".into(),
521            "test".into(),
522            vec![],
523            vec![],
524            Addr::unchecked("signer"),
525        );
526
527        let expected = MsgCreateUserGroup {
528            subspace_id: 1,
529            section_id: 1,
530            name: "test".into(),
531            description: "test".into(),
532            default_permissions: vec![],
533            initial_members: vec![],
534            creator: "signer".into(),
535        };
536
537        assert_eq!(expected, msg)
538    }
539
540    #[test]
541    fn test_edit_user_group() {
542        let msg = SubspacesMsg::edit_user_group(
543            1,
544            1,
545            Some("test".into()),
546            Some("test".into()),
547            Addr::unchecked("signer"),
548        );
549
550        let expected = MsgEditUserGroup {
551            subspace_id: 1,
552            group_id: 1,
553            name: "test".into(),
554            description: "test".into(),
555            signer: "signer".into(),
556        };
557
558        assert_eq!(expected, msg)
559    }
560
561    #[test]
562    fn test_move_user_group() {
563        let msg = SubspacesMsg::move_user_group(1, 1, 2, Addr::unchecked("signer"));
564
565        let expected = MsgMoveUserGroup {
566            subspace_id: 1,
567            group_id: 1,
568            new_section_id: 2,
569            signer: "signer".into(),
570        };
571
572        assert_eq!(expected, msg)
573    }
574
575    #[test]
576    fn test_set_user_group_permissions() {
577        let msg = SubspacesMsg::set_user_group_permissions(1, 1, vec![], Addr::unchecked("signer"));
578
579        let expected = MsgSetUserGroupPermissions {
580            subspace_id: 1,
581            group_id: 1,
582            permissions: vec![],
583            signer: "signer".into(),
584        };
585
586        assert_eq!(expected, msg)
587    }
588
589    #[test]
590    fn test_delete_user_group() {
591        let msg = SubspacesMsg::delete_user_group(1, 1, Addr::unchecked("signer"));
592
593        let expected = MsgDeleteUserGroup {
594            subspace_id: 1,
595            group_id: 1,
596            signer: "signer".into(),
597        };
598
599        assert_eq!(expected, msg)
600    }
601
602    #[test]
603    fn test_add_user_to_user_group() {
604        let msg = SubspacesMsg::add_user_to_user_group(
605            1,
606            1,
607            Addr::unchecked("user"),
608            Addr::unchecked("signer"),
609        );
610
611        let expected = MsgAddUserToUserGroup {
612            subspace_id: 1,
613            group_id: 1,
614            user: "user".into(),
615            signer: "signer".into(),
616        };
617
618        assert_eq!(expected, msg)
619    }
620
621    #[test]
622    fn test_remove_user_to_user_group() {
623        let msg = SubspacesMsg::remove_user_from_user_group(
624            1,
625            1,
626            Addr::unchecked("user"),
627            Addr::unchecked("signer"),
628        );
629
630        let expected = MsgRemoveUserFromUserGroup {
631            subspace_id: 1,
632            group_id: 1,
633            user: "user".into(),
634            signer: "signer".into(),
635        };
636
637        assert_eq!(expected, msg)
638    }
639
640    #[test]
641    fn test_set_user_permissions() {
642        let msg = SubspacesMsg::set_user_permissions(
643            1,
644            1,
645            Addr::unchecked("user"),
646            vec![],
647            Addr::unchecked("signer"),
648        );
649
650        let expected = MsgSetUserPermissions {
651            subspace_id: 1,
652            section_id: 1,
653            user: "user".into(),
654            permissions: vec![],
655            signer: "signer".into(),
656        };
657
658        assert_eq!(expected, msg)
659    }
660
661    #[test]
662    fn test_grant_treasury_authorization() {
663        let msg = SubspacesMsg::grant_treasury_authorization(
664            1,
665            Addr::unchecked("granter"),
666            Addr::unchecked("grantee"),
667            AuthzGrant {
668                authorization: Some(
669                    GenericAuthorization {
670                        msg: "test.v1beta1.MsgTest".into(),
671                    }
672                    .into(),
673                ),
674                expiration: Some(Timestamp::from(DateTime::from(
675                    DateTime::parse_from_rfc3339("2140-01-01T10:00:20.021Z").unwrap(),
676                ))),
677            },
678        );
679
680        let expected = MsgGrantTreasuryAuthorization {
681            subspace_id: 1,
682            granter: "granter".into(),
683            grantee: "grantee".into(),
684            grant: Some(AuthzGrant {
685                authorization: Some(
686                    GenericAuthorization {
687                        msg: "test.v1beta1.MsgTest".into(),
688                    }
689                    .into(),
690                ),
691                expiration: Some(Timestamp::from(DateTime::from(
692                    DateTime::parse_from_rfc3339("2140-01-01T10:00:20.021Z").unwrap(),
693                ))),
694            }),
695        };
696
697        assert_eq!(expected, msg)
698    }
699
700    #[test]
701    fn test_revoke_treasury_authorization() {
702        let msg = SubspacesMsg::revoke_treasury_authorization(
703            1,
704            Addr::unchecked("granter"),
705            Addr::unchecked("grantee"),
706            "test.v1beta1.MsgTest",
707        );
708
709        let expected = MsgRevokeTreasuryAuthorization {
710            subspace_id: 1,
711            granter: "granter".into(),
712            grantee: "grantee".into(),
713            msg_type_url: "test.v1beta1.MsgTest".into(),
714        };
715
716        assert_eq!(expected, msg)
717    }
718
719    #[test]
720    fn test_grant_allowance() {
721        let msg = SubspacesMsg::grant_allowance(
722            1,
723            Addr::unchecked("granter"),
724            Grantee::UserGrantee(UserGrantee {
725                user: "grantee".into(),
726            }),
727            Allowance::BasicAllowance(BasicAllowance {
728                spend_limit: vec![Coin::new(100, "stake").into()],
729                expiration: None,
730            }),
731        );
732
733        let expected = MsgGrantAllowance {
734            subspace_id: 1,
735            granter: "granter".into(),
736            grantee: Some(
737                UserGrantee {
738                    user: "grantee".into(),
739                }
740                .into(),
741            ),
742            allowance: Some(
743                Allowance::BasicAllowance(BasicAllowance {
744                    spend_limit: vec![Coin::new(100, "stake").into()],
745                    expiration: None,
746                })
747                .into(),
748            ),
749        };
750
751        assert_eq!(expected, msg)
752    }
753
754    #[test]
755    fn test_revoke_allowance() {
756        let msg = SubspacesMsg::revoke_allowance(
757            1,
758            Addr::unchecked("granter"),
759            Grantee::UserGrantee(UserGrantee {
760                user: "grantee".into(),
761            }),
762        );
763
764        let expected = MsgRevokeAllowance {
765            subspace_id: 1,
766            granter: "granter".into(),
767            grantee: Some(
768                UserGrantee {
769                    user: "grantee".into(),
770                }
771                .into(),
772            ),
773        };
774
775        assert_eq!(expected, msg)
776    }
777}