cosmos / cosmjs

The Swiss Army knife to power JavaScript based client solutions ranging from Web apps/explorers over browser extensions to server-side clients like faucets/scrapers.
https://cosmos.github.io/cosmjs/
Apache License 2.0
636 stars 324 forks source link

How to create correct amino-converter for Duration type #1589

Closed dawid-kruk closed 2 weeks ago

dawid-kruk commented 1 month ago

Hello, I have a big problem with amino converter for Duration type. Here is my proto definition of a message:

message MsgCreateVestingPool {
  string owner = 1;
  string name = 3;
  string amount = 4 [
    (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
    (gogoproto.nullable) = false
  ];
  google.protobuf.Duration duration = 5 [
    (gogoproto.nullable) = false,
    (gogoproto.stdduration) = true
  ];
  string vesting_type = 6;
}

Here is my typescript type for protobuf signing:

export interface MsgCreateVestingPool {
  owner: string;
  name: string;
  amount: string;
  duration: Duration | undefined;
  vestingType: string;
}

Duration is of this type:

export interface Duration {
  seconds: number;
  nanos: number;
}

And here is my AminoMsgCreateVestingPool

 export interface AminoMsgCreateVestingPool extends AminoMsg {
    readonly type: "cfevesting/CreateVestingPool";
    readonly value: {
        readonly amount: string,
        readonly duration: string,
        readonly name: string,
        readonly owner: string,
        readonly vesting_type: string,
    };
}

Here i have my amino converter:

 "/chain4energy.c4echain.cfevesting.MsgCreateVestingPool": {
      aminoType: "cfevesting/CreateVestingPool",
      toAmino: ({
                    amount,
                    duration,
                    vestingType,
                    name,
                    owner,
                }: MsgCreateVestingPool): AminoMsgCreateVestingPool["value"] => ({
          amount,
          duration: "100s",
          name: name,
          vesting_type: vestingType,
          owner,
      }),
      fromAmino: ({
                      amount,
                      duration,
                      name,
                      owner,
                      vesting_type,
                  }: AminoMsgCreateVestingPool["value"]): MsgCreateVestingPool => ({
          amount,
          duration: {seconds: 100, nanos: 0},
          name,
          owner,
          vestingType: vesting_type
      }),
  },

Here is error from the chain:

Error: Broadcasting transaction failed with code 4 (codespace: sdk). Log: signature verification failed; please verify account number (108756), sequence (16) and chain-id (veles-dev-test-1): unauthorized
    at SigningStargateClient.broadcastTx (@cosmjs_stargate.js?v=f839367f:40795:33)
    at async Proxy.createVestingPool (SplitVesting.vue:128:13)

I know that i'm hardcoding duration conversion for now but i don't know how to do it correctly. I was trying passing objects, numbers and other formats to AminoMsgCreateVestingPool type. I don't know what I can do more. Thank's in advance!

ppoliani commented 2 weeks ago

@dawid-kruk Did you find a solution to this?

dawid-kruk commented 2 weeks ago

@ppoliani Yes I did :) Below is code example:

 export interface MsgExample {
  duration: Duration | undefined;
}

 export interface AminoMsgExample extends AminoMsg {
  readonly type: "correct/Type";
  readonly value: {
    readonly duration: string,
  };
}

 "/abcd.abcd.abcd.MsgExample": {
      aminoType: "correct/Type",
  toAmino: ({
             duration,
           }: MsgExample): AminoMsgExample["value"] => ({
        duration: duration?(duration.seconds * 1000000000 + duration.nanos).toString():"0",
      }),
      fromAmino: ({
                    duration,
                  }: AminoMsgExample["value"]): MsgExample => {
        const durationInNanoseconds = parseInt(duration, 10);
        const seconds = Math.floor(durationInNanoseconds / 1_000_000_000);
        const nanos = durationInNanoseconds % 1_000_000_000;
        return {
          duration: {
            seconds,
            nanos,
          },
        }
      },
      }
ppoliani commented 2 weeks ago

Thanks @dawid-kruk I fell I'm having a similar issue https://github.com/cosmos/cosmjs/issues/1596 but can't get my head around it.