From dd399693c8a54abb5d94b244a7b41fa4dffdc362 Mon Sep 17 00:00:00 2001 From: joon9823 Date: Sat, 18 May 2024 00:54:16 +0900 Subject: [PATCH] Add MsgCancelUnbondingDelegation --- package-lock.json | 4 +- package.json | 2 +- src/core/Msg.ts | 7 + .../msgs/MsgCancelUnbondingDelegation.ts | 139 ++++++++++++++++++ src/core/mstaking/msgs/index.ts | 6 + 5 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 src/core/mstaking/msgs/MsgCancelUnbondingDelegation.ts diff --git a/package-lock.json b/package-lock.json index 602a3ed..49f5b40 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@initia/initia.js", - "version": "0.2.0", + "version": "0.2.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@initia/initia.js", - "version": "0.2.0", + "version": "0.2.1", "license": "MIT", "dependencies": { "@initia/initia.proto": "^0.2.0", diff --git a/package.json b/package.json index 40d1d95..5bd13bc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@initia/initia.js", - "version": "0.2.0", + "version": "0.2.1", "description": "The JavaScript SDK for Initia", "license": "MIT", "author": "InitiaLabs", diff --git a/src/core/Msg.ts b/src/core/Msg.ts index 7ab2404..c7f40fa 100644 --- a/src/core/Msg.ts +++ b/src/core/Msg.ts @@ -145,6 +145,7 @@ import { MsgDelegate, MsgEditValidator, MsgUndelegate, + MsgCancelUnbondingDelegation, MsgUpdateMstakingParams, } from './mstaking'; import { @@ -559,6 +560,8 @@ export namespace Msg { return MsgCreateValidator.fromAmino(data); case 'mstaking/MsgEditValidator': return MsgEditValidator.fromAmino(data); + case 'mstaking/MsgCancelUnbondingDelegation': + return MsgCancelUnbondingDelegation.fromAmino(data); case 'mstaking/MsgUpdateParams': return MsgUpdateMstakingParams.fromAmino(data); @@ -957,6 +960,8 @@ export namespace Msg { return MsgCreateValidator.fromData(data); case '/initia.mstaking.v1.MsgEditValidator': return MsgEditValidator.fromData(data); + case '/initia.mstaking.v1.MsgCancelUnbondingDelegation': + return MsgCancelUnbondingDelegation.fromData(data); case '/initia.mstaking.v1.MsgUpdateParams': return MsgUpdateMstakingParams.fromData(data); @@ -1358,6 +1363,8 @@ export namespace Msg { return MsgCreateValidator.unpackAny(proto); case '/initia.mstaking.v1.MsgEditValidator': return MsgEditValidator.unpackAny(proto); + case '/initia.mstaking.v1.MsgCancelUnbondingDelegation': + return MsgCancelUnbondingDelegation.unpackAny(proto); case '/initia.mstaking.v1.MsgUpdateParams': return MsgUpdateMstakingParams.unpackAny(proto); diff --git a/src/core/mstaking/msgs/MsgCancelUnbondingDelegation.ts b/src/core/mstaking/msgs/MsgCancelUnbondingDelegation.ts new file mode 100644 index 0000000..81ec2a7 --- /dev/null +++ b/src/core/mstaking/msgs/MsgCancelUnbondingDelegation.ts @@ -0,0 +1,139 @@ +import { Coins } from '../../Coins'; +import { JSONSerializable } from '../../../util/json'; +import { AccAddress, ValAddress } from '../../bech32'; +import { Any } from '@initia/initia.proto/google/protobuf/any'; +import { MsgCancelUnbondingDelegation as MsgCancelUnbondingDelegation_pb } from '@initia/initia.proto/initia/mstaking/v1/tx'; +import Long from 'long'; + +export class MsgCancelUnbondingDelegation extends JSONSerializable< + MsgCancelUnbondingDelegation.Amino, + MsgCancelUnbondingDelegation.Data, + MsgCancelUnbondingDelegation.Proto +> { + public amount: Coins; + + /** + * @param delegator_address delegator's account address + * @param validator_address validator's operator address + * @param amount INIT to be undelegated + */ + constructor( + public delegator_address: AccAddress, + public validator_address: ValAddress, + amount: Coins.Input, + public creation_height: number + ) { + super(); + this.amount = new Coins(amount); + } + + public static fromAmino( + data: MsgCancelUnbondingDelegation.Amino + ): MsgCancelUnbondingDelegation { + const { + value: { delegator_address, validator_address, amount, creation_height }, + } = data; + return new MsgCancelUnbondingDelegation( + delegator_address, + validator_address, + Coins.fromAmino(amount), + Number.parseInt(creation_height) + ); + } + + public toAmino(): MsgCancelUnbondingDelegation.Amino { + const { delegator_address, validator_address, amount, creation_height } = + this; + return { + type: 'mstaking/MsgCancelUnbondingDelegation', + value: { + delegator_address, + validator_address, + amount: amount.toAmino(), + creation_height: creation_height.toString(), + }, + }; + } + + public static fromData( + data: MsgCancelUnbondingDelegation.Data + ): MsgCancelUnbondingDelegation { + const { delegator_address, validator_address, amount, creation_height } = + data; + return new MsgCancelUnbondingDelegation( + delegator_address, + validator_address, + Coins.fromData(amount), + Number.parseInt(creation_height) + ); + } + + public toData(): MsgCancelUnbondingDelegation.Data { + const { delegator_address, validator_address, amount, creation_height } = + this; + return { + '@type': '/initia.mstaking.v1.MsgCancelUnbondingDelegation', + delegator_address, + validator_address, + amount: amount.toData(), + creation_height: creation_height.toString(), + }; + } + + public static fromProto( + proto: MsgCancelUnbondingDelegation.Proto + ): MsgCancelUnbondingDelegation { + return new MsgCancelUnbondingDelegation( + proto.delegatorAddress, + proto.validatorAddress, + Coins.fromProto(proto.amount as Coins.Proto), + proto.creationHeight.toNumber() + ); + } + + public toProto(): MsgCancelUnbondingDelegation.Proto { + const { delegator_address, validator_address, amount, creation_height } = + this; + return MsgCancelUnbondingDelegation_pb.fromPartial({ + amount: amount.toProto(), + delegatorAddress: delegator_address, + validatorAddress: validator_address, + creationHeight: Long.fromNumber(creation_height), + }); + } + + public packAny(): Any { + return Any.fromPartial({ + typeUrl: '/initia.mstaking.v1.MsgCancelUnbondingDelegation', + value: MsgCancelUnbondingDelegation_pb.encode(this.toProto()).finish(), + }); + } + + public static unpackAny(msgAny: Any): MsgCancelUnbondingDelegation { + return MsgCancelUnbondingDelegation.fromProto( + MsgCancelUnbondingDelegation_pb.decode(msgAny.value) + ); + } +} + +export namespace MsgCancelUnbondingDelegation { + export interface Amino { + type: 'mstaking/MsgCancelUnbondingDelegation'; + value: { + delegator_address: AccAddress; + validator_address: ValAddress; + amount: Coins.Amino; + creation_height: string; + }; + } + + export interface Data { + '@type': '/initia.mstaking.v1.MsgCancelUnbondingDelegation'; + delegator_address: AccAddress; + validator_address: ValAddress; + amount: Coins.Data; + creation_height: string; + } + + export type Proto = MsgCancelUnbondingDelegation_pb; +} diff --git a/src/core/mstaking/msgs/index.ts b/src/core/mstaking/msgs/index.ts index 29f37fe..1f11c2a 100644 --- a/src/core/mstaking/msgs/index.ts +++ b/src/core/mstaking/msgs/index.ts @@ -3,6 +3,7 @@ import { MsgUndelegate } from './MsgUndelegate'; import { MsgBeginRedelegate } from './MsgBeginRedelegate'; import { MsgCreateValidator } from './MsgCreateValidator'; import { MsgEditValidator } from './MsgEditValidator'; +import { MsgCancelUnbondingDelegation } from './MsgCancelUnbondingDelegation'; import { MsgUpdateMstakingParams } from './MsgUpdateMstakingParams'; export * from './MsgDelegate'; @@ -10,6 +11,7 @@ export * from './MsgUndelegate'; export * from './MsgBeginRedelegate'; export * from './MsgCreateValidator'; export * from './MsgEditValidator'; +export * from './MsgCancelUnbondingDelegation'; export * from './MsgUpdateMstakingParams'; export type MstakingMsg = @@ -18,6 +20,7 @@ export type MstakingMsg = | MsgBeginRedelegate | MsgCreateValidator | MsgEditValidator + | MsgCancelUnbondingDelegation | MsgUpdateMstakingParams; export namespace MstakingMsg { @@ -27,6 +30,7 @@ export namespace MstakingMsg { | MsgBeginRedelegate.Amino | MsgCreateValidator.Amino | MsgEditValidator.Amino + | MsgCancelUnbondingDelegation.Amino | MsgUpdateMstakingParams.Amino; export type Data = | MsgDelegate.Data @@ -34,6 +38,7 @@ export namespace MstakingMsg { | MsgBeginRedelegate.Data | MsgCreateValidator.Data | MsgEditValidator.Data + | MsgCancelUnbondingDelegation.Data | MsgUpdateMstakingParams.Data; export type Proto = | MsgDelegate.Proto @@ -41,5 +46,6 @@ export namespace MstakingMsg { | MsgBeginRedelegate.Proto | MsgCreateValidator.Proto | MsgEditValidator.Proto + | MsgCancelUnbondingDelegation.Proto | MsgUpdateMstakingParams.Proto; }