Skip to content

Commit

Permalink
intro new variables for gas/validator tips
Browse files Browse the repository at this point in the history
  • Loading branch information
quasisamurai committed Jun 13, 2024
1 parent a3b8250 commit 76b2d18
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ RELAYER_NEUTRON_CHAIN_TIMEOUT=10s
RELAYER_NEUTRON_CHAIN_GAS_PRICES=0.5untrn
RELAYER_NEUTRON_CHAIN_GAS_LIMIT=10000000
RELAYER_NEUTRON_CHAIN_GAS_ADJUSTMENT=2.0
RELAYER_NEUTRON_CHAIN_DEONM=untrn
RELAYER_NEUTRON_CHAIN_DENOM=untrn
RELAYER_NEUTRON_CHAIN_MAX_GAS=1000
RELAYER_NEUTRON_CHAIN_GAS_MULTIPLIER=1.1
RELAYER_NEUTRON_CHAIN_CONNECTION_ID=connection-0
RELAYER_NEUTRON_CHAIN_DEBUG=true
RELAYER_NEUTRON_CHAIN_KEYRING_BACKEND=test
Expand Down
3 changes: 3 additions & 0 deletions .env.example.dev
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ RELAYER_NEUTRON_CHAIN_HOME_DIR=../neutron/data/test-1
RELAYER_NEUTRON_CHAIN_SIGN_KEY_NAME=demowallet3
RELAYER_NEUTRON_CHAIN_TIMEOUT=1000s
RELAYER_NEUTRON_CHAIN_GAS_ADJUSTMENT=2.0
RELAYER_NEUTRON_CHAIN_DENOM=untrn
RELAYER_NEUTRON_CHAIN_MAX_GAS=1000
RELAYER_NEUTRON_CHAIN_GAS_MULTIPLIER=1.1
RELAYER_NEUTRON_CHAIN_TX_BROADCAST_TYPE=BroadcastTxCommit
RELAYER_NEUTRON_CHAIN_CONNECTION_ID=connection-0
RELAYER_NEUTRON_CHAIN_CLIENT_ID=07-tendermint-0
Expand Down
4 changes: 3 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ type NeutronChainConfig struct {
KeyringBackend string `required:"true" split_words:"true"`
OutputFormat string `split_words:"true" default:"json"`
SignModeStr string `split_words:"true" default:"direct"`
Denom string `split_words:"true" default:"denom"`
Denom string `required:"true" split_words:"true"`
MaxGas float64 `required:"true" split_words:"true"`
GasMultiplier float64 `required:"true" split_words:"true"`
}

type TargetChainConfig struct {
Expand Down
55 changes: 42 additions & 13 deletions internal/submit/tx_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/hex"
"fmt"
"strconv"
"strings"
"sync"

Expand Down Expand Up @@ -48,6 +49,8 @@ type TxSender struct {
gasLimit uint64
logger *zap.Logger
denom string
maxGas float64
gasMultiplier float64
}

func TestKeybase(chainID string, keyringRootDir string, cdc codec.Codec) (keyring.Keyring, error) {
Expand Down Expand Up @@ -78,17 +81,19 @@ func NewTxSender(
WithGasPrices(cfg.GasPrices)

txs := &TxSender{
lock: sync.Mutex{},
keybase: keybase,
txConfig: txConfig,
baseTxf: baseTxf,
rpcClient: rpcClient,
chainID: neutronChainID,
signKeyName: cfg.SignKeyName,
gasPrices: cfg.GasPrices,
gasLimit: cfg.GasLimit,
logger: logger,
denom: cfg.Denom,
lock: sync.Mutex{},
keybase: keybase,
txConfig: txConfig,
baseTxf: baseTxf,
rpcClient: rpcClient,
chainID: neutronChainID,
signKeyName: cfg.SignKeyName,
gasPrices: cfg.GasPrices,
gasLimit: cfg.GasLimit,
logger: logger,
denom: cfg.Denom,
maxGas: cfg.MaxGas,
gasMultiplier: cfg.GasMultiplier,
}
err := txs.refreshAccountInfo(ctx)
if err != nil {
Expand Down Expand Up @@ -252,14 +257,38 @@ func (txs *TxSender) queryDynamicPrice(ctx context.Context) (string, error) {
return gasPrice, nil
}

// getGasPrice tries to query dynamic price for denom provided in config. if query fails for some reason,
func (txs *TxSender) multiplyGas(gas string) (string, error) {
floatGas, err := strconv.ParseFloat(gas, 64)
if err != nil {
return "", err
}

multipliedGas := txs.gasMultiplier * floatGas
if multipliedGas > txs.maxGas {
multipliedGas = txs.maxGas
}

return strconv.FormatFloat(multipliedGas, 'g', 4, 64), nil
}

// getGasPrice tries to query dynamic price for denom provided in config.
// If successful:
// 1) multiply gas by gasMultiplier (tip validators)
// 2) if result is bigger than maxGas -> return max gas
// if query fails for some reason:
// func returns default gas price[s] from config as well
func (txs *TxSender) getGasPrice(ctx context.Context) (string, error) {
gasPrice, err := txs.queryDynamicPrice(ctx)
if err != nil {
return txs.gasPrices, err
}
return gasPrice, nil

multipliedGas, err := txs.multiplyGas(gasPrice)
if err != nil {
return "", err
}

return multipliedGas, nil
}

func (txs *TxSender) signAndBuildTxBz(ctx context.Context, txf tx.Factory, msgs []sdk.Msg) ([]byte, error) {
Expand Down

0 comments on commit 76b2d18

Please sign in to comment.