1
- use std:: collections:: HashMap ;
1
+ use std:: { collections:: HashMap , ops :: Deref } ;
2
2
3
3
use near_sdk:: { env, json_types:: U128 , near_bindgen, require, AccountId } ;
4
4
use sweat_jar_model:: {
@@ -13,7 +13,7 @@ use crate::{
13
13
model:: Deposit ,
14
14
view:: DetailedJarV2 ,
15
15
} ,
16
- product:: model:: v2:: ProductV2 ,
16
+ product:: model:: v2:: { InterestCalculator , ProductV2 } ,
17
17
Contract , ContractExt ,
18
18
} ;
19
19
@@ -22,18 +22,43 @@ impl Contract {
22
22
require ! ( product. is_enabled, "The product is disabled" ) ;
23
23
24
24
let account_id = env:: predecessor_account_id ( ) ;
25
- let account = self . get_account_mut ( & account_id) ;
26
- let jar = account. get_jar_mut ( & product. id ) ;
27
-
28
25
let now = env:: block_timestamp_ms ( ) ;
26
+
27
+ let account = self . get_account ( & account_id) ;
28
+ let jar = account. get_jar ( & product. id ) ;
29
+
29
30
let ( amount, partition_index) = jar. get_liquid_balance ( & product. terms , now) ;
30
31
31
32
require ! ( amount > 0 , "Nothing to restake" ) ;
32
33
33
- self . update_jar_cache ( account, & product. id ) ;
34
+ self . update_jar_cache ( & account_id, & product. id ) ;
35
+
36
+ let account = self . get_account_mut ( & account_id) ;
37
+ let jar = account. get_jar_mut ( & product. id ) ;
34
38
jar. clean_up_deposits ( partition_index) ;
35
39
account. deposit ( & product. id , amount) ;
36
40
}
41
+
42
+ fn get_total_interest_for_account ( & self , account : & AccountV2 ) -> AggregatedInterestView {
43
+ let mut detailed_amounts = HashMap :: < ProductId , U128 > :: new ( ) ;
44
+ let mut total_amount: TokenAmount = 0 ;
45
+
46
+ for ( product_id, jar) in account. jars . iter ( ) {
47
+ let product = self . get_product ( product_id) ;
48
+ let interest = product. terms . get_interest ( account, & jar) . 0 ;
49
+
50
+ detailed_amounts. insert ( product_id. clone ( ) , interest. into ( ) ) ;
51
+ total_amount += interest;
52
+ }
53
+
54
+ AggregatedInterestView {
55
+ amount : AggregatedTokenAmountView {
56
+ detailed : detailed_amounts,
57
+ total : U128 ( total_amount) ,
58
+ } ,
59
+ timestamp : env:: block_timestamp_ms ( ) ,
60
+ }
61
+ }
37
62
}
38
63
39
64
#[ near_bindgen]
@@ -43,7 +68,11 @@ impl JarApi for Contract {
43
68
return account
44
69
. jars
45
70
. iter ( )
46
- . flat_map ( |( product_id, jar) | DetailedJarV2 ( product_id. clone ( ) , jar. clone ( ) ) . into ( ) )
71
+ . flat_map ( |( product_id, jar) | {
72
+ let detailed_jar = & DetailedJarV2 ( product_id. clone ( ) , jar. clone ( ) ) ;
73
+ let views: Vec < JarView > = detailed_jar. into ( ) ;
74
+ views
75
+ } )
47
76
. collect ( ) ;
48
77
}
49
78
@@ -54,14 +83,13 @@ impl JarApi for Contract {
54
83
vec ! [ ]
55
84
}
56
85
57
- // TODO: add v2 support
58
86
fn get_total_interest ( & self , account_id : AccountId ) -> AggregatedInterestView {
59
87
if let Some ( account) = self . try_get_account ( & account_id) {
60
- return account . get_total_interest ( ) ;
88
+ return self . get_total_interest_for_account ( account ) ;
61
89
}
62
90
63
91
if let Some ( account) = self . get_account_legacy ( & account_id) {
64
- return AccountV2 :: from ( account) . get_total_interest ( ) ;
92
+ return self . get_total_interest_for_account ( & AccountV2 :: from ( account. deref ( ) ) ) ;
65
93
}
66
94
67
95
AggregatedInterestView :: default ( )
@@ -85,6 +113,7 @@ impl JarApi for Contract {
85
113
. jars
86
114
. keys ( )
87
115
. filter ( |product_id| self . get_product ( product_id) . is_enabled )
116
+ . cloned ( )
88
117
. collect ( )
89
118
} ) ;
90
119
for product_id in product_ids. iter ( ) {
@@ -105,29 +134,6 @@ impl JarApi for Contract {
105
134
}
106
135
}
107
136
108
- impl AccountV2 {
109
- fn get_total_interest ( & self ) -> AggregatedInterestView {
110
- let mut detailed_amounts = HashMap :: < JarIdView , U128 > :: new ( ) ;
111
- let mut total_amount: TokenAmount = 0 ;
112
-
113
- for ( product_id, jar) in self . jars {
114
- let product = self . get_product ( & product_id) ;
115
- let interest = product. terms . get_interest ( self , & jar) . 0 ;
116
-
117
- detailed_amounts. insert ( product_id, interest. into ( ) ) ;
118
- total_amount += interest;
119
- }
120
-
121
- AggregatedInterestView {
122
- amount : AggregatedTokenAmountView {
123
- detailed : detailed_amounts,
124
- total : U128 ( total_amount) ,
125
- } ,
126
- timestamp : env:: block_timestamp_ms ( ) ,
127
- } ;
128
- }
129
- }
130
-
131
137
impl From < & AccountV1 > for AccountV2 {
132
138
fn from ( value : & AccountV1 ) -> Self {
133
139
let mut account = AccountV2 {
@@ -137,7 +143,7 @@ impl From<&AccountV1> for AccountV2 {
137
143
is_penalty_applied : false ,
138
144
} ;
139
145
140
- for jar in value. jars {
146
+ for jar in value. jars . iter ( ) {
141
147
let deposit = Deposit :: new ( jar. created_at , jar. principal ) ;
142
148
account. push ( & jar. product_id , deposit) ;
143
149
0 commit comments