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

Error if mismatching # of args for instantiate/call #966

Merged
merged 2 commits into from
Feb 13, 2023
Merged
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
29 changes: 29 additions & 0 deletions crates/transcode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ impl ContractMessageTranscoder {
}
};

let args: Vec<_> = args.into_iter().collect();
if spec_args.len() != args.len() {
anyhow::bail!(
"Invalid number of input arguments: expected {}, {} provided",
spec_args.len(),
args.len()
)
}

let mut encoded = selector.to_bytes().to_vec();
for (spec, arg) in spec_args.iter().zip(args) {
let value = scon::parse_value(arg.as_ref())?;
Expand Down Expand Up @@ -503,6 +512,26 @@ mod tests {
Ok(())
}

#[test]
fn encode_mismatching_args_length() {
let metadata = generate_metadata();
let transcoder = ContractMessageTranscoder::new(metadata);

let result: Result<Vec<u8>> = transcoder.encode("new", Vec::<&str>::new());
assert!(result.is_err(), "Should return an error");
assert_eq!(
result.unwrap_err().to_string(),
"Invalid number of input arguments: expected 1, 0 provided"
);

let result: Result<Vec<u8>> = transcoder.encode("new", ["true", "false"]);
assert!(result.is_err(), "Should return an error");
assert_eq!(
result.unwrap_err().to_string(),
"Invalid number of input arguments: expected 1, 2 provided"
);
}

#[test]
fn encode_account_id_custom_ss58_encoding() -> Result<()> {
let metadata = generate_metadata();
Expand Down