Skip to content

Commit 1d3981d

Browse files
TzKT API: some methods
1 parent 41837d6 commit 1d3981d

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

tzkt/api/api.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"io"
78
"io/ioutil"
89
"net/http"
910
"net/url"
11+
"os"
1012
"path"
1113
"strconv"
1214
"time"
@@ -117,11 +119,10 @@ func (tzkt *API) json(ctx context.Context, endpoint string, args map[string]stri
117119
case http.StatusNoContent:
118120
return nil
119121
default:
120-
body, err := ioutil.ReadAll(resp.Body)
121-
if err != nil {
122+
if _, err := io.Copy(os.Stdout, resp.Body); err != nil {
122123
return err
123124
}
124-
return errors.New(fmt.Sprintf("%s: %s %s %v", resp.Status, string(body), endpoint, args))
125+
return errors.New(fmt.Sprintf("%s: %s %v", resp.Status, endpoint, args))
125126
}
126127
}
127128

tzkt/api/data.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ const (
2222
KindRegisterGlobalConstant = "register_constant"
2323
)
2424

25+
// urls
26+
const (
27+
BaseURL = "https://api.tzkt.io"
28+
)
29+
2530
// Operation -
2631
type Operation struct {
2732
ID uint64 `json:"id" mapstructure:"id"`

tzkt/api/operations.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ package api
22

33
import (
44
"context"
5+
"fmt"
6+
7+
"github.com/dipdup-net/go-lib/tools"
8+
"github.com/pkg/errors"
59
)
610

711
// GetEndorsements -
@@ -81,3 +85,18 @@ func (tzkt *API) GetMigrations(ctx context.Context, filters map[string]string) (
8185
err = tzkt.json(ctx, "/v1/operations/migrations", filters, false, &operations)
8286
return
8387
}
88+
89+
// GetOperationsByHash -
90+
func (tzkt *API) GetOperationsByHash(ctx context.Context, hash string, filters map[string]string) (operations []Operation, err error) {
91+
err = tzkt.json(ctx, fmt.Sprintf("/v1/operations/%s", hash), filters, false, &operations)
92+
return
93+
}
94+
95+
// GetTransactionsByHash -
96+
func (tzkt *API) GetTransactionsByHash(ctx context.Context, hash string, filters map[string]string) (operations []Transaction, err error) {
97+
if !tools.IsOperationHash(hash) {
98+
return nil, errors.Errorf("invalid operation hash: %s", hash)
99+
}
100+
err = tzkt.json(ctx, fmt.Sprintf("/v1/operations/transactions/%s", hash), filters, false, &operations)
101+
return
102+
}

0 commit comments

Comments
 (0)