Skip to content

Commit

Permalink
update: unisat brc20 v5 api.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhangguiguang committed Feb 12, 2025
1 parent efca372 commit cd85d7c
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 23 deletions.
10 changes: 5 additions & 5 deletions core/btc/brc20_inscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion core/btc/brc20_inscription_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestFetchBrc20Inscription(t *testing.T) {
owner := "bc1pdq423fm5dv00sl2uckmcve8y3w7guev8ka6qfweljlu23mmsw63qpjc9k7"
owner := "bc1p65yz8hsm3antzdtjzlxd7e4z60ht5reuepk970mu8pgf2acthq5qtk8283"

chain, err := NewChainWithChainnet(ChainMainnet)
require.Nil(t, err)
Expand Down
8 changes: 4 additions & 4 deletions core/btc/brc20_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/btc/brc20_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
18 changes: 13 additions & 5 deletions core/btc/brc20_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions core/btc/brc20_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
}
}
4 changes: 2 additions & 2 deletions core/btc/chainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit cd85d7c

Please sign in to comment.