Odra.toml
As mentioned in the previous article, Odra.toml is a file that contains information about all the contracts
that Odra will build. Let's take a look at the file structure again:
[[contracts]]
fqn = "flipper::Flipper"
The fqn (Fully Qualified Name) is the Rust path to your contract's struct, and it is used by the
building tools to locate and build the contract. In a single-crate project it has two segments:
the module (here flipper, i.e. src/flipper.rs) and the struct (Flipper).
The last segment of the fqn will be used as the name for your contract - the generated wasm file will
be in the above case named Flipper.wasm, matching the struct name exactly, capital letter included.
In a workspace project (cargo odra new -t workspace) the fqn gains a third, leading segment - the
crate name - so it reads <crate>::<module>::<Struct>, for example flipper::flipper::Flipper.
Adding a new contract manually
Besides using the cargo odra generate command, you can add a new contract to be compiled by hand.
To do this, add another [[contracts]] element, name it and make sure that the fqn is set correctly.
For example, if you want to create a new contract called counter, your Odra.toml file should finally
look like this:
[[contracts]]
fqn = "flipper::Flipper"
[[contracts]]
fqn = "counter::Counter"
Contracts from other crates
A contract does not have to live in your project. If the fqn starts with the name of a crate your
project depends on, Odra builds that crate's contract for you. For example, with odra-modules in
your Cargo.toml:
[[contracts]]
fqn = "odra_modules::erc20::Erc20"
[[contracts]]
fqn = "odra_modules::cep18_token::Cep18"
cargo odra build -c Erc20 and cargo odra schema -c Erc20 write wasm/Erc20.wasm and
resources/casper_contract_schemas/erc20_schema.json in the current project, next to the wasm files
and schemas of your own contracts - there is nothing to copy from the other crate's directory.
The contract is then used like any local one: the Erc20HostRef comes from odra_modules::erc20,
and env.deploy in tests or a livenet script finds the wasm by the struct name as usual.
Linking the other crate
Rust links a dependency only into a crate that refers to it. A project that lists
odra_modules::erc20::Erc20 but never mentions odra_modules in its code would get a wasm without
entry points. The bin/build_contract.rs and bin/build_schema.rs of a project generated for Odra
2.11.0 or newer take care of it with one line:
use my_project;
odra::link_contract_crates!();
The macro refers to every dependency that Odra.toml lists a contract of, so there is nothing to
edit when you add such a contract. Two cases need a hand-written use next to it instead:
- a project generated for an older Odra: add the
odra::link_contract_crates!();line to both files, oruse odra_modules;, - an optional dependency, or one from a
[target.*.dependencies]table: the macro leaves those out, because it cannot tell if they are there in a given build.
If the reference is missing, cargo odra build (0.2.1 and newer) stops with an error that names the
crate, instead of saving a wasm that cannot be installed.
What's next
In the next section, we'll take a closer look at the code that was generated by Odra by default - the famous
Flipper contract.