This repository has been archived by the owner on Jul 21, 2023. It is now read-only.
/
viewer.go
199 lines (173 loc) · 4.4 KB
/
viewer.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package viewer
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"sync"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/crypto"
"github.com/ava-labs/hypersdk/utils"
"github.com/ava-labs/hypersdk/vm"
"github.com/ava-labs/indexvm/auth"
iclient "github.com/ava-labs/indexvm/client"
"github.com/ava-labs/indexvm/examples/nftdisco/thegraph"
"github.com/ava-labs/indexvm/examples/shared/client"
"github.com/ava-labs/indexvm/examples/shared/ipfs"
iutils "github.com/ava-labs/indexvm/utils"
"github.com/inancgumus/screen"
icore "github.com/ipfs/interface-go-ipfs-core"
"golang.org/x/sync/errgroup"
)
type Viewer struct {
pending int
priv crypto.PrivateKey
auth chain.AuthFactory
addr string
icli *iclient.Client
cli *client.Client
scli *vm.DecisionRPCClient
ipfsAPI icore.CoreAPI
db database.Database
filters []*filter
filtersL sync.Mutex
nfts []*thegraph.NFT // don't need to lock
autoRateLogs chan string // only log when processing
recs chan *rec
txc chan *pendingTx
pendingL sync.Mutex
pendingM map[ids.ID]*pendingTx
seenL sync.Mutex
seenM map[string]struct{}
issued int
}
func Run(
ctx context.Context,
priv crypto.PrivateKey,
db database.Database,
ipfsDir string,
servicerEndpoint string,
indexEndpoint string,
pending int,
) error {
v := &Viewer{
priv: priv,
auth: auth.NewDirectFactory(priv),
pending: pending,
db: db,
filters: []*filter{},
nfts: []*thegraph.NFT{},
autoRateLogs: make(chan string, 128),
recs: make(chan *rec),
txc: make(chan *pendingTx),
pendingM: map[ids.ID]*pendingTx{},
seenM: map[string]struct{}{},
}
// Load filters
lfilters, err := LoadFilters(ctx, v.db)
if err != nil {
return err
}
v.filters = lfilters
utils.Outf("{{yellow}}loaded filters:{{/}} %d\n", len(v.filters))
// Load saved NFTs
lnfts, err := LoadNFTs(ctx, v.db)
if err != nil {
return err
}
v.nfts = lnfts
utils.Outf("{{yellow}}loaded nfts:{{/}} %d\n", len(v.nfts))
// Create IPFS node
ipfsAPI, ipfsNode, err := ipfs.New(ctx, ipfsDir)
if err != nil {
utils.Outf("{{red}}unable to start IPFS node:{{/}}\n")
return err
}
defer ipfsNode.Close()
v.ipfsAPI = ipfsAPI
// Create recommendation client
c := client.New(servicerEndpoint, priv)
v.cli = c
// Create indexvm client
icli := iclient.New(indexEndpoint)
v.icli = icli
port, err := icli.DecisionsPort(ctx)
if err != nil {
return err
}
host, err := utils.GetHost(indexEndpoint)
if err != nil {
return err
}
scli, err := vm.NewDecisionRPCClient(fmt.Sprintf("%s:%d", host, port))
if err != nil {
return err
}
defer scli.Close()
v.scli = scli
// Orient user
screen.Clear()
screen.MoveTopLeft()
// Get balance
pk := priv.PublicKey()
addr := iutils.Address(pk)
v.addr = addr
unlocked, _, _, err := icli.Balance(ctx, addr)
if err != nil {
return err
}
utils.Outf("{{green}}loaded identity:{{/}} %s\n", addr)
utils.Outf("{{green}}loaded balance:{{/}} %s\n", utils.FormatBalance(unlocked))
utils.Outf("{{green}}started IPFS node:{{/}} %s\n\n", ipfsNode.Identity.String())
localAddrs, err := ipfsAPI.Swarm().LocalAddrs(ctx)
if err != nil {
return err
}
utils.Outf("{{yellow}}local addrs:{{/}} %+v\n", localAddrs)
listenAddrs, err := ipfsAPI.Swarm().ListenAddrs(ctx)
if err != nil {
return err
}
utils.Outf("{{yellow}}listen addrs:{{/}} %+v\n", listenAddrs)
utils.Outf("\n")
// Wait for a balance before starting (recommender won't allow)
if err := icli.WaitForBalance(ctx, addr, 10_000); err != nil {
return err
}
g, gctx := errgroup.WithContext(ctx)
// Catch interrupt for smooth exit
cl := make(chan os.Signal, 1)
signal.Notify(cl, os.Interrupt)
g.Go(func() error {
select {
case <-gctx.Done():
return nil
case <-cl:
return context.Canceled
}
})
g.Go(func() error {
return v.runUI(gctx)
})
g.Go(func() error {
return v.provideRecommendations(gctx)
})
// Process transactions
g.Go(func() error {
return v.broadcastTransactions(gctx)
})
// Confirm and/or retry transactions
g.Go(func() error {
return v.confirmTransactions(gctx)
})
// Wait for exit or error
if err := g.Wait(); !errors.Is(err, context.Canceled) {
return err
}
return nil
}