Skip to content

Commit

Permalink
feat: Allow decimal gas prices
Browse files Browse the repository at this point in the history
  • Loading branch information
red-0ne committed Jan 3, 2025
1 parent 2980c94 commit 5bdc54b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
17 changes: 14 additions & 3 deletions pkg/client/tx/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ type txClient struct {
txTimeoutPool txTimeoutPool

// gasPrices is the gas unit prices used for sending transactions.
gasPrices cosmostypes.Coins
gasPrices cosmostypes.DecCoins

// connRetryLimit is the number of times the underlying replay client
// should retry in the event that it encounters an error or its connection is interrupted.
Expand Down Expand Up @@ -253,8 +253,19 @@ func (txnClient *txClient) SignAndBroadcast(
// Coin multiplication prevents doing it using a zero value.
if gasLimit > 0 {
txBuilder.SetGasLimit(gasLimit)
feeAmount := txnClient.gasPrices.MulInt(math.NewIntFromUint64(gasLimit))
txBuilder.SetFeeAmount(feeAmount)

gasLimitDec := math.LegacyNewDec(int64(gasLimit))
feeAmountDec := txnClient.gasPrices.MulDec(gasLimitDec)

feeCoins, changeCoins := feeAmountDec.TruncateDecimal()
// Ensure that any decimal remainder is added to the corresponding coin as a
// whole number.
for i, coin := range feeCoins {
if !changeCoins[i].IsZero() {
feeCoins[i] = coin.AddAmount(math.OneInt())
}
}
txBuilder.SetFeeAmount(feeCoins)
}

txBuilder.SetTimeoutHeight(uint64(timeoutHeight))
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/tx/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func WithConnRetryLimit(limit int) client.TxClientOption {
}

// WithGasPrices sets the gas price to be used when constructing transactions.
func WithGasPrices(gasPrices cosmostypes.Coins) client.TxClientOption {
func WithGasPrices(gasPrices cosmostypes.DecCoins) client.TxClientOption {
return func(client client.TxClient) {
client.(*txClient).gasPrices = gasPrices
}
Expand Down
17 changes: 13 additions & 4 deletions pkg/deps/config/suppliers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/cosmos/gogoproto/grpc"
"github.com/spf13/cobra"

"github.com/pokt-network/poktroll/app/volatile"
"github.com/pokt-network/poktroll/pkg/client/block"
"github.com/pokt-network/poktroll/pkg/client/delegation"
"github.com/pokt-network/poktroll/pkg/client/events"
Expand Down Expand Up @@ -359,7 +360,7 @@ func NewSupplySupplierClientsFn(signingKeyNames []string) SupplierFn {
return nil, err
}

gasPrices, err := cosmostypes.ParseCoinsNormalized(gasPriceStr)
gasPrices, err := cosmostypes.ParseDecCoins(gasPriceStr)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -482,10 +483,18 @@ func newSupplyTxClientsFn(
ctx context.Context,
deps depinject.Config,
signingKeyName string,
gasPrices cosmostypes.Coins,
gasPrices cosmostypes.DecCoins,
) (depinject.Config, error) {
uPOKTFound, _ := gasPrices.Find("upokt")
if !uPOKTFound {
// Ensure that the gas prices include upokt
uPOKTDenomFound := false
for _, gasPrice := range gasPrices {
if gasPrice.Denom == volatile.DenomuPOKT {
uPOKTDenomFound = true
break
}
}

if !uPOKTDenomFound {
return nil, fmt.Errorf("gas prices must include upokt")
}

Expand Down

0 comments on commit 5bdc54b

Please sign in to comment.