Skip to content

Commit

Permalink
Split compose_call into two functions (#706)
Browse files Browse the repository at this point in the history
* split compose_call

* add unit test for compose_call_for_pallet_metadata

* Add unit test to ac-compose-macro

* remove unused test import
  • Loading branch information
haerdib authored Feb 15, 2024
1 parent abcffd5 commit 541cd54
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
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);
}
}

0 comments on commit 541cd54

Please sign in to comment.