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(AztecMacro): Calling Functions #5081

Closed
Tracked by #5077
LHerskind opened this issue Mar 8, 2024 · 0 comments · Fixed by #5687
Closed
Tracked by #5077

feat(AztecMacro): Calling Functions #5081

LHerskind opened this issue Mar 8, 2024 · 0 comments · Fixed by #5687
Assignees

Comments

@LHerskind
Copy link
Contributor

LHerskind commented Mar 8, 2024

It is currently quite annoying to call functions in other contracts. Either you have to generate interfaces, or you end up writing calls like:

context.call_public_function(
    target,
    selector,
    [...arguments]
);

The ux here is not acceptable at the moment, it is error prone as invalid selector or number of arguments is just fine during compilation and will only fail at run-time. Also some cases, such as the target being the contract itself cannot be generated from the interfaces.

Since our calls are behaving similarly to solidity calls, and we don't support inlining as easily as internal functions etc can be inlined in the evm, it seems like a use like:

ContractName::at(context.this_address()).method(args);

Would be the best that we can achieve. This would be a major improvement over what we have currently 👀

I'm not sure how we would completely be doing this. But my thought is that we similarly to the ts going on atm could generate a struct "behind the scenes" as part of a macro and then use it.

This might work for the calls to self, I'm not fully sure how we would do it for external contracts, but the back of my mind telling me that we could do something like.

#[aztec(import-interface)]
use dep::contract::ContractName;

and then "replace" it with structs ContractNamePrivate and ContractNamePublic that we can insert at the calls using ContractName depending on the #[aztec(private)] or #[aztec(public)]

@github-project-automation github-project-automation bot moved this to Todo in A3 Mar 8, 2024
Thunkar added a commit that referenced this issue Apr 17, 2024
Closes #5081

This PR introduces autogenerated contract interfaces for easy intra and
inter contract interactions. The `aztec-macro` crate is used to stub
every non-internal private and public function and inject them into a
ghost struct which has the same name as the contract that generated
them. After that, they can be called like this:

```rust 

contract ImportTest {

  use dep::my_imported_contract::MyImportedContract;

  #[aztec(private)]
  fn a_private_fn() {
    let deserialized_return = MyImportedContract::at(some_address).another_private_fn(arg1, arg2).call(&mut context);
    MyImportedContract::at(some_address).a_public_fn(arg1).enqueue(&mut context);
  }

  #[aztec(public)]
  fn a_public_fn() {
    let deserialized_return = MyImportedContract::at(some_address).a_public_fn(arg1).call(&mut context);
  }

  #[aztec(private)]
  fn calling_my_own_fns() {
    ImportTest::at(context.this_address).a_private_fn().call(&mut context);
    ImportTest::at(context.this_address).a_public_fn().enqueue(&mut context);
  }
  
}
```

Return values are `deserialized_into()` automatically, providing "real"
return values thanks to
#5633

Also, some general cleanup was required to allow importing contracts in
another contracts. Main changes:

- `HirContext.fully_qualified_struct_path` now uses BFS to avoid
returning the longest path when looking for a struct in a crate. This is
required to avoid pulling structs usually imported in top-level
dependencies (usually notes from our main contract) from other imported
contracts.
- `pack_args_oracle` now has a slice mode in addition to its usual array
mode.


PENDING: 

~~AvmContext. The AVM team is discussing supporting args as slices. In
case it's decided not to do that, a workaround could possibly be
implemented using the macro, but it would be fairly complex.~~

Thanks to @fcarreiro and the amazing AVM team, this is now supported for
the AvmContext!

---------

Co-authored-by: esau <152162806+sklppy88@users.noreply.github.com>
Co-authored-by: Álvaro Rodríguez <sirasistant@gmail.com>
@github-project-automation github-project-automation bot moved this from Todo to Done in A3 Apr 17, 2024
AztecBot added a commit to noir-lang/noir that referenced this issue Apr 17, 2024
…ztec-packages#5687)

Closes AztecProtocol/aztec-packages#5081

This PR introduces autogenerated contract interfaces for easy intra and
inter contract interactions. The `aztec-macro` crate is used to stub
every non-internal private and public function and inject them into a
ghost struct which has the same name as the contract that generated
them. After that, they can be called like this:

```rust

contract ImportTest {

  use dep::my_imported_contract::MyImportedContract;

  #[aztec(private)]
  fn a_private_fn() {
    let deserialized_return = MyImportedContract::at(some_address).another_private_fn(arg1, arg2).call(&mut context);
    MyImportedContract::at(some_address).a_public_fn(arg1).enqueue(&mut context);
  }

  #[aztec(public)]
  fn a_public_fn() {
    let deserialized_return = MyImportedContract::at(some_address).a_public_fn(arg1).call(&mut context);
  }

  #[aztec(private)]
  fn calling_my_own_fns() {
    ImportTest::at(context.this_address).a_private_fn().call(&mut context);
    ImportTest::at(context.this_address).a_public_fn().enqueue(&mut context);
  }

}
```

Return values are `deserialized_into()` automatically, providing "real"
return values thanks to
AztecProtocol/aztec-packages#5633

Also, some general cleanup was required to allow importing contracts in
another contracts. Main changes:

- `HirContext.fully_qualified_struct_path` now uses BFS to avoid
returning the longest path when looking for a struct in a crate. This is
required to avoid pulling structs usually imported in top-level
dependencies (usually notes from our main contract) from other imported
contracts.
- `pack_args_oracle` now has a slice mode in addition to its usual array
mode.

PENDING:

~~AvmContext. The AVM team is discussing supporting args as slices. In
case it's decided not to do that, a workaround could possibly be
implemented using the macro, but it would be fairly complex.~~

Thanks to @fcarreiro and the amazing AVM team, this is now supported for
the AvmContext!

---------

Co-authored-by: esau <152162806+sklppy88@users.noreply.github.com>
Co-authored-by: Álvaro Rodríguez <sirasistant@gmail.com>
AztecBot added a commit to noir-lang/noir that referenced this issue Apr 17, 2024
…ztec-packages#5687)

Closes AztecProtocol/aztec-packages#5081

This PR introduces autogenerated contract interfaces for easy intra and
inter contract interactions. The `aztec-macro` crate is used to stub
every non-internal private and public function and inject them into a
ghost struct which has the same name as the contract that generated
them. After that, they can be called like this:

```rust

contract ImportTest {

  use dep::my_imported_contract::MyImportedContract;

  #[aztec(private)]
  fn a_private_fn() {
    let deserialized_return = MyImportedContract::at(some_address).another_private_fn(arg1, arg2).call(&mut context);
    MyImportedContract::at(some_address).a_public_fn(arg1).enqueue(&mut context);
  }

  #[aztec(public)]
  fn a_public_fn() {
    let deserialized_return = MyImportedContract::at(some_address).a_public_fn(arg1).call(&mut context);
  }

  #[aztec(private)]
  fn calling_my_own_fns() {
    ImportTest::at(context.this_address).a_private_fn().call(&mut context);
    ImportTest::at(context.this_address).a_public_fn().enqueue(&mut context);
  }

}
```

Return values are `deserialized_into()` automatically, providing "real"
return values thanks to
AztecProtocol/aztec-packages#5633

Also, some general cleanup was required to allow importing contracts in
another contracts. Main changes:

- `HirContext.fully_qualified_struct_path` now uses BFS to avoid
returning the longest path when looking for a struct in a crate. This is
required to avoid pulling structs usually imported in top-level
dependencies (usually notes from our main contract) from other imported
contracts.
- `pack_args_oracle` now has a slice mode in addition to its usual array
mode.

PENDING:

~~AvmContext. The AVM team is discussing supporting args as slices. In
case it's decided not to do that, a workaround could possibly be
implemented using the macro, but it would be fairly complex.~~

Thanks to @fcarreiro and the amazing AVM team, this is now supported for
the AvmContext!

---------

Co-authored-by: esau <152162806+sklppy88@users.noreply.github.com>
Co-authored-by: Álvaro Rodríguez <sirasistant@gmail.com>
AztecBot pushed a commit to AztecProtocol/aztec-nr that referenced this issue Apr 19, 2024
Closes AztecProtocol/aztec-packages#5081

This PR introduces autogenerated contract interfaces for easy intra and
inter contract interactions. The `aztec-macro` crate is used to stub
every non-internal private and public function and inject them into a
ghost struct which has the same name as the contract that generated
them. After that, they can be called like this:

```rust 

contract ImportTest {

  use dep::my_imported_contract::MyImportedContract;

  #[aztec(private)]
  fn a_private_fn() {
    let deserialized_return = MyImportedContract::at(some_address).another_private_fn(arg1, arg2).call(&mut context);
    MyImportedContract::at(some_address).a_public_fn(arg1).enqueue(&mut context);
  }

  #[aztec(public)]
  fn a_public_fn() {
    let deserialized_return = MyImportedContract::at(some_address).a_public_fn(arg1).call(&mut context);
  }

  #[aztec(private)]
  fn calling_my_own_fns() {
    ImportTest::at(context.this_address).a_private_fn().call(&mut context);
    ImportTest::at(context.this_address).a_public_fn().enqueue(&mut context);
  }
  
}
```

Return values are `deserialized_into()` automatically, providing "real"
return values thanks to
AztecProtocol/aztec-packages#5633

Also, some general cleanup was required to allow importing contracts in
another contracts. Main changes:

- `HirContext.fully_qualified_struct_path` now uses BFS to avoid
returning the longest path when looking for a struct in a crate. This is
required to avoid pulling structs usually imported in top-level
dependencies (usually notes from our main contract) from other imported
contracts.
- `pack_args_oracle` now has a slice mode in addition to its usual array
mode.


PENDING: 

~~AvmContext. The AVM team is discussing supporting args as slices. In
case it's decided not to do that, a workaround could possibly be
implemented using the macro, but it would be fairly complex.~~

Thanks to @fcarreiro and the amazing AVM team, this is now supported for
the AvmContext!

---------

Co-authored-by: esau <152162806+sklppy88@users.noreply.github.com>
Co-authored-by: Álvaro Rodríguez <sirasistant@gmail.com>
superstar0402 added a commit to superstar0402/aztec-nr that referenced this issue Aug 16, 2024
Closes AztecProtocol/aztec-packages#5081

This PR introduces autogenerated contract interfaces for easy intra and
inter contract interactions. The `aztec-macro` crate is used to stub
every non-internal private and public function and inject them into a
ghost struct which has the same name as the contract that generated
them. After that, they can be called like this:

```rust 

contract ImportTest {

  use dep::my_imported_contract::MyImportedContract;

  #[aztec(private)]
  fn a_private_fn() {
    let deserialized_return = MyImportedContract::at(some_address).another_private_fn(arg1, arg2).call(&mut context);
    MyImportedContract::at(some_address).a_public_fn(arg1).enqueue(&mut context);
  }

  #[aztec(public)]
  fn a_public_fn() {
    let deserialized_return = MyImportedContract::at(some_address).a_public_fn(arg1).call(&mut context);
  }

  #[aztec(private)]
  fn calling_my_own_fns() {
    ImportTest::at(context.this_address).a_private_fn().call(&mut context);
    ImportTest::at(context.this_address).a_public_fn().enqueue(&mut context);
  }
  
}
```

Return values are `deserialized_into()` automatically, providing "real"
return values thanks to
AztecProtocol/aztec-packages#5633

Also, some general cleanup was required to allow importing contracts in
another contracts. Main changes:

- `HirContext.fully_qualified_struct_path` now uses BFS to avoid
returning the longest path when looking for a struct in a crate. This is
required to avoid pulling structs usually imported in top-level
dependencies (usually notes from our main contract) from other imported
contracts.
- `pack_args_oracle` now has a slice mode in addition to its usual array
mode.


PENDING: 

~~AvmContext. The AVM team is discussing supporting args as slices. In
case it's decided not to do that, a workaround could possibly be
implemented using the macro, but it would be fairly complex.~~

Thanks to @fcarreiro and the amazing AVM team, this is now supported for
the AvmContext!

---------

Co-authored-by: esau <152162806+sklppy88@users.noreply.github.com>
Co-authored-by: Álvaro Rodríguez <sirasistant@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants