defuse_core/
accounts.rs

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