-
Notifications
You must be signed in to change notification settings - Fork 123
/
mod.rs
144 lines (126 loc) · 4.83 KB
/
mod.rs
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
/*
Copyright 2019 Supercomputing Systems AG
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//! Offers macros that build extrinsics for custom runtime modules based on the metadata.
//! Additionally, some predefined extrinsics for common runtime modules are implemented.
#[cfg(feature = "std")]
pub extern crate codec;
#[cfg(feature = "std")]
pub extern crate log;
#[cfg(feature = "std")]
pub mod balances;
#[cfg(feature = "std")]
pub mod contract;
pub mod xt_primitives;
/// Generates the extrinsic's call field for a given module and call passed as &str
/// # Arguments
///
/// * 'node_metadata' - This crate's parsed node metadata as field of the API.
/// * 'module' - Module name as &str for which the call is composed.
/// * 'call' - Call name as &str
/// * 'args' - Optional sequence of arguments of the call. They are not checked against the metadata.
/// As of now the user needs to check himself that the correct arguments are supplied.
#[macro_export]
macro_rules! compose_call {
($node_metadata: expr, $module: expr, $call_name: expr $(, $args: expr) *) => {
{
let module = $node_metadata.module_with_calls($module).unwrap().to_owned();
let call_index = module.calls.get($call_name).unwrap();
([module.index, *call_index as u8] $(, ($args)) *)
}
};
}
/// Generates an Unchecked extrinsic for a given call
/// # Arguments
///
/// * 'signer' - AccountKey that is used to sign the extrinsic.
/// * 'call' - call as returned by the compose_call! macro or via substrate's call enums.
/// * 'nonce' - signer's account nonce: u32
/// * 'era' - Era for extrinsic to be valid
/// * 'genesis_hash' - sp-runtime::Hash256/[u8; 32].
/// * 'runtime_spec_version' - RuntimeVersion.spec_version/u32
#[macro_export]
macro_rules! compose_extrinsic_offline {
($signer: expr,
$call: expr,
$nonce: expr,
$era: expr,
$genesis_hash: expr,
$genesis_or_current_hash: expr,
$runtime_spec_version: expr) => {{
use sp_runtime::generic::Era;
use $crate::extrinsic::xt_primitives::*;
let extra = GenericExtra::new($era, $nonce);
let raw_payload = SignedPayload::from_raw(
$call.clone(),
extra.clone(),
(
$runtime_spec_version,
$genesis_hash,
$genesis_or_current_hash,
(),
(),
(),
(),
),
);
let signature = raw_payload.using_encoded(|payload| $signer.sign(payload));
let mut arr: [u8; 32] = Default::default();
arr.clone_from_slice($signer.public().as_ref());
UncheckedExtrinsicV4::new_signed(
$call,
GenericAddress::from(AccountId::from(arr)),
signature.into(),
extra,
)
}};
}
/// Generates an Unchecked extrinsic for a given module and call passed as a &str.
/// # Arguments
///
/// * 'api' - This instance of API. If the *signer* field is not set, an unsigned extrinsic will be generated.
/// * 'module' - Module name as &str for which the call is composed.
/// * 'call' - Call name as &str
/// * 'args' - Optional sequence of arguments of the call. They are not checked against the metadata.
/// As of now the user needs to check himself that the correct arguments are supplied.
#[macro_export]
#[cfg(feature = "std")]
macro_rules! compose_extrinsic {
($api: expr,
$module: expr,
$call: expr
$(, $args: expr) *) => {
{
use $crate::extrinsic::codec::Compact;
use $crate::extrinsic::log::info;
use $crate::extrinsic::xt_primitives::*;
info!("Composing generic extrinsic for module {:?} and call {:?}", $module, $call);
let call = $crate::compose_call!($api.metadata.clone(), $module, $call $(, ($args)) *);
if let Some(signer) = $api.signer.clone() {
$crate::compose_extrinsic_offline!(
signer,
call.clone(),
$api.get_nonce().unwrap(),
Era::Immortal,
$api.genesis_hash,
$api.genesis_hash,
$api.runtime_version.spec_version
)
} else {
UncheckedExtrinsicV4 {
signature: None,
function: call.clone(),
}
}
}
};
}