Add import functionality for contracts #1156
Labels
aztec.nr
Helpful for development of Aztec.nr the smart contract framework
enhancement
New feature or request
Problem
Importing other blocks of code is a common feature used in many languages.
From
#include
in C toimport
in javascript.Currently, we have the ability to import Noir programs, but we have not implemented the necessary semantics around importing Noir contracts.
Why is import syntax different for Noir contracts?
Importing Noir contracts are wholly different to importing programs because when you import a function in another library as Noir program, it always gets inlined in your current Noir program.
Take the following two noir libraries:
This is equivalent to:
This does not apply for Noir smart contracts and in-fact calling a function from another smart contract is never inlined. This difference is due to the fact that a smart contract has shared state that can only be modified by its own functions, and inlining those functions in another smart contract breaks this property.
Proposed solution
The
Foo
contract above will inlineadd
intotriple_add
since both functions are secret.-
use my_contracts::Foo;
The
Bar
contract uses the line to import theFoo
contract.let foo_contract_instance: Foo = Foo(address_of_foo_contract);
This line will instantiate a new
Foo
contract with the given address. All contracts must be instantiated with an address. This is different to the constructor of the contract.To clarify, lets break down
Foo(address_of_foo_contract);
further. This can be desugared into:The first line emphasizes the fact that we are doing something closer to dynamic linking; at runtime the
address_of_foo_contract
is used to create aContract
instance. This could be any contract, it all depends on whataddress_of_foo_contract
is. Since our contract will not work if it is not aFoo
contract, we then assume that the methods inFoo
are available on thisContract
instance by using theas
keyword.The above two lines is not posible in Noir and I just used it to emphasize further what is happening.
let _d: Field = foo_contract_instance. triple_add(a, b, c);
Remember, this is a function call which is not inlined.
Alternatives considered
No response
Additional context
A nice to have is a way for a contract to say "I don't care what the contract instance is, it just needs to adhere to these methods" -- This requires something like traits/interface, so we avoid it for now.
We would also need to figure out how to error when a contract instantiated at runtime, does not provide a method it should.
Submission Checklist
The text was updated successfully, but these errors were encountered: