@@ -2,67 +2,29 @@ use near_sdk::{
2
2
json_types:: { Base64VecU8 , U128 } ,
3
3
log, near, serde_json, AccountId ,
4
4
} ;
5
- use sweat_jar_model:: { jar :: JarId , Local , ProductId , Score , TokenAmount , U32 , UTC } ;
5
+ use sweat_jar_model:: { Local , ProductId , Score , TokenAmount , U32 , UTC } ;
6
6
7
- use crate :: {
8
- common:: Timestamp ,
9
- env,
10
- jar:: model:: { Jar , JarCache } ,
11
- product:: model:: Product ,
12
- PACKAGE_NAME , VERSION ,
13
- } ;
7
+ use crate :: { common:: Timestamp , env, product:: model:: Product , PACKAGE_NAME , VERSION } ;
14
8
15
9
#[ derive( Debug ) ]
16
10
#[ near( serializers=[ json] ) ]
17
11
#[ serde( tag = "event" , content = "data" , rename_all = "snake_case" ) ]
18
12
pub enum EventKind {
19
13
RegisterProduct ( Product ) ,
20
- CreateJar ( EventJar ) ,
14
+ Deposit ( ( ProductId , U128 ) ) ,
21
15
Claim ( Vec < ClaimEventItem > ) ,
22
16
Withdraw ( WithdrawData ) ,
23
17
WithdrawAll ( Vec < WithdrawData > ) ,
24
- Migration ( Vec < MigrationEventItem > ) ,
25
18
Restake ( RestakeData ) ,
26
- RestakeAll ( Vec < RestakeData > ) ,
19
+ RestakeAll ( RestakeAllData ) ,
27
20
ApplyPenalty ( PenaltyData ) ,
28
21
BatchApplyPenalty ( BatchPenaltyData ) ,
29
22
EnableProduct ( EnableProductData ) ,
30
23
ChangeProductPublicKey ( ChangeProductPublicKeyData ) ,
31
- TopUp ( TopUpData ) ,
32
24
RecordScore ( Vec < ScoreData > ) ,
33
25
OldScoreWarning ( ( Score , Local ) ) ,
34
26
}
35
27
36
- #[ derive( Debug ) ]
37
- #[ near( serializers=[ json] ) ]
38
- pub struct EventJar {
39
- id : JarId ,
40
- account_id : AccountId ,
41
- product_id : ProductId ,
42
- created_at : Timestamp ,
43
- principal : TokenAmount ,
44
- cache : Option < JarCache > ,
45
- claimed_balance : TokenAmount ,
46
- is_pending_withdraw : bool ,
47
- is_penalty_applied : bool ,
48
- }
49
-
50
- impl From < Jar > for EventJar {
51
- fn from ( jar : Jar ) -> Self {
52
- Self {
53
- id : jar. id ,
54
- account_id : jar. account_id . clone ( ) ,
55
- product_id : jar. product_id . clone ( ) ,
56
- created_at : jar. created_at ,
57
- principal : jar. principal ,
58
- cache : jar. cache ,
59
- claimed_balance : jar. claimed_balance ,
60
- is_pending_withdraw : jar. is_pending_withdraw ,
61
- is_penalty_applied : jar. is_penalty_applied ,
62
- }
63
- }
64
- }
65
-
66
28
#[ derive( Debug ) ]
67
29
#[ near( serializers=[ json] ) ]
68
30
struct SweatJarEvent {
@@ -78,16 +40,50 @@ pub type ClaimEventItem = (ProductId, U128);
78
40
/// (id, fee, amount)
79
41
pub type WithdrawData = ( ProductId , U128 , U128 ) ;
80
42
43
+ // TODO: doc change
81
44
#[ derive( Debug ) ]
82
45
#[ near( serializers=[ json] ) ]
83
- pub struct MigrationEventItem {
84
- pub original_id : String ,
85
- pub id : JarId ,
86
- pub account_id : AccountId ,
46
+ pub struct RestakeData {
47
+ pub product_id : ProductId ,
48
+ pub restaked : U128 ,
87
49
}
88
50
89
- /// (`old_id`, `new_id`)
90
- pub type RestakeData = ( JarId , JarId ) ;
51
+ #[ derive( Debug ) ]
52
+ #[ near( serializers=[ json] ) ]
53
+ pub struct RestakeAllData {
54
+ pub timestamp : Timestamp ,
55
+ pub from : Vec < ProductId > ,
56
+ pub into : ProductId ,
57
+ pub restaked : U128 ,
58
+ pub withdrawn : U128 ,
59
+ }
60
+
61
+ impl RestakeAllData {
62
+ pub fn new (
63
+ timestamp : Timestamp ,
64
+ from : Vec < ProductId > ,
65
+ into : ProductId ,
66
+ restaked : TokenAmount ,
67
+ withdrawn : TokenAmount ,
68
+ ) -> Self {
69
+ RestakeAllData {
70
+ timestamp,
71
+ from,
72
+ into,
73
+ restaked : restaked. into ( ) ,
74
+ withdrawn : withdrawn. into ( ) ,
75
+ }
76
+ }
77
+ }
78
+
79
+ impl RestakeData {
80
+ pub fn new ( product_id : ProductId , restaked : TokenAmount ) -> Self {
81
+ RestakeData {
82
+ product_id,
83
+ restaked : restaked. into ( ) ,
84
+ }
85
+ }
86
+ }
91
87
92
88
#[ derive( Debug ) ]
93
89
#[ near( serializers=[ json] ) ]
@@ -121,13 +117,6 @@ pub struct ChangeProductPublicKeyData {
121
117
pub pk : Base64VecU8 ,
122
118
}
123
119
124
- #[ derive( Debug ) ]
125
- #[ near( serializers=[ json] ) ]
126
- pub struct TopUpData {
127
- pub id : JarId ,
128
- pub amount : U128 ,
129
- }
130
-
131
120
#[ derive( Debug ) ]
132
121
#[ near( serializers=[ json] ) ]
133
122
pub struct ScoreData {
@@ -172,15 +161,12 @@ impl SweatJarEvent {
172
161
173
162
#[ cfg( test) ]
174
163
mod test {
175
- use std:: str:: FromStr ;
176
-
177
- use near_sdk:: { json_types:: U128 , AccountId } ;
164
+ use near_sdk:: json_types:: U128 ;
178
165
use sweat_jar_model:: Local ;
179
166
180
167
use crate :: {
181
- common:: tests:: Context ,
182
- event:: { EventKind , ScoreData , SweatJarEvent , TopUpData } ,
183
- jar:: model:: { Jar , JarLastVersion } ,
168
+ common:: tests:: { Context , WhitespaceTrimmer } ,
169
+ event:: { EventKind , SweatJarEvent } ,
184
170
test_utils:: admin,
185
171
} ;
186
172
@@ -193,131 +179,28 @@ mod test {
193
179
194
180
#[ test]
195
181
fn event_to_string ( ) {
196
- assert_eq ! (
197
- SweatJarEvent :: from( EventKind :: TopUp ( TopUpData {
198
- id: 10 ,
199
- amount: U128 ( 50 ) ,
200
- } ) )
201
- . to_json_event_string( ) ,
202
- r#"EVENT_JSON:{
203
- "standard": "sweat_jar",
204
- "version": "3.3.10",
205
- "event": "top_up",
206
- "data": {
207
- "id": 10,
208
- "amount": "50"
209
- }
210
- }"#
211
- ) ;
212
-
213
- assert_eq ! (
214
- SweatJarEvent :: from( EventKind :: CreateJar (
215
- Jar :: V1 ( JarLastVersion {
216
- id: 555 ,
217
- account_id: "bob.near" . to_string( ) . try_into( ) . unwrap( ) ,
218
- product_id: "some_product" . to_string( ) ,
219
- created_at: 1234324235 ,
220
- principal: 78685678567 ,
221
- cache: None ,
222
- claimed_balance: 4324 ,
223
- is_pending_withdraw: false ,
224
- is_penalty_applied: false ,
225
- claim_remainder: 55555 ,
226
- } )
227
- . into( )
228
- ) )
229
- . to_json_event_string( ) ,
230
- r#"EVENT_JSON:{
231
- "standard": "sweat_jar",
232
- "version": "3.3.10",
233
- "event": "create_jar",
234
- "data": {
235
- "id": 555,
236
- "account_id": "bob.near",
237
- "product_id": "some_product",
238
- "created_at": 1234324235,
239
- "principal": 78685678567,
240
- "cache": null,
241
- "claimed_balance": 4324,
242
- "is_pending_withdraw": false,
243
- "is_penalty_applied": false
244
- }
245
- }"#
246
- ) ;
247
-
248
- assert_eq ! (
249
- SweatJarEvent :: from( EventKind :: Claim ( vec![
250
- ( "product_id" . to_string( ) , 1 . into( ) ) ,
251
- ( "another_product_id" . to_string( ) , 2 . into( ) )
252
- ] ) )
253
- . to_json_event_string( ) ,
254
- r#"EVENT_JSON:{
255
- "standard": "sweat_jar",
256
- "version": "3.3.10",
257
- "event": "claim",
258
- "data": [
259
- [
260
- "product_id",
261
- "1"
262
- ],
263
- [
264
- "another_product_id",
265
- "2"
266
- ]
267
- ]
268
- }"#
269
- ) ;
270
-
271
- assert_eq ! (
272
- SweatJarEvent :: from( EventKind :: RecordScore ( vec![
273
- ScoreData {
274
- account_id: AccountId :: from_str( "alice.near" ) . unwrap( ) ,
275
- score: vec![ ( 10 . into( ) , 10 . into( ) ) ] ,
276
- } ,
277
- ScoreData {
278
- account_id: AccountId :: from_str( "bob.near" ) . unwrap( ) ,
279
- score: vec![ ( 20 . into( ) , 20 . into( ) ) ] ,
280
- }
281
- ] ) )
282
- . to_json_event_string( ) ,
283
- r#"EVENT_JSON:{
284
- "standard": "sweat_jar",
285
- "version": "3.3.10",
286
- "event": "record_score",
287
- "data": [
288
- {
289
- "account_id": "alice.near",
290
- "score": [
291
- [
292
- "10",
293
- 10
294
- ]
295
- ]
296
- },
297
- {
298
- "account_id": "bob.near",
299
- "score": [
300
- [
301
- "20",
302
- 20
303
- ]
304
- ]
305
- }
306
- ]
307
- }"#
308
- ) ;
309
-
310
- assert_eq ! (
311
- SweatJarEvent :: from( EventKind :: OldScoreWarning ( ( 111 , Local ( 5 ) ) ) ) . to_json_event_string( ) ,
312
- r#"EVENT_JSON:{
313
- "standard": "sweat_jar",
314
- "version": "3.3.10",
315
- "event": "old_score_warning",
316
- "data": [
317
- 111,
318
- 5
319
- ]
320
- }"#
321
- ) ;
182
+ let event = SweatJarEvent :: from ( EventKind :: Claim ( vec ! [
183
+ ( "product_0" . to_string( ) , U128 ( 50 ) ) ,
184
+ ( "product_1" . to_string( ) , U128 ( 200 ) ) ,
185
+ ] ) )
186
+ . to_json_event_string ( ) ;
187
+ let json = r#"EVENT_JSON:{
188
+ "standard": "sweat_jar",
189
+ "version": "3.3.10",
190
+ "event": "claim",
191
+ "data": [ [ "product_0", "50" ], [ "product_1", "200" ] ]
192
+ }"# ;
193
+
194
+ assert_eq ! ( json. trim_whitespaces( ) , event. trim_whitespaces( ) ) ;
195
+
196
+ let event = SweatJarEvent :: from ( EventKind :: OldScoreWarning ( ( 111 , Local ( 5 ) ) ) ) . to_json_event_string ( ) ;
197
+ let json = r#"EVENT_JSON:{
198
+ "standard": "sweat_jar",
199
+ "version": "3.3.10",
200
+ "event": "old_score_warning",
201
+ "data": [ 111, 5 ]
202
+ }"# ;
203
+
204
+ assert_eq ! ( json. trim_whitespaces( ) , event. trim_whitespaces( ) ) ;
322
205
}
323
206
}
0 commit comments