Skip to content

Commit

Permalink
chore: revised description to match Finschia module (#26)
Browse files Browse the repository at this point in the history
* chore: revised description to match Finschia module

* rename: change the file name from "osmosis" to "finschia"
  • Loading branch information
Kynea0b authored Mar 26, 2024
1 parent 813d0f4 commit a554ae8
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 137 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
> :information_desk_person: If you want to use latest update from osmosis' main branch, checkout `autobuild-main` branch.
> :information_desk_person: If you want to use latest update from finschia' main branch, checkout `autobuild-main` branch.
# finschia-std

Expand Down
4 changes: 2 additions & 2 deletions osmosis-rust.code-workspace → finschia-rust.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"path": "."
},
{
"path": "packages/osmosis-std"
"path": "packages/finschia-std"
},
{
"path": "packages/osmosis-std-derive"
"path": "packages/finschia-std-derive"
},
{
"path": "packages/proto-build"
Expand Down
6 changes: 2 additions & 4 deletions packages/finschia-std-derive/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# osmosis-std-derive
# finschia-std-derive

[![osmosis-std-derive on crates.io](https://img.shields.io/crates/v/osmosis-std-derive.svg)](https://crates.io/crates/osmosis-std-derive) [![Docs](https://docs.rs/osmosis-std-derive/badge.svg)](https://docs.rs/osmosis-std-derive)

Procedural macro for augmenting proto-generated types to create better developer ergonomics. Internally used by `osmosis-std`
Procedural macro for augmenting proto-generated types to create better developer ergonomics. Internally used by `finschia-std`
2 changes: 1 addition & 1 deletion packages/finschia-std/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
description = "Standard library for Osmosis with CosmWasm support included"
description = "Standard library for Finschia with CosmWasm support included"
edition = "2021"
license = "MIT OR Apache-2.0"
name = "finschia-std"
Expand Down
145 changes: 25 additions & 120 deletions packages/finschia-std/README.md
Original file line number Diff line number Diff line change
@@ -1,141 +1,46 @@
# osmosis-std
# finschia-std

[![osmosis-std on crates.io](https://img.shields.io/crates/v/osmosis-std.svg)](https://crates.io/crates/osmosis-std) [![Docs](https://docs.rs/osmosis-std/badge.svg)](https://docs.rs/osmosis-std)

Osmosis's proto-generated types and helpers for interacting with the appchain. Compatible with CosmWasm contract.
Finschia's proto-generated types and helpers for interacting with the appchain. Compatible with CosmWasm contract.

## CosmWasm stargate message and stargate query

You can find all types and querier generated from osmosis's protobuf in their respective module in `osmosis_std`. To understand how each module works, please look at the [osmosis documentation](https://docs.osmosis.zone/osmosis-core/modules).
You can find all types and querier generated from finschia's protobuf in their respective module in `finschia_std`. To understand how each module works, please look at the [finschia-sdk modules](https://github.com/Finschia/finschia-sdk/blob/main/x/README.md).

[Full working example contract can be found here.](https://github.com/osmosis-labs/osmosis-rust/tree/main/examples/cosmwasm/contracts/osmosis-stargate)
[Full working example contract can be found here.](https://github.com/Finschia/finschia-wasm/tree/main/examples/contracts/collection)

### Publishing Osmosis' message from CosmWasm Contract
### Publishing Finschia' message from CosmWasm Contract

```rust
use cosmwasm_std::{CosmosMsg, Response, Env};
use osmosis_std::types::osmosis::tokenfactory::v1beta1::MsgCreateDenom;
use cosmwasm_std::{
CosmosMsg, DepsMut
};
use finschia_std::types::lbm::collection::v1::MsgIssueNft;

# type ContractError = cosmwasm_std::StdError;
// ..

pub fn try_create_denom(env: Env, subdenom: String) -> Result<Response, ContractError> {
let sender = env.contract.address.into();

// construct message and convert them into cosmos message
// (notice `CosmosMsg` type and `.into()`)
let msg_create_denom: CosmosMsg = MsgCreateDenom { sender, subdenom }.into();
pub fn try_issue_nft(
deps: DepsMut,
name: String,
meta: String,
owner: String,
) -> Result<Response, ContractError> {
let contract_id = CONTRACT_ID.load(deps.storage)?;
let msg_issue_nft: CosmosMsg = MsgIssueNft {
contract_id,
name,
meta,
owner,
}
.into();

Ok(Response::new()
.add_message(msg_create_denom)
.add_attribute("method", "try_create_denom"))
}

```

## Querying Osmosis' module

Each module has its own querier that derived from protobuf service definition that can be found [here](https://github.com/osmosis-labs/osmosis/tree/v12.1.0/proto/osmosis).

To avoid non-determinism in stargate queries, only some of them are whitelisted, you can find the list [here](https://github.com/osmosis-labs/osmosis/blob/v12.1.0/wasmbinding/stargate_whitelist.go).

```rust
use cosmwasm_std::{Deps, Env, StdResult};
use osmosis_std::types::osmosis::tokenfactory::v1beta1::{TokenfactoryQuerier, QueryDenomsFromCreatorResponse};

// ..

fn query_creator_denoms(deps: Deps, env: Env) -> StdResult<QueryDenomsFromCreatorResponse> {
// create `TokenfactoryQuerier`
let tokenfactory = TokenfactoryQuerier::new(&deps.querier);

// `TokenfactoryQuerier` has all the fns for querying the module
let res = tokenfactory.denoms_from_creator(env.contract.address.into())?;

Ok(QueryDenomsFromCreatorResponse { denoms: res.denoms })
}
```

## Querying Pool

When querying pool related values, eg. `Gamm::pool`, you might find that return type contains `Any`. It's a cosmos' way to implement polymorphism in protobuf.

https://github.com/osmosis-labs/osmosis/blob/f024498f1e8e0d2a1fe259cd9cc4223803fea0cd/proto/osmosis/gamm/v1beta1/query.proto#L82-L84

```proto
message QueryPoolResponse {
google.protobuf.Any pool = 1 [ (cosmos_proto.accepts_interface) = "PoolI" ];
.add_attribute("method", "try_issue_nft")
.add_submessage(SubMsg::reply_on_success(msg_issue_nft, ISSUE_NFT_REPLY_ID)))
}
```

This is needed due to osmosis supporting multiple pool types which will be added in the future.

For that matter, `osmosis-std` provides `TryFrom` trait for all possible `Any` used in all query responses in this crate.

That means the following code works:

```rust
use prost::DecodeError;
use cosmwasm_std::{Deps, StdResult, StdError};
use osmosis_std::types::osmosis::gamm::v1beta1::GammQuerier;

fn query_pool(
deps: &Deps,
pool_id: u64,
) -> StdResult<osmosis_std::types::osmosis::gamm::v1beta1::Pool> {
let res = GammQuerier::new(&deps.querier).pool(pool_id)?;
res.pool
.ok_or_else(|| StdError::not_found("pool"))?
.try_into() // convert `Any` to `osmosis_std::types::osmosis::gamm::v1beta1::Pool`
.map_err(|e: DecodeError| StdError::parse_err(
"osmosis_std::types::osmosis::gamm::v1beta1::Pool",
e
))
}
```

Or if later you want to support multiple pool type

```rust
use prost::{DecodeError, Message};
use cosmwasm_std::{Deps, StdResult, StdError};
use osmosis_std::types::osmosis::gamm::v1beta1::GammQuerier;

enum Pool {
Balancer(osmosis_std::types::osmosis::gamm::v1beta1::Pool),
StableSwap(osmosis_std::types::osmosis::gamm::poolmodels::stableswap::v1beta1::Pool),
}

impl TryFrom<osmosis_std::shim::Any> for Pool {
type Error = StdError;

fn try_from(value: osmosis_std::shim::Any) -> Result<Self, Self::Error> {
if let Ok(pool) = osmosis_std::types::osmosis::gamm::v1beta1::Pool::decode(value.value.as_slice()) {
return Ok(Pool::Balancer(pool));
}
if let Ok(pool) = osmosis_std::types::osmosis::gamm::poolmodels::stableswap::v1beta1::Pool::decode(value.value.as_slice()) {
return Ok(Pool::StableSwap(pool));
}

Err(StdError::parse_err(
"Pool",
"Unmatched pool: must be either `Balancer` or `StableSwap`."
))
}
}

fn query_pool(
deps: &Deps,
pool_id: u64,
) -> StdResult<Pool> {
let res = GammQuerier::new(&deps.querier).pool(pool_id)?;
res.pool
.ok_or_else(|| StdError::not_found("pool"))?
.try_into() // convert `Any` to `Pool`
}
```

When translate to rust, especially with CosmWasm, it can get tricky if we want to also support json (de)serialization. [It could erase type url information from serialized json as for current implementation.](https://github.com/osmosis-labs/osmosis-rust/issues/43).

## Non-CosmWasm Client

Expand Down
4 changes: 2 additions & 2 deletions packages/finschia-std/src/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,14 +381,14 @@ impl TryFrom<crate::types::cosmos::base::v1beta1::Coin> for cosmwasm_std::Coin {
}
}

/// Convert a list of `Coin` from osmosis proto generated proto `Coin` type to cosmwasm `Coin` type
/// Convert a list of `Coin` from finschia proto generated proto `Coin` type to cosmwasm `Coin` type
pub fn try_proto_to_cosmwasm_coins(
coins: impl IntoIterator<Item = crate::types::cosmos::base::v1beta1::Coin>,
) -> StdResult<Vec<cosmwasm_std::Coin>> {
coins.into_iter().map(|c| c.try_into()).collect()
}

/// Convert a list of `Coin` from cosmwasm `Coin` type to osmosis proto generated proto `Coin` type
/// Convert a list of `Coin` from cosmwasm `Coin` type to finschia proto generated proto `Coin` type
pub fn cosmwasm_to_proto_coins(
coins: impl IntoIterator<Item = cosmwasm_std::Coin>,
) -> Vec<crate::types::cosmos::base::v1beta1::Coin> {
Expand Down
2 changes: 1 addition & 1 deletion packages/proto-build/buf.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
version: v1
name: buf.build/osmosis-labs/osmosis
name: buf.build/Finschia
deps:
- buf.build/cosmos/cosmos-sdk
- buf.build/cosmos/cosmos-proto
Expand Down
8 changes: 3 additions & 5 deletions packages/proto-build/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Build Osmosis proto files. This build script clones the CosmosSDK and Osmosis version
//! specified in the COSMOS_SDK_REV and OSMOSIS_REV constant respectively and then
//! uses that to build the required proto files for further compilation.
//! This is based on the proto-compiler code in github.com/informalsystems/ibc-rs
//! Build Finschia proto files. This build script checks out the submodules and builds the profo files
//! according to the Finschia version in the env file.
use std::path::PathBuf;

Expand Down Expand Up @@ -49,7 +47,7 @@ pub fn generate(version_tags: &HashMap<String, String>) {
.get("IBC_GO_VERSION")
.expect("IBC_GO_VERSION is not set");

// cosmos/ics23 have not supported yet in Finschia. So it is fixed tag as like osmosis.
// cosmos/ics23 have not supported yet in Finschia.
let ics23_version = "rust/v0.10.0";
git::checkout_submodule(FINSCHIA_SDK_DIR, finschia_sdk_version);
git::checkout_submodule(WASMD_DIR, wasmd_version);
Expand Down
2 changes: 1 addition & 1 deletion packages/proto-build/src/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ pub fn allow_serde_vec_u8_as_base64_encoded_string(s: ItemStruct) -> ItemStruct
syn::ItemStruct { fields, ..s }
}

/// some of proto's fields in osmosis' modules are named `ID` but prost generates `id` field
/// some of proto's fields in finschia' modules are named `ID` but prost generates `id` field
/// this function adds `#[serde(alias = "ID")]` to the `id` field
/// so that serde can deserialize `ID` field to `id` field.
/// This is required because the `ID` field is used in the query response and is serialized as json.
Expand Down

0 comments on commit a554ae8

Please sign in to comment.