stargate: Add SigningStargateClient.undelegateTokens/withdrawRewards

This commit is contained in:
Ethan Frey 2021-02-23 20:39:45 +01:00 committed by willclarktech
parent c0ccf9be0c
commit 5032acf4ce
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
2 changed files with 105 additions and 2 deletions

View File

@ -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",
},
});
});
});

View File

@ -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<CosmosFeeTable> = { send: 80000, delegate: 160000 };
const defaultGasLimits: GasLimits<CosmosFeeTable> = {
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<BroadcastTxResponse> {
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<BroadcastTxResponse> {
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[],