From 7e0c07fdb575dad3d2e47fd467bc80b98aaba648 Mon Sep 17 00:00:00 2001 From: YashK Date: Tue, 3 Sep 2024 14:05:01 +0530 Subject: [PATCH] refactor: calculated gas limit using formula for reveal transaction --- utils/options.go | 35 ++++++++++++++++++++++++++++------- utils/options_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/utils/options.go b/utils/options.go index 21062e0c..a9239889 100644 --- a/utils/options.go +++ b/utils/options.go @@ -108,14 +108,24 @@ func (*GasStruct) GetGasLimit(transactionData types.TransactionOptions, txnOpts Value: txnOpts.Value, Data: inputData, } - gasLimit, err := ClientInterface.EstimateGasWithRetry(transactionData.Client, msg) - if err != nil { - log.Error("GetGasLimit: Error in getting gasLimit: ", err) - //If estimateGas throws an error for a transaction than gasLimit should be picked up from the config - log.Debugf("As there was an error from estimateGas, taking the gas limit value = %d from config", transactionData.Config.GasLimitOverride) - return transactionData.Config.GasLimitOverride, nil + var gasLimit uint64 + if transactionData.MethodName == "reveal" { + gasLimit, err = getGasLimitForReveal(transactionData.Client) + if err != nil { + log.Error("GetGasLimit: Error in getting gasLimit for reveal transaction: ", err) + return transactionData.Config.GasLimitOverride, err + } + log.Debug("Calculated gas limit for reveal: ", gasLimit) + } else { + gasLimit, err = ClientInterface.EstimateGasWithRetry(transactionData.Client, msg) + if err != nil { + log.Error("GetGasLimit: Error in getting gasLimit: ", err) + //If estimateGas throws an error for a transaction than gasLimit should be picked up from the config + log.Debugf("As there was an error from estimateGas, taking the gas limit value = %d from config", transactionData.Config.GasLimitOverride) + return transactionData.Config.GasLimitOverride, nil + } + log.Debug("Estimated Gas: ", gasLimit) } - log.Debug("Estimated Gas: ", gasLimit) return GasInterface.IncreaseGasLimitValue(transactionData.Client, gasLimit, transactionData.Config.GasLimitMultiplier) } @@ -138,3 +148,14 @@ func (*GasStruct) IncreaseGasLimitValue(client *ethclient.Client, gasLimit uint6 return gasLimit, nil } + +func getGasLimitForReveal(client *ethclient.Client) (uint64, error) { + toAssign, err := UtilsInterface.ToAssign(client) + if err != nil { + return 0, err + } + + // Apply the formula: gasLimit = 226864 + n * 85000 + gasLimit := 226864 + (uint64(toAssign) * 85000) + return gasLimit, nil +} diff --git a/utils/options_test.go b/utils/options_test.go index 54d7f907..71628fbd 100644 --- a/utils/options_test.go +++ b/utils/options_test.go @@ -345,6 +345,8 @@ func TestUtilsStruct_GetGasLimit(t *testing.T) { parseErr error inputData []byte packErr error + toAssign uint16 + toAssignErr error gasLimit uint64 gasLimitErr error increaseGasLimit uint64 @@ -423,6 +425,41 @@ func TestUtilsStruct_GetGasLimit(t *testing.T) { want: 5000000, wantErr: nil, }, + { + name: "Test 6: When the transaction is reveal and we get the calculated gasLimit for reveal txn", + args: args{ + transactionData: types.TransactionOptions{ + MethodName: "reveal", + Config: types.Configurations{ + GasLimitMultiplier: 2, + GasLimitOverride: 5000000, + }, + }, + parsedData: parsedData, + inputData: inputData, + toAssign: 3, + increaseGasLimit: 963728, + }, + want: 963728, + wantErr: nil, + }, + { + name: "Test 6: When the transaction is reveal and we get the error in getting toAssign during calculating gasLimit for reveal txn", + args: args{ + transactionData: types.TransactionOptions{ + MethodName: "reveal", + Config: types.Configurations{ + GasLimitMultiplier: 2, + GasLimitOverride: 5000000, + }, + }, + parsedData: parsedData, + inputData: inputData, + toAssignErr: errors.New("toAssign error"), + }, + want: 5000000, + wantErr: errors.New("toAssign error"), + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -445,6 +482,7 @@ func TestUtilsStruct_GetGasLimit(t *testing.T) { abiMock.On("Pack", parsedData, mock.AnythingOfType("string"), mock.Anything).Return(tt.args.inputData, tt.args.packErr) clientUtilsMock.On("EstimateGasWithRetry", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("ethereum.CallMsg")).Return(tt.args.gasLimit, tt.args.gasLimitErr) gasUtilsMock.On("IncreaseGasLimitValue", mock.AnythingOfType("*ethclient.Client"), mock.AnythingOfType("uint64"), mock.AnythingOfType("float32")).Return(tt.args.increaseGasLimit, tt.args.increaseGasLimitErr) + utilsMock.On("ToAssign", mock.Anything).Return(tt.args.toAssign, tt.args.toAssignErr) gasUtils := GasStruct{} got, err := gasUtils.GetGasLimit(tt.args.transactionData, txnOpts)