tronic 0.3.4

A modular, async-first Rust client for the Tron blockchain.
Documentation
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
use std::collections::HashMap;

use anyhow::anyhow;
use http::Uri;

use crate::Result;
use crate::client::Auth;
use crate::contracts::AbiEncode;
use crate::domain::address::TronAddress;
use crate::domain::trx::{self, Trx};
use crate::domain::{self, Hash32};
use crate::error::Error;
use crate::protocol;
use crate::protocol::wallet_client::WalletClient;
use crate::provider::grpc::middleware::auth_channel;

#[derive(Clone)]
pub struct GrpcProvider {
    channel: middleware::AuthChannel,
}

impl GrpcProvider {
    pub async fn new(node_uri: Uri, auth: Auth) -> Result<Self> {
        let scheme = node_uri.scheme().cloned();

        #[allow(unused_mut)]
        let mut builder = tonic::transport::Channel::builder(node_uri);

        #[cfg(not(feature = "tonic-tls"))]
        if scheme.is_some_and(|s| s.eq(&http::uri::Scheme::HTTPS)) {
            return Err(Error::Unexpected(anyhow!(
                "enable tonic-tls feature to use https"
            )));
        }
        #[cfg(feature = "tonic-tls")]
        if scheme.is_some_and(|s| s.eq(&http::uri::Scheme::HTTPS)) {
            let _ = rustls::crypto::CryptoProvider::install_default(
                rustls::crypto::ring::default_provider(),
            );

            builder = builder.tls_config(
                tonic::transport::ClientTlsConfig::new().with_native_roots(),
            )?;
        }
        let channel = builder.connect().await?;
        let channel = auth_channel(
            channel,
            match auth {
                Auth::Bearer { name, secret } => Some(middleware::BHAuth {
                    bearer_name: name.parse()?,
                    bearer_secret: secret,
                }),
                Auth::None => None,
            },
        );
        Ok(Self { channel })
    }
    fn wallet_client(&self) -> WalletClient<middleware::AuthChannel> {
        WalletClient::new(self.channel.clone())
    }
    fn return_to_result(ret: Option<protocol::Return>) -> Result<()> {
        if let Some(protocol::Return {
            result: false,
            code,
            message,
        }) = ret
        {
            Err(anyhow!(
                "failed: {}, code: {:#?}",
                String::from_utf8_lossy(&message),
                protocol::r#return::ResponseCode::try_from(code).unwrap(),
            )
            .into())
        } else {
            Ok(())
        }
    }
}

#[async_trait::async_trait]
impl crate::provider::TronProvider for GrpcProvider {
    async fn trasnfer_contract(
        &self,
        owner: domain::address::TronAddress,
        to: domain::address::TronAddress,
        amount: trx::Trx,
    ) -> Result<domain::transaction::TransactionExtention> {
        let grpc_transfer_contract = protocol::TransferContract {
            owner_address: owner.as_bytes().to_vec(),
            to_address: to.as_bytes().to_vec(),
            amount: amount.to_sun(),
        };
        let mut node = self.wallet_client();
        let txext = node
            .create_transaction2(grpc_transfer_contract)
            .await?
            .into_inner();
        if txext.txid.is_empty() {
            if let Some(ref r) = txext.result
                && r.message == b"Contract validate error : Validate TransferContract error, no OwnerAccount."
            {
                return Err(Error::NoAccount(owner))
            }
            Err(Error::Unexpected(anyhow!(
                "txid is empty: {}",
                String::from_utf8_lossy(
                    &txext.result.unwrap_or_default().message
                )
            )))
        } else {
            Ok(txext.into())
        }
    }
    async fn trigger_smart_contract<A: AbiEncode + Send>(
        &self,
        owner: TronAddress,
        contract: TronAddress,
        call: A,
    ) -> Result<domain::transaction::TransactionExtention> {
        let contract = protocol::TriggerSmartContract {
            owner_address: owner.as_bytes().to_vec(),
            contract_address: contract.as_bytes().to_vec(),
            data: call.encode(),
            ..Default::default()
        };

        let mut node = self.wallet_client();
        let reply = node
            .trigger_contract(contract)
            .await
            .map(|r| r.into_inner())?;
        Self::return_to_result(reply.result.clone())?;
        Ok(reply.into())
    }
    async fn broadcast_transaction(
        &self,
        transaction: domain::transaction::Transaction,
    ) -> Result<()> {
        let mut node = WalletClient::new(self.channel.clone());
        let transaction: protocol::Transaction = transaction.into();
        let response =
            node.broadcast_transaction(transaction).await?.into_inner();
        Self::return_to_result(Some(response))?;
        Ok(())
    }
    async fn estimate_energy(
        &self,
        contract: domain::contract::TriggerSmartContract,
    ) -> Result<i64> {
        let mut node = self.wallet_client();
        let contract: protocol::TriggerSmartContract = contract.into();
        let msg = node.estimate_energy(contract).await?.into_inner();
        Self::return_to_result(msg.result.clone())?;
        Ok(msg.energy_required)
    }
    async fn get_account(
        &self,
        address: TronAddress,
    ) -> Result<domain::account::Account> {
        let mut node = self.wallet_client();
        let account = protocol::Account {
            address: address.as_bytes().to_vec(),
            ..Default::default()
        };
        let account: domain::account::Account =
            node.get_account(account).await?.into_inner().into();
        Ok(account)
    }
    async fn get_account_resources(
        &self,
        address: TronAddress,
    ) -> Result<domain::account::AccountResourceUsage> {
        let mut node = self.wallet_client();
        let account = protocol::Account {
            address: address.as_bytes().to_vec(),
            ..Default::default()
        };
        let account_resource =
            node.get_account_resource(account).await?.into_inner();
        Ok(account_resource.into())
    }
    async fn trigger_constant_contract(
        &self,
        contract: domain::contract::TriggerSmartContract,
    ) -> Result<domain::transaction::TransactionExtention> {
        let mut node = self.wallet_client();
        let contract: protocol::TriggerSmartContract = contract.into();
        let txext = node
            .trigger_constant_contract(contract)
            .await
            .map(|r| r.into_inner())?;
        Self::return_to_result(txext.result.clone())?;
        Ok(txext.into())
    }
    async fn get_now_block(&self) -> Result<domain::block::BlockExtention> {
        let mut node = self.wallet_client();
        let now_block = node
            .get_now_block2(protocol::EmptyMessage::default())
            .await?
            .into_inner();
        Ok(now_block.into())
    }
    async fn account_permission_update(
        &self,
        contract: domain::contract::AccountPermissionUpdateContract,
    ) -> Result<domain::transaction::TransactionExtention> {
        let mut node = self.wallet_client();
        let contract: protocol::AccountPermissionUpdateContract =
            contract.into();
        let txext =
            node.account_permission_update(contract).await?.into_inner();
        Self::return_to_result(txext.result.clone())?;
        Ok(txext.into())
    }
    async fn get_transaction_by_id(
        &self,
        txid: Hash32,
    ) -> Result<domain::transaction::Transaction> {
        let mut node = self.wallet_client();
        let transaction = node
            .get_transaction_by_id(protocol::BytesMessage::from(txid))
            .await?
            .into_inner();
        Ok(transaction.into())
    }
    async fn get_transaction_info(
        &self,
        txid: Hash32,
    ) -> Result<domain::transaction::TransactionInfo> {
        let mut node = self.wallet_client();
        let transaction = node
            .get_transaction_info_by_id(protocol::BytesMessage::from(txid))
            .await?
            .into_inner();
        Ok(transaction.into())
    }
    async fn chain_parameters(&self) -> Result<HashMap<String, i64>> {
        let mut node = WalletClient::new(self.channel.clone());
        let chain_parameters = node
            .get_chain_parameters(protocol::EmptyMessage::default())
            .await?
            .into_inner()
            .chain_parameter
            .into_iter()
            .map(|ch_p| (ch_p.key, ch_p.value))
            .collect::<HashMap<_, _>>();
        Ok(chain_parameters)
    }
    async fn freeze_balance(
        &self,
        contract: domain::contract::FreezeBalanceV2Contract,
    ) -> Result<domain::transaction::TransactionExtention> {
        let mut node = WalletClient::new(self.channel.clone());
        let contract: protocol::FreezeBalanceV2Contract = contract.into();
        let txext = node.freeze_balance_v2(contract).await?.into_inner();
        Self::return_to_result(txext.result.clone())?;
        Ok(txext.into())
    }
    async fn unfreeze_balance(
        &self,
        contract: domain::contract::UnfreezeBalanceV2Contract,
    ) -> Result<domain::transaction::TransactionExtention> {
        let mut node = WalletClient::new(self.channel.clone());
        let contract: protocol::UnfreezeBalanceV2Contract = contract.into();
        let txext = node.unfreeze_balance_v2(contract).await?.into_inner();
        Self::return_to_result(txext.result.clone())?;
        Ok(txext.into())
    }
    async fn get_reward(&self, address: TronAddress) -> Result<Trx> {
        let mut node = WalletClient::new(self.channel.clone());
        let number = node
            .get_reward_info(protocol::BytesMessage {
                value: address.as_bytes().to_vec(),
            })
            .await?
            .into_inner();
        Ok(number.num.into())
    }
    async fn get_delegated_resource(
        &self,
        from_address: TronAddress,
        to_address: TronAddress,
    ) -> Result<Vec<domain::account::DelegatedResource>> {
        let mut node = WalletClient::new(self.channel.clone());
        let list = node
            .get_delegated_resource_v2(protocol::DelegatedResourceMessage {
                from_address: from_address.as_bytes().to_vec(),
                to_address: to_address.as_bytes().to_vec(),
            })
            .await?
            .into_inner()
            .delegated_resource;
        Ok(list.into_iter().map(Into::into).collect())
    }
    async fn get_delegated_resource_account(
        &self,
        address: TronAddress,
    ) -> Result<domain::account::DelegatedResourceAccountIndex> {
        let mut node = WalletClient::new(self.channel.clone());
        let index = node
            .get_delegated_resource_account_index_v2(protocol::BytesMessage {
                value: address.as_bytes().to_vec(),
            })
            .await?
            .into_inner();
        Ok(index.into())
    }
}

pub mod middleware {
    use http::HeaderName;
    use secrecy::SecretString;
    use tonic::transport::Channel;
    use tower::ServiceBuilder;

    pub use service::AuthChannel;

    #[derive(Clone)]
    pub struct BHAuth {
        pub bearer_name: HeaderName,
        pub bearer_secret: SecretString,
    }

    pub fn auth_channel(channel: Channel, auth: Option<BHAuth>) -> AuthChannel {
        ServiceBuilder::new()
            .layer(service::AuthChLayer::new(auth))
            .service(channel)
    }

    mod service {
        use http::{HeaderValue, Request, Response};
        use secrecy::ExposeSecret;
        use std::future::Future;
        use std::pin::Pin;
        use std::task::{Context, Poll};
        use tonic::body::Body;
        use tonic::transport::Channel;
        use tower::{Layer, Service};

        use super::BHAuth;

        #[derive(Clone)]
        pub struct AuthChannel {
            inner: Channel,
            info: Option<BHAuth>,
        }

        pub struct AuthChLayer {
            info: Option<BHAuth>,
        }

        impl AuthChLayer {
            pub fn new(info: Option<BHAuth>) -> AuthChLayer {
                Self { info }
            }
        }

        impl Layer<Channel> for AuthChLayer {
            type Service = AuthChannel;
            fn layer(&self, inner: Channel) -> Self::Service {
                AuthChannel {
                    inner,
                    info: self.info.clone(),
                }
            }
        }

        impl Service<Request<Body>> for AuthChannel {
            type Response = Response<Body>;
            type Error = Box<dyn std::error::Error + Send + Sync>;
            type Future = Pin<
                Box<
                    dyn Future<Output = Result<Self::Response, Self::Error>>
                        + Send,
                >,
            >;

            fn poll_ready(
                &mut self,
                cx: &mut Context<'_>,
            ) -> Poll<Result<(), Self::Error>> {
                self.inner.poll_ready(cx).map_err(Into::into)
            }

            fn call(&mut self, mut req: Request<Body>) -> Self::Future {
                // See: https://docs.rs/tower/latest/tower/trait.Service.html#be-careful-when-cloning-inner-services
                let clone = self.inner.clone();
                let mut inner = std::mem::replace(&mut self.inner, clone);
                let info = self.info.clone();

                Box::pin(async move {
                    if let Some(BHAuth {
                        bearer_name,
                        bearer_secret,
                    }) = info
                    {
                        match HeaderValue::from_str(
                            bearer_secret.expose_secret(),
                        ) {
                            Ok(secret) => {
                                req.headers_mut().insert(bearer_name, secret);
                            }
                            Err(e) => tracing::error!(
                                "failed to authorize grpc request: {e}"
                            ),
                        }
                    }
                    let response = inner.call(req).await?;
                    Ok::<_, Box<dyn std::error::Error + Send + Sync>>(response)
                })
            }
        }
    }
}