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

feat!: add predicates abigen #726

Merged
merged 20 commits into from
Dec 17, 2022
Merged

feat!: add predicates abigen #726

merged 20 commits into from
Dec 17, 2022

Conversation

hal3e
Copy link
Contributor

@hal3e hal3e commented Dec 7, 2022

This PR is is based on @mohammadfawaz sway branch which supports main function arguments for predicates.

We do not have to use the input_predicate_data(0) data anymore instead we pass the data directly into the main function. This is an example how a new predicate looks like:

predicate;

struct TestStruct {
    value: u64,
}

struct AnotherTestStruct {
    value: u64,
    number: u64,
}

fn main(test_struct: TestStruct, test_struct2: AnotherTestStruct) -> bool {
    test_struct.value == (test_struct2.value + test_struct2.number)
}

Previously, we could not useTestStruct or AnotherTestStruct directly in our test because it was not generated.
With the new abigen we generate all the types and we also generate an encoding function encode_data which has the same inputs as the predicate's main function...

This is how the new abigen is used:

    predicate_abigen!(
        MyPredicate,
        "packages/fuels/tests/predicates/predicate_structs/out/debug/predicate_structs-abi.json"
    );

    let predicate =
        MyPredicate::load_from("tests/predicates/predicate_structs/out/debug/predicate_structs.bin")?;

Send funds to a predicate:

    predicate
        .receive(&sender, amount, asset_id, None)
        .await?;

Spend funds from a predicate. First encode the data and then spend to a wallet:

    predicate
        .encode_data(
            TestStruct { value: 192 },
            AnotherTestStruct {
                value: 64,
                number: 128,
            },
        )
        .spend(&receiver, predicate_balance, asset_id, None)
        .await?;

What was done:

  • Extend the Abigen to handle predicates and add the predicate abigen macro
    • Added AbigenType enum to specify if it is a contract, script or predicate
  • Update the wallet's spend_predicate function to calculate the appropriate data offset for dynamic data
    • This means that we can use vectors as inputs in predicates
    • I moved the offset calculating function to fuels_core::offsets as it was used in fuels-signers and fuels-contract
  • Added test for:
    • Coin and Message predicates
    • Primitive and custom type inputs
    • Generics and Vectors
  • Update the docs

Breaking changes:

  • Predicate instantiation
  • Sending funds to and receiving funds from predicates

thanks @mohammadfawaz for the help from the compiler side!

@hal3e hal3e self-assigned this Dec 7, 2022
@hal3e hal3e requested a review from a team December 7, 2022 10:02
@hal3e hal3e marked this pull request as draft December 7, 2022 12:41
@MujkicA
Copy link
Contributor

MujkicA commented Dec 8, 2022

Interface looks good IMO

@hal3e hal3e added the breaking Introduces or requires breaking changes label Dec 14, 2022
@hal3e hal3e changed the title feat: add predicates abigen feat!: add predicates abigen Dec 14, 2022
@hal3e hal3e added the big label Dec 14, 2022
@hal3e hal3e marked this pull request as ready for review December 14, 2022 01:08
@mohammadfawaz
Copy link
Contributor

mohammadfawaz commented Dec 14, 2022

I'm assuming you need a new release of forc with my PR for this to get merged?

@hal3e
Copy link
Contributor Author

hal3e commented Dec 14, 2022

I'm assuming you need a new release of forc with my PR for this to get merged?

Yes. Currently the CI is using your branch.

Copy link
Member

@digorithm digorithm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a first pass focusing on the docs and interfaces. I thought I should publish the review without fully finishing it so you can work through the suggestions while I do another deeper pass!

By the way, it's looking beautiful!

docs/src/predicates/predicate-data.md Outdated Show resolved Hide resolved
docs/src/predicates/predicate-data.md Outdated Show resolved Hide resolved
docs/src/predicates/predicate-data.md Outdated Show resolved Hide resolved
docs/src/predicates/send-spend-predicate.md Outdated Show resolved Hide resolved
docs/src/predicates/send-spend-predicate.md Outdated Show resolved Hide resolved
docs/src/predicates/send-spend-predicate.md Outdated Show resolved Hide resolved
packages/fuels-core/src/code_gen/abigen.rs Outdated Show resolved Hide resolved
packages/fuels-core/src/code_gen/abigen.rs Outdated Show resolved Hide resolved
Copy link
Member

@digorithm digorithm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so good!

Consider this an approval, but since we have to wait for forc to be released so we can pin a released version, this is just a comment to prevent an accidental merge.

packages/fuels-contract/src/execution_script.rs Outdated Show resolved Hide resolved
packages/fuels/tests/predicates.rs Show resolved Hide resolved
@digorithm digorithm requested a review from a team December 15, 2022 00:17
Copy link
Contributor

@segfault-magnet segfault-magnet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work. Especially with the bindings and the predicate data offset.

packages/fuels-signers/src/wallet.rs Show resolved Hide resolved
Copy link
Member

@Salka1988 Salka1988 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awsome! 🏅

digorithm
digorithm previously approved these changes Dec 16, 2022
Copy link
Member

@digorithm digorithm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM -- wonderful work. These tests are 10/10.

packages/fuels-core/src/code_gen/functions_gen.rs Outdated Show resolved Hide resolved
Copy link
Member

@digorithm digorithm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@hal3e hal3e merged commit bc61fb6 into master Dec 17, 2022
@hal3e hal3e deleted the hal3e/predicate-main branch December 17, 2022 20:36
iqdecay pushed a commit that referenced this pull request Dec 26, 2022
Breaking changes:
- Predicate instantiation
- Sending funds to and receiving funds from predicates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
big breaking Introduces or requires breaking changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants