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
//! Dispatches calls internally, charging fees to the multisig account.
//!
//! ## Overview
//!
//! This module employs a custom `MultisigInternalOrigin` to ensure calls originate
//! from the multisig account itself, automating fee payments. The `dispatch_call` function
//! includes pre and post dispatch handling for streamlined fee management within the multisig context.

use crate::{
    fee_handling::{FeeAsset, MultisigFeeHandler},
    origin::{INV4Origin, MultisigInternalOrigin},
    Config, Error,
};
use frame_support::{dispatch::GetDispatchInfo, pallet_prelude::*};

use sp_runtime::traits::Dispatchable;

/// Dispatch a call executing pre/post dispatch for proper fee handling.
pub fn dispatch_call<T: Config>(
    core_id: <T as Config>::CoreId,
    fee_asset: &FeeAsset,
    call: <T as Config>::RuntimeCall,
) -> DispatchResultWithPostInfo
where
    T::AccountId: From<[u8; 32]>,
{
    // Create new custom origin as the multisig.
    let internal_origin = MultisigInternalOrigin::new(core_id);
    let multisig_account = internal_origin.to_account_id();
    let origin = INV4Origin::Multisig(internal_origin).into();

    let info = call.get_dispatch_info();
    let len = call.encode().len();

    // Execute pre dispatch using the multisig account instead of the extrinsic caller.
    let pre = <T::FeeCharger as MultisigFeeHandler<T>>::pre_dispatch(
        fee_asset,
        &multisig_account,
        &call,
        &info,
        len,
    )
    .map_err(|_| Error::<T>::CallFeePaymentFailed)?;

    let dispatch_result = call.dispatch(origin);

    let post = match dispatch_result {
        Ok(p) => p,
        Err(e) => e.post_info,
    };

    <T::FeeCharger as MultisigFeeHandler<T>>::post_dispatch(
        fee_asset,
        Some(pre),
        &info,
        &post,
        len,
        &dispatch_result.map(|_| ()).map_err(|e| e.error),
    )
    .map_err(|_| Error::<T>::CallFeePaymentFailed)?;

    dispatch_result
}