Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dawid-kruk opened this issue May 27, 2024 · 3 comments
Closed

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

dawid-kruk opened this issue May 27, 2024 · 3 comments

Comments

@dawid-kruk
Copy link

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
Copy link

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

@dawid-kruk
Copy link
Author

@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
Copy link

Thanks @dawid-kruk I fell I'm having a similar issue #1596 but can't get my head around it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants