-
Notifications
You must be signed in to change notification settings - Fork 404
add dynamic gasfee pricing as a signing client option - Feemarket/Osmosis #1911
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ertemann
wants to merge
3
commits into
cosmos:main
Choose a base branch
from
ertemann:add-feemarket-auto
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,248
−22
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import { encodeSecp256k1Pubkey, makeSignDoc as makeSignDocAmino } from "@cosmjs/amino"; | ||
| import { sha256 } from "@cosmjs/crypto"; | ||
| import { fromBase64, toHex, toUtf8 } from "@cosmjs/encoding"; | ||
| import { Int53, Uint53 } from "@cosmjs/math"; | ||
| import { Decimal, Int53, Uint53 } from "@cosmjs/math"; | ||
| import { | ||
| EncodeObject, | ||
| encodePubkey, | ||
|
|
@@ -20,6 +20,7 @@ | |
| createDefaultAminoConverters, | ||
| defaultRegistryTypes as defaultStargateTypes, | ||
| DeliverTxResponse, | ||
| DynamicGasPriceConfig, | ||
| Event, | ||
| GasPrice, | ||
| isDeliverTxFailure, | ||
|
|
@@ -28,6 +29,8 @@ | |
| MsgSendEncodeObject, | ||
| MsgUndelegateEncodeObject, | ||
| MsgWithdrawDelegatorRewardEncodeObject, | ||
| multiplyDecimalByNumber, | ||
| queryDynamicGasPrice, | ||
| SignerData, | ||
| StdFee, | ||
| } from "@cosmjs/stargate"; | ||
|
|
@@ -190,7 +193,8 @@ | |
| readonly aminoTypes?: AminoTypes; | ||
| readonly broadcastTimeoutMs?: number; | ||
| readonly broadcastPollIntervalMs?: number; | ||
| readonly gasPrice?: GasPrice; | ||
| /** Gas price configuration. Can be a static GasPrice or a DynamicGasPriceConfig for dynamic pricing. */ | ||
| readonly gasPrice?: GasPrice | DynamicGasPriceConfig; | ||
| } | ||
|
|
||
| export class SigningCosmWasmClient extends CosmWasmClient { | ||
|
|
@@ -200,10 +204,12 @@ | |
|
|
||
| private readonly signer: OfflineSigner; | ||
| private readonly aminoTypes: AminoTypes; | ||
| private readonly gasPrice: GasPrice | undefined; | ||
| private readonly gasPrice: GasPrice | DynamicGasPriceConfig | undefined; | ||
| // Starting with Cosmos SDK 0.47, we see many cases in which 1.3 is not enough anymore | ||
| // E.g. https://github.com/cosmos/cosmos-sdk/issues/16020 | ||
| private readonly defaultGasMultiplier = 1.4; | ||
| // Default multiplier for dynamic gas price (applied on top of queried price) | ||
| private readonly defaultDynamicGasMultiplier = 1.3; | ||
|
|
||
| /** | ||
| * Creates an instance by connecting to the given CometBFT RPC endpoint. | ||
|
|
@@ -615,10 +621,7 @@ | |
| ): Promise<DeliverTxResponse> { | ||
| let usedFee: StdFee; | ||
| 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); | ||
| const multiplier = typeof fee === "number" ? fee : this.defaultGasMultiplier; | ||
| usedFee = calculateFee(Math.round(gasEstimation * multiplier), this.gasPrice); | ||
| usedFee = await this.calculateFeeForTransaction(signerAddress, messages, memo, fee); | ||
| } else { | ||
| usedFee = fee; | ||
| } | ||
|
|
@@ -651,10 +654,7 @@ | |
| ): Promise<string> { | ||
| let usedFee: StdFee; | ||
| 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); | ||
| const multiplier = typeof fee === "number" ? fee : this.defaultGasMultiplier; | ||
| usedFee = calculateFee(Math.round(gasEstimation * multiplier), this.gasPrice); | ||
| usedFee = await this.calculateFeeForTransaction(signerAddress, messages, memo, fee); | ||
| } else { | ||
| usedFee = fee; | ||
| } | ||
|
|
@@ -663,6 +663,78 @@ | |
| return this.broadcastTxSync(txBytes); | ||
| } | ||
|
|
||
| private async calculateFeeForTransaction( | ||
| signerAddress: string, | ||
| messages: readonly EncodeObject[], | ||
| memo: string, | ||
| fee: "auto" | number, | ||
| ): Promise<StdFee> { | ||
| const gasEstimation = await this.simulate(signerAddress, messages, memo); | ||
| const multiplier = typeof fee === "number" ? fee : this.defaultGasMultiplier; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At this point we know the gas limit, right? Everything else below is just about the price. Adding const gasLimit = Math.ceil(gasEstimation * multiplier)here would remove the 4x |
||
|
|
||
| const gasPriceConfig = this.gasPrice; | ||
|
|
||
| // Check if gasPrice is dynamic config or static GasPrice | ||
| if (gasPriceConfig && "minGasPrice" in gasPriceConfig) { | ||
| // Dynamic gas price config | ||
| const dynamicGasConfig = gasPriceConfig; | ||
| const multiplierValue = dynamicGasConfig.multiplier ?? this.defaultDynamicGasMultiplier; | ||
| const minGasPrice = dynamicGasConfig.minGasPrice; | ||
| const maxGasPrice = dynamicGasConfig.maxGasPrice; | ||
|
|
||
| try { | ||
| const chainId = await this.getChainId(); | ||
| const queryClient = this.forceGetQueryClient(); | ||
| const dynamicGasPriceDecimal = await queryDynamicGasPrice( | ||
| queryClient, | ||
| dynamicGasConfig.denom, | ||
| chainId, | ||
| ); | ||
|
|
||
| // Multiply by multiplier 18 fractional digits for Dec type | ||
| const fractionalDigits = minGasPrice.amount.fractionalDigits; | ||
| const adjustedGasPrice = multiplyDecimalByNumber( | ||
| dynamicGasPriceDecimal, | ||
| multiplierValue, | ||
| fractionalDigits, | ||
| ); | ||
|
|
||
| // Apply min and max constraints using comparison methods | ||
| let finalGasPrice = adjustedGasPrice.isGreaterThan(minGasPrice.amount) | ||
| ? adjustedGasPrice | ||
| : minGasPrice.amount; | ||
| if (maxGasPrice) { | ||
| // Normalize maxGasPrice to same fractional digits if needed (user might create it differently) | ||
| const normalizedMaxGasPrice = | ||
| maxGasPrice.amount.fractionalDigits === fractionalDigits | ||
| ? maxGasPrice.amount | ||
| : Decimal.fromUserInput(maxGasPrice.amount.toString(), fractionalDigits); | ||
| finalGasPrice = finalGasPrice.isLessThan(normalizedMaxGasPrice) | ||
| ? finalGasPrice | ||
| : normalizedMaxGasPrice; | ||
| } | ||
| const dynamicGasPriceObj = new GasPrice(finalGasPrice, dynamicGasConfig.denom); | ||
| return calculateFee(Math.round(gasEstimation * multiplier), dynamicGasPriceObj); | ||
| } catch (error) { | ||
| // Fallback to minGasPrice if query fails | ||
| return calculateFee(Math.round(gasEstimation * multiplier), minGasPrice); | ||
| } | ||
| } else { | ||
| // Static gas price | ||
| if (!gasPriceConfig) { | ||
| throw new Error("Gas price must be set in the client options when auto gas is used."); | ||
| } | ||
| if ( | ||
| !(gasPriceConfig instanceof GasPrice) && | ||
| !("amount" in gasPriceConfig && "denom" in gasPriceConfig) | ||
| ) { | ||
| throw new Error("Gas price must be a GasPrice instance when using static pricing."); | ||
| } | ||
| const staticGasPrice = gasPriceConfig as GasPrice; | ||
| return calculateFee(Math.round(gasEstimation * multiplier), staticGasPrice); | ||
| } | ||
| } | ||
|
|
||
| public async sign( | ||
| signerAddress: string, | ||
| messages: readonly EncodeObject[], | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a gas price multiplier, not a gas multiplier, right? I think it is important to highlight this e.g. by renaming