Skip to content

Commit

Permalink
fix(white-whale package): use shim for CosmwasmExt
Browse files Browse the repository at this point in the history
`osmosis-std-derive` expects a shim package at `::crate::shim` which has
an `Any` struct
(https://github.com/osmosis-labs/osmosis-rust/blob/01affba52765e50f43cf52bbc12355fa2dc379f2/packages/osmosis-std-derive/src/lib.rs#L75-L81).

We create a shim file for this.
  • Loading branch information
kaimen-sano committed Jan 6, 2024
1 parent 070cd6e commit 57b31d9
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/white-whale/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ documentation = "https://whitewhale.money"
backtraces = ["cosmwasm-std/backtraces"]
injective = ["token_factory"]
token_factory = ["cosmwasm-std/stargate", "cosmwasm-std/cosmwasm_1_1"]
osmosis_token_factory = ["token_factory"] # this is for the osmosis token factory proto definitions, which defer from the standard token factory :)
osmosis_token_factory = [
"token_factory",
] # this is for the osmosis token factory proto definitions, which defer from the standard token factory :)

[dependencies]
cosmwasm-std.workspace = true
Expand All @@ -30,4 +32,4 @@ protobuf.workspace = true
uint.workspace = true
osmosis-std-derive.workspace = true
prost.workspace = true
prost-types.workspace = true
prost-types.workspace = true
3 changes: 3 additions & 0 deletions packages/white-whale/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ pub mod pool_network;
pub mod traits;
pub mod vault_network;
pub mod whale_lair;

// used in `denom.rs` for `CosmwasmExt`
mod shim;
89 changes: 89 additions & 0 deletions packages/white-whale/src/shim.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// osmosis-std-derive needs a `::crate::shim` to work
// we rip this file from `osmosis-std` package:
// https://github.com/osmosis-labs/osmosis-rust/blob/main/packages/osmosis-std/src/shim.rs
#[derive(Clone, PartialEq, Eq, ::prost::Message, schemars::JsonSchema)]
pub struct Any {
/// A URL/resource name that uniquely identifies the type of the serialized
/// protocol buffer message. This string must contain at least
/// one "/" character. The last segment of the URL's path must represent
/// the fully qualified name of the type (as in
/// `path/google.protobuf.Duration`). The name should be in a canonical form
/// (e.g., leading "." is not accepted).
///
/// In practice, teams usually precompile into the binary all types that they
/// expect it to use in the context of Any. However, for URLs which use the
/// scheme `http`, `https`, or no scheme, one can optionally set up a type
/// server that maps type URLs to message definitions as follows:
///
/// * If no scheme is provided, `https` is assumed.
/// * An HTTP GET on the URL must yield a \[google.protobuf.Type][\]
/// value in binary format, or produce an error.
/// * Applications are allowed to cache lookup results based on the
/// URL, or have them precompiled into a binary to avoid any
/// lookup. Therefore, binary compatibility needs to be preserved
/// on changes to types. (Use versioned type names to manage
/// breaking changes.)
///
/// Note: this functionality is not currently available in the official
/// protobuf release, and it is not used for type URLs beginning with
/// type.googleapis.com.
///
/// Schemes other than `http`, `https` (or the empty scheme) might be
/// used with implementation specific semantics.
///
#[prost(string, tag = "1")]
pub type_url: ::prost::alloc::string::String,
/// Must be a valid serialized protocol buffer of the above specified type.
#[prost(bytes = "vec", tag = "2")]
pub value: ::prost::alloc::vec::Vec<u8>,
}

macro_rules! impl_prost_types_exact_conversion {
($t:ident | $($arg:ident),*) => {
impl From<$t> for prost_types::$t {
fn from(src: $t) -> Self {
prost_types::$t {

Check warning on line 45 in packages/white-whale/src/shim.rs

View check run for this annotation

Codecov / codecov/patch

packages/white-whale/src/shim.rs#L44-L45

Added lines #L44 - L45 were not covered by tests
$(
$arg: src.$arg,

Check warning on line 47 in packages/white-whale/src/shim.rs

View check run for this annotation

Codecov / codecov/patch

packages/white-whale/src/shim.rs#L47

Added line #L47 was not covered by tests
)*
}
}
}

impl From<prost_types::$t> for $t {
fn from(src: prost_types::$t) -> Self {
$t {

Check warning on line 55 in packages/white-whale/src/shim.rs

View check run for this annotation

Codecov / codecov/patch

packages/white-whale/src/shim.rs#L54-L55

Added lines #L54 - L55 were not covered by tests
$(
$arg: src.$arg,

Check warning on line 57 in packages/white-whale/src/shim.rs

View check run for this annotation

Codecov / codecov/patch

packages/white-whale/src/shim.rs#L57

Added line #L57 was not covered by tests
)*
}
}
}
};
}

impl_prost_types_exact_conversion! { Any | type_url, value }

// depended on by cosmwasm.rs
pub mod as_str {
use serde::{de, Deserialize, Deserializer, Serializer};
use std::{fmt::Display, str::FromStr};

pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
T: FromStr,
T::Err: Display,
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
T::from_str(&s).map_err(de::Error::custom)
}

pub fn serialize<S, T>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
T: Display,
{
serializer.serialize_str(&value.to_string())
}
}

0 comments on commit 57b31d9

Please sign in to comment.