BlueWallet/models/networkTransactionFees.ts

49 lines
1.4 KiB
TypeScript
Raw Normal View History

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',
});
export class NetworkTransactionFee {
2019-02-01 07:19:09 +01:00
static StorageKey = 'NetworkTransactionFee';
private fastestFee: number;
private mediumFee: number;
private slowFee: number;
constructor(fastestFee = 2, mediumFee = 1, slowFee = 1) {
this.fastestFee = fastestFee;
2020-05-18 16:45:31 +02:00
this.mediumFee = mediumFee;
this.slowFee = slowFee;
}
}
export default class NetworkTransactionFees {
static recommendedFees(): Promise<NetworkTransactionFee> {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async resolve => {
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');
}
const response = await BlueElectrum.estimateFees();
2019-02-24 04:27:29 +01:00
if (typeof response === 'object') {
const networkFee = new NetworkTransactionFee(response.fast + 5, response.medium + 2, response.slow);
resolve(networkFee);
} else {
const networkFee = new NetworkTransactionFee(2, 1, 1);
resolve(networkFee);
}
} catch (err) {
console.warn(err);
const networkFee = new NetworkTransactionFee(2, 1, 1);
resolve(networkFee);
}
});
}
}