Skip to content

Commit

Permalink
read gas from config file
Browse files Browse the repository at this point in the history
  • Loading branch information
faddat committed May 29, 2024
1 parent 15af9c5 commit a735465
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func sendIBCTransferViaRPC(config Config, rpcEndpoint string, chainID string, se
txBuilder.SetGasLimit(gasLimit)

// Calculate fee based on gas limit and a fixed gas price
gasPrice := sdk.NewDecCoinFromDec(config.Denom, sdk.NewDecWithPrec(51, 5)) // 0.00051 token per gas unit
gasPrice := getGasPrice(config.Gas.Low, config.Denom)
feeAmount := gasPrice.Amount.MulInt64(int64(gasLimit)).RoundInt()
feecoin := sdk.NewCoin(config.Denom, feeAmount)
txBuilder.SetFeeAmount(sdk.NewCoins(feecoin))
Expand Down
6 changes: 3 additions & 3 deletions configurations/stargaze/nodes.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ rand_max = 400000

[gas]
zero = 0
low = 0.001
medium = 0.25
high = 0.04
low = 1
medium = 2
high = 3


[nodes]
Expand Down
21 changes: 21 additions & 0 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net"
Expand All @@ -11,6 +12,7 @@ import (
"time"

"github.com/BurntSushi/toml"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func getInitialSequence(address string, config Config) (int64, int64) {
Expand Down Expand Up @@ -94,3 +96,22 @@ func loadNodes() []string {
}
return config.Nodes.RPC
}

func getGasPrice(value interface{}, denom string) sdk.DecCoin {
var decValue sdk.Dec
switch v := value.(type) {
case int64:
decValue = sdk.NewDec(v)
case float64:
var err error
decValue, err = sdk.NewDecFromStr(fmt.Sprintf("%f", v))
if err != nil {
log.Fatalf("Failed to convert float64 to Dec: %v", err)
}
default:
log.Fatalf("Invalid type for gas price: %T", v)
}
// Assuming the gas price needs to be scaled to a specific precision
decCoin := sdk.NewDecCoinFromDec(denom, decValue.Quo(sdk.NewDec(10000))) // Adjust the scaling factor as needed
return decCoin
}
8 changes: 4 additions & 4 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ type Config struct {
BaseGas int `toml:"base_gas"`
Denom string `toml:"denom"`
Gas struct {
Zero float64 `toml:"zero"`
Low float64 `toml:"low"`
Medium float64 `toml:"medium"`
High float64 `toml:"high"`
Zero interface{} `toml:"zero"`
Low interface{} `toml:"low"`
Medium interface{} `toml:"medium"`
High interface{} `toml:"high"`
} `toml:"gas"`
Nodes struct {
RPC []string `toml:"rpc"`
Expand Down

0 comments on commit a735465

Please sign in to comment.