stargate: Add delegateTokens method to SigningStargateClient

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

View File

@ -40,6 +40,15 @@ describe("SigningStargateClient", () => {
],
gas: "80000",
},
delegate: {
amount: [
{
amount: "4000",
denom: "ucosm",
},
],
gas: "160000",
},
});
});
@ -71,6 +80,15 @@ describe("SigningStargateClient", () => {
],
gas: "80000",
},
delegate: {
amount: [
{
amount: "502400", // 3.14 * 160_000
denom: "utest",
},
],
gas: "160000",
},
});
});
@ -79,6 +97,7 @@ describe("SigningStargateClient", () => {
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic);
const gasLimits = {
send: 160000,
delegate: 120000,
};
const options = { gasLimits: gasLimits };
const client = await SigningStargateClient.connectWithSigner(simapp.tendermintUrl, wallet, options);
@ -93,6 +112,15 @@ describe("SigningStargateClient", () => {
],
gas: "160000",
},
delegate: {
amount: [
{
amount: "3000", // 0.025 * 120_000
denom: "ucosm",
},
],
gas: "120000",
},
});
});
@ -102,6 +130,7 @@ 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);
@ -116,6 +145,15 @@ describe("SigningStargateClient", () => {
],
gas: "160000",
},
delegate: {
amount: [
{
amount: "502400", // 3.14 * 160_000
denom: "utest",
},
],
gas: "160000",
},
});
});
});

View File

@ -66,10 +66,11 @@ import { BroadcastTxResponse, StargateClient } from "./stargateclient";
*/
export interface CosmosFeeTable extends FeeTable {
readonly send: StdFee;
readonly delegate: StdFee;
}
const defaultGasPrice = GasPrice.fromString("0.025ucosm");
const defaultGasLimits: GasLimits<CosmosFeeTable> = { send: 80000 };
const defaultGasLimits: GasLimits<CosmosFeeTable> = { send: 80000, delegate: 160000 };
export const defaultRegistryTypes: ReadonlyArray<[string, GeneratedType]> = [
["/cosmos.bank.v1beta1.MsgMultiSend", MsgMultiSend],
@ -198,6 +199,19 @@ export class SigningStargateClient extends StargateClient {
return this.signAndBroadcast(senderAddress, [sendMsg], this.fees.send, memo);
}
public async delegateTokens(
senderAddress: string,
validatorAddress: string,
amount: Coin,
memo = "",
): Promise<BroadcastTxResponse> {
const delegateMsg = {
typeUrl: "/cosmos.staking.v1beta1.MsgDelegate",
value: MsgDelegate.fromPartial({ delegatorAddress: senderAddress, validatorAddress, amount }),
};
return this.signAndBroadcast(senderAddress, [delegateMsg], this.fees.delegate, memo);
}
public async signAndBroadcast(
signerAddress: string,
messages: readonly EncodeObject[],