1use crate::error::Result;
2use crate::util::BinarySerializer;
3use crate::{
4 model::{Amount, Id, PrivateKey, Proof, PublicKey},
5 util::{sign_order, Hash},
6};
7
8use super::{Order, OrderType, SignedOrder};
9
10#[derive(Eq, PartialEq, Clone, Debug)]
11pub struct OrderInfoV3 {
12 id: Id,
13 chain_id: u8,
14 timestamp: u64,
15 sender: PublicKey,
16 fee: Amount,
17 order_type: OrderType,
18 amount: Amount,
19 price: Amount,
20 matcher: PublicKey,
21 expiration: u64,
22 proofs: Vec<Proof>,
23}
24
25impl OrderInfoV3 {
26 #[allow(clippy::too_many_arguments)]
27 pub fn new(
28 id: Id,
29 chain_id: u8,
30 timestamp: u64,
31 sender: PublicKey,
32 fee: Amount,
33 order_type: OrderType,
34 amount: Amount,
35 price: Amount,
36 matcher: PublicKey,
37 expiration: u64,
38 proofs: Vec<Proof>,
39 ) -> Self {
40 Self {
41 id,
42 chain_id,
43 timestamp,
44 sender,
45 fee,
46 order_type,
47 amount,
48 price,
49 matcher,
50 expiration,
51 proofs,
52 }
53 }
54
55 pub fn chain_id(&self) -> u8 {
56 self.chain_id
57 }
58
59 pub fn version(&self) -> u8 {
60 3
61 }
62
63 pub fn sender(&self) -> PublicKey {
64 self.sender.clone()
65 }
66
67 pub fn timestamp(&self) -> u64 {
68 self.timestamp
69 }
70
71 pub fn fee(&self) -> Amount {
72 self.fee.clone()
73 }
74
75 pub fn order_type(&self) -> OrderType {
76 self.order_type.clone()
77 }
78
79 pub fn amount(&self) -> Amount {
80 self.amount.clone()
81 }
82
83 pub fn price(&self) -> Amount {
84 self.price.clone()
85 }
86
87 pub fn matcher(&self) -> PublicKey {
88 self.matcher.clone()
89 }
90
91 pub fn expiration(&self) -> u64 {
92 self.expiration
93 }
94
95 pub fn id(&self) -> Id {
96 self.id.clone()
97 }
98
99 pub fn proofs(&self) -> Vec<Proof> {
100 self.proofs.clone()
101 }
102}
103
104#[derive(Eq, PartialEq, Clone, Debug)]
105pub struct OrderV3 {
106 chain_id: u8,
107 timestamp: u64,
108 sender: PublicKey,
109 fee: Amount,
110 order_type: OrderType,
111 amount: Amount,
112 price: Amount,
113 matcher: PublicKey,
114 expiration: u64,
115}
116
117#[allow(clippy::too_many_arguments)]
118impl OrderV3 {
119 pub fn new(
120 chain_id: u8,
121 timestamp: u64,
122 sender: PublicKey,
123 fee: Amount,
124 order_type: OrderType,
125 amount: Amount,
126 price: Amount,
127 matcher: PublicKey,
128 expiration: u64,
129 ) -> Self {
130 Self {
131 chain_id,
132 timestamp,
133 sender,
134 fee,
135 order_type,
136 amount,
137 price,
138 matcher,
139 expiration,
140 }
141 }
142
143 pub fn chain_id(&self) -> u8 {
144 self.chain_id
145 }
146
147 pub fn version(&self) -> u8 {
148 3
149 }
150
151 pub fn sender(&self) -> PublicKey {
152 self.sender.clone()
153 }
154
155 pub fn timestamp(&self) -> u64 {
156 self.timestamp
157 }
158
159 pub fn fee(&self) -> Amount {
160 self.fee.clone()
161 }
162
163 pub fn order_type(&self) -> OrderType {
164 self.order_type.clone()
165 }
166
167 pub fn amount(&self) -> Amount {
168 self.amount.clone()
169 }
170
171 pub fn price(&self) -> Amount {
172 self.price.clone()
173 }
174
175 pub fn matcher(&self) -> PublicKey {
176 self.matcher.clone()
177 }
178
179 pub fn expiration(&self) -> u64 {
180 self.expiration
181 }
182
183 pub fn id(&self) -> Result<Id> {
184 Ok(Id::from_bytes(&Hash::blake(&self.bytes()?)?))
185 }
186
187 pub fn bytes(&self) -> Result<Vec<u8>> {
188 BinarySerializer::order_body_bytes(&Order::V3(self.to_owned()))
189 }
190
191 pub fn sign(&self, private_key: &PrivateKey) -> Result<SignedOrder> {
192 sign_order(&Order::V3(self.clone()), private_key)
193 }
194}
195
196#[cfg(test)]
197mod tests {
198 use crate::model::{Amount, AssetId, ByteString, PublicKey};
199
200 use super::OrderV3;
201
202 #[test]
203 fn test_order_id_calculation() {
204 let order = OrderV3 {
205 chain_id: 84,
206 timestamp: 1664244861345,
207 sender: PublicKey::from_string("FarW7tFmnVJBsHUdDe9DMJcfUESh266UDmEm1vP6P2xE")
208 .expect("failed to parse sender public key"),
209 fee: Amount::new(10000000, None),
210 order_type: super::OrderType::Sell,
211 amount: Amount::new(10000000000, None),
212 price: Amount::new(
213 15000000,
214 Some(
215 AssetId::from_string("25FEqEjRkqK6yCkiT7Lz6SAYz7gUFCtxfCChnrVFD5AT")
216 .expect("failed to parse asset id"),
217 ),
218 ),
219 matcher: PublicKey::from_string("8QUAqtTckM5B8gvcuP7mMswat9SjKUuafJMusEoSn1Gy")
220 .expect("failed to parse matcher public key"),
221 expiration: 1666750461345,
222 };
223
224 assert_eq!(
225 "H2EaCndcFAETGaWkPifGdNBL3scaZ53Pgm4Ha4xvg9wb",
226 order.id().expect("failed to calculate order id").encoded()
227 );
228 }
229}