Skip to content

Commit 6f99039

Browse files
restore restake tests
1 parent b7b3952 commit 6f99039

File tree

3 files changed

+111
-131
lines changed

3 files changed

+111
-131
lines changed

contract/src/jar/api.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{
1818
};
1919

2020
impl Contract {
21-
fn restake_internal(&mut self, product: &ProductV2) {
21+
fn restake_internal(&mut self, product: &ProductV2) -> TokenAmount {
2222
require!(product.is_enabled, "The product is disabled");
2323

2424
let account_id = env::predecessor_account_id();
@@ -37,6 +37,8 @@ impl Contract {
3737
let jar = account.get_jar_mut(&product.id);
3838
jar.clean_up_deposits(partition_index);
3939
account.deposit(&product.id, amount);
40+
41+
amount
4042
}
4143

4244
fn get_total_interest_for_account(&self, account: &AccountV2) -> AggregatedInterestView {
@@ -103,7 +105,7 @@ impl JarApi for Contract {
103105
// TODO: add event logging
104106
}
105107

106-
fn restake_all(&mut self, product_ids: Option<Vec<ProductId>>) {
108+
fn restake_all(&mut self, product_ids: Option<Vec<ProductId>>) -> Vec<(ProductId, TokenAmount)> {
107109
let account_id = env::predecessor_account_id();
108110

109111
self.migrate_account_if_needed(&account_id);
@@ -116,11 +118,15 @@ impl JarApi for Contract {
116118
.cloned()
117119
.collect()
118120
});
121+
let mut result: Vec<(ProductId, TokenAmount)> = vec![];
119122
for product_id in product_ids.iter() {
120-
self.restake_internal(&self.get_product(product_id));
123+
let amount = self.restake_internal(&self.get_product(product_id));
124+
result.push((product_id.clone(), amount));
121125
}
122126

123127
// TODO: add event logging
128+
129+
result
124130
}
125131

126132
fn unlock_jars_for_account(&mut self, account_id: AccountId) {

contract/src/jar/tests/restake.rs

+100-126
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
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+
};
36

47
use crate::{
58
common::tests::Context,
@@ -12,140 +15,111 @@ use crate::{
1215
fn restake_by_not_owner() {
1316
let product = ProductV2::new();
1417
let alice_jar = JarV2::new();
15-
let mut ctx = Context::new(admin())
18+
let mut context = Context::new(admin())
1619
.with_products(&[product.clone()])
1720
.with_jars(&alice(), &[(product.id.clone(), alice_jar.clone())]);
1821

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());
2225
});
2326

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);
2629
});
2730

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);
3134
});
3235

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);
3538
});
3639
}
3740

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+
}

model/src/api.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
jar::{AggregatedInterestView, JarView},
1010
product::{ProductDto, ProductView},
1111
withdraw::{BulkWithdrawView, WithdrawView},
12-
ProductId, Score, UTC,
12+
ProductId, Score, TokenAmount, UTC,
1313
};
1414

1515
#[cfg(feature = "integration-test")]
@@ -84,7 +84,7 @@ pub trait JarApi {
8484
fn restake(&mut self, product_id: ProductId);
8585

8686
/// Restakes all jars for user, or only specified list of jars if `jars` argument is `Some`
87-
fn restake_all(&mut self, product_ids: Option<Vec<ProductId>>);
87+
fn restake_all(&mut self, product_ids: Option<Vec<ProductId>>) -> Vec<(ProductId, TokenAmount)>;
8888

8989
fn unlock_jars_for_account(&mut self, account_id: AccountId);
9090
}

0 commit comments

Comments
 (0)