account_query

Attribute Macro account_query 

Source
#[account_query]
Expand description

Procedural macro to extend an enum with standardized smart account query variants.

This macro checks whether the input enum is generic over T and inserts variants accordingly. If the enum is generic over T, the CanExecute variant uses CosmosMsg<T>; otherwise, it uses the non-generic CosmosMsg. It supports an optional custom payload type to override the default Binary type for the payload field, which remains wrapped in Option.

§Arguments

The macro accepts zero or one type argument via the attribute:

  • Payload type (optional): The type for the payload field in the ValidSignature variant (e.g., CustomPayload), wrapped in Option. Defaults to Option<cosmwasm_std::Binary> if not provided.

§Generated Variants

The macro inserts the following query variants:

  • CanExecute: Queries whether a message can be executed by a smart account.
  • ValidSignature: Verifies a single signature against provided data and an optional payload.

§Notes

  • The #[account_query_one] attribute must be applied before #[cw_serde] or other derive macros.
  • The enum must derive #[derive(QueryResponses)] from cosmwasm_schema to support the #[returns(...)] attributes used in the generated variants.
  • This macro is designed for single-signature validation, excluding the ValidSignatures variant included in the multi version.

§Examples

§Example 1: Basic usage with default payload type

use cw82::account_query_one;
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{Binary, Empty};

#[account_query_one]
#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
    // User-defined queries
}

// Generated:
// pub enum QueryMsg {
//     // User-defined queries
//
//     #[returns(::cw82::CanExecuteResponse)]
//     CanExecute {
//         sender: String,
//         msg: ::cosmwasm_std::CosmosMsg,
//     },
//
//     #[returns(::cw82::ValidSignatureResponse)]
//     ValidSignature {
//         data: ::cosmwasm_std::Binary,
//         signature: ::cosmwasm_std::Binary,
//         payload: Option<::cosmwasm_std::Binary>,
//     },
// }

§Example 2: Usage with a custom payload type

#[cw_serde]
pub struct CustomPayload {
    pub metadata: String,
}

#[account_query_one(CustomPayload)]
#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
    // User-defined queries
}

// Generated:
// pub enum QueryMsg {
//     // User-defined queries
//
//     #[returns(::cw82::CanExecuteResponse)]
//     CanExecute {
//         sender: String,
//         msg: ::cosmwasm_std::CosmosMsg,
//     },
//
//     #[returns(::cw82::ValidSignatureResponse)]
//     ValidSignature {
//         data: ::cosmwasm_std::Binary,
//         signature: ::cosmwasm_std::Binary,
//         payload: Option<CustomPayload>,
//     },
// }

§Example 3: Generic enum with custom payload type

#[account_query_one(CustomPayload)]
#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsgCustom<T> {
    // User-defined queries
}

// Generated:
// pub enum QueryMsgCustom<T> {
//     // User-defined queries
//
//     #[returns(::cw82::CanExecuteResponse)]
//     CanExecute {
//         sender: String,
//         msg: ::cosmwasm_std::CosmosMsg<T>,
//     },
//
//     #[returns(::cw82::ValidSignatureResponse)]
//     ValidSignature {
//         data: ::cosmwasm_std::Binary,
//         signature: ::cosmwasm_std::Binary,
//         payload: Option<CustomPayload>,
//     },
// }

§Errors

  • Fails with a compile-time error if the attribute argument is invalid (e.g., not a type path or more than one argument).
  • Fails if the input is not a valid enum or if the merge with generated variants cannot be performed.

This macro is part of the CW82 specification for smart accounts.