use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, JsonSchema)]
pub struct Params {
pub block: BlockInfo,
pub message: MessageInfo,
pub contract: ContractInfo,
}
#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, JsonSchema)]
pub struct BlockInfo {
pub height: i64,
pub time: i64,
pub chain_id: String,
}
#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, JsonSchema)]
pub struct MessageInfo {
pub signer: String,
pub sent_funds: Option<Vec<Coin>>,
}
#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, JsonSchema)]
pub struct ContractInfo {
pub address: String,
pub balance: Option<Vec<Coin>>,
}
#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, JsonSchema)]
pub struct Coin {
pub denom: String,
pub amount: String,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum CosmosMsg {
Send {
from_address: String,
to_address: String,
amount: Vec<Coin>,
},
Contract {
contract_addr: String,
msg: String,
send: Vec<Coin>,
},
Opaque {
data: String,
},
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum ContractResult {
Ok(Response),
Err(String),
}
impl ContractResult {
pub fn unwrap(self) -> Response {
match self {
ContractResult::Err(msg) => panic!("Unexpected error: {}", msg),
ContractResult::Ok(res) => res,
}
}
pub fn is_err(&self) -> bool {
match self {
ContractResult::Err(_) => true,
_ => false,
}
}
}
#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, JsonSchema)]
pub struct Response {
pub messages: Vec<CosmosMsg>,
pub log: Option<String>,
pub data: Option<String>,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct RawQuery {
pub key: String,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum QueryResult {
Ok(QueryResponse),
Err(String),
}
#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, JsonSchema)]
pub struct QueryResponse {
pub results: Vec<Model>,
}
#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, JsonSchema)]
pub struct Model {
pub key: String,
pub val: Vec<u8>,
}
impl QueryResult {
pub fn unwrap(self) -> QueryResponse {
match self {
QueryResult::Err(msg) => panic!("Unexpected error: {}", msg),
QueryResult::Ok(res) => res,
}
}
pub fn is_err(&self) -> bool {
match self {
QueryResult::Err(_) => true,
_ => false,
}
}
}
pub fn mock_params(signer: &str, sent: &[Coin], balance: &[Coin]) -> Params {
Params {
block: BlockInfo {
height: 12_345,
time: 1_571_797_419,
chain_id: "cosmos-testnet-14002".to_string(),
},
message: MessageInfo {
signer: signer.to_string(),
sent_funds: if sent.len() == 0 {
None
} else {
Some(sent.to_vec())
},
},
contract: ContractInfo {
address: "cosmos2contract".to_string(),
balance: if balance.len() == 0 {
None
} else {
Some(balance.to_vec())
},
},
}
}
pub fn coin(amount: &str, denom: &str) -> Vec<Coin> {
vec![Coin {
amount: amount.to_string(),
denom: denom.to_string(),
}]
}
#[cfg(test)]
mod test {
use super::*;
use crate::serde::{from_slice, to_vec};
#[test]
fn can_deser_error_result() {
let fail = ContractResult::Err("foobar".to_string());
let bin = to_vec(&fail).expect("encode contract result");
println!("error: {}", std::str::from_utf8(&bin).unwrap());
let back: ContractResult = from_slice(&bin).expect("decode contract result");
assert_eq!(fail, back);
}
#[test]
fn can_deser_ok_result() {
let send = ContractResult::Ok(Response {
messages: vec![CosmosMsg::Send {
from_address: "me".to_string(),
to_address: "you".to_string(),
amount: coin("1015", "earth"),
}],
log: Some("released funds!".to_string()),
data: None,
});
let bin = to_vec(&send).expect("encode contract result");
println!("ok: {}", std::str::from_utf8(&bin).unwrap());
let back: ContractResult = from_slice(&bin).expect("decode contract result");
assert_eq!(send, back);
}
}