Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split compose_call into two functions #706

Merged
merged 7 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions compose-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ maybe-async = { version = "0.2.7" }
# local
ac-primitives = { path = "../primitives", default-features = false }

[dev-dependencies]
ac-node-api = { path = "../node-api" }
frame-metadata = { version = "16.0" }
codec = { package = "parity-scale-codec", version = "3.6.1" }


[features]
default = ["std", "sync-api"]
sync-api = ["maybe-async/is_sync"]
Expand Down
57 changes: 53 additions & 4 deletions compose-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,25 @@ mod rpc;
macro_rules! compose_call {
($node_metadata: expr, $pallet: expr, $call_name: expr $(, $args: expr) *) => {
{
let pallet = $node_metadata.pallet_by_name($pallet).unwrap().to_owned();

let call_index = pallet.call_variant_by_name($call_name).unwrap().index;
let pallet_metadata = $node_metadata.pallet_by_name($pallet).unwrap().to_owned();
$crate::compose_call_for_pallet_metadata!(pallet_metadata, $call_name $(, ($args)) *)
}
};
}

([pallet.index(), call_index as u8] $(, ($args)) *)
/// Generates the extrinsic's call field for the given PalletMetadata
/// # Arguments
///
/// * 'pallet_metadata' - This crate's parsed pallet metadata as field of the API.
/// * 'call_name' - 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_for_pallet_metadata {
($pallet_metadata: expr, $call_name: expr $(, $args: expr) *) => {
{
let call_index = $pallet_metadata.call_variant_by_name($call_name).unwrap().index;
([$pallet_metadata.index(), call_index as u8] $(, ($args)) *)
}
};
}
Expand Down Expand Up @@ -143,3 +157,38 @@ macro_rules! compose_extrinsic {
}
};
}

#[cfg(test)]
mod tests {
use super::*;
use ac_node_api::Metadata;
use codec::Decode;
use frame_metadata::RuntimeMetadataPrefixed;
use std::fs;

#[test]
fn macro_compose_call_for_pallet_metadata_works() {
let encoded_metadata = fs::read("../ksm_metadata_v14.bin").unwrap();
let runtime_metadata_prefixed =
RuntimeMetadataPrefixed::decode(&mut encoded_metadata.as_slice()).unwrap();
let metadata = Metadata::try_from(runtime_metadata_prefixed).unwrap();

let pallet_metadata = metadata.pallet_by_name("Balances").unwrap();

let extra_parameter = 10000;
let expected_call_one = ([4, 0], extra_parameter);
let call_one = compose_call_for_pallet_metadata!(
&pallet_metadata,
"transfer_allow_death",
extra_parameter
);
assert_eq!(expected_call_one, call_one);
let expected_call_two = ([4, 8], extra_parameter);
let call_two = compose_call_for_pallet_metadata!(
&pallet_metadata,
"force_set_balance",
extra_parameter
);
assert_eq!(expected_call_two, call_two);
}
}
Loading