|
| 1 | +# Using cosmwasm-std |
| 2 | + |
| 3 | +cosmwasm-std is the standard library for building contracts in CosmWasm. It is |
| 4 | +compiled as part of the contract to Wasm. When creating a dependency to |
| 5 | +cosmwasm-std, the required Wasm imports and exports are created implicitely via |
| 6 | +C interfaces, e.g.: |
| 7 | + |
| 8 | +```rust |
| 9 | +// Exports |
| 10 | +#[no_mangle] |
| 11 | +extern "C" fn interface_version_8() -> () { /* ... */ } |
| 12 | +#[no_mangle] |
| 13 | +extern "C" fn allocate(size: usize) -> u32 { /* ... */ } |
| 14 | +#[no_mangle] |
| 15 | +extern "C" fn deallocate(pointer: u32) { /* ... */ } |
| 16 | + |
| 17 | +// Imports |
| 18 | +extern "C" { |
| 19 | + #[cfg(feature = "abort")] |
| 20 | + fn abort(source_ptr: u32); |
| 21 | + |
| 22 | + fn db_read(key: u32) -> u32; |
| 23 | + fn db_write(key: u32, value: u32); |
| 24 | + fn db_remove(key: u32); |
| 25 | + |
| 26 | + /* ... */ |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +As those exports are not namespaced, only one version of cosmwasm-std must exist |
| 31 | +in the dependency tree. Otherwise conflicting C exports are created. |
| 32 | + |
| 33 | +## cosmwasm-std features |
| 34 | + |
| 35 | +The libarary comes with the following features: |
| 36 | + |
| 37 | +| Feature | Enabled by default | Description | |
| 38 | +| ------------ | ------------------ | -------------------------------------------------------------------------- | |
| 39 | +| iterator | x | Storage iterators | |
| 40 | +| abort | x | A panic handler that aborts the contract execution with a helpfull message | |
| 41 | +| stargate | | Cosmos SDK 0.40+ features and IBC | |
| 42 | +| ibc3 | | New fields added in IBC v3 | |
| 43 | +| staking | | Access to the staking module | |
| 44 | +| baktraces | | Add backtraces to errors (for unit testing) | |
| 45 | +| cosmwasm_1_1 | | Features that require CosmWasm 1.1+ on the chain | |
| 46 | +| cosmwasm_1_2 | | Features that require CosmWasm 1.2+ on the chain | |
| 47 | + |
| 48 | +## The cosmwasm-std dependency for contract developers |
| 49 | + |
| 50 | +As a contract developer you can simply specify the dependency as follows in |
| 51 | +`Cargo.toml`: |
| 52 | + |
| 53 | +```toml |
| 54 | +cosmwasm-std = { version = "1.2.0" } |
| 55 | +``` |
| 56 | + |
| 57 | +Please note that it is recommended to set all 3 version components and use the |
| 58 | +minimum version you are willing to accept in the contract. For contracts this |
| 59 | +would usually be the latest stable version. |
| 60 | + |
| 61 | +Most likely you also want to enable the `stargate`, which is pretty basic these |
| 62 | +days and maybe you know your chain supports CosmWasm 1.2 or higher. Then you add |
| 63 | +those features: |
| 64 | + |
| 65 | +```toml |
| 66 | +cosmwasm-std = { version = "1.2.0", features = ["stargate", "cosmwasm_1_2"] } |
| 67 | +``` |
| 68 | + |
| 69 | +## The cosmwasm-std dependency for library developers |
| 70 | + |
| 71 | +When you are creating a library that uses cosmwasm-std, you should be incredibly |
| 72 | +careful with which features you require. The moment you add e.g. `cosmwasm_1_2` |
| 73 | +there it becomes impossible to use the contract in chains with lower CosmWasm |
| 74 | +versions. If you add `abort`, it becomes impossible for the contract developer |
| 75 | +to opt out of the abort feature due to your library. Since this affects the |
| 76 | +default features `abort` and `iterator`, you should always disable default |
| 77 | +features. |
| 78 | + |
| 79 | +Also libraries should define a loose version range that allows the contract |
| 80 | +developer to control which cosmwasm-std version they want to use in the final |
| 81 | +project. E.g. if your library does not work with 1.0.0 due to a bug fixed in |
| 82 | +1.0.1, your min version is 1.0.1 and not the latest stable. |
| 83 | + |
| 84 | +A typical dependency then looks like this: |
| 85 | + |
| 86 | +```toml |
| 87 | +# We really need `stargate` here as this is an IBC related library. `abort` and `iterator` are not needed. |
| 88 | +cosmwasm-std = { version = "1.0.1", default-features = false, features = ["stargate"] } |
| 89 | +``` |
0 commit comments