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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
use crate::core_types::{Coin, MsgInternal};
use crate::terra_u64_format;
use std::path::Path;

use crate::errors::TerraRustAPIError;
use crate::messages::Message;
use serde::Serialize;

#[derive(Serialize, Debug)]
/// Message: Exec Contract
pub struct MsgExecuteContract {
    pub coins: Vec<Coin>,
    pub contract: String,
    pub execute_msg: serde_json::Value,
    pub sender: String,
}

impl MsgInternal for MsgExecuteContract {}
impl MsgExecuteContract {
    /// use provided base64 exec message
    pub fn create_from_value(
        sender: &str,
        contract: &str,
        execute_msg: &serde_json::Value,
        coins: &[Coin],
    ) -> Result<Message, TerraRustAPIError> {
        let internal = MsgExecuteContract {
            sender: sender.into(),
            contract: contract.into(),
            execute_msg: execute_msg.clone(),
            coins: coins.to_vec(),
        };
        Ok(Message {
            s_type: "wasm/MsgExecuteContract".into(),
            value: serde_json::to_value(internal)?,
        })
    }
    /// use provided base64 exec message
    pub fn create_from_json(
        sender: &str,
        contract: &str,
        execute_msg_json: &str,
        coins: &[Coin],
    ) -> Result<Message, TerraRustAPIError> {
        let exec_b64: serde_json::Value = serde_json::from_str(execute_msg_json)?;
        MsgExecuteContract::create_from_value(sender, contract, &exec_b64, coins)
    }
}

#[derive(Serialize, Debug)]
/// Message: Exec Contract
pub struct MsgStoreCode {
    pub sender: String,
    pub wasm_byte_code: String,
}

impl MsgInternal for MsgStoreCode {}
impl MsgStoreCode {
    /// use provided base64 exec message
    pub fn create_from_b64(
        sender: &str,
        wasm_byte_code: &str,
    ) -> Result<Message, TerraRustAPIError> {
        let internal = MsgStoreCode {
            sender: sender.into(),
            wasm_byte_code: wasm_byte_code.into(),
        };
        Ok(Message {
            s_type: "wasm/MsgStoreCode".into(),
            value: serde_json::to_value(internal)?,
        })
    }
    /// use provided base64 exec message
    pub fn create_from_file(sender: &str, file_name: &Path) -> Result<Message, TerraRustAPIError> {
        let file_contents = std::fs::read(file_name)?;
        let exec_b64 = base64::encode(file_contents);
        MsgStoreCode::create_from_b64(sender, &exec_b64)
    }
}

#[derive(Serialize, Debug)]
/// Message: Exec Contract
pub struct MsgInstantiateContract {
    pub admin: String,
    #[serde(with = "terra_u64_format")]
    pub code_id: u64,
    pub sender: String,
    pub init_coins: Vec<Coin>,
    pub init_msg: serde_json::Value,
}

impl MsgInternal for MsgInstantiateContract {}
impl MsgInstantiateContract {
    /*
    /// use provided base64 exec message
    pub fn create_from_b64(
        sender: &str,
        admin: Option<String>,
        code_id: usize,
        init_msg: &str,
        init_coins: Vec<Coin>,
    ) -> Message {
        let internal = MsgInstantiateContract {
            admin: admin.map(|f| f.into()),
            code_id: code_id.to_string(),
            sender: sender.into(),
            init_coins,
            init_msg: init_msg.into(),
        };
        Message {
            s_type: "wasm/MsgInstantiateContract".into(),
            value: Box::new(internal),
        }
    }

     */
    /// create from JSON
    pub fn create_from_json(
        sender: &str,
        admin: Option<String>,
        code_id: u64,
        init_msg: &str,
        init_coins: Vec<Coin>,
    ) -> Result<Message, TerraRustAPIError> {
        // panic!("This message does not function");

        let contents: serde_json::Value = serde_json::from_str(init_msg)?;
        //let exec_b64 = base64::encode(contents.to_string());

        let internal = MsgInstantiateContract {
            admin: admin.unwrap_or_else(|| "".into()),
            code_id,
            sender: sender.into(),
            init_coins,
            init_msg: contents,
        };
        Ok(Message {
            s_type: "wasm/MsgInstantiateContract".into(),
            value: serde_json::to_value(internal)?,
        })
    }
    /// use provided base64 exec message
    /// switches ##SENDER##, ##ADMIN##, ##CODE_ID## with respective values
    pub fn create_from_file(
        sender: &str,
        admin: Option<String>,
        code_id: u64,
        init_file: &Path,
        init_coins: Vec<Coin>,
    ) -> Result<Message, TerraRustAPIError> {
        let contents = std::fs::read_to_string(init_file)?;
        let new_contents = Self::replace_parameters(sender, admin.clone(), code_id, &contents);
        Self::create_from_json(sender, admin, code_id, &new_contents, init_coins)
    }
    /// replace parts of the string with current values
    pub fn replace_parameters(
        sender: &str,
        admin: Option<String>,
        code_id: u64,
        instantiate_str: &str,
    ) -> String {
        let part_1 = instantiate_str
            .replace("##SENDER##", sender)
            .replace("##CODE_ID##", &format!("{}", code_id));
        match admin {
            Some(admin_str) => part_1.replace("##ADMIN##", &admin_str),
            None => part_1.replace("##ADMIN##", ""),
        }
    }
}

#[derive(Serialize, Debug)]
/// Message: Exec Contract
pub struct MsgMigrateContract {
    pub admin: String,
    pub contract: String,
    #[serde(with = "terra_u64_format")]
    pub new_code_id: u64,
    pub migrate_msg: serde_json::Value,
}

impl MsgInternal for MsgMigrateContract {}
impl MsgMigrateContract {
    /// create from JSON
    pub fn create_from_json(
        admin: &str,
        contract: &str,
        new_code_id: u64,
        migrate_msg: &str,
    ) -> Result<Message, TerraRustAPIError> {
        let contents: serde_json::Value = serde_json::from_str(migrate_msg)?;

        let internal = MsgMigrateContract {
            admin: String::from(admin),
            contract: String::from(contract),
            new_code_id,
            migrate_msg: contents,
        };
        Ok(Message {
            s_type: "wasm/MsgMigrateContract".into(),
            value: serde_json::to_value(internal)?,
        })
    }
    /// use provided base64 exec message
    /// switches ##SENDER##, ##ADMIN##, ##CODE_ID## with respective values
    pub fn create_from_file(
        admin: &str,
        contract: &str,
        new_code_id: u64,
        migrate_file: &Path,
    ) -> Result<Message, TerraRustAPIError> {
        let contents = std::fs::read_to_string(migrate_file)?;
        let new_contents = Self::replace_parameters(admin, contract, new_code_id, &contents);

        Self::create_from_json(admin, contract, new_code_id, &new_contents)
    }
    /// switches ##SENDER##, ##ADMIN##, ##CODE_ID## with respective values
    pub fn replace_parameters(
        admin: &str,
        contract: &str,
        new_code_id: u64,
        migrate_str: &str,
    ) -> String {
        migrate_str
            .replace("##ADMIN##", admin)
            .replace("##CONTRACT##", contract)
            .replace("##NEW_CODE_ID##", &format!("{}", new_code_id))
    }
}

#[cfg(test)]
mod tst {
    use super::*;
    /*
    #[test]
    pub fn test_b64() -> anyhow::Result<()> {
        let vote_1 = MsgExecuteContract::create_from_b64(
            "terra1vr0e7kylhu9am44v0s3gwkccmz7k3naxysrwew",
            "terra1f32xyep306hhcxxxf7mlyh0ucggc00rm2s9da5",
            "eyJjYXN0X3ZvdGUiOnsicG9sbF9pZCI6NDQsInZvdGUiOiJ5ZXMiLCJhbW91bnQiOiIxMDAwMDAwMCJ9fQ==",
            &vec![],
        );
        let vote_2 = MsgExecuteContract::create_from_json(
            "terra1vr0e7kylhu9am44v0s3gwkccmz7k3naxysrwew",
            "terra1f32xyep306hhcxxxf7mlyh0ucggc00rm2s9da5",
            r#"{"cast_vote":{"poll_id":44,"vote":"yes","amount":"10000000"}}"#,
            &vec![],
        );

        let js_1 = serde_json::to_string(&vote_1)?;
        let js_2 = serde_json::to_string(&vote_2)?;

        assert_eq!(js_1, js_2);
        let js_real = r#"{"type":"wasm/MsgExecuteContract","value":{"coins":[],"contract":"terra1f32xyep306hhcxxxf7mlyh0ucggc00rm2s9da5","execute_msg":"eyJjYXN0X3ZvdGUiOnsicG9sbF9pZCI6NDQsInZvdGUiOiJ5ZXMiLCJhbW91bnQiOiIxMDAwMDAwMCJ9fQ==","sender":"terra1vr0e7kylhu9am44v0s3gwkccmz7k3naxysrwew"}}"#;
        assert_eq!(js_1, js_real);
        Ok(())
    }

     */
    #[test]
    pub fn test_file_conversion() -> anyhow::Result<()> {
        let token_file = Path::new("./resources/terraswap_pair.wasm");
        let output = Path::new("./resources/terraswap_pair.output");
        let out_json = std::fs::read_to_string(output)?;
        let msg = MsgStoreCode::create_from_file(
            "terra1vr0e7kylhu9am44v0s3gwkccmz7k3naxysrwew",
            token_file,
        )?;

        let js = serde_json::to_string(&msg)?;
        log::debug!("Test file conversion: {} - {}", out_json.len(), js.len());
        assert_eq!(out_json.trim(), js);
        Ok(())
    }
}