Skip to content

Commit

Permalink
[Fix] adding nonce to getFunctionInputs for records (#753)
Browse files Browse the repository at this point in the history
* adding nonce to getFunctionInputs for records

* forgot to add nonce to input

* updating tests

* moving _nonce to members
  • Loading branch information
Brent C authored Oct 3, 2023
1 parent cb07bc0 commit 0604a41
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion wasm/Cargo.lock

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

17 changes: 13 additions & 4 deletions wasm/src/programs/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,15 @@ impl Program {
}

Reflect::set(&input, &"members".into(), &record_members).map_err(|_| "Failed to set property")?;

// Adding _nonce object to record
let _nonce = Object::new();
Reflect::set(&_nonce, &"name".into(), &"_nonce".into()).map_err(|_| "Failed to set property")?;
Reflect::set(&_nonce, &"type".into(), &"group".into()).map_err(|_| "Failed to set property")?;
Reflect::set(&_nonce, &"visibility".into(), &"public".into()).map_err(|_| "Failed to set property")?;

record_members.push(&JsValue::from(_nonce));

Ok(input)
}

Expand Down Expand Up @@ -674,25 +683,25 @@ function add_and_double:
fn test_get_inputs() {
let credits = Program::from(ProgramNative::credits().unwrap());
let inputs = credits.get_function_inputs("transfer_private".to_string()).unwrap();
let expected = r#"Array { obj: Object { obj: JsValue([Object({"type":"record","record":"credits","members":[{"name":"microcredits","type":"u64","visibility":"private"}],"register":"r0"}), Object({"type":"address","visibility":"private","register":"r1"}), Object({"type":"u64","visibility":"private","register":"r2"})]) } }"#.to_string();
let expected = r#"Array { obj: Object { obj: JsValue([Object({"type":"record","record":"credits","members":[{"name":"microcredits","type":"u64","visibility":"private"},{"name":"_nonce","type":"group","visibility":"public"}],"register":"r0"}), Object({"type":"address","visibility":"private","register":"r1"}), Object({"type":"u64","visibility":"private","register":"r2"})]) } }"#.to_string();
assert_eq!(format!("{:?}", inputs), expected);

let token_issue = Program::from_string(TOKEN_ISSUE).unwrap();
let inputs = token_issue.get_function_inputs("bump_token_version".to_string()).unwrap();
let expected = r#"Array { obj: Object { obj: JsValue([Object({"type":"address","visibility":"private","register":"r0"}), Object({"type":"record","record":"Token","members":[{"name":"microcredits","type":"u64","visibility":"private"},{"name":"amount","type":"u64","visibility":"private"},{"name":"token_data","type":"struct","struct_id":"token_metadata","members":[{"name":"token_id","type":"u32"},{"name":"version","type":"u32"}],"visibility":"private"}],"register":"r1"}), Object({"type":"struct","struct_id":"token_metadata","members":[{"name":"token_id","type":"u32"},{"name":"version","type":"u32"}],"visibility":"private","register":"r2"})]) } }"#;
let expected = r#"Array { obj: Object { obj: JsValue([Object({"type":"address","visibility":"private","register":"r0"}), Object({"type":"record","record":"Token","members":[{"name":"microcredits","type":"u64","visibility":"private"},{"name":"amount","type":"u64","visibility":"private"},{"name":"token_data","type":"struct","struct_id":"token_metadata","members":[{"name":"token_id","type":"u32"},{"name":"version","type":"u32"}],"visibility":"private"},{"name":"_nonce","type":"group","visibility":"public"}],"register":"r1"}), Object({"type":"struct","struct_id":"token_metadata","members":[{"name":"token_id","type":"u32"},{"name":"version","type":"u32"}],"visibility":"private","register":"r2"})]) } }"#;
assert_eq!(format!("{:?}", inputs), expected);
}

#[wasm_bindgen_test]
fn test_get_record() {
let credits = Program::from(ProgramNative::credits().unwrap());
let members = credits.get_record_members("credits".to_string()).unwrap();
let expected = r#"Object { obj: JsValue(Object({"type":"record","record":"credits","members":[{"name":"microcredits","type":"u64","visibility":"private"}]})) }"#.to_string();
let expected = r#"Object { obj: JsValue(Object({"type":"record","record":"credits","members":[{"name":"microcredits","type":"u64","visibility":"private"},{"name":"_nonce","type":"group","visibility":"public"}]})) }"#.to_string();
assert_eq!(format!("{:?}", members), expected);

let token_issue = Program::from_string(TOKEN_ISSUE).unwrap();
let members = token_issue.get_record_members("Token".to_string()).unwrap();
let expected = r#"Object { obj: JsValue(Object({"type":"record","record":"Token","members":[{"name":"microcredits","type":"u64","visibility":"private"},{"name":"amount","type":"u64","visibility":"private"},{"name":"token_data","type":"struct","struct_id":"token_metadata","members":[{"name":"token_id","type":"u32"},{"name":"version","type":"u32"}],"visibility":"private"}]})) }"#;
let expected = r#"Object { obj: JsValue(Object({"type":"record","record":"Token","members":[{"name":"microcredits","type":"u64","visibility":"private"},{"name":"amount","type":"u64","visibility":"private"},{"name":"token_data","type":"struct","struct_id":"token_metadata","members":[{"name":"token_id","type":"u32"},{"name":"version","type":"u32"}],"visibility":"private"},{"name":"_nonce","type":"group","visibility":"public"}]})) }"#;
assert_eq!(format!("{:?}", members), expected);
}

Expand Down

0 comments on commit 0604a41

Please sign in to comment.