2019-02-24 04:27:29 +01:00
|
|
|
import { BitcoinUnit } from './bitcoinUnits';
|
|
|
|
import BigNumber from 'bignumber.js';
|
|
|
|
let loc = require('../loc');
|
2018-10-20 23:10:21 +02:00
|
|
|
|
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',
|
|
|
|
});
|
|
|
|
|
2018-10-20 23:10:21 +02:00
|
|
|
export class NetworkTransactionFee {
|
2019-02-01 07:19:09 +01:00
|
|
|
static StorageKey = 'NetworkTransactionFee';
|
|
|
|
|
2018-12-11 23:52:46 +01:00
|
|
|
constructor(fastestFee = 1, halfHourFee = 1, hourFee = 1) {
|
2018-10-20 23:10:21 +02:00
|
|
|
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);
|
2018-10-20 23:10:21 +02:00
|
|
|
resolve(networkFee);
|
|
|
|
} else {
|
2018-12-11 23:52:46 +01:00
|
|
|
const networkFee = new NetworkTransactionFee(1, 1, 1);
|
|
|
|
reject(networkFee);
|
2018-10-20 23:10:21 +02:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.warn(err);
|
|
|
|
const networkFee = new NetworkTransactionFee(1, 1, 1);
|
|
|
|
reject(networkFee);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|