2020-07-01 13:56:52 +02:00
|
|
|
const BlueElectrum = require('../blue_modules/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';
|
|
|
|
|
2023-09-28 21:15:40 +02:00
|
|
|
private fastestFee: number;
|
|
|
|
private mediumFee: number;
|
|
|
|
private slowFee: number;
|
|
|
|
|
2023-01-01 19:18:15 +01:00
|
|
|
constructor(fastestFee = 2, mediumFee = 1, slowFee = 1) {
|
2018-10-20 23:10:21 +02:00
|
|
|
this.fastestFee = fastestFee;
|
2020-05-18 16:45:31 +02:00
|
|
|
this.mediumFee = mediumFee;
|
|
|
|
this.slowFee = slowFee;
|
2018-10-20 23:10:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class NetworkTransactionFees {
|
2023-09-28 21:15:40 +02:00
|
|
|
static recommendedFees(): Promise<NetworkTransactionFee> {
|
2020-06-01 14:54:23 +02:00
|
|
|
// eslint-disable-next-line no-async-promise-executor
|
2020-10-05 19:53:47 +02:00
|
|
|
return new Promise(async resolve => {
|
2018-10-20 23:10:21 +02:00
|
|
|
try {
|
2021-08-24 07:00:57 +02:00
|
|
|
const isDisabled = await BlueElectrum.isDisabled();
|
|
|
|
if (isDisabled) {
|
|
|
|
throw new Error('Electrum is disabled. Dont attempt to fetch fees');
|
|
|
|
}
|
2020-06-01 14:54:23 +02:00
|
|
|
const response = await BlueElectrum.estimateFees();
|
2019-02-24 04:27:29 +01:00
|
|
|
if (typeof response === 'object') {
|
2023-09-28 21:13:06 +02:00
|
|
|
const networkFee = new NetworkTransactionFee(response.fast + 5, response.medium + 2, response.slow);
|
2018-10-20 23:10:21 +02:00
|
|
|
resolve(networkFee);
|
|
|
|
} else {
|
2023-01-01 19:18:15 +01:00
|
|
|
const networkFee = new NetworkTransactionFee(2, 1, 1);
|
2020-10-05 19:53:47 +02:00
|
|
|
resolve(networkFee);
|
2018-10-20 23:10:21 +02:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.warn(err);
|
2023-01-01 19:18:15 +01:00
|
|
|
const networkFee = new NetworkTransactionFee(2, 1, 1);
|
2020-10-05 19:53:47 +02:00
|
|
|
resolve(networkFee);
|
2018-10-20 23:10:21 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|