Skip to content

Commit

Permalink
feat: add BKSNFTFetcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhangguiguang committed Jan 24, 2024
1 parent 141de60 commit 6d777e1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
16 changes: 8 additions & 8 deletions core/eth/blockscout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ func TestBlockScout_NFT(t *testing.T) {
owner := "0x30b31174e5FEd4598aA0dF3191EbAA5AAb48d43E" // nft very much
// owner := "0x9dE416AB881b322eE0b4c189C2dE624090280cF2" // nft few

showPage := func(p *BKSNFTPage) {
println("item count: ", p.Count())
f := p.ValueAt(0).ToBaseNFT()
println("first item: ", f.Id, f.Name, f.Image)
println("has next page: ", p.HasNextPage())
println("====================")
}

page1, err := api.Nft(owner, nil)
require.Nil(t, err)
showPage(page1)
Expand All @@ -28,3 +20,11 @@ func TestBlockScout_NFT(t *testing.T) {
require.Nil(t, err)
showPage(page2)
}

func showPage(p *BKSNFTPage) {
println("item count: ", p.Count())
f := p.ValueAt(0).ToBaseNFT()
println("first item: ", f.Id, f.Name, f.Image)
println("has next page: ", p.HasNextPage())
println("====================")
}
36 changes: 36 additions & 0 deletions core/eth/nft.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,39 @@ func (f *RSS3Fetcher) FetchNFTsJsonString(owner string) (*base.OptionalString, e
}
return &base.OptionalString{Value: string(bytes)}, nil
}

type BKSNFTFetcher struct {
ApiUrl string
Owner string

nextPage *BKSPageParams
quired bool // 用来区分, 没有 nextPage 时, 是第一页还是最后一页
}

func NewBKSNFTFetcher(url string, owner string) *BKSNFTFetcher {
return &BKSNFTFetcher{
ApiUrl: url,
Owner: owner,
}
}

func (f *BKSNFTFetcher) HasNextPage() bool {
return !f.quired || (f.nextPage != nil && f.nextPage.Raw != nil)
}

func (f *BKSNFTFetcher) ResetPage() {
f.nextPage = nil
f.quired = false
}

func (f *BKSNFTFetcher) FetchNextPage() (*BKSNFTPage, error) {
if !f.HasNextPage() {
return nil, nil
}
p, err := NewBlockScout(f.ApiUrl).Nft(f.Owner, f.nextPage)
if err != nil {
return nil, err
}
f.nextPage = p.NextPage_
return p, err
}
21 changes: 21 additions & 0 deletions core/eth/nft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package eth

import (
"encoding/json"
"reflect"
"strconv"
"testing"

Expand Down Expand Up @@ -94,3 +95,23 @@ func TestBEVMjosjo(t *testing.T) {
println("item_count:", resp.Next_page_params.ItemsCount)
println("value:", resp.Next_page_params.Value)
}

func TestBKSNFTFetcher_FetchNextPage(t *testing.T) {
owner := "0x30b31174e5FEd4598aA0dF3191EbAA5AAb48d43E" // nft very much
// owner := "0x9dE416AB881b322eE0b4c189C2dE624090280cF2" // nft few
fetcher := NewBKSNFTFetcher(BlockScoutURLEth, owner)

page1, err := fetcher.FetchNextPage()
require.Nil(t, err)
showPage(page1)

page2, err := fetcher.FetchNextPage()
require.Nil(t, err)
showPage(page2)

fetcher.ResetPage()
page1re, err := fetcher.FetchNextPage()
require.Nil(t, err)
showPage(page1re)
require.True(t, reflect.DeepEqual(page1, page1re))
}

0 comments on commit 6d777e1

Please sign in to comment.