shuttle_core/
operation_builder.rs

1use operation::{CreateAccountOperation, CreatePassiveOfferOperation, InflationOperation,
2                ManageDataOperation, ManageOfferOperation, Operation, PathPaymentOperation,
3                PaymentOperation};
4use crypto::PublicKey;
5use asset::Asset;
6use amount::{Amount, Price};
7
8/// Build an [`Operation`](enum.Operation.html).
9#[derive(Debug)]
10pub struct OperationBuilder;
11
12impl OperationBuilder {
13    /// Build an [`InflationOperation`](struct.InflationOperation.html).
14    pub fn inflation() -> InflationOperationBuilder {
15        InflationOperationBuilder::new()
16    }
17
18    /// Build a [`CreateAccountOperation`](struct.CreateAccountOperation.html) with
19    /// `destination` address and starting `balance`.
20    pub fn create_account(
21        destination: PublicKey,
22        balance: Amount,
23    ) -> CreateAccountOperationBuilder {
24        CreateAccountOperationBuilder::new(destination, balance)
25    }
26
27    /// Build a [`PaymentOperation`](struct.PaymentOperation.html) sending `amount`
28    /// units of the `asset` to the `destination` account.
29    pub fn payment(
30        destination: PublicKey,
31        asset: Asset,
32        amount: Amount,
33    ) -> PaymentOperationBuilder {
34        PaymentOperationBuilder::new(destination, asset, amount)
35    }
36
37    /// Build a [`PathPaymentOperation`](struct.PathPaymentOperation.html).
38    pub fn path_payment(
39        destination: PublicKey,
40        send_asset: Asset,
41        send_max: Amount,
42        dest_asset: Asset,
43        dest_amount: Amount,
44    ) -> PathPaymentOperationBuilder {
45        PathPaymentOperationBuilder::new(destination, send_asset, send_max, dest_asset, dest_amount)
46    }
47
48    /// Build a [`ManageOfferOperation`](struct.ManageOfferOperation.html).
49    pub fn manage_offer(
50        selling: Asset,
51        buying: Asset,
52        amount: Amount,
53        price: Price,
54    ) -> ManageOfferOperationBuilder {
55        ManageOfferOperationBuilder::new(selling, buying, amount, price)
56    }
57
58    /// Build a [`CreatePassiveOfferOperation`](struct.CreatePassiveOfferOperation.html).
59    pub fn create_passive_offer(
60        selling: Asset,
61        buying: Asset,
62        amount: Amount,
63        price: Price,
64    ) -> CreatePassiveOfferOperationBuilder {
65        CreatePassiveOfferOperationBuilder::new(selling, buying, amount, price)
66    }
67
68    /// Build a [`ManageDataOperation`](struct.ManageDataOperation.html) setting the key `name` to `value`.
69    pub fn set_data(name: String, value: Vec<u8>) -> ManageDataOperationBuilder {
70        ManageDataOperationBuilder::set_data(name, value)
71    }
72
73    /// Build a [`ManageDataOperation`](struct.ManageDataOperation.html) removing key `name`.
74    pub fn delete_data(name: String) -> ManageDataOperationBuilder {
75        ManageDataOperationBuilder::delete_data(name)
76    }
77}
78
79/// `CreateAccountOperation` builder.
80#[derive(Debug, Clone)]
81pub struct CreateAccountOperationBuilder {
82    inner: CreateAccountOperation,
83}
84
85impl CreateAccountOperationBuilder {
86    /// Create with `destination` address and starting `balance`.
87    pub fn new(destination: PublicKey, balance: Amount) -> Self {
88        let inner = CreateAccountOperation {
89            source: None,
90            destination,
91            balance,
92        };
93        CreateAccountOperationBuilder { inner }
94    }
95
96    /// Set the operation `source`.
97    pub fn with_source(mut self, source: PublicKey) -> Self {
98        self.inner.source = Some(source);
99        self
100    }
101
102    /// Return the `Operation`.
103    pub fn build(self) -> Operation {
104        Operation::CreateAccount(self.inner)
105    }
106}
107
108/// `PaymentOperation` builder.
109#[derive(Debug, Clone)]
110pub struct PaymentOperationBuilder {
111    inner: PaymentOperation,
112}
113
114impl PaymentOperationBuilder {
115    /// Create payment of `amount` units of `asset` to `destination` address.
116    pub fn new(destination: PublicKey, asset: Asset, amount: Amount) -> Self {
117        let inner = PaymentOperation {
118            source: None,
119            destination,
120            asset,
121            amount,
122        };
123        PaymentOperationBuilder { inner }
124    }
125
126    /// Set the operation `source`.
127    pub fn with_source(mut self, source: PublicKey) -> Self {
128        self.inner.source = Some(source);
129        self
130    }
131
132    /// Return the `Operation`.
133    pub fn build(self) -> Operation {
134        Operation::Payment(self.inner)
135    }
136}
137
138/// `PathPaymentOperation` builder.
139#[derive(Debug, Clone)]
140pub struct PathPaymentOperationBuilder {
141    inner: PathPaymentOperation,
142}
143
144impl PathPaymentOperationBuilder {
145    /// TODO
146    pub fn new(
147        destination: PublicKey,
148        send_asset: Asset,
149        send_max: Amount,
150        dest_asset: Asset,
151        dest_amount: Amount,
152    ) -> PathPaymentOperationBuilder {
153        let inner = PathPaymentOperation {
154            source: None,
155            destination,
156            send_asset,
157            send_max,
158            dest_asset,
159            dest_amount,
160            path: Vec::new(),
161        };
162        PathPaymentOperationBuilder { inner }
163    }
164
165    /// Set the operation `source`.
166    pub fn with_source(mut self, source: PublicKey) -> Self {
167        self.inner.source = Some(source);
168        self
169    }
170
171    /// Set the payment path.
172    pub fn with_path(mut self, path: Vec<Asset>) -> Self {
173        self.inner.path = path;
174        self
175    }
176
177    /// Push `asset` to the payment path.
178    pub fn push_asset(mut self, asset: Asset) -> Self {
179        self.inner.path.push(asset);
180        self
181    }
182
183    /// Return the `Operation`.
184    pub fn build(self) -> Operation {
185        Operation::PathPayment(self.inner)
186    }
187}
188
189/// `ManageOfferOperation` builder.
190#[derive(Debug, Clone)]
191pub struct ManageOfferOperationBuilder {
192    inner: ManageOfferOperation,
193}
194
195impl ManageOfferOperationBuilder {
196    /// TODO
197    pub fn new(
198        selling: Asset,
199        buying: Asset,
200        amount: Amount,
201        price: Price,
202    ) -> ManageOfferOperationBuilder {
203        let inner = ManageOfferOperation {
204            source: None,
205            selling,
206            buying,
207            amount,
208            price,
209            offer_id: 0,
210        };
211        ManageOfferOperationBuilder { inner }
212    }
213
214    /// Set the operation `source`.
215    pub fn with_source(mut self, source: PublicKey) -> Self {
216        self.inner.source = Some(source);
217        self
218    }
219
220    /// Set the offer `id`.
221    pub fn with_offer_id(mut self, id: u64) -> Self {
222        self.inner.offer_id = id;
223        self
224    }
225
226    /// Return the `Operation`.
227    pub fn build(self) -> Operation {
228        Operation::ManageOffer(self.inner)
229    }
230}
231
232/// `CreatePassiveOfferOperation` builder.
233#[derive(Debug, Clone)]
234pub struct CreatePassiveOfferOperationBuilder {
235    inner: CreatePassiveOfferOperation,
236}
237
238impl CreatePassiveOfferOperationBuilder {
239    /// TODO
240    pub fn new(
241        selling: Asset,
242        buying: Asset,
243        amount: Amount,
244        price: Price,
245    ) -> CreatePassiveOfferOperationBuilder {
246        let inner = CreatePassiveOfferOperation {
247            source: None,
248            selling,
249            buying,
250            amount,
251            price,
252        };
253        CreatePassiveOfferOperationBuilder { inner }
254    }
255
256    /// Set the operation `source`.
257    pub fn with_source(mut self, source: PublicKey) -> Self {
258        self.inner.source = Some(source);
259        self
260    }
261
262    /// Return the `Operation`.
263    pub fn build(self) -> Operation {
264        Operation::CreatePassiveOffer(self.inner)
265    }
266}
267
268/// `ManageDataOperation` build
269#[derive(Debug, Clone)]
270pub struct ManageDataOperationBuilder {
271    inner: ManageDataOperation,
272}
273
274impl ManageDataOperationBuilder {
275    /// Create a new operation to set account data `name` to `value`.
276    pub fn set_data(name: String, value: Vec<u8>) -> Self {
277        let inner = ManageDataOperation {
278            source: None,
279            name,
280            value: Some(value),
281        };
282        ManageDataOperationBuilder { inner }
283    }
284
285    /// Create a new operation to delete `name` from the account data.
286    pub fn delete_data(name: String) -> Self {
287        let inner = ManageDataOperation {
288            source: None,
289            name,
290            value: None,
291        };
292        ManageDataOperationBuilder { inner }
293    }
294
295    /// Set the operation `source`.
296    pub fn with_source(mut self, source: PublicKey) -> Self {
297        self.inner.source = Some(source);
298        self
299    }
300
301    /// Return the `Operation`.
302    pub fn build(self) -> Operation {
303        Operation::ManageData(self.inner)
304    }
305}
306
307/// `InflationOperation` builder.
308#[derive(Debug, Clone)]
309pub struct InflationOperationBuilder {
310    inner: InflationOperation,
311}
312
313impl InflationOperationBuilder {
314    /// Create a new inflation operation.
315    pub fn new() -> Self {
316        let inner = InflationOperation { source: None };
317        InflationOperationBuilder { inner }
318    }
319
320    /// Set the operation `source`.
321    pub fn with_source(mut self, source: PublicKey) -> Self {
322        self.inner.source = Some(source);
323        self
324    }
325
326    /// Return the `Operation`.
327    pub fn build(self) -> Operation {
328        Operation::Inflation(self.inner)
329    }
330}