Function provwasm_std::add_json_attribute[][src]

pub fn add_json_attribute<H: Into<Addr>, S: Into<String>, T: Serialize + ?Sized>(
    address: H,
    name: S,
    data: &T
) -> StdResult<CosmosMsg<ProvenanceMsg>>
Expand description

Create a message that will add a JSON attribute to an account. Serializable types can be passed into this function, but it’s up to the user to handle StdResult error case.

Example

// Imports required
use cosmwasm_std::{Env, Addr, Response, StdResult};
use provwasm_std::{add_json_attribute, ProvenanceMsg};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

// Add a label attribute. NOTE: The name below must resolve to the contract address.
fn exec_add_label(
    env: Env,
    address: Addr,
    text: String,
) -> StdResult<Response<ProvenanceMsg>> {
    let attr_name = String::from("label.my-contract.sc.pb");
    let timestamp = env.block.time.nanos();
    let label = Label { text, timestamp };
    let msg = add_json_attribute(address, &attr_name, &label)?;
    let mut res = Response::new();
    res.add_message(msg);
    Ok(res)
}

// Text with a timestamp.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct Label {
    pub text: String,
    pub timestamp: u64,
}