Skip to content

Commit

Permalink
Support bigmap update fetching (#9)
Browse files Browse the repository at this point in the history
* support fetch bigmap updates by level
  • Loading branch information
hvthhien authored Sep 26, 2023
1 parent 09d7d25 commit 9cdcad8
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions bigmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,30 @@ import (
"net/http"
"net/url"
"strings"
"time"
)

type BigmapUpdate struct {
ID uint64 `json:"id"`
Level uint64 `json:"level"`
Timestamp time.Time `json:"timestamp"`
Bigmap uint64 `json:"bigmap"`
Contract Account `json:"contract"`
Path string `json:"path"`
Action string `json:"action"`
Content Content `json:"content"`
}

type Content struct {
Hash string `json:"hash"`
Key string `json:"key"`
Value BigmapValue `json:"value"`
}

type BigmapValue struct {
TokenID string `json:"token_id"`
}

// GetBigMapValueByPointer returns the value of a key in a bigmap.
func (c *TZKT) GetBigMapValueByPointer(pointer int, key string) ([]byte, error) {
u := url.URL{
Expand Down Expand Up @@ -116,3 +138,38 @@ func (c *TZKT) GetBigMapPointerForContractTokenMetadata(contract string) (int, e

return pointers[0], nil
}

// GetBigmapUpdatesByLevel returns the bigmap updates of given tags
func (c *TZKT) GetTokenMetadataBigmapUpdatesByLevel(level string, offset, limit int) ([]BigmapUpdate, error) {
if limit == 0 {
limit = 100
}

v := url.Values{
"level.ge": []string{level},
"sort": []string{"level"},
"offset": []string{fmt.Sprint(offset)},
"limit": []string{fmt.Sprint(limit)},
"tags.any": []string{"token_metadata"},
}

u := url.URL{
Scheme: "https",
Host: c.endpoint,
Path: "/v1/bigmaps/updates",
RawQuery: v.Encode(),
}

var bigmaps []BigmapUpdate

req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}

if err := c.request(req, &bigmaps); err != nil {
return nil, err
}

return bigmaps, nil
}

0 comments on commit 9cdcad8

Please sign in to comment.