@@ -64,6 +64,91 @@ fn query_creator_denoms(deps: Deps, env: Env) -> StdResult<QueryDenomsFromCreato
64
64
}
65
65
```
66
66
67
+ ## Querying Pool
68
+
69
+ 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.
70
+
71
+ https://github.com/osmosis-labs/osmosis/blob/f024498f1e8e0d2a1fe259cd9cc4223803fea0cd/proto/osmosis/gamm/v1beta1/query.proto#L82-L84
72
+
73
+ ``` proto
74
+ message QueryPoolResponse {
75
+ google.protobuf.Any pool = 1 [ (cosmos_proto.accepts_interface) = "PoolI" ];
76
+ }
77
+ ```
78
+
79
+ This is needed due to osmosis supporting multiple pool types which will be added in the future.
80
+
81
+ For that matter, ` osmosis-std ` provides ` TryFrom ` trait for all possible ` Any ` used in all query responses in this crate.
82
+
83
+ That means the following code works:
84
+
85
+ ``` rust
86
+ use prost :: DecodeError ;
87
+ use cosmwasm_std :: {Deps , StdResult , StdError };
88
+ use osmosis_std :: types :: osmosis :: gamm :: v1beta1 :: GammQuerier ;
89
+
90
+ fn query_pool (
91
+ deps : & Deps ,
92
+ pool_id : u64 ,
93
+ ) -> StdResult <osmosis_std :: types :: osmosis :: gamm :: v1beta1 :: Pool > {
94
+ let res = GammQuerier :: new (& deps . querier). pool (pool_id )? ;
95
+ res . pool
96
+ . ok_or_else (|| StdError :: NotFound {
97
+ kind : " pool" . to_string (),
98
+ })?
99
+ . try_into () // convert `Any` to `osmosis_std::types::osmosis::gamm::v1beta1::Pool`
100
+ . map_err (| e : DecodeError | StdError :: ParseErr {
101
+ target_type : " osmosis_std::types::osmosis::gamm::v1beta1::Pool" . to_string (),
102
+ msg : e . to_string (),
103
+ })
104
+ }
105
+ ```
106
+
107
+ Or if later you want to support multiple pool type
108
+
109
+ ``` rust
110
+ use prost :: {DecodeError , Message };
111
+ use cosmwasm_std :: {Deps , StdResult , StdError };
112
+ use osmosis_std :: types :: osmosis :: gamm :: v1beta1 :: GammQuerier ;
113
+
114
+ enum Pool {
115
+ Balancer (osmosis_std :: types :: osmosis :: gamm :: v1beta1 :: Pool ),
116
+ StableSwap (osmosis_std :: types :: osmosis :: gamm :: poolmodels :: stableswap :: v1beta1 :: Pool ),
117
+ }
118
+
119
+ impl TryFrom <osmosis_std :: shim :: Any > for Pool {
120
+ type Error = StdError ;
121
+
122
+ fn try_from (value : osmosis_std :: shim :: Any ) -> Result <Self , Self :: Error > {
123
+ if let Ok (pool ) = osmosis_std :: types :: osmosis :: gamm :: v1beta1 :: Pool :: decode (value . value. as_slice ()) {
124
+ return Ok (Pool :: Balancer (pool ));
125
+ }
126
+ if let Ok (pool ) = osmosis_std :: types :: osmosis :: gamm :: poolmodels :: stableswap :: v1beta1 :: Pool :: decode (value . value. as_slice ()) {
127
+ return Ok (Pool :: StableSwap (pool ));
128
+ }
129
+
130
+ Err (StdError :: ParseErr {
131
+ target_type : " Pool" . to_string (),
132
+ msg : " Unmatched pool: must be either `Balancer` or `StableSwap`." . to_string (),
133
+ })
134
+ }
135
+ }
136
+
137
+ fn query_pool (
138
+ deps : & Deps ,
139
+ pool_id : u64 ,
140
+ ) -> StdResult <Pool > {
141
+ let res = GammQuerier :: new (& deps . querier). pool (pool_id )? ;
142
+ res . pool
143
+ . ok_or_else (|| StdError :: NotFound {
144
+ kind : " pool" . to_string (),
145
+ })?
146
+ . try_into () // convert `Any` to `Pool`
147
+ }
148
+ ```
149
+
150
+ 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 ) .
151
+
67
152
## Non-CosmWasm Client
68
153
69
154
(WIP)
0 commit comments