Skip to content

Commit

Permalink
Implements json abi with call paths for structs and enums. (FuelLabs#…
Browse files Browse the repository at this point in the history
…4137)

## Description

Reference to external dependency struct now looks like: `"type": "struct
abi_with_tuples::Person",`
`"type": "enum std::option::Option",`

Struct reference in root module still looks like:
`"type": "struct MyStruct",`

Struct reference in sub modules looks like:
`"type": "struct dep_1::MyStruct1",`

` forc build` will not generate json ABI with call path by default to do
so we have to use the flag `--json-abi-with-callpaths`, one example is:

```cargo run --bin forc build --json-abi-with-callpaths --path `pwd`/test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/abi_with_same_name_types/```

This will generate `test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/abi_with_same_name_types/out/debug/abi_with_same_name_types-abi.json` with call paths for structs and enums.

This is related to:
FuelLabs/fuel-specs#450
FuelLabs/fuels-rs#791

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand areas.
- [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my feature works.
- [x] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
  • Loading branch information
esdrubal authored Mar 2, 2023
1 parent 50ab59e commit 40cf1c2
Show file tree
Hide file tree
Showing 28 changed files with 436 additions and 111 deletions.
3 changes: 3 additions & 0 deletions forc-pkg/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ pub struct BuildProfile {
pub terse: bool,
pub time_phases: bool,
pub include_tests: bool,
pub json_abi_with_callpaths: bool,
pub error_on_warnings: bool,
}

Expand Down Expand Up @@ -605,6 +606,7 @@ impl BuildProfile {
terse: false,
time_phases: false,
include_tests: false,
json_abi_with_callpaths: false,
error_on_warnings: false,
}
}
Expand All @@ -619,6 +621,7 @@ impl BuildProfile {
terse: false,
time_phases: false,
include_tests: false,
json_abi_with_callpaths: false,
error_on_warnings: false,
}
}
Expand Down
17 changes: 15 additions & 2 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ use std::{
str::FromStr,
};
use sway_core::{
abi_generation::{evm_json_abi, fuel_json_abi},
abi_generation::{
evm_json_abi,
fuel_json_abi::{self, JsonAbiContext},
},
asm_generation::ProgramABI,
decl_engine::{DeclEngine, DeclRefFunction},
fuel_prelude::{
Expand Down Expand Up @@ -217,6 +220,8 @@ pub struct PkgOpts {
///
/// By default, this is `<project-root>/out`.
pub output_directory: Option<String>,
/// Outputs json abi with callpath instead of struct and enum names.
pub json_abi_with_callpaths: bool,
}

#[derive(Default, Clone)]
Expand Down Expand Up @@ -1727,7 +1732,14 @@ pub fn compile(
let mut types = vec![];
ProgramABI::Fuel(time_expr!(
"generate JSON ABI program",
fuel_json_abi::generate_json_abi_program(typed_program, engines.te(), &mut types)
fuel_json_abi::generate_json_abi_program(
&mut JsonAbiContext {
program: typed_program,
json_abi_with_callpaths: profile.json_abi_with_callpaths,
},
engines.te(),
&mut types
)
))
}
BuildTarget::EVM => {
Expand Down Expand Up @@ -1944,6 +1956,7 @@ fn build_profile_from_opts(
profile.terse |= pkg.terse;
profile.time_phases |= time_phases;
profile.include_tests |= tests;
profile.json_abi_with_callpaths |= pkg.json_abi_with_callpaths;
profile.error_on_warnings |= error_on_warnings;

Ok((selected_build_profile.to_string(), profile))
Expand Down
1 change: 1 addition & 0 deletions forc-plugins/forc-client/src/op/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ fn build_opts_from_cmd(cmd: &cmd::Deploy) -> pkg::BuildOpts {
terse: cmd.pkg.terse,
locked: cmd.pkg.locked,
output_directory: cmd.pkg.output_directory.clone(),
json_abi_with_callpaths: cmd.pkg.json_abi_with_callpaths,
},
print: pkg::PrintOpts {
ast: cmd.print.ast,
Expand Down
1 change: 1 addition & 0 deletions forc-plugins/forc-client/src/op/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ fn build_opts_from_cmd(cmd: &cmd::Run) -> pkg::BuildOpts {
terse: cmd.pkg.terse,
locked: cmd.pkg.locked,
output_directory: cmd.pkg.output_directory.clone(),
json_abi_with_callpaths: cmd.pkg.json_abi_with_callpaths,
},
print: pkg::PrintOpts {
ast: cmd.print.ast,
Expand Down
1 change: 1 addition & 0 deletions forc/src/cli/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ fn opts_from_cmd(cmd: Command) -> forc_test::Opts {
terse: cmd.build.pkg.terse,
locked: cmd.build.pkg.locked,
output_directory: cmd.build.pkg.output_directory,
json_abi_with_callpaths: cmd.build.pkg.json_abi_with_callpaths,
},
print: pkg::PrintOpts {
ast: cmd.build.print.ast,
Expand Down
3 changes: 3 additions & 0 deletions forc/src/cli/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ pub struct Pkg {
/// needs to be updated, Forc will exit with an error
#[clap(long)]
pub locked: bool,
/// Outputs json abi with callpaths instead of names for struct and enums.
#[clap(long)]
pub json_abi_with_callpaths: bool,
}

/// Options related to minifying output.
Expand Down
1 change: 1 addition & 0 deletions forc/src/ops/forc_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ fn opts_from_cmd(cmd: BuildCommand) -> pkg::BuildOpts {
terse: cmd.build.pkg.terse,
locked: cmd.build.pkg.locked,
output_directory: cmd.build.pkg.output_directory,
json_abi_with_callpaths: cmd.build.pkg.json_abi_with_callpaths,
},
print: pkg::PrintOpts {
ast: cmd.build.print.ast,
Expand Down
Loading

0 comments on commit 40cf1c2

Please sign in to comment.