telescope icon indicating copy to clipboard operation
telescope copied to clipboard

use implicit recursion similar to fromPartial for toAmino/fromAmino

Open pyramation opened this issue 3 years ago • 3 comments

currently the toAmino/fromAmino functionality requires the proto parser store and proto refs so that it can query information about nested dependencies and imports.

This could be simplified by taking an approach for amino encoding similar to how fromPartial, fromJSON, toJSON and other methods inspired from ts-proto

This could be accomplished by adding toAminoandfromAmino` methods to the message objects:

export const MsgCreateValidator = {
  encode(message: MsgCreateValidator, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
    if (message.description !== undefined) {
      Description.encode(message.description, writer.uint32(10).fork()).ldelim();
    }
    if (message.commission !== undefined) {
      CommissionRates.encode(message.commission, writer.uint32(18).fork()).ldelim();
    }
  }
  toAmino(message, obj) {
      if (message.description !== undefined) {
         obj.description = Description.toAmino(message.description);
     }
  }

pyramation avatar May 04 '22 16:05 pyramation

this may be required for infinitely recursive structures.

Part of why this PR didn't get merged, it seemed that some structures were infinitely nested: https://github.com/osmosis-labs/telescope/pull/198

pyramation avatar Sep 12 '22 02:09 pyramation

types themselves

  • [x] CoinSDKType
  • [x] CoinAminoType is like SDKType but with these https://github.com/osmosis-labs/telescope/blob/main/PROTO-AMINO.md

encoders

  • [ ] string
  • [x] numbers
  • [x] long
  • [ ] enum (using functions)

duration types

  • [ ] https://github.com/osmosis-labs/osmojs/issues/12
  • [ ] https://github.com/bitsongofficial/sinfonia-ui/blob/9aaa13947b971d220ae64600704645faf2133efb/src/signing/amino-types.ts#L89
  • [ ] instead of coding the subfields inline, why not delegate to Duration.toAmino() and Duration.fromAmino() - can we do message type level encoding?

pyramation avatar Nov 26 '22 04:11 pyramation

enum example

  "/cosmos.gov.v1beta1.MsgVote": {
    aminoType: "cosmos-sdk/MsgVote",
    toAmino: ({ option, proposalId, voter }: MsgVote): AminoMsgVote["value"] => {
      return {
        option: option,
        proposal_id: proposalId.toString(),
        voter: voter,
      };
    },
    fromAmino: ({ option, proposal_id, voter }: AminoMsgVote["value"]): MsgVote => {
      return {
        option: voteOptionFromJSON(option),
        proposalId: Long.fromString(proposal_id),
        voter: voter,
      };
    },
  },

pyramation avatar Nov 29 '22 05:11 pyramation