osmosis-labs / osmosis-rust

Rust libraries for osmosis
Apache License 2.0
59 stars 52 forks source link

fix json deserialization into stableswap pool type #85

Closed sampocs closed 11 months ago

sampocs commented 1 year ago

Context

The stableswap pool type has a scaling factor attribute of type Vec<u64>; however, the json schema defines the same field as an array of Strings. As a result, when querying this pool, the deserialization fails. This PR adds a custom serializer/deserializer to safely convert the Vec<String> type from the query response into the preferred Vec<u64> type.

Brief Changelog

Code to reproduce the error

use osmosis_std::types::osmosis::gamm::poolmodels::stableswap::v1beta1::Pool as StableswapPool;
use osmosis_std::types::osmosis::poolmanager::v1beta1::PoolmanagerQuerier;

let query_pool_resp = PoolmanagerQuerier::new(&deps.querier).pool(pool_id)?;
let stableswap_pool: StableswapPool = query_pool_resp
    .pool
    .ok_or(ContractError::PoolNotFoundOsmosis { pool_id })?
    .try_into()
    .map_err(|e| {
        StdError::parse_err(
            "osmosis_std::types::osmosis::gamm::poolmodels::stableswap::v1beta1::Pool",
            e,
        )
    })?;

Error message before fix

Error: rpc error: code = InvalidArgument desc = failed to execute message; message index: 0: Error parsing into type osmosis_std::types::osmosis::poolmanager::v1beta1::PoolResponse: Invalid type string "100000". Expected u64: execute wasm contract failed: invalid request
iboss-ptk commented 11 months ago

Hi @sampocs! would you mind making this automated? With the current change, it will be replaced when type gets regenerated.

This is similar to https://github.com/osmosis-labs/osmosis-rust/blob/5489d6f29cf739bbc2635622aa073bde4a479732/packages/proto-build/src/transformers.rs#L150

So we can add another transformer function next to allow_serde_int_as_str and register it here: https://github.com/osmosis-labs/osmosis-rust/blob/5489d6f29cf739bbc2635622aa073bde4a479732/packages/proto-build/src/transform.rs#L164

sampocs commented 11 months ago

Sorry for the delay @iboss-ptk! Please see the above commit and let me know if this addresses it for you.

Also, how can I test this?

iboss-ptk commented 11 months ago

@sampocs sorry for late response, you can unit test the transformer logic similar to these tests (basically test if the transformed tokens match expectation).

To see if actual code generation works as expected, you can cd packages/proto-build && cargo run (note that cargo run -p proto-build does not work for some reason I haven't investigated).

iboss-ptk commented 11 months ago

The changes and the fix is already in main https://github.com/osmosis-labs/osmosis-rust/commit/473d53917ec594872674b0d8b99f992a7d6b806a