1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use schemars::JsonSchema;
use std::convert::TryFrom;
use std::fmt;

use crate::binary::Binary;
use crate::errors::StdError;
use crate::types::Empty;

use super::attribute::{attr, Attribute};
use super::cosmos_msg::CosmosMsg;
use super::handle::HandleResponse;
use super::init::InitResponse;
use super::migrate::MigrateResponse;

#[derive(Clone, Debug, PartialEq)]
pub struct Context<T = Empty>
where
    T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
    messages: Vec<CosmosMsg<T>>,
    /// The attributes that will be emitted as part of a "wasm" event
    attributes: Vec<Attribute>,
    data: Option<Binary>,
}

impl<T> Default for Context<T>
where
    T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
    fn default() -> Self {
        Context {
            messages: vec![],
            attributes: vec![],
            data: None,
        }
    }
}

impl<T> Context<T>
where
    T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
    pub fn new() -> Self {
        Context::default()
    }

    pub fn add_attribute<K: ToString, V: ToString>(&mut self, key: K, value: V) {
        self.attributes.push(attr(key, value));
    }

    pub fn add_message<U: Into<CosmosMsg<T>>>(&mut self, msg: U) {
        self.messages.push(msg.into());
    }

    pub fn set_data<U: Into<Binary>>(&mut self, data: U) {
        self.data = Some(data.into());
    }
}

impl<T> TryFrom<Context<T>> for InitResponse<T>
where
    T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
    type Error = StdError;

    fn try_from(ctx: Context<T>) -> Result<Self, Self::Error> {
        if ctx.data.is_some() {
            Err(StdError::generic_err(
                "cannot convert Context with data to InitResponse",
            ))
        } else {
            Ok(InitResponse {
                messages: ctx.messages,
                attributes: ctx.attributes,
            })
        }
    }
}

impl<T> From<Context<T>> for HandleResponse<T>
where
    T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
    fn from(ctx: Context<T>) -> Self {
        HandleResponse {
            messages: ctx.messages,
            attributes: ctx.attributes,
            data: ctx.data,
        }
    }
}

impl<T> From<Context<T>> for MigrateResponse<T>
where
    T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
    fn from(ctx: Context<T>) -> Self {
        MigrateResponse {
            messages: ctx.messages,
            attributes: ctx.attributes,
            data: ctx.data,
        }
    }
}

#[cfg(test)]
mod test {
    use super::super::{BankMsg, HandleResponse, InitResponse, MigrateResponse};
    use super::*;
    use crate::addresses::HumanAddr;
    use crate::coins;
    use crate::errors::{StdError, StdResult};
    use std::convert::TryInto;

    #[test]
    fn empty_context() {
        let ctx = Context::new();

        let init: InitResponse = ctx.clone().try_into().unwrap();
        assert_eq!(init, InitResponse::default());

        let init: HandleResponse = ctx.clone().try_into().unwrap();
        assert_eq!(init, HandleResponse::default());

        let init: MigrateResponse = ctx.clone().try_into().unwrap();
        assert_eq!(init, MigrateResponse::default());
    }

    #[test]
    fn full_context() {
        let mut ctx = Context::new();

        // build it up with the builder commands
        ctx.add_attribute("sender", &HumanAddr::from("john"));
        ctx.add_attribute("action", "test");
        ctx.add_message(BankMsg::Send {
            from_address: HumanAddr::from("goo"),
            to_address: HumanAddr::from("foo"),
            amount: coins(128, "uint"),
        });

        // and this is what is should return
        let expected_msgs = vec![CosmosMsg::Bank(BankMsg::Send {
            from_address: HumanAddr::from("goo"),
            to_address: HumanAddr::from("foo"),
            amount: coins(128, "uint"),
        })];
        let expected_attributes = vec![attr("sender", "john"), attr("action", "test")];
        let expected_data = Some(Binary::from(b"banana"));

        // try InitResponse before setting data
        let init: InitResponse = ctx.clone().try_into().unwrap();
        assert_eq!(&init.messages, &expected_msgs);
        assert_eq!(&init.attributes, &expected_attributes);

        ctx.set_data(b"banana");
        // should fail with data set
        let init_err: StdResult<InitResponse> = ctx.clone().try_into();
        match init_err.unwrap_err() {
            StdError::GenericErr { msg, .. } => {
                assert_eq!(msg, "cannot convert Context with data to InitResponse")
            }
            err => panic!("Unexpected error: {:?}", err),
        }

        // try Handle with everything set
        let handle: HandleResponse = ctx.clone().try_into().unwrap();
        assert_eq!(&handle.messages, &expected_msgs);
        assert_eq!(&handle.attributes, &expected_attributes);
        assert_eq!(&handle.data, &expected_data);

        // try Migrate with everything set
        let migrate: MigrateResponse = ctx.clone().try_into().unwrap();
        assert_eq!(&migrate.messages, &expected_msgs);
        assert_eq!(&migrate.attributes, &expected_attributes);
        assert_eq!(&migrate.data, &expected_data);
    }
}