Skip to content

Commit 7dde54d

Browse files
authored
Merge pull request #191 from openbitdev/koni/dev/issue-189
Optimize fee rate level and bitcoin balance
2 parents 5be8779 + c160d19 commit 7dde54d

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

packages/extension-base/src/services/chain-service/handler/bitcoin/strategy/BlockStream/index.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import { SWError } from '@subwallet/extension-base/background/errors/SWError';
55
import { _BEAR_TOKEN } from '@subwallet/extension-base/services/chain-service/constants';
6-
import { BitcoinAddressSummaryInfo, BlockStreamBlock, BlockStreamFeeEstimates, BlockStreamTransactionDetail, BlockStreamTransactionStatus, Brc20BalanceItem, Inscription, InscriptionFetchedData, RunesInfoByAddress, RunesInfoByAddressFetchedData, RuneTxs, RuneTxsResponse, UpdateOpenBitUtxo } from '@subwallet/extension-base/services/chain-service/handler/bitcoin/strategy/BlockStream/types';
6+
import { BitcoinAddressSummaryInfo, BlockStreamBlock, BlockStreamFeeEstimates, BlockStreamTransactionDetail, BlockStreamTransactionStatus, Brc20BalanceItem, Inscription, InscriptionFetchedData, RecommendedFeeEstimates, RunesInfoByAddress, RunesInfoByAddressFetchedData, RuneTxs, RuneTxsResponse, UpdateOpenBitUtxo } from '@subwallet/extension-base/services/chain-service/handler/bitcoin/strategy/BlockStream/types';
77
import { BitcoinApiStrategy, BitcoinTransactionEventMap } from '@subwallet/extension-base/services/chain-service/handler/bitcoin/strategy/types';
88
import { OBResponse } from '@subwallet/extension-base/services/chain-service/types';
99
import { HiroService } from '@subwallet/extension-base/services/hiro-service';
@@ -150,6 +150,38 @@ export class BlockStreamRequestStrategy extends BaseApiRequestStrategy implement
150150
}, 0);
151151
}
152152

153+
getRecommendedFeeRate (): Promise<BitcoinFeeInfo> {
154+
return this.addRequest<BitcoinFeeInfo>(async (): Promise<BitcoinFeeInfo> => {
155+
const _rs = await getRequest(this.getUrl('fee-estimates/recommended'), undefined, this.headers);
156+
const rs = await _rs.json() as OBResponse<RecommendedFeeEstimates>;
157+
158+
if (rs.status_code !== 200) {
159+
throw new SWError('BlockStreamRequestStrategy.getRecommendedFeeRate', rs.message);
160+
}
161+
162+
const result = rs.result;
163+
164+
const convertTimeMilisec = {
165+
fastestFee: 10 * 60000,
166+
halfHourFee: 30 * 60000,
167+
hourFee: 60 * 60000
168+
};
169+
170+
const convertFee = (fee: number) => parseInt(new BigN(fee).toFixed());
171+
172+
return {
173+
type: 'bitcoin',
174+
busyNetwork: false,
175+
options: {
176+
slow: { feeRate: convertFee(result.hourFee), time: convertTimeMilisec.hourFee },
177+
average: { feeRate: convertFee(result.halfHourFee), time: convertTimeMilisec.halfHourFee },
178+
fast: { feeRate: convertFee(result.fastestFee), time: convertTimeMilisec.fastestFee },
179+
default: 'slow'
180+
}
181+
};
182+
}, 0);
183+
}
184+
153185
getUtxos (address: string): Promise<UtxoResponseItem[]> {
154186
return this.addRequest<UtxoResponseItem[]>(async (): Promise<UtxoResponseItem[]> => {
155187
const _rs = await getRequest(this.getUrl(`address/${address}/utxo`), undefined, this.headers);

packages/extension-base/src/services/chain-service/handler/bitcoin/strategy/BlockStream/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,14 @@ export interface BlockStreamFeeEstimates {
198198
8: number;
199199
}
200200

201+
export interface RecommendedFeeEstimates {
202+
fastestFee: number,
203+
halfHourFee: number,
204+
hourFee: number,
205+
economyFee: number,
206+
minimumFee: number
207+
}
208+
201209
export interface BlockStreamTransactionVectorOutput {
202210
scriptpubkey: string;
203211
scriptpubkey_asm: string;

packages/extension-base/src/services/chain-service/handler/bitcoin/strategy/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export interface BitcoinApiStrategy extends Omit<ApiRequestStrategy, 'addRequest
1818
getTransactionStatus (txHash: string): Promise<BitcoinTransactionStatus>;
1919
getTransactionDetail (txHash: string): Promise<BitcoinTx>;
2020
getFeeRate (): Promise<BitcoinFeeInfo>;
21+
getRecommendedFeeRate (): Promise<BitcoinFeeInfo>;
2122
getUtxos (address: string): Promise<UtxoResponseItem[]>;
2223
getTxHex (txHash: string): Promise<string>;
2324
sendRawTransaction (rawTransaction: string): EventEmitter<BitcoinTransactionEventMap>;

packages/extension-base/src/services/fee-service/service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export default class FeeService {
142142
} else if (type === 'bitcoin') {
143143
const api = this.state.getBitcoinApi(chain);
144144

145-
api.api.getFeeRate()
145+
api.api.getRecommendedFeeRate()
146146
.then((info) => {
147147
observer.next(info);
148148
})

0 commit comments

Comments
 (0)