From 2e919bbf8ed584d771e306488fe135f7f98393b9 Mon Sep 17 00:00:00 2001 From: Diego Campo Date: Thu, 16 Jan 2025 15:51:31 +0100 Subject: [PATCH] Addition of FlexibleCosmosIntAmount to support int and str --- lib/domain_config.go | 37 ++++++++++++++++--------------- lib/repo_tx_registration.go | 7 +++--- lib/repo_tx_utils.go | 6 +++--- lib/type_allora.go | 43 +++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 26 deletions(-) diff --git a/lib/domain_config.go b/lib/domain_config.go index 324cf4e..74df3de 100644 --- a/lib/domain_config.go +++ b/lib/domain_config.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" - cosmossdk_io_math "cosmossdk.io/math" emissions "github.com/allora-network/allora-chain/x/emissions/types" bank "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/ignite/cli/v28/ignite/pkg/cosmosaccount" @@ -37,23 +36,23 @@ type WalletConfig struct { Address string // will be overwritten by the keystore. This is the 1 value that is auto-generated in this struct AddressKeyName string // load a address by key from the keystore AddressRestoreMnemonic string - 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 string // gas prices to use for the allora client - "auto" for auto-calculated fees - GasPriceUpdateInterval int64 // number of seconds to wait between updates to the gas price - MaxFees cosmossdk_io_math.Int // max fees to pay for a single transaction - NodeRpc string // rpc node for allora chain - MaxRetries int64 // retry to get data from chain up to this many times per query or tx - RetryDelay int64 // number of seconds to wait between retries (general case) - AccountSequenceRetryDelay int64 // number of seconds to wait between retries in case of account sequence error - SubmitTx bool // useful for dev/testing. set to false to run in dry-run processes without committing to the chain - BlockDurationEstimated float64 // estimated average block duration in seconds - WindowCorrectionFactor float64 // correction factor for the time estimation, suggested range 0.7-0.9. - TimeoutRPCSecondsQuery int64 // timeout for rpc queries in seconds, including retries - TimeoutRPCSecondsTx int64 // timeout for rpc data send in seconds, including retries - TimeoutRPCSecondsRegistration int64 // timeout for rpc registration in seconds, including retries - TimeoutHTTPConnection int64 // timeout for http connection in seconds + 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 string // gas prices to use for the allora client - "auto" for auto-calculated fees + GasPriceUpdateInterval int64 // number of seconds to wait between updates to the gas price + MaxFees FlexibleCosmosIntAmount // max fees to pay for a single transaction (as string or number) + NodeRpc string // rpc node for allora chain + MaxRetries int64 // retry to get data from chain up to this many times per query or tx + RetryDelay int64 // number of seconds to wait between retries (general case) + AccountSequenceRetryDelay int64 // number of seconds to wait between retries in case of account sequence error + SubmitTx bool // useful for dev/testing. set to false to run in dry-run processes without committing to the chain + BlockDurationEstimated float64 // estimated average block duration in seconds + WindowCorrectionFactor float64 // correction factor for the time estimation, suggested range 0.7-0.9. + TimeoutRPCSecondsQuery int64 // timeout for rpc queries in seconds, including retries + TimeoutRPCSecondsTx int64 // timeout for rpc data send in seconds, including retries + TimeoutRPCSecondsRegistration int64 // timeout for rpc registration in seconds, including retries + TimeoutHTTPConnection int64 // timeout for http connection in seconds } // Properties auto-generated based on what the user has provided in WalletConfig fields of UserConfig @@ -96,7 +95,7 @@ type ReputerConfig struct { // Will not repute if current stake is less than this, after trying to add any necessary stake. // This is idempotent in that it will not add more stake than specified here. // Set to 0 to effectively disable this feature and use whatever stake has already been added. - MinStake cosmossdk_io_math.Int + MinStake FlexibleCosmosIntAmount GroundTruthParameters map[string]string // Map for variable configuration values LossFunctionParameters LossFunctionParameters // Map for variable configuration values } diff --git a/lib/repo_tx_registration.go b/lib/repo_tx_registration.go index 05d1283..205bb3b 100644 --- a/lib/repo_tx_registration.go +++ b/lib/repo_tx_registration.go @@ -4,9 +4,8 @@ import ( "context" "fmt" - "github.com/rs/zerolog/log" - emissionstypes "github.com/allora-network/allora-chain/x/emissions/types" + "github.com/rs/zerolog/log" ) // True if the actor is ultimately, definitively registered for the specified topic, else False @@ -148,9 +147,9 @@ func (node *NodeConfig) RegisterAndStakeReputerIdempotently(ctx context.Context, return false, err } - minStake := config.MinStake + minStake := config.MinStake.Number if minStake.LTE(stake) { - log.Info().Msg("Stake above minimum requested stake, skipping adding stake.") + log.Info().Interface("stake", stake).Interface("minStake", minStake).Msg("Stake above minimum requested stake, skipping adding stake.") return true, nil } else { log.Info().Interface("stake", stake).Interface("minStake", minStake).Interface("stakeToAdd", minStake.Sub(stake)).Msg("Stake below minimum requested stake, adding stake.") diff --git a/lib/repo_tx_utils.go b/lib/repo_tx_utils.go index 30f63b8..3c1d10a 100644 --- a/lib/repo_tx_utils.go +++ b/lib/repo_tx_utils.go @@ -258,12 +258,12 @@ func (node *NodeConfig) SendDataWithRetry(ctx context.Context, req sdktypes.Msg, feeAdjustment := int64(float64(recalculateFees) * excessFactorFees) fees = fees.Add(cosmossdk_io_math.NewInt(feeAdjustment)) // Limit fees to maxFees - if fees.GT(node.Wallet.MaxFees) { + if fees.GT(node.Wallet.MaxFees.Number) { log.Warn().Uint64("gas", txService.Gas()).Interface("limit", node.Wallet.MaxFees).Msg("Gas limit exceeded, using maxFees instead") - fees = node.Wallet.MaxFees + fees = node.Wallet.MaxFees.Number } txOptions := cosmosclient.TxOptions{ // nolint: exhaustruct - Fees: fmt.Sprintf("%duallo", fees), + Fees: fmt.Sprintf("%suallo", fees.String()), } log.Info().Str("fees", txOptions.Fees).Msg("Attempting tx with calculated fees") txService, err = node.Chain.Client.CreateTxWithOptions(ctx, node.Chain.Account, txOptions, req) diff --git a/lib/type_allora.go b/lib/type_allora.go index f82cb92..db92691 100644 --- a/lib/type_allora.go +++ b/lib/type_allora.go @@ -1,5 +1,48 @@ package lib +import ( + "encoding/json" + "fmt" + "strconv" + + cosmossdk_io_math "cosmossdk.io/math" +) + type Address = string type Allo = int64 type BlockHeight = int64 + +// FlexibleCosmosIntAmount represents amounts of tokens, where the amount can be specified or as a string +type FlexibleCosmosIntAmount struct { + Number cosmossdk_io_math.Int +} + +func (fv *FlexibleCosmosIntAmount) String() string { + fmt.Println("fv.Number", fv.Number) + return fv.Number.String() +} + +func (fv *FlexibleCosmosIntAmount) UnmarshalJSON(data []byte) error { + // Handle number + if data[0] >= '0' && data[0] <= '9' { + var num int64 + if err := json.Unmarshal(data, &num); err != nil { + return fmt.Errorf("failed to parse number: %w", err) + } + fv.Number = cosmossdk_io_math.NewInt(num) + return nil + } + + // Handle string + var str string + if err := json.Unmarshal(data, &str); err == nil { + num, err := strconv.ParseInt(str, 10, 64) + if err != nil { + return fmt.Errorf("failed to convert string to number: %w", err) + } + fv.Number = cosmossdk_io_math.NewInt(num) + return nil + } + + return fmt.Errorf("invalid value: %s", string(data)) +}