-
Notifications
You must be signed in to change notification settings - Fork 5
/
snapshot.go
121 lines (93 loc) Β· 3.26 KB
/
snapshot.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package cmd
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"os"
"github.com/benleb/gloomberg/internal/ticker"
"github.com/benleb/gloomberg/internal/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var snapshotAddress string
// generateCmd represents the generate command.
var snapshotCmd = &cobra.Command{
Use: "snapshot",
Short: "Generate snapshot of wallets",
Long: `Generate snapshot of wallets using third API provider.`,
Run: func(_ *cobra.Command, args []string) {
if len(args) < 1 && snapshotAddress == "" {
fmt.Println("β missing argument: contract address")
return
}
var contract string
if len(args) < 1 {
contract = snapshotAddress
} else {
// get first argument
contract = args[0]
}
// check if argument is given
if snapshotAddress != "" {
contract = snapshotAddress
}
viper.Set("log.logFile", "/tmp/gloomberg-generate.log")
viper.Set("log.verbose", true)
fmt.Println("--")
fmt.Printf("π getOwnersForCollection from alchemy Β· contract: %s\n", contract)
apikey := viper.GetString("api_keys.alchemy")
// check if apikey is set
if apikey == "" {
fmt.Printf("β missing alchemy apikey, check .yaml config\n")
return
}
url := "https://eth-mainnet.g.alchemy.com/nft/v2/" + apikey + "/getOwnersForCollection?contractAddress=" + contract
response, err := utils.HTTP.GetWithTLS12(context.TODO(), url)
if err != nil {
if os.IsTimeout(err) {
fmt.Printf("βοΈ getContractMetadata from alchemy Β· timeout while fetching: %+v\n", err.Error())
} else {
fmt.Printf("β getContractMetadata from alchemy Β· error: %+v\n", err.Error())
}
return
}
defer response.Body.Close()
// read the response body
responseBody, err := io.ReadAll(response.Body)
if err != nil {
fmt.Printf("β getContractMetadata from alchemy Β· response read error: %+v\n", err.Error())
return
}
// decode the data
var owners *ticker.GetOwnersForCollectionResponse
if err := json.NewDecoder(bytes.NewReader(responseBody)).Decode(&owners); err != nil {
fmt.Printf("β decode error: %s\n", err.Error())
return
}
fmt.Printf("π %d owners found\n", len(owners.OwnerAddresses))
// save struct as json
jsonString, err := json.Marshal(owners)
if err != nil {
fmt.Printf("β decode error: %s\n", err.Error())
}
// write jsonString to file
fileName := "./wallets/" + contract + ".json"
fmt.Printf("π writing file: %s\n", fileName)
err = os.WriteFile(fileName, jsonString, 0o644) //nolint:gosec
if err != nil {
fmt.Printf("β writing file error: %s\n", err.Error())
}
},
}
func init() {
rootCmd.AddCommand(snapshotCmd)
// intentionally called the flag "snapshot.alchemy.key" to make the difference between the flag and the viper key clear
// (usually the flag and the viper key have the same name to avoid confusion)
// snapshotCmd.Flags().StringVar(&apikey, "alchemy.key", "", "alchemy api key")
// bind the cobra/pflags flag "alchemy.key" to the viper key "alchemy.apiKey
// _ = viper.BindPFlag("alchemy.apiKey", snapshotCmd.Flags().Lookup("alchemy.key"))
// we do not need to bind the address flag to a viper key because we do not need it anywhere else
snapshotCmd.Flags().StringVar(&snapshotAddress, "address", "", "contract address to snapshot")
}