Skip to content

Commit

Permalink
Fix array type of abi json
Browse files Browse the repository at this point in the history
  • Loading branch information
yanganto committed Jan 31, 2022
1 parent 46eaacd commit 607173c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
1 change: 1 addition & 0 deletions sewup-derive/Cargo.toml
Expand Up @@ -13,6 +13,7 @@ proc-macro = true

[dependencies]
regex = "1"
fancy-regex = "0.7.1"
tiny-keccak = { version = "2.0.2", features = ["keccak"] }
proc-macro2 = "1.0.27"
proc-macro-error = "1.0.4"
Expand Down
30 changes: 30 additions & 0 deletions sewup-derive/src/function_tests.rs
Expand Up @@ -66,6 +66,36 @@ fn test_parse_fn_attr() {
r#"{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferTo","# +
r#""outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"# +
r#""stateMutability":"nonpayable","type":"function"}"#)));

assert_eq!(parse_fn_attr("transfer_to_batch".to_string(), r#"
xxxxxxxx,
inputs=[
{ "internalType": "address[]", "name": "recipient", "type": "address[]" },
{ "internalType": "uint256[]", "name": "amount", "type": "uint256[]" }
],
outputs=[
{ "internalType": "bool", "name": "", "type": "bool" }
],
stateMutability=nonpayable
"#.to_string()), Ok((Some("xxxxxxxx".to_string()),
r#"{"constant":false,"inputs":[{"internalType":"address[]","name":"recipient","type":"address[]"},"#.to_owned() +
r#"{"internalType":"uint256[]","name":"amount","type":"uint256[]"}],"name":"transferToBatch","# +
r#""outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"# +
r#""stateMutability":"nonpayable","type":"function"}"#)));

assert_eq!(parse_fn_attr("transfer_to_batch".to_string(), r#"
4e1273f4,
constant=true,
inputs=[
{ "internalType": "address[]", "name": "account", "type": "address[]" },
{ "internalType": "uint256[]", "name": "token_id", "type": "uint256[]" }
],
outputs=[{ "internalType": "uint256[]", "name": "", "type": "uint256[]" }]
"#.to_string()), Ok((Some("4e1273f4".to_string()),
r#"{"constant":true,"inputs":[{"internalType":"address[]","name":"account","type":"address[]"},"#.to_owned() +
r#"{"internalType":"uint256[]","name":"token_id","type":"uint256[]"}],"name":"transferToBatch","# +
r#""outputs":[{"internalType":"uint256[]","name":"","type":"uint256[]"}],"payable":false,"# +
r#""stateMutability":"view","type":"function"}"#)));
}

#[test]
Expand Down
13 changes: 7 additions & 6 deletions sewup-derive/src/lib.rs
Expand Up @@ -5,6 +5,7 @@ extern crate proc_macro;
mod function_tests;

use convert_case::{Case::Camel, Casing};
use fancy_regex::Regex as FancyRegex;
use proc_macro::TokenStream;
use proc_macro2::{Ident, Span};
use proc_macro_error::{abort, abort_call_site, proc_macro_error};
Expand Down Expand Up @@ -282,8 +283,8 @@ fn parse_fn_attr(fn_name: String, attr: String) -> Result<(Option<String>, Strin
json.push_str(r#""constant":false,"#)
}

if let Some(cap) =
unsafe { Regex::new(r"inputs=(?P<inputs>\[[^\[\]]*\])").unwrap_unchecked() }
if let Ok(Some(cap)) =
unsafe { FancyRegex::new(r#"inputs=(?<inputs>\[.*?\])(?!")"#).unwrap_unchecked() }
.captures(&attr_str)
{
json.push_str(r#""inputs":"#);
Expand All @@ -309,8 +310,8 @@ fn parse_fn_attr(fn_name: String, attr: String) -> Result<(Option<String>, Strin
json.push_str(&format!(r#""name":"{}","#, fn_name.to_case(Camel)));
}

if let Some(cap) =
unsafe { Regex::new(r"outputs=(?P<outputs>\[[^\[\]]*\])").unwrap_unchecked() }
if let Ok(Some(cap)) =
unsafe { FancyRegex::new(r#"outputs=(?<outputs>\[.*?\])(?!")"#).unwrap_unchecked() }
.captures(&attr_str)
{
json.push_str(r#""outputs":"#);
Expand Down Expand Up @@ -392,7 +393,7 @@ fn parse_fn_attr(fn_name: String, attr: String) -> Result<(Option<String>, Strin
/// constant=true,
/// inputs=[
/// { "internalType": "address", "name": "account", "type": "address" },
/// { "internalType": "uinit256", "name": "token_id", "type": "uinit256" }
/// { "internalType": "uint256", "name": "token_id", "type": "uint256" }
/// ],
/// name=balanceOf,
/// outputs=[
Expand Down Expand Up @@ -554,7 +555,7 @@ pub fn ewasm_constructor(_attr: TokenStream, item: TokenStream) -> TokenStream {
/// constant=true,
/// inputs=[
/// { "internalType": "address", "name": "account", "type": "address" },
/// { "internalType": "uinit256", "name": "token_id", "type": "uinit256" }
/// { "internalType": "uint256", "name": "token_id", "type": "uint256" }
/// ],
/// name=balanceOf,
/// outputs=[
Expand Down

0 comments on commit 607173c

Please sign in to comment.