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
use crate::client::wasm_types::{
    WasmCodeResult, WasmContractInfoResult, WasmParameterResult, WasmQueryRawResult,
};
use crate::Terra;

use serde::Deserialize;

pub struct Wasm<'a> {
    terra: &'a Terra,
}

impl Wasm<'_> {
    pub fn create(terra: &'_ Terra) -> Wasm<'_> {
        Wasm { terra }
    }
    pub async fn codes(&self, code_id: u64) -> anyhow::Result<WasmCodeResult> {
        let code = self
            .terra
            .send_cmd::<WasmCodeResult>(&format!("/wasm/codes/{}", code_id), None)
            .await?;
        Ok(code)
    }
    pub async fn info(&self, contract_address: &str) -> anyhow::Result<WasmContractInfoResult> {
        let code = self
            .terra
            .send_cmd::<WasmContractInfoResult>(
                &format!("/wasm/contracts/{}", contract_address),
                None,
            )
            .await?;
        Ok(code)
    }
    pub async fn parameters(&self) -> anyhow::Result<WasmParameterResult> {
        let code = self
            .terra
            .send_cmd::<WasmParameterResult>("/wasm/parameters", None)
            .await?;
        Ok(code)
    }
    pub async fn query<T: for<'de> Deserialize<'de>>(
        &self,
        contract_address: &str,
        json_query: &str,
    ) -> anyhow::Result<T> {
        let code = self
            .terra
            .send_cmd::<T>(
                &format!("/wasm/contracts/{}/store?", contract_address),
                Some(&format!("query_msg={}", json_query)),
            )
            .await?;
        Ok(code)
    }
    pub async fn query_raw(
        &self,
        contract_address: &str,
        key: &str,
        sub_key: &Option<String>,
    ) -> anyhow::Result<(String, String)> {
        let json_query = match sub_key {
            Some(sub_key_str) => format!("key={}&subkey={}", key, &sub_key_str),
            None => format!("key={}", key),
        };

        let code = self
            .terra
            .send_cmd::<WasmQueryRawResult>(
                &format!("/wasm/contracts/{}/store/raw?", contract_address),
                Some(&json_query),
            )
            .await?;
        let key_vec = subtle_encoding::base64::decode(code.result.key.as_bytes())?;
        let key = String::from_utf8(key_vec)?;
        eprintln!("{}", code.result.key);
        let value_vec = subtle_encoding::base64::decode(code.result.value)?;
        let value = String::from_utf8(value_vec)?;

        Ok((key, value))
    }
}