gsdk/
events.rs

1// This file is part of Gear.
2//
3// Copyright (C) 2021-2025 Gear Technologies Inc.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Events api
20
21use crate::{
22    Api, AsGear,
23    config::GearConfig,
24    gear::{self},
25    result::Result,
26};
27use subxt::{OnlineClient, blocks::ExtrinsicEvents as TxEvents, tx::TxInBlock};
28
29impl Api {
30    /// Capture the dispatch info of any extrinsic and display the weight spent
31    pub async fn capture_dispatch_info(
32        &self,
33        tx: &TxInBlock<GearConfig, OnlineClient<GearConfig>>,
34    ) -> Result<TxEvents<GearConfig>> {
35        let events = tx.fetch_events().await?;
36
37        for ev in events.iter() {
38            if let gear::Event::System(system_event) = ev?.as_gear()? {
39                let extrinsic_result = match system_event {
40                    gear::system::Event::ExtrinsicFailed {
41                        dispatch_error,
42                        dispatch_info,
43                    } => Some((dispatch_info, Err(self.decode_error(dispatch_error)))),
44                    gear::system::Event::ExtrinsicSuccess { dispatch_info } => {
45                        Some((dispatch_info, Ok(())))
46                    }
47                    _ => None,
48                };
49
50                if let Some((dispatch_info, result)) = extrinsic_result {
51                    log::info!("	Weight cost: {:?}", dispatch_info.weight);
52                    result?;
53                    break;
54                }
55            }
56        }
57
58        Ok(events)
59    }
60}