跳到正文
cccskills
免费在 GitHub 公开

api-design

Best practices for designing CosmWasm smart contract APIs. Use when defining message types, designing execute/query interfaces, or optimizing API ergonomics.

中文简介正在准备中。当前显示原文说明。

查看安装方法

包含的文件(1)

  • SKILL.md4.5 KB

SKILL.md(原文)

安装前可以查看交给 AI 代理的指令内容。

CosmWasm API Design Best Practices

Core Principles

  1. Minimalism - Include only what's necessary; avoid bloated APIs
  2. Clarity - Names should be self-documenting
  3. Consistency - Follow established patterns across all contracts
  4. Documentation - Every public type and field must have doc comments

Message Type Patterns

InstantiateMsg

/// Contract instantiation message
#[cosmwasm_schema::cw_serde]
#[derive(Default)]
pub struct MyContractInstantiateMsg {
    /// Optional configuration parameter with sensible default
    #[serde(default)]
    pub some_config: Option<String>,
}

Guidelines:

  • Derive Default when possible for easier testing
  • Use #[serde(default)] for optional fields
  • Keep required fields minimal
  • Document each field

ExecuteMsg

/// Contract execute messages
#[cosmwasm_schema::cw_serde]
#[derive(cw_orch::ExecuteFns)]
pub enum MyContractExecuteMsg {
    /// Update the contract configuration
    UpdateConfig {
        /// New admin address (optional)
        new_admin: Option<String>,
    },
    /// Process an action with the given parameters
    ProcessAction {
        /// Unique identifier for the action
        action_id: String,
        /// Amount to process
        amount: Uint128,
    },
}

Guidelines:

  • Use verb-based names (Update, Process, Create, Remove)
  • Group related parameters in structs if >3 fields
  • Document each variant AND each field
  • Derive ExecuteFns for cw-orch integration

QueryMsg

/// Contract query messages
#[cosmwasm_schema::cw_serde]
#[derive(cw_orch::QueryFns, QueryResponses)]
pub enum MyContractQueryMsg {
    /// Get the current configuration
    #[returns(ConfigResponse)]
    Config {},
    
    /// Get item by ID
    #[returns(ItemResponse)]
    Item {
        /// The item identifier
        id: String,
    },
    
    /// List all items with pagination
    #[returns(ItemsResponse)]
    Items {
        /// Start after this ID for pagination
        start_after: Option<String>,
        /// Maximum number of items to return
        limit: Option<u32>,
    },
}

Guidelines:

  • Always include #[returns(ResponseType)] attribute
  • Use noun-based names for queries
  • Include pagination for list queries (start_after, limit)
  • Derive QueryFns and QueryResponses

Response Types

#[cosmwasm_schema::cw_serde]
pub struct ConfigResponse {
    /// Current admin address
    pub admin: Addr,
    /// Whether the contract is paused
    pub paused: bool,
}

#[cosmwasm_schema::cw_serde]
pub struct ItemsResponse {
    /// List of items
    pub items: Vec<ItemInfo>,
}

Guidelines:

  • Response types should mirror what clients need
  • Use specific types (Addr, Uint128) not strings
  • Document all fields

Abstract SDK Integration

Use the app_msg_types! macro to generate wrapper types:

use crate::contract::MyContract;
use cosmwasm_schema::QueryResponses;

// Generates ExecuteMsg, QueryMsg, InstantiateMsg wrappers
abstract_app::app_msg_types!(MyContract, MyContractExecuteMsg, MyContractQueryMsg);

Documentation Standards

Rust Doc Comments

/// Brief one-line description of the variant.
/// 
/// Optional longer description that explains:
/// - When to use this
/// - Side effects
/// - Related messages
/// 
/// # Errors
/// 
/// Returns `ContractError::Unauthorized` if caller is not admin.

Field Documentation

Every field must have a doc comment:

  • Describe what the field represents
  • Mention default values if applicable
  • Note any constraints (min/max values, format)

Serde Patterns

Optional Fields with Defaults

#[serde(default)]
pub optional_field: Option<String>,

#[serde(default = "default_limit")]
pub limit: u32,

fn default_limit() -> u32 {
    10
}

Flatten for Nested Configs

#[cosmwasm_schema::cw_serde]
pub struct InstantiateMsg {
    #[serde(flatten)]
    pub base_config: BaseConfig,
    pub custom_field: String,
}

Rename for JSON Clarity

#[serde(rename = "owner")]
pub owner_addr: Addr,

评价

还没有评价。欢迎分享你的使用感受。

评论

无需登录即可发表。欢迎分享使用感受、提问,或说明在哪些环境下无法运行。

还没有评论。来写第一条吧。

提交后会立即公开。请勿填写邮箱、电话等个人信息。URL 最多 2 个。

0 / 1000 字

此表单受 reCAPTCHA 保护,适用 Google 的隐私权政策和服务条款。

同一仓库的技能

Guide for writing Rust doc comments that produce accurate generated contract documentation. Use when editing Instantiate/Execute/Query/Response types or any public schema-facing API.

中文简介正在准备中。当前显示原文说明。

axone-protocol/contracts1232026年10月14日 更新

Guide for writing conventional commit messages. Use when committing changes, writing commit messages, or reviewing commit history.

中文简介正在准备中。当前显示原文说明。

axone-protocol/contracts1232026年10月14日 更新

Axone contract structure and Abstract SDK patterns. Use when scaffolding or refactoring contracts, deciding layer boundaries, wiring AppContract entrypoints, or adding module metadata and replies.

中文简介正在准备中。当前显示原文说明。

axone-protocol/contracts1232026年10月14日 更新

Axone deployment workflows with cargo-make, cw-orch, and Abstract. Use when publishing modules, installing them on accounts, running local chain tasks, or inspecting deployments.

中文简介正在准备中。当前显示原文说明。

axone-protocol/contracts1232026年10月14日 更新

Guide for regenerating Axone contract schemas and rendered Markdown docs. Use when contract APIs or metadata change, when checking generated-doc drift, or when preparing documentation commits.

中文简介正在准备中。当前显示原文说明。

axone-protocol/contracts1232026年10月14日 更新

Domain-driven modeling patterns for Axone contracts. Use when introducing domain concepts, encoding invariants, or deciding boundaries between domain, handlers, services, gateways, queries, and state.

中文简介正在准备中。当前显示原文说明。

axone-protocol/contracts1232026年10月14日 更新

查看 axone-protocol 的全部技能

报告此技能的问题