defuse_core/
nonce.rs

1use defuse_bitmap::{BitMap256, U248, U256};
2use defuse_map_utils::{IterableMap, Map};
3use near_sdk::near;
4
5pub type Nonce = U256;
6
7/// See [permit2 nonce schema](https://docs.uniswap.org/contracts/permit2/reference/signature-transfer#nonce-schema)
8#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
9#[near(serializers = [borsh, json])]
10#[derive(Debug, Clone, Default)]
11pub struct Nonces<T: Map<K = U248, V = U256>>(BitMap256<T>);
12
13impl<T> Nonces<T>
14where
15    T: Map<K = U248, V = U256>,
16{
17    #[inline]
18    pub const fn new(bitmap: T) -> Self {
19        Self(BitMap256::new(bitmap))
20    }
21
22    #[inline]
23    pub fn is_used(&self, n: Nonce) -> bool {
24        self.0.get_bit(n)
25    }
26
27    #[inline]
28    pub fn commit(&mut self, n: Nonce) -> bool {
29        !self.0.set_bit(n)
30    }
31
32    #[inline]
33    pub fn iter(&self) -> impl Iterator<Item = Nonce> + '_
34    where
35        T: IterableMap,
36    {
37        self.0.as_iter()
38    }
39}