Using references
The Bulletin Board example uses a reference to the HighlightedPosts
contract to delete highlights. In this section, we will take a look at three basic building blocks of cross-contract calls.
Exporting a reference
The very first thing we need to do is to export the reference to make it visible outside of the 'callee' contract's module. Note that you don't have to declare it: this is already done, courtesy of Ink!'s macro system. The reference's name will be your contract's name with a Ref
suffix, so in our example this will be:
Please remember that the export above needs to be placed at the top level and not inside the highlighted_posts
module.
So far so good, let's switch the files and start using the reference!
Initializing the reference
A very important thing to note is that the reference needs to be initialized with a code hash of a specific contract. That is: for this to work, the HighlightedPosts
contract needs to be already deployed on the chain and you need to know its code hash!
Assuming we were able to get ourselves a code hash, we will need to use it to initialize the reference. In our example, it will look like this:
The salt_bytes
can be pretty much anything you want: here we set it to the concatenation of the version and the caller's account. The endowment
is a value transferred along with the call and, counter-intuitively, is required by the API (even though we don't actually transfer anything).
Calling methods on the reference
Let's assume we have the reference saved to our storage struct as highlighted_posts_board
. Now, in order to call the method, we use the familiar syntax:
Just like that! Of course, remember to handle the Result
of this call. If you want to check if the compiler truly type-checks this call, you can make a typo in the method or pass a param that doesn't make sense, like the number 42.
Last updated
Was this helpful?