diff --git a/core/eth/blockscout_test.go b/core/eth/blockscout_test.go index c791b3b..897a545 100644 --- a/core/eth/blockscout_test.go +++ b/core/eth/blockscout_test.go @@ -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) @@ -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("====================") +} diff --git a/core/eth/nft.go b/core/eth/nft.go index 673c281..a76047b 100644 --- a/core/eth/nft.go +++ b/core/eth/nft.go @@ -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 +} diff --git a/core/eth/nft_test.go b/core/eth/nft_test.go index 2321107..69c14c2 100644 --- a/core/eth/nft_test.go +++ b/core/eth/nft_test.go @@ -2,6 +2,7 @@ package eth import ( "encoding/json" + "reflect" "strconv" "testing" @@ -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)) +}