1
1
use near_sdk:: test_utils:: test_env:: { alice, bob, carol} ;
2
- use sweat_jar_model:: api:: JarApi ;
2
+ use sweat_jar_model:: {
3
+ api:: { JarApi , ProductApi } ,
4
+ MS_IN_DAY , MS_IN_YEAR ,
5
+ } ;
3
6
4
7
use crate :: {
5
8
common:: tests:: Context ,
@@ -12,140 +15,111 @@ use crate::{
12
15
fn restake_by_not_owner ( ) {
13
16
let product = ProductV2 :: new ( ) ;
14
17
let alice_jar = JarV2 :: new ( ) ;
15
- let mut ctx = Context :: new ( admin ( ) )
18
+ let mut context = Context :: new ( admin ( ) )
16
19
. with_products ( & [ product. clone ( ) ] )
17
20
. with_jars ( & alice ( ) , & [ ( product. id . clone ( ) , alice_jar. clone ( ) ) ] ) ;
18
21
19
- ctx . switch_account ( bob ( ) ) ;
20
- expect_panic ( & ctx , "Account bob.near is not found" , || {
21
- ctx . contract ( ) . restake ( product. id . clone ( ) ) ;
22
+ context . switch_account ( bob ( ) ) ;
23
+ expect_panic ( & context , "Account bob.near is not found" , || {
24
+ context . contract ( ) . restake ( product. id . clone ( ) ) ;
22
25
} ) ;
23
26
24
- expect_panic ( & ctx , "Account bob.near is not found" , || {
25
- ctx . contract ( ) . restake_all ( None ) ;
27
+ expect_panic ( & context , "Account bob.near is not found" , || {
28
+ context . contract ( ) . restake_all ( None ) ;
26
29
} ) ;
27
30
28
- ctx . switch_account ( carol ( ) ) ;
29
- expect_panic ( & ctx , "Account carol.near is not found" , || {
30
- ctx . contract ( ) . restake ( product. id ) ;
31
+ context . switch_account ( carol ( ) ) ;
32
+ expect_panic ( & context , "Account carol.near is not found" , || {
33
+ context . contract ( ) . restake ( product. id ) ;
31
34
} ) ;
32
35
33
- expect_panic ( & ctx , "Account carol.near is not found" , || {
34
- ctx . contract ( ) . restake_all ( None ) ;
36
+ expect_panic ( & context , "Account carol.near is not found" , || {
37
+ context . contract ( ) . restake_all ( None ) ;
35
38
} ) ;
36
39
}
37
40
38
- // #[test]
39
- // #[should_panic(expected = "The jar is not mature yet")]
40
- // fn restake_before_maturity() {
41
- // let alice = alice();
42
- // let admin = admin();
43
- //
44
- // let product = Product::new().with_allows_restaking(true);
45
- // let jar = Jar::new(0);
46
- // let mut context = Context::new(admin).with_products(&[product]).with_jars(&[jar.clone()]);
47
- //
48
- // context.switch_account(&alice);
49
- // assert!(context.contract().restake_all(None).is_empty());
50
- // context.contract().restake(U32(jar.id));
51
- // }
52
- //
53
- // #[test]
54
- // #[should_panic(expected = "The product doesn't support restaking")]
55
- // fn restake_when_restaking_is_not_supported() {
56
- // let alice = alice();
57
- // let admin = admin();
58
- //
59
- // let product = Product::new().with_allows_restaking(false);
60
- //
61
- // let jar = Jar::new(0);
62
- // let mut context = Context::new(admin).with_products(&[product]).with_jars(&[jar.clone()]);
63
- //
64
- // context.switch_account(&alice);
65
- // assert!(context.contract().restake_all(None).is_empty());
66
- // context.contract().restake(U32(jar.id));
67
- // }
68
- //
69
- // #[test]
70
- // #[should_panic(expected = "The product is disabled")]
71
- // fn restake_with_disabled_product() {
72
- // let alice = alice();
73
- // let admin = admin();
74
- //
75
- // let product = Product::new().with_allows_restaking(true);
76
- // let jar = Jar::new(0);
77
- // let mut context = Context::new(admin.clone())
78
- // .with_products(&[product.clone()])
79
- // .with_jars(&[jar.clone()]);
80
- //
81
- // context.switch_account(&admin);
82
- // context.with_deposit_yocto(1, |context| context.contract().set_enabled(product.id, false));
83
- //
84
- // context.contract().products_cache.borrow_mut().clear();
85
- //
86
- // context.set_block_timestamp_in_days(366);
87
- //
88
- // context.switch_account(&alice);
89
- // assert!(context.contract().restake_all(None).is_empty());
90
- // context.contract().restake(U32(jar.id));
91
- // }
92
- //
93
- // #[test]
94
- // #[should_panic(expected = "The jar is empty, nothing to restake")]
95
- // fn restake_empty_jar() {
96
- // let alice = alice();
97
- // let admin = admin();
98
- //
99
- // let product = Product::new().with_allows_restaking(true);
100
- // let jar = Jar::new(0).principal(0);
101
- // let mut context = Context::new(admin).with_products(&[product]).with_jars(&[jar.clone()]);
102
- //
103
- // context.set_block_timestamp_in_days(366);
104
- //
105
- // context.switch_account(&alice);
106
- // assert!(context.contract().restake_all(None).is_empty());
107
- // context.contract().restake(U32(jar.id));
108
- // }
109
- //
110
- // #[test]
111
- // fn restake_after_maturity_for_restakable_product() {
112
- // let alice = alice();
113
- // let admin = admin();
114
- //
115
- // let product = Product::new().with_allows_restaking(true);
116
- // let jar = Jar::new(0);
117
- // let mut context = Context::new(admin).with_products(&[product]).with_jars(&[jar.clone()]);
118
- //
119
- // context.set_block_timestamp_in_days(366);
120
- //
121
- // context.switch_account(&alice);
122
- // context.contract().restake(U32(jar.id));
123
- //
124
- // let alice_jars = context.contract().get_jars_for_account(alice);
125
- //
126
- // assert_eq!(2, alice_jars.len());
127
- // assert_eq!(0, alice_jars.iter().find(|item| item.id.0 == 0).unwrap().principal.0);
128
- // assert_eq!(
129
- // 1_000_000,
130
- // alice_jars.iter().find(|item| item.id.0 == 1).unwrap().principal.0
131
- // );
132
- // }
133
- //
134
- // #[test]
135
- // #[should_panic(expected = "The product doesn't support restaking")]
136
- // fn restake_after_maturity_for_not_restakable_product() {
137
- // let alice = alice();
138
- // let admin = admin();
139
- //
140
- // let product = Product::new().with_allows_restaking(false);
141
- // let jar = Jar::new(0);
142
- // let mut context = Context::new(admin.clone())
143
- // .with_products(&[product.clone()])
144
- // .with_jars(&[jar.clone()]);
145
- //
146
- // context.set_block_timestamp_in_days(366);
147
- //
148
- // context.switch_account(&alice);
149
- // assert!(context.contract().restake_all(None).is_empty());
150
- // context.contract().restake(U32(jar.id));
151
- // }
41
+ #[ test]
42
+ #[ should_panic( expected = "Nothing to restake" ) ]
43
+ fn restake_before_maturity ( ) {
44
+ let alice = alice ( ) ;
45
+ let admin = admin ( ) ;
46
+
47
+ let product = ProductV2 :: new ( ) ;
48
+ let alice_jar = JarV2 :: new ( ) ;
49
+ let mut context = Context :: new ( admin)
50
+ . with_products ( & [ product. clone ( ) ] )
51
+ . with_jars ( & alice, & [ ( product. id . clone ( ) , alice_jar. clone ( ) ) ] ) ;
52
+
53
+ context. switch_account ( & alice) ;
54
+ assert ! ( context. contract( ) . restake_all( None ) . is_empty( ) ) ;
55
+ context. contract ( ) . restake ( product. id ) ;
56
+ }
57
+
58
+ #[ test]
59
+ #[ should_panic( expected = "The product is disabled" ) ]
60
+ fn restake_with_disabled_product ( ) {
61
+ let alice = alice ( ) ;
62
+ let admin = admin ( ) ;
63
+
64
+ let product = ProductV2 :: new ( ) ;
65
+ let alice_jar = JarV2 :: new ( ) ;
66
+ let mut context = Context :: new ( admin. clone ( ) )
67
+ . with_products ( & [ product. clone ( ) ] )
68
+ . with_jars ( & alice, & [ ( product. id . clone ( ) , alice_jar. clone ( ) ) ] ) ;
69
+
70
+ context. switch_account ( & admin) ;
71
+ context. with_deposit_yocto ( 1 , |context| context. contract ( ) . set_enabled ( product. id . clone ( ) , false ) ) ;
72
+
73
+ context. contract ( ) . products_cache . borrow_mut ( ) . clear ( ) ;
74
+
75
+ context. set_block_timestamp_in_days ( 366 ) ;
76
+
77
+ context. switch_account ( & alice) ;
78
+ assert ! ( context. contract( ) . restake_all( None ) . is_empty( ) ) ;
79
+ context. contract ( ) . restake ( product. id ) ;
80
+ }
81
+
82
+ #[ test]
83
+ #[ should_panic( expected = "Nothing to restake" ) ]
84
+ fn restake_empty_jar ( ) {
85
+ let alice = alice ( ) ;
86
+ let admin = admin ( ) ;
87
+
88
+ let product = ProductV2 :: new ( ) ;
89
+ let alice_jar = JarV2 :: new ( ) ;
90
+ let mut context = Context :: new ( admin. clone ( ) )
91
+ . with_products ( & [ product. clone ( ) ] )
92
+ . with_jars ( & alice, & [ ( product. id . clone ( ) , alice_jar. clone ( ) ) ] ) ;
93
+
94
+ context. set_block_timestamp_in_days ( 366 ) ;
95
+
96
+ context. switch_account ( & alice) ;
97
+ assert ! ( context. contract( ) . restake_all( None ) . is_empty( ) ) ;
98
+ context. contract ( ) . restake ( product. id ) ;
99
+ }
100
+
101
+ #[ test]
102
+ fn restake_after_maturity ( ) {
103
+ let alice = alice ( ) ;
104
+ let admin = admin ( ) ;
105
+
106
+ let product = ProductV2 :: new ( ) ;
107
+ let principal = 1_000_000 ;
108
+ let alice_jar = JarV2 :: new ( ) . with_deposit ( 0 , principal) ;
109
+ let mut context = Context :: new ( admin. clone ( ) )
110
+ . with_products ( & [ product. clone ( ) ] )
111
+ . with_jars ( & alice, & [ ( product. id . clone ( ) , alice_jar. clone ( ) ) ] ) ;
112
+
113
+ let restake_time = MS_IN_YEAR + MS_IN_DAY ;
114
+ context. set_block_timestamp_in_ms ( restake_time) ;
115
+
116
+ context. switch_account ( & alice) ;
117
+ context. contract ( ) . restake ( product. id . clone ( ) ) ;
118
+
119
+ let alice_jars = context. contract ( ) . get_jars_for_account ( alice) ;
120
+ assert_eq ! ( 1 , alice_jars. len( ) ) ;
121
+
122
+ let jar = alice_jars. first ( ) . unwrap ( ) ;
123
+ assert_eq ! ( principal, jar. principal. 0 ) ;
124
+ assert_eq ! ( restake_time, jar. created_at. 0 ) ;
125
+ }
0 commit comments