BlueWallet/models/networkTransactionFees.js

65 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-02-24 04:27:29 +01:00
import { BitcoinUnit } from './bitcoinUnits';
import BigNumber from 'bignumber.js';
let loc = require('../loc');
2019-02-24 04:27:29 +01:00
const BlueElectrum = require('../BlueElectrum');
2019-08-27 06:05:27 +02:00
export const NetworkTransactionFeeType = Object.freeze({
FAST: 'Fast',
MEDIUM: 'MEDIUM',
SLOW: 'SLOW',
CUSTOM: 'CUSTOM',
});
export class NetworkTransactionFee {
2019-02-01 07:19:09 +01:00
static StorageKey = 'NetworkTransactionFee';
constructor(fastestFee = 1, halfHourFee = 1, hourFee = 1) {
this.fastestFee = fastestFee;
this.halfHourFee = halfHourFee;
this.hourFee = hourFee;
}
}
export default class NetworkTransactionFees {
static recommendedFees() {
return new Promise(async (resolve, reject) => {
try {
2019-02-24 04:27:29 +01:00
let response = await BlueElectrum.estimateFees();
if (typeof response === 'object') {
2019-02-24 04:28:20 +01:00
const fast = loc.formatBalanceWithoutSuffix(
new BigNumber(response.fast)
.multipliedBy(100000)
.toNumber()
.toFixed(0),
BitcoinUnit.SATS,
);
const medium = loc.formatBalanceWithoutSuffix(
new BigNumber(response.medium)
.multipliedBy(100000)
.toNumber()
.toFixed(0),
BitcoinUnit.SATS,
);
const slow = loc.formatBalanceWithoutSuffix(
new BigNumber(response.slow)
.multipliedBy(100000)
.toNumber()
.toFixed(0),
BitcoinUnit.SATS,
);
const networkFee = new NetworkTransactionFee(fast, medium, slow);
resolve(networkFee);
} else {
const networkFee = new NetworkTransactionFee(1, 1, 1);
reject(networkFee);
}
} catch (err) {
console.warn(err);
const networkFee = new NetworkTransactionFee(1, 1, 1);
reject(networkFee);
}
});
}
}