-
Notifications
You must be signed in to change notification settings - Fork 0
/
harvest_key_checker.go
69 lines (55 loc) · 1.73 KB
/
harvest_key_checker.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
package setup
import (
"context"
"log"
"time"
"github.com/proximax-storage/go-xpx-chain-sdk/sdk"
"github.com/proximax-storage/go-xpx-chain-sdk/sdk/websocket"
sync "github.com/proximax-storage/go-xpx-chain-sync"
)
type HarvestKeyChecker struct {
Client *sdk.Client
}
func NewHarvestKeyChecker(client *sdk.Client) *HarvestKeyChecker {
return &HarvestKeyChecker{
Client: client,
}
}
func (h *HarvestKeyChecker) IsRegistered(ctx context.Context, harvestKey string) bool {
harvesterAccount, err := h.Client.NewAccountFromPrivateKey(harvestKey)
if err != nil {
log.Fatal("Error creating harvester account: ", err)
}
harvester, err := h.Client.Account.GetAccountHarvesting(ctx, harvesterAccount.Address)
if err != nil {
log.Fatal("Error requesting harvesting info: ", err)
}
return harvester != nil
}
func (h *HarvestKeyChecker) Register(ctx context.Context, cfg *sdk.Config, harvestKey string, signerPrivateKey string) {
signerAccount, err := h.Client.NewAccountFromPrivateKey(signerPrivateKey)
if err != nil {
log.Fatal("Error creating signer account: ", err)
}
harvesterAccount, err := h.Client.NewAccountFromPublicKey(harvestKey)
if err != nil {
log.Fatal("Error creating harvester account: ", err)
}
ws, err := websocket.NewClient(ctx, cfg)
if err != nil {
log.Fatal("Error creating websocket client: ", err)
}
htx, err := h.Client.NewHarvesterTransaction(
sdk.NewDeadline(time.Hour),
sdk.AddHarvester,
harvesterAccount,
)
res, err := sync.Announce(ctx, cfg, ws, signerAccount, htx)
if err != nil {
log.Fatal("Error announcing AddHarvesterTransaction: ", err)
}
if res.Err() != nil {
log.Fatal("Error confirming AddHarvesterTransaction: ", res.Err())
}
log.Println("Harvest key registered successfully")
}