Skip to content

Commit

Permalink
Use flat abigen only and drop support for the old JSON ABI (#559)
Browse files Browse the repository at this point in the history
This forces the Abigen workflow to use only the new FlatAbigen (now renamed to just Abigen).
  • Loading branch information
digorithm authored Sep 1, 2022
1 parent d43b295 commit aaeccdb
Show file tree
Hide file tree
Showing 15 changed files with 1,806 additions and 3,922 deletions.
72 changes: 40 additions & 32 deletions examples/rust_bindings/src/abi.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
[
{
"type": "function",
"inputs": [
{
"name": "arg",
"type": "u64"
{
"types": [
{
"typeId": 0,
"type": "u64",
"components": null,
"typeParameters": null
}
],
"functions": [
{
"inputs": [
{
"name": "value",
"type": 0,
"typeArguments": null
}
],
"name": "initialize_counter",
"output": {
"name": "",
"type": 0,
"typeArguments": null
}
],
"name": "initialize_counter",
"outputs": [
{
"name": "arg",
"type": "u64"
},
{
"inputs": [
{
"name": "value",
"type": 0,
"typeArguments": null
}
],
"name": "increment_counter",
"output": {
"name": "",
"type": 0,
"typeArguments": null
}
]
},
{
"type": "function",
"inputs": [
{
"name": "arg",
"type": "u64"
}
],
"name": "increment_counter",
"outputs": [
{
"name": "arg",
"type": "u64"
}
]
}
]
}
]
}
78 changes: 43 additions & 35 deletions examples/rust_bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,49 @@ mod tests {
abigen!(
MyContract,
r#"
[
{
"type": "function",
"inputs": [
{
"name": "arg",
"type": "u64"
}
],
"name": "initialize_counter",
"outputs": [
{
"name": "arg",
"type": "u64"
}
]
},
{
"type": "function",
"inputs": [
{
"name": "arg",
"type": "u64"
}
],
"name": "increment_counter",
"outputs": [
{
"name": "arg",
"type": "u64"
}
]
}
]
"#
{
"types": [
{
"typeId": 0,
"type": "u64",
"components": null,
"typeParameters": null
}
],
"functions": [
{
"inputs": [
{
"name": "value",
"type": 0,
"typeArguments": null
}
],
"name": "initialize_counter",
"output": {
"name": "",
"type": 0,
"typeArguments": null
}
},
{
"inputs": [
{
"name": "value",
"type": 0,
"typeArguments": null
}
],
"name": "increment_counter",
"output": {
"name": "",
"type": 0,
"typeArguments": null
}
}
]
}
"#
);
// ANCHOR_END: abigen_with_string
Ok(())
Expand Down
68 changes: 11 additions & 57 deletions packages/fuels-abigen-macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use fuels_core::code_gen::abigen::Abigen;
use fuels_core::code_gen::flat_abigen::FlatAbigen;
use proc_macro::TokenStream;
use proc_macro2::Span;

Expand All @@ -12,68 +11,23 @@ use syn::{parse_macro_input, Ident, LitStr, Token};
pub fn abigen(input: TokenStream) -> TokenStream {
let args = parse_macro_input!(input as Spanned<ContractArgs>);

// Temporary while we support both JSON ABI formats.
// First, we check whether it's an inline JSON string. If not, we assume it's a path.
// For both cases, we check if it's the new flat JSON ABI format
// or the old one.
let is_flat =
if args.abi.starts_with('\n') || args.abi.starts_with('{') || args.abi.starts_with('[') {
// These keys are only found in the new JSON ABI format.
args.abi.contains("types") && args.abi.contains("typeArguments")
} else {
// This is a file, not an inline JSON.
// Check if "flat-abi" is in the file name.
args.abi.split('/').last().unwrap().contains("flat-abi")
};

if is_flat {
FlatAbigen::new(&args.name, &args.abi)
.unwrap()
.expand()
.unwrap()
.into()
} else {
Abigen::new(&args.name, &args.abi)
.unwrap()
.expand()
.unwrap()
.into()
}
Abigen::new(&args.name, &args.abi)
.unwrap()
.expand()
.unwrap()
.into()
}

#[proc_macro]
pub fn wasm_abigen(input: TokenStream) -> TokenStream {
let args = parse_macro_input!(input as Spanned<ContractArgs>);

// Temporary while we support both JSON ABI formats.
// First, we check whether it's an inline JSON string. If not, we assume it's a path.
// For both cases, we check if it's the new flat JSON ABI format
// or the old one.
let is_flat =
if args.abi.starts_with('\n') || args.abi.starts_with('{') || args.abi.starts_with('[') {
// These keys are only found in the new JSON ABI format.
args.abi.contains("types") && args.abi.contains("typeArguments")
} else {
// This is a file, not an inline JSON.
// Check if "flat-abi" is in the file name.
args.abi.split('/').last().unwrap().contains("flat-abi")
};

if is_flat {
FlatAbigen::new(&args.name, &args.abi)
.unwrap()
.no_std()
.expand()
.unwrap()
.into()
} else {
Abigen::new(&args.name, &args.abi)
.unwrap()
.no_std()
.expand()
.unwrap()
.into()
}
Abigen::new(&args.name, &args.abi)
.unwrap()
.no_std()
.expand()
.unwrap()
.into()
}

/// Trait that abstracts functionality for inner data that can be parsed and
Expand Down
Loading

0 comments on commit aaeccdb

Please sign in to comment.