Writing Smart Contracts on Real #
Prerequisites #
cargo install cosmwasm-check
Step 1: Set Up Your Development Environment #
-
cargo new --lib real-first-contract cd real-first-contract -
[package]
name = "real-first-contract"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies]
cosmwasm-std = "1.5.0"
cosmwasm-schema = "1.5.0"
serde = { version = "1.0", features = ["derive"] }
Step 2: Write a Simple Smart Contract #
use cosmwasm_std::{
entry_point, to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult,
};
use cosmwasm_schema::{cw_serde, QueryResponses};
#[cw_serde]
pub struct InstantiateMsg {
pub count: i32,
}
#[cw_serde]
pub enum ExecuteMsg {
Increment {},
}
#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
#[returns(GetCountResponse)]
GetCount {},
}
#[cw_serde]
pub struct GetCountResponse {
pub count: i32,
}
// State storage
#[cw_serde]
pub struct State {
pub count: i32,
}
const CONTRACT_STATE: cosmwasm_std::Item<State> = cosmwasm_std::Item::new("state");
#[entry_point]
pub fn instantiate(
deps: DepsMut,
_env: Env,
_info: MessageInfo,
msg: InstantiateMsg,
) -> StdResult<Response> {
let state = State { count: msg.count };
CONTRACT_STATE.save(deps.storage, &state)?;
Ok(Response::new().add_attribute("method", "instantiate"))
}
#[entry_point]
pub fn execute(
deps: DepsMut,
_env: Env,
_info: MessageInfo,
msg: ExecuteMsg,
) -> StdResult<Response> {
match msg {
ExecuteMsg::Increment {} => {
let mut state = CONTRACT_STATE.load(deps.storage)?;
state.count += 1;
CONTRACT_STATE.save(deps.storage, &state)?;
Ok(Response::new().add_attribute("method", "increment"))
}
}
}
#[entry_point]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::GetCount {} => {
let state = CONTRACT_STATE.load(deps.storage)?;
to_binary(&GetCountResponse { count: state.count })
}
}
}
Step 3: Compile and Optimize Your Contract #
cargo build --release --target wasm32-unknown-unknown
cosmwasm-check target/wasm32-unknown-unknown/release/real_first_contract.wasm
Step 4: Deploy the Smart Contract on Real #
reald tx wasm store target/wasm32-unknown-unknown/release/real_first_contract.wasm --from <YOUR_WALLET> --chain-id real-testnet-1 --gas auto --fees 5000ureal
reald tx wasm instantiate 1 '{"count": 0}' --from <YOUR_WALLET> --chain-id real-testnet-1 --label "Real First Contract" --gas auto --fees 5000ureal
-
reald tx wasm execute <CONTRACT_ADDRESS> '{"increment": {}}' --from <YOUR_WALLET> --chain-id real-testnet-1 --gas auto --fees 5000ureal -
reald query wasm contract-state smart <CONTRACT_ADDRESS> '{"get_count": {}}' --chain-id real-testnet-1