Building Your Contract
Run the following command to compile your smart contract:
1
cargo +nightly contract build
Copied!
This special command will turn your ink! project into a Wasm binary which you can deploy to your chain. If all goes well, you should see a target folder which contains this .wasm file.
1
target
2
└── flipper.wasm
Copied!

Contract Metadata

By running the next command we'll generate the contract metadata (a.k.a. the contract ABI):
1
cargo +nightly contract generate-metadata
Copied!
You should have a new JSON file (metadata.json) in the same target directory:
1
target
2
├── flipper.wasm
3
└── metadata.json
Copied!
Let's take a look at the structure inside:
1
{
2
"metadataVersion": "0.1.0",
3
"source": {
4
"hash": "0x11ba777b3457bf64cb99421786c90d421afde1c56579aa3dae336e58ccc8f783",
5
"language": "ink! 3.0.0-rc1",
6
"compiler": "rustc 1.47.0-nightly"
7
},
8
"contract": {
9
"name": "flipper",
10
"version": "0.1.0",
11
"authors": [
12
"[your_name] "
13
]
14
},
15
"spec": {
16
"constructors": [
17
{
18
"args": [
19
{
20
"name": "init_value",
21
"type": {
22
"displayName": [
23
"bool"
24
],
25
"type": 1
26
}
27
}
28
],
29
"docs": [
30
" Constructor that initializes the `bool` value to the given `init_value`."
31
],
32
"name": [
33
"new"
34
],
35
"selector": "0xd183512b"
36
},
37
{
38
"args": [],
39
"docs": [
40
" Constructor that initializes the `bool` value to `false`.",
41
"",
42
" Constructors can delegate to other constructors."
43
],
44
"name": [
45
"default"
46
],
47
"selector": "0x6a3712e2"
48
}
49
],
50
"docs": [],
51
"events": [],
52
"messages": [
53
{
54
"args": [],
55
"docs": [
56
" A message that can be called on instantiated contracts.",
57
" This one flips the value of the stored `bool` from `true`",
58
" to `false` and vice versa."
59
],
60
"mutates": true,
61
"name": [
62
"flip"
63
],
64
"payable": false,
65
"returnType": null,
66
"selector": "0xc096a5f3"
67
},
68
{
69
"args": [],
70
"docs": [
71
" Simply returns the current value of our `bool`."
72
],
73
"mutates": false,
74
"name": [
75
"get"
76
],
77
"payable": false,
78
"returnType": {
79
"displayName": [
80
"bool"
81
],
82
"type": 1
83
},
84
"selector": "0x1e5ca456"
85
}
86
]
87
},
88
"storage": {
89
"struct": {
90
"fields": [
91
{
92
"layout": {
93
"cell": {
94
"key": "0x0000000000000000000000000000000000000000000000000000000000000000",
95
"ty": 1
96
}
97
},
98
"name": "value"
99
}
100
]
101
}
102
},
103
"types": [
104
{
105
"def": {
106
"primitive": "bool"
107
}
108
}
109
]
110
}
Copied!
You can see that this file describes all the interfaces that can be used to interact with your contract.
  • Registry provides the strings and custom types used throughout the rest of the JSON.
  • Storage defines all the storage items managed by your contract and how to ultimately access them.
  • Contract stores information about the callable functions like constructors and messages a user can call to interact with your contract. It also has helpful information like the events that are emitted by the contract or any docs.
If you look close at the constructors and messages, you will also notice a selector which is a 4-byte hash of the function name and is used to route your contract calls to the correct functions.
Learn More
ink! provides a built-in overflow protection enabled on our Cargo.toml file. It is recommended to keep it enabled to prevent potential overflow errors in your contract.
1
[profile.release]
2
panic = "abort" <-- Panics shall be treated as aborts: reduces binary size
3
lto = true <-- enable link-time-optimization: more efficient codegen
4
opt-level = "z" <-- Optimize for small binary output
5
overflow-checks = true <-- Arithmetic overflow protection
Copied!
Last modified 2mo ago
Export as PDF
Copy link