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
// This file is part of Gear.
//
// Copyright (C) 2021-2024 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Utils

use super::Inner;
use crate::{
    backtrace::BacktraceStatus,
    config::GearConfig,
    metadata::{
        calls::SudoCall, sudo::Event as SudoEvent, vara_runtime::RuntimeCall, CallInfo, Event,
    },
    result::Result,
    signer::SignerRpc,
    TxInBlock, TxStatus,
};
use anyhow::anyhow;
use colored::Colorize;
use scale_value::Composite;
use sp_core::H256;
use std::sync::Arc;
use subxt::{
    blocks::ExtrinsicEvents,
    config::polkadot::PolkadotExtrinsicParamsBuilder,
    dynamic::Value,
    tx::{DynamicPayload, TxProgress},
    Error as SubxtError, OnlineClient,
};

type TxProgressT = TxProgress<GearConfig, OnlineClient<GearConfig>>;
pub type EventsResult = Result<(H256, ExtrinsicEvents<GearConfig>)>;

impl Inner {
    /// Logging balance spent
    pub async fn log_balance_spent(&self, before: u128) -> Result<()> {
        let signer_rpc = SignerRpc(Arc::new(self.clone()));
        match signer_rpc.get_balance().await {
            Ok(balance) => {
                let after = before.saturating_sub(balance);
                log::info!("\tBalance spent: {after}");
            }
            Err(e) => log::info!("\tAccount was removed from storage: {e}"),
        }

        Ok(())
    }

    /// Propagates log::info for given status.
    pub(crate) fn log_status(status: &TxStatus) {
        match status {
            TxStatus::Validated => log::info!("\tStatus: Validated"),
            TxStatus::Broadcasted { num_peers } => log::info!("\tStatus: Broadcast( {num_peers} )"),
            TxStatus::NoLongerInBestBlock => log::info!("\tStatus: NoLongerInBestBlock"),
            TxStatus::InBestBlock(b) => log::info!(
                "\tStatus: InBestBlock( block hash: {}, extrinsic hash: {} )",
                b.block_hash(),
                b.extrinsic_hash()
            ),
            TxStatus::InFinalizedBlock(b) => log::info!(
                "\tStatus: Finalized( block hash: {}, extrinsic hash: {} )",
                b.block_hash(),
                b.extrinsic_hash()
            ),
            TxStatus::Error { message: e } => log::error!("\tStatus: Error( {e:?} )"),
            TxStatus::Dropped { message: e } => log::error!("\tStatus: Dropped( {e:?} )"),
            TxStatus::Invalid { message: e } => log::error!("\tStatus: Invalid( {e:?} )"),
        }
    }

    /// Listen transaction process and print logs.
    pub async fn process<'a>(&self, tx: DynamicPayload) -> Result<TxInBlock> {
        use subxt::tx::TxStatus::*;

        let signer_rpc = SignerRpc(Arc::new(self.clone()));
        let before = signer_rpc.get_balance().await?;

        let mut process = self.sign_and_submit_then_watch(&tx).await?;
        let (pallet, name) = (tx.pallet_name(), tx.call_name());
        let extrinsic = format!("{pallet}::{name}").magenta().bold();

        log::info!("Pending {extrinsic} ...");
        let mut queue: Vec<BacktraceStatus> = Default::default();
        let mut hash: Option<H256> = None;

        while let Some(status) = process.next().await {
            let status = status?;
            Self::log_status(&status);

            if let Some(h) = &hash {
                self.backtrace
                    .clone()
                    .append(*h, BacktraceStatus::from(&status));
            } else {
                queue.push((&status).into());
            }

            match status {
                Validated | Broadcasted { .. } | NoLongerInBestBlock => (),
                InBestBlock(b) => {
                    hash = Some(b.extrinsic_hash());
                    self.backtrace.append(
                        b.extrinsic_hash(),
                        BacktraceStatus::InBestBlock {
                            block_hash: b.block_hash(),
                            extrinsic_hash: b.extrinsic_hash(),
                        },
                    );
                }
                InFinalizedBlock(b) => {
                    log::info!("Submitted {extrinsic} !");
                    log::info!("\tBlock Hash: {:?}", b.block_hash());
                    log::info!("\tTransaction Hash: {:?}", b.extrinsic_hash());
                    self.log_balance_spent(before).await?;
                    return Ok(b);
                }
                _ => {
                    self.log_balance_spent(before).await?;
                    return Err(status.into());
                }
            }
        }

        Err(anyhow!("Transaction wasn't found").into())
    }

    /// Process sudo transaction.
    pub async fn process_sudo(&self, tx: DynamicPayload) -> EventsResult {
        let tx = self.process(tx).await?;
        let events = tx.wait_for_success().await?;
        for event in events.iter() {
            let event = event?.as_root_event::<Event>()?;
            if let Event::Sudo(SudoEvent::Sudid {
                sudo_result: Err(err),
            }) = event
            {
                return Err(self.api().decode_error(err).into());
            }
        }

        Ok((tx.block_hash(), events))
    }

    /// Run transaction.
    ///
    /// This function allows us to execute any transactions in gear.
    ///
    /// # You may not need this.
    ///
    /// Read the docs of [`Signer`](`super::Signer`) to checkout the wrappred transactions,
    /// we need this function only when we want to execute a transaction
    /// which has not been wrapped in `gsdk`.
    ///
    /// # Example
    ///
    /// ```ignore
    /// use gsdk::{
    ///   Api,
    ///   Signer,
    ///   metadata::calls::BalancesCall,
    ///   Value,
    /// };
    ///
    /// let api = Api::new(None).await?;
    /// let signer = Signer::new(api, "//Alice", None).await?;
    ///
    /// {
    ///     let args = vec![
    ///         Value::unnamed_variant("Id", [Value::from_bytes(dest.into())]),
    ///         Value::u128(value),
    ///     ];
    ///     let in_block = signer.run_tx(BalancesCall::TransferKeepAlive, args).await?;
    /// }
    ///
    /// // The code above euqals to:
    ///
    /// {
    ///    let in_block = signer.calls.transfer_keep_alive(dest, value).await?;
    /// }
    ///
    /// // ...
    /// ```
    pub async fn run_tx<'a, Call: CallInfo>(
        &self,
        call: Call,
        fields: impl Into<Composite<()>>,
    ) -> Result<TxInBlock> {
        let tx = subxt::dynamic::tx(Call::PALLET, call.call_name(), fields.into());

        self.process(tx).await
    }

    /// Run transaction with sudo.
    pub async fn sudo_run_tx<'a, Call: CallInfo>(
        &self,
        call: Call,
        fields: impl Into<Composite<()>>,
    ) -> EventsResult {
        let tx = subxt::dynamic::tx(Call::PALLET, call.call_name(), fields.into());

        self.process_sudo(tx).await
    }

    /// `pallet_sudo::sudo`
    pub async fn sudo(&self, call: RuntimeCall) -> EventsResult {
        self.sudo_run_tx(SudoCall::Sudo, vec![Value::from(call)])
            .await
    }

    /// Wrapper for submit and watch with nonce.
    async fn sign_and_submit_then_watch<'a>(
        &self,
        tx: &DynamicPayload,
    ) -> Result<TxProgressT, SubxtError> {
        if let Some(nonce) = self.nonce {
            self.api
                .tx()
                .create_signed(
                    tx,
                    &self.signer,
                    PolkadotExtrinsicParamsBuilder::new().nonce(nonce).build(),
                )
                .await?
                .submit_and_watch()
                .await
        } else {
            self.api
                .tx()
                .sign_and_submit_then_watch_default(tx, &self.signer)
                .await
        }
    }
}