m10_sdk/builders/
transfer.rs

1use crate::account::AccountId;
2use crate::builders::{TxnFilter, DEFAULT_TXN_LIMIT};
3use crate::WithContext;
4use core::convert::From;
5use core::default::Default;
6use m10_protos::prost::Any;
7use m10_protos::sdk::list_transfer_request::Filter;
8use m10_protos::{sdk, Metadata};
9
10pub struct StepBuilder {
11    from: AccountId,
12    to: AccountId,
13    amount: u64,
14    metadata: Vec<Any>,
15}
16
17impl StepBuilder {
18    pub fn new(from: AccountId, to: AccountId, amount: u64) -> Self {
19        Self {
20            from,
21            to,
22            amount,
23            metadata: vec![],
24        }
25    }
26
27    pub fn metadata(mut self, value: impl Metadata) -> Self {
28        self.metadata.push(value.any());
29        self
30    }
31
32    pub fn any_metadata(mut self, value: Any) -> Self {
33        self.metadata.push(value);
34        self
35    }
36
37    pub fn custom_metadata(
38        mut self,
39        type_url: impl Into<String>,
40        payload: impl Into<Vec<u8>>,
41    ) -> Self {
42        self.metadata.push(Any {
43            type_url: type_url.into(),
44            value: payload.into(),
45        });
46        self
47    }
48}
49
50impl From<StepBuilder> for sdk::TransferStep {
51    fn from(step: StepBuilder) -> Self {
52        Self {
53            from_account_id: step.from.to_vec(),
54            to_account_id: step.to.to_vec(),
55            amount: step.amount,
56            metadata: step.metadata,
57        }
58    }
59}
60
61#[derive(Default)]
62pub struct TransferBuilder(Vec<StepBuilder>);
63
64impl TransferBuilder {
65    pub fn new() -> Self {
66        Self::default()
67    }
68
69    pub fn step(mut self, step: StepBuilder) -> Self {
70        self.0.push(step);
71        self
72    }
73}
74
75impl From<TransferBuilder> for sdk::CreateTransfer {
76    fn from(transfer: TransferBuilder) -> Self {
77        Self {
78            transfer_steps: transfer
79                .0
80                .into_iter()
81                .map(sdk::TransferStep::from)
82                .collect(),
83        }
84    }
85}
86
87pub struct TransferFilter {
88    filter: Filter,
89    include_child_accounts: bool,
90}
91
92impl TxnFilter<TransferFilter> {
93    pub fn by_account(id: AccountId) -> Self {
94        Self {
95            filter: TransferFilter {
96                filter: Filter::AccountId(id.to_vec()),
97                include_child_accounts: false,
98            },
99            min: 0,
100            max: u64::MAX,
101            limit: DEFAULT_TXN_LIMIT,
102        }
103    }
104
105    pub fn by_context_id(context_id: Vec<u8>) -> Self {
106        Self {
107            filter: TransferFilter {
108                filter: Filter::ContextId(context_id),
109                include_child_accounts: false,
110            },
111            min: 0,
112            max: u64::MAX,
113            limit: DEFAULT_TXN_LIMIT,
114        }
115    }
116
117    pub fn include_child_accounts(mut self, enable: bool) -> Self {
118        self.filter.include_child_accounts = enable;
119        self
120    }
121}
122
123impl From<TxnFilter<TransferFilter>> for sdk::ListTransferRequest {
124    fn from(filter: TxnFilter<TransferFilter>) -> Self {
125        sdk::ListTransferRequest {
126            filter: Some(filter.filter.filter),
127            min_tx_id: filter.min,
128            max_tx_id: filter.max,
129            include_child_accounts: filter.filter.include_child_accounts,
130            limit: filter.limit,
131        }
132    }
133}
134
135impl WithContext for TransferBuilder {}