Skip to content

Commit

Permalink
Addition of FlexibleCosmosIntAmount to support int and str
Browse files Browse the repository at this point in the history
  • Loading branch information
xmariachi committed Jan 16, 2025
1 parent 0e934fd commit 2e919bb
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 26 deletions.
37 changes: 18 additions & 19 deletions lib/domain_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 3 additions & 4 deletions lib/repo_tx_registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.")
Expand Down
6 changes: 3 additions & 3 deletions lib/repo_tx_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
43 changes: 43 additions & 0 deletions lib/type_allora.go
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit 2e919bb

Please sign in to comment.