Skip to content

Commit

Permalink
Error if mismatching # of args for instantiate/call (use-ink#966)
Browse files Browse the repository at this point in the history
* Error if mismatching # of args for instantiate/call

* Change error message
  • Loading branch information
ascjones authored Feb 13, 2023
1 parent 0eebc11 commit fded85c
Showing 1 changed file with 29 additions and 0 deletions.
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

0 comments on commit fded85c

Please sign in to comment.