-
Notifications
You must be signed in to change notification settings - Fork 13
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
refactor: decouple base fee fetching as part of quote from simulation #550
Changes from 2 commits
979a958
a933e0c
052139b
5004eec
dc26ec0
5862547
9b1e5dd
e68a836
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package domain | ||
|
||
import "github.com/osmosis-labs/osmosis/osmomath" | ||
|
||
// BaseFee holds the denom and current base fee | ||
type BaseFee struct { | ||
Denom string | ||
CurrentFee osmomath.Dec | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package tx | |
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
cosmosclient "github.com/cosmos/cosmos-sdk/client" | ||
txclient "github.com/cosmos/cosmos-sdk/client/tx" | ||
|
@@ -11,8 +12,10 @@ import ( | |
signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
"github.com/osmosis-labs/osmosis/v26/app/params" | ||
txfeestypes "github.com/osmosis-labs/osmosis/v26/x/txfees/types" | ||
"github.com/osmosis-labs/sqs/domain" | ||
"github.com/osmosis-labs/sqs/domain/keyring" | ||
routerrepo "github.com/osmosis-labs/sqs/router/repository" | ||
"google.golang.org/grpc" | ||
|
||
gogogrpc "github.com/cosmos/gogoproto/grpc" | ||
) | ||
|
@@ -22,7 +25,6 @@ type MsgSimulator interface { | |
BuildTx( | ||
ctx context.Context, | ||
keyring keyring.Keyring, | ||
txfeesClient txfeestypes.QueryClient, | ||
encodingConfig params.EncodingConfig, | ||
account *authtypes.BaseAccount, | ||
chainID string, | ||
|
@@ -43,19 +45,19 @@ type MsgSimulator interface { | |
// which is the fee amount in the base denomination. | ||
PriceMsgs( | ||
ctx context.Context, | ||
txfeesClient txfeestypes.QueryClient, | ||
encodingConfig cosmosclient.TxConfig, | ||
account *authtypes.BaseAccount, | ||
chainID string, | ||
msg ...sdk.Msg, | ||
) (uint64, sdk.Coin, error) | ||
) domain.QuotePriceInfo | ||
} | ||
|
||
// NewGasCalculator creates a new GasCalculator instance. | ||
func NewGasCalculator(clientCtx gogogrpc.ClientConn, calculateGas CalculateGasFn) MsgSimulator { | ||
// NewMsgSimulator creates a new GasCalculator instance. | ||
func NewMsgSimulator(clientCtx gogogrpc.ClientConn, calculateGas CalculateGasFn, memoryRouterRepository routerrepo.RouterRepository) MsgSimulator { | ||
return &txGasCalulator{ | ||
clientCtx: clientCtx, | ||
calculateGas: calculateGas, | ||
clientCtx: clientCtx, | ||
calculateGas: calculateGas, | ||
memoryRouterRepository: memoryRouterRepository, | ||
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. NOTE: See first this comment. Can we instead of pulling in type FeeSource interface {
GetBaseFee() domain.BaseFee
} 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. Done |
||
} | ||
} | ||
|
||
|
@@ -64,16 +66,16 @@ type CalculateGasFn func(clientCtx gogogrpc.ClientConn, txf txclient.Factory, ms | |
|
||
// txGasCalulator is a GasCalculator implementation that uses simulated transactions to calculate gas. | ||
type txGasCalulator struct { | ||
clientCtx gogogrpc.ClientConn | ||
calculateGas CalculateGasFn | ||
clientCtx grpc.ClientConnInterface | ||
calculateGas CalculateGasFn | ||
memoryRouterRepository routerrepo.RouterRepository | ||
} | ||
|
||
// BuildTx constructs a transaction using the provided parameters and messages. | ||
// Returns a TxBuilder and any error encountered. | ||
func (c *txGasCalulator) BuildTx( | ||
ctx context.Context, | ||
keyring keyring.Keyring, | ||
txfeesClient txfeestypes.QueryClient, | ||
encodingConfig params.EncodingConfig, | ||
account *authtypes.BaseAccount, | ||
chainID string, | ||
|
@@ -90,13 +92,13 @@ func (c *txGasCalulator) BuildTx( | |
return nil, err | ||
} | ||
|
||
gasAdjusted, feecoin, err := c.PriceMsgs(ctx, txfeesClient, encodingConfig.TxConfig, account, chainID, msg...) | ||
if err != nil { | ||
return nil, err | ||
priceInfo := c.PriceMsgs(ctx, encodingConfig.TxConfig, account, chainID, msg...) | ||
if priceInfo.Err != "" { | ||
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. While 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. It was changed to string so that we can return this to the client as a JSON response. Duplicating Happy for this to be iterated in a follow-up if you're feeling strongly |
||
return nil, errors.New(priceInfo.Err) | ||
} | ||
|
||
txBuilder.SetGasLimit(gasAdjusted) | ||
txBuilder.SetFeeAmount(sdk.Coins{feecoin}) | ||
txBuilder.SetGasLimit(priceInfo.AdjustedGasUsed) | ||
txBuilder.SetFeeAmount(sdk.Coins{priceInfo.FeeCoin}) | ||
|
||
sigV2 := BuildSignatures(privKey.PubKey(), nil, account.Sequence) | ||
err = txBuilder.SetSignatures(sigV2) | ||
|
@@ -145,23 +147,33 @@ func (c *txGasCalulator) SimulateMsgs(encodingConfig cosmosclient.TxConfig, acco | |
} | ||
|
||
// PriceMsgs implements MsgSimulator. | ||
func (c *txGasCalulator) PriceMsgs(ctx context.Context, txfeesClient txfeestypes.QueryClient, encodingConfig cosmosclient.TxConfig, account *authtypes.BaseAccount, chainID string, msg ...sdk.Msg) (uint64, sdk.Coin, error) { | ||
func (c *txGasCalulator) PriceMsgs(ctx context.Context, encodingConfig cosmosclient.TxConfig, account *authtypes.BaseAccount, chainID string, msg ...sdk.Msg) domain.QuotePriceInfo { | ||
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.
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. Done |
||
baseFee := c.memoryRouterRepository.GetBaseFee() | ||
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. Do we want to keep backwards compatibility with previous implementation where we are able to compute fee by making request to chain instead of using pre-computed fee value? For example, is there any possible scenario where we might want to recompute fee for each simulation? 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. The base fee can only be updated after processing transactions. As a result, refetching it once per-block is appropriate |
||
if baseFee.CurrentFee.IsNil() || baseFee.CurrentFee.IsZero() { | ||
return domain.QuotePriceInfo{Err: "base fee is zero or nil"} | ||
} | ||
if baseFee.Denom == "" { | ||
return domain.QuotePriceInfo{Err: "base fee denom is empty"} | ||
} | ||
|
||
_, gasAdjusted, err := c.SimulateMsgs( | ||
encodingConfig, | ||
account, | ||
chainID, | ||
msg, | ||
) | ||
if err != nil { | ||
return 0, sdk.Coin{}, err | ||
return domain.QuotePriceInfo{Err: err.Error(), BaseFee: baseFee.CurrentFee} | ||
} | ||
|
||
feeCoin, err := CalculateFeeCoin(ctx, txfeesClient, gasAdjusted) | ||
if err != nil { | ||
return 0, sdk.Coin{}, err | ||
} | ||
feeAmount := CalculateFeeAmount(baseFee.CurrentFee, gasAdjusted) | ||
|
||
return gasAdjusted, feeCoin, nil | ||
return domain.QuotePriceInfo{ | ||
AdjustedGasUsed: gasAdjusted, | ||
FeeCoin: sdk.Coin{Denom: baseFee.Denom, Amount: feeAmount}, | ||
BaseFee: baseFee.CurrentFee, | ||
Err: "", | ||
} | ||
} | ||
|
||
// CalculateGas calculates the gas required for a transaction using the provided transaction factory and messages. | ||
|
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.
Do we really want to have new abstraction? I think
sdk.Coin
already would decribe it quite well:We can have type alias, but I still personally find it redundant:
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.
Dec