Skip to main content

bullet_exchange_interface/
transaction.rs

1//! Transaction types.
2
3use crate::{define_enum, define_simple_type, define_struct};
4
5/// The maxium size of each transaction.
6pub const MAX_TX_SIZE: usize = 8000;
7
8/// Fix the Address type for this module.
9pub type ExchangeCall = crate::message::CallMessage<crate::address::Address>;
10pub type BankCall = bank::CallMessage<crate::address::Address>;
11
12#[derive(
13    Clone,
14    Debug,
15    Eq,
16    Hash,
17    Ord,
18    PartialEq,
19    PartialOrd,
20    serde::Serialize,
21    serde::Deserialize,
22    borsh::BorshDeserialize,
23    borsh::BorshSerialize,
24)]
25#[cfg_attr(feature = "schema", derive(sov_universal_wallet::UniversalWallet))]
26#[repr(u8)]
27#[serde(rename_all = "snake_case")]
28#[borsh(use_discriminant = true)]
29/// The top-level structure to be send to the Rollup.
30#[non_exhaustive]
31pub enum Transaction {
32    V0(Version0) = 0,
33}
34
35/// A transaction with a single signer.
36#[derive(
37    Clone,
38    Debug,
39    Eq,
40    Hash,
41    Ord,
42    PartialEq,
43    PartialOrd,
44    serde::Serialize,
45    serde::Deserialize,
46    borsh::BorshDeserialize,
47    borsh::BorshSerialize,
48)]
49#[cfg_attr(feature = "schema", derive(sov_universal_wallet::UniversalWallet))]
50pub struct Version0 {
51    /// The signature of the transaction.
52    #[serde(with = "hex::serde")]
53    #[cfg_attr(feature = "schema", sov_wallet(display = "hex"))]
54    pub signature: [u8; 64],
55    #[serde(with = "hex::serde")]
56    #[cfg_attr(feature = "schema", sov_wallet(display = "hex"))]
57    pub pub_key: [u8; 32],
58    pub runtime_call: RuntimeCall,
59    pub uniqueness: UniquenessData,
60    pub details: TxDetails,
61}
62
63define_struct! {
64    /// The transaction to be signed.
65    struct UnsignedTransaction {
66        runtime_call: RuntimeCall,
67        uniqueness: UniquenessData,
68        details: TxDetails,
69    }
70}
71
72define_enum! {
73    /// The enum to distinguish the rollup modules.
74    #[non_exhaustive]
75    enum RuntimeCall {
76        Bank(BankCall) = 2,
77        Exchange(ExchangeCall) = 7,
78    }
79}
80
81define_enum! {
82    /// A nonce to detect replays.
83    #[non_exhaustive]
84    enum UniquenessData {
85        Nonce(u64) = 0,
86        Generation(u64) = 1,
87    }
88}
89
90define_struct! {
91    /// Metadata to be given to any transactions.
92    struct TxDetails {
93        max_priority_fee_bips: PriorityFeeBips,
94        /// The max fee one is willing to pay for this transaction.
95        max_fee: Amount,
96        /// Optionally limit the number of gas to be used.
97        gas_limit: Option<Gas>,
98        /// The chain-id from the schema.
99        chain_id: u64,
100    }
101}
102
103define_simple_type!(
104    #[cfg_attr(feature = "schema", derive(sov_universal_wallet::UniversalWallet))]
105    Gas([u64; 2])
106        + Debug
107);
108define_simple_type!(PriorityFeeBips(u64));
109define_simple_type!(Amount(u128));
110#[cfg(feature = "schema")]
111impl sov_universal_wallet::ty::IntegerDisplayable for Amount {
112    fn integer_type() -> sov_universal_wallet::ty::IntegerType {
113        sov_universal_wallet::ty::IntegerType::u128
114    }
115}
116
117pub mod bank {
118    use crate::string::CustomString;
119    use crate::transaction::Amount;
120    use crate::{define_enum, define_simple_type, define_struct};
121
122    define_simple_type!(
123        #[cfg_attr(feature = "schema", derive(sov_universal_wallet::UniversalWallet))]
124        TokenId(
125            #[cfg_attr(
126                feature = "schema",
127                sov_wallet(display(bech32m(prefix = "token_id_prefix()")))
128            )]
129            [u8; 32]
130        ) + Debug
131    );
132
133    #[cfg(feature = "schema")]
134    fn token_id_prefix() -> &'static str {
135        "token_"
136    }
137    define_enum! {
138        /// CallMessage for the Bank module.
139        #[non_exhaustive]
140        enum CallMessage<Address> {
141            #[cfg_attr(feature="schema", sov_wallet(show_as = "Transfer to address {} {} with memo `{}`."))]
142            TransferWithMemo {
143                to: Address,
144                coins: Coins,
145                memo: CustomString,
146            } = 6,
147        }
148    }
149    define_struct! {
150        #[cfg_attr(feature="schema", sov_wallet(show_as = "{} coins of token ID {}"))]
151        struct Coins {
152            #[cfg_attr(feature="schema", sov_wallet(fixed_point(from_field(1, offset = 31))))]
153            amount: Amount,
154            token_id: TokenId,
155        }
156    }
157}