-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a tx gasPrice Buffer to reduce INSUFFICIENT_TX_FEE cases (#…
…2692) * chore: add buffer Signed-off-by: nikolay <n.atanasow94@gmail.com> * chore: edit tests in order to fix code duplication warning Signed-off-by: nikolay <n.atanasow94@gmail.com> * chore: move the calculations to formatter.ts Signed-off-by: nikolay <n.atanasow94@gmail.com> * chore: fix readme Signed-off-by: nikolay <n.atanasow94@gmail.com> * chore: add utils class Signed-off-by: nikolay <n.atanasow94@gmail.com> * chore: fix sonarcloud issue Signed-off-by: nikolay <n.atanasow94@gmail.com> --------- Signed-off-by: nikolay <n.atanasow94@gmail.com>
- Loading branch information
Showing
7 changed files
with
129 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/*- | ||
* | ||
* Hedera JSON RPC Relay | ||
* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
import constants from './lib/constants'; | ||
|
||
export class Utils { | ||
public static readonly addPercentageBufferToGasPrice = (gasPrice: number): number => { | ||
// converting to tinybar and afterward to weibar again is needed | ||
// in order to handle the possibility of an invalid floating number being calculated as a gas price | ||
// e.g. | ||
// current gas price = 126 | ||
// buffer = 10% | ||
// buffered gas price = 126 + 12.6 = 138.6 <--- invalid tinybars | ||
gasPrice += | ||
Math.round( | ||
(gasPrice / constants.TINYBAR_TO_WEIBAR_COEF) * (Number(process.env.GAS_PRICE_PERCENTAGE_BUFFER || 0) / 100), | ||
) * constants.TINYBAR_TO_WEIBAR_COEF; | ||
|
||
return gasPrice; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/*- | ||
* | ||
* Hedera JSON RPC Relay | ||
* | ||
* Copyright (C) 2024 Hedera Hashgraph, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
import { expect } from 'chai'; | ||
import { Utils } from '../../src/utils'; | ||
import constants from '../../src/lib/constants'; | ||
|
||
describe('Utils', () => { | ||
describe('addPercentageBufferToGasPrice', () => { | ||
afterEach(() => { | ||
process.env.GAS_PRICE_PERCENTAGE_BUFFER = '0'; | ||
}); | ||
|
||
const TW_COEF = constants.TINYBAR_TO_WEIBAR_COEF; | ||
const TEST_CASES = [ | ||
{ testName: 'zero input', buffer: '0', input: 0, output: 0 }, | ||
{ testName: 'buffer 0%', buffer: '0', input: 10 * TW_COEF, output: 10 * TW_COEF }, | ||
{ testName: 'buffer 7%', buffer: '7', input: 140 * TW_COEF, output: 150 * TW_COEF }, | ||
{ testName: 'buffer 10%', buffer: '10', input: 126 * TW_COEF, output: 139 * TW_COEF }, | ||
{ testName: 'buffer 12.25%', buffer: '12.25', input: 56 * TW_COEF, output: 63 * TW_COEF }, | ||
{ testName: 'negative buffer -6%', buffer: '-6', input: 100 * TW_COEF, output: 94 * TW_COEF }, | ||
{ testName: 'negative buffer -12.58%', buffer: '-12.58', input: 136 * TW_COEF, output: 119 * TW_COEF }, | ||
]; | ||
for (let i in TEST_CASES) { | ||
it(TEST_CASES[i].testName, () => { | ||
process.env.GAS_PRICE_PERCENTAGE_BUFFER = TEST_CASES[i].buffer; | ||
expect(Utils.addPercentageBufferToGasPrice(TEST_CASES[i].input)).to.equal(TEST_CASES[i].output); | ||
}); | ||
} | ||
}); | ||
}); |