From 5032acf4cec0f6b1d5946d75810cf7dcc0613179 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Tue, 23 Feb 2021 20:39:45 +0100 Subject: [PATCH] stargate: Add SigningStargateClient.undelegateTokens/withdrawRewards --- .../src/signingstargateclient.spec.ts | 73 ++++++++++++++++++- .../stargate/src/signingstargateclient.ts | 34 ++++++++- 2 files changed, 105 insertions(+), 2 deletions(-) diff --git a/packages/stargate/src/signingstargateclient.spec.ts b/packages/stargate/src/signingstargateclient.spec.ts index c6f0d3b4..7caaa979 100644 --- a/packages/stargate/src/signingstargateclient.spec.ts +++ b/packages/stargate/src/signingstargateclient.spec.ts @@ -49,6 +49,24 @@ describe("SigningStargateClient", () => { ], gas: "160000", }, + undelegate: { + amount: [ + { + amount: "4000", + denom: "ucosm", + }, + ], + gas: "160000", + }, + withdraw: { + amount: [ + { + amount: "4000", + denom: "ucosm", + }, + ], + gas: "160000", + }, }); }); @@ -89,6 +107,24 @@ describe("SigningStargateClient", () => { ], gas: "160000", }, + undelegate: { + amount: [ + { + amount: "502400", + denom: "utest", + }, + ], + gas: "160000", + }, + withdraw: { + amount: [ + { + amount: "502400", + denom: "utest", + }, + ], + gas: "160000", + }, }); }); @@ -121,6 +157,24 @@ describe("SigningStargateClient", () => { ], gas: "120000", }, + undelegate: { + amount: [ + { + amount: "4000", + denom: "ucosm", + }, + ], + gas: "160000", + }, + withdraw: { + amount: [ + { + amount: "4000", + denom: "ucosm", + }, + ], + gas: "160000", + }, }); }); @@ -130,7 +184,6 @@ describe("SigningStargateClient", () => { const gasPrice = GasPrice.fromString("3.14utest"); const gasLimits = { send: 160000, - delegate: 160000, }; const options = { gasPrice: gasPrice, gasLimits: gasLimits }; const client = await SigningStargateClient.connectWithSigner(simapp.tendermintUrl, wallet, options); @@ -154,6 +207,24 @@ describe("SigningStargateClient", () => { ], gas: "160000", }, + undelegate: { + amount: [ + { + amount: "502400", + denom: "utest", + }, + ], + gas: "160000", + }, + withdraw: { + amount: [ + { + amount: "502400", + denom: "utest", + }, + ], + gas: "160000", + }, }); }); }); diff --git a/packages/stargate/src/signingstargateclient.ts b/packages/stargate/src/signingstargateclient.ts index 13c894d6..03379dd9 100644 --- a/packages/stargate/src/signingstargateclient.ts +++ b/packages/stargate/src/signingstargateclient.ts @@ -67,10 +67,17 @@ import { BroadcastTxResponse, StargateClient } from "./stargateclient"; export interface CosmosFeeTable extends FeeTable { readonly send: StdFee; readonly delegate: StdFee; + readonly undelegate: StdFee; + readonly withdraw: StdFee; } const defaultGasPrice = GasPrice.fromString("0.025ucosm"); -const defaultGasLimits: GasLimits = { send: 80000, delegate: 160000 }; +const defaultGasLimits: GasLimits = { + send: 80000, + delegate: 160000, + undelegate: 160000, + withdraw: 160000, +}; export const defaultRegistryTypes: ReadonlyArray<[string, GeneratedType]> = [ ["/cosmos.bank.v1beta1.MsgMultiSend", MsgMultiSend], @@ -212,6 +219,31 @@ export class SigningStargateClient extends StargateClient { return this.signAndBroadcast(senderAddress, [delegateMsg], this.fees.delegate, memo); } + public async undelegateTokens( + senderAddress: string, + validatorAddress: string, + amount: Coin, + memo = "", + ): Promise { + const undelegateMsg = { + typeUrl: "/cosmos.staking.v1beta1.MsgUndelegate", + value: MsgUndelegate.fromPartial({ delegatorAddress: senderAddress, validatorAddress, amount }), + }; + return this.signAndBroadcast(senderAddress, [undelegateMsg], this.fees.undelegate, memo); + } + + public async withdrawRewards( + senderAddress: string, + validatorAddress: string, + memo = "", + ): Promise { + const withdrawMsg = { + typeUrl: "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward", + value: MsgWithdrawDelegatorReward.fromPartial({ delegatorAddress: senderAddress, validatorAddress }), + }; + return this.signAndBroadcast(senderAddress, [withdrawMsg], this.fees.withdraw, memo); + } + public async signAndBroadcast( signerAddress: string, messages: readonly EncodeObject[],