Allow the use of a custom gas multiplier

This commit is contained in:
Simon Warta 2021-11-19 11:26:56 +01:00
parent 86467822f9
commit 37b472af44
3 changed files with 51 additions and 21 deletions

View File

@ -34,6 +34,16 @@ const client = await SigningStargateClient.connectWithSigner(rpcEndpoint, wallet
console.log("Successfully broadcasted:", result);
}
// Send transaction (using sendTokens with auto gas and custom muliplier)
{
const recipient = "cosmos1xv9tklw7d82sezh9haa573wufgy59vmwe6xxe5";
const amount = coins(1234567, "ucosm");
const memo = "With simulate";
const result = await client.sendTokens(account.address, recipient, amount, 1.2, memo);
assertIsBroadcastTxSuccess(result);
console.log("Successfully broadcasted:", result);
}
// Send transaction (using sendTokens with manual gas)
{
const recipient = "cosmos1xv9tklw7d82sezh9haa573wufgy59vmwe6xxe5";
@ -72,6 +82,24 @@ const client = await SigningStargateClient.connectWithSigner(rpcEndpoint, wallet
console.log("Successfully broadcasted:", result);
}
// Send transaction (using signAndBroadcast with auto gas and custom muliplier)
{
const recipient = "cosmos1xv9tklw7d82sezh9haa573wufgy59vmwe6xxe5";
const amount = coins(1234567, "ucosm");
const sendMsg: MsgSendEncodeObject = {
typeUrl: "/cosmos.bank.v1beta1.MsgSend",
value: {
fromAddress: account.address,
toAddress: recipient,
amount: amount,
},
};
const memo = "With simulate";
const result = await client.signAndBroadcast(account.address, [sendMsg], 1.4, memo);
assertIsBroadcastTxSuccess(result);
console.log("Successfully broadcasted:", result);
}
// Send transaction (using signAndBroadcast with manual gas)
{
const recipient = "cosmos1xv9tklw7d82sezh9haa573wufgy59vmwe6xxe5";

View File

@ -224,7 +224,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
public async upload(
senderAddress: string,
wasmCode: Uint8Array,
fee: StdFee | "auto",
fee: StdFee | "auto" | number,
memo = "",
): Promise<UploadResult> {
const compressed = pako.gzip(wasmCode, { level: 9 });
@ -258,7 +258,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
codeId: number,
msg: Record<string, unknown>,
label: string,
fee: StdFee | "auto",
fee: StdFee | "auto" | number,
options: InstantiateOptions = {},
): Promise<InstantiateResult> {
const instantiateContractMsg: MsgInstantiateContractEncodeObject = {
@ -289,7 +289,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
senderAddress: string,
contractAddress: string,
newAdmin: string,
fee: StdFee | "auto",
fee: StdFee | "auto" | number,
memo = "",
): Promise<ChangeAdminResult> {
const updateAdminMsg: MsgUpdateAdminEncodeObject = {
@ -313,7 +313,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
public async clearAdmin(
senderAddress: string,
contractAddress: string,
fee: StdFee | "auto",
fee: StdFee | "auto" | number,
memo = "",
): Promise<ChangeAdminResult> {
const clearAdminMsg: MsgClearAdminEncodeObject = {
@ -338,7 +338,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
contractAddress: string,
codeId: number,
migrateMsg: Record<string, unknown>,
fee: StdFee | "auto",
fee: StdFee | "auto" | number,
memo = "",
): Promise<MigrateResult> {
const migrateContractMsg: MsgMigrateContractEncodeObject = {
@ -364,7 +364,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
senderAddress: string,
contractAddress: string,
msg: Record<string, unknown>,
fee: StdFee | "auto",
fee: StdFee | "auto" | number,
memo = "",
funds?: readonly Coin[],
): Promise<ExecuteResult> {
@ -391,7 +391,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
senderAddress: string,
recipientAddress: string,
amount: readonly Coin[],
fee: StdFee | "auto",
fee: StdFee | "auto" | number,
memo = "",
): Promise<BroadcastTxResponse> {
const sendMsg: MsgSendEncodeObject = {
@ -409,7 +409,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
delegatorAddress: string,
validatorAddress: string,
amount: Coin,
fee: StdFee | "auto",
fee: StdFee | "auto" | number,
memo = "",
): Promise<BroadcastTxResponse> {
const delegateMsg: MsgDelegateEncodeObject = {
@ -423,7 +423,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
delegatorAddress: string,
validatorAddress: string,
amount: Coin,
fee: StdFee | "auto",
fee: StdFee | "auto" | number,
memo = "",
): Promise<BroadcastTxResponse> {
const undelegateMsg: MsgUndelegateEncodeObject = {
@ -436,7 +436,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
public async withdrawRewards(
delegatorAddress: string,
validatorAddress: string,
fee: StdFee | "auto",
fee: StdFee | "auto" | number,
memo = "",
): Promise<BroadcastTxResponse> {
const withdrawDelegatorRewardMsg: MsgWithdrawDelegatorRewardEncodeObject = {
@ -457,14 +457,15 @@ export class SigningCosmWasmClient extends CosmWasmClient {
public async signAndBroadcast(
signerAddress: string,
messages: readonly EncodeObject[],
fee: StdFee | "auto",
fee: StdFee | "auto" | number,
memo = "",
): Promise<BroadcastTxResponse> {
let usedFee: StdFee;
if (fee == "auto") {
if (fee == "auto" || typeof fee === "number") {
assertDefined(this.gasPrice, "Gas price must be set in the client options when auto gas is used.");
const gasEstimation = await this.simulate(signerAddress, messages, memo);
usedFee = calculateFee(Math.round(gasEstimation * 1.3), this.gasPrice);
const muliplier = typeof fee === "number" ? fee : 1.3;
usedFee = calculateFee(Math.round(gasEstimation * muliplier), this.gasPrice);
} else {
usedFee = fee;
}

View File

@ -208,7 +208,7 @@ export class SigningStargateClient extends StargateClient {
senderAddress: string,
recipientAddress: string,
amount: readonly Coin[],
fee: StdFee | "auto",
fee: StdFee | "auto" | number,
memo = "",
): Promise<BroadcastTxResponse> {
const sendMsg: MsgSendEncodeObject = {
@ -226,7 +226,7 @@ export class SigningStargateClient extends StargateClient {
delegatorAddress: string,
validatorAddress: string,
amount: Coin,
fee: StdFee | "auto",
fee: StdFee | "auto" | number,
memo = "",
): Promise<BroadcastTxResponse> {
const delegateMsg: MsgDelegateEncodeObject = {
@ -244,7 +244,7 @@ export class SigningStargateClient extends StargateClient {
delegatorAddress: string,
validatorAddress: string,
amount: Coin,
fee: StdFee | "auto",
fee: StdFee | "auto" | number,
memo = "",
): Promise<BroadcastTxResponse> {
const undelegateMsg: MsgUndelegateEncodeObject = {
@ -261,7 +261,7 @@ export class SigningStargateClient extends StargateClient {
public async withdrawRewards(
delegatorAddress: string,
validatorAddress: string,
fee: StdFee | "auto",
fee: StdFee | "auto" | number,
memo = "",
): Promise<BroadcastTxResponse> {
const withdrawMsg: MsgWithdrawDelegatorRewardEncodeObject = {
@ -283,7 +283,7 @@ export class SigningStargateClient extends StargateClient {
timeoutHeight: Height | undefined,
/** timeout in seconds */
timeoutTimestamp: number | undefined,
fee: StdFee | "auto",
fee: StdFee | "auto" | number,
memo = "",
): Promise<BroadcastTxResponse> {
const timeoutTimestampNanoseconds = timeoutTimestamp
@ -307,14 +307,15 @@ export class SigningStargateClient extends StargateClient {
public async signAndBroadcast(
signerAddress: string,
messages: readonly EncodeObject[],
fee: StdFee | "auto",
fee: StdFee | "auto" | number,
memo = "",
): Promise<BroadcastTxResponse> {
let usedFee: StdFee;
if (fee == "auto") {
if (fee == "auto" || typeof fee === "number") {
assertDefined(this.gasPrice, "Gas price must be set in the client options when auto gas is used.");
const gasEstimation = await this.simulate(signerAddress, messages, memo);
usedFee = calculateFee(Math.round(gasEstimation * 1.3), this.gasPrice);
const muliplier = typeof fee === "number" ? fee : 1.3;
usedFee = calculateFee(Math.round(gasEstimation * muliplier), this.gasPrice);
} else {
usedFee = fee;
}