Skip to content

Commit

Permalink
fix: revert batched logging (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasyafromrussia authored Feb 26, 2024
1 parent 2accf93 commit d8b71b0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 38 deletions.
6 changes: 2 additions & 4 deletions contract/src/record/api.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use claim_model::{
account_record::AccountRecord,
api::RecordApi,
event::{BatchedEmitData, RecordData},
event::{emit, EventKind::Record, RecordData},
};
use near_sdk::{json_types::U128, near_bindgen, store::Vector, AccountId};

use crate::{common::now_seconds, Contract, ContractExt, StorageKey::AccrualsEntry};

const RECORD_EVENT_BATCH_SIZE: usize = 100;

#[near_bindgen]
impl RecordApi for Contract {
fn record_batch_for_hold(&mut self, amounts: Vec<(AccountId, U128)>) {
Expand Down Expand Up @@ -43,6 +41,6 @@ impl RecordApi for Contract {
}
}

event_data.emit_batched(RECORD_EVENT_BATCH_SIZE);
emit(Record(event_data));
}
}
40 changes: 6 additions & 34 deletions model/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION");
content = "data",
rename_all = "snake_case"
)]
pub enum EventKind<'a> {
pub enum EventKind {
Burn(BurnData),
Claim(ClaimData),
Clean(CleanData),
Record(RecordData),
RecordChunk(RecordChunk<'a>),
}

#[derive(Serialize, Debug)]
Expand Down Expand Up @@ -47,13 +46,6 @@ pub struct RecordData {
pub amounts: Vec<(AccountId, U128)>,
}

#[derive(Serialize, Debug)]
#[serde(crate = "near_sdk::serde")]
pub struct RecordChunk<'a> {
pub timestamp: UnixTimestamp,
pub amounts: &'a [(AccountId, U128)],
}

impl RecordData {
pub fn new(timestamp: UnixTimestamp) -> Self {
Self {
Expand All @@ -63,37 +55,17 @@ impl RecordData {
}
}

pub trait BatchedEmitData {
fn emit_batched(self, batch_size: usize);
}

impl BatchedEmitData for RecordData {
fn emit_batched(self, batch_size: usize) {
if self.amounts.len() <= batch_size {
emit(EventKind::Record(self));
return;
}

for amounts in self.amounts.chunks(batch_size) {
emit(EventKind::RecordChunk(RecordChunk {
timestamp: self.timestamp,
amounts,
}));
}
}
}

#[derive(Serialize, Debug)]
#[serde(crate = "near_sdk::serde", rename_all = "snake_case")]
struct SweatClaimEvent<'a> {
struct SweatClaimEvent {
standard: &'static str,
version: &'static str,
#[serde(flatten)]
event_kind: EventKind<'a>,
event_kind: EventKind,
}

impl<'a> From<EventKind<'a>> for SweatClaimEvent<'a> {
fn from(event_kind: EventKind<'a>) -> Self {
impl From<EventKind> for SweatClaimEvent {
fn from(event_kind: EventKind) -> Self {
Self {
standard: PACKAGE_NAME,
version: VERSION,
Expand All @@ -106,7 +78,7 @@ pub fn emit(event: EventKind) {
log!(SweatClaimEvent::from(event).to_json_event_string());
}

impl SweatClaimEvent<'_> {
impl SweatClaimEvent {
fn to_json_string(&self) -> String {
serde_json::to_string_pretty(self)
.unwrap_or_else(|err| env::panic_str(&format!("Failed to serialize SweatClaimEvent: {err}")))
Expand Down

0 comments on commit d8b71b0

Please sign in to comment.