Skip to content

Commit

Permalink
Round down fee in tinybars (#9973)
Browse files Browse the repository at this point in the history
- Round down when converting gas price to fee in tinybars

Signed-off-by: Xin Li <[email protected]>
  • Loading branch information
xin-hedera authored Dec 17, 2024
1 parent 9091326 commit 26878b7
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ public class RestSpecTest extends RestJavaIntegrationTest {
REST_BASE_PATH.resolve("blocks/{id}/hash-96.json"),
REST_BASE_PATH.resolve("network/exchangerate/no-params.json"),
REST_BASE_PATH.resolve("network/exchangerate/timestamp-upper-bound.json"),
REST_BASE_PATH.resolve("network/fees/no-params.json"),
REST_BASE_PATH.resolve("network/fees/order.json"),
// Disable the following test cases since it fails in PR #9973 due to the fact that the PR fixes bug in
// network fees endpoint however the test class runs the hedera-mirror-rest:latest image without the fix
// REST_BASE_PATH.resolve("network/fees/no-params.json"),
// REST_BASE_PATH.resolve("network/fees/order.json"),
REST_BASE_PATH.resolve("network/fees/timestamp-not-found.json"),
REST_BASE_PATH.resolve("network/stake/no-params.json"),
REST_BASE_PATH.resolve("topics/{id}/messages/all-params.json"),
Expand Down
4 changes: 2 additions & 2 deletions hedera-mirror-rest/__tests__/specs/network/fees/fallback.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@
"transaction_type": "ContractCall"
},
{
"gas": 67,
"gas": 66,
"transaction_type": "ContractCreate"
},
{
"gas": 64,
"gas": 63,
"transaction_type": "EthereumTransaction"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@
"transaction_type": "ContractCall"
},
{
"gas": 66,
"gas": 65,
"transaction_type": "ContractCreate"
},
{
"gas": 57,
"gas": 56,
"transaction_type": "EthereumTransaction"
}
],
Expand Down
8 changes: 4 additions & 4 deletions hedera-mirror-rest/__tests__/specs/network/fees/order.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
"transaction_type": "ContractCall"
},
{
"gas": 66,
"gas": 65,
"transaction_type": "ContractCreate"
},
{
"gas": 57,
"gas": 56,
"transaction_type": "EthereumTransaction"
}
],
Expand All @@ -65,11 +65,11 @@
"responseJson": {
"fees": [
{
"gas": 57,
"gas": 56,
"transaction_type": "EthereumTransaction"
},
{
"gas": 66,
"gas": 65,
"transaction_type": "ContractCreate"
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
"transaction_type": "ContractCall"
},
{
"gas": 66,
"gas": 65,
"transaction_type": "ContractCreate"
},
{
"gas": 57,
"gas": 56,
"transaction_type": "EthereumTransaction"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@
"transaction_type": "ContractCall"
},
{
"gas": 67,
"gas": 66,
"transaction_type": "ContractCreate"
},
{
"gas": 64,
"gas": 63,
"transaction_type": "EthereumTransaction"
}
],
Expand Down
34 changes: 27 additions & 7 deletions hedera-mirror-rest/__tests__/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1792,30 +1792,50 @@ describe('Utils addHexPrefix tests', () => {
});
});

describe('Utils convertGasPriceToTinyBars tests', () => {
const defaultGasPrice = 11566419;
describe('convertGasPriceToTinyBars', () => {
const defaultGasPrice = 852000;
const defaultHbars = 30000;
const defaultCents = 285576;
const defaultCents = 851000;
const specs = [
{
name: 'no args',
args: [],
expected: null,
},
{
name: 'has undefined arg',
args: [defaultGasPrice, defaultHbars, undefined],
name: 'zero cents',
args: [defaultGasPrice, defaultHbars, 0],
expected: null,
},
{
name: 'has undefined gasPrice',
args: [, defaultHbars, defaultCents],
expected: null,
},
{
name: 'has undefined hbars',
args: [defaultGasPrice, , defaultCents],
expected: null,
},
{
name: 'has undefined cents',
args: [defaultGasPrice, defaultHbars],
expected: null,
},
{
name: 'should return estimated tinybars',
args: [defaultGasPrice, defaultHbars, defaultCents],
expected: 1215,
expected: 30n,
},
{
name: 'should return estimated tinybars with rounding down',
args: [2 * defaultCents - 1, defaultHbars, defaultCents],
expected: 59n,
},
{
name: 'should return the minimum tinybars',
args: [1, defaultHbars, defaultCents],
expected: 1,
expected: 1n,
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ describe('FeeScheduleViewModel', () => {

const feesResult = {
EthereumTransaction: {
gas: 57,
gas: 56n,
transaction_type: 'EthereumTransaction',
},
ContractCreate: {
gas: 66,
gas: 65n,
transaction_type: 'ContractCreate',
},
ContractCall: {
gas: 49,
gas: 49n,
transaction_type: 'ContractCall',
},
};
Expand Down
2 changes: 1 addition & 1 deletion hedera-mirror-rest/model/feeSchedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {proto} from '@hashgraph/proto';
import {FileDecodeError} from '../errors';

class FeeSchedule {
static FEE_DIVISOR_FACTOR = 1000;
static FEE_DIVISOR_FACTOR = 1000n;

/**
* Parses fee schedule into object
Expand Down
25 changes: 13 additions & 12 deletions hedera-mirror-rest/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1622,22 +1622,23 @@ const conflictingPathParam = (req, paramName, possibleConflicts = []) => {
/**
* Converts gas price into tiny bars
*
* @param {number} gasPrice
* @param {number} hbarPerTinyCent
* @param {number} centsPerHbar
* @returns {number|null} `null` if the gasPrice cannot be converted. The minimum `number` returned is 1
* @param {number} gasPrice - gas price
* @param {number} hbars - equivalent hbars from exchange rate
* @param {number} cents - equivalent cents from exchange rate
* @returns {BigInt|null} `null` if the gasPrice cannot be converted. The minimum value returned is 1n
*/
const convertGasPriceToTinyBars = (gasPrice, hbarsPerTinyCent, centsPerHbar) => {
if ([gasPrice, hbarsPerTinyCent, centsPerHbar].some((n) => !_.isNumber(n))) {
const convertGasPriceToTinyBars = (gasPrice, hbars, cents) => {
if (!_.isNumber(gasPrice) || !_.isNumber(hbars) || !_.isNumber(cents)) {
return null;
}
const tinyCentsBN = math.bignumber(gasPrice / FeeSchedule.FEE_DIVISOR_FACTOR);
const hbarMultiplierBN = math.bignumber(hbarsPerTinyCent);
const centsDivisorBN = math.bignumber(centsPerHbar);
const hbarCentsBN = math.multiply(tinyCentsBN, hbarMultiplierBN);

const tinyBars = math.divide(hbarCentsBN, centsDivisorBN).toNumber();
return Math.round(Math.max(tinyBars, 1));
cents = BigInt(cents);
if (cents === 0n) {
return null;
}

const fee = (BigInt(gasPrice) * BigInt(hbars)) / (cents * FeeSchedule.FEE_DIVISOR_FACTOR);
return bigIntMax(fee, 1n);
};

const boundToOp = {
Expand Down

0 comments on commit 26878b7

Please sign in to comment.