#![allow(deprecated)]
use cosmwasm_std::{Binary, CosmosMsg, QueryRequest};
use crate::errors::{NibiruError, NibiruResult};
use crate::proto::cosmos;
pub trait NibiruProstMsg: prost::Message {
fn to_bytes(&self) -> Vec<u8>;
fn to_binary(&self) -> Binary;
fn try_into_stargate_msg(&self, type_url: &str) -> CosmosMsg {
let value = self.to_binary();
CosmosMsg::Stargate {
type_url: type_url.to_string(),
value,
}
}
fn from_any(any: &prost_types::Any) -> Result<Self, prost::DecodeError>
where
Self: Default + prost::Name + Sized,
{
any.to_msg()
}
}
impl<M> NibiruProstMsg for M
where
M: prost::Message,
{
fn to_bytes(&self) -> Vec<u8> {
self.encode_to_vec()
}
fn to_binary(&self) -> Binary {
Binary::from(self.encode_to_vec())
}
}
pub trait NibiruStargateMsg: prost::Message + prost::Name {
#![allow(clippy::wrong_self_convention)]
fn into_stargate_msg(&self) -> CosmosMsg;
fn type_url(&self) -> String;
}
impl<M> NibiruStargateMsg for M
where
M: prost::Message + prost::Name,
{
fn into_stargate_msg(&self) -> CosmosMsg {
CosmosMsg::Stargate {
type_url: self.type_url(),
value: self.to_binary(),
}
}
fn type_url(&self) -> String {
format!("/{}.{}", Self::PACKAGE, Self::NAME)
}
}
pub trait NibiruStargateQuery: prost::Message + prost::Name {
#![allow(clippy::wrong_self_convention)]
fn into_stargate_query(
&self,
) -> NibiruResult<QueryRequest<cosmwasm_std::Empty>>;
fn path(&self) -> String;
}
impl<M> NibiruStargateQuery for M
where
M: prost::Message + prost::Name,
{
fn into_stargate_query(
&self,
) -> NibiruResult<QueryRequest<cosmwasm_std::Empty>> {
if !self.type_url().contains("Query") {
return Err(NibiruError::ProstNameisNotQuery {
type_url: self.type_url(),
});
}
Ok(QueryRequest::Stargate {
path: self.path(),
data: self.to_binary(),
})
}
fn path(&self) -> String {
let service_name = format!(
"Query/{}",
Self::NAME
.trim_start_matches("Query")
.trim_end_matches("Request")
);
format!("/{}.{}", Self::PACKAGE, service_name)
}
}
impl From<cosmwasm_std::Coin> for cosmos::base::v1beta1::Coin {
fn from(cw_coin: cosmwasm_std::Coin) -> Self {
cosmos::base::v1beta1::Coin {
denom: cw_coin.denom,
amount: cw_coin.amount.to_string(),
}
}
}