Skip to content

Commit 3b53c14

Browse files
add events
1 parent 7a6e5c8 commit 3b53c14

File tree

11 files changed

+134
-210
lines changed

11 files changed

+134
-210
lines changed

contract/src/common/tests.rs

+17
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,20 @@ impl TokenUtils for u128 {
147147
self * 10u128.pow(18)
148148
}
149149
}
150+
151+
pub trait WhitespaceTrimmer {
152+
fn trim_whitespaces(&self) -> String;
153+
}
154+
155+
impl WhitespaceTrimmer for &str {
156+
fn trim_whitespaces(&self) -> String {
157+
let words: Vec<_> = self.split_whitespace().collect();
158+
words.join(" ")
159+
}
160+
}
161+
162+
impl WhitespaceTrimmer for String {
163+
fn trim_whitespaces(&self) -> String {
164+
self.as_str().trim_whitespaces()
165+
}
166+
}

contract/src/event.rs

+70-187
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,29 @@ use near_sdk::{
22
json_types::{Base64VecU8, U128},
33
log, near, serde_json, AccountId,
44
};
5-
use sweat_jar_model::{jar::JarId, Local, ProductId, Score, TokenAmount, U32, UTC};
5+
use sweat_jar_model::{Local, ProductId, Score, TokenAmount, U32, UTC};
66

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};
148

159
#[derive(Debug)]
1610
#[near(serializers=[json])]
1711
#[serde(tag = "event", content = "data", rename_all = "snake_case")]
1812
pub enum EventKind {
1913
RegisterProduct(Product),
20-
CreateJar(EventJar),
14+
Deposit((ProductId, U128)),
2115
Claim(Vec<ClaimEventItem>),
2216
Withdraw(WithdrawData),
2317
WithdrawAll(Vec<WithdrawData>),
24-
Migration(Vec<MigrationEventItem>),
2518
Restake(RestakeData),
26-
RestakeAll(Vec<RestakeData>),
19+
RestakeAll(RestakeAllData),
2720
ApplyPenalty(PenaltyData),
2821
BatchApplyPenalty(BatchPenaltyData),
2922
EnableProduct(EnableProductData),
3023
ChangeProductPublicKey(ChangeProductPublicKeyData),
31-
TopUp(TopUpData),
3224
RecordScore(Vec<ScoreData>),
3325
OldScoreWarning((Score, Local)),
3426
}
3527

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-
6628
#[derive(Debug)]
6729
#[near(serializers=[json])]
6830
struct SweatJarEvent {
@@ -78,16 +40,50 @@ pub type ClaimEventItem = (ProductId, U128);
7840
/// (id, fee, amount)
7941
pub type WithdrawData = (ProductId, U128, U128);
8042

43+
// TODO: doc change
8144
#[derive(Debug)]
8245
#[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,
8749
}
8850

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

9288
#[derive(Debug)]
9389
#[near(serializers=[json])]
@@ -121,13 +117,6 @@ pub struct ChangeProductPublicKeyData {
121117
pub pk: Base64VecU8,
122118
}
123119

124-
#[derive(Debug)]
125-
#[near(serializers=[json])]
126-
pub struct TopUpData {
127-
pub id: JarId,
128-
pub amount: U128,
129-
}
130-
131120
#[derive(Debug)]
132121
#[near(serializers=[json])]
133122
pub struct ScoreData {
@@ -172,15 +161,12 @@ impl SweatJarEvent {
172161

173162
#[cfg(test)]
174163
mod test {
175-
use std::str::FromStr;
176-
177-
use near_sdk::{json_types::U128, AccountId};
164+
use near_sdk::json_types::U128;
178165
use sweat_jar_model::Local;
179166

180167
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},
184170
test_utils::admin,
185171
};
186172

@@ -193,131 +179,28 @@ mod test {
193179

194180
#[test]
195181
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());
322205
}
323206
}

contract/src/ft_receiver.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl FungibleTokenReceiver for Contract {
3535
match ft_message {
3636
FtMessage::Stake(message) => {
3737
let receiver_id = message.receiver_id.unwrap_or(sender_id);
38-
self.deposit(receiver_id, message.ticket, amount, &message.signature);
38+
self.deposit(receiver_id, message.ticket, amount.0, &message.signature);
3939
}
4040
}
4141

contract/src/jar/api.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use sweat_jar_model::{
99

1010
use crate::{
1111
assert::assert_not_locked_legacy,
12+
event::{emit, EventKind, RestakeData},
1213
jar::{account::v1::AccountV1, model::AccountLegacyV2, view::DetailedJarV2},
1314
product::model::v1::{InterestCalculator, Product},
1415
score::AccountScore,
@@ -97,9 +98,12 @@ impl JarApi for Contract {
9798
self.assert_migrated(&env::predecessor_account_id());
9899

99100
let result = self.restake_internal(&env::predecessor_account_id(), &self.get_product(&product_id));
100-
require!(result.is_some(), "Nothing to restake");
101101

102-
// TODO: add event logging
102+
if let Some(amount) = result {
103+
emit(EventKind::Restake(RestakeData::new(product_id, amount)));
104+
} else {
105+
require!(result.is_some(), "Nothing to restake");
106+
}
103107
}
104108

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

0 commit comments

Comments
 (0)