1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use anyhow::Result;
use diem_logger::prelude::warn;
use diem_sdk::{
    client::{Client, MethodRequest},
    transaction_builder::{Currency, TransactionFactory},
    types::{
        account_address::AccountAddress,
        account_config::{testnet_dd_account_address, treasury_compliance_account_address},
        chain_id::ChainId,
        transaction::{authenticator::AuthenticationKey, metadata, SignedTransaction},
        LocalAccount,
    },
};
use serde::Deserialize;
use std::{fmt, sync::Mutex};

#[derive(Debug)]
pub enum Response {
    DDAccountNextSeqNum(u64),
    SubmittedTxns(Vec<SignedTransaction>),
}

impl std::fmt::Display for Response {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Response::DDAccountNextSeqNum(v1) => write!(f, "{}", v1),
            Response::SubmittedTxns(v2) => {
                write!(f, "{}", hex::encode(bcs::to_bytes(&v2).unwrap()))
            }
        }
    }
}

#[derive(Deserialize)]
pub struct MintParams {
    pub amount: u64,
    pub currency_code: Currency,
    pub auth_key: AuthenticationKey,
    pub return_txns: Option<bool>,
    pub is_designated_dealer: Option<bool>,
    pub trade_id: Option<String>,
    pub vasp_domain: Option<String>,
    pub is_remove_domain: Option<bool>,
}

impl std::fmt::Display for MintParams {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{:?}", self.vasp_domain)
    }
}

impl MintParams {
    fn bcs_metadata(&mut self) -> Vec<u8> {
        match self.trade_id.take() {
            Some(trade_id) => {
                let metadata = metadata::Metadata::CoinTradeMetadata(
                    metadata::CoinTradeMetadata::CoinTradeMetadataV0(
                        metadata::CoinTradeMetadataV0 {
                            trade_ids: vec![trade_id],
                        },
                    ),
                );
                bcs::to_bytes(&metadata).unwrap_or_else(|e| {
                    warn!("Unable to serialize trade_id: {}", e);
                    vec![]
                })
            }
            _ => vec![],
        }
    }

    fn receiver(&self) -> AccountAddress {
        self.auth_key.derived_address()
    }
}

pub struct Service {
    treasury_account: Mutex<LocalAccount>,
    dd_account: Mutex<LocalAccount>,
    transaction_factory: TransactionFactory,
    client: Client,
}

impl Service {
    pub fn new(server_url: String, chain_id: ChainId, private_key_file: String) -> Self {
        let treasury_account = LocalAccount::new(
            treasury_compliance_account_address(),
            generate_key::load_key(&private_key_file),
            0,
        );
        let dd_account = LocalAccount::new(
            testnet_dd_account_address(),
            generate_key::load_key(private_key_file),
            0,
        );
        let client = Client::new(server_url);
        Service {
            treasury_account: Mutex::new(treasury_account),
            dd_account: Mutex::new(dd_account),
            transaction_factory: TransactionFactory::new(chain_id)
                .with_transaction_expiration_time(30),
            client,
        }
    }

    pub async fn process(&self, mut params: MintParams) -> Result<Response> {
        let (tc_seq, dd_seq, receiver_seq) = self.sequences(params.receiver()).await?;
        let txns = {
            let mut treasury_account = self.treasury_account.lock().unwrap();
            let mut dd_account = self.dd_account.lock().unwrap();

            // If the onchain sequence_number is greater than what we have, update our
            // sequence_numbers
            if tc_seq > treasury_account.sequence_number() {
                *treasury_account.sequence_number_mut() = tc_seq;
            }
            if dd_seq > dd_account.sequence_number() {
                *dd_account.sequence_number_mut() = tc_seq;
            }

            let mut txns = vec![];
            if receiver_seq.is_none() {
                let builder = if params.is_designated_dealer.unwrap_or(false) {
                    self.transaction_factory.create_designated_dealer(
                        params.currency_code,
                        0, // sliding_nonce
                        params.auth_key,
                        &format!("No. {} DD", treasury_account.sequence_number()),
                        false, // add all currencies
                    )
                } else {
                    self.transaction_factory.create_parent_vasp_account(
                        params.currency_code,
                        0, // sliding_nonce
                        params.auth_key,
                        &format!("No. {} VASP", treasury_account.sequence_number()),
                        false, // add all currencies
                    )
                };

                txns.push(treasury_account.sign_with_transaction_builder(builder));
            }

            if let (Some(ref vasp_domain), Some(is_remove_domain)) =
                (&params.vasp_domain, params.is_remove_domain)
            {
                let builder = if is_remove_domain {
                    self.transaction_factory.remove_vasp_domain(
                        params.receiver(),
                        vasp_domain.as_str().as_bytes().to_vec(),
                    )
                } else {
                    self.transaction_factory.add_vasp_domain(
                        params.receiver(),
                        vasp_domain.as_str().as_bytes().to_vec(),
                    )
                };
                txns.push(treasury_account.sign_with_transaction_builder(builder));
            }

            txns.push(dd_account.sign_with_transaction_builder(
                self.transaction_factory.peer_to_peer_with_metadata(
                    params.currency_code,
                    params.receiver(),
                    params.amount,
                    params.bcs_metadata(),
                    vec![],
                ),
            ));
            txns
        };

        let batch = txns
            .iter()
            .map(MethodRequest::submit)
            .collect::<Result<_, _>>()
            .expect("serialization should not fail");
        let response = self.client.batch(batch).await?;

        // If there was an issue submitting a transaction we should just reset our sequence_numbers
        // to what was on chain
        if response.iter().any(Result::is_err) {
            *self.treasury_account.lock().unwrap().sequence_number_mut() = tc_seq;
            *self.dd_account.lock().unwrap().sequence_number_mut() = dd_seq;
        }

        if params.return_txns.unwrap_or(false) {
            Ok(Response::SubmittedTxns(txns))
        } else {
            Ok(Response::DDAccountNextSeqNum(
                self.dd_account.lock().unwrap().sequence_number(),
            ))
        }
    }

    async fn sequences(&self, receiver: AccountAddress) -> Result<(u64, u64, Option<u64>)> {
        let accounts = vec![
            treasury_compliance_account_address(),
            testnet_dd_account_address(),
            receiver,
        ];
        let responses = self
            .client
            .batch(
                accounts
                    .into_iter()
                    .map(MethodRequest::get_account)
                    .collect(),
            )
            .await?
            .into_iter()
            .map(|r| r.map_err(anyhow::Error::new))
            .map(|r| r.map(|response| response.into_inner().unwrap_get_account()))
            .collect::<Result<Vec<_>>>()?;
        let treasury_compliance = responses
            .get(0)
            .as_ref()
            .ok_or_else(|| {
                anyhow::format_err!("get treasury compliance account response not found")
            })?
            .as_ref()
            .ok_or_else(|| anyhow::format_err!("treasury compliance account not found"))?
            .sequence_number;
        let designated_dealer = responses
            .get(1)
            .as_ref()
            .ok_or_else(|| anyhow::format_err!("get designated dealer account response not found"))?
            .as_ref()
            .ok_or_else(|| anyhow::format_err!("designated dealer account not found"))?
            .sequence_number;
        let receiver = responses
            .get(2)
            .as_ref()
            .ok_or_else(|| anyhow::format_err!("get receiver account response not found"))?
            .as_ref();
        let receiver_seq_num = receiver.map(|account| account.sequence_number);
        Ok((treasury_compliance, designated_dealer, receiver_seq_num))
    }
}