From cd85d7cf5c3660dac465b1fcc540c02cae235c8e Mon Sep 17 00:00:00 2001 From: GG <863867759@qq.com> Date: Wed, 12 Feb 2025 22:18:23 +0800 Subject: [PATCH] update: unisat brc20 v5 api. --- core/btc/brc20_inscription.go | 10 +++++----- core/btc/brc20_inscription_test.go | 2 +- core/btc/brc20_token.go | 8 ++++---- core/btc/brc20_token_test.go | 2 +- core/btc/brc20_types.go | 18 +++++++++++++----- core/btc/brc20_util.go | 10 +++++----- core/btc/chainnet.go | 4 ++-- 7 files changed, 31 insertions(+), 23 deletions(-) diff --git a/core/btc/brc20_inscription.go b/core/btc/brc20_inscription.go index 6edff26..2403118 100644 --- a/core/btc/brc20_inscription.go +++ b/core/btc/brc20_inscription.go @@ -30,13 +30,13 @@ func (c *Chain) FetchBrc20Inscription(owner string, cursor string, pageSize int) } header := unisatRequestHeader(owner) - url := fmt.Sprintf("%v/wallet-v4/address/inscriptions?address=%v&cursor=%v&size=%v", host, owner, offset, pageSize) + url := fmt.Sprintf("%v/v5/ordinals/inscriptions?address=%v&cursor=%v&size=%v", host, owner, offset, pageSize) resp, err := httpUtil.Request(http.MethodGet, url, header, nil) if err != nil { return } var rawPage rawBrc20InscriptionPage - if err = decodeUnisatResponseV4(*resp, &rawPage); err != nil { + if err = decodeUnisatResponseV5(*resp, &rawPage); err != nil { return } @@ -109,13 +109,13 @@ func (c *Chain) fetchBrc20UnconfirmedTransferableInscription(owner string, ticke } header := unisatRequestHeader(owner) - url := fmt.Sprintf("%v/wallet-v4/address/inscriptions?address=%v&cursor=%v&size=%v", host, owner, 0, 50) + url := fmt.Sprintf("%v/v5/ordinals/inscriptions?address=%v&cursor=%v&size=%v", host, owner, 0, 50) resp, err := httpUtil.Request(http.MethodGet, url, header, nil) if err != nil { return } var rawPage rawBrc20InscriptionPage - if err = decodeUnisatResponseV4(*resp, &rawPage); err != nil { + if err = decodeUnisatResponseV5(*resp, &rawPage); err != nil { return } @@ -149,7 +149,7 @@ func (c *Chain) fetchBrc20UnconfirmedTransferableInscription(owner string, ticke InscriptionNumber: inscription.InscriptionNumber, Amount: obj.Amt, Ticker: obj.Tick, - Unconfirmed: true, + Confirmations: 0, }, nil }) if err != nil { diff --git a/core/btc/brc20_inscription_test.go b/core/btc/brc20_inscription_test.go index 11dc383..d719ae9 100644 --- a/core/btc/brc20_inscription_test.go +++ b/core/btc/brc20_inscription_test.go @@ -7,7 +7,7 @@ import ( ) func TestFetchBrc20Inscription(t *testing.T) { - owner := "bc1pdq423fm5dv00sl2uckmcve8y3w7guev8ka6qfweljlu23mmsw63qpjc9k7" + owner := "bc1p65yz8hsm3antzdtjzlxd7e4z60ht5reuepk970mu8pgf2acthq5qtk8283" chain, err := NewChainWithChainnet(ChainMainnet) require.Nil(t, err) diff --git a/core/btc/brc20_token.go b/core/btc/brc20_token.go index b47d52e..688ebd5 100644 --- a/core/btc/brc20_token.go +++ b/core/btc/brc20_token.go @@ -148,13 +148,13 @@ func (c *Chain) FetchBrc20TokenBalance(owner string, cursor string, pageSize int } header := unisatRequestHeader(owner) - url := fmt.Sprintf("%v/wallet-v4/brc20/tokens?address=%v&cursor=%v&size=%v", host, owner, offset, pageSize) + url := fmt.Sprintf("%v/v5/brc20/list?address=%v&cursor=%v&size=%v", host, owner, offset, pageSize) resp, err := httpUtil.Request(http.MethodGet, url, header, nil) if err != nil { return } var rawPage rawBrc20TokenBalancePage - if err = decodeUnisatResponseV4(*resp, &rawPage); err != nil { + if err = decodeUnisatResponseV5(*resp, &rawPage); err != nil { return } @@ -178,12 +178,12 @@ func (c *Chain) fetchTokenSummary(owner, ticker string) (summary *unisatTokenSum } header := unisatRequestHeader(owner) - url := fmt.Sprintf("%v/wallet-v4/brc20/token-summary?address=%v&ticker=%v", host, owner, ticker) + url := fmt.Sprintf("%v/v5/brc20/token-summary?address=%v&ticker=%v", host, owner, ticker) resp, err := httpUtil.Request(http.MethodGet, url, header, nil) if err != nil { return } - if err = decodeUnisatResponseV4(*resp, &summary); err != nil { + if err = decodeUnisatResponseV5(*resp, &summary); err != nil { return } return summary, nil diff --git a/core/btc/brc20_token_test.go b/core/btc/brc20_token_test.go index 1368ff5..787f909 100644 --- a/core/btc/brc20_token_test.go +++ b/core/btc/brc20_token_test.go @@ -44,5 +44,5 @@ func TestBrc20TokenBalances(t *testing.T) { require.Nil(t, err) balancePage, err := chain.FetchBrc20TokenBalance(owner, "0", 10) require.Nil(t, err) - t.Log(balancePage.Items) + t.Log(balancePage.JsonString()) } diff --git a/core/btc/brc20_types.go b/core/btc/brc20_types.go index 645bd9a..03b72fc 100644 --- a/core/btc/brc20_types.go +++ b/core/btc/brc20_types.go @@ -30,10 +30,14 @@ func (p *unisatRawPage[T]) MapToSdkPage(offset, size int) *inter.SdkPageable[T] // - MARK - type Brc20TokenBalance struct { - Ticker string `json:"ticker"` //: "zbit", - OverallBalance string `json:"overallBalance"` //: "0", - TransferableBalance string `json:"transferableBalance"` //: "0", - AvailableBalance string `json:"availableBalance"` //: "0" + Ticker string `json:"ticker"` //: "zbit", + OverallBalance string `json:"overallBalance"` //: "0", + TransferableBalance string `json:"transferableBalance"` //: "0", + AvailableBalanceSafe string `json:"availableBalanceSafe"` //: "0" +} + +func (j *Brc20TokenBalance) AvailableBalance() string { + return j.AvailableBalanceSafe } func (j *Brc20TokenBalance) JsonString() (*base.OptionalString, error) { @@ -141,11 +145,15 @@ func (bp *Brc20InscriptionPage) AsNFTPage() *NFTPage { } type Brc20TransferableInscription struct { + Confirmations int64 `json:"confirmations"` InscriptionNumber int64 `json:"inscriptionNumber"` InscriptionId string `json:"inscriptionId"` Amount string `json:"amount"` Ticker string `json:"ticker"` - Unconfirmed bool `json:"unconfirmed,omitempty"` +} + +func (j *Brc20TransferableInscription) Unconfirmed() bool { + return j.Confirmations <= 0 } func (j *Brc20TransferableInscription) JsonString() (*base.OptionalString, error) { diff --git a/core/btc/brc20_util.go b/core/btc/brc20_util.go index 41ab2db..1a8dec3 100644 --- a/core/btc/brc20_util.go +++ b/core/btc/brc20_util.go @@ -10,8 +10,8 @@ import ( "github.com/coming-chat/wallet-SDK/pkg/httpUtil" ) -// the v2 resp.Body should like `{code: *, msg: *, data: *}` -func decodeUnisatResponseV2(resp httpUtil.Res, out interface{}) error { +// the v5 resp.Body should like `{code: *, msg: *, data: *}` +func decodeUnisatResponseV5(resp httpUtil.Res, out interface{}) error { err := responseJsonCheck(resp) if err != nil { return err @@ -76,9 +76,9 @@ func responseJsonCheck(resp httpUtil.Res) error { func unisatRequestHeader(address string) map[string]string { return map[string]string{ - "X-Client": "UniSat Wallet", - "X-Version": "1.1.33", + "x-client": "UniSat Wallet", + "x-version": "1.5.4", "x-address": address, - "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36", + "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36", } } diff --git a/core/btc/chainnet.go b/core/btc/chainnet.go index 34dcc42..2335250 100644 --- a/core/btc/chainnet.go +++ b/core/btc/chainnet.go @@ -102,9 +102,9 @@ func comingOrdHost(chainnet string) (string, error) { func unisatHost(chainnet string) (string, error) { switch chainnet { case ChainMainnet, ChainBitcoin: - return "https://api.unisat.io", nil + return "https://wallet-api.unisat.io", nil case ChainTestnet: - return "https://api-testnet.unisat.io", nil + return "https://wallet-api-testnet.unisat.io", nil } return "", ErrUnsupportedChain }