Skip to content

Commit

Permalink
refactor: calculated gas limit using formula for reveal transaction (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Yashk767 authored Sep 3, 2024
1 parent 7f77a9f commit 92ba408
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
35 changes: 28 additions & 7 deletions utils/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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
}
38 changes: 38 additions & 0 deletions utils/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand Down

0 comments on commit 92ba408

Please sign in to comment.