Skip to content

Commit

Permalink
automate gas
Browse files Browse the repository at this point in the history
  • Loading branch information
faddat committed Oct 29, 2024
1 parent 6b838dc commit e66822b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 43 deletions.
67 changes: 29 additions & 38 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 16 additions & 3 deletions broadcast/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,23 @@ func BuildAndSignTransaction(
return nil, err
}

// Estimate gas limit
// Estimate gas limit with a buffer
txSize := len(msg.String())
gasLimit := uint64(int64(txSize)*txParams.Config.GasPerByte + txParams.Config.BaseGas)
txBuilder.SetGasLimit(gasLimit)
baseGas := txParams.Config.BaseGas
gasPerByte := txParams.Config.GasPerByte

// Calculate estimated gas
estimatedGas := uint64(int64(txSize)*gasPerByte + baseGas)

// Add a buffer (e.g., 20%)
buffer := uint64(float64(estimatedGas) * 0.2)
gasLimit := estimatedGas + buffer

if txParams.Config.Gas.Limit > 0 {
txBuilder.SetGasLimit(uint64(txParams.Config.Gas.Limit))
} else {
txBuilder.SetGasLimit(gasLimit)
}

// Calculate fee
gasPrice := sdk.NewDecCoinFromDec(
Expand Down
16 changes: 16 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,22 @@ func TransferFunds(sender types.Account, receiverAddress string, amount sdkmath.
continue
}

// Assuming you are inside the loop where you handle the transaction response
if resp.Code == 41 {
fmt.Printf("Transaction failed with code %d: %s\n", resp.Code, resp.RawLog)
maxBlockGas := 75000000
newGasLimit := maxBlockGas - 1000000
txParams.Config.Gas.Limit = int64(newGasLimit)
fmt.Printf("Reducing gas limit to %d and retrying...\n", newGasLimit)

// Retry sending the transaction
resp, _, err = broadcast.SendTransactionViaGRPC(ctx, txParams, sequence, grpcClient)

Check failure on line 384 in main.go

View workflow job for this annotation

GitHub Actions / lint

SA4006: this value of `resp` is never used (staticcheck)
if err != nil {
return fmt.Errorf("failed to send transaction: %v", err)
}
continue
}

return fmt.Errorf("transaction failed with code %d: %s", resp.Code, resp.RawLog)
}

Expand Down
5 changes: 3 additions & 2 deletions nodes.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ rand_max = 30000
revision_number = 4
timeout_height = 21720608
slip44 = 118
positions = 50 # Number of positions to use from the seed phrase
positions = 50 # Number of positions to use from the seed phrase

broadcast_mode = "grpc" # or "rpc"


msg_type = "bank_send"
msg_type = "store_code"

[msg_params]

Expand All @@ -43,6 +43,7 @@ label = "statefilestore"
[gas]
low = 25
precision = 3
# limit = 75000000

[nodes]
rpc = ["http://127.0.0.1:26657"]
Expand Down
1 change: 1 addition & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ type GasConfig struct {
Medium int64 `toml:"medium"`
High int64 `toml:"high"`
Precision int64 `toml:"precision"`
Limit int64 `toml:"limit"` // to be set entirely by software
}

type NodesConfig struct {
Expand Down

0 comments on commit e66822b

Please sign in to comment.