Skip to content

Commit

Permalink
fix: update fee on every wallet refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
limpbrains committed Nov 19, 2024
1 parent 940e4bc commit 7b05fc0
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions src/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ export class Wallet {
});
}
this.isRefreshing = true;
this.updateFeeEstimates();
try {
await this.setZeroIndexAddresses();
const r1 = await this.updateAddressIndexes();
Expand Down Expand Up @@ -2759,13 +2760,25 @@ export class Wallet {
public async getFeeEstimates(network = this._network): Promise<IOnchainFees> {
try {
if (network === EAvailableNetworks.bitcoinRegtest) {
return defaultFeesShape;
return { ...defaultFeesShape, timestamp: Date.now() };
}
const urlModifier = network === 'bitcoin' ? '' : 'testnet/';
const response = await fetch(
`https://mempool.space/${urlModifier}api/v1/fees/recommended`
);
const res: IGetFeeEstimatesResponse = await response.json();
// check the response for the expected properties
if (
!(
res.fastestFee > 0 &&
res.halfHourFee > 0 &&
res.hourFee > 0 &&
res.minimumFee > 0
)
) {
throw new Error('Unexpected response from mempool.space');
}

return {
fast: res.fastestFee,
normal: res.halfHourFee,
Expand All @@ -2791,9 +2804,19 @@ export class Wallet {
if (network !== EAvailableNetworks.bitcoinMainnet) {
return defaultFeesShape;
}
const url = 'https://blocktank.synonym.to/api/v2/info';
const url = 'https://api1.blocktank.to/api/info';
const response = await fetch(url);
const res: IBtInfo = await response.json();
// check the response for the expected properties
if (
!(
res?.onchain?.feeRates?.fast > 0 &&
res?.onchain?.feeRates?.mid > 0 &&
res?.onchain?.feeRates?.slow > 0
)
) {
throw new Error('Unexpected response from blocktank');
}
const { fast, mid, slow } = res.onchain.feeRates;
return {
fast,
Expand All @@ -2804,7 +2827,7 @@ export class Wallet {
};
} catch (e) {
console.log('Unable to fetch fee estimates.', e);
return defaultFeesShape;
return this.feeEstimates;
}
}

Expand Down Expand Up @@ -3698,21 +3721,18 @@ export class Wallet {
* Updates the fee estimates for the current network.
* @public
* @async
* @param {true} [forceUpdate] Ignores the timestamp if set true and forces the update
* @param {boolean} [forceUpdate] Ignores the timestamp if set true and forces the update
* @returns {Promise<Result<IOnchainFees>>}
*/
public async updateFeeEstimates(
forceUpdate = false
): Promise<Result<IOnchainFees>> {
const timestamp = this.feeEstimates.timestamp;
const difference = Math.floor((Date.now() - timestamp) / 1000);
if (!forceUpdate && difference < 1800) {
if (!forceUpdate && difference < 60) {
return ok(this.feeEstimates);
}
const feeEstimates = await this.getFeeEstimates();
if (!feeEstimates) {
return err('Unable to retrieve fee estimates.');
}
this.feeEstimates = feeEstimates;
await this.saveWalletData('feeEstimates', feeEstimates);
return ok(feeEstimates);
Expand Down

0 comments on commit 7b05fc0

Please sign in to comment.