Skip to content

Commit

Permalink
feat(btc): support all net’s suggest fee rate.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhangguiguang committed Apr 18, 2024
1 parent e22d52c commit 55a8365
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
15 changes: 8 additions & 7 deletions core/btc/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,19 @@ type FeeRate struct {
}

func (c *Chain) SuggestFeeRate() (rates *FeeRate, err error) {
defer base.CatchPanicAndMapToBasicError(&err)

url := ""
switch c.Chainnet {
case ChainBitcoin, ChainMainnet:
return SuggestFeeRate()
url = "https://mempool.space/api/v1/fees/recommended"
case ChainTestnet:
url = "https://mempool.space/testnet/api/v1/fees/recommended"
case ChainSignet:
url = "https://mempool.space/signet/api/v1/fees/recommended"
default:
return &FeeRate{1, 1, 1}, nil
}
}

func SuggestFeeRate() (rates *FeeRate, err error) {
defer base.CatchPanicAndMapToBasicError(&err)

url := "https://mempool.space/api/v1/fees/recommended"
response, err := httpUtil.Request(http.MethodGet, url, nil, nil)
if err != nil {
return
Expand Down
8 changes: 6 additions & 2 deletions core/btc/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,16 @@ func TestChain_SuggestFeeRate(t *testing.T) {
}
for _, chainnet := range chains {
t.Run(chainnet, func(t *testing.T) {
t.Logf("===== %v =====", chainnet)
chain, err := NewChainWithChainnet(chainnet)
require.Nil(t, err)
rate, err := chain.SuggestFeeRate()
require.Nil(t, err)
t.Logf("\n high: %v\n average: %v\n low: %v\n", rate.High, rate.Average, rate.Low)
t.Logf(`
===== %v =====
high: %v
average: %v
low: %v
`, chainnet, rate.High, rate.Average, rate.Low)
})
}
}
Expand Down

0 comments on commit 55a8365

Please sign in to comment.