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

Port ethabi json tests #232

Merged
merged 15 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 14 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
208 changes: 208 additions & 0 deletions crates/json-abi/tests/abi_item_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
mod test_helpers;

use alloy_json_abi::{
AbiItem, Event, EventParam, Function,
InternalType::{Other, Struct},
Param, StateMutability,
};
use std::borrow::Cow;

#[test]
fn operation() {
let s = r#"{
"type":"function",
"inputs": [{
"name":"a",
"type":"address"
}],
"name":"foo",
"outputs": [],
"stateMutability": "nonpayable"
}"#;

let deserialized: AbiItem<'static> = serde_json::from_str(s).unwrap();

#[allow(deprecated)]
let function = Function {
name: "foo".to_owned(),
inputs: vec![Param {
name: "a".to_owned(),
internal_type: None,
ty: "address".into(),
components: vec![],
}],
outputs: vec![],
state_mutability: StateMutability::NonPayable,
};

assert_eq!(deserialized, AbiItem::Function(Cow::Owned(function)));
let ser = serde_json::to_string(&deserialized).unwrap();
let de: AbiItem<'_> = serde_json::from_str(&ser).unwrap();
assert_eq!(&deserialized, &de);
assert_ser_de!(AbiItem<'_>, deserialized);
}

#[test]
fn event_operation_with_tuple_array_input() {
let s = r#"{
"type":"event",
"inputs": [
{
"name":"a",
"type":"address",
"indexed":true
},
{
"components": [
{
"internalType": "address",
"name": "to",
"type": "address"
},
{
"internalType": "uint256",
"name": "value",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "data",
"type": "bytes"
}
],
"indexed": false,
"internalType": "struct Action[]",
"name": "b",
"type": "tuple[]"
}
],
"name":"E",
"outputs": [],
"anonymous": false
}"#;

let deserialized: AbiItem<'static> = serde_json::from_str(s).unwrap();

let event = Event {
name: "E".to_owned(),
inputs: vec![
EventParam {
name: "a".to_owned(),
indexed: true,
ty: "address".into(),
components: vec![],
internal_type: None,
},
EventParam {
name: "b".to_owned(),
indexed: false,
ty: "tuple[]".into(),
components: vec![
Param {
name: "to".into(),
ty: "address".into(),
components: vec![],
internal_type: Some(Other {
contract: None,
ty: "address".into(),
}),
},
Param {
name: "value".into(),
ty: "uint256".into(),
components: vec![],
internal_type: Some(Other {
contract: None,
ty: "uint256".into(),
}),
},
Param {
name: "data".into(),
ty: "bytes".into(),
components: vec![],
internal_type: Some(Other {
contract: None,
ty: "bytes".into(),
}),
},
],
internal_type: Some(Struct {
contract: None,
ty: "Action[]".into(),
}),
},
],
anonymous: false,
};

assert_eq!(deserialized, AbiItem::Event(Cow::Owned(event)));
assert_ser_de!(AbiItem<'_>, deserialized);
}

// #[test]
// fn sanitize_function_name() {
mouseless0x marked this conversation as resolved.
Show resolved Hide resolved
// fn test_sanitize_function_name(name: &str, expected: &str) {
// let s = format!(
// r#"{{
// "type":"function",
// "inputs": [{{
// "name":"a",
// "type":"address"
// }}],
// "name":"{}",
// "outputs": []
// }}"#,
// name
// );

// let deserialized: AbiItem = serde_json::from_str(&s).unwrap();
// let function = match &deserialized {
// AbiItem::Function(f) => f,
// _ => panic!("expected function"),
// };

// assert_eq!(function.name, expected);

// assert_ser_de!(deserialized);
// }

// test_sanitize_function_name("foo", "foo");
// test_sanitize_function_name("foo()", "foo");
// test_sanitize_function_name("()", "");
// test_sanitize_function_name("", "");
// }

// #[test]
// fn sanitize_event_name() {
// fn test_sanitize_event_name(name: &str, expected: &str) {
// let s = format!(
// r#"{{
// "type":"event",
// "inputs": [{{
// "name":"a",
// "type":"address",
// "indexed":true
// }}],
// "name":"{}",
// "outputs": [],
// "anonymous": false
// }}"#,
// name
// );

// let deserialized: AbiItem = serde_json::from_str(&s).unwrap();
// let event = match deserialized {
// AbiItem::Event(e) => e,
// _ => panic!("expected event!"),
// };

// assert_eq!(event.name, expected);

// assert_ser_de!(AbiItem::Event(event.clone()));
// }

// test_sanitize_event_name("foo", "foo");
// test_sanitize_event_name("foo()", "foo");
// test_sanitize_event_name("()", "");
// test_sanitize_event_name("", "");
// }
Loading
Loading