ink-wrapper
ink-wrapper
is a tool that generates type-safe code for calling a substrate smart contract based on the metadata
(.json
) file for that contract.
Installation
Install the tool from crates.io:
Usage
Setup
Given some metadata file like metadata.json
run the tool and save the output to a file in your project:
We only take minimal steps to format the output of the tool, so we recommend that you run it through a formatter when (re)generating:
|
The output should compile with no warnings, please create an issue if any warnings pop up in your project in the generated code.
Make sure the file you generated is included in your module structure:
You will need the following dependencies for the wrapper to work:
= "0.2.0"
= { = "parity-scale-codec", = "3", = false, = ["derive"] }
= "4.0.1"
# You only need this one if you have messages of the form `Trait::message`, like the ones generated by openbrush, for
# example.
= "0.1.68"
# This one is optional, but you most likely need it as well if you're using the default `aleph_client` implementation
# for actually making calls. Otherwise, you will need to implement `ink_wrapper_types::Connection` and
# `ink_wrapper_types::SignedConnection` yourself.
= "3.0.0"
Basic usage
With that, you're ready to use the wrappers in your code. The generated module will have an Instance
struct that
represents an instance of your contract. You can either talk to an existing instance by converting an account_id
to
an Instance
:
let account_id: AccountId = ...;
let instance: Instance = account_id.into;
Or (assuming the contract code has already been uploaded) create an instance using one of the generated constructors:
let instance = some_constructor.await?;
And then call methods on your contract:
let result = instance.some_getter.await?;
let tx_info = instance.some_mutator.await?;
Note that any methods that have names like Trait::method_name
will be grouped into traits in the generated module. You
might encounter this if you're using openbrush, for example their PSP22
implementation generates method names like
PSP22::balance_of
. You need to use
the generated traits to access these:
use PSP22 as _;
instance.balance_of.await?
In the examples above, conn
is anything that implements ink_wrapper_types::Connection
(and
ink_wrapper_types::SignedConnection
if you want to use constructors or mutators). Default implementations are provided
for the connection in aleph_client
.
Events
ink_wrapper_types::Connection
also allows you to fetch events for a given TxInfo
:
use Connection as _;
let tx_info = instance.some_mutator.await?;
let all_events = conn.get_contract_events.await?;
let contract_events = all_events.for_contract;
let sub_contract_events = all_events.for_contract;
The all_events
object above may contain events from multiple contracts if the contract called into them. In that case,
you can filter and parse these events by calling for_contract
on it, with the various contracts you're interested in.
Example
Look at test-project
in the project's repo for a fuller example. Note that test-project
is missing the actual
wrappers, which are normally generated when testing. The easiest way to regenerate them is by running
make all-dockerized
(requires docker) - see Development for more on that.
Development
Use the commands provided in the Makefile
to replicate the build process run on CI:
The most hassle-free is to just run everything in docker:
If you have the tooling installed on your host and start a node yourself, you can also run the build on your host:
In case there are any runaway containers from all-dockerized
you can kill them: