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

feat: add staking msg cancel unbond #1535

Merged
2 changes: 2 additions & 0 deletions packages/stargate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export {
isAminoMsgWithdrawDelegatorReward,
isAminoMsgWithdrawValidatorCommission,
isMsgBeginRedelegateEncodeObject,
isMsgCancelUnbondingDelegationEncodeObject,
isMsgCreateValidatorEncodeObject,
isMsgDelegateEncodeObject,
isMsgDepositEncodeObject,
Expand All @@ -78,6 +79,7 @@ export {
MintExtension,
MintParams,
MsgBeginRedelegateEncodeObject,
MsgCancelUnbondingDelegationEncodeObject,
MsgCreateValidatorEncodeObject,
MsgDelegateEncodeObject,
MsgDepositEncodeObject,
Expand Down
2 changes: 2 additions & 0 deletions packages/stargate/src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,13 @@ export {
} from "./staking/aminomessages";
export {
isMsgBeginRedelegateEncodeObject,
isMsgCancelUnbondingDelegationEncodeObject,
isMsgCreateValidatorEncodeObject,
isMsgDelegateEncodeObject,
isMsgEditValidatorEncodeObject,
isMsgUndelegateEncodeObject,
MsgBeginRedelegateEncodeObject,
MsgCancelUnbondingDelegationEncodeObject,
MsgCreateValidatorEncodeObject,
MsgDelegateEncodeObject,
MsgEditValidatorEncodeObject,
Expand Down
49 changes: 49 additions & 0 deletions packages/stargate/src/modules/staking/aminomessages.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { coin } from "@cosmjs/proto-signing";
import { PubKey as CosmosCryptoSecp256k1Pubkey } from "cosmjs-types/cosmos/crypto/secp256k1/keys";
import {
MsgBeginRedelegate,
MsgCancelUnbondingDelegation,
MsgCreateValidator,
MsgDelegate,
MsgEditValidator,
Expand All @@ -13,6 +14,7 @@ import {
import { AminoTypes } from "../../aminotypes";
import {
AminoMsgBeginRedelegate,
AminoMsgCancelUnbondingDelegation,
AminoMsgCreateValidator,
AminoMsgDelegate,
AminoMsgEditValidator,
Expand Down Expand Up @@ -199,6 +201,30 @@ describe("AminoTypes", () => {
};
expect(aminoMsg).toEqual(expected);
});

it("works for MsgCancelUnbondingDelegation", () => {
const msg: MsgCancelUnbondingDelegation = {
delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5",
amount: coin(1234, "ucosm"),
creationHeight: BigInt("1"),
};
const aminoTypes = new AminoTypes(createStakingAminoConverters());
const aminoMsg = aminoTypes.toAmino({
typeUrl: "/cosmos.staking.v1beta1.MsgCancelUnbondingDelegation",
value: msg,
});
const expected: AminoMsgCancelUnbondingDelegation = {
type: "cosmos-sdk/MsgCancelUnbondingDelegation",
value: {
delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5",
amount: coin(1234, "ucosm"),
creation_height: "1",
},
};
expect(aminoMsg).toEqual(expected);
});
});

describe("fromAmino", () => {
Expand Down Expand Up @@ -362,5 +388,28 @@ describe("AminoTypes", () => {
value: expectedValue,
});
});

it("works for MsgCancelUnbondingDelegation", () => {
const aminoMsg: AminoMsgCancelUnbondingDelegation = {
type: "cosmos-sdk/MsgCancelUnbondingDelegation",
value: {
delegator_address: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
validator_address: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5",
amount: coin(1234, "ucosm"),
creation_height: "1",
},
};
const msg = new AminoTypes(createStakingAminoConverters()).fromAmino(aminoMsg);
const expectedValue: MsgCancelUnbondingDelegation = {
delegatorAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
validatorAddress: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5",
amount: coin(1234, "ucosm"),
creationHeight: BigInt("1"),
};
expect(msg).toEqual({
typeUrl: "/cosmos.staking.v1beta1.MsgCancelUnbondingDelegation",
value: expectedValue,
});
});
});
});
43 changes: 43 additions & 0 deletions packages/stargate/src/modules/staking/aminomessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { decodePubkey, encodePubkey } from "@cosmjs/proto-signing";
import { assertDefinedAndNotNull } from "@cosmjs/utils";
import {
MsgBeginRedelegate,
MsgCancelUnbondingDelegation,
MsgCreateValidator,
MsgDelegate,
MsgEditValidator,
Expand Down Expand Up @@ -143,6 +144,20 @@ export function isAminoMsgUndelegate(msg: AminoMsg): msg is AminoMsgUndelegate {
return msg.type === "cosmos-sdk/MsgUndelegate";
}

export interface AminoMsgCancelUnbondingDelegation extends AminoMsg {
readonly type: "cosmos-sdk/MsgCancelUnbondingDelegation";
readonly value: {
readonly delegator_address: string;
readonly validator_address: string;
readonly amount: Coin;
readonly creation_height: string;
};
}

export function isAminoMsgCancelUnbondingDelegation(msg: AminoMsg): msg is AminoMsgCancelUnbondingDelegation {
return msg.type === "cosmos-sdk/MsgCancelUnbondingDelegation";
}

export function createStakingAminoConverters(): Record<string, AminoConverter> {
return {
"/cosmos.staking.v1beta1.MsgBeginRedelegate": {
Expand Down Expand Up @@ -326,5 +341,33 @@ export function createStakingAminoConverters(): Record<string, AminoConverter> {
amount: amount,
}),
},
"/cosmos.staking.v1beta1.MsgCancelUnbondingDelegation": {
aminoType: "cosmos-sdk/MsgCancelUnbondingDelegation",
toAmino: ({
delegatorAddress,
validatorAddress,
amount,
creationHeight,
}: MsgCancelUnbondingDelegation): AminoMsgCancelUnbondingDelegation["value"] => {
assertDefinedAndNotNull(amount, "missing amount");
return {
delegator_address: delegatorAddress,
validator_address: validatorAddress,
amount: amount,
creation_height: creationHeight.toString(),
};
},
fromAmino: ({
delegator_address,
validator_address,
amount,
creation_height,
}: AminoMsgCancelUnbondingDelegation["value"]): MsgCancelUnbondingDelegation => ({
delegatorAddress: delegator_address,
validatorAddress: validator_address,
amount: amount,
creationHeight: BigInt(creation_height),
}),
},
};
}
16 changes: 16 additions & 0 deletions packages/stargate/src/modules/staking/messages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EncodeObject, GeneratedType } from "@cosmjs/proto-signing";
import {
MsgBeginRedelegate,
MsgCancelUnbondingDelegation,
MsgCreateValidator,
MsgDelegate,
MsgEditValidator,
Expand All @@ -13,6 +14,7 @@ export const stakingTypes: ReadonlyArray<[string, GeneratedType]> = [
["/cosmos.staking.v1beta1.MsgDelegate", MsgDelegate],
["/cosmos.staking.v1beta1.MsgEditValidator", MsgEditValidator],
["/cosmos.staking.v1beta1.MsgUndelegate", MsgUndelegate],
["/cosmos.staking.v1beta1.MsgCancelUnbondingDelegation", MsgCancelUnbondingDelegation],
];

export interface MsgBeginRedelegateEncodeObject extends EncodeObject {
Expand Down Expand Up @@ -59,3 +61,17 @@ export interface MsgUndelegateEncodeObject extends EncodeObject {
export function isMsgUndelegateEncodeObject(object: EncodeObject): object is MsgUndelegateEncodeObject {
return (object as MsgUndelegateEncodeObject).typeUrl === "/cosmos.staking.v1beta1.MsgUndelegate";
}

export interface MsgCancelUnbondingDelegationEncodeObject extends EncodeObject {
readonly typeUrl: "/cosmos.staking.v1beta1.MsgCancelUnbondingDelegation";
readonly value: Partial<MsgCancelUnbondingDelegation>;
}

export function isMsgCancelUnbondingDelegationEncodeObject(
object: EncodeObject,
): object is MsgCancelUnbondingDelegationEncodeObject {
return (
(object as MsgCancelUnbondingDelegationEncodeObject).typeUrl ===
"/cosmos.staking.v1beta1.MsgCancelUnbondingDelegation"
);
}