From e66822b190b22e77801a710d69dd2edf33a5f3df Mon Sep 17 00:00:00 2001 From: Jacob Gadikian Date: Tue, 29 Oct 2024 14:57:18 +0700 Subject: [PATCH] automate gas --- Cargo.lock | 67 +++++++++++++++++----------------------- broadcast/transaction.go | 19 ++++++++++-- main.go | 16 ++++++++++ nodes.toml | 5 +-- types/types.go | 1 + 5 files changed, 65 insertions(+), 43 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 07a83f2..e8001ef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -188,9 +188,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "cc" -version = "1.1.24" +version = "1.1.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "812acba72f0a070b003d3697490d2b55b837230ae7c6c6497f05cc2ddbb8d938" +checksum = "c2e7962b54006dcfcc61cb72735f4d89bb97061dd6a7ed882ec6b8ee53714c6f" dependencies = [ "jobserver", "libc", @@ -246,7 +246,7 @@ checksum = "1b5658b1dc64e10b56ae7a449f678f96932a96f6cfad1769d608d1d1d656480a" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.85", ] [[package]] @@ -352,7 +352,7 @@ checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.85", ] [[package]] @@ -410,7 +410,7 @@ checksum = "cb7330aeadfbe296029522e6c40f315320aba36fc43a5b3632f3795348f3bd22" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.85", "unicode-xid", ] @@ -614,9 +614,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.159" +version = "0.2.161" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5" +checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" [[package]] name = "memchr" @@ -654,12 +654,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.20.1" +version = "1.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "82881c4be219ab5faaf2ad5e5e5ecdff8c66bd7402ca3160975c93b24961afd1" -dependencies = [ - "portable-atomic", -] +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" [[package]] name = "p256" @@ -685,12 +682,6 @@ version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" -[[package]] -name = "portable-atomic" -version = "1.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc9c68a3f6da06753e9335d63e27f6b9754dd1920d941135b7ea8224f141adb2" - [[package]] name = "ppv-lite86" version = "0.2.20" @@ -711,9 +702,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.86" +version = "1.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" dependencies = [ "unicode-ident", ] @@ -822,7 +813,7 @@ dependencies = [ "proc-macro2", "quote", "serde_derive_internals", - "syn 2.0.79", + "syn 2.0.85", ] [[package]] @@ -846,9 +837,9 @@ checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" [[package]] name = "serde" -version = "1.0.210" +version = "1.0.214" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" +checksum = "f55c3193aca71c12ad7890f1785d2b73e1b9f63a0bbc353c08ef26fe03fc56b5" dependencies = [ "serde_derive", ] @@ -864,13 +855,13 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.210" +version = "1.0.214" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" +checksum = "de523f781f095e28fa605cdce0f8307e451cc0fd14e2eb4cd2e98a355b147766" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.85", ] [[package]] @@ -881,14 +872,14 @@ checksum = "18d26a20a969b9e3fdf2fc2d9f21eda6c40e2de84c9408bb5d3b05d499aae711" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.85", ] [[package]] name = "serde_json" -version = "1.0.128" +version = "1.0.132" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8" +checksum = "d726bfaff4b320266d395898905d0eba0345aae23b54aee3a737e260fd46db03" dependencies = [ "itoa", "memchr", @@ -962,9 +953,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.79" +version = "2.0.85" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590" +checksum = "5023162dfcd14ef8f32034d8bcd4cc5ddc61ef7a247c024a33e24e1f24d21b56" dependencies = [ "proc-macro2", "quote", @@ -973,22 +964,22 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.64" +version = "1.0.65" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84" +checksum = "5d11abd9594d9b38965ef50805c5e469ca9cc6f197f883f717e0269a3057b3d5" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.64" +version = "1.0.65" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3" +checksum = "ae71770322cbd277e69d762a16c444af02aa0575ac0d174f0b9562d3b37f8602" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.85", ] [[package]] @@ -1039,7 +1030,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.85", ] [[package]] @@ -1059,7 +1050,7 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.79", + "syn 2.0.85", ] [[package]] diff --git a/broadcast/transaction.go b/broadcast/transaction.go index 89b12ea..dfdbc81 100644 --- a/broadcast/transaction.go +++ b/broadcast/transaction.go @@ -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( diff --git a/main.go b/main.go index af1048a..462e297 100644 --- a/main.go +++ b/main.go @@ -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) + 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) } diff --git a/nodes.toml b/nodes.toml index 0f12087..8b6f0cf 100644 --- a/nodes.toml +++ b/nodes.toml @@ -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] @@ -43,6 +43,7 @@ label = "statefilestore" [gas] low = 25 precision = 3 +# limit = 75000000 [nodes] rpc = ["http://127.0.0.1:26657"] diff --git a/types/types.go b/types/types.go index 1b00810..ad5253f 100644 --- a/types/types.go +++ b/types/types.go @@ -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 {