Skip to content

Commit

Permalink
Initial feemarket impl
Browse files Browse the repository at this point in the history
  • Loading branch information
xmariachi committed Nov 30, 2024
1 parent f058947 commit 10561e6
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 17 deletions.
4 changes: 2 additions & 2 deletions config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"alloraHomeDir": "",
"gas": "auto",
"gasAdjustment": 1.5,
"gasPrices": 0.08,
"maxFees": 200000,
"gasPrices": "auto",
"maxFees": 2000000,
"nodeRpc": "https://allora-rpc.testnet.allora.network",
"maxRetries": 5,
"retryDelay": 3,
Expand Down
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/joho/godotenv v1.5.1
github.com/prometheus/client_golang v1.20.5
github.com/rs/zerolog v1.33.0
github.com/skip-mev/feemarket v1.1.1
github.com/stretchr/testify v1.9.0
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
)
Expand Down Expand Up @@ -66,9 +67,9 @@ require (
github.com/dgraph-io/badger/v4 v4.2.0 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/dvsekhvalnov/jose2go v1.6.0 // indirect
github.com/emicklei/dot v1.6.1 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/dvsekhvalnov/jose2go v1.7.0 // indirect
github.com/emicklei/dot v1.6.2 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/getsentry/sentry-go v0.27.0 // indirect
Expand Down Expand Up @@ -166,3 +167,5 @@ require (
pgregory.net/rapid v1.1.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)

// replace github.com/skip-mev/feemarket => /home/diego/workspace-general/feemarket
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/allora-network/allora-chain v0.6.1-0.20241125202548-bed44dbc9bf8 h1:8tXP9XH7ydzPo3y6hiY6kZcepiL96l7nNtz/EvQxfPA=
github.com/allora-network/allora-chain v0.6.1-0.20241125202548-bed44dbc9bf8/go.mod h1:2Yn8VSrrgDE5xyJ9rJFuAmQSRYRBPOnmITWO7Qu2A4U=
github.com/allora-network/allora-chain v0.6.1-0.20241125235711-96936db717d8 h1:EWOk9vUJiW4kZ+q5o33bZ/sfHbNlF6ub1U6WUBBAKuU=
github.com/allora-network/allora-chain v0.6.1-0.20241125235711-96936db717d8/go.mod h1:2Yn8VSrrgDE5xyJ9rJFuAmQSRYRBPOnmITWO7Qu2A4U=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
Expand Down Expand Up @@ -178,14 +176,14 @@ github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:Htrtb
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/dvsekhvalnov/jose2go v1.6.0 h1:Y9gnSnP4qEI0+/uQkHvFXeD2PLPJeXEL+ySMEA2EjTY=
github.com/dvsekhvalnov/jose2go v1.6.0/go.mod h1:QsHjhyTlD/lAVqn/NSbVZmSCGeDehTB/mPZadG+mhXU=
github.com/dvsekhvalnov/jose2go v1.7.0 h1:bnQc8+GMnidJZA8zc6lLEAb4xNrIqHwO+9TzqvtQZPo=
github.com/dvsekhvalnov/jose2go v1.7.0/go.mod h1:QsHjhyTlD/lAVqn/NSbVZmSCGeDehTB/mPZadG+mhXU=
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
github.com/eapache/queue v1.1.0/go.mod h1:6eCeP0CKFpHLu8blIFXhExK/dRa7WDZfr6jVFPTqq+I=
github.com/edsrzf/mmap-go v1.0.0/go.mod h1:YO35OhQPt3KJa3ryjFM5Bs14WD66h8eGKpfaBNrHW5M=
github.com/emicklei/dot v1.6.1 h1:ujpDlBkkwgWUY+qPId5IwapRW/xEoligRSYjioR6DFI=
github.com/emicklei/dot v1.6.1/go.mod h1:DeV7GvQtIw4h2u73RKBkkFdvVAz0D9fzeJrgPW6gy/s=
github.com/emicklei/dot v1.6.2 h1:08GN+DD79cy/tzN6uLCT84+2Wk9u+wvqP+Hkx/dIR8A=
github.com/emicklei/dot v1.6.2/go.mod h1:DeV7GvQtIw4h2u73RKBkkFdvVAz0D9fzeJrgPW6gy/s=
github.com/envoyproxy/go-control-plane v0.6.9/go.mod h1:SBwIajubJHhxtWwsL9s8ss4safvEdbitLhGGK48rN6g=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
Expand All @@ -196,8 +194,8 @@ github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/fortytw2/leaktest v1.3.0 h1:u8491cBMTQ8ft8aeV+adlcytMZylmA5nnwwkRZjI8vw=
Expand Down Expand Up @@ -634,6 +632,8 @@ github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPx
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/skip-mev/feemarket v1.1.1 h1:L34K7N2J6o635kzNYRAvQ93+hAFtSiJ2t03jmaNx0zw=
github.com/skip-mev/feemarket v1.1.1/go.mod h1:DUa6djUsTeMOrbrcIZqWSVxU9IZNCXp96ruaojyBNpc=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
Expand Down
4 changes: 3 additions & 1 deletion lib/domain_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
bank "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/ignite/cli/v28/ignite/pkg/cosmosaccount"
"github.com/ignite/cli/v28/ignite/pkg/cosmosclient"
feemarkettypes "github.com/skip-mev/feemarket/x/feemarket/types"
)

const (
Expand All @@ -25,7 +26,7 @@ type WalletConfig struct {
AlloraHomeDir string // home directory for the allora keystore
Gas string // gas to use for the allora client
GasAdjustment float64 // gas adjustment to use for the allora client
GasPrices float64 // gas prices to use for the allora client - 0 for no fees
GasPrices string // gas prices to use for the allora client - "auto" for no fees
MaxFees uint64 // max gas to use for the allora client
NodeRpc string // rpc node for allora chain
MaxRetries int64 // retry to get data from chain up to this many times per query or tx
Expand All @@ -43,6 +44,7 @@ type ChainConfig struct {
Client *cosmosclient.Client
EmissionsQueryClient emissions.QueryServiceClient
BankQueryClient bank.QueryClient
FeeMarketQueryClient feemarkettypes.QueryClient
DefaultBondDenom string
AddressPrefix string // prefix for the allora addresses
}
Expand Down
5 changes: 5 additions & 0 deletions lib/factory_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/ignite/cli/v28/ignite/pkg/cosmosaccount"
"github.com/ignite/cli/v28/ignite/pkg/cosmosclient"
feemarkettypes "github.com/skip-mev/feemarket/x/feemarket/types"
)

func getAlloraClient(config *UserConfig) (*cosmosclient.Client, error) {
Expand Down Expand Up @@ -107,6 +108,9 @@ func (c *UserConfig) GenerateNodeConfig() (*NodeConfig, error) {
// Create bank client
bankClient := banktypes.NewQueryClient(client.Context())

// Where other clients are initialized, add:
feeMarketQueryClient := feemarkettypes.NewQueryClient(client.Context())

// Check chainId is set
if client.Context().ChainID == "" {
return nil, errors.New("ChainId is empty")
Expand All @@ -125,6 +129,7 @@ func (c *UserConfig) GenerateNodeConfig() (*NodeConfig, error) {
Client: client,
EmissionsQueryClient: queryClient,
BankQueryClient: bankClient,
FeeMarketQueryClient: feeMarketQueryClient,
}

Node := NodeConfig{
Expand Down
39 changes: 39 additions & 0 deletions lib/repo_query_fee.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package lib

import (
"context"
"fmt"
"strconv"
"time"

"github.com/cosmos/cosmos-sdk/types/query"
"github.com/rs/zerolog/log"
feemarkettypes "github.com/skip-mev/feemarket/x/feemarket/types"
)

// GetBaseFee queries the current base fee from the feemarket module
func (node *NodeConfig) GetBaseFee(ctx context.Context) (float64, error) {
resp, err := QueryDataWithRetry(
ctx,
node.Wallet.MaxRetries,
time.Duration(node.Wallet.RetryDelay)*time.Second,
func(ctx context.Context, req query.PageRequest) (*feemarkettypes.GasPriceResponse, error) {
return node.Chain.FeeMarketQueryClient.GasPrice(ctx, &feemarkettypes.GasPriceRequest{Denom: node.Chain.DefaultBondDenom})
},
query.PageRequest{},
"get base fee",
)
if err != nil {
return 0, err
}

// Convert legacyDec to string first, then to float64
baseFeeStr := resp.Price.Amount.String()
baseFee, err := strconv.ParseFloat(baseFeeStr, 64)
if err != nil {
return 0, fmt.Errorf("failed to parse base fee: %w", err)
}

log.Debug().Float64("baseFee", baseFee).Msg("Retrieved base fee from chain")
return baseFee, nil
}
26 changes: 23 additions & 3 deletions lib/repo_tx_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
sdktypes "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ignite/cli/v28/ignite/pkg/cosmosclient"
feemarkettypes "github.com/skip-mev/feemarket/x/feemarket/types"
)

const ERROR_MESSAGE_ABCI_ERROR_CODE_MARKER = "error code:"
Expand Down Expand Up @@ -77,6 +78,8 @@ func processError(err error, infoMsg string, retryCount int64, node *NodeConfig)
return ERROR_PROCESSING_CONTINUE, nil
case int(sdkerrors.ErrInsufficientFee.ABCICode()):
return ERROR_PROCESSING_FEES, nil
case int(feemarkettypes.ErrNoFeeCoins.ABCICode()):
return ERROR_PROCESSING_FEES, nil
case int(sdkerrors.ErrTxTooLarge.ABCICode()):
return ERROR_PROCESSING_ERROR, errorsmod.Wrapf(err, "tx too large")
case int(sdkerrors.ErrTxInMempoolCache.ABCICode()):
Expand Down Expand Up @@ -139,12 +142,29 @@ func processError(err error, infoMsg string, retryCount int64, node *NodeConfig)
return ERROR_PROCESSING_ERROR, errorsmod.Wrapf(err, "failed to process error")
}

// Helper function to get gas prices either from config or chain
func (node *NodeConfig) getGasPrices(ctx context.Context) (float64, error) {
if node.Wallet.GasPrices == "auto" {
return node.GetBaseFee(ctx)
}

gasPrices, err := strconv.ParseFloat(node.Wallet.GasPrices, 64)
if err != nil {
return 0, fmt.Errorf("invalid gas prices format: %w", err)
}
return gasPrices, nil
}

// SendDataWithRetry attempts to send data, handling retries, with fee awareness.
// Custom handling for different errors.
func (node *NodeConfig) SendDataWithRetry(ctx context.Context, req sdktypes.Msg, infoMsg string, timeoutHeight uint64) (*cosmosclient.Response, error) {
var txResp *cosmosclient.Response
// Excess fees correction factor translated to fees using configured gas prices
excessFactorFees := float64(EXCESS_CORRECTION_IN_GAS) * node.Wallet.GasPrices
gasPrices, err := node.getGasPrices(ctx)
if err != nil {
return nil, fmt.Errorf("failed to get gas prices: %w", err)
}
excessFactorFees := float64(EXCESS_CORRECTION_IN_GAS) * gasPrices
// Keep track of how many times fees need to be recalculated to avoid missing fee info between errors
recalculateFees := 0
// Use to keep track of expected sequence number between errors
Expand Down Expand Up @@ -220,9 +240,9 @@ func (node *NodeConfig) SendDataWithRetry(ctx context.Context, req sdktypes.Msg,
}

// Handle fees if necessary
if node.Wallet.GasPrices > 0 && recalculateFees > 0 {
if gasPrices > 0 && recalculateFees > 0 {
// Precalculate fees
fees := uint64(float64(txService.Gas()+EXCESS_CORRECTION_IN_GAS) * node.Wallet.GasPrices)
fees := uint64(float64(txService.Gas()+EXCESS_CORRECTION_IN_GAS) * gasPrices)
// Add excess fees correction factor to increase with each fee-problematic retry
fees = fees + uint64(float64(recalculateFees)*excessFactorFees)
// Limit fees to maxFees
Expand Down

0 comments on commit 10561e6

Please sign in to comment.