1use defuse_crypto::PublicKey;
2use defuse_serde_utils::base64::Base64;
3use near_sdk::{AccountIdRef, near};
4use serde_with::serde_as;
5use std::{borrow::Cow, collections::BTreeSet};
6
7use crate::{Nonce, Salt, amounts::Amounts};
8
9#[must_use = "make sure to `.emit()` this event"]
10#[near(serializers = [json])]
11#[derive(Debug, Clone)]
12pub struct AccountEvent<'a, T> {
13 pub account_id: Cow<'a, AccountIdRef>,
14
15 #[serde(flatten)]
16 pub event: T,
17}
18
19impl<T> AccountEvent<'_, T> {
20 pub fn into_owned(self) -> AccountEvent<'static, T> {
21 AccountEvent {
22 account_id: Cow::Owned(self.account_id.into_owned()),
23 event: self.event,
24 }
25 }
26}
27
28impl<'a, T> AccountEvent<'a, T> {
29 #[inline]
30 pub fn new(account_id: impl Into<Cow<'a, AccountIdRef>>, event: T) -> Self {
31 Self {
32 account_id: account_id.into(),
33 event,
34 }
35 }
36}
37
38#[must_use = "make sure to `.emit()` this event"]
39#[near(serializers = [json])]
40#[derive(Debug, Clone)]
41pub struct PublicKeyEvent<'a> {
42 pub public_key: Cow<'a, PublicKey>,
43}
44
45#[near(serializers = [json])]
46#[derive(Debug, Clone)]
47pub struct NonceEvent {
48 #[serde_as(as = "Base64")]
49 pub nonce: Nonce,
50}
51
52impl NonceEvent {
53 #[inline]
54 pub const fn new(nonce: Nonce) -> Self {
55 Self { nonce }
56 }
57}
58
59#[must_use = "make sure to `.emit()` this event"]
60#[near(serializers = [json])]
61#[derive(Debug, Clone)]
62pub struct SaltRotationEvent {
63 pub current: Salt,
64 pub invalidated: BTreeSet<Salt>,
65}
66
67#[near(serializers = [json])]
68#[derive(Debug, Clone)]
69pub struct TransferEvent<'a> {
70 pub receiver_id: Cow<'a, AccountIdRef>,
71
72 pub tokens: Cow<'a, Amounts>,
73
74 #[serde(default, skip_serializing_if = "Option::is_none")]
75 pub memo: Cow<'a, Option<String>>,
76}