Skip to content

Keychain SDK: estimate gas automatically #681

@Pitasi

Description

@Pitasi

Instead of hardcoding gas limits in go-client and keychain-sdk, it would nice to have an automatic gas estimation.

(note to contributors: keep in mind that go-client and keychain-sdk are consumer libraries, used by other developers, so we want the cleanest API possible).


I imagine a flag to turn on/off gas estimation, while keeping the ability to specify a maximum gas limit. I.e.:

if (config.estimateGasEnabled && config.gasLimit > 0) {
  gasLimit = min(gasEstimation, config.gasLimit)
} else if (config.estimateGasEnabled) {
  gasLimit = gasEstimation
} else {
  gasLimit = config.gasLimit
}

The estimation can be calculated like this (proof of concept):

import (
  "github.com/cosmos/cosmos-sdk/types/tx"
)

var gasAdjustment float64 = 1.1 // this should be configurable by users

func estimate(txBytes []byte) (float64, error) {
	txSvcClient := tx.NewServiceClient(grpcConn)
	simRes, err := txSvcClient.Simulate(context.Background(), &tx.SimulateRequest{
		TxBytes: txBytes,
	})
	if err != nil {
		return nil, 0, err
	}

	return gasAdjustment * float64(simRes.GasInfo.GasUsed), nil
}

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions