osmosis-labs / osmojs

OsmosJS makes it easy to compose and broadcast Osmosis and Cosmos messages
https://cosmology.zone/products/osmojs
Apache License 2.0
63 stars 32 forks source link

decoding RPC for Pools Request doesn't decode Pool automatically #5

Closed pyramation closed 1 year ago

pyramation commented 2 years ago

not sure if this is intended, but decoding RPC for Pools Request doesn't decode Pool automatically.

import { osmosis } from "osmojs";

import { createProtobufRpcClient, QueryClient } from "@cosmjs/stargate";
import { Tendermint34Client } from "@cosmjs/tendermint-rpc";

export const main = async () => {

    const tmClient = await Tendermint34Client.connect(RPC_ENDPOINT);
    const QueryClientImpl = osmosis.gamm.v1beta1.QueryClientImpl;
    const client = new QueryClient(tmClient);
    const rpc = createProtobufRpcClient(client);
    const queryService = new QueryClientImpl(rpc);
    const res = await queryService.pools({})

   // THIS WORKS but has to be manually applied
    res.pools.map(({ typeUrl, value }) => {
        console.log(osmosis.gamm.v1beta1.Pool.decode(value));
    })
};

main().then(() => {
    console.log('all done')
})
pyramation commented 2 years ago

editing the code this also works by editing the QueryPoolsResponse.decode method

First import Pool object deps

var _balancer = require("../pool-models/balancer/balancerPool");

update the decode method

  decode: function decode(input, length) {
    var reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
    var end = length === undefined ? reader.len : reader.pos + length;
    var message = createBaseQueryPoolsResponse();

    while (reader.pos < end) {
      var tag = reader.uint32();

      switch (tag >>> 3) {
        case 1:
          // message.pools.push(_any.Any.decode(reader, reader.uint32()));
          message.pools.push(_balancer.Pool.decode(reader, reader.uint32()))
          break;

        case 2:
          message.pagination = _pagination.PageResponse.decode(reader, reader.uint32());
          break;

        default:
          reader.skipType(tag & 7);
          break;
      }
    }

    return message;
  },
Steven-Chang commented 1 year ago

Looks like decoding works automatically now. This is what the response looks like.

{ '$typeUrl': '/osmosis.gamm.v1beta1.Pool', address: 'osmo1zg4rlhp843nn3jk0uz2kvk4zzvp7wm6qpvlnuv7dnp4jlz9pmpws90duk8', id: Long { low: 82, high: 0, unsigned: true }, poolParams: { swapFee: '2000000000000000', exitFee: '0', smoothWeightChangeParams: undefined }, futurePoolGovernor: '24h', totalShares: { denom: 'gamm/pool/82', amount: '100000000000000000' }, poolAssets: [ [Object], [Object] ], totalWeight: '1073741824000000' },

Any chance you can update https://github.com/cosmology-tech/cosmology/tree/main/packages/core @pyramation ?