fix bug with extra precision

This commit is contained in:
Alexey 2020-10-20 18:11:07 +03:00
parent a05a1e62d4
commit ca24732a9b
2 changed files with 21 additions and 3 deletions

@ -1,6 +1,6 @@
{
"name": "gas-price-oracle",
"version": "0.2.1",
"version": "0.2.2",
"description": "Gas Price Oracle library for Ethereum dApps.",
"main": "lib/index.js",
"homepage": "https://github.com/peppersec/gas-price-oracle",

@ -41,7 +41,7 @@ export class GasPriceOracle {
standard: parseFloat(gas[standardPropertyName]) / denominator,
low: parseFloat(gas[lowPropertyName]) / denominator,
};
return gasPrices;
return this.normalize(gasPrices);
} else {
throw new Error(`Fetch gasPrice from ${name} oracle failed. Trying another one...`);
}
@ -108,7 +108,25 @@ export class GasPriceOracle {
const middle = Math.floor(allPrices.length / 2);
medianGasPrice[type] = isEven ? (allPrices[middle - 1] + allPrices[middle]) / 2.0 : allPrices[middle];
}
return medianGasPrice;
return this.normalize(medianGasPrice);
}
/**
* Normalizes GasPrice values to Gwei. No more than 9 decimals basically
* @param GasPrice _gas
*/
normalize(_gas: GasPrice): GasPrice {
const format = {
decimalSeparator: '.',
groupSeparator: '',
};
const decimals = 9;
const gas: GasPrice = { ..._gas };
for (const type of Object.keys(gas) as Array<keyof GasPrice>) {
gas[type] = Number(new BigNumber(gas[type]).toFormat(decimals, format));
}
return gas;
}
async fetchGasPricesOnChain(): Promise<number> {