Skip to content

Commit

Permalink
Merge pull request #38 from InjectiveLabs/gas_price_estimate
Browse files Browse the repository at this point in the history
 Estimate the gas price
  • Loading branch information
albertchon authored Jul 2, 2021
2 parents 7c8f733 + e1654e7 commit c94e9af
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ PEGGO_ETH_FROM=
PEGGO_ETH_PASSPHRASE=
PEGGO_ETH_PK=
PEGGO_ETH_USE_LEDGER=false
PEGGO_ETH_GAS_PRICE_ADJUSTMENT=1.3

PEGGO_RELAY_VALSETS=true
PEGGO_RELAY_BATCHES=true
Expand Down
8 changes: 8 additions & 0 deletions cmd/peggo/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func initEthereumOptions(
cmd *cli.Cmd,
ethChainID **int,
ethNodeRPC **string,
ethGasPriceAdjustment **float64,
) {
*ethChainID = cmd.Int(cli.IntOpt{
Name: "eth-chain-id",
Expand All @@ -155,6 +156,13 @@ func initEthereumOptions(
EnvVar: "PEGGO_ETH_RPC",
Value: "http://localhost:1317",
})

*ethGasPriceAdjustment = cmd.Float64(cli.Float64Opt{
Name: "eth_gas_price_adjustment",
Desc: "gas price adjustment for Ethereum transactions",
EnvVar: "PEGGO_ETH_GAS_PRICE_ADJUSTMENT",
Value: float64(1.3),
})
}

func initEthereumKeyOptions(
Expand Down
8 changes: 5 additions & 3 deletions cmd/peggo/orchestrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ func orchestratorCmd(cmd *cli.Cmd) {
cosmosUseLedger *bool

// Ethereum params
ethChainID *int
ethNodeRPC *string
ethChainID *int
ethNodeRPC *string
ethGasPriceAdjustment *float64

// Ethereum Key Management
ethKeystoreDir *string
Expand Down Expand Up @@ -101,6 +102,7 @@ func orchestratorCmd(cmd *cli.Cmd) {
cmd,
&ethChainID,
&ethNodeRPC,
&ethGasPriceAdjustment,
)

initEthereumKeyOptions(
Expand Down Expand Up @@ -238,7 +240,7 @@ func orchestratorCmd(cmd *cli.Cmd) {
ethProvider := provider.NewEVMProvider(evmRPC)
log.Infoln("Connected to Ethereum RPC at", *ethNodeRPC)

ethCommitter, err := committer.NewEthCommitter(ethKeyFromAddress, signerFn, ethProvider)
ethCommitter, err := committer.NewEthCommitter(ethKeyFromAddress, *ethGasPriceAdjustment, signerFn, ethProvider)
orShutdown(err)

peggyContract, err := peggy.NewPeggyContract(ethCommitter, peggyAddress)
Expand Down
23 changes: 21 additions & 2 deletions orchestrator/ethereum/committer/eth_committer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
// can be used to submit txns into Ethereum, Matic, and other EVM-compatible networks.
func NewEthCommitter(
fromAddress common.Address,
ethGasPriceAdjustment float64,
fromSigner bind.SignerFn,
evmProvider provider.EVMProviderWithRet,
committerOpts ...EVMCommitterOption,
Expand All @@ -30,6 +31,8 @@ func NewEthCommitter(
"module": "eth_committer",
},

ethGasPriceAdjustment: ethGasPriceAdjustment,

fromAddress: fromAddress,
fromSigner: fromSigner,
evmProvider: evmProvider,
Expand All @@ -54,8 +57,9 @@ type ethCommitter struct {
fromAddress common.Address
fromSigner bind.SignerFn

evmProvider provider.EVMProviderWithRet
nonceCache util.NonceCache
ethGasPriceAdjustment float64
evmProvider provider.EVMProviderWithRet
nonceCache util.NonceCache

svcTags metrics.Tags
}
Expand Down Expand Up @@ -86,6 +90,21 @@ func (e *ethCommitter) SendTx(
Context: ctx, // with RPC timeout
}

// Figure out the gas price values
suggestedGasPrice, err := e.evmProvider.SuggestGasPrice(opts.Context)
if err != nil {
return common.Hash{}, errors.Errorf("failed to suggest gas price: %v", err)
}

// Suggested gas price is not accurate. Increment by multiplying with gasprice adjustment factor
incrementedPrice := big.NewFloat(0).Mul(new(big.Float).SetInt(suggestedGasPrice), big.NewFloat(e.ethGasPriceAdjustment))

// set gasprice to incremented gas price.
gasPrice := new(big.Int)
incrementedPrice.Int(gasPrice)

opts.GasPrice = gasPrice

resyncNonces := func(from common.Address) {
e.nonceCache.Sync(from, func() (uint64, error) {
nonce, err := e.evmProvider.PendingNonceAt(context.TODO(), from)
Expand Down

0 comments on commit c94e9af

Please sign in to comment.