generated from okp4/template-rust
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #333 from okp4/build/smart-contract-template
🖨️ Smart contract scaffolding
- Loading branch information
Showing
13 changed files
with
219 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
line-length: false | ||
no-hard-tabs: false | ||
no-inline-html: false | ||
no-duplicate-header: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -230,11 +230,36 @@ cargo make chain-clean | |
⚠️ Please be cautious when running this command as it will completely clean the chain's home directory and the action is | ||
irreversible. | ||
|
||
## Documentation | ||
## Develop | ||
|
||
### Smart Contracts scaffolding | ||
|
||
When developing a new Smart Contract, you can use the scaffolding to generate the Smart Contract's code. | ||
|
||
#### Pre-requisites | ||
|
||
Be sure you have the following tools installed: | ||
|
||
- [ffizer](https://ffizer.github.io/ffizer/book/#install) v2.10.3 or higher | ||
|
||
#### Generate the scaffolding | ||
|
||
To generate the scaffolding, just run: | ||
|
||
```sh | ||
cargo make scaffold-smart-contract | ||
``` | ||
|
||
Then, follow the instructions. | ||
|
||
### Documentation | ||
|
||
The documentation of the smart contracts must be committed to the repository. The documentation is generated from the | ||
smart contracts' schema. | ||
|
||
To generate the documentation follow the steps below. | ||
|
||
### Tools installation | ||
#### Pre-requisites | ||
|
||
Be sure you have the following tools installed: | ||
|
||
|
@@ -246,7 +271,7 @@ Then, install the dependencies: | |
yarn global add @adobe/[email protected] | ||
``` | ||
|
||
### Generate the documentation | ||
#### Generate the documentation | ||
|
||
To generate the documentation, just run: | ||
|
||
|
@@ -257,7 +282,7 @@ cargo make docs-generate | |
|
||
You'll find the generated documentation under the `docs` folder. | ||
|
||
### Commit the documentation | ||
#### Commit the documentation | ||
|
||
When developing a new contract, you should commit the generated documentation to the repository. For this, gerenate the | ||
documentation and commit the changes: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
variables: | ||
- name: contract_name | ||
default_value: okp4-foo | ||
|
||
scripts: | ||
- message: | | ||
🖨️ Smart contract 🚀 {{ contract_name }} scaffolded! Enjoy! |
58 changes: 58 additions & 0 deletions
58
templates/okp4-smart-contract/{{ contract_name }}/Cargo.ffizer.hbs.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
[package] | ||
authors = ["OKP4"] | ||
edition = "2021" | ||
name = "{{ contract_name }}" | ||
rust-version = "1.69" | ||
version = "0.0.1" | ||
|
||
exclude = [ | ||
# Those files are rust-optimizer artifacts. You might want to commit them for convenience but they should not be part of the source code publication. | ||
"contract.wasm", | ||
"hash.txt", | ||
] | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
[profile.release] | ||
codegen-units = 1 | ||
debug = false | ||
debug-assertions = false | ||
incremental = false | ||
lto = true | ||
opt-level = 3 | ||
overflow-checks = true | ||
panic = 'abort' | ||
rpath = false | ||
|
||
[dependencies] | ||
cosmwasm-schema.workspace = true | ||
cosmwasm-std.workspace = true | ||
cosmwasm-storage.workspace = true | ||
cw-storage-plus.workspace = true | ||
cw-utils.workspace = true | ||
cw2.workspace = true | ||
okp4-logic-bindings.workspace = true | ||
okp4-objectarium-client.workspace = true | ||
okp4-objectarium.workspace = true | ||
schemars.workspace = true | ||
serde.workspace = true | ||
thiserror.workspace = true | ||
|
||
[dev-dependencies] | ||
cw-multi-test.workspace = true | ||
|
||
[features] | ||
# for more explicit tests, cargo test --features=backtraces | ||
backtraces = ["cosmwasm-std/backtraces"] | ||
# use library feature to disable all instantiate/execute/query exports | ||
library = [] | ||
|
||
[package.metadata.scripts] | ||
optimize = """docker run --rm -v "$(pwd)":/code \ | ||
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \ | ||
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \ | ||
cosmwasm/rust-optimizer:0.12.10 | ||
""" |
12 changes: 12 additions & 0 deletions
12
templates/okp4-smart-contract/{{ contract_name }}/Makefile.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[tasks.generate_schema] | ||
args = ["run", "--bin", "schema"] | ||
command = "cargo" | ||
|
||
[tasks.schema] | ||
dependencies = ["generate_schema"] | ||
script = ''' | ||
SCHEMA=$(find schema -type f -maxdepth 1 -name '*.json' -print0) | ||
TITLE=$(jq -r .contract_name $SCHEMA) | ||
jq --arg description "$(cat README.md)" '. + {description: $description}' $SCHEMA > $SCHEMA.tmp && mv $SCHEMA.tmp $SCHEMA | ||
jq --arg title $TITLE '. + {title: $title}' $SCHEMA > $SCHEMA.tmp && mv $SCHEMA.tmp $SCHEMA | ||
''' |
1 change: 1 addition & 0 deletions
1
templates/okp4-smart-contract/{{ contract_name }}/README.ffizer.hbs.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# {{ contract_name }} |
11 changes: 11 additions & 0 deletions
11
templates/okp4-smart-contract/{{ contract_name }}/src/bin/schema.ffizer.hbs.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use cosmwasm_schema::write_api; | ||
|
||
use {{ replace contract_name "-" "_" }}::msg::{ExecuteMsg, InstantiateMsg, QueryMsg}; | ||
|
||
fn main() { | ||
write_api! { | ||
instantiate: InstantiateMsg, | ||
execute: ExecuteMsg, | ||
query: QueryMsg, | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
templates/okp4-smart-contract/{{ contract_name }}/src/contract.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#[cfg(not(feature = "library"))] | ||
use cosmwasm_std::entry_point; | ||
use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult}; | ||
use cw2::set_contract_version; | ||
|
||
use crate::error::ContractError; | ||
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg}; | ||
|
||
const CONTRACT_NAME: &str = concat!("crates.io:", env!("CARGO_PKG_NAME")); | ||
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); | ||
|
||
#[cfg_attr(not(feature = "library"), entry_point)] | ||
pub fn instantiate( | ||
deps: DepsMut<'_>, | ||
_env: Env, | ||
_info: MessageInfo, | ||
_msg: InstantiateMsg, | ||
) -> Result<Response, ContractError> { | ||
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; | ||
|
||
Err(StdError::generic_err("Not implemented").into()) | ||
} | ||
|
||
#[cfg_attr(not(feature = "library"), entry_point)] | ||
pub fn execute( | ||
_deps: DepsMut<'_>, | ||
_env: Env, | ||
_info: MessageInfo, | ||
_msg: ExecuteMsg, | ||
) -> Result<Response, ContractError> { | ||
Err(StdError::generic_err("Not implemented").into()) | ||
} | ||
|
||
pub mod execute {} | ||
|
||
#[cfg_attr(not(feature = "library"), entry_point)] | ||
pub fn query(_deps: Deps<'_>, _env: Env, _msg: QueryMsg) -> StdResult<Binary> { | ||
Err(StdError::generic_err("Not implemented")) | ||
} | ||
|
||
pub mod query {} | ||
|
||
#[cfg(test)] | ||
mod tests {} |
8 changes: 8 additions & 0 deletions
8
templates/okp4-smart-contract/{{ contract_name }}/src/error.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use cosmwasm_std::StdError; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug, PartialEq)] | ||
pub enum ContractError { | ||
#[error("{0}")] | ||
Std(#[from] StdError), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
pub mod contract; | ||
mod error; | ||
pub mod msg; | ||
pub mod state; | ||
|
||
pub use crate::error::ContractError; |
28 changes: 28 additions & 0 deletions
28
templates/okp4-smart-contract/{{ contract_name }}/src/msg.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use cosmwasm_schema::{cw_serde, QueryResponses}; | ||
|
||
/// Instantiate message | ||
#[cw_serde] | ||
pub struct InstantiateMsg {} | ||
|
||
/// Execute messages | ||
#[cw_serde] | ||
pub enum ExecuteMsg { | ||
/// # Foo | ||
Foo, | ||
} | ||
|
||
/// Query messages | ||
#[cw_serde] | ||
#[derive(QueryResponses)] | ||
pub enum QueryMsg { | ||
/// # Bar | ||
#[returns(BarResponse)] | ||
Bar { foo: String }, | ||
} | ||
|
||
/// # BarResponse | ||
#[cw_serde] | ||
pub struct BarResponse { | ||
/// The foo value | ||
pub foo: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|