/
new.go
61 lines (57 loc) · 1.53 KB
/
new.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Request info from blockscan.
//
// Example:
//
// // To get the balance of address "0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7"
// scanner, err:=blockscan.New("avalanche","")
// if err != nil { return err }
// res, err := scanner.GetBalance("0xb31f66aa3c1e785363f0875a1b74e27b85fd66c7")
// if err != nil { return err }
// fmt.Println(res)
package blockscan
import (
"errors"
"fmt"
"github.com/0xVanfer/blockscan/internal/constants"
)
type Scanner struct {
UrlHead string // Url head based on network.
ApiKey string // User's api key, length should be 34.
}
// Create a new scanner.
//
// "network" should be the full name of the chain,
// such as "ethereum", "avalanche", etc.
//
// "apiKey" is the key for blockscan requests.
// When the key is "", default key will be used.
// Otherwise, the key length should be 34.
//
// To get an apikey on ethereum, visit
// https://docs.etherscan.io/getting-started/viewing-api-usage-statistics
func New(network string, apiKey string) (*Scanner, error) {
var urlHead string
var defaultKey string
for n, info := range constants.BlockscanCnf {
if n == network {
urlHead = info.URLHead
defaultKey = info.APIKey
break
}
}
if urlHead == "" {
return nil, errors.New("network not supported")
}
if apiKey == "" {
apiKey = defaultKey
fmt.Println("You do not have a blockscan api key. Unecpected errors may occur when running.")
}
if len(apiKey) != 34 {
err := errors.New("api key length should be 34")
return nil, err
}
return &Scanner{
UrlHead: urlHead,
ApiKey: apiKey,
}, nil
}